From 4b89ee0063b69c80e52bbfab9be1830acff60266 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Tue, 5 Mar 2019 16:53:36 +0100 Subject: [PATCH 1/8] Cart totals implementation --- .../QuoteGraphQl/Model/Resolver/CartTaxes.php | 65 +++++++++ .../QuoteGraphQl/Model/Resolver/Totals.php | 74 ++++++++++ .../Magento/QuoteGraphQl/etc/schema.graphqls | 14 ++ .../GraphQl/Quote/Customer/CartTotalsTest.php | 132 ++++++++++++++++++ .../_files/product_simple_with_tax.php | 45 ++++++ .../product_simple_with_tax_rollback.php | 8 ++ .../Checkout/_files/quote_with_tax.php | 67 +++++++++ .../_files/quote_with_tax_rollback.php | 22 +++ .../_files/customer_with_tax_group.php | 10 ++ .../customer_with_tax_group_rollback.php | 7 + 10 files changed, 444 insertions(+) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax.php create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_rollback.php create mode 100644 dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php create mode 100644 dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php new file mode 100644 index 000000000000..53782c488fb8 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php @@ -0,0 +1,65 @@ +totalsCollector = $totalsCollector; + } + + /** + * @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')); + } + + $data = []; + + /** @var Quote $quote */ + $quote = $value['model']; + $appliedTaxes = $this->totalsCollector->collectQuoteTotals($value['model'])->getAppliedTaxes(); + + if (count($appliedTaxes) == 0) { + return []; + } + + $currency = $quote->getQuoteCurrencyCode(); + foreach ($appliedTaxes as $appliedTax) { + $data[] = [ + 'label' => $appliedTax['id'], + 'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency] + ]; + } + + return $data; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php new file mode 100644 index 000000000000..fd77d7a9d0ef --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php @@ -0,0 +1,74 @@ +cartTotalRepository = $cartTotalRepository; + } + + /** + * @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')); + } + + $cartTotals = $this->cartTotalRepository->get($value['model']->getId()); + + $currency = $cartTotals->getQuoteCurrencyCode(); + $data = $this->addCurrencyCode([ + 'grand_total' => ['value' => $cartTotals->getGrandTotal(), ], + 'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax()], + 'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal()], + 'subtotal_with_discount_excluding_tax' => ['value' => $cartTotals->getSubtotalWithDiscount()] + ], $currency); + + $data['model'] = $value['model']; + + return $data; + } + + /** + * Adds currency code to the totals + * + * @param array $totals + * @param string|null $currencyCode + * @return array + */ + private function addCurrencyCode(array $totals, $currencyCode): array + { + foreach ($totals as &$total) { + $total['currency'] = $currencyCode; + } + + return $totals; + } +} diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index e4ced2351572..24694303f4ec 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -120,6 +120,19 @@ input PaymentMethodInput { additional_data: PaymentMethodAdditionalDataInput } +type CartPrices { + grand_total: Money + subtotal_including_tax: Money + subtotal_excluding_tax: Money + subtotal_with_discount_excluding_tax: Money + applied_taxes: [CartTaxItem] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartTaxes") +} + +type CartTaxItem { + amount: Money! + label: String! +} + input PaymentMethodAdditionalDataInput { } @@ -150,6 +163,7 @@ type Cart { billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress") available_payment_methods: [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods") selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod") + prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Totals") } type CartAddress { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php new file mode 100644 index 000000000000..36da00bdfa79 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -0,0 +1,132 @@ +quoteResource = $objectManager->create(QuoteResource::class); + $this->quoteFactory = $objectManager->create(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->create(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax.php + */ + public function testGetCartTotalsForCustomerWithTaxApplied() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_tax'); + + $query = <<sendRequestWithToken($query); + + self::assertArrayHasKey('prices', $response['cart']); + $pricesResponse = $response['cart']['prices']; + self::assertEquals(10, $pricesResponse['grand_total']['value']); + self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + + $appliedTaxesResponse = $pricesResponse['applied_taxes']; + + self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); + self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); + self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); + } + + /** + * @param string $reversedQuoteId + * @return string + */ + private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * 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('customer@example.com', 'password'); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + + return $this->graphQlQuery($query, [], '', $headerMap); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php new file mode 100644 index 000000000000..00c2e6673646 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php @@ -0,0 +1,45 @@ +create(ProductRepositoryInterface::class); +/** @var ProductFactory $productFactory */ +$productFactory = $objectManager->create(ProductFactory::class); +$product = $productFactory->create(); +$product + ->setTypeId('simple') + ->setId(1) + ->setAttributeSetId(4) + ->setWebsiteIds([1]) + ->setName('Simple Product') + ->setSku('simple') + ->setPrice(10) + ->setMetaTitle('meta title') + ->setMetaKeyword('meta keyword') + ->setMetaDescription('meta description') + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 22, 'is_in_stock' => 1]) + ->setQty(22); + + +/** @var TaxClassCollectionFactory $taxClassCollectionFactory */ +$taxClassCollectionFactory = $objectManager->create(TaxClassCollectionFactory::class); +$taxClassCollection = $taxClassCollectionFactory->create(); + +/** @var TaxClassModel $taxClass */ +$taxClassCollection->addFieldToFilter('class_type', TaxClassModel::TAX_CLASS_TYPE_PRODUCT); +$taxClass = $taxClassCollection->getFirstItem(); + +$product->setCustomAttribute('tax_class_id', $taxClass->getClassId()); +$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php new file mode 100644 index 000000000000..a7d58fdf7f6f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php @@ -0,0 +1,8 @@ +get(AddressRepositoryInterface::class); +$customerAddress = $addressRepository->getById(1); +$customerAddress->setRegionId(12); // Taxable region +$addressRepository->save($customerAddress); +/** @var CustomerRepositoryInterface $customerRepository */ +$customerRepository = $objectManager->create(CustomerRepositoryInterface::class); +$customer = $customerRepository->get('customer@example.com'); + +/** @var Address $quoteAddress */ +$quoteAddress = $objectManager->create(Address::class); +$quoteAddress->importCustomerAddressData($customerAddress); + +/** @var Quote $quote */ +$quote = $objectManager->create(Quote::class); +$quote->setStoreId( + 1 +)->setIsActive( + true +)->setIsMultiShipping( + false +)->assignCustomer( + $customer +)->setShippingAddress( + $quoteAddress +)->setBillingAddress( + $quoteAddress +)->setCheckoutMethod( + 'customer' +)->setReservedOrderId( + 'test_order_tax' +)->addProduct( + $product +); + +$quote->getShippingAddress()->setRegionId(12); + +$quoteRepository = $objectManager->get( + \Magento\Quote\Api\CartRepositoryInterface::class +); + +$quoteRepository->save($quote); + +/** @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(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_rollback.php new file mode 100644 index 000000000000..1e1eb326d7c8 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_rollback.php @@ -0,0 +1,22 @@ +create(Quote::class); +$quote->load('test_order_tax', 'reserved_order_id')->delete(); + +/** @var QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create(QuoteIdMask::class); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php new file mode 100644 index 000000000000..4792df5cdb2f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php @@ -0,0 +1,10 @@ +setGroupId(3); // 3 is a predefined retailer group +$customer->save(); diff --git a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php new file mode 100644 index 000000000000..48a09a41c7e0 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php @@ -0,0 +1,7 @@ + Date: Sat, 9 Mar 2019 11:51:55 +0100 Subject: [PATCH 2/8] Added test coverage for guest user --- .../GraphQl/Quote/Customer/CartTotalsTest.php | 2 +- .../GraphQl/Quote/Guest/CartTotalsTest.php | 116 ++++++++++++++++++ ...th_tax.php => quote_with_tax_customer.php} | 0 ...p => quote_with_tax_customer_rollback.php} | 0 .../Checkout/_files/quote_with_tax_guest.php | 64 ++++++++++ .../_files/quote_with_tax_guest_rollback.php | 21 ++++ 6 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php rename dev/tests/integration/testsuite/Magento/Checkout/_files/{quote_with_tax.php => quote_with_tax_customer.php} (100%) rename dev/tests/integration/testsuite/Magento/Checkout/_files/{quote_with_tax_rollback.php => quote_with_tax_customer_rollback.php} (100%) create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php index 36da00bdfa79..a6ddc7cff20c 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -49,7 +49,7 @@ protected function setUp() } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax_customer.php */ public function testGetCartTotalsForCustomerWithTaxApplied() { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php new file mode 100644 index 000000000000..9396af53e0cc --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php @@ -0,0 +1,116 @@ +quoteResource = $objectManager->create(QuoteResource::class); + $this->quoteFactory = $objectManager->create(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->create(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax_guest.php + */ + public function testGetCartTotalsForCustomerWithTaxApplied() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_tax'); + + $query = <<graphQlQuery($query); + + self::assertArrayHasKey('prices', $response['cart']); + $pricesResponse = $response['cart']['prices']; + self::assertEquals(10, $pricesResponse['grand_total']['value']); + self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + + $appliedTaxesResponse = $pricesResponse['applied_taxes']; + + self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); + self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); + self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); + } + + /** + * @param string $reversedQuoteId + * @return string + */ + private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php similarity index 100% rename from dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax.php rename to dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer_rollback.php similarity index 100% rename from dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_rollback.php rename to dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer_rollback.php diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php new file mode 100644 index 000000000000..75c20e45f11e --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php @@ -0,0 +1,64 @@ +create(AddressInterface::class); +$quoteAddress->setData( + [ + 'telephone' => 3468676, + 'postcode' => 75477, + 'country_id' => 'US', + 'city' => 'CityM', + 'company' => 'CompanyName', + 'street' => 'Green str, 67', + 'lastname' => 'Smith', + 'firstname' => 'John', + 'region_id' => 12 + ] +); +//$quoteAddress->save(); + +/** @var Quote $quote */ +$quote = $objectManager->create(Quote::class); +$quote->setStoreId( + 1 +)->setIsActive( + true +)->setIsMultiShipping( + false +)->setShippingAddress( + $quoteAddress +)->setBillingAddress( + $quoteAddress +)->setCheckoutMethod( + 'customer' +)->setReservedOrderId( + 'test_order_tax' +)->addProduct( + $product +); + +$quote->getShippingAddress()->setRegionId(12); + +$quoteRepository = $objectManager->get( + \Magento\Quote\Api\CartRepositoryInterface::class +); + +$quoteRepository->save($quote); + +/** @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(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php new file mode 100644 index 000000000000..6fcbdf7276de --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php @@ -0,0 +1,21 @@ +create(Quote::class); +$quote->load('test_order_tax', 'reserved_order_id')->delete(); + +/** @var QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create(QuoteIdMask::class); +$quoteIdMask->delete($quote->getId()); From 8210d824dc4accdb631a03f47f3d311e2ff74ad5 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sat, 9 Mar 2019 12:40:56 +0100 Subject: [PATCH 3/8] Use a single resolver for totals/taxes for performance improvement --- .../QuoteGraphQl/Model/Resolver/CartTaxes.php | 65 ------------------- .../QuoteGraphQl/Model/Resolver/Totals.php | 62 +++++++++++------- .../Magento/QuoteGraphQl/etc/schema.graphqls | 2 +- .../GraphQl/Quote/Customer/CartTotalsTest.php | 2 +- .../GraphQl/Quote/Guest/CartTotalsTest.php | 2 +- 5 files changed, 40 insertions(+), 93 deletions(-) delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php deleted file mode 100644 index 53782c488fb8..000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartTaxes.php +++ /dev/null @@ -1,65 +0,0 @@ -totalsCollector = $totalsCollector; - } - - /** - * @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')); - } - - $data = []; - - /** @var Quote $quote */ - $quote = $value['model']; - $appliedTaxes = $this->totalsCollector->collectQuoteTotals($value['model'])->getAppliedTaxes(); - - if (count($appliedTaxes) == 0) { - return []; - } - - $currency = $quote->getQuoteCurrencyCode(); - foreach ($appliedTaxes as $appliedTax) { - $data[] = [ - 'label' => $appliedTax['id'], - 'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency] - ]; - } - - return $data; - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php index fd77d7a9d0ef..926f2b2de3cf 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php @@ -11,7 +11,8 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Quote\Api\CartTotalRepositoryInterface; +use Magento\Quote\Model\Quote\Address\Total; +use Magento\Quote\Model\Quote\TotalsCollector; /** * @inheritdoc @@ -19,17 +20,17 @@ class Totals implements ResolverInterface { /** - * @var CartTotalRepositoryInterface + * @var TotalsCollector */ - private $cartTotalRepository; + private $totalsCollector; /** - * @param CartTotalRepositoryInterface $cartTotalRepository + * @param TotalsCollector $totalsCollector */ public function __construct( - CartTotalRepositoryInterface $cartTotalRepository + TotalsCollector $totalsCollector ) { - $this->cartTotalRepository = $cartTotalRepository; + $this->totalsCollector = $totalsCollector; } /** @@ -41,34 +42,45 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value throw new LocalizedException(__('"model" value should be specified')); } - $cartTotals = $this->cartTotalRepository->get($value['model']->getId()); + /** @var Quote $quote */ + $quote = $value['model']; + $cartTotals = $this->totalsCollector->collectQuoteTotals($quote); + $currency = $quote->getQuoteCurrencyCode(); - $currency = $cartTotals->getQuoteCurrencyCode(); - $data = $this->addCurrencyCode([ - 'grand_total' => ['value' => $cartTotals->getGrandTotal(), ], - 'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax()], - 'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal()], - 'subtotal_with_discount_excluding_tax' => ['value' => $cartTotals->getSubtotalWithDiscount()] - ], $currency); - - $data['model'] = $value['model']; - - return $data; + return [ + 'grand_total' => ['value' => $cartTotals->getGrandTotal(), 'currency' => $currency], + 'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax(), 'currency' => $currency], + 'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal(), 'currency' => $currency], + 'subtotal_with_discount_excluding_tax' => [ + 'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency + ], + 'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency), + 'model' => $quote + ]; } /** - * Adds currency code to the totals + * Returns taxes applied to the current quote * - * @param array $totals - * @param string|null $currencyCode + * @param Total $total + * @param string $currency * @return array */ - private function addCurrencyCode(array $totals, $currencyCode): array + private function getAppliedTaxes(Total $total, string $currency): array { - foreach ($totals as &$total) { - $total['currency'] = $currencyCode; + $appliedTaxes = $total->getAppliedTaxes(); + + if (count($appliedTaxes) === 0) { + return []; + } + + foreach ($appliedTaxes as $appliedTax) { + $appliedTaxesData[] = [ + 'label' => $appliedTax['id'], + 'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency] + ]; } - return $totals; + return $appliedTaxesData; } } diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 24694303f4ec..516da1512d04 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -125,7 +125,7 @@ type CartPrices { subtotal_including_tax: Money subtotal_excluding_tax: Money subtotal_with_discount_excluding_tax: Money - applied_taxes: [CartTaxItem] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartTaxes") + applied_taxes: [CartTaxItem] } type CartTaxItem { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php index a6ddc7cff20c..f50ebf38aa1e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -90,7 +90,7 @@ public function testGetCartTotalsForCustomerWithTaxApplied() self::assertArrayHasKey('prices', $response['cart']); $pricesResponse = $response['cart']['prices']; - self::assertEquals(10, $pricesResponse['grand_total']['value']); + self::assertEquals(10.83, $pricesResponse['grand_total']['value']); self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php index 9396af53e0cc..b399fea37951 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php @@ -90,7 +90,7 @@ public function testGetCartTotalsForCustomerWithTaxApplied() self::assertArrayHasKey('prices', $response['cart']); $pricesResponse = $response['cart']['prices']; - self::assertEquals(10, $pricesResponse['grand_total']['value']); + self::assertEquals(10.83, $pricesResponse['grand_total']['value']); self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); From 269730337b2e10b6d4addc85a0cc60fff55e4289 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sat, 9 Mar 2019 15:30:01 +0100 Subject: [PATCH 4/8] Test coverage for cart with no tax applied --- .../GraphQl/Quote/Customer/CartTotalsTest.php | 68 ++++++++++++------ .../GraphQl/Quote/Guest/CartTotalsTest.php | 72 ++++++++++++------- .../_files/quote_with_address_guest.php | 61 ++++++++++++++++ .../quote_with_address_guest_rollback.php | 21 ++++++ .../Checkout/_files/quote_with_tax_guest.php | 1 - 5 files changed, 176 insertions(+), 47 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest.php create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php index f50ebf38aa1e..7efbc61be6cd 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -42,20 +42,62 @@ class CartTotalsTest extends GraphQlAbstract protected function setUp() { $objectManager = Bootstrap::getObjectManager(); - $this->quoteResource = $objectManager->create(QuoteResource::class); - $this->quoteFactory = $objectManager->create(QuoteFactory::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); - $this->customerTokenService = $objectManager->create(CustomerTokenServiceInterface::class); + $this->quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); } /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax_customer.php */ - public function testGetCartTotalsForCustomerWithTaxApplied() + public function testGetCartTotalsWithTaxApplied() { $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_tax'); + $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $response = $this->sendRequestWithToken($query); + + self::assertArrayHasKey('prices', $response['cart']); + $pricesResponse = $response['cart']['prices']; + self::assertEquals(10.83, $pricesResponse['grand_total']['value']); + self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + + $appliedTaxesResponse = $pricesResponse['applied_taxes']; + + self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); + self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); + self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @group last + */ + public function testGetTotalsWithNoTaxApplied() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $response = $this->sendRequestWithToken($query); + + $pricesResponse = $response['cart']['prices']; + self::assertEquals(20, $pricesResponse['grand_total']['value']); + self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEmpty($pricesResponse['applied_taxes']); + } - $query = <<sendRequestWithToken($query); - - self::assertArrayHasKey('prices', $response['cart']); - $pricesResponse = $response['cart']['prices']; - self::assertEquals(10.83, $pricesResponse['grand_total']['value']); - self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); - - $appliedTaxesResponse = $pricesResponse['applied_taxes']; - - self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); - self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); - self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); } /** diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php index b399fea37951..d634bd0acbaf 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php @@ -7,7 +7,6 @@ namespace Magento\GraphQl\Quote\Guest; -use Magento\Integration\Api\CustomerTokenServiceInterface; use Magento\Quote\Model\QuoteFactory; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; @@ -19,11 +18,6 @@ */ class CartTotalsTest extends GraphQlAbstract { - /** - * @var CustomerTokenServiceInterface - */ - private $customerTokenService; - /** * @var QuoteResource */ @@ -42,20 +36,60 @@ class CartTotalsTest extends GraphQlAbstract protected function setUp() { $objectManager = Bootstrap::getObjectManager(); - $this->quoteResource = $objectManager->create(QuoteResource::class); - $this->quoteFactory = $objectManager->create(QuoteFactory::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); - $this->customerTokenService = $objectManager->create(CustomerTokenServiceInterface::class); + $this->quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); } /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax_guest.php */ - public function testGetCartTotalsForCustomerWithTaxApplied() + public function testGetCartTotalsWithTaxApplied() { $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_tax'); + $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('prices', $response['cart']); + $pricesResponse = $response['cart']['prices']; + self::assertEquals(10.83, $pricesResponse['grand_total']['value']); + self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + + $appliedTaxesResponse = $pricesResponse['applied_taxes']; + + self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); + self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); + self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_guest.php + */ + public function testGetTotalsWithNoTaxApplied() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $response = $this->graphQlQuery($query); - $query = <<graphQlQuery($query); - - self::assertArrayHasKey('prices', $response['cart']); - $pricesResponse = $response['cart']['prices']; - self::assertEquals(10.83, $pricesResponse['grand_total']['value']); - self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); - - $appliedTaxesResponse = $pricesResponse['applied_taxes']; - - self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); - self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); - self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); } /** diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest.php new file mode 100644 index 000000000000..da496f01ec05 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest.php @@ -0,0 +1,61 @@ +create(AddressInterface::class); +$quoteAddress->setData( + [ + 'telephone' => 3468676, + 'postcode' => 75477, + 'country_id' => 'US', + 'city' => 'CityM', + 'company' => 'CompanyName', + 'street' => 'Green str, 67', + 'lastname' => 'Smith', + 'firstname' => 'John', + 'region_id' => 1 + ] +); + +/** @var Quote $quote */ +$quote = $objectManager->create(Quote::class); +$quote->setStoreId( + 1 +)->setIsActive( + true +)->setIsMultiShipping( + false +)->setShippingAddress( + $quoteAddress +)->setBillingAddress( + $quoteAddress +)->setCheckoutMethod( + 'customer' +)->setReservedOrderId( + 'test_order_1' +)->addProduct( + $product +); + +$quoteRepository = $objectManager->get( + \Magento\Quote\Api\CartRepositoryInterface::class +); + +$quoteRepository->save($quote); + +/** @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(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php new file mode 100644 index 000000000000..3130ca9353e9 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php @@ -0,0 +1,21 @@ +create(Quote::class); +$quote->load('test_order_1', 'reserved_order_id')->delete(); + +/** @var QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create(QuoteIdMask::class); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php index 75c20e45f11e..09d47f3cdca7 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php @@ -25,7 +25,6 @@ 'region_id' => 12 ] ); -//$quoteAddress->save(); /** @var Quote $quote */ $quote = $objectManager->create(Quote::class); From 2aeb8748ce4cf467e81744276c81dd521452a6fe Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sun, 10 Mar 2019 09:58:15 +0100 Subject: [PATCH 5/8] Test case for getting totals with no quote address set --- .../GraphQl/Quote/Customer/CartTotalsTest.php | 21 +++++++++++++++++- .../GraphQl/Quote/Guest/CartTotalsTest.php | 20 +++++++++++++++++ ...uote_with_customer_no_address_rollback.php | 22 +++++++++++++++++++ .../_files/quote_with_tax_customer.php | 4 ++-- .../Checkout/_files/quote_with_tax_guest.php | 6 ++--- 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php index 7efbc61be6cd..8dc6650b68f9 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -73,7 +73,6 @@ public function testGetCartTotalsWithTaxApplied() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - * @group last */ public function testGetTotalsWithNoTaxApplied() { @@ -89,6 +88,26 @@ public function testGetTotalsWithNoTaxApplied() self::assertEmpty($pricesResponse['applied_taxes']); } + /** + * The totals calculation is based on quote address. + * But the totals should be calculated even if no address is set + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_customer_no_address.php + */ + public function testGetCartTotalsWithNoAddressSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $response = $this->sendRequestWithToken($query); + + $pricesResponse = $response['cart']['prices']; + self::assertEquals(10, $pricesResponse['grand_total']['value']); + self::assertEquals(10, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEmpty($pricesResponse['applied_taxes']); + } + /** * Generates GraphQl query for retrieving cart totals * diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php index d634bd0acbaf..19ef071b5040 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php @@ -81,6 +81,26 @@ public function testGetTotalsWithNoTaxApplied() self::assertEmpty($pricesResponse['applied_taxes']); } + /** + * The totals calculation is based on quote address. + * But the totals should be calculated even if no address is set + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testGetCartTotalsWithNoAddressSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $response = $this->graphQlQuery($query); + + $pricesResponse = $response['cart']['prices']; + self::assertEquals(10, $pricesResponse['grand_total']['value']); + self::assertEquals(10, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEmpty($pricesResponse['applied_taxes']); + } + /** * Generates GraphQl query for retrieving cart totals * diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php new file mode 100644 index 000000000000..83c80d736c16 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php @@ -0,0 +1,22 @@ +create(Quote::class); +$quote->load('test_order_1', 'reserved_order_id')->delete(); + +/** @var QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager->create(QuoteIdMask::class); +$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php index a8a02f5fe87e..e9cb45b546a4 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php @@ -59,8 +59,8 @@ $quoteRepository->save($quote); /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ -$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() - ->create(\Magento\Quote\Model\QuoteIdMaskFactory::class) +$quoteIdMask = $objectManager + ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class) ->create(); $quoteIdMask->setQuoteId($quote->getId()); $quoteIdMask->setDataChanges(true); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php index 09d47f3cdca7..78d5b0739d76 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php @@ -39,7 +39,7 @@ )->setBillingAddress( $quoteAddress )->setCheckoutMethod( - 'customer' + 'guest' )->setReservedOrderId( 'test_order_tax' )->addProduct( @@ -55,8 +55,8 @@ $quoteRepository->save($quote); /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ -$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() - ->create(\Magento\Quote\Model\QuoteIdMaskFactory::class) +$quoteIdMask = $objectManager + ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class) ->create(); $quoteIdMask->setQuoteId($quote->getId()); $quoteIdMask->setDataChanges(true); From 7cca7811bcc6004c6b9ae826ccdbd22c6be27e1d Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sun, 10 Mar 2019 09:58:59 +0100 Subject: [PATCH 6/8] Missing fixture --- .../_files/quote_with_customer_no_address.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php new file mode 100644 index 000000000000..ef030f8bcd92 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php @@ -0,0 +1,48 @@ +create(CustomerRepositoryInterface::class); +$customer = $customerRepository->get('customer@example.com'); + +/** @var Quote $quote */ +$quote = $objectManager->create(Quote::class); +$quote->setStoreId( + 1 +)->setIsActive( + true +)->setIsMultiShipping( + false +)->assignCustomer( + $customer +)->setCheckoutMethod( + 'customer' +)->setReservedOrderId( + 'test_order_1' +)->addProduct( + $product +); + +$quoteRepository = $objectManager->get( + \Magento\Quote\Api\CartRepositoryInterface::class +); + +$quoteRepository->save($quote); + +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ +$quoteIdMask = $objectManager + ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class) + ->create(); +$quoteIdMask->setQuoteId($quote->getId()); +$quoteIdMask->setDataChanges(true); +$quoteIdMask->save(); From 9e8519de2367581ff26dc76432e88a7ccbf987d8 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 11 Apr 2019 12:24:00 +0200 Subject: [PATCH 7/8] Tests refactoring based on new fixtures principle --- .../GraphQl/Quote/Customer/CartTotalsTest.php | 47 ++++++++----- .../GraphQl/Quote/Guest/CartTotalsTest.php | 53 +++++++++------ .../_files/product_simple_with_tax.php | 45 ------------- .../product_simple_with_tax_rollback.php | 8 --- .../_files/quote_with_address_guest.php | 61 ----------------- .../quote_with_address_guest_rollback.php | 21 ------ .../_files/quote_with_customer_no_address.php | 48 ------------- ...uote_with_customer_no_address_rollback.php | 22 ------ .../_files/quote_with_tax_customer.php | 67 ------------------- .../quote_with_tax_customer_rollback.php | 22 ------ .../Checkout/_files/quote_with_tax_guest.php | 63 ----------------- .../_files/quote_with_tax_guest_rollback.php | 21 ------ .../_files/customer_with_tax_group.php | 10 --- .../customer_with_tax_group_rollback.php | 7 -- .../_files/apply_tax_for_simple_product.php | 26 +++++++ .../Tax/_files/tax_rule_for_region_1.php | 53 +++++++++++++++ .../_files/tax_rule_for_region_1_rollback.php | 38 +++++++++++ 17 files changed, 181 insertions(+), 431 deletions(-) delete mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php delete mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer_rollback.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php delete mode 100644 dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php delete mode 100644 dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php index 8dc6650b68f9..4a25e9331d86 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -49,34 +49,46 @@ protected function setUp() } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax_customer.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_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/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php */ public function testGetCartTotalsWithTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_tax'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); $response = $this->sendRequestWithToken($query); self::assertArrayHasKey('prices', $response['cart']); $pricesResponse = $response['cart']['prices']; - self::assertEquals(10.83, $pricesResponse['grand_total']['value']); - self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEquals(21.5, $pricesResponse['grand_total']['value']); + self::assertEquals(21.5, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); $appliedTaxesResponse = $pricesResponse['applied_taxes']; - self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); - self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); + self::assertEquals('US-TEST-*-Rate-1', $appliedTaxesResponse[0]['label']); + self::assertEquals(1.5, $appliedTaxesResponse[0]['amount']['value']); self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @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/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php */ public function testGetTotalsWithNoTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); $response = $this->sendRequestWithToken($query); @@ -92,19 +104,22 @@ public function testGetTotalsWithNoTaxApplied() * The totals calculation is based on quote address. * But the totals should be calculated even if no address is set * - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_customer_no_address.php + * @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 */ public function testGetCartTotalsWithNoAddressSet() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); $response = $this->sendRequestWithToken($query); $pricesResponse = $response['cart']['prices']; - self::assertEquals(10, $pricesResponse['grand_total']['value']); - self::assertEquals(10, $pricesResponse['subtotal_including_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['grand_total']['value']); + self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); self::assertEmpty($pricesResponse['applied_taxes']); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php index 19ef071b5040..9fec6fdfd269 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php @@ -42,42 +42,52 @@ protected function setUp() } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_tax_guest.php + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php */ public function testGetCartTotalsWithTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_tax'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); $response = $this->graphQlQuery($query); self::assertArrayHasKey('prices', $response['cart']); $pricesResponse = $response['cart']['prices']; - self::assertEquals(10.83, $pricesResponse['grand_total']['value']); - self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEquals(21.5, $pricesResponse['grand_total']['value']); + self::assertEquals(21.5, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); $appliedTaxesResponse = $pricesResponse['applied_taxes']; - self::assertEquals('US-CA-*-Rate 1', $appliedTaxesResponse[0]['label']); - self::assertEquals(0.83, $appliedTaxesResponse[0]['amount']['value']); + self::assertEquals('US-TEST-*-Rate-1', $appliedTaxesResponse[0]['label']); + self::assertEquals(1.5, $appliedTaxesResponse[0]['amount']['value']); self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_guest.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php */ public function testGetTotalsWithNoTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); $response = $this->graphQlQuery($query); $pricesResponse = $response['cart']['prices']; - self::assertEquals(10, $pricesResponse['grand_total']['value']); - self::assertEquals(10, $pricesResponse['subtotal_including_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['grand_total']['value']); + self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); self::assertEmpty($pricesResponse['applied_taxes']); } @@ -85,19 +95,22 @@ public function testGetTotalsWithNoTaxApplied() * The totals calculation is based on quote address. * But the totals should be calculated even if no address is set * - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @group recent */ public function testGetCartTotalsWithNoAddressSet() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); $response = $this->graphQlQuery($query); $pricesResponse = $response['cart']['prices']; - self::assertEquals(10, $pricesResponse['grand_total']['value']); - self::assertEquals(10, $pricesResponse['subtotal_including_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']); - self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['grand_total']['value']); + self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); self::assertEmpty($pricesResponse['applied_taxes']); } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php deleted file mode 100644 index 00c2e6673646..000000000000 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax.php +++ /dev/null @@ -1,45 +0,0 @@ -create(ProductRepositoryInterface::class); -/** @var ProductFactory $productFactory */ -$productFactory = $objectManager->create(ProductFactory::class); -$product = $productFactory->create(); -$product - ->setTypeId('simple') - ->setId(1) - ->setAttributeSetId(4) - ->setWebsiteIds([1]) - ->setName('Simple Product') - ->setSku('simple') - ->setPrice(10) - ->setMetaTitle('meta title') - ->setMetaKeyword('meta keyword') - ->setMetaDescription('meta description') - ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) - ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) - ->setStockData(['use_config_manage_stock' => 1, 'qty' => 22, 'is_in_stock' => 1]) - ->setQty(22); - - -/** @var TaxClassCollectionFactory $taxClassCollectionFactory */ -$taxClassCollectionFactory = $objectManager->create(TaxClassCollectionFactory::class); -$taxClassCollection = $taxClassCollectionFactory->create(); - -/** @var TaxClassModel $taxClass */ -$taxClassCollection->addFieldToFilter('class_type', TaxClassModel::TAX_CLASS_TYPE_PRODUCT); -$taxClass = $taxClassCollection->getFirstItem(); - -$product->setCustomAttribute('tax_class_id', $taxClass->getClassId()); -$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php deleted file mode 100644 index a7d58fdf7f6f..000000000000 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_tax_rollback.php +++ /dev/null @@ -1,8 +0,0 @@ -create(AddressInterface::class); -$quoteAddress->setData( - [ - 'telephone' => 3468676, - 'postcode' => 75477, - 'country_id' => 'US', - 'city' => 'CityM', - 'company' => 'CompanyName', - 'street' => 'Green str, 67', - 'lastname' => 'Smith', - 'firstname' => 'John', - 'region_id' => 1 - ] -); - -/** @var Quote $quote */ -$quote = $objectManager->create(Quote::class); -$quote->setStoreId( - 1 -)->setIsActive( - true -)->setIsMultiShipping( - false -)->setShippingAddress( - $quoteAddress -)->setBillingAddress( - $quoteAddress -)->setCheckoutMethod( - 'customer' -)->setReservedOrderId( - 'test_order_1' -)->addProduct( - $product -); - -$quoteRepository = $objectManager->get( - \Magento\Quote\Api\CartRepositoryInterface::class -); - -$quoteRepository->save($quote); - -/** @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(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php deleted file mode 100644 index 3130ca9353e9..000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_address_guest_rollback.php +++ /dev/null @@ -1,21 +0,0 @@ -create(Quote::class); -$quote->load('test_order_1', 'reserved_order_id')->delete(); - -/** @var QuoteIdMask $quoteIdMask */ -$quoteIdMask = $objectManager->create(QuoteIdMask::class); -$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php deleted file mode 100644 index ef030f8bcd92..000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address.php +++ /dev/null @@ -1,48 +0,0 @@ -create(CustomerRepositoryInterface::class); -$customer = $customerRepository->get('customer@example.com'); - -/** @var Quote $quote */ -$quote = $objectManager->create(Quote::class); -$quote->setStoreId( - 1 -)->setIsActive( - true -)->setIsMultiShipping( - false -)->assignCustomer( - $customer -)->setCheckoutMethod( - 'customer' -)->setReservedOrderId( - 'test_order_1' -)->addProduct( - $product -); - -$quoteRepository = $objectManager->get( - \Magento\Quote\Api\CartRepositoryInterface::class -); - -$quoteRepository->save($quote); - -/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ -$quoteIdMask = $objectManager - ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class) - ->create(); -$quoteIdMask->setQuoteId($quote->getId()); -$quoteIdMask->setDataChanges(true); -$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php deleted file mode 100644 index 83c80d736c16..000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_customer_no_address_rollback.php +++ /dev/null @@ -1,22 +0,0 @@ -create(Quote::class); -$quote->load('test_order_1', 'reserved_order_id')->delete(); - -/** @var QuoteIdMask $quoteIdMask */ -$quoteIdMask = $objectManager->create(QuoteIdMask::class); -$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php deleted file mode 100644 index e9cb45b546a4..000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer.php +++ /dev/null @@ -1,67 +0,0 @@ -get(AddressRepositoryInterface::class); -$customerAddress = $addressRepository->getById(1); -$customerAddress->setRegionId(12); // Taxable region -$addressRepository->save($customerAddress); -/** @var CustomerRepositoryInterface $customerRepository */ -$customerRepository = $objectManager->create(CustomerRepositoryInterface::class); -$customer = $customerRepository->get('customer@example.com'); - -/** @var Address $quoteAddress */ -$quoteAddress = $objectManager->create(Address::class); -$quoteAddress->importCustomerAddressData($customerAddress); - -/** @var Quote $quote */ -$quote = $objectManager->create(Quote::class); -$quote->setStoreId( - 1 -)->setIsActive( - true -)->setIsMultiShipping( - false -)->assignCustomer( - $customer -)->setShippingAddress( - $quoteAddress -)->setBillingAddress( - $quoteAddress -)->setCheckoutMethod( - 'customer' -)->setReservedOrderId( - 'test_order_tax' -)->addProduct( - $product -); - -$quote->getShippingAddress()->setRegionId(12); - -$quoteRepository = $objectManager->get( - \Magento\Quote\Api\CartRepositoryInterface::class -); - -$quoteRepository->save($quote); - -/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ -$quoteIdMask = $objectManager - ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class) - ->create(); -$quoteIdMask->setQuoteId($quote->getId()); -$quoteIdMask->setDataChanges(true); -$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer_rollback.php deleted file mode 100644 index 1e1eb326d7c8..000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_customer_rollback.php +++ /dev/null @@ -1,22 +0,0 @@ -create(Quote::class); -$quote->load('test_order_tax', 'reserved_order_id')->delete(); - -/** @var QuoteIdMask $quoteIdMask */ -$quoteIdMask = $objectManager->create(QuoteIdMask::class); -$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php deleted file mode 100644 index 78d5b0739d76..000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest.php +++ /dev/null @@ -1,63 +0,0 @@ -create(AddressInterface::class); -$quoteAddress->setData( - [ - 'telephone' => 3468676, - 'postcode' => 75477, - 'country_id' => 'US', - 'city' => 'CityM', - 'company' => 'CompanyName', - 'street' => 'Green str, 67', - 'lastname' => 'Smith', - 'firstname' => 'John', - 'region_id' => 12 - ] -); - -/** @var Quote $quote */ -$quote = $objectManager->create(Quote::class); -$quote->setStoreId( - 1 -)->setIsActive( - true -)->setIsMultiShipping( - false -)->setShippingAddress( - $quoteAddress -)->setBillingAddress( - $quoteAddress -)->setCheckoutMethod( - 'guest' -)->setReservedOrderId( - 'test_order_tax' -)->addProduct( - $product -); - -$quote->getShippingAddress()->setRegionId(12); - -$quoteRepository = $objectManager->get( - \Magento\Quote\Api\CartRepositoryInterface::class -); - -$quoteRepository->save($quote); - -/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ -$quoteIdMask = $objectManager - ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class) - ->create(); -$quoteIdMask->setQuoteId($quote->getId()); -$quoteIdMask->setDataChanges(true); -$quoteIdMask->save(); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php deleted file mode 100644 index 6fcbdf7276de..000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_tax_guest_rollback.php +++ /dev/null @@ -1,21 +0,0 @@ -create(Quote::class); -$quote->load('test_order_tax', 'reserved_order_id')->delete(); - -/** @var QuoteIdMask $quoteIdMask */ -$quoteIdMask = $objectManager->create(QuoteIdMask::class); -$quoteIdMask->delete($quote->getId()); diff --git a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php deleted file mode 100644 index 4792df5cdb2f..000000000000 --- a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group.php +++ /dev/null @@ -1,10 +0,0 @@ -setGroupId(3); // 3 is a predefined retailer group -$customer->save(); diff --git a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php deleted file mode 100644 index 48a09a41c7e0..000000000000 --- a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_with_tax_group_rollback.php +++ /dev/null @@ -1,7 +0,0 @@ -get(ProductRepositoryInterface::class); +$product = $productRepository->get('simple_product'); + +/** @var TaxClassCollectionFactory $taxClassCollectionFactory */ +$taxClassCollectionFactory = $objectManager->get(TaxClassCollectionFactory::class); +$taxClassCollection = $taxClassCollectionFactory->create(); + +/** @var TaxClassModel $taxClass */ +$taxClassCollection->addFieldToFilter('class_type', TaxClassModel::TAX_CLASS_TYPE_PRODUCT); +$taxClass = $taxClassCollection->getFirstItem(); + +$product->setCustomAttribute('tax_class_id', $taxClass->getClassId()); +$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php b/dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php new file mode 100644 index 000000000000..aca55bd8414f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php @@ -0,0 +1,53 @@ +get(DataObjectHelper::class); +/** @var RateFactory $rateFactory */ +$rateFactory = $objectManager->get(RateFactory::class); +/** @var RuleFactory $ruleFactory */ +$ruleFactory = $objectManager->get(RuleFactory::class); +/** @var RateRepository $rateRepository */ +$rateRepository = $objectManager->get(TaxRateRepositoryInterface::class); +/** @var TaxRuleRepository $ruleRepository */ +$ruleRepository = $objectManager->get(TaxRuleRepositoryInterface::class); +/** @var Rate $rate */ +$rate = $rateFactory->create(); +$rateData = [ + Rate::KEY_COUNTRY_ID => 'US', + Rate::KEY_REGION_ID => '1', + Rate::KEY_POSTCODE => '*', + Rate::KEY_CODE => 'US-TEST-*-Rate-1', + Rate::KEY_PERCENTAGE_RATE => '7.5', +]; +$dataObjectHelper->populateWithArray($rate, $rateData, TaxRateInterface::class); +$rateRepository->save($rate); + +$rule = $ruleFactory->create(); +$ruleData = [ + Rule::KEY_CODE=> 'GraphQl Test Rule', + Rule::KEY_PRIORITY => '0', + Rule::KEY_POSITION => '0', + Rule::KEY_CUSTOMER_TAX_CLASS_IDS => [3], + Rule::KEY_PRODUCT_TAX_CLASS_IDS => [2], + Rule::KEY_TAX_RATE_IDS => [$rate->getId()], +]; +$dataObjectHelper->populateWithArray($rule, $ruleData, TaxRuleInterface::class); +$ruleRepository->save($rule); diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1_rollback.php b/dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1_rollback.php new file mode 100644 index 000000000000..aba1960624ed --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Tax/_files/tax_rule_for_region_1_rollback.php @@ -0,0 +1,38 @@ +get(RateFactory::class); +/** @var RuleFactory $ruleFactory */ +$ruleFactory = $objectManager->get(RuleFactory::class); +/** @var RateRepository $rateRepository */ +$rateRepository = $objectManager->get(TaxRateRepositoryInterface::class); +/** @var TaxRuleRepository $ruleRepository */ +$ruleRepository = $objectManager->get(TaxRuleRepositoryInterface::class); +/** @var RateResource $rateResource */ +$rateResource = $objectManager->get(RateResource::class); +/** @var RuleResource $ruleResource */ +$ruleResource = $objectManager->get(RuleResource::class); + +$rate = $rateFactory->create(); +$rateResource->load($rate, 'US-TEST-*-Rate-1', Rate::KEY_CODE); +$rule = $ruleFactory->create(); +$ruleResource->load($rule, 'GraphQl Test Rule', Rule::KEY_CODE); +$ruleRepository->delete($rule); +$rateRepository->delete($rate); From 265d82e51adadf3d1c8ede0166c0aa82eac856f6 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Mon, 15 Apr 2019 12:31:55 -0500 Subject: [PATCH 8/8] GraphQL-425: [Place order] Cart grand total --- .../Resolver/{Totals.php => CartPrices.php} | 7 +- .../Magento/QuoteGraphQl/etc/schema.graphqls | 2 +- .../GraphQl/Quote/Customer/CartTotalsTest.php | 112 ++++++++++-------- .../GraphQl/Quote/Guest/CartTotalsTest.php | 69 ++++++----- 4 files changed, 102 insertions(+), 88 deletions(-) rename app/code/Magento/QuoteGraphQl/Model/Resolver/{Totals.php => CartPrices.php} (94%) diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php similarity index 94% rename from app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php rename to app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php index 926f2b2de3cf..7a9bdd926764 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/Totals.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php @@ -11,13 +11,14 @@ 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\Quote\Model\Quote\Address\Total; use Magento\Quote\Model\Quote\TotalsCollector; /** * @inheritdoc */ -class Totals implements ResolverInterface +class CartPrices implements ResolverInterface { /** * @var TotalsCollector @@ -68,10 +69,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value */ private function getAppliedTaxes(Total $total, string $currency): array { + $appliedTaxesData = []; $appliedTaxes = $total->getAppliedTaxes(); if (count($appliedTaxes) === 0) { - return []; + return $appliedTaxesData; } foreach ($appliedTaxes as $appliedTax) { @@ -80,7 +82,6 @@ private function getAppliedTaxes(Total $total, string $currency): array 'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency] ]; } - return $appliedTaxesData; } } diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 85a0703a137e..70690b102306 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -173,7 +173,7 @@ type Cart { billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress") available_payment_methods: [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods") selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod") - prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Totals") + prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPrices") } type CartAddress { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php index 4a25e9331d86..bb8acfce629f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -7,10 +7,8 @@ namespace Magento\GraphQl\Quote\Customer; +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; use Magento\Integration\Api\CustomerTokenServiceInterface; -use Magento\Quote\Model\QuoteFactory; -use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; -use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; @@ -25,26 +23,14 @@ class CartTotalsTest extends GraphQlAbstract private $customerTokenService; /** - * @var QuoteResource + * @var GetMaskedQuoteIdByReservedOrderId */ - private $quoteResource; - - /** - * @var QuoteFactory - */ - private $quoteFactory; - - /** - * @var QuoteIdToMaskedQuoteIdInterface - */ - private $quoteIdToMaskedId; + private $getMaskedQuoteIdByReservedOrderId; 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); } @@ -60,9 +46,9 @@ protected function setUp() */ public function testGetCartTotalsWithTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); - $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); - $response = $this->sendRequestWithToken($query); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('prices', $response['cart']); $pricesResponse = $response['cart']['prices']; @@ -88,9 +74,9 @@ public function testGetCartTotalsWithTaxApplied() */ public function testGetTotalsWithNoTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); - $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); - $response = $this->sendRequestWithToken($query); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); $pricesResponse = $response['cart']['prices']; self::assertEquals(20, $pricesResponse['grand_total']['value']); @@ -111,9 +97,9 @@ public function testGetTotalsWithNoTaxApplied() */ public function testGetCartTotalsWithNoAddressSet() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); - $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); - $response = $this->sendRequestWithToken($query); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); $pricesResponse = $response['cart']['prices']; self::assertEquals(20, $pricesResponse['grand_total']['value']); @@ -123,13 +109,57 @@ public function testGetCartTotalsWithNoAddressSet() self::assertEmpty($pricesResponse['applied_taxes']); } + /** + * _security + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php + */ + public function testGetTotalsFromGuestCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * _security + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_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/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php + */ + public function testGetTotalsFromAnotherCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com')); + } + /** * Generates GraphQl query for retrieving cart totals * * @param string $maskedQuoteId * @return string */ - private function getCartTotalsGraphqlQuery(string $maskedQuoteId): string + private function getQuery(string $maskedQuoteId): string { return <<quoteFactory->create(); - $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } - - /** - * Sends a GraphQL request with using a bearer token - * - * @param string $query + * @param string $username + * @param string $password * @return array - * @throws \Magento\Framework\Exception\AuthenticationException */ - private function sendRequestWithToken(string $query): array + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array { - - $customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; - - return $this->graphQlQuery($query, [], '', $headerMap); + return $headerMap; } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php index 9fec6fdfd269..ee2d6a2b31de 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php @@ -7,9 +7,7 @@ namespace Magento\GraphQl\Quote\Guest; -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\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; @@ -19,26 +17,17 @@ class CartTotalsTest extends GraphQlAbstract { /** - * @var QuoteResource + * @var GetMaskedQuoteIdByReservedOrderId */ - private $quoteResource; + private $getMaskedQuoteIdByReservedOrderId; /** - * @var QuoteFactory + * @inheritdoc */ - private $quoteFactory; - - /** - * @var QuoteIdToMaskedQuoteIdInterface - */ - private $quoteIdToMaskedId; - 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); } /** @@ -52,8 +41,8 @@ protected function setUp() */ public function testGetCartTotalsWithTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); - $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query); self::assertArrayHasKey('prices', $response['cart']); @@ -79,8 +68,8 @@ public function testGetCartTotalsWithTaxApplied() */ public function testGetTotalsWithNoTaxApplied() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); - $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query); $pricesResponse = $response['cart']['prices']; @@ -102,8 +91,8 @@ public function testGetTotalsWithNoTaxApplied() */ public function testGetCartTotalsWithNoAddressSet() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_quote'); - $query = $this->getCartTotalsGraphqlQuery($maskedQuoteId); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query); $pricesResponse = $response['cart']['prices']; @@ -114,13 +103,35 @@ public function testGetCartTotalsWithNoAddressSet() self::assertEmpty($pricesResponse['applied_taxes']); } + /** + * _security + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_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/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php + */ + public function testGetSelectedShippingMethodFromCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query); + } + /** * Generates GraphQl query for retrieving cart totals * * @param string $maskedQuoteId * @return string */ - private function getCartTotalsGraphqlQuery(string $maskedQuoteId): string + private function getQuery(string $maskedQuoteId): string { return <<quoteFactory->create(); - $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } }