Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

GraphQl-439: Improve coverage for mutation addSimpleProductToCart or Cart contains a product after product got status Out of Stock #475

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
76b25c2
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Mar 15, 2019
9bb2932
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Mar 26, 2019
6df7a81
GraphQl-439: Improve coverage for mutation addSimpleProductToCart or …
XxXgeoXxX Jul 18, 2019
09c7897
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Jul 18, 2019
9fd75e9
GraphQl-439: Improve coverage for mutation addSimpleProductToCart or …
XxXgeoXxX Jul 18, 2019
f9a5784
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Jul 29, 2019
58cfea5
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Jul 31, 2019
20f0d55
Delete mypatch.patch
XxXgeoXxX Jul 31, 2019
9664559
Delete Fix_the_special_price_expression.patch
XxXgeoXxX Jul 31, 2019
74ddb92
Delete AddProductToCartTest.php
XxXgeoXxX Jul 31, 2019
24b0339
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Jul 31, 2019
5234a6d
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Jul 31, 2019
0431ed2
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Jul 31, 2019
198ba53
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Aug 1, 2019
ce3af88
Merge branch '2.3-develop' of github.com:magento/magento2 into 2.3-de…
XxXgeoXxX Aug 1, 2019
fcfc435
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Aug 1, 2019
3f63007
Merge branch '2.3-develop' of github.com:magento/magento2 into 2.3-de…
XxXgeoXxX Sep 4, 2019
1a5675b
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Sep 4, 2019
66f42b0
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Sep 4, 2019
304eae3
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Sep 4, 2019
5f38def
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Sep 4, 2019
d4419da
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Sep 5, 2019
a98f4e2
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Sep 5, 2019
24697ea
Improve coverage for mutation addSimpleProductToCart or Cart contains…
XxXgeoXxX Sep 5, 2019
abec8b4
Merge remote-tracking branch 'origin/2.3-develop' into 2.3-develop#439
TomashKhamlai Nov 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Message\AbstractMessage;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;

Expand Down Expand Up @@ -53,31 +52,6 @@ public function execute(Quote $cart, array $cartItems): void
foreach ($cartItems as $cartItemData) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't move this logic after error checking.

Now it works as:

  1. Perform adding products to cart
  2. Check errors after the previous operation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes were reverted

$this->addProductToCart->execute($cart, $cartItemData);
}

if ($cart->getData('has_error')) {
throw new GraphQlInputException(
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
);
}

$this->cartRepository->save($cart);
}

/**
* Collecting cart errors
*
* @param Quote $cart
* @return string
*/
private function getCartErrors(Quote $cart): string
{
$errorMessages = [];

/** @var AbstractMessage $error */
foreach ($cart->getErrors() as $error) {
$errorMessages[] = $error->getText();
}

return implode(PHP_EOL, $errorMessages);
}
}
57 changes: 57 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
* @inheritdoc
*/
class CartMessages implements ResolverInterface
{
/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}
$cart = $value['model'];
if (empty($cart->getData('has_error'))) {
return [];
}
return $this->getCartErrors($cart);
}

/**
* Collecting cart errors
*
* @param Quote $cart
* @return array
*/
private function getCartErrors(Quote $cart): array
{
$errorMessages = [];

/** @var AbstractMessage $error */
foreach ($cart->getMessages() as $idettifier => $error) {
$errorMessages[] = [
'identifier' => $idettifier,
'text' => $error->getText()
];
}

return $errorMessages;
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ type Cart {
prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPrices")
total_quantity: Float! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartTotalQuantity")
is_virtual: Boolean! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartIsVirtual")
messages: [Message] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartMessages") @doc(description:"Return all shopping cart messages for a guest or logged in user")
}

interface CartAddressInterface @typeResolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressTypeResolver") {
Expand Down Expand Up @@ -366,3 +367,8 @@ type Order {
order_number: String!
order_id: String @deprecated(reason: "The order_id field is deprecated, use order_number instead.")
}

type Message {
identifier: String
text: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;

/**
* Class GetCartMessagesTest
*
* @package Magento\GraphQl\Quote
*/
class CartMessagesTest extends GraphQlAbstract
{
/**
* @var QuoteResource
*/
private $quoteResource;

/**
* @var QuoteFactory
*/
private $quoteFactory;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;

/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testCartWithoutErrors()
{
$maskedQuoteId = $this->getMaskedQuoteId();

$query = $this->getCartMessagesQuery($maskedQuoteId);
$response = $this->graphQlMutation($query);
self::assertEquals([], $response['cart']['messages']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/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
*/
public function testCartWithOutOfStockProduct()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);

$query = $this->getCartMessagesQuery($maskedQuoteId);

$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals('Some of the products are out of stock.', $response['cart']['messages'][0]['text']);
}

/**
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getMaskedQuoteId() : string
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* @param string $maskedQuoteId
* @return string
*/
public function getCartMessagesQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id:"{$maskedQuoteId}"){
messages{
text
}
}
}
QUERY;
}

/**
* Retrieve customer authorization headers
*
* @param string $username
* @param string $password
* @return array
* @throws AuthenticationException
*/
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}