AclRetrieverTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorization\Test\Unit\Model\Acl;
  7. use Magento\Authorization\Model\Acl\AclRetriever;
  8. use Magento\Authorization\Model\ResourceModel\Role\Collection as RoleCollection;
  9. use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
  10. use Magento\Authorization\Model\ResourceModel\Rules\Collection as RulesCollection;
  11. use Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory as RulesCollectionFactory;
  12. use Magento\Authorization\Model\Role;
  13. use Magento\Authorization\Model\UserContextInterface;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class AclRetrieverTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var AclRetriever
  21. */
  22. protected $aclRetriever;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject|Role $roleMock */
  24. protected $roleMock;
  25. protected function setup()
  26. {
  27. $this->aclRetriever = $this->createAclRetriever();
  28. }
  29. public function testGetAllowedResourcesByUserTypeGuest()
  30. {
  31. $expectedResources = ['anonymous'];
  32. $allowedResources = $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_GUEST, null);
  33. $this->assertEquals(
  34. $expectedResources,
  35. $allowedResources,
  36. 'Allowed resources for guests should be \'anonymous\'.'
  37. );
  38. }
  39. public function testGetAllowedResourcesByUserTypeCustomer()
  40. {
  41. $expectedResources = ['self'];
  42. $allowedResources = $this->aclRetriever->getAllowedResourcesByUser(
  43. UserContextInterface::USER_TYPE_CUSTOMER,
  44. null
  45. );
  46. $this->assertEquals(
  47. $expectedResources,
  48. $allowedResources,
  49. 'Allowed resources for customers should be \'self\'.'
  50. );
  51. }
  52. /**
  53. * @expectedException \Magento\Framework\Exception\AuthorizationException
  54. * @expectedExceptionMessage The role wasn't found for the user. Verify the role and try again.
  55. */
  56. public function testGetAllowedResourcesByUserRoleNotFound()
  57. {
  58. $this->roleMock->expects($this->once())->method('getId')->will($this->returnValue(null));
  59. $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, null);
  60. }
  61. public function testGetAllowedResourcesByUser()
  62. {
  63. $this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  64. $expectedResources = ['Magento_Backend::dashboard', 'Magento_Cms::page'];
  65. $this->assertEquals(
  66. $expectedResources,
  67. $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, 1)
  68. );
  69. }
  70. /**
  71. * @return AclRetriever
  72. */
  73. protected function createAclRetriever()
  74. {
  75. $this->roleMock = $this->createPartialMock(\Magento\Authorization\Model\Role::class, ['getId', '__wakeup']);
  76. /** @var \PHPUnit_Framework_MockObject_MockObject|RoleCollection $roleCollectionMock */
  77. $roleCollectionMock = $this->createPartialMock(
  78. \Magento\Authorization\Model\ResourceModel\Role\Collection::class,
  79. ['setUserFilter', 'getFirstItem']
  80. );
  81. $roleCollectionMock->expects($this->any())->method('setUserFilter')->will($this->returnSelf());
  82. $roleCollectionMock->expects($this->any())->method('getFirstItem')->will($this->returnValue($this->roleMock));
  83. /** @var \PHPUnit_Framework_MockObject_MockObject|RoleCollectionFactory $roleCollectionFactoryMock */
  84. $roleCollectionFactoryMock = $this->createPartialMock(
  85. \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class,
  86. ['create']
  87. );
  88. $roleCollectionFactoryMock->expects($this->any())->method('create')->will(
  89. $this->returnValue($roleCollectionMock)
  90. );
  91. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\Rules $rulesMock1 */
  92. $rulesMock1 = $this->createPartialMock(
  93. \Magento\Authorization\Model\Rules::class,
  94. ['getResourceId', '__wakeup']
  95. );
  96. $rulesMock1->expects($this->any())->method('getResourceId')->will(
  97. $this->returnValue('Magento_Backend::dashboard')
  98. );
  99. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\Rules $rulesMock1 */
  100. $rulesMock2 = $this->createPartialMock(
  101. \Magento\Authorization\Model\Rules::class,
  102. ['getResourceId', '__wakeup']
  103. );
  104. $rulesMock2->expects($this->any())->method('getResourceId')->will($this->returnValue('Magento_Cms::page'));
  105. /** @var \PHPUnit_Framework_MockObject_MockObject|RulesCollection $rulesCollectionMock */
  106. $rulesCollectionMock = $this->createPartialMock(
  107. \Magento\Authorization\Model\ResourceModel\Rules\Collection::class,
  108. ['getByRoles', 'load', 'getItems']
  109. );
  110. $rulesCollectionMock->expects($this->any())->method('getByRoles')->will($this->returnSelf());
  111. $rulesCollectionMock->expects($this->any())->method('load')->will($this->returnSelf());
  112. $rulesCollectionMock->expects($this->any())->method('getItems')->will(
  113. $this->returnValue([$rulesMock1, $rulesMock2])
  114. );
  115. /** @var \PHPUnit_Framework_MockObject_MockObject|RulesCollectionFactory $rulesCollectionFactoryMock */
  116. $rulesCollectionFactoryMock = $this->createPartialMock(
  117. \Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory::class,
  118. ['create']
  119. );
  120. $rulesCollectionFactoryMock->expects($this->any())->method('create')->will(
  121. $this->returnValue($rulesCollectionMock)
  122. );
  123. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Acl $aclMock */
  124. $aclMock = $this->createPartialMock(\Magento\Framework\Acl::class, ['has', 'isAllowed']);
  125. $aclMock->expects($this->any())->method('has')->will($this->returnValue(true));
  126. $aclMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
  127. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Acl\Builder $aclBuilderMock */
  128. $aclBuilderMock = $this->createPartialMock(\Magento\Framework\Acl\Builder::class, ['getAcl']);
  129. $aclBuilderMock->expects($this->any())->method('getAcl')->will($this->returnValue($aclMock));
  130. return new AclRetriever(
  131. $aclBuilderMock,
  132. $roleCollectionFactoryMock,
  133. $rulesCollectionFactoryMock,
  134. $this->createMock(\Psr\Log\LoggerInterface::class)
  135. );
  136. }
  137. }