PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Catalog/Controller/Product/CompareTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 317 lines | 226 code | 53 blank | 38 comment | 0 complexity | 08a5c5ebc9d75e31b3d82fc8cf183c16 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Catalog\Controller\Product;
  8. use Magento\Framework\Message\MessageInterface;
  9. /**
  10. * @magentoDataFixture Magento/Catalog/controllers/_files/products.php
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class CompareTest extends \Magento\TestFramework\TestCase\AbstractController
  15. {
  16. /**
  17. * @var \Magento\Catalog\Model\ProductRepository
  18. */
  19. protected $productRepository;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. /** @var $objectManager \Magento\TestFramework\ObjectManager */
  24. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  25. $this->productRepository = $objectManager->create(\Magento\Catalog\Model\ProductRepository::class);
  26. }
  27. public function testAddAction()
  28. {
  29. $this->_requireVisitorWithNoProducts();
  30. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  31. /** @var \Magento\Framework\Data\Form\FormKey $formKey */
  32. $formKey = $objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
  33. $product = $this->productRepository->get('simple_product_1');
  34. $this->dispatch(
  35. sprintf(
  36. 'catalog/product_compare/add/product/%s/form_key/%s?nocookie=1',
  37. $product->getEntityId(),
  38. $formKey->getFormKey()
  39. )
  40. );
  41. $this->assertSessionMessages(
  42. $this->equalTo(['You added product Simple Product 1 Name to the comparison list.']),
  43. MessageInterface::TYPE_SUCCESS
  44. );
  45. $this->assertRedirect();
  46. $this->_assertCompareListEquals([$product->getEntityId()]);
  47. }
  48. public function testIndexActionAddProducts()
  49. {
  50. $this->_requireVisitorWithNoProducts();
  51. $product = $this->productRepository->get('simple_product_2');
  52. $this->dispatch('catalog/product_compare/index/items/' . $product->getEntityId());
  53. $this->assertRedirect($this->equalTo('http://localhost/index.php/catalog/product_compare/index/'));
  54. $this->_assertCompareListEquals([$product->getEntityId()]);
  55. }
  56. public function testRemoveAction()
  57. {
  58. $this->_requireVisitorWithTwoProducts();
  59. $product = $this->productRepository->get('simple_product_2');
  60. $this->dispatch('catalog/product_compare/remove/product/' . $product->getEntityId());
  61. $this->assertSessionMessages(
  62. $this->equalTo(['You removed product Simple Product 2 Name from the comparison list.']),
  63. MessageInterface::TYPE_SUCCESS
  64. );
  65. $this->assertRedirect();
  66. $restProduct = $this->productRepository->get('simple_product_1');
  67. $this->_assertCompareListEquals([$restProduct->getEntityId()]);
  68. }
  69. public function testRemoveActionWithSession()
  70. {
  71. $this->_requireCustomerWithTwoProducts();
  72. $product = $this->productRepository->get('simple_product_1');
  73. $this->dispatch('catalog/product_compare/remove/product/' . $product->getEntityId());
  74. $secondProduct = $this->productRepository->get('simple_product_2');
  75. $this->assertSessionMessages(
  76. $this->equalTo(['You removed product Simple Product 1 Name from the comparison list.']),
  77. MessageInterface::TYPE_SUCCESS
  78. );
  79. $this->assertRedirect();
  80. $this->_assertCompareListEquals([$secondProduct->getEntityId()]);
  81. }
  82. public function testIndexActionDisplay()
  83. {
  84. $this->_requireVisitorWithTwoProducts();
  85. $layout = $this->_objectManager->get(\Magento\Framework\View\LayoutInterface::class);
  86. $layout->setIsCacheable(false);
  87. $this->dispatch('catalog/product_compare/index');
  88. $responseBody = $this->getResponse()->getBody();
  89. $this->assertContains('Products Comparison List', $responseBody);
  90. $this->assertContains('simple_product_1', $responseBody);
  91. $this->assertContains('Simple Product 1 Name', $responseBody);
  92. $this->assertContains('Simple Product 1 Full Description', $responseBody);
  93. $this->assertContains('Simple Product 1 Short Description', $responseBody);
  94. $this->assertContains('$1,234.56', $responseBody);
  95. $this->assertContains('simple_product_2', $responseBody);
  96. $this->assertContains('Simple Product 2 Name', $responseBody);
  97. $this->assertContains('Simple Product 2 Full Description', $responseBody);
  98. $this->assertContains('Simple Product 2 Short Description', $responseBody);
  99. $this->assertContains('$987.65', $responseBody);
  100. }
  101. public function testClearAction()
  102. {
  103. $this->_requireVisitorWithTwoProducts();
  104. $this->dispatch('catalog/product_compare/clear');
  105. $this->assertSessionMessages(
  106. $this->equalTo(['You cleared the comparison list.']),
  107. MessageInterface::TYPE_SUCCESS
  108. );
  109. $this->assertRedirect();
  110. $this->_assertCompareListEquals([]);
  111. }
  112. /**
  113. * @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
  114. */
  115. public function testRemoveActionProductNameXss()
  116. {
  117. $this->_prepareCompareListWithProductNameXss();
  118. $product = $this->productRepository->get('product-with-xss');
  119. $this->dispatch('catalog/product_compare/remove/product/' . $product->getEntityId() . '?nocookie=1');
  120. $this->assertSessionMessages(
  121. $this->equalTo(
  122. ['You removed product &lt;script&gt;alert(&quot;xss&quot;);&lt;/script&gt; from the comparison list.']
  123. ),
  124. MessageInterface::TYPE_SUCCESS
  125. );
  126. }
  127. protected function _prepareCompareListWithProductNameXss()
  128. {
  129. /** @var $visitor \Magento\Customer\Model\Visitor */
  130. $visitor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  131. ->create(\Magento\Customer\Model\Visitor::class);
  132. /** @var \Magento\Framework\Stdlib\DateTime $dateTime */
  133. $visitor->setSessionId(md5(time()) . md5(microtime()))
  134. ->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT))
  135. ->save();
  136. /** @var $item \Magento\Catalog\Model\Product\Compare\Item */
  137. $item = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  138. \Magento\Catalog\Model\Product\Compare\Item::class
  139. );
  140. $firstProductEntityId = $this->productRepository->get('product-with-xss')->getEntityId();
  141. $item->setVisitorId($visitor->getId())->setProductId($firstProductEntityId)->save();
  142. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  143. \Magento\Customer\Model\Visitor::class
  144. )->load(
  145. $visitor->getId()
  146. );
  147. }
  148. protected function _requireVisitorWithNoProducts()
  149. {
  150. /** @var $visitor \Magento\Customer\Model\Visitor */
  151. $visitor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  152. ->create(\Magento\Customer\Model\Visitor::class);
  153. $visitor->setSessionId(md5(time()) . md5(microtime()))
  154. ->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT))
  155. ->save();
  156. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  157. \Magento\Customer\Model\Visitor::class
  158. )->load(
  159. $visitor->getId()
  160. );
  161. $this->_assertCompareListEquals([]);
  162. }
  163. protected function _requireVisitorWithTwoProducts()
  164. {
  165. /** @var $visitor \Magento\Customer\Model\Visitor */
  166. $visitor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  167. ->create(\Magento\Customer\Model\Visitor::class);
  168. $visitor->setSessionId(md5(time()) . md5(microtime()))
  169. ->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT))
  170. ->save();
  171. /** @var $item \Magento\Catalog\Model\Product\Compare\Item */
  172. $item = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  173. \Magento\Catalog\Model\Product\Compare\Item::class
  174. );
  175. $firstProductEntityId = $this->productRepository->get('simple_product_1')->getEntityId();
  176. $secondProductEntityId = $this->productRepository->get('simple_product_2')->getEntityId();
  177. $item->setVisitorId($visitor->getId())->setProductId($firstProductEntityId)->save();
  178. /** @var $item \Magento\Catalog\Model\Product\Compare\Item */
  179. $item = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  180. \Magento\Catalog\Model\Product\Compare\Item::class
  181. );
  182. $item->setVisitorId($visitor->getId())->setProductId($secondProductEntityId)->save();
  183. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  184. \Magento\Customer\Model\Visitor::class
  185. )->load(
  186. $visitor->getId()
  187. );
  188. $this->_assertCompareListEquals([$firstProductEntityId, $secondProductEntityId]);
  189. }
  190. protected function _requireCustomerWithTwoProducts()
  191. {
  192. $customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  193. ->create(\Magento\Customer\Model\Customer::class);
  194. /** @var \Magento\Customer\Model\Customer $customer */
  195. $customer
  196. ->setWebsiteId(1)
  197. ->setId(1)
  198. ->setEntityTypeId(1)
  199. ->setAttributeSetId(1)
  200. ->setEmail('customer@example.com')
  201. ->setPassword('password')
  202. ->setGroupId(1)
  203. ->setStoreId(1)
  204. ->setIsActive(1)
  205. ->setFirstname('Firstname')
  206. ->setLastname('Lastname')
  207. ->setDefaultBilling(1)
  208. ->setDefaultShipping(1);
  209. $customer->isObjectNew(true);
  210. $customer->save();
  211. /** @var $session \Magento\Customer\Model\Session */
  212. $session = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  213. ->get(\Magento\Customer\Model\Session::class);
  214. $session->setCustomerId(1);
  215. /** @var $visitor \Magento\Customer\Model\Visitor */
  216. $visitor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  217. ->create(\Magento\Customer\Model\Visitor::class);
  218. $visitor->setSessionId(md5(time()) . md5(microtime()))
  219. ->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT))
  220. ->save();
  221. $firstProductEntityId = $this->productRepository->get('simple_product_1')->getEntityId();
  222. $secondProductEntityId = $this->productRepository->get('simple_product_2')->getEntityId();
  223. /** @var $item \Magento\Catalog\Model\Product\Compare\Item */
  224. $item = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  225. ->create(\Magento\Catalog\Model\Product\Compare\Item::class);
  226. $item->setVisitorId($visitor->getId())
  227. ->setCustomerId(1)
  228. ->setProductId($firstProductEntityId)
  229. ->save();
  230. /** @var $item \Magento\Catalog\Model\Product\Compare\Item */
  231. $item = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  232. ->create(\Magento\Catalog\Model\Product\Compare\Item::class);
  233. $item->setVisitorId($visitor->getId())
  234. ->setProductId($secondProductEntityId)
  235. ->save();
  236. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Visitor::class)
  237. ->load($visitor->getId());
  238. $this->_assertCompareListEquals([$firstProductEntityId, $secondProductEntityId]);
  239. }
  240. /**
  241. * Assert that current visitor has exactly expected products in compare list
  242. *
  243. * @param array $expectedProductIds
  244. */
  245. protected function _assertCompareListEquals(array $expectedProductIds)
  246. {
  247. /** @var $compareItems \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection */
  248. $compareItems = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  249. \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection::class
  250. );
  251. $compareItems->useProductItem(true);
  252. // important
  253. $compareItems->setVisitorId(
  254. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  255. \Magento\Customer\Model\Visitor::class)->getId()
  256. );
  257. $actualProductIds = [];
  258. foreach ($compareItems as $compareItem) {
  259. /** @var $compareItem \Magento\Catalog\Model\Product\Compare\Item */
  260. $actualProductIds[] = $compareItem->getProductId();
  261. }
  262. $this->assertEquals($expectedProductIds, $actualProductIds, "Products in current visitor's compare list.");
  263. }
  264. }