|
| 1 | +<?php |
| 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\Customer; |
| 9 | + |
| 10 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test for get specified shipping address |
| 17 | + */ |
| 18 | +class GetSpecifiedShippingAddressTest extends GraphQlAbstract |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var CustomerTokenServiceInterface |
| 22 | + */ |
| 23 | + private $customerTokenService; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 27 | + */ |
| 28 | + private $getMaskedQuoteIdByReservedOrderId; |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritdoc |
| 32 | + */ |
| 33 | + protected function setUp() |
| 34 | + { |
| 35 | + $objectManager = Bootstrap::getObjectManager(); |
| 36 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 37 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 42 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 43 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 44 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 45 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 46 | + */ |
| 47 | + public function testGetSpecifiedShippingAddress() |
| 48 | + { |
| 49 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 50 | + $query = $this->getQuery($maskedQuoteId); |
| 51 | + |
| 52 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 53 | + self::assertArrayHasKey('cart', $response); |
| 54 | + self::assertArrayHasKey('shipping_addresses', $response['cart']); |
| 55 | + |
| 56 | + $expectedShippingAddressData = [ |
| 57 | + 'firstname' => 'John', |
| 58 | + 'lastname' => 'Smith', |
| 59 | + 'company' => 'CompanyName', |
| 60 | + 'street' => [ |
| 61 | + 'Green str, 67' |
| 62 | + ], |
| 63 | + 'city' => 'CityM', |
| 64 | + 'region' => [ |
| 65 | + 'code' => 'AL', |
| 66 | + 'label' => 'Alabama', |
| 67 | + ], |
| 68 | + 'postcode' => '75477', |
| 69 | + 'country' => [ |
| 70 | + 'code' => 'US', |
| 71 | + 'label' => 'US', |
| 72 | + ], |
| 73 | + 'telephone' => '3468676', |
| 74 | + '__typename' => 'ShippingCartAddress', |
| 75 | + 'customer_notes' => null, |
| 76 | + ]; |
| 77 | + self::assertEquals($expectedShippingAddressData, current($response['cart']['shipping_addresses'])); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 82 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 83 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 84 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 85 | + */ |
| 86 | + public function testGetSpecifiedShippingAddressIfShippingAddressIsNotSet() |
| 87 | + { |
| 88 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 89 | + $query = $this->getQuery($maskedQuoteId); |
| 90 | + |
| 91 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 92 | + self::assertArrayHasKey('cart', $response); |
| 93 | + self::assertArrayHasKey('shipping_addresses', $response['cart']); |
| 94 | + |
| 95 | + $expectedShippingAddressData = [ |
| 96 | + 'firstname' => null, |
| 97 | + 'lastname' => null, |
| 98 | + 'company' => null, |
| 99 | + 'street' => [ |
| 100 | + '' |
| 101 | + ], |
| 102 | + 'city' => null, |
| 103 | + 'region' => [ |
| 104 | + 'code' => null, |
| 105 | + 'label' => null, |
| 106 | + ], |
| 107 | + 'postcode' => null, |
| 108 | + 'country' => [ |
| 109 | + 'code' => null, |
| 110 | + 'label' => null, |
| 111 | + ], |
| 112 | + 'telephone' => null, |
| 113 | + '__typename' => 'ShippingCartAddress', |
| 114 | + 'customer_notes' => null, |
| 115 | + ]; |
| 116 | + self::assertEquals($expectedShippingAddressData, current($response['cart']['shipping_addresses'])); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 121 | + * @expectedException \Exception |
| 122 | + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" |
| 123 | + */ |
| 124 | + public function testGetSpecifiedShippingAddressOfNonExistentCart() |
| 125 | + { |
| 126 | + $maskedQuoteId = 'non_existent_masked_id'; |
| 127 | + $query = $this->getQuery($maskedQuoteId); |
| 128 | + |
| 129 | + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * _security |
| 134 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 135 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 136 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 137 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 138 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 139 | + */ |
| 140 | + public function testGetSpecifiedShippingAddressFromAnotherGuestCart() |
| 141 | + { |
| 142 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 143 | + |
| 144 | + $this->expectExceptionMessage( |
| 145 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 146 | + ); |
| 147 | + $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * _security |
| 152 | + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php |
| 153 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 154 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 155 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 156 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 157 | + */ |
| 158 | + public function testGetSpecifiedShippingAddressFromAnotherCustomerCart() |
| 159 | + { |
| 160 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 161 | + |
| 162 | + $this->expectExceptionMessage( |
| 163 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 164 | + ); |
| 165 | + $this->graphQlQuery( |
| 166 | + $this->getQuery($maskedQuoteId), |
| 167 | + [], |
| 168 | + '', |
| 169 | + $this-> getHeaderMap( '[email protected]') |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @param string $maskedQuoteId |
| 175 | + * @return string |
| 176 | + */ |
| 177 | + private function getQuery(string $maskedQuoteId): string |
| 178 | + { |
| 179 | + return <<<QUERY |
| 180 | +{ |
| 181 | + cart(cart_id: "$maskedQuoteId") { |
| 182 | + shipping_addresses { |
| 183 | + firstname |
| 184 | + lastname |
| 185 | + company |
| 186 | + street |
| 187 | + city |
| 188 | + region |
| 189 | + { |
| 190 | + code |
| 191 | + label |
| 192 | + } |
| 193 | + postcode |
| 194 | + country |
| 195 | + { |
| 196 | + code |
| 197 | + label |
| 198 | + } |
| 199 | + telephone |
| 200 | + __typename |
| 201 | + customer_notes |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | +QUERY; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * @param string $username |
| 210 | + * @param string $password |
| 211 | + * @return array |
| 212 | + */ |
| 213 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 214 | + { |
| 215 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 216 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 217 | + return $headerMap; |
| 218 | + } |
| 219 | +} |
0 commit comments