|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 12 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture; |
| 13 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 14 | +use Magento\Customer\Test\Fixture\Customer; |
| 15 | +use Magento\Framework\Exception\AuthenticationException; |
| 16 | +use Magento\Framework\Exception\EmailNotConfirmedException; |
| 17 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 18 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 19 | +use Magento\Quote\Test\Fixture\CustomerCart; |
| 20 | +use Magento\Quote\Test\Fixture\QuoteIdMask; |
| 21 | +use Magento\TestFramework\Fixture\DataFixture; |
| 22 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 23 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 24 | +use Magento\TestFramework\Helper\Bootstrap; |
| 25 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 26 | + |
| 27 | +class AddressBookIdTest extends GraphQlAbstract |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var DataFixtureStorage |
| 31 | + */ |
| 32 | + private $fixtures; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var CustomerTokenServiceInterface |
| 36 | + */ |
| 37 | + private $customerTokenService; |
| 38 | + |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + $this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 42 | + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @throws Exception |
| 47 | + */ |
| 48 | + #[ |
| 49 | + DataFixture(Customer::class, ['addresses' => [['postcode' => '12345']]], as: 'customer'), |
| 50 | + DataFixture(ProductFixture::class, as: 'product'), |
| 51 | + DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'), |
| 52 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']), |
| 53 | + DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'), |
| 54 | + ] |
| 55 | + public function testDefaultAddressBookId(): void |
| 56 | + { |
| 57 | + $customer = $this->fixtures->get('customer'); |
| 58 | + $addresses = $customer->getAddresses(); |
| 59 | + $customerAddress = array_shift($addresses); |
| 60 | + |
| 61 | + $this->graphQlMutation( |
| 62 | + $this->getSetShippingAddressOnCartMutation( |
| 63 | + $this->fixtures->get('quoteIdMask')->getMaskedId(), |
| 64 | + (int)$customerAddress->getId() |
| 65 | + ), |
| 66 | + [], |
| 67 | + '', |
| 68 | + $this->getCustomerAuthHeaders($customer->getEmail()) |
| 69 | + ); |
| 70 | + |
| 71 | + $this->assertEquals( |
| 72 | + [ |
| 73 | + "customerCart" => [ |
| 74 | + "shipping_addresses" => [ |
| 75 | + ["id" => (int)$customerAddress->getId()] |
| 76 | + ], |
| 77 | + "billing_address" => ["id" => (int)$customerAddress->getId()] |
| 78 | + ] |
| 79 | + ], |
| 80 | + $this->graphQlMutation( |
| 81 | + $this->getCustomerCartQuery(), |
| 82 | + [], |
| 83 | + '', |
| 84 | + $this->getCustomerAuthHeaders($customer->getEmail()) |
| 85 | + ) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @throws Exception |
| 91 | + */ |
| 92 | + #[ |
| 93 | + DataFixture(Customer::class, ['addresses' => [['postcode' => '12345']]], as: 'customer'), |
| 94 | + DataFixture(ProductFixture::class, as: 'product'), |
| 95 | + DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'), |
| 96 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']), |
| 97 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 98 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 99 | + ] |
| 100 | + public function testAddressBookIdForNewQuoteAddress(): void |
| 101 | + { |
| 102 | + $this->assertEquals( |
| 103 | + [ |
| 104 | + "customerCart" => [ |
| 105 | + "shipping_addresses" => [ |
| 106 | + ["id" => ""] |
| 107 | + ], |
| 108 | + "billing_address" => ["id" => ""] |
| 109 | + ] |
| 110 | + ], |
| 111 | + $this->graphQlMutation( |
| 112 | + $this->getCustomerCartQuery(), |
| 113 | + [], |
| 114 | + '', |
| 115 | + $this->getCustomerAuthHeaders($this->fixtures->get('customer')->getEmail()) |
| 116 | + ) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get setShippingAddressOnCart mutation |
| 122 | + * |
| 123 | + * @param string $cartId |
| 124 | + * @param int $customerAddressId |
| 125 | + * @return string |
| 126 | + */ |
| 127 | + private function getSetShippingAddressOnCartMutation(string $cartId, int $customerAddressId): string |
| 128 | + { |
| 129 | + return <<<MUTATION |
| 130 | + mutation { |
| 131 | + setShippingAddressesOnCart( |
| 132 | + input: { |
| 133 | + cart_id: "{$cartId}" |
| 134 | + shipping_addresses: { |
| 135 | + customer_address_id: "$customerAddressId" |
| 136 | + } |
| 137 | + } |
| 138 | + ) { |
| 139 | + cart { |
| 140 | + billing_address { |
| 141 | + __typename |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + MUTATION; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Get Customer Cart Query |
| 151 | + * |
| 152 | + * @return string |
| 153 | + */ |
| 154 | + private function getCustomerCartQuery(): string |
| 155 | + { |
| 156 | + return <<<QUERY |
| 157 | + { |
| 158 | + customerCart { |
| 159 | + shipping_addresses { |
| 160 | + id |
| 161 | + } |
| 162 | + billing_address { |
| 163 | + id |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + QUERY; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Generates token for GQL and returns header with generated token |
| 172 | + * |
| 173 | + * @param string $customerEmail |
| 174 | + * @return array |
| 175 | + * @throws AuthenticationException |
| 176 | + * @throws EmailNotConfirmedException |
| 177 | + */ |
| 178 | + private function getCustomerAuthHeaders(string $customerEmail): array |
| 179 | + { |
| 180 | + return [ |
| 181 | + 'Authorization' => 'Bearer ' . $this->customerTokenService->createCustomerAccessToken( |
| 182 | + $customerEmail, |
| 183 | + 'password' |
| 184 | + ) |
| 185 | + ]; |
| 186 | + } |
| 187 | +} |
0 commit comments