This repository was archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
Query for retrieving shopping cart information #266
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dabc886
Added possibility to retrieve cart information
rogyar 0ad09b2
Merge branch '2.3-develop' into get-cart
rogyar f5608dd
Merge branch '2.3-develop' into get-cart
rogyar a53a63e
API-functional tests added
rogyar 6e2f29c
Removed unnecessary annotation
rogyar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?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\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\QuoteGraphQl\Model\Cart\GetCartForUser; | ||
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromCart; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class Cart implements ResolverInterface | ||
{ | ||
/** | ||
* @var ExtractDataFromCart | ||
*/ | ||
private $extractDataFromCart; | ||
|
||
/** | ||
* @var GetCartForUser | ||
*/ | ||
private $getCartForUser; | ||
|
||
/** | ||
* @param GetCartForUser $getCartForUser | ||
* @param ExtractDataFromCart $extractDataFromCart | ||
*/ | ||
public function __construct( | ||
GetCartForUser $getCartForUser, | ||
ExtractDataFromCart $extractDataFromCart | ||
) { | ||
$this->getCartForUser = $getCartForUser; | ||
$this->extractDataFromCart = $extractDataFromCart; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($args['cart_id'])) { | ||
throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); | ||
} | ||
$maskedCartId = $args['cart_id']; | ||
|
||
$currentUserId = $context->getUserId(); | ||
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId); | ||
|
||
$data = array_merge( | ||
[ | ||
'cart_id' => $maskedCartId, | ||
'model' => $cart | ||
], | ||
$this->extractDataFromCart->execute($cart) | ||
); | ||
|
||
return $data; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Quote; | ||
|
||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; | ||
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test for getting cart information | ||
*/ | ||
class GetCartTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @var QuoteResource | ||
*/ | ||
private $quoteResource; | ||
|
||
/** | ||
* @var Quote | ||
*/ | ||
private $quote; | ||
|
||
/** | ||
* @var QuoteIdToMaskedQuoteIdInterface | ||
*/ | ||
private $quoteIdToMaskedId; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$objectManager = Bootstrap::getObjectManager(); | ||
$this->quoteResource = $objectManager->create(QuoteResource::class); | ||
$this->quote = $objectManager->create(Quote::class); | ||
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); | ||
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php | ||
*/ | ||
public function testGetOwnCartForRegisteredCustomer() | ||
{ | ||
$reservedOrderId = 'test_order_item_with_items'; | ||
$this->quoteResource->load( | ||
$this->quote, | ||
$reservedOrderId, | ||
'reserved_order_id' | ||
); | ||
|
||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); | ||
$query = $this->prepareGetCartQuery($maskedQuoteId); | ||
|
||
$response = $this->sendRequestWithToken($query); | ||
|
||
self::assertArrayHasKey('Cart', $response); | ||
self::assertNotEmpty($response['Cart']['items']); | ||
self::assertNotEmpty($response['Cart']['addresses']); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php | ||
*/ | ||
public function testGetCartFromAnotherCustomer() | ||
{ | ||
$reservedOrderId = 'test_order_item_with_items'; | ||
$this->quoteResource->load( | ||
$this->quote, | ||
$reservedOrderId, | ||
'reserved_order_id' | ||
); | ||
|
||
|
||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); | ||
$query = $this->prepareGetCartQuery($maskedQuoteId); | ||
|
||
self::expectExceptionMessage("The current user cannot perform operations on cart \"$maskedQuoteId\""); | ||
|
||
$this->graphQlQuery($query); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php | ||
*/ | ||
public function testGetCartForGuest() | ||
{ | ||
$reservedOrderId = 'test_order_1'; | ||
$this->quoteResource->load( | ||
$this->quote, | ||
$reservedOrderId, | ||
'reserved_order_id' | ||
); | ||
|
||
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); | ||
$query = $this->prepareGetCartQuery($maskedQuoteId); | ||
|
||
$response = $this->graphQlQuery($query); | ||
|
||
self::assertArrayHasKey('Cart', $response); | ||
} | ||
|
||
public function testGetNonExistentCart() | ||
{ | ||
$maskedQuoteId = 'non_existent_masked_id'; | ||
$query = $this->prepareGetCartQuery($maskedQuoteId); | ||
|
||
self::expectExceptionMessage("Could not find a cart with ID \"$maskedQuoteId\""); | ||
|
||
$this->graphQlQuery($query); | ||
} | ||
|
||
/** | ||
* Generates query for setting the specified shipping method on cart | ||
* | ||
* @param string $maskedQuoteId | ||
* @return string | ||
*/ | ||
private function prepareGetCartQuery( | ||
string $maskedQuoteId | ||
) : string { | ||
return <<<QUERY | ||
{ | ||
Cart(cart_id: "$maskedQuoteId") { | ||
applied_coupon { | ||
code | ||
} | ||
items { | ||
id | ||
} | ||
addresses { | ||
firstname, | ||
lastname, | ||
address_type | ||
} | ||
} | ||
} | ||
|
||
QUERY; | ||
} | ||
|
||
/** | ||
* Sends a GraphQL request with using a bearer token | ||
* | ||
* @param string $query | ||
* @return array | ||
* @throws \Magento\Framework\Exception\AuthenticationException | ||
*/ | ||
private function sendRequestWithToken(string $query): array | ||
{ | ||
|
||
$customerToken = $this->customerTokenService->createCustomerAccessToken('[email protected]', 'password'); | ||
$headerMap = ['Authorization' => 'Bearer ' . $customerToken]; | ||
|
||
return $this->graphQlQuery($query, [], '', $headerMap); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,11 @@ | |
->setIsMultiShipping(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, add rollback for this fixture There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rollback for the mentioned fixture exists (active_quote_rollback.php). As for masked id - the corresponding database entry is being removed when the quote is removed, so the current rollback works for masked ids as well (have just double checked this behavior) |
||
->setReservedOrderId('test_order_1') | ||
->save(); | ||
|
||
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ | ||
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() | ||
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class) | ||
->create(); | ||
$quoteIdMask->setQuoteId($quote->getId()); | ||
$quoteIdMask->setDataChanges(true); | ||
$quoteIdMask->save(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to load Quote?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me shed some light here.
I use this fixture because we perform tests for this quote. I use this fixture since it assigns maskedQuoteId to the quote. Also, this fixture provides a guest quote (it's not assigned to a customer)
So, the flow is: we load the guest quote, we perform tests for the guest quote.
As for the model loading, we need to get maskedQuoteId somehow but we don't have a quoteId that can be used for quoteIdToMaskedId service (only reservedOrderId). So, I load the model for retrieving quoteId.