Skip to content

Commit

Permalink
ENGCOM-5181: [Checkout coverage] Added discount information to shoppi…
Browse files Browse the repository at this point in the history
…ng cart #717

 - Merge Pull Request magento/graphql-ce#717 from magento/graphql-ce:683-add-discount-information-cart
 - Merged commits:
   1. 31b34ad
   2. 8515566
   3. fe708a2
   4. 382d41f
   5. a1e126c
   6. 2d8bc6d
  • Loading branch information
magento-engcom-team committed Jun 12, 2019
2 parents fdfaed6 + 2d8bc6d commit 936f07c
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 5 deletions.
19 changes: 19 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency
],
'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency),
'discount' => $this->getDiscount($cartTotals, $currency),
'model' => $quote
];
}
Expand Down Expand Up @@ -84,4 +85,22 @@ private function getAppliedTaxes(Total $total, string $currency): array
}
return $appliedTaxesData;
}

/**
* Returns information about an applied discount
*
* @param Total $total
* @param string $currency
* @return array|null
*/
private function getDiscount(Total $total, string $currency)
{
if ($total->getDiscountAmount() === 0) {
return null;
}
return [
'label' => explode(', ', $total->getDiscountDescription()),
'amount' => ['value' => $total->getDiscountAmount(), 'currency' => $currency]
];
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type CartPrices {
grand_total: Money
subtotal_including_tax: Money
subtotal_excluding_tax: Money
discount: CartDiscount
subtotal_with_discount_excluding_tax: Money
applied_taxes: [CartTaxItem]
}
Expand All @@ -155,6 +156,11 @@ type CartTaxItem {
label: String!
}

type CartDiscount {
amount: Money!
label: [String!]!
}

type SetPaymentMethodOnCartOutput {
cart: Cart!
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?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 is getting cart discount for registered customer.
*/
class CartDiscountTest 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/Quote/_files/cart_rule_discount_no_coupon.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 testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-10, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.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/apply_coupon.php
*/
public function testGetDiscountInformationWithTwoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-11, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders', 'Test Coupon for General'], $discountResponse['label']);
}

/**
* @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 testGetDiscountInformationWithNoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertEquals(null, $response['cart']['prices']['discount']);
}

/**
* Generates GraphQl query for retrieving cart discount
*
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
prices {
discount {
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Guest;

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

/**
* Test is getting cart discount for guest customer.
*/
class CartDiscountTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

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

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.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
*/
public function testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-10, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.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/apply_coupon.php
*/
public function testGetDiscountInformationWithTwoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-15, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders', '5$ fixed discount on whole cart'], $discountResponse['label']);
}

/**
* @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
*/
public function testGetDiscountInformationWithNoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);
self::assertEquals(null, $response['cart']['prices']['discount']);
}

/**
* Generates GraphQl query for retrieving cart discount.
*
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
prices {
discount {
label
amount {
value
currency
}
}
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function testGetTotalsWithNoTaxApplied()
* @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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$data = [
'name' => 'Test Coupon for General',
'is_active' => true,
'store_labels' => [0 => 'Test Coupon for General'],
'website_ids' => [
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Store\Model\StoreManagerInterface::class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Customer\Model\GroupManagement as CustomerGroupManagement;
use Magento\Framework\Api\DataObjectHelper;
use Magento\SalesRule\Api\Data\RuleInterface;
use Magento\SalesRule\Api\Data\RuleLabelInterface;
use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\SalesRule\Model\Data\Rule as RuleData;
use Magento\SalesRule\Model\Data\RuleLabelFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var RuleRepositoryInterface $ruleRepository */
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
/** @var DataObjectHelper $dataObjectHelper */
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
$ruleLabel = $objectManager->create(RuleLabelInterface::class);
$ruleLabelFactory = $objectManager->get(RuleLabelFactory::class);


/** @var RuleData $salesRule */
$salesRule = $objectManager->create(RuleData::class);
/** @var RuleLabelInterface $ruleLabel */
$ruleLabel = $ruleLabelFactory->create();
$ruleLabel->setStoreId(0);
$ruleLabel->setStoreLabel('50% Off for all orders');
$ruleData = [
'name' => '50% Off for all orders',
'is_active' => 1,
'customer_group_ids' => [CustomerGroupManagement::NOT_LOGGED_IN_ID, 1],
'coupon_type' => RuleData::COUPON_TYPE_NO_COUPON,
'conditions' => [],
'simple_action' => 'by_percent',
'discount_amount' => 50,
'discount_step' => 0,
'website_ids' => [
$objectManager->get(
StoreManagerInterface::class
)->getWebsite()->getId(),
],
'discount_qty' => 0,
'apply_to_shipping' => 1,
'simple_free_shipping' => 1,
'stop_rules_processing' => 0
];
$dataObjectHelper->populateWithArray($salesRule, $ruleData, RuleInterface::class);
$salesRule->setStoreLabels([$ruleLabel]);

$ruleRepository->save($salesRule);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var RuleRepositoryInterface $ruleRepository */
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
/** @var RuleCollectionFactory $ruleCollectionFactory */
$ruleCollectionFactory = $objectManager->get(RuleCollectionFactory::class);
$ruleCollection = $ruleCollectionFactory->create();

foreach ($ruleCollection->getItems() as $rule) {
$ruleRepository->deleteById($rule->getRuleId());
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
*/
declare(strict_types=1);

use Magento\Framework\Registry;
use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
$registry = Bootstrap::getObjectManager()->get(Registry::class);
/** @var RuleRepositoryInterface $ruleRepository */
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);

/** @var Magento\SalesRule\Model\Rule $rule */
$rule = $registry->registry('cart_rule_100_percent_off');
if ($rule) {
$rule->delete();
$ruleId = $registry->registry('Magento/SalesRule/_files/cart_rule_100_percent_off');

if ($ruleId) {
$ruleRepository->deleteById($ruleId);
}
Loading

0 comments on commit 936f07c

Please sign in to comment.