WebapiRoleLocatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Test\Unit\Model;
  7. use Magento\Authorization\Model\ResourceModel\Role\Collection as RoleCollection;
  8. use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
  9. use Magento\Authorization\Model\Role;
  10. use Magento\Authorization\Model\UserContextInterface;
  11. class WebapiRoleLocatorTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Webapi\Model\WebapiRoleLocator
  15. */
  16. protected $locator;
  17. /**
  18. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  19. */
  20. protected $objectManager;
  21. /**
  22. * @var UserContextInterface
  23. */
  24. protected $userContext;
  25. /**
  26. * @var RoleCollectionFactory
  27. */
  28. protected $roleCollectionFactory;
  29. /**
  30. * @var RoleCollection
  31. */
  32. protected $roleCollection;
  33. /**
  34. * @var Role
  35. */
  36. protected $role;
  37. protected function setUp()
  38. {
  39. $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $userId = 'userId';
  41. $userType = 'userType';
  42. $this->userContext = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
  43. ->disableOriginalConstructor()
  44. ->setMethods(['getUserId', 'getUserType'])
  45. ->getMock();
  46. $this->userContext->expects($this->once())
  47. ->method('getUserId')
  48. ->will($this->returnValue($userId));
  49. $this->userContext->expects($this->once())
  50. ->method('getUserType')
  51. ->will($this->returnValue($userType));
  52. $this->roleCollectionFactory = $this->getMockBuilder(
  53. \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class
  54. )->disableOriginalConstructor()->setMethods(['create'])->getMock();
  55. $this->roleCollection = $this->getMockBuilder(\Magento\Authorization\Model\ResourceModel\Role\Collection::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['setUserFilter', 'getFirstItem'])
  58. ->getMock();
  59. $this->roleCollectionFactory->expects($this->once())
  60. ->method('create')
  61. ->will($this->returnValue($this->roleCollection));
  62. $this->roleCollection->expects($this->once())
  63. ->method('setUserFilter')
  64. ->with($userId, $userType)
  65. ->will($this->returnValue($this->roleCollection));
  66. $this->role = $this->getMockBuilder(\Magento\Authorization\Model\Role::class)
  67. ->disableOriginalConstructor()
  68. ->setMethods(['getId', '__wakeup'])
  69. ->getMock();
  70. $this->roleCollection->expects($this->once())
  71. ->method('getFirstItem')
  72. ->will($this->returnValue($this->role));
  73. $this->locator = $this->_objectManager->getObject(
  74. \Magento\Webapi\Model\WebapiRoleLocator::class,
  75. [
  76. 'userContext' => $this->userContext,
  77. 'roleCollectionFactory' => $this->roleCollectionFactory
  78. ]
  79. );
  80. }
  81. public function testNoRoleId()
  82. {
  83. $this->role->expects($this->once())
  84. ->method('getId')
  85. ->will($this->returnValue(null));
  86. $this->assertEquals('', $this->locator->getAclRoleId());
  87. }
  88. public function testGetAclRoleId()
  89. {
  90. $roleId = 9;
  91. $this->role->expects($this->exactly(2))
  92. ->method('getId')
  93. ->will($this->returnValue($roleId));
  94. $this->assertEquals($roleId, $this->locator->getAclRoleId());
  95. }
  96. }