|
| 1 | +<?php /** @noinspection SpellCheckingInspection */ |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote; |
| 9 | + |
| 10 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 11 | +use Magento\Quote\Model\Quote; |
| 12 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 13 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for getting cart information |
| 19 | + */ |
| 20 | +class GetCartTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CustomerTokenServiceInterface |
| 24 | + */ |
| 25 | + private $customerTokenService; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var QuoteResource |
| 29 | + */ |
| 30 | + private $quoteResource; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Quote |
| 34 | + */ |
| 35 | + private $quote; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 39 | + */ |
| 40 | + private $quoteIdToMaskedId; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + $objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->quoteResource = $objectManager->create(QuoteResource::class); |
| 49 | + $this->quote = $objectManager->create(Quote::class); |
| 50 | + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); |
| 51 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php |
| 56 | + */ |
| 57 | + public function testGetOwnCartForRegisteredCustomer() |
| 58 | + { |
| 59 | + $reservedOrderId = 'test_order_item_with_items'; |
| 60 | + $this->quoteResource->load( |
| 61 | + $this->quote, |
| 62 | + $reservedOrderId, |
| 63 | + 'reserved_order_id' |
| 64 | + ); |
| 65 | + |
| 66 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); |
| 67 | + $query = $this->prepareGetCartQuery($maskedQuoteId); |
| 68 | + |
| 69 | + $response = $this->sendRequestWithToken($query); |
| 70 | + |
| 71 | + self::assertArrayHasKey('Cart', $response); |
| 72 | + self::assertNotEmpty($response['Cart']['items']); |
| 73 | + self::assertNotEmpty($response['Cart']['addresses']); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php |
| 78 | + */ |
| 79 | + public function testGetCartFromAnotherCustomer() |
| 80 | + { |
| 81 | + $reservedOrderId = 'test_order_item_with_items'; |
| 82 | + $this->quoteResource->load( |
| 83 | + $this->quote, |
| 84 | + $reservedOrderId, |
| 85 | + 'reserved_order_id' |
| 86 | + ); |
| 87 | + |
| 88 | + |
| 89 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); |
| 90 | + $query = $this->prepareGetCartQuery($maskedQuoteId); |
| 91 | + |
| 92 | + self::expectExceptionMessage("The current user cannot perform operations on cart \"$maskedQuoteId\""); |
| 93 | + |
| 94 | + $this->graphQlQuery($query); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 99 | + */ |
| 100 | + public function testGetCartForGuest() |
| 101 | + { |
| 102 | + $reservedOrderId = 'test_order_1'; |
| 103 | + $this->quoteResource->load( |
| 104 | + $this->quote, |
| 105 | + $reservedOrderId, |
| 106 | + 'reserved_order_id' |
| 107 | + ); |
| 108 | + |
| 109 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); |
| 110 | + $query = $this->prepareGetCartQuery($maskedQuoteId); |
| 111 | + |
| 112 | + $response = $this->graphQlQuery($query); |
| 113 | + |
| 114 | + self::assertArrayHasKey('Cart', $response); |
| 115 | + } |
| 116 | + |
| 117 | + public function testGetNonExistentCart() |
| 118 | + { |
| 119 | + $maskedQuoteId = 'non_existent_masked_id'; |
| 120 | + $query = $this->prepareGetCartQuery($maskedQuoteId); |
| 121 | + |
| 122 | + self::expectExceptionMessage("Could not find a cart with ID \"$maskedQuoteId\""); |
| 123 | + |
| 124 | + $this->graphQlQuery($query); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Generates query for setting the specified shipping method on cart |
| 129 | + * |
| 130 | + * @param string $maskedQuoteId |
| 131 | + * @return string |
| 132 | + */ |
| 133 | + private function prepareGetCartQuery( |
| 134 | + string $maskedQuoteId |
| 135 | + ) : string { |
| 136 | + return <<<QUERY |
| 137 | +{ |
| 138 | + Cart(cart_id: "$maskedQuoteId") { |
| 139 | + applied_coupon { |
| 140 | + code |
| 141 | + } |
| 142 | + items { |
| 143 | + id |
| 144 | + } |
| 145 | + addresses { |
| 146 | + firstname, |
| 147 | + lastname, |
| 148 | + address_type |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +
|
| 153 | +QUERY; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Sends a GraphQL request with using a bearer token |
| 158 | + * |
| 159 | + * @param string $query |
| 160 | + * @return array |
| 161 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 162 | + */ |
| 163 | + private function sendRequestWithToken(string $query): array |
| 164 | + { |
| 165 | + |
| 166 | + $customerToken = $this-> customerTokenService-> createCustomerAccessToken( '[email protected]', 'password'); |
| 167 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 168 | + |
| 169 | + return $this->graphQlQuery($query, [], '', $headerMap); |
| 170 | + } |
| 171 | +} |
0 commit comments