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

Commit

Permalink
ENGCOM-5544: [Checkout] Prevent generating new masked quote id for sa…
Browse files Browse the repository at this point in the history
…me quote #797
  • Loading branch information
lenaorobei committed Aug 12, 2019
2 parents de7187a + eaab479 commit c115516
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;

/**
Expand All @@ -31,19 +33,27 @@ class CreateEmptyCartForCustomer
*/
private $quoteIdMaskResourceModel;

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

/**
* @param CartManagementInterface $cartManagement
* @param QuoteIdMaskFactory $quoteIdMaskFactory
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
*/
public function __construct(
CartManagementInterface $cartManagement,
QuoteIdMaskFactory $quoteIdMaskFactory,
QuoteIdMaskResourceModel $quoteIdMaskResourceModel
QuoteIdMaskResourceModel $quoteIdMaskResourceModel,
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
) {
$this->cartManagement = $cartManagement;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel;
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
}

/**
Expand All @@ -55,16 +65,56 @@ public function __construct(
*/
public function execute(int $customerId, string $predefinedMaskedQuoteId = null): string
{
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);
$quoteId = (int) $this->cartManagement->createEmptyCartForCustomer($customerId);

if ($predefinedMaskedQuoteId !== null) {
$maskedId = $this->createPredefinedMaskId($quoteId, $predefinedMaskedQuoteId);
} else {
$maskedId = $this->getQuoteMaskId($quoteId);
}

return $maskedId;
}

/**
* Create quote masked id from predefined value
*
* @param int $quoteId
* @param string $maskId
* @return string
* @throws \Magento\Framework\Exception\AlreadyExistsException
*/
private function createPredefinedMaskId(int $quoteId, string $maskId): string
{
/** @var QuoteIdMask $quoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($quoteId);

if (isset($predefinedMaskedQuoteId)) {
$quoteIdMask->setMaskedId($predefinedMaskedQuoteId);
}
$quoteIdMask->setMaskedId($maskId);

$this->quoteIdMaskResourceModel->save($quoteIdMask);

return $quoteIdMask->getMaskedId();
}

/**
* Fetch or create masked id for customer's active quote
*
* @param int $quoteId
* @return string
* @throws \Magento\Framework\Exception\AlreadyExistsException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getQuoteMaskId(int $quoteId): string
{
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
if ($maskedId === '') {
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($quoteId);

$this->quoteIdMaskResourceModel->save($quoteIdMask);
$maskedId = $quoteIdMask->getMaskedId();
}

return $maskedId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ public function testCreateEmptyCart()
self::assertEquals('default', $guestCart->getStore()->getCode());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCreateEmptyMultipleRequestsCart()
{
$query = $this->getQuery();
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());

self::assertArrayHasKey('createEmptyCart', $response);
self::assertNotEmpty($response['createEmptyCart']);
$maskedCartId = $response['createEmptyCart'];

$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
self::assertArrayHasKey('createEmptyCart', $response);
self::assertNotEmpty($response['createEmptyCart']);

self::assertEquals($maskedCartId, $response['createEmptyCart']);
}

/**
* @magentoApiDataFixture Magento/Store/_files/second_store.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
Expand Down

0 comments on commit c115516

Please sign in to comment.