Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebapiRoleLocatorTest.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
13 
14 class WebapiRoleLocatorTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $locator;
20 
24  protected $objectManager;
25 
29  protected $userContext;
30 
35 
39  protected $roleCollection;
40 
44  protected $role;
45 
46  protected function setUp()
47  {
48  $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
49 
50  $userId = 'userId';
51  $userType = 'userType';
52 
53  $this->userContext = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
54  ->disableOriginalConstructor()
55  ->setMethods(['getUserId', 'getUserType'])
56  ->getMock();
57  $this->userContext->expects($this->once())
58  ->method('getUserId')
59  ->will($this->returnValue($userId));
60  $this->userContext->expects($this->once())
61  ->method('getUserType')
62  ->will($this->returnValue($userType));
63 
64  $this->roleCollectionFactory = $this->getMockBuilder(
65  \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class
66  )->disableOriginalConstructor()->setMethods(['create'])->getMock();
67 
68  $this->roleCollection = $this->getMockBuilder(\Magento\Authorization\Model\ResourceModel\Role\Collection::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['setUserFilter', 'getFirstItem'])
71  ->getMock();
72  $this->roleCollectionFactory->expects($this->once())
73  ->method('create')
74  ->will($this->returnValue($this->roleCollection));
75  $this->roleCollection->expects($this->once())
76  ->method('setUserFilter')
77  ->with($userId, $userType)
78  ->will($this->returnValue($this->roleCollection));
79 
80  $this->role = $this->getMockBuilder(\Magento\Authorization\Model\Role::class)
81  ->disableOriginalConstructor()
82  ->setMethods(['getId', '__wakeup'])
83  ->getMock();
84 
85  $this->roleCollection->expects($this->once())
86  ->method('getFirstItem')
87  ->will($this->returnValue($this->role));
88 
89  $this->locator = $this->_objectManager->getObject(
90  \Magento\Webapi\Model\WebapiRoleLocator::class,
91  [
92  'userContext' => $this->userContext,
93  'roleCollectionFactory' => $this->roleCollectionFactory
94  ]
95  );
96  }
97 
98  public function testNoRoleId()
99  {
100  $this->role->expects($this->once())
101  ->method('getId')
102  ->will($this->returnValue(null));
103 
104  $this->assertEquals('', $this->locator->getAclRoleId());
105  }
106 
107  public function testGetAclRoleId()
108  {
109  $roleId = 9;
110 
111  $this->role->expects($this->exactly(2))
112  ->method('getId')
113  ->will($this->returnValue($roleId));
114 
115  $this->assertEquals($roleId, $this->locator->getAclRoleId());
116  }
117 }
$roleId
Definition: webapi_user.php:22