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

Commit

Permalink
Merge pull request #4053 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Apr 16, 2019
2 parents 3290565 + 9ebee4b commit af6820c
Show file tree
Hide file tree
Showing 15 changed files with 1,092 additions and 41 deletions.
87 changes: 87 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

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

/**
* @inheritdoc
*/
class CartPrices implements ResolverInterface
{
/**
* @var TotalsCollector
*/
private $totalsCollector;

/**
* @param TotalsCollector $totalsCollector
*/
public function __construct(
TotalsCollector $totalsCollector
) {
$this->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'));
}

/** @var Quote $quote */
$quote = $value['model'];
$cartTotals = $this->totalsCollector->collectQuoteTotals($quote);
$currency = $quote->getQuoteCurrencyCode();

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
];
}

/**
* Returns taxes applied to the current quote
*
* @param Total $total
* @param string $currency
* @return array
*/
private function getAppliedTaxes(Total $total, string $currency): array
{
$appliedTaxesData = [];
$appliedTaxes = $total->getAppliedTaxes();

if (count($appliedTaxes) === 0) {
return $appliedTaxesData;
}

foreach ($appliedTaxes as $appliedTax) {
$appliedTaxesData[] = [
'label' => $appliedTax['id'],
'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency]
];
}
return $appliedTaxesData;
}
}
7 changes: 7 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());

if ($context->getUserId() === 0) {
if (!$cart->getCustomerEmail()) {
throw new GraphQlInputException(__("Guest email for cart is missing. Please enter"));
}
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
}

try {
$orderId = $this->cartManagement->placeOrder($cart->getId());
$order = $this->orderRepository->get($orderId);
Expand Down
14 changes: 14 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ input PaymentMethodInput {
purchase_order_number: String @doc(description:"Purchase order number")
}

type CartPrices {
grand_total: Money
subtotal_including_tax: Money
subtotal_excluding_tax: Money
subtotal_with_discount_excluding_tax: Money
applied_taxes: [CartTaxItem]
}

type CartTaxItem {
amount: Money!
label: String!
}

type SetPaymentMethodOnCartOutput {
cart: Cart!
}
Expand Down Expand Up @@ -160,6 +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\\CartPrices")
}

type CartAddress {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Customer;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test getting cart totals for registered customer
*/
class CartTotalsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @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->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('prices', $response['cart']);
$pricesResponse = $response['cart']['prices'];
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-TEST-*-Rate-1', $appliedTaxesResponse[0]['label']);
self::assertEquals(1.5, $appliedTaxesResponse[0]['amount']['value']);
self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']);
}

/**
* @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->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']);
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']);
}

/**
* The totals calculation is based on quote address.
* But the totals should be calculated even if no address is set
*
* @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->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']);
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']);
}

/**
* _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 getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
prices {
grand_total {
value,
currency
}
subtotal_including_tax {
value
currency
}
subtotal_excluding_tax {
value
currency
}
subtotal_with_discount_excluding_tax {
value
currency
}
applied_taxes {
label
amount {
value
currency
}
}
}
}
}
QUERY;
}

/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
Loading

0 comments on commit af6820c

Please sign in to comment.