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

[Test Coverage] 'RemoveCouponFromCart' functionality #497

Merged
merged 12 commits into from
Apr 5, 2019
Merged
19 changes: 17 additions & 2 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/AppliedCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,27 @@
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CouponManagementInterface;

/**
* @inheritdoc
*/
class AppliedCoupon implements ResolverInterface
{
/**
* @var CouponManagementInterface
*/
private $couponManagement;

/**
* @param CouponManagementInterface $couponManagement
*/
public function __construct(
CouponManagementInterface $couponManagement
) {
$this->couponManagement = $couponManagement;
}

/**
* @inheritdoc
*/
Expand All @@ -26,9 +41,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
throw new LocalizedException(__('"model" value should be specified'));
}
$cart = $value['model'];
$cartId = $cart->getId();

$appliedCoupon = $cart->getCouponCode();

$appliedCoupon = $this->couponManagement->get($cartId);
return $appliedCoupon ? ['code' => $appliedCoupon] : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
try {
$this->couponManagement->remove($cartId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
$message = $e->getMessage();
if (preg_match('/The "\d+" Cart doesn\'t contain products/', $message)) {
$message = 'Cart does not contain products';
}
throw new GraphQlNoSuchEntityException(__($message), $e);
} catch (CouldNotDeleteException $e) {
throw new LocalizedException(__($e->getMessage()), $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,63 +126,6 @@ public function testGuestCustomerAttemptToChangeCustomerCart()
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testRemoveCoupon()
{
$couponCode = '2?ds5!2d';

/* Apply coupon to the quote */
$this->quoteResource->load(
$this->quote,
'test_order_with_simple_product_without_address',
'reserved_order_id'
);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
$this->quoteResource->load(
$this->quote,
'test_order_with_simple_product_without_address',
'reserved_order_id'
);
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
$this->graphQlQuery($query);

/* Remove coupon from quote */
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('removeCouponFromCart', $response);
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testRemoveCouponFromCustomerCartByGuest()
{
$this->quoteResource->load(
$this->quote,
'test_order_with_simple_product_without_address',
'reserved_order_id'
);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
$this->quoteResource->load(
$this->quote,
'test_order_with_simple_product_without_address',
'reserved_order_id'
);
$this->quote->setCustomerId(1);
$this->quoteResource->save($this->quote);
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);

self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlQuery($query);
}

/**
* @param string $maskedQuoteId
* @param string $couponCode
Expand All @@ -200,26 +143,6 @@ private function prepareAddCouponRequestQuery(string $maskedQuoteId, string $cou
}
}
}
QUERY;
}

/**
* @param string $maskedQuoteId
* @return string
*/
private function prepareRemoveCouponRequestQuery(string $maskedQuoteId): string
{
return <<<QUERY
mutation {
removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) {
cart {
applied_coupon {
code
}
}
}
}

QUERY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?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;

/**
* Check removing of the coupon from customer cart
*/
class RemoveCouponFromCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

/**
* @inheritdoc
*/
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/Catalog/_files/product_simple.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 testRemoveCouponFromCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('removeCouponFromCart', $response);
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
*/
public function testRemoveCouponFromNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);

$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @expectedException \Exception
* @expectedExceptionMessage Cart does not contain products
*/
public function testRemoveCouponFromEmptyCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveCouponFromCartIfCouponWasNotSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('removeCouponFromCart', $response);
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 testRemoveCouponFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 testRemoveCouponFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}

/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
mutation {
removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) {
cart {
applied_coupon {
code
}
}
}
}

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