IndexTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class IndexTest extends \Magento\TestFramework\TestCase\AbstractController
  11. {
  12. /**
  13. * @var \Magento\Customer\Model\Session
  14. */
  15. protected $_customerSession;
  16. /**
  17. * @var \Magento\Framework\Message\ManagerInterface
  18. */
  19. protected $_messages;
  20. /**
  21. * @var \Magento\Customer\Helper\View
  22. */
  23. protected $_customerViewHelper;
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  28. $this->_customerSession = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  29. \Magento\Customer\Model\Session::class,
  30. [$logger]
  31. );
  32. /** @var \Magento\Customer\Api\AccountManagementInterface $service */
  33. $service = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  34. \Magento\Customer\Api\AccountManagementInterface::class
  35. );
  36. $customer = $service->authenticate('customer@example.com', 'password');
  37. $this->_customerSession->setCustomerDataAsLoggedIn($customer);
  38. $this->_customerViewHelper = $this->_objectManager->create(\Magento\Customer\Helper\View::class);
  39. $this->_messages = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  40. \Magento\Framework\Message\ManagerInterface::class
  41. );
  42. }
  43. protected function tearDown()
  44. {
  45. $this->_customerSession->logout();
  46. $this->_customerSession = null;
  47. parent::tearDown();
  48. }
  49. /**
  50. * Verify wishlist view action
  51. *
  52. * The following is verified:
  53. * - \Magento\Wishlist\Model\ResourceModel\Item\Collection
  54. * - \Magento\Wishlist\Block\Customer\Wishlist
  55. * - \Magento\Wishlist\Block\Customer\Wishlist\Items
  56. * - \Magento\Wishlist\Block\Customer\Wishlist\Item\Column
  57. * - \Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Cart
  58. * - \Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Comment
  59. * - \Magento\Wishlist\Block\Customer\Wishlist\Button
  60. * - that \Magento\Wishlist\Block\Customer\Wishlist\Item\Options doesn't throw a fatal error
  61. *
  62. * @magentoDataFixture Magento/Wishlist/_files/wishlist.php
  63. */
  64. public function testItemColumnBlock()
  65. {
  66. $this->dispatch('wishlist/index/index');
  67. $body = $this->getResponse()->getBody();
  68. $this->assertEquals(
  69. 1,
  70. \Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
  71. '//img[contains(@src, "small_image.jpg") and @alt = "Simple Product"]',
  72. $body
  73. )
  74. );
  75. $this->assertEquals(
  76. 1,
  77. \Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
  78. '//textarea[contains(@name, "description")]',
  79. $body
  80. )
  81. );
  82. }
  83. /**
  84. * @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
  85. * @magentoDataFixture Magento/Customer/_files/customer.php
  86. * @magentoAppArea frontend
  87. */
  88. public function testAddActionProductNameXss()
  89. {
  90. /** @var \Magento\Framework\Data\Form\FormKey $formKey */
  91. $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
  92. $this->getRequest()->setPostValue([
  93. 'form_key' => $formKey->getFormKey(),
  94. ]);
  95. /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
  96. $productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  97. ->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  98. $product = $productRepository->get('product-with-xss');
  99. $this->dispatch('wishlist/index/add/product/' . $product->getId() . '?nocookie=1');
  100. $this->assertSessionMessages(
  101. $this->equalTo(
  102. [
  103. "\n&lt;script&gt;alert(&quot;xss&quot;);&lt;/script&gt; has been added to your Wish List. "
  104. . 'Click <a href="http://localhost/index.php/">here</a> to continue shopping.',
  105. ]
  106. ),
  107. \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
  108. );
  109. }
  110. /**
  111. * @magentoDbIsolation disabled
  112. * @magentoDataFixture Magento/Wishlist/_files/wishlist_with_product_qty_increments.php
  113. */
  114. public function testAllcartAction()
  115. {
  116. $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class)->getFormKey();
  117. $this->getRequest()->setParam('form_key', $formKey);
  118. $this->dispatch('wishlist/index/allcart');
  119. /** @var \Magento\Checkout\Model\Cart $cart */
  120. $cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
  121. $quoteCount = $cart->getQuote()->getItemsCollection()->count();
  122. $this->assertEquals(0, $quoteCount);
  123. $this->assertSessionMessages(
  124. $this->contains('You can buy this product only in quantities of 5 at a time for "Simple Product".'),
  125. \Magento\Framework\Message\MessageInterface::TYPE_ERROR
  126. );
  127. }
  128. /**
  129. * @magentoDataFixture Magento/Wishlist/_files/wishlist.php
  130. */
  131. public function testSendAction()
  132. {
  133. \Magento\TestFramework\Helper\Bootstrap::getInstance()
  134. ->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
  135. $request = [
  136. 'form_key' => $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class)->getFormKey(),
  137. 'emails' => 'test@tosend.com',
  138. 'message' => 'message',
  139. 'rss_url' => null, // no rss
  140. ];
  141. $this->getRequest()->setPostValue($request);
  142. $this->getRequest()->setMethod('POST');
  143. $this->_objectManager->get(\Magento\Framework\Registry::class)->register(
  144. 'wishlist',
  145. $this->_objectManager->get(\Magento\Wishlist\Model\Wishlist::class)->loadByCustomerId(1)
  146. );
  147. $this->dispatch('wishlist/index/send');
  148. /** @var \Magento\TestFramework\Mail\Template\TransportBuilderMock $transportBuilder */
  149. $transportBuilder = $this->_objectManager->get(
  150. \Magento\TestFramework\Mail\Template\TransportBuilderMock::class
  151. );
  152. $actualResult = quoted_printable_decode($transportBuilder->getSentMessage()->getRawMessage());
  153. $this->assertStringMatchesFormat(
  154. '%A' . $this->_customerViewHelper->getCustomerName($this->_customerSession->getCustomerDataObject())
  155. . ' wants to share this Wish List%A',
  156. $actualResult
  157. );
  158. }
  159. }