|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote\Customer; |
| 9 | + |
| 10 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 11 | +use Magento\Customer\Test\Fixture\Customer; |
| 12 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 13 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 14 | +use Magento\Quote\Test\Fixture\CustomerCart; |
| 15 | +use Magento\Store\Test\Fixture\Store; |
| 16 | +use Magento\TestFramework\Fixture\DataFixture; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 19 | + |
| 20 | +/** |
| 21 | + * Ensures customerCart respects Store header for store-scoped product attributes |
| 22 | + */ |
| 23 | +class CustomerCartStoreScopeTest extends GraphQlAbstract |
| 24 | +{ |
| 25 | + /** @var CustomerTokenServiceInterface */ |
| 26 | + private $customerTokenService; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string Customer token for requests |
| 30 | + */ |
| 31 | + private $customerToken; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->customerTokenService = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() |
| 36 | + ->get(CustomerTokenServiceInterface::class); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Customer cart returns product name overridden per Store header |
| 41 | + */ |
| 42 | + #[ |
| 43 | + DataFixture(Store::class, as: 'store2'), |
| 44 | + DataFixture(Customer::class, as: 'customer'), |
| 45 | + DataFixture(ProductFixture::class, ['name' => 'Def Name'], as: 'product'), |
| 46 | + DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'), |
| 47 | + DataFixture( |
| 48 | + AddProductToCartFixture::class, |
| 49 | + [ |
| 50 | + 'cart_id' => '$cart.id$', |
| 51 | + 'product_id' => '$product.id$', |
| 52 | + 'qty' => 1 |
| 53 | + ] |
| 54 | + ) |
| 55 | + ] |
| 56 | + public function testCustomerCartProductNameRespectsStoreHeader(): void |
| 57 | + { |
| 58 | + //Set product name per store |
| 59 | + $secondStore = DataFixtureStorageManager::getStorage()->get('store2'); |
| 60 | + $productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() |
| 61 | + ->get(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 62 | + $product = DataFixtureStorageManager::getStorage()->get('product'); |
| 63 | + $product->setStoreId($secondStore->getId())->setName('Second Name'); |
| 64 | + $productRepository->save($product); |
| 65 | + //get customer cart for different stores |
| 66 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 67 | + $query = <<<QUERY |
| 68 | +{ |
| 69 | + customerCart { |
| 70 | + itemsV2(pageSize: 10) { |
| 71 | + items { product { sku name } } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +QUERY; |
| 76 | + |
| 77 | + // Default store |
| 78 | + $headersDefault = $this->getHeaderMap($customer->getEmail()); |
| 79 | + $headersDefault['Store'] = 'default'; |
| 80 | + $responseDefault = $this->graphQlQuery($query, [], '', $headersDefault); |
| 81 | + self::assertEquals('Def Name', $responseDefault['customerCart']['itemsV2']['items'][0]['product']['name']); |
| 82 | + |
| 83 | + // Second store |
| 84 | + $headersSecond = $this->getHeaderMap($customer->getEmail()); |
| 85 | + $headersSecond['Store'] = $secondStore->getCode(); |
| 86 | + $responseSecond = $this->graphQlQuery($query, [], '', $headersSecond); |
| 87 | + self::assertEquals('Second Name', $responseSecond['customerCart']['itemsV2']['items'][0]['product']['name']); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Invalid Store header returns a standardized error |
| 92 | + */ |
| 93 | + #[ |
| 94 | + DataFixture(Customer::class, as: 'customer') |
| 95 | + ] |
| 96 | + public function testCustomerCartInvalidStoreHeader(): void |
| 97 | + { |
| 98 | + $this->expectException(\Exception::class); |
| 99 | + $this->expectExceptionMessage('Requested store is not found'); |
| 100 | + |
| 101 | + $query = <<<QUERY |
| 102 | +{ |
| 103 | + customerCart { id } |
| 104 | +} |
| 105 | +QUERY; |
| 106 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 107 | + $headers = $this->getHeaderMap($customer->getEmail()); |
| 108 | + $headers['Store'] = 'not_existing_store'; |
| 109 | + $this->graphQlQuery($query, [], '', $headers); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get headers with authorization token |
| 114 | + * |
| 115 | + * @param string $username |
| 116 | + * @param string $password |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 120 | + { |
| 121 | + $token = $this->getCustomerToken($username, $password); |
| 122 | + return ['Authorization' => 'Bearer ' . $token]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get and cache customer token |
| 127 | + * |
| 128 | + * @param string $username |
| 129 | + * @param string $password |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + private function getCustomerToken( |
| 133 | + string $username, |
| 134 | + string $password |
| 135 | + ) : string { |
| 136 | + if (!$this->customerToken) { |
| 137 | + $this->customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 138 | + } |
| 139 | + return $this->customerToken; |
| 140 | + } |
| 141 | +} |
0 commit comments