|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\GraphQl\Sales; |
| 20 | + |
| 21 | +use Exception; |
| 22 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 23 | +use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture; |
| 24 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture; |
| 25 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture; |
| 26 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
| 27 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 28 | +use Magento\Customer\Test\Fixture\Customer as CustomerFixture; |
| 29 | +use Magento\Framework\Exception\AuthenticationException; |
| 30 | +use Magento\Framework\Exception\LocalizedException; |
| 31 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 32 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 33 | +use Magento\Quote\Test\Fixture\CustomerCart as CustomerCartFixture; |
| 34 | +use Magento\TestFramework\Fixture\DataFixture; |
| 35 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 36 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 37 | +use Magento\TestFramework\Helper\Bootstrap; |
| 38 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 39 | + |
| 40 | +/** |
| 41 | + * GraphQl tests for @see \Magento\SalesStorefrontCompatibilityGraphQl\Model\Resolver\OrderIsVirtual |
| 42 | + */ |
| 43 | +class OrderIsVirtualTest extends GraphQlAbstract |
| 44 | +{ |
| 45 | + /** |
| 46 | + * @var CustomerTokenServiceInterface |
| 47 | + */ |
| 48 | + private $customerTokenService; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var DataFixtureStorage |
| 52 | + */ |
| 53 | + private $fixtures; |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheridoc |
| 57 | + * @throws LocalizedException |
| 58 | + */ |
| 59 | + protected function setUp(): void |
| 60 | + { |
| 61 | + parent::setUp(); |
| 62 | + |
| 63 | + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 64 | + $this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test graphql customer orders is not virtual |
| 69 | + * |
| 70 | + * @return void |
| 71 | + * @throws AuthenticationException|LocalizedException |
| 72 | + * @throws Exception |
| 73 | + */ |
| 74 | + #[ |
| 75 | + DataFixture(ProductFixture::class, as: 'product'), |
| 76 | + DataFixture(CustomerFixture::class, as: 'customer'), |
| 77 | + DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'quote'), |
| 78 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$quote.id$', 'product_id' => '$product.id$']), |
| 79 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$quote.id$']), |
| 80 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$quote.id$']), |
| 81 | + DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 82 | + DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 83 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order'), |
| 84 | + ] |
| 85 | + public function testCustomerOrderIsNotVirtual(): void |
| 86 | + { |
| 87 | + $customerEmail = $this->fixtures->get('customer')->getEmail(); |
| 88 | + $response = $this->graphQlQuery( |
| 89 | + $this->getCustomerOrdersQuery(), |
| 90 | + [], |
| 91 | + '', |
| 92 | + $this->getCustomerAuthHeaders($customerEmail) |
| 93 | + ); |
| 94 | + self::assertArrayHasKey('customerOrders', $response); |
| 95 | + self::assertArrayHasKey('items', $response['customerOrders']); |
| 96 | + self::assertCount(1, $response['customerOrders']['items']); |
| 97 | + self::assertFalse($response['customerOrders']['items'][0]['is_virtual']); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test graphql customer orders is virtual |
| 102 | + * |
| 103 | + * @return void |
| 104 | + * @throws AuthenticationException|LocalizedException |
| 105 | + * @throws Exception |
| 106 | + */ |
| 107 | + #[ |
| 108 | + DataFixture(ProductFixture::class, ['type_id' => 'virtual'], as: 'product'), |
| 109 | + DataFixture(CustomerFixture::class, as: 'customer'), |
| 110 | + DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'quote'), |
| 111 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$quote.id$', 'product_id' => '$product.id$']), |
| 112 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$quote.id$']), |
| 113 | + DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 114 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order'), |
| 115 | + ] |
| 116 | + public function testCustomerOrderIsVirtual(): void |
| 117 | + { |
| 118 | + $customerEmail = $this->fixtures->get('customer')->getEmail(); |
| 119 | + $response = $this->graphQlQuery( |
| 120 | + $this->getCustomerOrdersQuery(), |
| 121 | + [], |
| 122 | + '', |
| 123 | + $this->getCustomerAuthHeaders($customerEmail) |
| 124 | + ); |
| 125 | + self::assertArrayHasKey('customerOrders', $response); |
| 126 | + self::assertArrayHasKey('items', $response['customerOrders']); |
| 127 | + self::assertCount(1, $response['customerOrders']['items']); |
| 128 | + self::assertTrue($response['customerOrders']['items'][0]['is_virtual']); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Generate graphql query body for customer orders with 'is_virtual' field |
| 133 | + * |
| 134 | + * @return string |
| 135 | + */ |
| 136 | + private function getCustomerOrdersQuery(): string |
| 137 | + { |
| 138 | + return <<<QUERY |
| 139 | +query { |
| 140 | + customerOrders { |
| 141 | + items { |
| 142 | + is_virtual |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | +QUERY; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns the header with customer token for GQL Mutation |
| 151 | + * |
| 152 | + * @param string $email |
| 153 | + * @return array |
| 154 | + * @throws AuthenticationException |
| 155 | + */ |
| 156 | + private function getCustomerAuthHeaders(string $email): array |
| 157 | + { |
| 158 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, 'password'); |
| 159 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 160 | + } |
| 161 | +} |
0 commit comments