Skip to content

Commit

Permalink
LYNX-450: Include field "is_virtual" in "CustomerOrder" GQL type (#256)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sumesh-GL authored Jun 13, 2024
1 parent b42125f commit a55a333
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/code/Magento/SalesGraphQl/Model/Resolver/OrderIsVirtual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained from
* Adobe.
*/
declare(strict_types=1);

namespace Magento\SalesGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Sales\Model\Order;

/**
* Resolver for the is_virtual in CustomerOrder
*/
class OrderIsVirtual implements ResolverInterface
{
/**
* @inheritDoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): bool
{
if (!isset($value['model']) || !($value['model'] instanceof Order)) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var Order $order */
$order = $value['model'];

return (bool) $order->getIsVirtual();
}
}
1 change: 1 addition & 0 deletions app/code/Magento/SalesGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type CustomerOrder @doc(description: "Contains details about each of the custome
token: String! @doc(description: "The token that can be used to retrieve the order using order query.") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Token")
applied_coupons: [AppliedCoupon!]! @doc(description: "Coupons applied to the order.")
email: String @doc(description: "Order customer email.")
is_virtual: Boolean! @doc(description: "`TRUE` if the order is virtual") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\OrderIsVirtual")
}

type OrderAddress @doc(description: "Contains detailed information about an order's billing and shipping addresses."){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\GraphQl\Sales;

use Exception;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture;
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture;
use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture;
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture;
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
use Magento\Quote\Test\Fixture\CustomerCart as CustomerCartFixture;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* GraphQl tests for @see \Magento\SalesStorefrontCompatibilityGraphQl\Model\Resolver\OrderIsVirtual
*/
class OrderIsVirtualTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var DataFixtureStorage
*/
private $fixtures;

/**
* @inheridoc
* @throws LocalizedException
*/
protected function setUp(): void
{
parent::setUp();

$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
$this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage();
}

/**
* Test graphql customer orders is not virtual
*
* @return void
* @throws AuthenticationException|LocalizedException
* @throws Exception
*/
#[
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(CustomerFixture::class, as: 'customer'),
DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'quote'),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$quote.id$', 'product_id' => '$product.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order'),
]
public function testCustomerOrderIsNotVirtual(): void
{
$customerEmail = $this->fixtures->get('customer')->getEmail();
$response = $this->graphQlQuery(
$this->getCustomerOrdersQuery(),
[],
'',
$this->getCustomerAuthHeaders($customerEmail)
);
self::assertArrayHasKey('customerOrders', $response);
self::assertArrayHasKey('items', $response['customerOrders']);
self::assertCount(1, $response['customerOrders']['items']);
self::assertFalse($response['customerOrders']['items'][0]['is_virtual']);
}

/**
* Test graphql customer orders is virtual
*
* @return void
* @throws AuthenticationException|LocalizedException
* @throws Exception
*/
#[
DataFixture(ProductFixture::class, ['type_id' => 'virtual'], as: 'product'),
DataFixture(CustomerFixture::class, as: 'customer'),
DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'quote'),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$quote.id$', 'product_id' => '$product.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order'),
]
public function testCustomerOrderIsVirtual(): void
{
$customerEmail = $this->fixtures->get('customer')->getEmail();
$response = $this->graphQlQuery(
$this->getCustomerOrdersQuery(),
[],
'',
$this->getCustomerAuthHeaders($customerEmail)
);
self::assertArrayHasKey('customerOrders', $response);
self::assertArrayHasKey('items', $response['customerOrders']);
self::assertCount(1, $response['customerOrders']['items']);
self::assertTrue($response['customerOrders']['items'][0]['is_virtual']);
}

/**
* Generate graphql query body for customer orders with 'is_virtual' field
*
* @return string
*/
private function getCustomerOrdersQuery(): string
{
return <<<QUERY
query {
customerOrders {
items {
is_virtual
}
}
}
QUERY;
}

/**
* Returns the header with customer token for GQL Mutation
*
* @param string $email
* @return array
* @throws AuthenticationException
*/
private function getCustomerAuthHeaders(string $email): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, 'password');
return ['Authorization' => 'Bearer ' . $customerToken];
}
}

0 comments on commit a55a333

Please sign in to comment.