SessionTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Unit test for session \Magento\Customer\Model\Session
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Test\Unit\Model;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class SessionTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $_storageMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $_eventManagerMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_httpContextMock;
  26. /**
  27. * @var \Magento\Framework\UrlFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $urlFactoryMock;
  30. /**
  31. * @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $customerFactoryMock;
  34. /**
  35. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $customerRepositoryMock;
  38. /**
  39. * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $responseMock;
  42. /**
  43. * @var \Magento\Customer\Model\Session
  44. */
  45. protected $_model;
  46. /**
  47. * @return void
  48. */
  49. protected function setUp()
  50. {
  51. $this->_storageMock = $this->createPartialMock(
  52. \Magento\Customer\Model\Session\Storage::class,
  53. ['getIsCustomerEmulated', 'getData', 'unsIsCustomerEmulated', '__sleep', '__wakeup']
  54. );
  55. $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  56. $this->_httpContextMock = $this->createMock(\Magento\Framework\App\Http\Context::class);
  57. $this->urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
  58. $this->customerFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods(['create'])
  61. ->getMock();
  62. $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  63. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  64. $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
  65. $this->_model = $helper->getObject(
  66. \Magento\Customer\Model\Session::class,
  67. [
  68. 'customerFactory' => $this->customerFactoryMock,
  69. 'storage' => $this->_storageMock,
  70. 'eventManager' => $this->_eventManagerMock,
  71. 'httpContext' => $this->_httpContextMock,
  72. 'urlFactory' => $this->urlFactoryMock,
  73. 'customerRepository' => $this->customerRepositoryMock,
  74. 'response' => $this->responseMock,
  75. ]
  76. );
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function testSetCustomerAsLoggedIn()
  82. {
  83. $customer = $this->createMock(\Magento\Customer\Model\Customer::class);
  84. $customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  85. $customer->expects($this->any())
  86. ->method('getDataModel')
  87. ->will($this->returnValue($customerDto));
  88. $this->_eventManagerMock->expects($this->at(0))
  89. ->method('dispatch')
  90. ->with('customer_login', ['customer' => $customer]);
  91. $this->_eventManagerMock->expects($this->at(1))
  92. ->method('dispatch')
  93. ->with('customer_data_object_login', ['customer' => $customerDto]);
  94. $_SESSION = [];
  95. $this->_model->setCustomerAsLoggedIn($customer);
  96. $this->assertSame($customer, $this->_model->getCustomer());
  97. }
  98. /**
  99. * @return void
  100. */
  101. public function testSetCustomerDataAsLoggedIn()
  102. {
  103. $customer = $this->createMock(\Magento\Customer\Model\Customer::class);
  104. $customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  105. $this->customerFactoryMock->expects($this->once())
  106. ->method('create')
  107. ->will($this->returnValue($customer));
  108. $customer->expects($this->once())
  109. ->method('updateData')
  110. ->with($customerDto)
  111. ->will($this->returnSelf());
  112. $this->_eventManagerMock->expects($this->at(0))
  113. ->method('dispatch')
  114. ->with('customer_login', ['customer' => $customer]);
  115. $this->_eventManagerMock->expects($this->at(1))
  116. ->method('dispatch')
  117. ->with('customer_data_object_login', ['customer' => $customerDto]);
  118. $this->_model->setCustomerDataAsLoggedIn($customerDto);
  119. $this->assertSame($customer, $this->_model->getCustomer());
  120. }
  121. /**
  122. * @return void
  123. */
  124. public function testAuthenticate()
  125. {
  126. $urlMock = $this->createMock(\Magento\Framework\Url::class);
  127. $urlMock->expects($this->exactly(2))
  128. ->method('getUrl')
  129. ->willReturn('');
  130. $urlMock->expects($this->once())
  131. ->method('getRebuiltUrl')
  132. ->willReturn('');
  133. $this->urlFactoryMock->expects($this->exactly(4))
  134. ->method('create')
  135. ->willReturn($urlMock);
  136. $urlMock->expects($this->once())
  137. ->method('getUseSession')
  138. ->willReturn(false);
  139. $this->responseMock->expects($this->once())
  140. ->method('setRedirect')
  141. ->with('')
  142. ->willReturn('');
  143. $this->assertFalse($this->_model->authenticate());
  144. }
  145. /**
  146. * @return void
  147. */
  148. public function testLoginById()
  149. {
  150. $customerId = 1;
  151. $customerDataMock = $this->prepareLoginDataMock($customerId);
  152. $this->customerRepositoryMock->expects($this->once())
  153. ->method('getById')
  154. ->with($this->equalTo($customerId))
  155. ->will($this->returnValue($customerDataMock));
  156. $this->assertTrue($this->_model->loginById($customerId));
  157. }
  158. /**
  159. * @param int $customerId
  160. * @return \PHPUnit_Framework_MockObject_MockObject
  161. */
  162. protected function prepareLoginDataMock($customerId)
  163. {
  164. $customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  165. $customerDataMock->expects($this->once())
  166. ->method('getId')
  167. ->will($this->returnValue($customerId));
  168. $customerMock = $this->createPartialMock(
  169. \Magento\Customer\Model\Customer::class,
  170. ['getId', 'isConfirmationRequired', 'getConfirmation', 'updateData', 'getGroupId']
  171. );
  172. $customerMock->expects($this->once())
  173. ->method('getId')
  174. ->will($this->returnValue($customerId));
  175. $customerMock->expects($this->once())
  176. ->method('isConfirmationRequired')
  177. ->will($this->returnValue(true));
  178. $customerMock->expects($this->never())
  179. ->method('getConfirmation')
  180. ->will($this->returnValue($customerId));
  181. $this->customerFactoryMock->expects($this->once())
  182. ->method('create')
  183. ->will($this->returnValue($customerMock));
  184. $customerMock->expects($this->once())
  185. ->method('updateData')
  186. ->with($customerDataMock)
  187. ->will($this->returnSelf());
  188. $this->_httpContextMock->expects($this->exactly(3))
  189. ->method('setValue');
  190. return $customerDataMock;
  191. }
  192. /**
  193. * @param bool $expectedResult
  194. * @param bool $isCustomerIdValid
  195. * @param bool $isCustomerEmulated
  196. * @dataProvider getIsLoggedInDataProvider
  197. */
  198. public function testIsLoggedIn($expectedResult, $isCustomerIdValid, $isCustomerEmulated)
  199. {
  200. $customerId = 1;
  201. $this->_storageMock->expects($this->any())->method('getData')->with('customer_id')
  202. ->will($this->returnValue($customerId));
  203. if ($isCustomerIdValid) {
  204. $this->customerRepositoryMock->expects($this->once())
  205. ->method('getById')
  206. ->with($customerId);
  207. } else {
  208. $this->customerRepositoryMock->expects($this->once())
  209. ->method('getById')
  210. ->with($customerId)
  211. ->will($this->throwException(new \Exception('Customer ID is invalid.')));
  212. }
  213. $this->_storageMock->expects($this->any())->method('getIsCustomerEmulated')
  214. ->will($this->returnValue($isCustomerEmulated));
  215. $this->assertEquals($expectedResult, $this->_model->isLoggedIn());
  216. }
  217. /**
  218. * @return array
  219. */
  220. public function getIsLoggedInDataProvider()
  221. {
  222. return [
  223. ['expectedResult' => true, 'isCustomerIdValid' => true, 'isCustomerEmulated' => false],
  224. ['expectedResult' => false, 'isCustomerIdValid' => true, 'isCustomerEmulated' => true],
  225. ['expectedResult' => false, 'isCustomerIdValid' => false, 'isCustomerEmulated' => false],
  226. ['expectedResult' => false, 'isCustomerIdValid' => false, 'isCustomerEmulated' => true],
  227. ];
  228. }
  229. /**
  230. * @return void
  231. */
  232. public function testSetCustomerRemovesFlagThatShowsIfCustomerIsEmulated()
  233. {
  234. $customerMock = $this->createMock(\Magento\Customer\Model\Customer::class);
  235. $this->_storageMock->expects($this->once())->method('unsIsCustomerEmulated');
  236. $this->_model->setCustomer($customerMock);
  237. }
  238. }