CartTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Controller\Cart
  8. */
  9. namespace Magento\ConfigurableProduct\Controller;
  10. use Magento\Framework\App\Request\Http as HttpRequest;
  11. class CartTest extends \Magento\TestFramework\TestCase\AbstractController
  12. {
  13. /**
  14. * Test for \Magento\Checkout\Controller\Cart::configureAction() with configurable product
  15. *
  16. * @magentoDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
  17. */
  18. public function testConfigureActionWithConfigurableProduct()
  19. {
  20. /** @var $session \Magento\Checkout\Model\Session */
  21. $session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class);
  22. $quoteItem = $this->_getQuoteItemIdByProductId($session->getQuote(), 1);
  23. $this->assertNotNull($quoteItem, 'Cannot get quote item for configurable product');
  24. $this->dispatch(
  25. 'checkout/cart/configure/id/' . $quoteItem->getId() . '/product_id/' . $quoteItem->getProduct()->getId()
  26. );
  27. $response = $this->getResponse();
  28. $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
  29. $this->assertEquals(
  30. 1,
  31. \Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
  32. '//button[@type="submit" and @title="Update Cart"]',
  33. $response->getBody()
  34. ),
  35. 'Response for configurable product doesn\'t contain "Update Cart" button'
  36. );
  37. $this->assertEquals(
  38. 1,
  39. \Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
  40. '//select[contains(@class,"super-attribute-select")]',
  41. $response->getBody()
  42. ),
  43. 'Response for configurable product doesn\'t contain select for super attribute'
  44. );
  45. }
  46. /**
  47. * Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id
  48. *
  49. * @param \Magento\Quote\Model\Quote $quote
  50. * @param mixed $productId
  51. * @return \Magento\Quote\Model\Quote\Item|null
  52. */
  53. private function _getQuoteItemIdByProductId(\Magento\Quote\Model\Quote $quote, $productId)
  54. {
  55. /** @var $quoteItems \Magento\Quote\Model\Quote\Item[] */
  56. $quoteItems = $quote->getAllItems();
  57. foreach ($quoteItems as $quoteItem) {
  58. if ($productId == $quoteItem->getProductId()) {
  59. return $quoteItem;
  60. }
  61. }
  62. return null;
  63. }
  64. /**
  65. * Test for \Magento\Checkout\Controller\Cart\CouponPost::execute() with configurable product with last option
  66. * Covers MAGETWO-60352
  67. *
  68. * @magentoDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product_last_variation.php
  69. */
  70. public function testExecuteForConfigurableLastOption()
  71. {
  72. /** @var $session \Magento\Checkout\Model\Session */
  73. $session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class);
  74. $quote = $session->getQuote();
  75. $quote->setData('trigger_recollect', 1)->setTotalsCollectedFlag(true);
  76. $inputData = [
  77. 'remove' => 0,
  78. 'coupon_code' => 'test'
  79. ];
  80. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  81. $this->getRequest()->setPostValue($inputData);
  82. $this->dispatch(
  83. 'checkout/cart/couponPost/'
  84. );
  85. $this->assertSessionMessages(
  86. $this->equalTo(['The coupon code &quot;test&quot; is not valid.']),
  87. \Magento\Framework\Message\MessageInterface::TYPE_ERROR
  88. );
  89. }
  90. }