Skip to content

Commit 5a4e93f

Browse files
Added unit test to assert cache IDs for different ACL roles must not be equal
1 parent 61f847e commit 5a4e93f

File tree

1 file changed

+90
-1
lines changed
  • app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier

1 file changed

+90
-1
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Framework\UrlInterface;
1515
use Magento\Store\Model\Store;
1616
use Magento\Framework\AuthorizationInterface;
17+
use Magento\Backend\Model\Auth\Session;
18+
use Magento\Authorization\Model\Role;
19+
use Magento\User\Model\User;
1720

1821
/**
1922
* Class CategoriesTest
@@ -52,6 +55,11 @@ class CategoriesTest extends AbstractModifierTest
5255
*/
5356
private $authorizationMock;
5457

58+
/**
59+
* @var \Magento\Backend\Model\Auth\Session|\PHPUnit_Framework_MockObject_MockObject
60+
*/
61+
private $sessionMock;
62+
5563
protected function setUp()
5664
{
5765
parent::setUp();
@@ -73,6 +81,10 @@ protected function setUp()
7381
$this->authorizationMock = $this->getMockBuilder(AuthorizationInterface::class)
7482
->disableOriginalConstructor()
7583
->getMock();
84+
$this->sessionMock = $this->getMockBuilder(Session::class)
85+
->setMethods(['getUser'])
86+
->disableOriginalConstructor()
87+
->getMock();
7688

7789
$this->categoryCollectionFactoryMock->expects($this->any())
7890
->method('create')
@@ -89,6 +101,26 @@ protected function setUp()
89101
$this->categoryCollectionMock->expects($this->any())
90102
->method('getIterator')
91103
->willReturn(new \ArrayIterator([]));
104+
105+
$roleAdmin = $this->getMockBuilder(Role::class)
106+
->setMethods(['getId'])
107+
->disableOriginalConstructor()
108+
->getMock();
109+
$roleAdmin->expects($this->any())
110+
->method('getId')
111+
->willReturn(0);
112+
113+
$userAdmin = $this->getMockBuilder(User::class)
114+
->setMethods(['getRole'])
115+
->disableOriginalConstructor()
116+
->getMock();
117+
$userAdmin->expects($this->any())
118+
->method('getRole')
119+
->willReturn($roleAdmin);
120+
121+
$this->sessionMock->expects($this->any())
122+
->method('getUser')
123+
->willReturn($userAdmin);
92124
}
93125

94126
/**
@@ -102,11 +134,28 @@ protected function createModel()
102134
'locator' => $this->locatorMock,
103135
'categoryCollectionFactory' => $this->categoryCollectionFactoryMock,
104136
'arrayManager' => $this->arrayManagerMock,
105-
'authorization' => $this->authorizationMock
137+
'authorization' => $this->authorizationMock,
138+
'session' => $this->sessionMock
106139
]
107140
);
108141
}
109142

143+
/**
144+
* @param object $object
145+
* @param string $method
146+
* @param array $args
147+
* @return mixed
148+
* @throws \ReflectionException
149+
*/
150+
private function invokeMethod($object, $method, $args = [])
151+
{
152+
$class = new \ReflectionClass(Categories::class);
153+
$method = $class->getMethod($method);
154+
$method->setAccessible(true);
155+
156+
return $method->invokeArgs($object, $args);
157+
}
158+
110159
public function testModifyData()
111160
{
112161
$this->assertSame([], $this->getModel()->modifyData([]));
@@ -177,4 +226,44 @@ public function modifyMetaLockedDataProvider()
177226
{
178227
return [[true], [false]];
179228
}
229+
230+
/**
231+
* Asserts that a user with an ACL role ID of 0 and a user with an ACL role ID of 1 do not have the same cache IDs
232+
* Assumes a store ID of 0
233+
*
234+
* @throws \ReflectionException
235+
*/
236+
public function testAclCacheIds()
237+
{
238+
$categoriesAdmin = $this->createModel();
239+
$cacheIdAdmin = $this->invokeMethod($categoriesAdmin, 'getCategoriesTreeCacheId', [0]);
240+
241+
$roleAclUser = $this->getMockBuilder(Role::class)
242+
->disableOriginalConstructor()
243+
->getMock();
244+
$roleAclUser->expects($this->any())
245+
->method('getId')
246+
->willReturn(1);
247+
248+
$userAclUser = $this->getMockBuilder(User::class)
249+
->disableOriginalConstructor()
250+
->getMock();
251+
$userAclUser->expects($this->any())
252+
->method('getRole')
253+
->will($this->returnValue($roleAclUser));
254+
255+
$this->sessionMock = $this->getMockBuilder(Session::class)
256+
->setMethods(['getUser'])
257+
->disableOriginalConstructor()
258+
->getMock();
259+
260+
$this->sessionMock->expects($this->any())
261+
->method('getUser')
262+
->will($this->returnValue($userAclUser));
263+
264+
$categoriesAclUser = $this->createModel();
265+
$cacheIdAclUser = $this->invokeMethod($categoriesAclUser, 'getCategoriesTreeCacheId', [0]);
266+
267+
$this->assertNotEquals($cacheIdAdmin, $cacheIdAclUser);
268+
}
180269
}

0 commit comments

Comments
 (0)