From 9000000a7cad2bbd8ddb320bfcf643a92e8cd1c9 Mon Sep 17 00:00:00 2001 From: Roman Glushko Date: Sun, 17 Mar 2019 14:34:14 +0200 Subject: [PATCH] #481 Added more test cases covered --- .../Customer/RemoveCouponFromCartTest.php | 178 +++++++++++++++++- 1 file changed, 176 insertions(+), 2 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveCouponFromCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveCouponFromCartTest.php index cba25e0c40e39..818af4c8853ec 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveCouponFromCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveCouponFromCartTest.php @@ -8,10 +8,14 @@ namespace Magento\GraphQl\Quote\Customer; use Magento\Framework\Exception\AuthenticationException; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Integration\Api\CustomerTokenServiceInterface; use Magento\Quote\Model\Quote; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; +use Magento\SalesRule\Api\CouponRepositoryInterface; +use Magento\SalesRule\Model\Coupon; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; @@ -40,6 +44,16 @@ class RemoveCouponFromCartTest extends GraphQlAbstract */ private $customerTokenService; + /** + * @var CouponRepositoryInterface + */ + private $couponRepository; + + /** + * @var Coupon + */ + private $coupon; + protected function setUp() { $objectManager = Bootstrap::getObjectManager(); @@ -47,6 +61,8 @@ protected function setUp() $this->quote = $objectManager->create(Quote::class); $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + $this->couponRepository = $objectManager->get(CouponRepositoryInterface::class); + $this->coupon = $objectManager->create(Coupon::class); } /** @@ -81,16 +97,119 @@ public function testRemoveCouponFromCart() self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php + */ + public function testRemoveCouponFromCartTwice() + { + $couponCode = '2?ds5!2d'; + + /* Apply coupon to the customer 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->quote->setCustomerId(1); + $this->quoteResource->save($this->quote); + + $queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password'); + $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); + $this->graphQlQuery($query, [], '', $queryHeaders); + + /* Remove coupon from the quote */ + $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $queryHeaders); + + self::assertArrayHasKey('removeCouponFromCart', $response); + self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']); + + /* Remove coupon from the quote the second time */ + $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $queryHeaders); + + self::assertArrayHasKey('removeCouponFromCart', $response); + self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php + */ + public function testRemoveCouponFromEmptyCart() + { + /* Assign the empty quote to the customer */ + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $quoteId = (int)$this->quote->getId(); + $maskedQuoteId = $this->quoteIdToMaskedId->execute($quoteId); + + $this->quote->setCustomerId(1); + $this->quoteResource->save($this->quote); + + /* Remove coupon from the empty quote */ + $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId); + $queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password'); + + $this->expectExceptionMessage("The \"$quoteId\" Cart doesn't contain products"); + $this->graphQlQuery($query, [], '', $queryHeaders); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php + */ + public function testRemoveCouponFromCartWithoutItems() + { + $couponCode = '2?ds5!2d'; + + /* Assign the quote to the customer */ + $this->quoteResource->load( + $this->quote, + 'test_order_with_simple_product_without_address', + 'reserved_order_id' + ); + + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $this->quote->setCustomerId(1); + $this->quoteResource->save($this->quote); + + /* Apply coupon to the customer quote */ + $queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password'); + $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); + $this->graphQlQuery($query, [], '', $queryHeaders); + + /* Clear the quote */ + $this->quote->removeAllItems(); + $this->quoteResource->save($this->quote); + + /* Remove coupon from the customer quote */ + $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $queryHeaders); + + self::assertArrayHasKey('removeCouponFromCart', $response); + self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']); + } + /** * @magentoApiDataFixture Magento/Customer/_files/two_customers.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php */ - public function testRemoveCouponFromAonotherCustomerCart() + public function testRemoveCouponFromAnotherCustomerCart() { $couponCode = '2?ds5!2d'; - /* Apply coupon to the first customer quote */ + /* Assign the quote to the customer */ $this->quoteResource->load( $this->quote, 'test_order_with_simple_product_without_address', @@ -101,6 +220,7 @@ public function testRemoveCouponFromAonotherCustomerCart() $this->quote->setCustomerId(1); $this->quoteResource->save($this->quote); + /* Apply coupon to the first customer quote */ $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); $queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password'); $this->graphQlQuery($query, [], '', $queryHeaders); @@ -141,6 +261,60 @@ public function testRemoveCouponFromGuestCart() $this->graphQlQuery($query, [], '', $queryHeaders); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php + */ + public function testRemoveNonExistentCouponFromCart() + { + $couponCode = '2?ds5!2d'; + + /* Assign the quote to the customer */ + $this->quoteResource->load( + $this->quote, + 'test_order_with_simple_product_without_address', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $this->quote->setCustomerId(1); + $this->quoteResource->save($this->quote); + + /* Apply coupon to the customer quote */ + $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); + $queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password'); + $this->graphQlQuery($query, [], '', $queryHeaders); + + /* Remove the coupon */ + $this->removeCoupon($couponCode); + + /* Remove the non-existent coupon from the quote */ + $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId); + + $response = $this->graphQlQuery($query, [], '', $queryHeaders); + + self::assertArrayHasKey('removeCouponFromCart', $response); + self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']); + } + + /** + * Remove the given coupon code from the database + * + * @param string $couponCode + * @throws LocalizedException + * @throws NoSuchEntityException + */ + private function removeCoupon(string $couponCode): void + { + $this->coupon->loadByCode($couponCode); + $couponId = $this->coupon->getCouponId(); + + if ($couponId) { + $this->couponRepository->deleteById($couponId); + } + } + /** * Retrieve customer authorization headers *