Skip to content

MC-26683: Removed get errors of cart allowing to add product to cart #27015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 19, 2020
12 changes: 1 addition & 11 deletions app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\Quote\Model\Quote;

/**
* Add products to cart
* Adding products to cart using GraphQL
*/
class AddProductsToCart
{
Expand Down Expand Up @@ -54,16 +54,6 @@ public function execute(Quote $cart, array $cartItems): void
$this->addProductToCart->execute($cart, $cartItemData);
}

if ($cart->getData('has_error')) {
$e = new GraphQlInputException(__('Shopping cart errors'));
$errors = $cart->getErrors();
foreach ($errors as $error) {
/** @var MessageInterface $error */
$e->addError(new GraphQlInputException(__($error->getText())));
}
throw $e;
}

$this->cartRepository->save($cart);
}
}
7 changes: 7 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote\Item as QuoteItem;
Expand All @@ -29,6 +30,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$cart = $value['model'];

$itemsData = [];
if ($cart->getData('has_error')) {
$errors = $cart->getErrors();
foreach ($errors as $error) {
$itemsData[] = new GraphQlInputException(__($error->getText()));
}
}
foreach ($cart->getAllVisibleItems() as $cartItem) {
/**
* @var QuoteItem $cartItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
namespace Magento\GraphQl\Quote\Guest;

use Exception;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
Expand Down Expand Up @@ -79,6 +81,57 @@ public function testAddSimpleProductToCart()
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
}

/**
* Add disabled product to cart
*
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @return void
*/
public function testAddDisabledProductToCart(): void
{
$sku = 'simple3';
$quantity = 2;

$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $quantity);

$this->expectException(ResponseContainsErrorsException::class);
$this->expectExceptionMessage(
'Could not add the product with SKU ' . $sku . ' to the shopping cart: ' .
'Product that you are trying to add is not available.'
);

$this->graphQlMutation($query);
}

/**
* Add out of stock product to cart
*
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php
* @return void
* @throws NoSuchEntityException
*/
public function testAddOutOfStockProductToCart(): void
{
$sku = 'simple1';
$quantity = 1;

$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $quantity);

$this->expectException(ResponseContainsErrorsException::class);
$this->expectExceptionMessage(
'Some of the products are out of stock.'
);

$this->graphQlMutation($query);
}

/**
* @expectedException Exception
* @expectedExceptionMessage Required parameter "cart_id" is missing
Expand Down Expand Up @@ -191,7 +244,7 @@ public function testAddSimpleProductToCustomerCart()
private function getQuery(string $maskedQuoteId, string $sku, float $quantity): string
{
return <<<QUERY
mutation {
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{$maskedQuoteId}"
Expand Down