|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2025 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\GraphQl\Sales; |
| 10 | + |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 12 | +use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture; |
| 13 | +use Magento\Checkout\Test\Fixture\SetBillingAddress; |
| 14 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture; |
| 15 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
| 16 | +use Magento\Checkout\Test\Fixture\SetShippingAddress; |
| 17 | +use Magento\Customer\Test\Fixture\Customer; |
| 18 | +use Magento\Framework\Locale\ResolverInterface as LocaleResolver; |
| 19 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 20 | +use Magento\Quote\Test\Fixture\AddProductToCart; |
| 21 | +use Magento\Quote\Test\Fixture\CustomerCart; |
| 22 | +use Magento\Store\Test\Fixture\Store; |
| 23 | +use Magento\TestFramework\Helper\Bootstrap; |
| 24 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 25 | +use Magento\TestFramework\Fixture\DataFixture; |
| 26 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 27 | + |
| 28 | +class OrderDateFormattingTest extends GraphQlAbstract |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var CustomerTokenServiceInterface |
| 32 | + */ |
| 33 | + private $customerTokenService; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var LocaleResolver |
| 37 | + */ |
| 38 | + private $localeResolver; |
| 39 | + |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + $objectManager = Bootstrap::getObjectManager(); |
| 43 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 44 | + $this->localeResolver = $objectManager->get(LocaleResolver::class); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test order_date with null/empty created_at |
| 49 | + */ |
| 50 | + #[ |
| 51 | + DataFixture(Store::class), |
| 52 | + DataFixture(ProductFixture::class, as: 'product1'), |
| 53 | + DataFixture(Customer::class, as: 'customer'), |
| 54 | + DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'quote'), |
| 55 | + DataFixture(AddProductToCart::class, ['cart_id' => '$quote.id$', 'product_id' => '$product1.id$', 'qty' => 2]), |
| 56 | + DataFixture(SetBillingAddress::class, ['cart_id' => '$quote.id$']), |
| 57 | + DataFixture(SetShippingAddress::class, ['cart_id' => '$quote.id$']), |
| 58 | + DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 59 | + DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 60 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order'), |
| 61 | + ] |
| 62 | + public function testOrderDateWithInvalidCreatedAt() |
| 63 | + { |
| 64 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 65 | + $customerEmail = $customer->getEmail(); |
| 66 | + $password = 'password'; |
| 67 | + $customerToken = $this->customerTokenService |
| 68 | + ->createCustomerAccessToken($customerEmail, $password); |
| 69 | + |
| 70 | + $query = <<<QUERY |
| 71 | +{ |
| 72 | + customer { |
| 73 | + orders { |
| 74 | + items { |
| 75 | + order_date |
| 76 | + created_at |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +QUERY; |
| 82 | + |
| 83 | + $response = $this->graphQlQuery($query, [], '', ['Authorization' => 'Bearer ' . $customerToken]); |
| 84 | + |
| 85 | + $this->assertArrayHasKey('customer', $response); |
| 86 | + $this->assertArrayHasKey('orders', $response['customer']); |
| 87 | + $expectedFormat = 'd/m/Y H:i:s'; |
| 88 | + $dateString = $response['customer']['orders']['items'][0]['order_date']; |
| 89 | + $date = \DateTime::createFromFormat($expectedFormat, $dateString); |
| 90 | + $isValid = $date && $date->format($expectedFormat) === $dateString; |
| 91 | + $this->assertTrue($isValid, "Date format is not valid: $dateString"); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Test order_date formatting performance with multiple locales |
| 97 | + */ |
| 98 | + #[ |
| 99 | + DataFixture(Store::class), |
| 100 | + DataFixture(ProductFixture::class, as: 'product1'), |
| 101 | + DataFixture(Customer::class, as: 'customer'), |
| 102 | + DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'quote'), |
| 103 | + DataFixture(AddProductToCart::class, ['cart_id' => '$quote.id$', 'product_id' => '$product1.id$', 'qty' => 2]), |
| 104 | + DataFixture(SetBillingAddress::class, ['cart_id' => '$quote.id$']), |
| 105 | + DataFixture(SetShippingAddress::class, ['cart_id' => '$quote.id$']), |
| 106 | + DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 107 | + DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 108 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order'), |
| 109 | + ] |
| 110 | + public function testOrderDateFormattingPerformance() |
| 111 | + { |
| 112 | + $locales = ['en_US', 'fr_FR', 'de_DE', 'es_ES', 'it_IT']; |
| 113 | + |
| 114 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 115 | + $customerEmail = $customer->getEmail(); |
| 116 | + $password = 'password'; |
| 117 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($customerEmail, $password); |
| 118 | + |
| 119 | + $query = <<<QUERY |
| 120 | +{ |
| 121 | + customer { |
| 122 | + orders { |
| 123 | + items { |
| 124 | + order_date |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +QUERY; |
| 130 | + |
| 131 | + foreach ($locales as $locale) { |
| 132 | + $this->localeResolver->setLocale($locale); |
| 133 | + $response = $this->graphQlQuery($query, [], '', ['Authorization' => 'Bearer ' . $customerToken]); |
| 134 | + $this->assertArrayHasKey('customer', $response); |
| 135 | + $this->assertArrayHasKey('orders', $response['customer']); |
| 136 | + $expectedFormat = 'd/m/Y H:i:s'; |
| 137 | + $dateString = $response['customer']['orders']['items'][0]['order_date']; |
| 138 | + $date = \DateTime::createFromFormat($expectedFormat, $dateString); |
| 139 | + $isValid = $date && $date->format($expectedFormat) === $dateString; |
| 140 | + $this->assertTrue($isValid, "Date format is not valid: $dateString"); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments