CartTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Helper;
  7. use \Magento\Checkout\Helper\Cart;
  8. use Magento\Framework\App\Action\Action;
  9. use Magento\Framework\DataObject;
  10. use Magento\Quote\Model\Quote\Item;
  11. class CartTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $urlBuilderMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $requestMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $scopeConfigMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $cartMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $checkoutSessionMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Url\EncoderInterface
  35. */
  36. protected $urlEncoder;
  37. /**
  38. * @var Cart
  39. */
  40. protected $helper;
  41. protected function setUp()
  42. {
  43. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  44. ->disableOriginalConstructor()->getMock();
  45. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  46. /** @var \Magento\Framework\App\Helper\Context $context */
  47. $context = $objectManagerHelper->getObject(
  48. \Magento\Framework\App\Helper\Context::class,
  49. [
  50. 'httpRequest' => $this->requestMock,
  51. ]
  52. );
  53. $className = \Magento\Checkout\Helper\Cart::class;
  54. $arguments = $objectManagerHelper->getConstructArguments($className, ['context' => $context]);
  55. $this->urlBuilderMock = $context->getUrlBuilder();
  56. $this->urlEncoder = $context->getUrlEncoder();
  57. $this->urlEncoder->expects($this->any())
  58. ->method('encode')
  59. ->willReturnCallback(function ($url) {
  60. return strtr(base64_encode($url), '+/=', '-_,');
  61. });
  62. $this->scopeConfigMock = $context->getScopeConfig();
  63. $this->cartMock = $arguments['checkoutCart'];
  64. $this->checkoutSessionMock = $arguments['checkoutSession'];
  65. $this->helper = $objectManagerHelper->getObject($className, $arguments);
  66. }
  67. public function testGetCart()
  68. {
  69. $this->assertEquals($this->cartMock, $this->helper->getCart());
  70. }
  71. public function testGetRemoveUrl()
  72. {
  73. $quoteItemId = 1;
  74. $quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  75. $quoteItemMock->expects($this->any())->method('getId')->will($this->returnValue($quoteItemId));
  76. $currentUrl = 'http://www.example.com/';
  77. $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($currentUrl));
  78. $params = [
  79. 'id' => $quoteItemId,
  80. Action::PARAM_NAME_BASE64_URL => strtr(base64_encode($currentUrl), '+/=', '-_,'),
  81. ];
  82. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/delete', $params);
  83. $this->helper->getRemoveUrl($quoteItemMock);
  84. }
  85. public function testGetCartUrl()
  86. {
  87. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart', []);
  88. $this->helper->getCartUrl();
  89. }
  90. public function testGetQuote()
  91. {
  92. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  93. $this->checkoutSessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
  94. $this->assertEquals($quoteMock, $this->helper->getQuote());
  95. }
  96. public function testGetItemsCount()
  97. {
  98. $itemsCount = 1;
  99. $this->cartMock->expects($this->any())->method('getItemsCount')->will($this->returnValue($itemsCount));
  100. $this->assertEquals($itemsCount, $this->helper->getItemsCount());
  101. }
  102. public function testGetItemsQty()
  103. {
  104. $itemsQty = 1;
  105. $this->cartMock->expects($this->any())->method('getItemsQty')->will($this->returnValue($itemsQty));
  106. $this->assertEquals($itemsQty, $this->helper->getItemsQty());
  107. }
  108. public function testGetSummaryCount()
  109. {
  110. $summaryQty = 1;
  111. $this->cartMock->expects($this->any())->method('getSummaryQty')->will($this->returnValue($summaryQty));
  112. $this->assertEquals($summaryQty, $this->helper->getSummaryCount());
  113. }
  114. public function testAddUrlWithUencPlaceholder()
  115. {
  116. $productEntityId = 1;
  117. $storeId = 1;
  118. $isRequestSecure = false;
  119. $productMock = $this->createPartialMock(
  120. \Magento\Catalog\Model\Product::class,
  121. ['getEntityId', 'hasUrlDataObject', 'getUrlDataObject', '__wakeup']
  122. );
  123. $productMock->expects($this->any())->method('getEntityId')->will($this->returnValue($productEntityId));
  124. $productMock->expects($this->any())->method('hasUrlDataObject')->will($this->returnValue(true));
  125. $productMock->expects($this->any())->method('getUrlDataObject')
  126. ->will($this->returnValue(new DataObject(['store_id' => $storeId])));
  127. $this->requestMock->expects($this->any())->method('getRouteName')->will($this->returnValue('checkout'));
  128. $this->requestMock->expects($this->any())->method('getControllerName')->will($this->returnValue('cart'));
  129. $this->requestMock->expects($this->once())->method('isSecure')->willReturn($isRequestSecure);
  130. $params = [
  131. Action::PARAM_NAME_URL_ENCODED => strtr("%uenc%", '+/=', '-_,'),
  132. 'product' => $productEntityId,
  133. 'custom_param' => 'value',
  134. '_scope' => $storeId,
  135. '_scope_to_url' => true,
  136. 'in_cart' => 1,
  137. '_secure' => $isRequestSecure
  138. ];
  139. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/add', $params);
  140. $this->helper->getAddUrl($productMock, ['custom_param' => 'value', 'useUencPlaceholder' => 1]);
  141. }
  142. public function testGetIsVirtualQuote()
  143. {
  144. $isVirtual = true;
  145. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  146. $this->checkoutSessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
  147. $quoteMock->expects($this->any())->method('isVirtual')->will($this->returnValue($isVirtual));
  148. $this->assertEquals($isVirtual, $this->helper->getIsVirtualQuote());
  149. }
  150. public function testGetShouldRedirectToCart()
  151. {
  152. $storeId = 1;
  153. $this->scopeConfigMock->expects($this->once())->method('isSetFlag')
  154. ->with(Cart::XML_PATH_REDIRECT_TO_CART, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
  155. ->will($this->returnValue(true));
  156. $this->assertTrue($this->helper->getShouldRedirectToCart($storeId));
  157. }
  158. public function testGetAddUrl()
  159. {
  160. $productEntityId = 1;
  161. $storeId = 1;
  162. $isRequestSecure = false;
  163. $productMock = $this->createPartialMock(
  164. \Magento\Catalog\Model\Product::class,
  165. ['getEntityId', 'hasUrlDataObject', 'getUrlDataObject', '__wakeup']
  166. );
  167. $productMock->expects($this->any())->method('getEntityId')->will($this->returnValue($productEntityId));
  168. $productMock->expects($this->any())->method('hasUrlDataObject')->will($this->returnValue(true));
  169. $productMock->expects($this->any())->method('getUrlDataObject')
  170. ->will($this->returnValue(new DataObject(['store_id' => $storeId])));
  171. $currentUrl = 'http://www.example.com/';
  172. $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($currentUrl));
  173. $this->requestMock->expects($this->any())->method('getRouteName')->will($this->returnValue('checkout'));
  174. $this->requestMock->expects($this->any())->method('getControllerName')->will($this->returnValue('cart'));
  175. $this->requestMock->expects($this->once())->method('isSecure')->willReturn($isRequestSecure);
  176. $params = [
  177. Action::PARAM_NAME_URL_ENCODED => strtr(base64_encode($currentUrl), '+/=', '-_,'),
  178. 'product' => $productEntityId,
  179. 'custom_param' => 'value',
  180. '_scope' => $storeId,
  181. '_scope_to_url' => true,
  182. 'in_cart' => 1,
  183. '_secure' => $isRequestSecure
  184. ];
  185. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/add', $params);
  186. $this->helper->getAddUrl($productMock, ['custom_param' => 'value']);
  187. }
  188. /**
  189. * @param integer $id
  190. * @param string $url
  191. * @param bool $isAjax
  192. * @param string $expectedPostData
  193. *
  194. * @dataProvider deletePostJsonDataProvider
  195. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  196. */
  197. public function testGetDeletePostJson($id, $url, $isAjax, $expectedPostData)
  198. {
  199. $item = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  200. $item->expects($this->once())
  201. ->method('getId')
  202. ->will($this->returnValue($id));
  203. $this->requestMock->expects($this->once())
  204. ->method('isAjax')
  205. ->will($this->returnValue($isAjax));
  206. $this->urlBuilderMock->expects($this->any())
  207. ->method('getCurrentUrl')
  208. ->will($this->returnValue($url));
  209. $this->urlBuilderMock->expects($this->once())
  210. ->method('getUrl')
  211. ->will($this->returnValue($url));
  212. $result = $this->helper->getDeletePostJson($item);
  213. $this->assertEquals($expectedPostData, $result);
  214. }
  215. /**
  216. * @return array
  217. */
  218. public function deletePostJsonDataProvider()
  219. {
  220. $url = 'http://localhost.com/dev/checkout/cart/delete/';
  221. $uenc = strtr(base64_encode($url), '+/=', '-_,');
  222. $id = 1;
  223. $expectedPostData1 = json_encode(
  224. [
  225. 'action' => $url,
  226. 'data' => ['id' => $id, 'uenc' => $uenc],
  227. ]
  228. );
  229. $expectedPostData2 = json_encode(
  230. [
  231. 'action' => $url,
  232. 'data' => ['id' => $id],
  233. ]
  234. );
  235. return [
  236. [$id, $url, false, $expectedPostData1],
  237. [$id, $url, true, $expectedPostData2],
  238. ];
  239. }
  240. }