-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-5181: [Checkout coverage] Added discount information to shoppi…
…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
Showing
10 changed files
with
339 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartDiscountTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartDiscountTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...tests/integration/testsuite/Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); |
21 changes: 21 additions & 0 deletions
21
...egration/testsuite/Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon_rollback.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.