SessionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Checkout\Model\Session
  8. */
  9. namespace Magento\Checkout\Test\Unit\Model;
  10. use \Magento\Checkout\Model\Session;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class SessionTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  18. */
  19. protected $_helper;
  20. /**
  21. * @var \Magento\Checkout\Model\Session
  22. */
  23. protected $_session;
  24. protected function setUp()
  25. {
  26. $this->_helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  27. }
  28. /**
  29. * @param int|null $orderId
  30. * @param int|null $incrementId
  31. * @param \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject $orderMock
  32. * @dataProvider getLastRealOrderDataProvider
  33. */
  34. public function testGetLastRealOrder($orderId, $incrementId, $orderMock)
  35. {
  36. $orderFactory = $this->getMockBuilder(\Magento\Sales\Model\OrderFactory::class)
  37. ->disableOriginalConstructor()
  38. ->setMethods(['create'])
  39. ->getMock();
  40. $orderFactory->expects($this->once())->method('create')->will($this->returnValue($orderMock));
  41. $messageCollectionFactory = $this->getMockBuilder(\Magento\Framework\Message\CollectionFactory::class)
  42. ->disableOriginalConstructor()
  43. ->setMethods(['create'])
  44. ->getMock();
  45. $quoteRepository = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  46. $appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['isInstalled']);
  47. $appState->expects($this->any())->method('isInstalled')->will($this->returnValue(true));
  48. $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  49. $request->expects($this->any())->method('getHttpHost')->will($this->returnValue([]));
  50. $constructArguments = $this->_helper->getConstructArguments(
  51. \Magento\Checkout\Model\Session::class,
  52. [
  53. 'request' => $request,
  54. 'orderFactory' => $orderFactory,
  55. 'messageCollectionFactory' => $messageCollectionFactory,
  56. 'quoteRepository' => $quoteRepository,
  57. 'storage' => new \Magento\Framework\Session\Storage()
  58. ]
  59. );
  60. $this->_session = $this->_helper->getObject(\Magento\Checkout\Model\Session::class, $constructArguments);
  61. $this->_session->setLastRealOrderId($orderId);
  62. $this->assertSame($orderMock, $this->_session->getLastRealOrder());
  63. if ($orderId == $incrementId) {
  64. $this->assertSame($orderMock, $this->_session->getLastRealOrder());
  65. }
  66. }
  67. /**
  68. * @return array
  69. */
  70. public function getLastRealOrderDataProvider()
  71. {
  72. return [
  73. [null, 1, $this->_getOrderMock(1, null)],
  74. [1, 1, $this->_getOrderMock(1, 1)],
  75. [1, null, $this->_getOrderMock(null, 1)]
  76. ];
  77. }
  78. /**
  79. * @param int|null $incrementId
  80. * @param int|null $orderId
  81. * @return \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  82. */
  83. protected function _getOrderMock($incrementId, $orderId)
  84. {
  85. /** @var $order \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order */
  86. $order = $this->getMockBuilder(
  87. \Magento\Sales\Model\Order::class
  88. )->disableOriginalConstructor()->setMethods(
  89. ['getIncrementId', 'loadByIncrementId', '__sleep', '__wakeup']
  90. )->getMock();
  91. if ($orderId && $incrementId) {
  92. $order->expects($this->once())->method('getIncrementId')->will($this->returnValue($incrementId));
  93. $order->expects($this->once())->method('loadByIncrementId')->with($orderId);
  94. }
  95. return $order;
  96. }
  97. /**
  98. * @param $paramToClear
  99. * @dataProvider clearHelperDataDataProvider
  100. */
  101. public function testClearHelperData($paramToClear)
  102. {
  103. $storage = new \Magento\Framework\Session\Storage('default', [$paramToClear => 'test_data']);
  104. $this->_session = $this->_helper->getObject(\Magento\Checkout\Model\Session::class, ['storage' => $storage]);
  105. $this->_session->clearHelperData();
  106. $this->assertNull($this->_session->getData($paramToClear));
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function clearHelperDataDataProvider()
  112. {
  113. return [
  114. ['redirect_url'],
  115. ['last_order_id'],
  116. ['last_real_order_id'],
  117. ['additional_messages']
  118. ];
  119. }
  120. /**
  121. * @param bool $hasOrderId
  122. * @param bool $hasQuoteId
  123. * @dataProvider restoreQuoteDataProvider
  124. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  125. */
  126. public function testRestoreQuote($hasOrderId, $hasQuoteId)
  127. {
  128. $order = $this->createPartialMock(
  129. \Magento\Sales\Model\Order::class,
  130. ['getId', 'loadByIncrementId', '__wakeup']
  131. );
  132. $order->expects($this->once())->method('getId')->will($this->returnValue($hasOrderId ? 'order id' : null));
  133. $orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  134. $orderFactory->expects($this->once())->method('create')->will($this->returnValue($order));
  135. $quoteRepository = $this->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)
  136. ->setMethods(['save'])
  137. ->getMockForAbstractClass();
  138. $storage = new \Magento\Framework\Session\Storage();
  139. $store = $this->createMock(\Magento\Store\Model\Store::class);
  140. $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
  141. $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
  142. $eventManager = $this->getMockForAbstractClass(\Magento\Framework\Event\ManagerInterface::class);
  143. /** @var Session $session */
  144. $session = $this->_helper->getObject(
  145. \Magento\Checkout\Model\Session::class,
  146. [
  147. 'orderFactory' => $orderFactory,
  148. 'quoteRepository' => $quoteRepository,
  149. 'storage' => $storage,
  150. 'storeManager' => $storeManager,
  151. 'eventManager' => $eventManager
  152. ]
  153. );
  154. $lastOrderId = 'last order id';
  155. $quoteId = 'quote id';
  156. $anotherQuoteId = 'another quote id';
  157. $session->setLastRealOrderId($lastOrderId);
  158. $session->setQuoteId($quoteId);
  159. if ($hasOrderId) {
  160. $order->setQuoteId($quoteId);
  161. $quote = $this->createPartialMock(
  162. \Magento\Quote\Model\Quote::class,
  163. ['setIsActive', 'getId', 'setReservedOrderId', '__wakeup', 'save']
  164. );
  165. if ($hasQuoteId) {
  166. $quoteRepository->expects($this->once())->method('get')->with($quoteId)->willReturn($quote);
  167. $quote->expects(
  168. $this->any()
  169. )->method(
  170. 'getId'
  171. )->will(
  172. $this->returnValue($anotherQuoteId)
  173. );
  174. $eventManager->expects(
  175. $this->once()
  176. )->method(
  177. 'dispatch'
  178. )->with(
  179. 'restore_quote',
  180. ['order' => $order, 'quote' => $quote]
  181. );
  182. $quote->expects(
  183. $this->once()
  184. )->method(
  185. 'setIsActive'
  186. )->with(
  187. $this->equalTo(1)
  188. )->will(
  189. $this->returnSelf()
  190. );
  191. $quote->expects(
  192. $this->once()
  193. )->method(
  194. 'setReservedOrderId'
  195. )->with(
  196. $this->isNull()
  197. )->will(
  198. $this->returnSelf()
  199. );
  200. $quoteRepository->expects($this->once())->method('save')->with($quote);
  201. } else {
  202. $quoteRepository->expects($this->once())
  203. ->method('get')
  204. ->with($quoteId)
  205. ->willThrowException(
  206. new \Magento\Framework\Exception\NoSuchEntityException()
  207. );
  208. $quote->expects($this->never())->method('setIsActive');
  209. $quote->expects($this->never())->method('setReservedOrderId');
  210. $quote->expects($this->never())->method('save');
  211. }
  212. }
  213. $result = $session->restoreQuote();
  214. if ($hasOrderId && $hasQuoteId) {
  215. $this->assertNull($session->getLastRealOrderId());
  216. $this->assertEquals($anotherQuoteId, $session->getQuoteId());
  217. } else {
  218. $this->assertEquals($lastOrderId, $session->getLastRealOrderId());
  219. $this->assertEquals($quoteId, $session->getQuoteId());
  220. }
  221. $this->assertEquals($result, $hasOrderId && $hasQuoteId);
  222. }
  223. /**
  224. * @return array
  225. */
  226. public function restoreQuoteDataProvider()
  227. {
  228. return [[true, true], [true, false], [false, true], [false, false]];
  229. }
  230. public function testHasQuote()
  231. {
  232. $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $session = $this->_helper->getObject(\Magento\Checkout\Model\Session::class, ['quote' => $quote]);
  236. $this->assertFalse($session->hasQuote());
  237. }
  238. public function testReplaceQuote()
  239. {
  240. $replaceQuoteId = 3;
  241. $websiteId = 1;
  242. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  243. ->disableOriginalConstructor()
  244. ->setMethods(['getWebsiteId', '__wakeup'])
  245. ->getMock();
  246. $store->expects($this->any())
  247. ->method('getWebsiteId')
  248. ->will($this->returnValue($websiteId));
  249. $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
  250. $storeManager->expects($this->any())
  251. ->method('getStore')
  252. ->will($this->returnValue($store));
  253. $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  254. ->disableOriginalConstructor()
  255. ->getMock();
  256. $quote->expects($this->once())
  257. ->method('getId')
  258. ->will($this->returnValue($replaceQuoteId));
  259. $storage = $this->getMockBuilder(\Magento\Framework\Session\Storage::class)
  260. ->disableOriginalConstructor()
  261. ->setMethods(['setData', 'getData'])
  262. ->getMock();
  263. $storage->expects($this->any())
  264. ->method('getData')
  265. ->willReturn($replaceQuoteId);
  266. $storage->expects($this->any())
  267. ->method('setData');
  268. $quoteIdMaskMock = $this->createPartialMock(
  269. \Magento\Quote\Model\QuoteIdMask::class,
  270. ['getMaskedId', 'load', 'setQuoteId', 'save']
  271. );
  272. $quoteIdMaskMock->expects($this->once())->method('load')->with($replaceQuoteId, 'quote_id')->willReturnSelf();
  273. $quoteIdMaskMock->expects($this->once())->method('getMaskedId')->willReturn(null);
  274. $quoteIdMaskMock->expects($this->once())->method('setQuoteId')->with($replaceQuoteId)->willReturnSelf();
  275. $quoteIdMaskMock->expects($this->once())->method('save');
  276. $quoteIdMaskFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteIdMaskFactory::class, ['create']);
  277. $quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($quoteIdMaskMock);
  278. $session = $this->_helper->getObject(
  279. \Magento\Checkout\Model\Session::class,
  280. [
  281. 'storeManager' => $storeManager,
  282. 'storage' => $storage,
  283. 'quoteIdMaskFactory' => $quoteIdMaskFactoryMock
  284. ]
  285. );
  286. $session->replaceQuote($quote);
  287. $this->assertSame($quote, $session->getQuote());
  288. $this->assertEquals($replaceQuoteId, $session->getQuoteId());
  289. }
  290. public function testClearStorage()
  291. {
  292. $storage = $this->getMockBuilder(\Magento\Framework\Session\Storage::class)
  293. ->disableOriginalConstructor()
  294. ->setMethods(['unsetData'])
  295. ->getMock();
  296. $storage->expects($this->once())
  297. ->method('unsetData');
  298. $session = $this->_helper->getObject(
  299. \Magento\Checkout\Model\Session::class,
  300. [
  301. 'storage' => $storage
  302. ]
  303. );
  304. $this->assertInstanceOf(\Magento\Checkout\Model\Session::class, $session->clearStorage());
  305. $this->assertFalse($session->hasQuote());
  306. }
  307. public function testResetCheckout()
  308. {
  309. /** @var $session \Magento\Checkout\Model\Session */
  310. $session = $this->_helper->getObject(
  311. \Magento\Checkout\Model\Session::class,
  312. ['storage' => new \Magento\Framework\Session\Storage()]
  313. );
  314. $session->resetCheckout();
  315. $this->assertEquals(\Magento\Checkout\Model\Session::CHECKOUT_STATE_BEGIN, $session->getCheckoutState());
  316. }
  317. public function testGetStepData()
  318. {
  319. $stepData = [
  320. 'simple' => 'data',
  321. 'complex' => [
  322. 'key' => 'value',
  323. ],
  324. ];
  325. /** @var $session \Magento\Checkout\Model\Session */
  326. $session = $this->_helper->getObject(
  327. \Magento\Checkout\Model\Session::class,
  328. ['storage' => new \Magento\Framework\Session\Storage()]
  329. );
  330. $session->setSteps($stepData);
  331. $this->assertEquals($stepData, $session->getStepData());
  332. $this->assertFalse($session->getStepData('invalid_key'));
  333. $this->assertEquals($stepData['complex'], $session->getStepData('complex'));
  334. $this->assertFalse($session->getStepData('simple', 'invalid_sub_key'));
  335. $this->assertEquals($stepData['complex']['key'], $session->getStepData('complex', 'key'));
  336. }
  337. public function testSetStepData()
  338. {
  339. $stepData = [
  340. 'complex' => [
  341. 'key' => 'value',
  342. ],
  343. ];
  344. /** @var $session \Magento\Checkout\Model\Session */
  345. $session = $this->_helper->getObject(
  346. \Magento\Checkout\Model\Session::class,
  347. ['storage' => new \Magento\Framework\Session\Storage()]
  348. );
  349. $session->setSteps($stepData);
  350. $session->setStepData('complex', 'key2', 'value2');
  351. $session->setStepData('simple', ['key' => 'value']);
  352. $session->setStepData('simple', 'key2', 'value2');
  353. $expectedResult = [
  354. 'complex' => [
  355. 'key' => 'value',
  356. 'key2' => 'value2',
  357. ],
  358. 'simple' => [
  359. 'key' => 'value',
  360. 'key2' => 'value2',
  361. ],
  362. ];
  363. $this->assertEquals($expectedResult, $session->getSteps());
  364. }
  365. }