|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe. |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Sales; |
| 9 | + |
| 10 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 11 | +use Magento\ConfigurableProduct\Test\Fixture\AddProductToCart as AddConfigurableProductToCartFixture; |
| 12 | +use Magento\Quote\Test\Fixture\QuoteIdMask; |
| 13 | +use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture; |
| 14 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture; |
| 15 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture; |
| 16 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
| 17 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 18 | +use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture; |
| 19 | +use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture; |
| 20 | +use Magento\Customer\Test\Fixture\Customer; |
| 21 | +use Magento\Framework\Exception\AuthenticationException; |
| 22 | +use Magento\GraphQl\GetCustomerAuthenticationHeader; |
| 23 | +use Magento\Quote\Test\Fixture\CustomerCart; |
| 24 | +use Magento\TestFramework\Fixture\DataFixture; |
| 25 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 26 | +use Magento\TestFramework\Helper\Bootstrap; |
| 27 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test for orders with configurable product |
| 31 | + */ |
| 32 | +class RetrieveOrdersWithConfigurableProductByOrderNumberTest extends GraphQlAbstract |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var GetCustomerAuthenticationHeader |
| 36 | + */ |
| 37 | + private $customerAuthenticationHeader; |
| 38 | + |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + parent::setUp(); |
| 42 | + $objectManager = Bootstrap::getObjectManager(); |
| 43 | + $this->customerAuthenticationHeader = $objectManager->get(GetCustomerAuthenticationHeader::class); |
| 44 | + } |
| 45 | + |
| 46 | + #[ |
| 47 | + DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'), |
| 48 | + DataFixture(ProductFixture::class, as: 'product'), |
| 49 | + DataFixture(AttributeFixture::class, as: 'attribute'), |
| 50 | + DataFixture( |
| 51 | + ConfigurableProductFixture::class, |
| 52 | + ['_options' => ['$attribute$'], '_links' => ['$product$']], |
| 53 | + 'configurable_product' |
| 54 | + ), |
| 55 | + DataFixture( |
| 56 | + CustomerCart::class, |
| 57 | + [ |
| 58 | + 'customer_id' => '$customer.id$' |
| 59 | + ], |
| 60 | + 'quote' |
| 61 | + ), |
| 62 | + DataFixture(QuoteIdMask::class, ['cart_id' => '$quote.id$'], 'quoteIdMask'), |
| 63 | + DataFixture( |
| 64 | + AddConfigurableProductToCartFixture::class, |
| 65 | + [ |
| 66 | + 'cart_id' => '$quote.id$', |
| 67 | + 'product_id' => '$configurable_product.id$', |
| 68 | + 'child_product_id' => '$product.id$', |
| 69 | + 'qty' => 1 |
| 70 | + ], |
| 71 | + ), |
| 72 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$quote.id$']), |
| 73 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$quote.id$']), |
| 74 | + DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 75 | + DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 76 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order'), |
| 77 | + ] |
| 78 | + public function testGetCustomerOrderConfigurableProduct(): void |
| 79 | + { |
| 80 | + $order = DataFixtureStorageManager::getStorage()->get('order'); |
| 81 | + $orderNumber = $order->getIncrementId(); |
| 82 | + $product = DataFixtureStorageManager::getStorage()->get('product'); |
| 83 | + $configurableProduct = DataFixtureStorageManager::getStorage()->get('configurable_product'); |
| 84 | + $customerOrderResponse = $this->getCustomerOrderQueryConfigurableProduct($orderNumber); |
| 85 | + $customerOrderItems = $customerOrderResponse[0]; |
| 86 | + $configurableItemInTheOrder = $customerOrderItems['items'][0]; |
| 87 | + $this->assertEquals( |
| 88 | + $product->getSku(), |
| 89 | + $configurableItemInTheOrder['product_sku'] |
| 90 | + ); |
| 91 | + |
| 92 | + $expectedConfigurableOptions = [ |
| 93 | + '__typename' => 'ConfigurableOrderItem', |
| 94 | + 'product_sku' => $product->getSku(), |
| 95 | + 'product_name' => $configurableProduct->getName(), |
| 96 | + 'parent_sku' => $configurableProduct->getSku(), |
| 97 | + 'product_url_key' => $configurableProduct->getUrlKey(), |
| 98 | + 'quantity_ordered' => 1 |
| 99 | + ]; |
| 100 | + $this->assertEquals($expectedConfigurableOptions, $configurableItemInTheOrder); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get customer order query for configurable order items |
| 105 | + * |
| 106 | + * @param $orderNumber |
| 107 | + * @return array |
| 108 | + * @throws AuthenticationException |
| 109 | + */ |
| 110 | + private function getCustomerOrderQueryConfigurableProduct($orderNumber): array |
| 111 | + { |
| 112 | + $query = |
| 113 | + <<<QUERY |
| 114 | +{ |
| 115 | + customer { |
| 116 | + orders(filter:{number:{eq:"{$orderNumber}"}}) { |
| 117 | + total_count |
| 118 | + items { |
| 119 | + id |
| 120 | + number |
| 121 | + order_date |
| 122 | + status |
| 123 | + items { |
| 124 | + __typename |
| 125 | + product_sku |
| 126 | + product_name |
| 127 | + product_url_key |
| 128 | + quantity_ordered |
| 129 | + ... on ConfigurableOrderItem { |
| 130 | + parent_sku |
| 131 | + } |
| 132 | + } |
| 133 | + total { |
| 134 | + base_grand_total{value currency} |
| 135 | + grand_total{value currency} |
| 136 | + subtotal {value currency } |
| 137 | + total_tax{value currency} |
| 138 | + taxes {amount{value currency} title rate} |
| 139 | + total_shipping{value currency} |
| 140 | + shipping_handling |
| 141 | + { |
| 142 | + amount_including_tax{value} |
| 143 | + amount_excluding_tax{value} |
| 144 | + total_amount{value} |
| 145 | + discounts{amount{value}} |
| 146 | + taxes {amount{value} title rate} |
| 147 | + } |
| 148 | + discounts {amount{value currency} label} |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +QUERY; |
| 155 | + $currentEmail = 'customer@example.com'; |
| 156 | + $currentPassword = 'password'; |
| 157 | + $response = $this->graphQlQuery( |
| 158 | + $query, |
| 159 | + [], |
| 160 | + '', |
| 161 | + $this->customerAuthenticationHeader->execute($currentEmail, $currentPassword) |
| 162 | + ); |
| 163 | + |
| 164 | + $this->assertArrayHasKey('orders', $response['customer']); |
| 165 | + $this->assertArrayHasKey('items', $response['customer']['orders']); |
| 166 | + $customerOrderItemsInResponse = $response['customer']['orders']['items']; |
| 167 | + return $customerOrderItemsInResponse; |
| 168 | + } |
| 169 | +} |
0 commit comments