Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AclRetrieverTest.php
Go to the documentation of this file.
1 <?php
8 
10 
12 use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
14 use Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory as RulesCollectionFactory;
17 
21 class AclRetrieverTest extends \PHPUnit\Framework\TestCase
22 {
26  protected $aclRetriever;
27 
29  protected $roleMock;
30 
31  protected function setup()
32  {
33  $this->aclRetriever = $this->createAclRetriever();
34  }
35 
37  {
38  $expectedResources = ['anonymous'];
39  $allowedResources = $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_GUEST, null);
40  $this->assertEquals(
41  $expectedResources,
43  'Allowed resources for guests should be \'anonymous\'.'
44  );
45  }
46 
48  {
49  $expectedResources = ['self'];
50  $allowedResources = $this->aclRetriever->getAllowedResourcesByUser(
52  null
53  );
54  $this->assertEquals(
55  $expectedResources,
57  'Allowed resources for customers should be \'self\'.'
58  );
59  }
60 
66  {
67  $this->roleMock->expects($this->once())->method('getId')->will($this->returnValue(null));
68  $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, null);
69  }
70 
72  {
73  $this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(1));
74  $expectedResources = ['Magento_Backend::dashboard', 'Magento_Cms::page'];
75  $this->assertEquals(
76  $expectedResources,
77  $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, 1)
78  );
79  }
80 
84  protected function createAclRetriever()
85  {
86  $this->roleMock = $this->createPartialMock(\Magento\Authorization\Model\Role::class, ['getId', '__wakeup']);
87 
89  $roleCollectionMock = $this->createPartialMock(
90  \Magento\Authorization\Model\ResourceModel\Role\Collection::class,
91  ['setUserFilter', 'getFirstItem']
92  );
93  $roleCollectionMock->expects($this->any())->method('setUserFilter')->will($this->returnSelf());
94  $roleCollectionMock->expects($this->any())->method('getFirstItem')->will($this->returnValue($this->roleMock));
95 
97  $roleCollectionFactoryMock = $this->createPartialMock(
98  \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class,
99  ['create']
100  );
101  $roleCollectionFactoryMock->expects($this->any())->method('create')->will(
102  $this->returnValue($roleCollectionMock)
103  );
104 
106  $rulesMock1 = $this->createPartialMock(
107  \Magento\Authorization\Model\Rules::class,
108  ['getResourceId', '__wakeup']
109  );
110  $rulesMock1->expects($this->any())->method('getResourceId')->will(
111  $this->returnValue('Magento_Backend::dashboard')
112  );
114  $rulesMock2 = $this->createPartialMock(
115  \Magento\Authorization\Model\Rules::class,
116  ['getResourceId', '__wakeup']
117  );
118  $rulesMock2->expects($this->any())->method('getResourceId')->will($this->returnValue('Magento_Cms::page'));
119 
121  $rulesCollectionMock = $this->createPartialMock(
122  \Magento\Authorization\Model\ResourceModel\Rules\Collection::class,
123  ['getByRoles', 'load', 'getItems']
124  );
125  $rulesCollectionMock->expects($this->any())->method('getByRoles')->will($this->returnSelf());
126  $rulesCollectionMock->expects($this->any())->method('load')->will($this->returnSelf());
127  $rulesCollectionMock->expects($this->any())->method('getItems')->will(
128  $this->returnValue([$rulesMock1, $rulesMock2])
129  );
130 
132  $rulesCollectionFactoryMock = $this->createPartialMock(
133  \Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory::class,
134  ['create']
135  );
136  $rulesCollectionFactoryMock->expects($this->any())->method('create')->will(
137  $this->returnValue($rulesCollectionMock)
138  );
139 
141  $aclMock = $this->createPartialMock(\Magento\Framework\Acl::class, ['has', 'isAllowed']);
142  $aclMock->expects($this->any())->method('has')->will($this->returnValue(true));
143  $aclMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
144 
146  $aclBuilderMock = $this->createPartialMock(\Magento\Framework\Acl\Builder::class, ['getAcl']);
147  $aclBuilderMock->expects($this->any())->method('getAcl')->will($this->returnValue($aclMock));
148 
149  return new AclRetriever(
150  $aclBuilderMock,
151  $roleCollectionFactoryMock,
152  $rulesCollectionFactoryMock,
153  $this->createMock(\Psr\Log\LoggerInterface::class)
154  );
155  }
156 }
$allowedResources
Definition: get.php:17