This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,231 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Checkout\Controller\Cart;
use Laminas\Stdlib\Parameters;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\SessionFactory as CheckoutSessionFactory;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Escaper;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\TestFramework\Store\ExecuteInStoreContext;
use Magento\TestFramework\TestCase\AbstractController;
/**
* Class add product to cart controller.
*
* @see \Magento\Checkout\Controller\Cart\Add
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class AddTest extends AbstractController
{
/** @var SerializerInterface */
private $json;
/** @var CheckoutSessionFactory */
private $checkoutSessionFactory;
/** @var ProductRepositoryInterface */
private $productRepository;
/** @var ExecuteInStoreContext */
private $executeInStoreContext;
/** @var Escaper */
private $escaper;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->json = $this->_objectManager->get(SerializerInterface::class);
$this->checkoutSessionFactory = $this->_objectManager->get(CheckoutSessionFactory::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
$this->executeInStoreContext = $this->_objectManager->get(ExecuteInStoreContext::class);
$this->escaper = $this->_objectManager->get(Escaper::class);
}
/**
* Test with simple product and activated redirect to cart
*
* @magentoDataFixture Magento/Catalog/_files/products.php
* @magentoConfigFixture current_store checkout/cart/redirect_to_cart 1
*
* @return void
*/
public function testMessageAtAddToCartWithRedirect(): void
{
$this->prepareReferer();
$checkoutSession = $this->checkoutSessionFactory->create();
$postData = [
'qty' => '1',
'product' => '1',
'custom_price' => 1,
'isAjax' => 1,
];
$this->dispatchAddToCartRequest($postData);
$this->assertEquals(
$this->json->serialize(['backUrl' => 'http://localhost/checkout/cart/']),
$this->getResponse()->getBody()
);
$this->assertSessionMessages(
$this->containsEqual((string)__('You added %1 to your shopping cart.', 'Simple Product')),
MessageInterface::TYPE_SUCCESS
);
$this->assertCount(1, $checkoutSession->getQuote()->getItemsCollection());
}
/**
* Test with simple product and deactivated redirect to cart
*
* @magentoDataFixture Magento/Catalog/_files/products.php
* @magentoConfigFixture current_store checkout/cart/redirect_to_cart 0
*
* @return void
*/
public function testMessageAtAddToCartWithoutRedirect(): void
{
$this->prepareReferer();
$checkoutSession = $this->checkoutSessionFactory->create();
$postData = [
'qty' => '1',
'product' => '1',
'custom_price' => 1,
'isAjax' => 1,
];
$this->dispatchAddToCartRequest($postData);
$this->assertFalse($this->getResponse()->isRedirect());
$this->assertEquals('[]', $this->getResponse()->getBody());
$message = (string)__(
'You added %1 to your <a href="%2">shopping cart</a>.',
'Simple Product',
'http://localhost/checkout/cart/'
);
$this->assertSessionMessages(
$this->containsEqual("\n" . $message),
MessageInterface::TYPE_SUCCESS
);
$this->assertCount(1, $checkoutSession->getQuote()->getItemsCollection());
}
/**
* @dataProvider wrongParamsDataProvider
*
* @param array $params
* @return void
*/
public function testWithWrongParams(array $params): void
{
$this->prepareReferer();
$this->dispatchAddToCartRequest($params);
$this->assertRedirect($this->stringContains('http://localhost/test'));
}
/**
* @return array
*/
public function wrongParamsDataProvider(): array
{
return [
'empty_params' => ['params' => []],
'with_not_existing_product_id' => ['params' => ['product' => 989]],
];
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
*
* @return void
*/
public function testAddProductFromUnavailableWebsite(): void
{
$this->prepareReferer();
$product = $this->productRepository->get('simple-1');
$postData = ['product' => $product->getId()];
$this->executeInStoreContext->execute('fixture_second_store', [$this, 'dispatchAddToCartRequest'], $postData);
$this->assertRedirect($this->stringContains('http://localhost/test'));
$message = $this->escaper->escapeHtml(
(string)__('The product wasn\'t found. Verify the product and try again.')
);
$this->assertSessionMessages($this->containsEqual($message), MessageInterface::TYPE_ERROR);
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
*
* @return void
*/
public function testAddProductWithUnavailableQty(): void
{
$product = $this->productRepository->get('simple-1');
$postData = ['product' => $product->getId(), 'qty' => '1000'];
$this->dispatchAddToCartRequest($postData);
$message = (string)__('The requested qty is not available');
$this->assertSessionMessages($this->containsEqual($message), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains($product->getProductUrl()));
}
/**
* @magentoDataFixture Magento/Catalog/_files/products_related_multiple.php
*
* @return void
*/
public function testAddProductWithRelated(): void
{
$this->prepareReferer();
$checkoutSession = $this->checkoutSessionFactory->create();
$product = $this->productRepository->get('simple_with_cross');
$params = [
'product' => $product->getId(),
'related_product' => implode(',', $product->getRelatedProductIds()),
];
$this->dispatchAddToCartRequest($params);
$this->assertCount(3, $checkoutSession->getQuote()->getItemsCollection());
$message = (string)__(
'You added %1 to your <a href="%2">shopping cart</a>.',
$product->getName(),
'http://localhost/checkout/cart/'
);
$this->assertSessionMessages(
$this->containsEqual("\n" . $message),
MessageInterface::TYPE_SUCCESS
);
}
/**
* Dispatch add product to cart request.
*
* @param array $postData
* @return void
*/
public function dispatchAddToCartRequest(array $postData = []): void
{
$this->getRequest()->setPostValue($postData);
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('checkout/cart/add');
}
/**
* Prepare referer to test.
*
* @return void
*/
private function prepareReferer(): void
{
$parameters = $this->_objectManager->create(Parameters::class);
$parameters->set('HTTP_REFERER', 'http://localhost/test');
$this->getRequest()->setServer($parameters);
}
}
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Checkout\Controller\Cart\Index;
use Magento\Framework\App\Request\Http as HttpRequest;
/**
* @magentoDbIsolation enabled
*/
class CouponPostTest extends \Magento\TestFramework\TestCase\AbstractController
{
/**
* Test for \Magento\Checkout\Controller\Cart\CouponPost::execute() with simple product
*
* @magentoDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
*/
public function testExecute()
{
/** @var $session \Magento\Checkout\Model\Session */
$session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class);
$quote = $session->getQuote();
$quote->setData('trigger_recollect', 1)->setTotalsCollectedFlag(true);
$inputData = [
'remove' => 0,
'coupon_code' => 'test'
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($inputData);
$this->dispatch(
'checkout/cart/couponPost/'
);
$this->assertSessionMessages(
$this->equalTo(['The coupon code &quot;test&quot; is not valid.']),
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
);
}
/**
* Testing by adding a valid coupon to cart
*
* @magentoDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
* @magentoDataFixture Magento/Usps/Fixtures/cart_rule_coupon_free_shipping.php
* @return void
*/
public function testAddingValidCoupon(): void
{
/** @var $session \Magento\Checkout\Model\Session */
$session = $this->_objectManager->create(\Magento\Checkout\Model\Session::class);
$quote = $session->getQuote();
$quote->setData('trigger_recollect', 1)->setTotalsCollectedFlag(true);
$couponCode = 'IMPHBR852R61';
$inputData = [
'remove' => 0,
'coupon_code' => $couponCode
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($inputData);
$this->dispatch(
'checkout/cart/couponPost/'
);
$this->assertSessionMessages(
$this->equalTo(['You used coupon code &quot;' . $couponCode . '&quot;.']),
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
);
}
}
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Checkout\Controller\Cart;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Message\MessageInterface;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\TestFramework\TestCase\AbstractController;
/**
* Integration tests for \Magento\Checkout\Controller\Cart\UpdateItemOptions class.
*/
class UpdateItemOptionsTest extends AbstractController
{
/**
* @var FormKey
*/
private $formKey;
/**
* @var CheckoutSession
*/
private $checkoutSession;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->formKey = $this->_objectManager->get(FormKey::class);
$this->checkoutSession = $this->_objectManager->get(CheckoutSession::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
}
/**
* Tests that product is successfully updated in the shopping cart.
*
* @magentoAppArea frontend
* @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
*/
public function testUpdateProductOptionsInQuote()
{
$product = $this->productRepository->get('simple');
$quoteItem = $this->checkoutSession->getQuote()->getItemByProduct($product);
$postData = $this->preparePostData($product, $quoteItem);
$this->dispatchUpdateItemOptionsRequest($postData);
$this->assertTrue($this->getResponse()->isRedirect());
$this->assertRedirect($this->stringContains('/checkout/cart/'));
$message = (string)__(
'%1 was updated in your shopping cart.',
$product->getName()
);
$this->assertSessionMessages(
$this->containsEqual($message),
MessageInterface::TYPE_SUCCESS
);
}
/**
* Tests that product can't be updated with an empty shopping cart.
*
* @magentoAppArea frontend
* @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
*/
public function testUpdateProductOptionsWithEmptyQuote()
{
$product = $this->productRepository->get('simple');
$quoteItem = $this->checkoutSession->getQuote()->getItemByProduct($product);
$postData = $this->preparePostData($product, $quoteItem);
$this->checkoutSession->clearQuote();
$this->dispatchUpdateItemOptionsRequest($postData);
$this->assertTrue($this->getResponse()->isRedirect());
$this->assertRedirect($this->stringContains('/checkout/cart/'));
$message = (string)__('The quote item isn&#039;t found. Verify the item and try again.');
$this->assertSessionMessages(
$this->containsEqual($message),
MessageInterface::TYPE_ERROR
);
}
/**
* Prepare post data for the request.
*
* @param ProductInterface $product
* @param QuoteItem|bool $quoteItem
* @return array
*/
private function preparePostData(ProductInterface $product, $quoteItem): array
{
return [
'product' => $product->getId(),
'selected_configurable_option' => '',
'related_product' => '',
'item' => $quoteItem->getId(),
'form_key' => $this->formKey->getFormKey(),
'qty' => '2',
];
}
/**
* Perform request for updating product options in a quote item.
*
* @param array $postData
* @return void
*/
private function dispatchUpdateItemOptionsRequest(array $postData): void
{
$this->getRequest()->setPostValue($postData);
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('checkout/cart/updateItemOptions/id/' . $postData['item']);
}
}
@@ -0,0 +1,140 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Checkout\Controller\Cart;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Session;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Serialize\Serializer\Json;
class UpdateItemQtyTest extends \Magento\TestFramework\TestCase\AbstractController
{
/**
* @var Json
*/
private $json;
/**
* @var FormKey
*/
private $formKey;
/**
* @var Session
*/
private $session;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
protected function setUp(): void
{
parent::setUp();
$this->json = $this->_objectManager->create(Json::class);
$this->formKey = $this->_objectManager->get(FormKey::class);
$this->session = $this->_objectManager->create(Session::class);
$this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
}
/**
* Tests of cart validation.
*
* @param array $requestQuantity
* @param array $expectedResponse
*
* @magentoDbIsolation enabled
* @magentoAppArea frontend
* @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
* @dataProvider requestDataProvider
*/
public function testExecute($requestQuantity, $expectedResponse)
{
try {
/** @var $product Product */
$product = $this->productRepository->get('simple');
} catch (\Exception $e) {
$this->fail('No such product entity');
}
$quoteItem = $this->session
->getQuote()
->getItemByProduct($product);
$this->assertNotNull($quoteItem, 'Cannot get quote item for simple product');
$request = [];
if (!empty($requestQuantity) && is_array($requestQuantity)) {
$request= [
'form_key' => $this->formKey->getFormKey(),
'cart' => [
$quoteItem->getId() => $requestQuantity,
]
];
}
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($request);
$this->dispatch('checkout/cart/updateItemQty');
$response = $this->getResponse()->getBody();
$this->assertEquals($this->getErrorMessage($response), $this->getErrorMessage($expectedResponse));
}
/**
* @param $response
* @return string
*/
protected function getErrorMessage($response)
{
$error = '';
try {
$data = is_array($response) ? $response : $this->json->unserialize($response);
$error = $this->json->unserialize($data['error_message'])[0]['error'];
} catch (\Exception $e) {
if (!empty($data['error_message'])) {
$error = $data['error_message'];
}
}
return $error;
}
/**
* Variations of request data.
* @returns array
*/
public function requestDataProvider(): array
{
return [
[
'request' => [],
'response' => [
'success' => false,
'error_message' => 'Something went wrong while saving the page.'.
' Please refresh the page and try again.'
]
],
[
'request' => ['qty' => 2],
'response' => [
'success' => true,
]
],
[
'request' => ['qty' => 230],
'response' => [
'success' => false,
'error_message' => '[{"error":"The requested qty is not available","itemId":3}]']
],
];
}
}
@@ -0,0 +1,603 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Test class for \Magento\Checkout\Controller\Cart
*/
namespace Magento\Checkout\Controller;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Model\ResourceModel\CustomerRepository;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\Data\CartItemInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\QuoteRepository;
use Magento\Quote\Test\Fixture\AddProductToCart;
use Magento\Quote\Test\Fixture\GuestCart;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Request;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as OrderItemCollection;
use Magento\Framework\App\Request\Http as HttpRequest;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @magentoDbIsolation enabled
*/
class CartTest extends \Magento\TestFramework\TestCase\AbstractController
{
/** @var CheckoutSession */
private $checkoutSession;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var DataFixtureStorage
*/
private $fixtures;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->checkoutSession = $this->_objectManager->get(CheckoutSession::class);
$this->_objectManager->addSharedInstance($this->checkoutSession, CheckoutSession::class);
$this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
$this->fixtures = DataFixtureStorageManager::getStorage();
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->_objectManager->removeSharedInstance(CheckoutSession::class);
parent::tearDown();
}
/**
* Test for \Magento\Checkout\Controller\Cart::configureAction() with simple product
*
* @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
*/
public function testConfigureActionWithSimpleProduct()
{
/** @var $session CheckoutSession */
$session = $this->_objectManager->create(CheckoutSession::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
/** @var $product \Magento\Catalog\Model\Product */
$product = $productRepository->get('simple');
$quoteItem = $this->_getQuoteItemIdByProductId($session->getQuote(), $product->getId());
$this->assertNotNull($quoteItem, 'Cannot get quote item for simple product');
$this->dispatch(
'checkout/cart/configure/id/' . $quoteItem->getId() . '/product_id/' . $quoteItem->getProduct()->getId()
);
$response = $this->getResponse();
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
$this->assertEquals(
1,
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
'//button[@type="submit" and @title="Update Cart"]',
$response->getBody()
),
'Response for simple product doesn\'t contain "Update Cart" button'
);
}
/**
* Test for \Magento\Checkout\Controller\Cart::configureAction() with simple product and custom option
*
* @magentoDataFixture Magento/Checkout/_files/cart_with_simple_product_and_custom_options.php
*/
public function testConfigureActionWithSimpleProductAndCustomOption()
{
/** @var Quote $quote */
$quote = $this->getQuote('test_order_item_with_custom_options');
$this->checkoutSession->setQuoteId($quote->getId());
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
/** @var $product \Magento\Catalog\Model\Product */
$product = $productRepository->get('simple_with_custom_options');
$quoteItem = $this->_getQuoteItemIdByProductId($quote, $product->getId());
$this->assertNotNull($quoteItem, 'Cannot get quote item for simple product with custom option');
$this->dispatch(
'checkout/cart/configure/id/' . $quoteItem->getId() . '/product_id/' . $quoteItem->getProduct()->getId()
);
$response = $this->getResponse();
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
$this->assertEquals(
1,
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
'//button[@type="submit" and @title="Update Cart"]',
$response->getBody()
),
'Response for simple product with custom option doesn\'t contain "Update Cart" button'
);
$this->assertEquals(
1,
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
'//input[contains(@class,"product-custom-option") and @type="text"]',
$response->getBody()
),
'Response for simple product with custom option doesn\'t contain custom option input field'
);
}
/**
* Test for \Magento\Checkout\Controller\Cart::configureAction() with bundle product
*
* @magentoDataFixture Magento/Checkout/_files/quote_with_bundle_product.php
* @magentoDbIsolation disabled
*/
public function testConfigureActionWithBundleProduct()
{
/** @var $session CheckoutSession */
$session = $this->_objectManager->create(CheckoutSession::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
/** @var $product \Magento\Catalog\Model\Product */
$product = $productRepository->get('bundle-product');
$quoteItem = $this->_getQuoteItemIdByProductId($session->getQuote(), $product->getId());
$this->assertNotNull($quoteItem, 'Cannot get quote item for bundle product');
$this->dispatch(
'checkout/cart/configure/id/' . $quoteItem->getId() . '/product_id/' . $quoteItem->getProduct()->getId()
);
$response = $this->getResponse();
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
$this->assertEquals(
1,
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
'//button[@type="submit" and @title="Update Cart"]',
$response->getBody()
),
'Response for bundle product doesn\'t contain "Update Cart" button'
);
}
/**
* Test for \Magento\Checkout\Controller\Cart::configureAction() with downloadable product
*
* @magentoDataFixture Magento/Checkout/_files/quote_with_downloadable_product.php
*/
public function testConfigureActionWithDownloadableProduct()
{
/** @var $session CheckoutSession */
$session = $this->_objectManager->create(CheckoutSession::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
/** @var $product \Magento\Catalog\Model\Product */
$product = $productRepository->get('downloadable-product');
$quoteItem = $this->_getQuoteItemIdByProductId($session->getQuote(), $product->getId());
$this->assertNotNull($quoteItem, 'Cannot get quote item for downloadable product');
$this->dispatch(
'checkout/cart/configure/id/' . $quoteItem->getId() . '/product_id/' . $quoteItem->getProduct()->getId()
);
$response = $this->getResponse();
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
$this->assertEquals(
1,
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
'//button[@type="submit" and @title="Update Cart"]',
$response->getBody()
),
'Response for downloadable product doesn\'t contain "Update Cart" button'
);
$this->assertEquals(
1,
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
'//*[@id="downloadable-links-list"]',
$response->getBody()
),
'Response for downloadable product doesn\'t contain links for download'
);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
* @magentoAppIsolation enabled
*/
public function testUpdatePostAction()
{
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
/** @var $product \Magento\Catalog\Model\Product */
$product = $productRepository->get('simple');
/** Preconditions */
$customerFromFixture = 1;
$productId = $product->getId();
$originalQuantity = 1;
$updatedQuantity = 2;
/** @var $checkoutSession CheckoutSession */
$checkoutSession = $this->_objectManager->create(CheckoutSession::class);
$quoteItem = $this->_getQuoteItemIdByProductId($checkoutSession->getQuote(), $productId);
/** @var FormKey $formKey */
$formKey = $this->_objectManager->get(FormKey::class);
$postData = [
'cart' => [$quoteItem->getId() => ['qty' => $updatedQuantity]],
'update_cart_action' => 'update_qty',
'form_key' => $formKey->getFormKey(),
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($postData);
/** @var $customerSession \Magento\Customer\Model\Session */
$customerSession = $this->_objectManager->create(\Magento\Customer\Model\Session::class);
$customerSession->setCustomerId($customerFromFixture);
$this->assertNotNull($quoteItem, 'Cannot get quote item for simple product');
$this->assertEquals(
$originalQuantity,
$quoteItem->getQty(),
"Precondition failed: invalid quote item quantity"
);
/** Execute SUT */
$this->dispatch('checkout/cart/updatePost');
/** Check results */
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->_objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load($checkoutSession->getQuote()->getId());
$quoteItem = $this->_getQuoteItemIdByProductId($quote, $product->getId());
$this->assertEquals($updatedQuantity, $quoteItem->getQty(), "Invalid quote item quantity");
}
/**
* Gets quote by reserved order id.
*
* @param string $reservedOrderId
* @return Quote
*/
private function getQuote($reservedOrderId)
{
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
->create();
/** @var CartRepositoryInterface $quoteRepository */
$quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class);
$items = $quoteRepository->getList($searchCriteria)->getItems();
return array_pop($items);
}
/**
* Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id
*
* @param \Magento\Quote\Model\Quote $quote
* @param string|int $productId
*
* @return \Magento\Quote\Model\Quote\Item|null
*/
private function _getQuoteItemIdByProductId($quote, $productId)
{
/** @var $quoteItems \Magento\Quote\Model\Quote\Item[] */
$quoteItems = $quote->getAllItems();
foreach ($quoteItems as $quoteItem) {
if ($productId == $quoteItem->getProductId()) {
return $quoteItem;
}
}
return null;
}
/**
* Test for \Magento\Checkout\Controller\Cart::execute() with simple product
*
* @param string $area
* @param string $expectedPrice
* @magentoDataFixture Magento/Catalog/_files/products.php
* @magentoAppIsolation enabled
* @dataProvider addAddProductDataProvider
*/
public function testAddToCartSimpleProduct($area, $expectedPrice)
{
$formKey = $this->_objectManager->get(FormKey::class);
$postData = [
'qty' => '1',
'product' => '1',
'custom_price' => 1,
'form_key' => $formKey->getFormKey(),
'isAjax' => 1
];
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea($area);
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($postData);
$quote = $this->_objectManager->create(\Magento\Checkout\Model\Cart::class);
/** @var \Magento\Checkout\Controller\Cart\Add $controller */
$controller = $this->_objectManager->create(\Magento\Checkout\Controller\Cart\Add::class, [$quote]);
$controller->execute();
$this->assertStringContainsString(json_encode([]), $this->getResponse()->getBody());
$items = $quote->getItems()->getItems();
$this->assertIsArray($items, 'Quote doesn\'t have any items');
$this->assertCount(1, $items, 'Expected quote items not equal to 1');
$item = reset($items);
$this->assertEquals(1, $item->getProductId(), 'Quote has more than one product');
$this->assertEquals($expectedPrice, $item->getPrice(), 'Expected product price failed');
}
/**
* Data provider for testAddToCartSimpleProduct
*/
public function addAddProductDataProvider()
{
return [
'frontend' => ['frontend', 'expected_price' => 10],
'adminhtml' => ['adminhtml', 'expected_price' => 1]
];
}
/**
* @covers \Magento\Checkout\Controller\Cart\Addgroup::execute()
*
* Test customer can add items to cart only if they belong to him.
*
* @param bool $loggedIn
* @param string $request
* @magentoAppArea frontend
* @magentoDataFixture Magento/Checkout/_files/order_items.php
* @dataProvider reorderItemsDataProvider
* @return void
*/
public function testReorderItems(bool $loggedIn, string $request)
{
// Make sure test starts without logged in customer.
$customerSession = $this->_objectManager->get(CustomerSession::class);
$customerSession->logout();
$checkoutSession = Bootstrap::getObjectManager()->get(CheckoutSession::class);
$expected = [];
if ($loggedIn && $request == Request::METHOD_POST) {
$customer = $this->_objectManager->create(CustomerRepository::class)->get('customer2@example.com');
$customerSession->setCustomerDataObject($customer);
$orderCollection = $this->_objectManager->create(OrderCollection::class);
$orderCollection->addFieldToFilter('customer_id', $customer->getId());
$orderItemCollection = $this->_objectManager->create(OrderItemCollection::class);
$orderItemCollection->addFieldToFilter('order_id', ['in' => $orderCollection->getAllIds()]);
$expected = $orderItemCollection->getColumnValues('product_id');
}
$this->prepareRequest($request);
$this->dispatch('checkout/cart/addGroup');
$this->assertEquals(
$expected,
$checkoutSession->getQuote()->getItemsCollection()->getColumnValues('product_id')
);
// Make sure test doesn't left logged in customer after execution.
$customerSession->logout();
}
/**
* Data provider for testReorderItems.
*
* @return array
*/
public function reorderItemsDataProvider()
{
return [
[
'logged_in' => false,
'request_type' => Request::METHOD_POST,
],
[
'logged_in' => false,
'request_type' => Request::METHOD_GET,
],
[
'logged_in' => true,
'request_type' => Request::METHOD_POST,
],
[
'logged_in' => true,
'request_type' => Request::METHOD_GET,
],
];
}
/**
* Prepare request for testReorderItems.
*
* @param string $method
* @return void
*/
private function prepareRequest(string $method)
{
/** @var OrderItemCollection $orderItems */
$orderItems = $this->_objectManager->create(OrderItemCollection::class);
/** @var FormKey $key */
$key = $this->_objectManager->get(FormKey::class);
$data = [
'form_key' => $key->getFormKey(),
'order_items' => $orderItems->getAllIds(),
];
$this->getRequest()->setMethod($method);
switch ($method) {
case Request::METHOD_POST:
$this->getRequest()->setPostValue($data);
break;
case Request::METHOD_GET:
default:
$this->getRequest()->setParams($data);
break;
}
}
/**
* @throws NoSuchEntityException
* @throws LocalizedException
*/
#[
DataFixture(ProductFixture::class, ['sku' => 's1', 'stock_item' => ['is_in_stock' => true]], 'p1'),
DataFixture(ProductFixture::class, ['sku' => 's2','stock_item' => ['is_in_stock' => true]], 'p2'),
DataFixture(GuestCart::class, as: 'cart'),
DataFixture(
AddProductToCart::class,
['cart_id' => '$cart.id$', 'product_id' => '$p1.id$', 'qty' => 1],
'item1'
),
DataFixture(
AddProductToCart::class,
['cart_id' => '$cart.id$', 'product_id' => '$p2.id$', 'qty' => 1],
'item2'
)
]
public function testUpdatePostActionWithMultipleProducts()
{
$cartId = (int)$this->fixtures->get('cart')->getId();
if (!$cartId) {
$this->fail('quote fixture failed');
}
/** @var QuoteRepository $quoteRepository */
$quoteRepository = Bootstrap::getObjectManager()->get(QuoteRepository::class);
$quote = $quoteRepository->get($cartId);
$checkoutSession = Bootstrap::getObjectManager()->get(CheckoutSession::class);
$checkoutSession->setQuoteId($quote->getId());
/** @var \Magento\Quote\Model\Quote\Item $item1 */
$item1 = $this->fixtures->get('item1');
/** @var \Magento\Quote\Model\Quote\Item $item2 */
$item2 = $this->fixtures->get('item2');
$p1 = $this->fixtures->get('p1');
/** @var $p1 Product */
$product1 = $this->productRepository->get($p1->getSku(), true);
$stockItem = $product1->getExtensionAttributes()->getStockItem();
$stockItem->setQty(0);
$stockItem->setIsInStock(false);
$stockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class);
$stockItemRepository->save($stockItem);
$originalQuantity = 1;
$updatedQuantity = 2;
$this->assertEquals(
$originalQuantity + $originalQuantity,
$quote->getItemsQty(),
"Precondition failed: quote totals does not match."
);
$response = $this->updatePostRequest($quote, $item1, $item2, $updatedQuantity, $updatedQuantity, true);
$this->assertStringContainsString(
'"itemId":'.$item1->getId().'}]',
$response['error_message']
);
$response = $this->updatePostRequest($quote, $item1, $item2, $originalQuantity, $updatedQuantity, false);
$this->assertStringContainsString(
'"itemId":'.$item1->getId().'}]',
$response['error_message']
);
$this->assertEquals(
$originalQuantity + $updatedQuantity,
$quote->getItemsQty(),
"Precondition failed: quote totals does not match."
);
$response = $this->updatePostRequest($quote, $item1, $item2, $updatedQuantity, $updatedQuantity, false);
$this->assertStringContainsString(
'"itemId":'.$item1->getId().'}]',
$response['error_message']
);
$this->assertEquals(
$originalQuantity + $updatedQuantity,
$quote->getItemsQty(),
"Precondition failed: quote totals does not match."
);
}
/**
* @param CartInterface $quote
* @param CartItemInterface $item1
* @param CartItemInterface $item2
* @param float $qty1
* @param float $qty2
* @param bool $updateQty
* @return mixed
* @throws LocalizedException
*/
private function updatePostRequest(
CartInterface $quote,
CartItemInterface $item1,
CartItemInterface $item2,
float $qty1,
float $qty2,
bool $updateQty = true
): array {
/** @var FormKey $formKey */
$formKey = Bootstrap::getObjectManager()->get(FormKey::class);
$request = [
'cart' => [
$item1->getId() => ['qty' => $qty1],
$item2->getId() => ['qty' => $qty2]
],
'update_cart_action' => 'update_qty',
'form_key' => $formKey->getFormKey(),
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($request);
if ($updateQty) {
$this->dispatch('checkout/cart/updateItemQty');
} else {
$this->dispatch('checkout/cart/updatePost');
$quote->collectTotals();
}
$response = $this->getResponse()->getBody();
$response = json_decode($response, true);
return $response;
}
}