CompositeUserContextTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  7. use \Magento\Authorization\Model\CompositeUserContext;
  8. use Magento\Framework\ObjectManager\Helper\Composite as CompositeHelper;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. class CompositeUserContextTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var CompositeUserContext
  14. */
  15. protected $userContext;
  16. /**
  17. * @var CompositeHelper
  18. */
  19. protected $compositeHelperMock;
  20. /**
  21. * @var ObjectManager
  22. */
  23. protected $objectManager;
  24. protected function setUp()
  25. {
  26. $this->objectManager = new ObjectManager($this);
  27. $this->compositeHelperMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\Helper\Composite::class)
  28. ->disableOriginalConstructor()
  29. ->setMethods(['filterAndSortDeclaredComponents'])
  30. ->getMock();
  31. $this->compositeHelperMock
  32. ->expects($this->any())
  33. ->method('filterAndSortDeclaredComponents')
  34. ->will($this->returnArgument(0));
  35. $this->userContext = $this->objectManager->getObject(
  36. \Magento\Authorization\Model\CompositeUserContext::class,
  37. ['compositeHelper' => $this->compositeHelperMock]
  38. );
  39. }
  40. public function testConstructor()
  41. {
  42. $userContextMock = $this->createUserContextMock();
  43. $contexts = [
  44. [
  45. 'sortOrder' => 10,
  46. 'type' => $userContextMock,
  47. ],
  48. ];
  49. $model = $this->objectManager->getObject(
  50. \Magento\Authorization\Model\CompositeUserContext::class,
  51. ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
  52. );
  53. $this->verifyUserContextIsAdded($model, $userContextMock);
  54. }
  55. public function testGetUserId()
  56. {
  57. $expectedUserId = 1;
  58. $expectedUserType = 'Customer';
  59. $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
  60. ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
  61. $userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
  62. $userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
  63. $contexts = [
  64. [
  65. 'sortOrder' => 10,
  66. 'type' => $userContextMock,
  67. ],
  68. ];
  69. $this->userContext = $this->objectManager->getObject(
  70. \Magento\Authorization\Model\CompositeUserContext::class,
  71. ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
  72. );
  73. $actualUserId = $this->userContext->getUserId();
  74. $this->assertEquals($expectedUserId, $actualUserId, 'User ID is defined incorrectly.');
  75. }
  76. public function testGetUserType()
  77. {
  78. $expectedUserId = 1;
  79. $expectedUserType = 'Customer';
  80. $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
  81. ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
  82. $userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
  83. $userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
  84. $contexts = [
  85. [
  86. 'sortOrder' => 10,
  87. 'type' => $userContextMock,
  88. ],
  89. ];
  90. $this->userContext = $this->objectManager->getObject(
  91. \Magento\Authorization\Model\CompositeUserContext::class,
  92. ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
  93. );
  94. $actualUserType = $this->userContext->getUserType();
  95. $this->assertEquals($expectedUserType, $actualUserType, 'User Type is defined incorrectly.');
  96. }
  97. public function testUserContextCaching()
  98. {
  99. $expectedUserId = 1;
  100. $expectedUserType = 'Customer';
  101. $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
  102. ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
  103. $userContextMock->expects($this->exactly(3))->method('getUserType')
  104. ->will($this->returnValue($expectedUserType));
  105. $userContextMock->expects($this->exactly(3))->method('getUserId')
  106. ->will($this->returnValue($expectedUserId));
  107. $contexts = [
  108. [
  109. 'sortOrder' => 10,
  110. 'type' => $userContextMock,
  111. ],
  112. ];
  113. $this->userContext = $this->objectManager->getObject(
  114. \Magento\Authorization\Model\CompositeUserContext::class,
  115. ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
  116. );
  117. $this->userContext->getUserId();
  118. $this->userContext->getUserId();
  119. $this->userContext->getUserType();
  120. $this->userContext->getUserType();
  121. }
  122. public function testEmptyUserContext()
  123. {
  124. $expectedUserId = null;
  125. $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
  126. ->disableOriginalConstructor()->setMethods(['getUserId'])->getMock();
  127. $userContextMock->expects($this->any())->method('getUserId')
  128. ->will($this->returnValue($expectedUserId));
  129. $contexts = [
  130. [
  131. 'sortOrder' => 10,
  132. 'type' => $userContextMock,
  133. ],
  134. ];
  135. $this->userContext = $this->objectManager->getObject(
  136. \Magento\Authorization\Model\CompositeUserContext::class,
  137. ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
  138. );
  139. $actualUserId = $this->userContext->getUserId();
  140. $this->assertEquals($expectedUserId, $actualUserId, 'User ID is defined incorrectly.');
  141. }
  142. /**
  143. * @param int|null $userId
  144. * @param string|null $userType
  145. * @return \PHPUnit_Framework_MockObject_MockObject
  146. */
  147. protected function createUserContextMock($userId = null, $userType = null)
  148. {
  149. $useContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
  150. ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
  151. if ($userId !== null && $userType !== null) {
  152. $useContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
  153. $useContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
  154. }
  155. return $useContextMock;
  156. }
  157. /**
  158. * @param CompositeUserContext $model
  159. * @param CompositeUserContext $userContextMock
  160. */
  161. protected function verifyUserContextIsAdded($model, $userContextMock)
  162. {
  163. $userContext = new \ReflectionProperty(
  164. \Magento\Authorization\Model\CompositeUserContext::class,
  165. 'userContexts'
  166. );
  167. $userContext->setAccessible(true);
  168. $values = $userContext->getValue($model);
  169. $this->assertCount(1, $values, 'User context is not registered.');
  170. $this->assertEquals($userContextMock, $values[0], 'User context is registered incorrectly.');
  171. }
  172. }