GuestTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Helper;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Sales\Helper\Guest;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class GuestTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Sales\Helper\Guest */
  15. protected $guest;
  16. /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $sessionMock;
  18. /** @var \Magento\Framework\Stdlib\CookieManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $cookieManagerMock;
  20. /** @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $cookieMetadataFactoryMock;
  22. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $managerInterfaceMock;
  24. /** @var \PHPUnit_Framework_MockObject_MockObject */
  25. protected $orderFactoryMock;
  26. /** @var \Magento\Framework\App\ViewInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $viewInterfaceMock;
  28. /** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $storeModelMock;
  30. /** @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $salesOrderMock;
  32. /**
  33. * @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $orderRepository;
  36. /**
  37. * @var \Magento\Framework\Api\SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $searchCriteriaBuilder;
  40. protected function setUp()
  41. {
  42. $appContextHelperMock = $this->createMock(\Magento\Framework\App\Helper\Context::class);
  43. $storeManagerInterfaceMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  44. $registryMock = $this->createMock(\Magento\Framework\Registry::class);
  45. $this->sessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  46. $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
  47. $this->cookieMetadataFactoryMock = $this->createMock(
  48. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
  49. );
  50. $this->managerInterfaceMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  51. $this->orderFactoryMock = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  52. $this->viewInterfaceMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  53. $this->storeModelMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->salesOrderMock = $this->createPartialMock(
  57. \Magento\Sales\Model\Order::class,
  58. [
  59. 'getProtectCode',
  60. 'loadByIncrementIdAndStoreId',
  61. 'loadByIncrementId',
  62. 'getId',
  63. 'getStoreId',
  64. 'getBillingAddress',
  65. '__wakeup'
  66. ]
  67. );
  68. $this->orderRepository = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
  69. ->setMethods(['getList'])
  70. ->disableOriginalConstructor()
  71. ->getMockForAbstractClass();
  72. $this->searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
  73. ->setMethods(['addFilter', 'create'])
  74. ->disableOriginalConstructor()
  75. ->getMockForAbstractClass();
  76. $orderSearchResult = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderSearchResultInterface::class)
  77. ->setMethods(['getTotalCount', 'getItems'])
  78. ->disableOriginalConstructor()
  79. ->getMockForAbstractClass();
  80. $this->searchCriteriaBuilder->method('addFilter')->willReturnSelf();
  81. $resultRedirectFactory =
  82. $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
  83. ->setMethods(['create'])
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->orderRepository->method('getList')->willReturn($orderSearchResult);
  87. $orderSearchResult->method('getTotalCount')->willReturn(1);
  88. $orderSearchResult->method('getItems')->willReturn([ 2 => $this->salesOrderMock]);
  89. $searchCriteria = $this
  90. ->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
  91. ->getMockForAbstractClass();
  92. $storeManagerInterfaceMock->expects($this->any())->method('getStore')->willReturn($this->storeModelMock);
  93. $this->searchCriteriaBuilder->method('create')->willReturn($searchCriteria);
  94. $this->storeModelMock->method('getId')->willReturn(1);
  95. $this->salesOrderMock->expects($this->any())->method('getStoreId')->willReturn(1);
  96. $objectManagerHelper = new ObjectManagerHelper($this);
  97. $this->guest = $objectManagerHelper->getObject(
  98. \Magento\Sales\Helper\Guest::class,
  99. [
  100. 'context' => $appContextHelperMock,
  101. 'storeManager' => $storeManagerInterfaceMock,
  102. 'coreRegistry' => $registryMock,
  103. 'customerSession' => $this->sessionMock,
  104. 'cookieManager' => $this->cookieManagerMock,
  105. 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
  106. 'messageManager' => $this->managerInterfaceMock,
  107. 'orderFactory' => $this->orderFactoryMock,
  108. 'view' => $this->viewInterfaceMock,
  109. 'orderRepository' => $this->orderRepository,
  110. 'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
  111. 'resultRedirectFactory' => $resultRedirectFactory
  112. ]
  113. );
  114. }
  115. public function testLoadValidOrderNotEmptyPost()
  116. {
  117. $post = [
  118. 'oar_order_id' => 1,
  119. 'oar_type' => 'email',
  120. 'oar_billing_lastname' => 'oar_billing_lastname',
  121. 'oar_email' => 'oar_email',
  122. 'oar_zip' => 'oar_zip',
  123. ];
  124. $incrementId = $post['oar_order_id'];
  125. $protectedCode = 'protectedCode';
  126. $this->sessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
  127. $requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  128. $requestMock->expects($this->once())->method('getPostValue')->willReturn($post);
  129. $this->salesOrderMock->expects($this->any())->method('getId')->willReturn($incrementId);
  130. $billingAddressMock = $this->createPartialMock(
  131. \Magento\Sales\Model\Order\Address::class,
  132. ['getLastname', 'getEmail', '__wakeup']
  133. );
  134. $billingAddressMock->expects($this->once())->method('getLastname')->willReturn(($post['oar_billing_lastname']));
  135. $billingAddressMock->expects($this->once())->method('getEmail')->willReturn(($post['oar_email']));
  136. $this->salesOrderMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
  137. $this->salesOrderMock->expects($this->once())->method('getProtectCode')->willReturn($protectedCode);
  138. $metaDataMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class);
  139. $metaDataMock->expects($this->once())->method('setPath')
  140. ->with(Guest::COOKIE_PATH)
  141. ->willReturnSelf();
  142. $metaDataMock->expects($this->once())
  143. ->method('setHttpOnly')
  144. ->with(true)
  145. ->willReturnSelf();
  146. $this->cookieMetadataFactoryMock->expects($this->once())
  147. ->method('createPublicCookieMetadata')
  148. ->willReturn($metaDataMock);
  149. $this->cookieManagerMock->expects($this->once())
  150. ->method('setPublicCookie')
  151. ->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock);
  152. $this->assertTrue($this->guest->loadValidOrder($requestMock));
  153. }
  154. public function testLoadValidOrderStoredCookie()
  155. {
  156. $protectedCode = 'protectedCode';
  157. $incrementId = 1;
  158. $cookieData = $protectedCode . ':' . $incrementId;
  159. $cookieDataHash = base64_encode($cookieData);
  160. $this->sessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
  161. $this->cookieManagerMock->expects($this->once())
  162. ->method('getCookie')
  163. ->with(Guest::COOKIE_NAME)
  164. ->willReturn($cookieDataHash);
  165. $this->salesOrderMock->expects($this->any())->method('getId')->willReturn($incrementId);
  166. $this->salesOrderMock->expects($this->once())->method('getProtectCode')->willReturn($protectedCode);
  167. $metaDataMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class);
  168. $metaDataMock->expects($this->once())
  169. ->method('setPath')
  170. ->with(Guest::COOKIE_PATH)
  171. ->willReturnSelf();
  172. $metaDataMock->expects($this->once())
  173. ->method('setHttpOnly')
  174. ->with(true)
  175. ->willReturnSelf();
  176. $this->cookieMetadataFactoryMock->expects($this->once())
  177. ->method('createPublicCookieMetadata')
  178. ->willReturn($metaDataMock);
  179. $this->cookieManagerMock->expects($this->once())
  180. ->method('setPublicCookie')
  181. ->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock);
  182. $requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  183. $this->assertTrue($this->guest->loadValidOrder($requestMock));
  184. }
  185. }