Skip to content

Commit a55a333

Browse files
authored
LYNX-450: Include field "is_virtual" in "CustomerOrder" GQL type (#256)
* LYNX-450: Include field "is_virtual" in "CustomerOrder" GQL type Added 'is_virtual' field to 'CustomerOrder' GQL type * LYNX-450: Include field "is_virtual" in "CustomerOrder" GQL type Added new resolver for is_virtual field * LYNX-450: Include field "is_virtual" in "CustomerOrder" GQL type Updated resolver $value['model'] condition
1 parent b42125f commit a55a333

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\SalesGraphQl\Model\Resolver;
18+
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
21+
use Magento\Framework\GraphQl\Config\Element\Field;
22+
use Magento\Framework\GraphQl\Query\ResolverInterface;
23+
use Magento\Sales\Model\Order;
24+
25+
/**
26+
* Resolver for the is_virtual in CustomerOrder
27+
*/
28+
class OrderIsVirtual implements ResolverInterface
29+
{
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): bool
34+
{
35+
if (!isset($value['model']) || !($value['model'] instanceof Order)) {
36+
throw new LocalizedException(__('"model" value should be specified'));
37+
}
38+
/** @var Order $order */
39+
$order = $value['model'];
40+
41+
return (bool) $order->getIsVirtual();
42+
}
43+
}

app/code/Magento/SalesGraphQl/etc/schema.graphqls

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type CustomerOrder @doc(description: "Contains details about each of the custome
7878
token: String! @doc(description: "The token that can be used to retrieve the order using order query.") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Token")
7979
applied_coupons: [AppliedCoupon!]! @doc(description: "Coupons applied to the order.")
8080
email: String @doc(description: "Order customer email.")
81+
is_virtual: Boolean! @doc(description: "`TRUE` if the order is virtual") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\OrderIsVirtual")
8182
}
8283

8384
type OrderAddress @doc(description: "Contains detailed information about an order's billing and shipping addresses."){
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)