This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,294 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\Framework\Exception\AuthenticationException;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test adding simple product to Cart
*/
class AddSimpleProductToCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testAddSimpleProductToCart()
{
$sku = 'simple_product';
$quantity = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $quantity);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
self::assertArrayHasKey('shipping_addresses', $response['addSimpleProductsToCart']['cart']);
self::assertEmpty($response['addSimpleProductsToCart']['cart']['shipping_addresses']);
self::assertEquals($quantity, $response['addSimpleProductsToCart']['cart']['items'][0]['quantity']);
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
self::assertArrayHasKey('prices', $response['addSimpleProductsToCart']['cart']['items'][0]);
self::assertArrayHasKey('price', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
$price = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['price'];
self::assertArrayHasKey('value', $price);
self::assertEquals(10, $price['value']);
self::assertArrayHasKey('currency', $price);
self::assertEquals('USD', $price['currency']);
self::assertArrayHasKey('row_total', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
$rowTotal = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total'];
self::assertArrayHasKey('value', $rowTotal);
self::assertEquals(20, $rowTotal['value']);
self::assertArrayHasKey('currency', $rowTotal);
self::assertEquals('USD', $rowTotal['currency']);
self::assertArrayHasKey(
'row_total_including_tax',
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']
);
$rowTotalIncludingTax =
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total_including_tax'];
self::assertArrayHasKey('value', $rowTotalIncludingTax);
self::assertEquals(20, $rowTotalIncludingTax['value']);
self::assertArrayHasKey('currency', $rowTotalIncludingTax);
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testAddSimpleProductToCartIfCartIdIsEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_id" is missing');
$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "",
cart_items: []
}
) {
cart {
items {
id
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testAddSimpleProductToCartIfCartItemsAreEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_items" is missing');
$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "cart_id",
cart_items: []
}
) {
cart {
items {
id
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
*
*/
public function testAddProductToNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$sku = 'simple_product';
$quantity = 2;
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId, $sku, $quantity);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*
*/
public function testNonExistentProductToCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a product with SKU "simple_product"');
$sku = 'simple_product';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
*/
public function testAddSimpleProductToGuestCart()
{
$sku = 'simple_product';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testAddSimpleProductToAnotherCustomerCart()
{
$sku = 'simple_product';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
}
/**
* @param string $maskedQuoteId
* @param string $sku
* @param float $quantity
* @return string
*/
private function getQuery(string $maskedQuoteId, string $sku, float $quantity): string
{
return <<<QUERY
mutation {
addSimpleProductsToCart(input: {
cart_id: "{$maskedQuoteId}",
cart_items: [
{
data: {
quantity: {$quantity}
sku: "{$sku}"
}
}
]
}) {
cart {
items {
id
quantity
product {
sku
}
prices {
price {
value
currency
}
row_total {
value
currency
}
row_total_including_tax {
value
currency
}
}
}
shipping_addresses {
firstname
lastname
company
street
city
postcode
telephone
country {
code
label
}
__typename
}
}
}
}
QUERY;
}
/**
* Retrieve customer authorization headers
*
* @param string $username
* @param string $password
* @return array
* @throws AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,238 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\Framework\Exception\AuthenticationException;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test adding virtual product to Cart
*/
class AddVirtualProductToCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testAddVirtualProductToCart()
{
$sku = 'virtual_product';
$quantity = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $quantity);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response['addVirtualProductsToCart']);
self::assertEquals($quantity, $response['addVirtualProductsToCart']['cart']['items'][0]['quantity']);
self::assertEquals($sku, $response['addVirtualProductsToCart']['cart']['items'][0]['product']['sku']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testAddVirtualProductToCartIfCartIdIsEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_id" is missing');
$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "",
cart_items: []
}
) {
cart {
items {
id
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testAddVirtualProductToCartIfCartItemsAreEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_items" is missing');
$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "cart_id",
cart_items: []
}
) {
cart {
items {
id
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
*
*/
public function testAddVirtualToNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$sku = 'virtual_product';
$qty = 2;
$nonExistentMaskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($nonExistentMaskedQuoteId, $sku, $qty);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*
*/
public function testNonExistentProductToCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a product with SKU "virtual_product"');
$sku = 'virtual_product';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
*/
public function testAddVirtualProductToGuestCart()
{
$sku = 'virtual_product';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testAddVirtualProductToAnotherCustomerCart()
{
$sku = 'virtual_product';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
}
/**
* @param string $maskedQuoteId
* @param string $sku
* @param float $quantity
* @return string
*/
private function getQuery(string $maskedQuoteId, string $sku, float $quantity): string
{
return <<<QUERY
mutation {
addVirtualProductsToCart(input: {
cart_id: "{$maskedQuoteId}",
cart_items: [
{
data: {
quantity: {$quantity}
sku: "{$sku}"
}
}
]
}) {
cart {
items {
id
quantity
product {
sku
}
}
}
}
}
QUERY;
}
/**
* Retrieve customer authorization headers
*
* @param string $username
* @param string $password
* @return array
* @throws AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,227 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Framework\Exception\AuthenticationException;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test Apply Coupon to Cart functionality for customer
*/
class ApplyCouponToCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testApplyCouponToCart()
{
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('applyCouponToCart', $response);
self::assertEquals($couponCode, $response['applyCouponToCart']['cart']['applied_coupon']['code']);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testApplyCouponTwice()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('A coupon is already applied to the cart. Please remove it to apply another');
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey("applyCouponToCart", $response);
self::assertEquals($couponCode, $response['applyCouponToCart']['cart']['applied_coupon']['code']);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testApplyCouponToCartWithoutItems()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cart does not contain products.');
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testApplyCouponToGuestCart()
{
$this->expectException(\Exception::class);
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/Customer/_files/two_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testApplyCouponToAnotherCustomerCart()
{
$this->expectException(\Exception::class);
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer_two@example.com'));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testApplyNonExistentCouponToCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The coupon code isn\'t valid. Verify the code and try again.');
$couponCode = 'non_existent_coupon_code';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testApplyCouponToNonExistentCart()
{
$this->expectException(\Exception::class);
$couponCode = '2?ds5!2d';
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId, $couponCode);
self::expectExceptionMessage('Could not find a cart with ID "' . $maskedQuoteId . '"');
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* Products in cart don't fit to the coupon
*
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/restrict_coupon_usage_for_simple_product.php
*/
public function testApplyCouponWhichIsNotApplicable()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The coupon code isn\'t valid. Verify the code and try again.');
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* Retrieve customer authorization headers
*
* @param string $username
* @param string $password
* @return array
* @throws AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
/**
* @param string $maskedQuoteId
* @param string $couponCode
* @return string
*/
private function getQuery(string $maskedQuoteId, string $couponCode): string
{
return <<<QUERY
mutation {
applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) {
cart {
applied_coupon {
code
}
}
}
}
QUERY;
}
}
@@ -0,0 +1,222 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Integration\Api\CustomerTokenServiceInterface;
/**
* Test for assigning customer to the guest cart
*/
class AssignCustomerToGuestCartTest extends GraphQlAbstract
{
/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;
/**
* @var GetQuoteByReservedOrderId
*/
private $getQuoteByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->getQuoteByReservedOrderId = $objectManager->get(GetQuoteByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* Test for assigning customer to the guest cart
*
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testAssignCustomerToGuestCart(): void
{
$guestQuote = $this->getQuoteByReservedOrderId->execute('test_order_with_virtual_product_without_address');
$guestQuoteItem = $guestQuote->getAllVisibleItems()[0];
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$customerQuote = $this->getQuoteByReservedOrderId->execute('test_quote');
$customerQuoteItem = $customerQuote->getAllVisibleItems()[0];
$response = $this->graphQlMutation(
$this->getAssignCustomerToGuestCartMutation($guestQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
$this->assertArrayHasKey('assignCustomerToGuestCart', $response);
$this->assertArrayHasKey('items', $response['assignCustomerToGuestCart']);
$items = $response['assignCustomerToGuestCart']['items'];
$this->assertCount(2, $items);
$this->assertEquals($customerQuoteItem->getQty(), $items[1]['quantity']);
$this->assertEquals($customerQuoteItem->getSku(), $items[1]['product']['sku']);
$this->assertEquals($guestQuoteItem->getQty(), $items[0]['quantity']);
$this->assertEquals($guestQuoteItem->getSku(), $items[0]['product']['sku']);
}
/**
* Test that customer cart is expired after assigning
*
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testCustomerCartExpiryAfterAssigning(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The cart isn\'t active.');
$guestQuote = $this->getQuoteByReservedOrderId->execute('test_order_with_virtual_product_without_address');
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$customerQuote = $this->getQuoteByReservedOrderId->execute('test_quote');
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
$this->graphQlMutation(
$this->getAssignCustomerToGuestCartMutation($guestQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
$this->graphQlMutation(
$this->getCartQuery($customerQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
}
/**
* Test for assigning customer to non existent cart
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testAssigningCustomerToNonExistentCart(): void
{
$guestQuoteMaskedId = "non_existent_masked_id";
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Could not find a cart with ID \"{$guestQuoteMaskedId}\"");
$this->graphQlMutation(
$this->getAssignCustomerToGuestCartMutation($guestQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
}
/**
* Test for assigning customer to the customer cart
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testAssignCustomerToCustomerCart(): void
{
$customerQuote = $this->getQuoteByReservedOrderId->execute('test_quote');
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$customerQuoteMaskedId}\""
);
$this->graphQlMutation(
$this->getAssignCustomerToGuestCartMutation($customerQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
}
/**
* Create the assignCustomerToGuestCart mutation
*
* @param string $guestQuoteMaskedId
* @return string
*/
private function getAssignCustomerToGuestCartMutation(string $guestQuoteMaskedId): string
{
return <<<QUERY
mutation {
assignCustomerToGuestCart(
cart_id: "{$guestQuoteMaskedId}"
){
items {
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* Get cart query
*
* @param string $maskedId
* @return string
*/
private function getCartQuery(string $maskedId): string
{
return <<<QUERY
{
cart(cart_id: "{$maskedId}") {
items {
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* Retrieve customer authorization headers
*
* @param string $username
* @param string $password
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test is getting cart discount for registered customer.
*/
class CartDiscountTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-10, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testGetDiscountInformationWithTwoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-11, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders', 'Test Coupon for General'], $discountResponse['label']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetDiscountInformationWithNoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertNull($response['cart']['prices']['discount']);
}
/**
* Generates GraphQl query for retrieving cart discount
*
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
prices {
discount {
label
amount {
value
currency
}
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,308 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test getting cart totals for registered customer
*/
class CartTotalsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetCartTotalsWithTaxApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$cartItem = $response['cart']['items'][0];
self::assertEquals(10, $cartItem['prices']['price']['value']);
self::assertEquals(10.75, $cartItem['prices']['price_including_tax']['value']);
self::assertEquals(20, $cartItem['prices']['row_total']['value']);
self::assertEquals(21.5, $cartItem['prices']['row_total_including_tax']['value']);
self::assertArrayHasKey('prices', $response['cart']);
$pricesResponse = $response['cart']['prices'];
self::assertEquals(21.5, $pricesResponse['grand_total']['value']);
self::assertEquals(21.5, $pricesResponse['subtotal_including_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);
$appliedTaxesResponse = $pricesResponse['applied_taxes'];
self::assertEquals('US-TEST-*-Rate-1', $appliedTaxesResponse[0]['label']);
self::assertEquals(1.5, $appliedTaxesResponse[0]['amount']['value']);
self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_with_translated_titles.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetCartTotalsWithTranslatedTaxTitles()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$cartItem = $response['cart']['items'][0];
self::assertEquals(10, $cartItem['prices']['price']['value']);
self::assertEquals(20, $cartItem['prices']['row_total']['value']);
self::assertEquals(21.5, $cartItem['prices']['row_total_including_tax']['value']);
self::assertArrayHasKey('prices', $response['cart']);
$pricesResponse = $response['cart']['prices'];
self::assertEquals(21.5, $pricesResponse['grand_total']['value']);
self::assertEquals(21.5, $pricesResponse['subtotal_including_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);
$appliedTaxesResponse = $pricesResponse['applied_taxes'];
self::assertEquals('Rate Title on storeview 1', $appliedTaxesResponse[0]['label']);
self::assertEquals(1.5, $appliedTaxesResponse[0]['amount']['value']);
self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetCartTotalsWithEmptyCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('prices', $response['cart']);
$pricesResponse = $response['cart']['prices'];
self::assertEquals(0, $pricesResponse['grand_total']['value']);
self::assertEquals(0, $pricesResponse['subtotal_including_tax']['value']);
self::assertEquals(0, $pricesResponse['subtotal_excluding_tax']['value']);
self::assertEquals(0, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);
$appliedTaxesResponse = $pricesResponse['applied_taxes'];
self::assertCount(0, $appliedTaxesResponse);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetTotalsWithNoTaxApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$cartItem = $response['cart']['items'][0];
self::assertEquals(10, $cartItem['prices']['price']['value']);
self::assertEquals(10, $cartItem['prices']['price_including_tax']['value']);
self::assertEquals(20, $cartItem['prices']['row_total']['value']);
self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']);
$pricesResponse = $response['cart']['prices'];
self::assertEquals(20, $pricesResponse['grand_total']['value']);
self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);
self::assertEmpty($pricesResponse['applied_taxes']);
}
/**
* The totals calculation is based on quote address.
* But the totals should be calculated even if no address is set
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetCartTotalsWithNoAddressSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$cartItem = $response['cart']['items'][0];
self::assertEquals(10, $cartItem['prices']['price']['value']);
self::assertEquals(10, $cartItem['prices']['price_including_tax']['value']);
self::assertEquals(20, $cartItem['prices']['row_total']['value']);
self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']);
$pricesResponse = $response['cart']['prices'];
self::assertEquals(20, $pricesResponse['grand_total']['value']);
self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']);
self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);
self::assertEmpty($pricesResponse['applied_taxes']);
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetTotalsFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetTotalsFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
/**
* Generates GraphQl query for retrieving cart totals
*
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
items {
prices {
price {
value
currency
}
price_including_tax {
value
currency
}
row_total {
value
currency
}
row_total_including_tax {
value
currency
}
}
}
prices {
grand_total {
value
currency
}
subtotal_including_tax {
value
currency
}
subtotal_excluding_tax {
value
currency
}
subtotal_with_discount_excluding_tax {
value
currency
}
applied_taxes {
label
amount {
value
currency
}
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,539 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Registry;
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* End to checkout tests for customer
*/
class CheckoutEndToEndTest extends GraphQlAbstract
{
/**
* @var Registry
*/
private $registry;
/**
* @var QuoteCollectionFactory
*/
private $quoteCollectionFactory;
/**
* @var QuoteResource
*/
private $quoteResource;
/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @var CollectionFactory
*/
private $orderCollectionFactory;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var OrderFactory
*/
private $orderFactory;
/**
* @var array
*/
private $headers = [];
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->registry = $objectManager->get(Registry::class);
$this->quoteCollectionFactory = $objectManager->get(QuoteCollectionFactory::class);
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteIdMaskFactory = $objectManager->get(QuoteIdMaskFactory::class);
$this->customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
$this->orderCollectionFactory = $objectManager->get(CollectionFactory::class);
$this->orderRepository = $objectManager->get(OrderRepositoryInterface::class);
$this->orderFactory = $objectManager->get(OrderFactory::class);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
*/
public function testCheckoutWorkflow()
{
$quantity = 2;
$this->createCustomer();
$token = $this->loginCustomer();
$this->headers = ['Authorization' => 'Bearer ' . $token];
$sku = $this->findProduct();
$cartId = $this->createEmptyCart();
$this->addProductToCart($cartId, $quantity, $sku);
$this->setBillingAddress($cartId);
$shippingMethod = $this->setShippingAddress($cartId);
$paymentMethod = $this->setShippingMethod($cartId, $shippingMethod);
$this->setPaymentMethod($cartId, $paymentMethod);
$orderIncrementId = $this->placeOrder($cartId);
$order = $this->orderFactory->create();
$order->loadByIncrementId($orderIncrementId);
$this->checkOrderInHistory($orderIncrementId);
$this->assertNotEmpty($order->getEmailSent());
}
/**
* @return void
*/
private function createCustomer(): void
{
$query = <<<QUERY
mutation {
createCustomer(
input: {
firstname: "endto"
lastname: "endtester"
email: "customer@example.com"
password: "123123Qa"
}
) {
customer {
id
}
}
}
QUERY;
$this->graphQlMutation($query);
}
/**
* @return string
*/
private function loginCustomer(): string
{
$query = <<<QUERY
mutation {
generateCustomerToken(
email: "customer@example.com"
password: "123123Qa"
) {
token
}
}
QUERY;
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('generateCustomerToken', $response);
self::assertArrayHasKey('token', $response['generateCustomerToken']);
self::assertNotEmpty($response['generateCustomerToken']['token']);
return $response['generateCustomerToken']['token'];
}
/**
* @return string
*/
private function findProduct(): string
{
$query = <<<QUERY
{
products (
filter: {
sku: {
eq:"simple1"
}
}
pageSize: 1
currentPage: 1
) {
items {
sku
}
}
}
QUERY;
$response = $this->graphQlQuery($query);
self::assertArrayHasKey('products', $response);
self::assertArrayHasKey('items', $response['products']);
self::assertCount(1, $response['products']['items']);
$product = current($response['products']['items']);
self::assertArrayHasKey('sku', $product);
self::assertNotEmpty($product['sku']);
return $product['sku'];
}
/**
* @return string
*/
private function createEmptyCart(): string
{
$query = <<<QUERY
mutation {
createEmptyCart
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->headers);
self::assertArrayHasKey('createEmptyCart', $response);
self::assertNotEmpty($response['createEmptyCart']);
return $response['createEmptyCart'];
}
/**
* @param string $cartId
* @param float $qty
* @param string $sku
* @return void
*/
private function addProductToCart(string $cartId, float $qty, string $sku): void
{
$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{$cartId}"
cart_items: [
{
data: {
quantity: {$qty}
sku: "{$sku}"
}
}
]
}
) {
cart {
items {
quantity
product {
sku
}
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->headers);
}
/**
* @param string $cartId
* @param array $auth
* @return array
*/
private function setBillingAddress(string $cartId): void
{
$query = <<<QUERY
mutation {
setBillingAddressOnCart(
input: {
cart_id: "{$cartId}"
billing_address: {
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
postcode: "887766"
telephone: "88776655"
region: "TX"
country_code: "US"
}
}
}
) {
cart {
billing_address {
__typename
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->headers);
}
/**
* @param string $cartId
* @return array
*/
private function setShippingAddress(string $cartId): array
{
$query = <<<QUERY
mutation {
setShippingAddressesOnCart(
input: {
cart_id: "$cartId"
shipping_addresses: [
{
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "TX"
postcode: "887766"
country_code: "US"
telephone: "88776655"
}
}
]
}
) {
cart {
shipping_addresses {
available_shipping_methods {
carrier_code
method_code
amount {
value
}
}
}
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->headers);
self::assertArrayHasKey('setShippingAddressesOnCart', $response);
self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
self::assertArrayHasKey('shipping_addresses', $response['setShippingAddressesOnCart']['cart']);
self::assertCount(1, $response['setShippingAddressesOnCart']['cart']['shipping_addresses']);
$shippingAddress = current($response['setShippingAddressesOnCart']['cart']['shipping_addresses']);
self::assertArrayHasKey('available_shipping_methods', $shippingAddress);
self::assertCount(1, $shippingAddress['available_shipping_methods']);
$availableShippingMethod = current($shippingAddress['available_shipping_methods']);
self::assertArrayHasKey('carrier_code', $availableShippingMethod);
self::assertNotEmpty($availableShippingMethod['carrier_code']);
self::assertArrayHasKey('method_code', $availableShippingMethod);
self::assertNotEmpty($availableShippingMethod['method_code']);
self::assertArrayHasKey('amount', $availableShippingMethod);
self::assertArrayHasKey('value', $availableShippingMethod['amount']);
self::assertNotEmpty($availableShippingMethod['amount']['value']);
return $availableShippingMethod;
}
/**
* @param string $cartId
* @param array $method
* @return array
*/
private function setShippingMethod(string $cartId, array $method): array
{
$query = <<<QUERY
mutation {
setShippingMethodsOnCart(input: {
cart_id: "{$cartId}",
shipping_methods: [
{
carrier_code: "{$method['carrier_code']}"
method_code: "{$method['method_code']}"
}
]
}) {
cart {
available_payment_methods {
code
title
}
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->headers);
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
self::assertArrayHasKey('available_payment_methods', $response['setShippingMethodsOnCart']['cart']);
self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['available_payment_methods']);
$availablePaymentMethod = current($response['setShippingMethodsOnCart']['cart']['available_payment_methods']);
self::assertArrayHasKey('code', $availablePaymentMethod);
self::assertNotEmpty($availablePaymentMethod['code']);
self::assertArrayHasKey('title', $availablePaymentMethod);
self::assertNotEmpty($availablePaymentMethod['title']);
return $availablePaymentMethod;
}
/**
* @param string $cartId
* @param array $method
* @return void
*/
private function setPaymentMethod(string $cartId, array $method): void
{
$query = <<<QUERY
mutation {
setPaymentMethodOnCart(
input: {
cart_id: "{$cartId}"
payment_method: {
code: "{$method['code']}"
}
}
) {
cart {
selected_payment_method {
code
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->headers);
}
/**
* @param string $cartId
* @return string
*/
private function placeOrder(string $cartId): string
{
$query = <<<QUERY
mutation {
placeOrder(
input: {
cart_id: "{$cartId}"
}
) {
order {
order_number
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->headers);
self::assertArrayHasKey('placeOrder', $response);
self::assertArrayHasKey('order', $response['placeOrder']);
self::assertArrayHasKey('order_number', $response['placeOrder']['order']);
self::assertNotEmpty($response['placeOrder']['order']['order_number']);
return $response['placeOrder']['order']['order_number'];
}
/**
* @param string $orderId
* @return void
*/
private function checkOrderInHistory(string $orderId): void
{
$query = <<<QUERY
{
customerOrders {
items {
increment_id
grand_total
}
}
}
QUERY;
$response = $this->graphQlQuery($query, [], '', $this->headers);
self::assertArrayHasKey('customerOrders', $response);
self::assertArrayHasKey('items', $response['customerOrders']);
self::assertCount(1, $response['customerOrders']['items']);
$order = current($response['customerOrders']['items']);
self::assertArrayHasKey('increment_id', $order);
self::assertEquals($orderId, $order['increment_id']);
self::assertArrayHasKey('grand_total', $order);
}
protected function tearDown(): void
{
$this->deleteCustomer();
$this->deleteQuote();
$this->deleteOrder();
parent::tearDown();
}
/**
* @return void
*/
private function deleteCustomer(): void
{
$email = 'customer@example.com';
try {
$customer = $this->customerRepository->get($email);
} catch (\Exception $exception) {
return;
}
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', true);
$this->customerRepository->delete($customer);
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', false);
}
/**
* @return void
*/
private function deleteQuote(): void
{
$quoteCollection = $this->quoteCollectionFactory->create();
foreach ($quoteCollection as $quote) {
$this->quoteResource->delete($quote);
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($quote->getId())
->delete();
}
}
/**
* @return void
*/
private function deleteOrder()
{
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', true);
$orderCollection = $this->orderCollectionFactory->create();
foreach ($orderCollection as $order) {
$this->orderRepository->delete($order);
}
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', false);
}
}
@@ -0,0 +1,217 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Quote\Api\GuestCartRepositoryInterface;
/**
* Test for empty cart creation mutation for customer
*/
class CreateEmptyCartTest extends GraphQlAbstract
{
/**
* @var GuestCartRepositoryInterface
*/
private $guestCartRepository;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var QuoteCollectionFactory
*/
private $quoteCollectionFactory;
/**
* @var QuoteResource
*/
private $quoteResource;
/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteCollectionFactory = $objectManager->get(QuoteCollectionFactory::class);
$this->guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteIdMaskFactory = $objectManager->get(QuoteIdMaskFactory::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCreateEmptyCart()
{
$query = $this->getQuery();
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
self::assertArrayHasKey('createEmptyCart', $response);
self::assertNotEmpty($response['createEmptyCart']);
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
self::assertNotNull($guestCart->getId());
self::assertEquals(1, $guestCart->getCustomer()->getId());
self::assertEquals('default', $guestCart->getStore()->getCode());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCreateEmptyMultipleRequestsCart()
{
$query = $this->getQuery();
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
self::assertArrayHasKey('createEmptyCart', $response);
self::assertNotEmpty($response['createEmptyCart']);
$maskedCartId = $response['createEmptyCart'];
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
self::assertArrayHasKey('createEmptyCart', $response);
self::assertNotEmpty($response['createEmptyCart']);
self::assertEquals($maskedCartId, $response['createEmptyCart']);
}
/**
* @magentoApiDataFixture Magento/Store/_files/second_store.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCreateEmptyCartWithNotDefaultStore()
{
$query = $this->getQuery();
$headerMap = $this->getHeaderMapWithCustomerToken();
$headerMap['Store'] = 'fixture_second_store';
$response = $this->graphQlMutation($query, [], '', $headerMap);
self::assertArrayHasKey('createEmptyCart', $response);
self::assertNotEmpty($response['createEmptyCart']);
/* guestCartRepository is used for registered customer to get the cart hash */
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
self::assertNotNull($guestCart->getId());
self::assertEquals(1, $guestCart->getCustomer()->getId());
self::assertEquals('fixture_second_store', $guestCart->getStore()->getCode());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCreateEmptyCartWithPredefinedCartId()
{
$predefinedCartId = '572cda51902b5b517c0e1a2b2fd004b4';
$query = <<<QUERY
mutation {
createEmptyCart (input: {cart_id: "{$predefinedCartId}"})
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
self::assertArrayHasKey('createEmptyCart', $response);
self::assertEquals($predefinedCartId, $response['createEmptyCart']);
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
self::assertNotNull($guestCart->getId());
self::assertEquals(1, $guestCart->getCustomer()->getId());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
*/
public function testCreateEmptyCartIfPredefinedCartIdAlreadyExists()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cart with ID "572cda51902b5b517c0e1a2b2fd004b4" already exists.');
$predefinedCartId = '572cda51902b5b517c0e1a2b2fd004b4';
$query = <<<QUERY
mutation {
createEmptyCart (input: {cart_id: "{$predefinedCartId}"})
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
$this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
*/
public function testCreateEmptyCartWithWrongPredefinedCartId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cart ID length should to be 32 symbols.');
$predefinedCartId = '572';
$query = <<<QUERY
mutation {
createEmptyCart (input: {cart_id: "{$predefinedCartId}"})
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMapWithCustomerToken());
}
/**
* @return string
*/
private function getQuery(): string
{
return <<<QUERY
mutation {
createEmptyCart
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMapWithCustomerToken(
string $username = 'customer@example.com',
string $password = 'password'
): array {
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
protected function tearDown(): void
{
$quoteCollection = $this->quoteCollectionFactory->create();
foreach ($quoteCollection as $quote) {
$this->quoteResource->delete($quote);
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($quote->getId())
->delete();
}
parent::tearDown();
}
}
@@ -0,0 +1,101 @@
<?php
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Customer\Test\Fixture\Customer;
use Magento\GraphQl\GetCustomerAuthenticationHeader;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for guest cart creation mutation for customer
*/
class CreateGuestCartTest extends GraphQlAbstract
{
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var QuoteCollectionFactory
*/
private $quoteCollectionFactory;
/**
* @var QuoteResource
*/
private $quoteResource;
/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->quoteCollectionFactory = $this->objectManager->get(QuoteCollectionFactory::class);
$this->quoteResource = $this->objectManager->get(QuoteResource::class);
$this->quoteIdMaskFactory = $this->objectManager->get(QuoteIdMaskFactory::class);
}
#[
DataFixture(Customer::class, as: 'customer')
]
public function testFailForLoggedInUser()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Use `Query.customerCart` for logged in customer.");
$customer = DataFixtureStorageManager::getStorage()->get('customer');
$query = $this->getQuery();
$this->graphQlMutation(
$query,
[],
'',
$this->objectManager->get(GetCustomerAuthenticationHeader::class)->execute($customer->getEmail())
);
}
/**
* @return string
*/
private function getQuery(): string
{
return <<<QUERY
mutation {
createGuestCart {
cart {
id
}
}
}
QUERY;
}
protected function tearDown(): void
{
$quoteCollection = $this->quoteCollectionFactory->create();
foreach ($quoteCollection as $quote) {
$this->quoteResource->delete($quote);
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($quote->getId())
->delete();
}
parent::tearDown();
}
}
@@ -0,0 +1,188 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for get available payment methods
*/
class GetAvailablePaymentMethodsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailablePaymentMethods()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('available_payment_methods', $response['cart']);
self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']);
self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product_with_zero_price.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product_with_zero_price.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_freeshipping_shipping_method.php
*/
public function testGetAvailablePaymentMethodsForZeroSubTotalCheckout(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('available_payment_methods', $response['cart']);
self::assertEquals('free', current($response['cart']['available_payment_methods'])['code']);
self::assertEquals(
'No Payment Information Required',
current($response['cart']['available_payment_methods'])['title']
);
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailablePaymentMethodsFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailablePaymentMethodsFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoConfigFixture default_store payment/paypal_billing_agreement/active 0
* @magentoConfigFixture default_store payment/checkmo/active 0
* @magentoConfigFixture default_store payment/free/active 0
*/
public function testGetAvailablePaymentMethodsIfPaymentsAreNotPresent()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('available_payment_methods', $response['cart']);
self::assertEmpty($response['cart']['available_payment_methods']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
*/
public function testGetAvailablePaymentMethodsOfNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
available_payment_methods {
code
title
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,252 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for get available shipping methods
*/
class GetAvailableShippingMethodsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* Test case: get available shipping methods from current customer quote
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailableShippingMethods()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]);
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);
$expectedAddressData = [
'amount' => [
'value' => 10,
'currency' => 'USD',
],
'carrier_code' => 'flatrate',
'carrier_title' => 'Flat Rate',
'error_message' => '',
'method_code' => 'flatrate',
'method_title' => 'Fixed',
'price_incl_tax' => [
'value' => 10,
'currency' => 'USD',
],
'price_excl_tax' => [
'value' => 10,
'currency' => 'USD',
],
];
self::assertEquals(
$expectedAddressData,
$response['cart']['shipping_addresses'][0]['available_shipping_methods'][0]
);
}
/**
* Test case: get available shipping methods from current customer quote with configurable product
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/CatalogRule/_files/configurable_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_configurable_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailableShippingMethodsWithConfigurableProduct()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]);
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);
$expectedAddressData = [
'amount' => [
'value' => 5,
'currency' => 'USD',
],
'carrier_code' => 'flatrate',
'carrier_title' => 'Flat Rate',
'error_message' => '',
'method_code' => 'flatrate',
'method_title' => 'Fixed',
'price_incl_tax' => [
'value' => 5,
'currency' => 'USD',
],
'price_excl_tax' => [
'value' => 5,
'currency' => 'USD',
],
];
self::assertEquals(
$expectedAddressData,
$response['cart']['shipping_addresses'][0]['available_shipping_methods'][0]
);
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailableShippingMethodsFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* Test case: get available shipping methods from quote of another customer
*
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailableShippingMethodsFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
/**
* Test case: get available shipping methods when all shipping methods are disabled
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoConfigFixture default_store carriers/flatrate/active 0
* @magentoConfigFixture default_store carriers/tablerate/active 0
* @magentoConfigFixture default_store carriers/freeshipping/active 0
*/
public function testGetAvailableShippingMethodsIfShippingMethodsAreNotPresent()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());
self::assertEmpty($response['cart']['shipping_addresses'][0]['available_shipping_methods']);
}
/**
* Test case: get available shipping methods from non-existent cart
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetAvailableShippingMethodsOfNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
query {
cart (cart_id: "{$maskedQuoteId}") {
shipping_addresses {
available_shipping_methods {
amount {
value
currency
}
carrier_code
carrier_title
error_message
method_code
method_title
price_excl_tax {
value
currency
}
price_incl_tax {
value
currency
}
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,126 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for getting email from cart
*/
class GetCartEmailTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testGetCartEmail()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('cart', $response);
$this->assertArrayHasKey('email', $response['cart']);
$this->assertEquals('customer@example.com', $response['cart']['email']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetCartEmailFromNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
*/
public function testGetEmailFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testGetEmailFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id:"$maskedQuoteId") {
email
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,119 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for getting is_virtual from cart
*/
class GetCartIsVirtualTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetCartIsNotVirtual()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('cart', $response);
$this->assertArrayHasKey('is_virtual', $response['cart']);
$this->assertFalse($response['cart']['is_virtual']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_virtual_product.php
*/
public function testGetMixedCartIsNotVirtual()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('cart', $response);
$this->assertArrayHasKey('is_virtual', $response['cart']);
$this->assertFalse($response['cart']['is_virtual']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_virtual_product.php
*/
public function testGetCartIsVirtual()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('cart', $response);
$this->assertArrayHasKey('is_virtual', $response['cart']);
$this->assertTrue($response['cart']['is_virtual']);
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id:"$maskedQuoteId") {
is_virtual
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,292 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\Customer\Model\CustomerAuthUpdate;
use Magento\Customer\Model\CustomerRegistry;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for getting cart information
*/
class GetCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var CustomerAuthUpdate
*/
private $customerAuthUpdate;
/**
* @var CustomerRegistry
*/
private $customerRegistry;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
$this->customerAuthUpdate = Bootstrap::getObjectManager()->get(CustomerAuthUpdate::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_virtual_product.php
*/
public function testGetCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('id', $response['cart']);
self::assertEquals($maskedQuoteId, $response['cart']['id']);
self::assertArrayHasKey('items', $response['cart']);
self::assertCount(2, $response['cart']['items']);
self::assertNotEmpty($response['cart']['items'][0]['id']);
self::assertEquals(2, $response['cart']['items'][0]['quantity']);
self::assertEquals('simple_product', $response['cart']['items'][0]['product']['sku']);
self::assertNotEmpty($response['cart']['items'][1]['id']);
self::assertEquals(2, $response['cart']['items'][1]['quantity']);
self::assertEquals('virtual-product', $response['cart']['items'][1]['product']['sku']);
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
*/
public function testGetGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testGetAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetCartIfCartIdIsEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_id" is missing');
$maskedQuoteId = '';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetCartIfCartIdIsMissed()
{
$this->expectException(\Exception::class);
$message = 'Field "cart" argument "cart_id" of type "String!" is required but not provided.';
$this->expectExceptionMessage($message);
$query = <<<QUERY
{
cart {
email
}
}
QUERY;
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
*/
public function testGetNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/make_cart_inactive.php
*
*/
public function testGetInactiveCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The cart isn\'t active.');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_customer_not_default_store.php
*/
public function testGetCartWithNotDefaultStore()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1_not_default_store');
$query = $this->getQuery($maskedQuoteId);
$headerMap = $this->getHeaderMap();
$headerMap['Store'] = 'fixture_second_store';
$response = $this->graphQlQuery($query, [], '', $headerMap);
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('items', $response['cart']);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
* @magentoApiDataFixture Magento/Store/_files/second_store.php
*
*/
public function testGetCartWithWrongStore()
{
$this->expectException(\Exception::class);
$message = 'The account sign-in was incorrect or your account is disabled temporarily.';
$this->expectExceptionMessage($message.' Please wait and try again later.');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$query = $this->getQuery($maskedQuoteId);
$headerMap = $this->getHeaderMap();
$headerMap['Store'] = 'fixture_second_store';
$this->graphQlQuery($query, [], '', $headerMap);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_customer_not_default_store.php
*
*/
public function testGetCartWithNotExistingStore()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Requested store is not found');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1_not_default_store');
$query = $this->getQuery($maskedQuoteId);
$headerMap = $this->getHeaderMap();
$headerMap['Store'] = 'not_existing_store';
$this->graphQlQuery($query, [], '', $headerMap);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetCartForLockedCustomer()
{
$this->markTestSkipped('https://github.com/magento/graphql-ce/issues/750');
/* lock customer */
$customerSecure = $this->customerRegistry->retrieveSecureData(1);
$customerSecure->setLockExpires('2030-12-31 00:00:00');
$this->customerAuthUpdate->saveAuth(1);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The account is locked"
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "{$maskedQuoteId}") {
id
items {
id
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,265 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Quote\Model\ResourceModel\Quote\Collection;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for getting Customer cart information
*/
class GetCustomerCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
/** @var \Magento\Quote\Model\Quote $quote */
$quoteCollection = $this->objectManager->create(Collection::class);
foreach ($quoteCollection as $quote) {
$quote->delete();
}
parent::tearDown();
}
/**
* Query for an existing active customer cart
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetActiveCustomerCart()
{
$quantity = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$customerCartQuery = $this->getCustomerCartQuery();
$response = $this->graphQlQuery($customerCartQuery, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('customerCart', $response);
$this->assertArrayHasKey('items', $response['customerCart']);
$this->assertNotEmpty($response['customerCart']['items']);
$this->assertEquals(2, $response['customerCart']['total_quantity']);
$this->assertArrayHasKey('id', $response['customerCart']);
$this->assertNotEmpty($response['customerCart']['id']);
$this->assertEquals($maskedQuoteId, $response['customerCart']['id']);
$this->assertEquals(
$quantity,
$response['customerCart']['items'][0]['quantity'],
'Incorrect quantity of products in cart'
);
}
/**
* Query for an existing customer cart with no masked quote id
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart_without_masked_quote_id.php
*/
public function testGetLoggedInCustomerCartWithoutMaskedQuoteId()
{
$customerCartQuery = $this->getCustomerCartQuery();
$response = $this->graphQlQuery($customerCartQuery, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('customerCart', $response);
$this->assertArrayHasKey('items', $response['customerCart']);
$this->assertEmpty($response['customerCart']['items']);
$this->assertEquals(0, $response['customerCart']['total_quantity']);
$this->assertArrayHasKey('id', $response['customerCart']);
$this->assertNotEmpty($response['customerCart']['id']);
$this->assertNotNull($response['customerCart']['id']);
}
/**
* Query for customer cart for a user with no existing active cart
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetNewCustomerCart()
{
$customerCartQuery = $this->getCustomerCartQuery();
$response = $this->graphQlQuery($customerCartQuery, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('customerCart', $response);
$this->assertArrayHasKey('id', $response['customerCart']);
$this->assertNotNull($response['customerCart']['id']);
$this->assertNotEmpty($response['customerCart']['id']);
$this->assertEmpty($response['customerCart']['items']);
$this->assertEquals(0, $response['customerCart']['total_quantity']);
}
/**
* Query for customer cart with no customer token passed
*
*/
public function testGetCustomerCartWithNoCustomerToken()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The request is allowed for logged in customer');
$customerCartQuery = $this->getCustomerCartQuery();
$this->graphQlQuery($customerCartQuery);
}
/**
* Query for customer cart after customer token is revoked
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetCustomerCartAfterTokenRevoked()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The request is allowed for logged in customer');
$customerCartQuery = $this->getCustomerCartQuery();
$headers = $this->getHeaderMap();
$response = $this->graphQlMutation($customerCartQuery, [], '', $headers);
$this->assertArrayHasKey('customerCart', $response);
$this->assertArrayHasKey('id', $response['customerCart']);
$this->assertNotNull($response['customerCart']['id']);
$this->assertNotEmpty($response['customerCart']['id']);
$this->revokeCustomerToken();
$customerCartQuery = $this->getCustomerCartQuery();
$this->graphQlQuery($customerCartQuery, [], '', $headers);
}
/**
* Querying for the customer cart twice->should return the same cart
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testRequestCustomerCartTwice()
{
$customerCartQuery = $this->getCustomerCartQuery();
$response = $this->graphQlMutation($customerCartQuery, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('customerCart', $response);
$this->assertArrayHasKey('id', $response['customerCart']);
$this->assertNotNull($response['customerCart']['id']);
$cartId = $response['customerCart']['id'];
$customerCartQuery = $this->getCustomerCartQuery();
$response2 = $this->graphQlQuery($customerCartQuery, [], '', $this->getHeaderMap());
$this->assertEquals($cartId, $response2['customerCart']['id']);
}
/**
* Query for inactive Customer cart - in case of not finding an active cart, it should create a new one
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/make_cart_inactive.php
*/
public function testGetInactiveCustomerCart()
{
$customerCartQuery = $this->getCustomerCartQuery();
$response = $this->graphQlQuery($customerCartQuery, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('customerCart', $response);
$this->assertNotEmpty($response['customerCart']['id']);
$this->assertEmpty($response['customerCart']['items']);
$this->assertEmpty($response['customerCart']['total_quantity']);
}
/**
* Querying for an existing customer cart for second store
*
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_customer_not_default_store.php
*/
public function testGetCustomerCartSecondStore()
{
$customerCartQuery = $this->getCustomerCartQuery();
$maskedQuoteIdSecondStore = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1_not_default_store');
$headerMap = $this->getHeaderMap();
$headerMap['Store'] = 'fixture_second_store';
$responseSecondStore = $this->graphQlQuery($customerCartQuery, [], '', $headerMap);
$this->assertEquals($maskedQuoteIdSecondStore, $responseSecondStore['customerCart']['id']);
}
/**
* Query to revoke customer token
*
* @return void
*/
private function revokeCustomerToken(): void
{
$query = <<<QUERY
mutation{
revokeCustomerToken{
result
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* Query customer cart
*
* @return string
*/
private function getCustomerCartQuery(): string
{
return <<<QUERY
{
customerCart {
total_quantity
id
items {
id
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* Create a header with customer token
*
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,174 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for getting selected payment method from cart
*/
class GetSelectedPaymentMethodTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*/
public function testGetSelectedPaymentMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('cart', $response);
$this->assertArrayHasKey('selected_payment_method', $response['cart']);
$this->assertArrayHasKey('code', $response['cart']['selected_payment_method']);
$this->assertEquals('checkmo', $response['cart']['selected_payment_method']['code']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetSelectedPaymentMethodBeforeSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('cart', $response);
$this->assertArrayHasKey('selected_payment_method', $response['cart']);
$this->assertArrayHasKey('code', $response['cart']['selected_payment_method']);
$this->assertEquals('', $response['cart']['selected_payment_method']['code']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetSelectedPaymentMethodFromNonExistentCart()
{
$this->expectException(\Exception::class);
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
'Could not find a cart with ID "non_existent_masked_id"'
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*/
public function testGetSelectedPaymentMethodFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testGetSelectedPaymentMethodFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id:"$maskedQuoteId") {
selected_payment_method {
code
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,383 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Framework\Exception\AuthenticationException;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for get selected shipping method
*/
class GetSelectedShippingMethodTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoConfigFixture default_store tax/calculation/price_includes_tax 1
* @magentoConfigFixture default_store tax/calculation/shipping_includes_tax 1
* @magentoConfigFixture default_store tax/cart_display/shipping 2
* @magentoConfigFixture default_store tax/classes/shipping_tax_class 2
* @magentoConfigFixture default_store tax/display/shipping 2
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/Tax/_files/tax_rule_region_1_al.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodWithTax(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Flat Rate', $shippingAddress['selected_shipping_method']['carrier_title']);
self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Fixed', $shippingAddress['selected_shipping_method']['method_title']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
$amount = $shippingAddress['selected_shipping_method']['amount'];
self::assertArrayHasKey('value', $amount);
self::assertEquals(10, $amount['value']);
self::assertArrayHasKey('currency', $amount);
self::assertEquals('USD', $amount['currency']);
self::assertArrayHasKey('price_excl_tax', $shippingAddress['selected_shipping_method']);
$priceExclTax = $shippingAddress['selected_shipping_method']['price_excl_tax'];
self::assertArrayHasKey('value', $priceExclTax);
self::assertEquals(10, $priceExclTax['value']);
self::assertArrayHasKey('currency', $priceExclTax);
self::assertEquals('USD', $priceExclTax['currency']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
$priceInclTax = $shippingAddress['selected_shipping_method']['price_incl_tax'];
self::assertArrayHasKey('value', $priceInclTax);
self::assertEquals(10.75, $priceInclTax['value']);
self::assertArrayHasKey('currency', $priceInclTax);
self::assertEquals('USD', $priceInclTax['currency']);
}
/**
* @magentoConfigFixture default_store tax/calculation/shipping_includes_tax 1
* @magentoConfigFixture default_store tax/cart_display/shipping 2
* @magentoConfigFixture default_store tax/classes/shipping_tax_class 2
* @magentoConfigFixture default_store tax/display/shipping 2
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/Tax/_files/tax_rule_region_1_al.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_canada_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodWithAddressWithoutTax(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Flat Rate', $shippingAddress['selected_shipping_method']['carrier_title']);
self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Fixed', $shippingAddress['selected_shipping_method']['method_title']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
$amount = $shippingAddress['selected_shipping_method']['amount'];
self::assertArrayHasKey('value', $amount);
self::assertEquals(10, $amount['value']);
self::assertArrayHasKey('currency', $amount);
self::assertEquals('USD', $amount['currency']);
self::assertArrayHasKey('price_excl_tax', $shippingAddress['selected_shipping_method']);
$priceExclTax = $shippingAddress['selected_shipping_method']['price_excl_tax'];
self::assertArrayHasKey('value', $priceExclTax);
self::assertEquals(10, $priceExclTax['value']);
self::assertArrayHasKey('currency', $priceExclTax);
self::assertEquals('USD', $priceExclTax['currency']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
$priceInclTax = $shippingAddress['selected_shipping_method']['price_incl_tax'];
self::assertArrayHasKey('value', $priceInclTax);
self::assertEquals(10, $priceInclTax['value']);
self::assertArrayHasKey('currency', $priceInclTax);
self::assertEquals('USD', $priceInclTax['currency']);
}
/**
* @magentoConfigFixture default_store tax/calculation/shipping_includes_tax 0
* @magentoConfigFixture default_store tax/cart_display/shipping 1
* @magentoConfigFixture default_store tax/classes/shipping_tax_class 0
* @magentoConfigFixture default_store tax/display/shipping 1
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodWithoutTax(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Flat Rate', $shippingAddress['selected_shipping_method']['carrier_title']);
self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Fixed', $shippingAddress['selected_shipping_method']['method_title']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
$amount = $shippingAddress['selected_shipping_method']['amount'];
self::assertArrayHasKey('value', $amount);
self::assertEquals(10, $amount['value']);
self::assertArrayHasKey('currency', $amount);
self::assertEquals('USD', $amount['currency']);
self::assertArrayHasKey('price_excl_tax', $shippingAddress['selected_shipping_method']);
$priceExclTax = $shippingAddress['selected_shipping_method']['price_excl_tax'];
self::assertArrayHasKey('value', $priceExclTax);
self::assertEquals(10, $priceExclTax['value']);
self::assertArrayHasKey('currency', $priceExclTax);
self::assertEquals('USD', $priceExclTax['currency']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
$priceInclTax = $shippingAddress['selected_shipping_method']['price_incl_tax'];
self::assertArrayHasKey('value', $priceInclTax);
self::assertEquals(10, $priceInclTax['value']);
self::assertArrayHasKey('currency', $priceInclTax);
self::assertEquals('USD', $priceInclTax['currency']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetSelectedShippingMethodBeforeSet(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertNull($shippingAddress['selected_shipping_method']);
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodFromGuestCart(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodFromAnotherCustomerCart(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet(): void
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertNull($shippingAddress['selected_shipping_method']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
*/
public function testGetGetSelectedShippingMethodOfNonExistentCart(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* @param string $username
* @param string $password
* @return array
* @throws AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
return ['Authorization' => 'Bearer ' . $customerToken];
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
shipping_addresses {
available_shipping_methods {
carrier_code
method_code
}
selected_shipping_method {
carrier_code
method_code
carrier_title
method_title
amount {
value
currency
}
price_excl_tax {
value
currency
}
price_incl_tax {
value
currency
}
}
}
}
}
QUERY;
}
}
@@ -0,0 +1,238 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for get specified billing address
*/
class GetSpecifiedBillingAddressTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetSpecifiedBillingAddress()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('billing_address', $response['cart']);
$expectedBillingAddressData = [
'firstname' => 'John',
'lastname' => 'Smith',
'company' => 'CompanyName',
'street' => [
'Green str, 67'
],
'city' => 'CityM',
'region' => [
'code' => 'AL',
'label' => 'Alabama',
],
'postcode' => '75477',
'country' => [
'code' => 'US',
'label' => 'US',
],
'telephone' => '3468676',
'__typename' => 'BillingCartAddress',
'customer_notes' => null,
];
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetSpecifiedBillingAddressIfBillingAddressIsNotSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('billing_address', $response['cart']);
self::assertNull($response['cart']['billing_address']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetSpecifiedBillingAddressOfNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetSpecifiedBillingAddressFromAnotherGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testGetSpecifiedBillingAddressFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery(
$this->getQuery($maskedQuoteId),
[],
'',
$this->getHeaderMap('customer2@search.example.com')
);
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
billing_address {
firstname
lastname
company
street
city
region
{
code
label
}
postcode
country
{
code
label
}
telephone
__typename
customer_notes
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address_without_telephone.php
*/
public function testGetSpecifiedBillingAddressWithoutTelephone()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('billing_address', $response['cart']);
$expectedBillingAddressData = [
'firstname' => 'John',
'lastname' => 'Smith',
'company' => 'CompanyName',
'street' => [
'Green str, 67'
],
'city' => 'CityM',
'region' => [
'code' => 'AL',
'label' => 'Alabama',
],
'postcode' => '75477',
'country' => [
'code' => 'US',
'label' => 'US',
],
'telephone' => '',
'__typename' => 'BillingCartAddress',
'customer_notes' => null,
];
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
}
}
@@ -0,0 +1,201 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for get specified shipping address
*/
class GetSpecifiedShippingAddressTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetSpecifiedShippingAddress()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
$uid = $response['cart']['shipping_addresses'][0]['uid'];
$expectedShippingAddressData = [
'uid' => $uid,
'firstname' => 'John',
'lastname' => 'Smith',
'company' => 'CompanyName',
'street' => [
'Green str, 67'
],
'city' => 'CityM',
'region' => [
'code' => 'AL',
'label' => 'Alabama',
],
'postcode' => '75477',
'country' => [
'code' => 'US',
'label' => 'US',
],
'telephone' => '3468676',
'__typename' => 'ShippingCartAddress',
'customer_notes' => null,
];
self::assertEquals($expectedShippingAddressData, current($response['cart']['shipping_addresses']));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGetSpecifiedShippingAddressIfShippingAddressIsNotSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertEquals([], $response['cart']['shipping_addresses']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGetSpecifiedShippingAddressOfNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetSpecifiedShippingAddressFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetSpecifiedShippingAddressFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery(
$this->getQuery($maskedQuoteId),
[],
'',
$this->getHeaderMap('customer2@search.example.com')
);
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
shipping_addresses {
uid
firstname
lastname
company
street
city
region
{
code
label
}
postcode
country
{
code
label
}
telephone
__typename
customer_notes
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,454 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Integration\Api\CustomerTokenServiceInterface;
/**
* Test for merging customer carts
*/
class MergeCartsTest extends GraphQlAbstract
{
/**
* @var QuoteResource
*/
private $quoteResource;
/**
* @var QuoteFactory
*/
private $quoteFactory;
/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
protected function tearDown(): void
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, '1', 'customer_id');
$this->quoteResource->delete($quote);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testMergeGuestWithCustomerCart()
{
$customerQuote = $this->quoteFactory->create();
$this->quoteResource->load($customerQuote, 'test_quote', 'reserved_order_id');
$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$query = $this->getCartMergeMutation($guestQuoteMaskedId, $customerQuoteMaskedId);
$mergeResponse = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('mergeCarts', $mergeResponse);
$cartResponse = $mergeResponse['mergeCarts'];
self::assertArrayHasKey('items', $cartResponse);
self::assertCount(2, $cartResponse['items']);
$cartResponse = $this->graphQlMutation(
$this->getCartQuery($customerQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
self::assertArrayHasKey('cart', $cartResponse);
self::assertArrayHasKey('items', $cartResponse['cart']);
self::assertCount(2, $cartResponse['cart']['items']);
$item1 = $cartResponse['cart']['items'][0];
self::assertArrayHasKey('quantity', $item1);
self::assertEquals(2, $item1['quantity']);
$item2 = $cartResponse['cart']['items'][1];
self::assertArrayHasKey('quantity', $item2);
self::assertEquals(1, $item2['quantity']);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_virtual_product_with_100_qty.php
*/
public function testMergeGuestWithCustomerCartWithOutOfStockQuantity()
{
$customerQuote = $this->quoteFactory->create();
$this->quoteResource->load($customerQuote, 'test_quote', 'reserved_order_id');
$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$query = $this->getCartMergeMutation($guestQuoteMaskedId, $customerQuoteMaskedId);
$mergeResponse = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('mergeCarts', $mergeResponse);
$cartResponse = $mergeResponse['mergeCarts'];
self::assertArrayHasKey('items', $cartResponse);
self::assertCount(1, $cartResponse['items']);
$cartResponse = $this->graphQlMutation(
$this->getCartQuery($customerQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
self::assertArrayHasKey('cart', $cartResponse);
self::assertArrayHasKey('items', $cartResponse['cart']);
self::assertCount(1, $cartResponse['cart']['items']);
$item1 = $cartResponse['cart']['items'][0];
self::assertArrayHasKey('quantity', $item1);
self::assertEquals(100, $item1['quantity']);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testGuestCartExpiryAfterMerge()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The cart isn\'t active.');
$customerQuote = $this->quoteFactory->create();
$this->quoteResource->load($customerQuote, 'test_quote', 'reserved_order_id');
$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$query = $this->getCartMergeMutation($guestQuoteMaskedId, $customerQuoteMaskedId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
$this->graphQlMutation(
$this->getCartQuery($guestQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/two_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testMergeTwoCustomerCarts()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The current user cannot perform operations on cart');
$firstQuote = $this->quoteFactory->create();
$this->quoteResource->load($firstQuote, 'test_quote', 'reserved_order_id');
$firstMaskedId = $this->quoteIdToMaskedId->execute((int)$firstQuote->getId());
$createCartResponse = $this->graphQlMutation(
$this->getCreateEmptyCartMutation(),
[],
'',
$this->getHeaderMap('customer_two@example.com')
);
self::assertArrayHasKey('createEmptyCart', $createCartResponse);
$secondMaskedId = $createCartResponse['createEmptyCart'];
$this->addSimpleProductToCart($secondMaskedId, $this->getHeaderMap());
$query = $this->getCartMergeMutation($firstMaskedId, $secondMaskedId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testMergeCartsWithEmptySourceCartId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "source_cart_id" is missing');
$customerQuote = $this->quoteFactory->create();
$this->quoteResource->load($customerQuote, 'test_quote', 'reserved_order_id');
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
$guestQuoteMaskedId = "";
$query = $this->getCartMergeMutation($guestQuoteMaskedId, $customerQuoteMaskedId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testMergeCartsWithEmptyDestinationCartId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The parameter "destination_cart_id" cannot be empty');
$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$customerQuoteMaskedId = "";
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$query = $this->getCartMergeMutation($guestQuoteMaskedId, $customerQuoteMaskedId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testMergeCartsWithoutDestinationCartId()
{
$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$query = $this->getCartMergeMutationWithoutDestinationCartId(
$guestQuoteMaskedId
);
$mergeResponse = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('mergeCarts', $mergeResponse);
$cartResponse = $mergeResponse['mergeCarts'];
self::assertArrayHasKey('items', $cartResponse);
self::assertCount(2, $cartResponse['items']);
$customerQuote = $this->quoteFactory->create();
$this->quoteResource->load($customerQuote, 'test_quote', 'reserved_order_id');
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
$cartResponse = $this->graphQlMutation(
$this->getCartQuery($customerQuoteMaskedId),
[],
'',
$this->getHeaderMap()
);
self::assertArrayHasKey('cart', $cartResponse);
self::assertArrayHasKey('items', $cartResponse['cart']);
self::assertCount(2, $cartResponse['cart']['items']);
$item1 = $cartResponse['cart']['items'][0];
self::assertArrayHasKey('quantity', $item1);
self::assertEquals(2, $item1['quantity']);
$item2 = $cartResponse['cart']['items'][1];
self::assertArrayHasKey('quantity', $item2);
self::assertEquals(1, $item2['quantity']);
}
/**
* Add simple product to cart
*
* @param string $maskedId
* @param array $headerMap
* @throws \Exception
*/
private function addSimpleProductToCart(string $maskedId, array $headerMap): void
{
$result = $this->graphQlMutation($this->getAddProductToCartMutation($maskedId), [], '', $headerMap);
self::assertArrayHasKey('addSimpleProductsToCart', $result);
self::assertArrayHasKey('cart', $result['addSimpleProductsToCart']);
self::assertArrayHasKey('items', $result['addSimpleProductsToCart']['cart']);
self::assertArrayHasKey(0, $result['addSimpleProductsToCart']['cart']['items']);
self::assertArrayHasKey('quantity', $result['addSimpleProductsToCart']['cart']['items'][0]);
self::assertEquals(1, $result['addSimpleProductsToCart']['cart']['items'][0]['quantity']);
self::assertArrayHasKey('product', $result['addSimpleProductsToCart']['cart']['items'][0]);
self::assertArrayHasKey('sku', $result['addSimpleProductsToCart']['cart']['items'][0]['product']);
self::assertEquals('simple_product', $result['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
}
/**
* Create the mergeCart mutation
*
* @param string $guestQuoteMaskedId
* @param string $customerQuoteMaskedId
* @return string
*/
private function getCartMergeMutation(string $guestQuoteMaskedId, string $customerQuoteMaskedId): string
{
return <<<QUERY
mutation {
mergeCarts(
source_cart_id: "{$guestQuoteMaskedId}"
destination_cart_id: "{$customerQuoteMaskedId}"
){
items {
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* Create the mergeCart mutation
*
* @param string $guestQuoteMaskedId
* @return string
*/
private function getCartMergeMutationWithoutDestinationCartId(
string $guestQuoteMaskedId
): string {
return <<<QUERY
mutation {
mergeCarts(
source_cart_id: "{$guestQuoteMaskedId}"
){
items {
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* Get cart query
*
* @param string $maskedId
* @return string
*/
private function getCartQuery(string $maskedId): string
{
return <<<QUERY
{
cart(cart_id: "{$maskedId}") {
items {
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* Get create empty cart mutation
*
* @return string
*/
private function getCreateEmptyCartMutation(): string
{
return <<<QUERY
mutation {
createEmptyCart
}
QUERY;
}
/**
* Get add product to cart mutation
*
* @param string $maskedId
* @return string
*/
private function getAddProductToCartMutation(string $maskedId): string
{
return <<<QUERY
mutation {
addSimpleProductsToCart(input: {
cart_id: "{$maskedId}"
cart_items: {
data: {
quantity: 1
sku: "simple_product"
}
}
}) {
cart {
items {
quantity
product {
sku
}
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,490 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Catalog\Test\Fixture\ProductStock as ProductStockFixture;
use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture;
use Magento\Customer\Test\Fixture\Customer;
use Magento\Framework\Registry;
use Magento\Indexer\Test\Fixture\Indexer;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Test\Fixture\CustomerCart;
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
use Magento\Quote\Test\Fixture\QuoteIdMask;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\TestFramework\Fixture\Config;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
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\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
/**
* Test for placing an order for customer
*/
class PlaceOrderTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var CollectionFactory
*/
private $orderCollectionFactory;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var Registry
*/
private $registry;
/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedQuoteIdInterface;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteIdToMaskedQuoteIdInterface = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->orderCollectionFactory = $objectManager->get(CollectionFactory::class);
$this->orderRepository = $objectManager->get(OrderRepositoryInterface::class);
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
}
#[
Config('carriers/flatrate/active', '1', 'store', 'default'),
Config('carriers/tablerate/active', '1', 'store', 'default'),
Config('carriers/freeshipping/active', '1', 'store', 'default'),
Config('payment/banktransfer/active', '1', 'store', 'default'),
Config('payment/cashondelivery/active', '1', 'store', 'default'),
Config('payment/checkmo/active', '1', 'store', 'default'),
Config('payment/purchaseorder/active', '1', 'store', 'default'),
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrder()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('placeOrder', $response);
self::assertArrayHasKey('order_number', $response['placeOrder']['order']);
self::assertEquals($reservedOrderId, $response['placeOrder']['order']['order_number']);
}
#[
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
]
public function testPlaceOrderIfCartIdIsEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_id" is missing');
$maskedQuoteId = '';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
#[
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderWithNoItemsInCart()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals(1, count($response['placeOrder']['errors']));
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
self::assertEquals(
'A server error stopped your order from being placed. Please try to place your order again.',
$response['placeOrder']['errors'][0]['message']
);
}
#[
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderWithNoShippingAddress()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals(1, count($response['placeOrder']['errors']));
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
self::assertEquals(
'Some addresses can\'t be used due to the configurations for specific countries.',
$response['placeOrder']['errors'][0]['message']
);
}
#[
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderWithNoShippingMethod()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals(1, count($response['placeOrder']['errors']));
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
self::assertEquals(
'The shipping method is missing. Select the shipping method and try again.',
$response['placeOrder']['errors'][0]['message']
);
}
#[
Config('carriers/flatrate/active', '1', 'store', 'default'),
Config('carriers/tablerate/active', '1', 'store', 'default'),
Config('carriers/freeshipping/active', '1', 'store', 'default'),
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderWithNoBillingAddress()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals(1, count($response['placeOrder']['errors']));
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
self::assertStringContainsString(
'Please check the billing address information.',
$response['placeOrder']['errors'][0]['message']
);
}
#[
Config('carriers/flatrate/active', '1', 'store', 'default'),
Config('carriers/tablerate/active', '1', 'store', 'default'),
Config('carriers/freeshipping/active', '1', 'store', 'default'),
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderWithNoPaymentMethod()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals(1, count($response['placeOrder']['errors']));
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
self::assertEquals(
'Enter a valid payment method and try again.',
$response['placeOrder']['errors'][0]['message']
);
}
#[
Config('carriers/flatrate/active', '1', 'store', 'default'),
Config('carriers/tablerate/active', '1', 'store', 'default'),
Config('carriers/freeshipping/active', '1', 'store', 'default'),
Config('cataloginventory/options/enable_inventory_check', 1),
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(
ProductStockFixture::class,
[
'prod_id' => '$product.id$',
'is_in_stock' => 0,
'prod_qty' => 0
],
'prodStock'
),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderWithOutOfStockProduct()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals(1, count($response['placeOrder']['errors']));
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
self::assertEquals(
'Some of the products are out of stock.',
$response['placeOrder']['errors'][0]['message']
);
}
#[
Config('carriers/flatrate/active', '1', 'store', 'default'),
Config('carriers/tablerate/active', '1', 'store', 'default'),
Config('carriers/freeshipping/active', '1', 'store', 'default'),
Config('cataloginventory/options/enable_inventory_check', 0),
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(
ProductStockFixture::class,
[
'prod_id' => '$product.id$',
'is_in_stock' => 0,
'prod_qty' => 0
],
'prodStock'
),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderWithOutOfStockProductWithDisabledInventoryCheck()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertEquals(1, count($response['placeOrder']['errors']));
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
self::assertEquals(
'Enter a valid payment method and try again.',
$response['placeOrder']['errors'][0]['message']
);
}
#[
Config('carriers/flatrate/active', '1', 'store', 'default'),
Config('carriers/tablerate/active', '1', 'store', 'default'),
Config('carriers/freeshipping/active', '1', 'store', 'default'),
Config('payment/banktransfer/active', '1', 'store', 'default'),
Config('payment/cashondelivery/active', '1', 'store', 'default'),
Config('payment/checkmo/active', '1', 'store', 'default'),
Config('payment/purchaseorder/active', '1', 'store', 'default'),
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(GuestCartFixture::class, ['reserved_order_id' => 'test_quote'], as: 'cart'),
DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
]
public function testPlaceOrderOfGuestCart()
{
$cart = DataFixtureStorageManager::getStorage()->get('cart');
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessageMatches('/The current user cannot perform operations on cart*/');
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
#[
Config('carriers/flatrate/active', '1', 'store', 'default'),
Config('carriers/tablerate/active', '1', 'store', 'default'),
Config('carriers/freeshipping/active', '1', 'store', 'default'),
Config('payment/banktransfer/active', '1', 'store', 'default'),
Config('payment/cashondelivery/active', '1', 'store', 'default'),
Config('payment/checkmo/active', '1', 'store', 'default'),
Config('payment/purchaseorder/active', '1', 'store', 'default'),
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Indexer::class, as: 'indexer'),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
DataFixture(Customer::class, ['email' => 'customer3@search.example.com'], as: 'customer2'),
DataFixture(
CustomerCart::class,
[
'customer_id' => '$customer.id$',
'reserved_order_id' => 'test_quote'
],
'cart'
),
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
]
public function testPlaceOrderOfAnotherCustomerCart()
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessageMatches('/The current user cannot perform operations on cart*/');
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
mutation {
placeOrder(input: {cart_id: "{$maskedQuoteId}"}) {
order {
order_number
}
errors {
message
code
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', true);
$orderCollection = $this->orderCollectionFactory->create();
foreach ($orderCollection as $order) {
$this->orderRepository->delete($order);
}
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', false);
parent::tearDown();
}
}
@@ -0,0 +1,186 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Check removing of the coupon from customer cart
*/
class RemoveCouponFromCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testRemoveCouponFromCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('removeCouponFromCart', $response);
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testRemoveCouponFromCartIfCartIdIsEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "cart_id" is missing');
$maskedQuoteId = '';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testRemoveCouponFromNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testRemoveCouponFromEmptyCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cart does not contain products');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveCouponFromCartIfCouponWasNotSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('removeCouponFromCart', $response);
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']);
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testRemoveCouponFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testRemoveCouponFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
mutation {
removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) {
cart {
applied_coupon {
code
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,273 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for removeItemFromCartTest mutation
*/
class RemoveItemFromCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var GetQuoteItemIdByReservedQuoteIdAndSku
*/
private $getQuoteItemIdByReservedQuoteIdAndSku;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->getQuoteItemIdByReservedQuoteIdAndSku = $objectManager->get(
GetQuoteItemIdByReservedQuoteIdAndSku::class
);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveItemFromCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$itemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');
$query = $this->getQuery($maskedQuoteId, $itemId);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('removeItemFromCart', $response);
$this->assertArrayHasKey('cart', $response['removeItemFromCart']);
$this->assertCount(0, $response['removeItemFromCart']['cart']['items']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testRemoveItemFromNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$query = $this->getQuery('non_existent_masked_id', 1);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveNonExistentItem()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$notExistentItemId = 999;
$this->expectExceptionMessage("The cart doesn't contain the item");
$query = $this->getQuery($maskedQuoteId, $notExistentItemId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
*/
public function testRemoveItemIfItemIsNotBelongToCart()
{
$firstQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$secondQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute(
'test_order_with_virtual_product',
'virtual-product'
);
$this->expectExceptionMessage("The cart doesn't contain the item");
$query = $this->getQuery($firstQuoteMaskedId, $secondQuoteItemId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveItemFromGuestCart()
{
$guestQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$guestQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$guestQuoteMaskedId\""
);
$query = $this->getQuery($guestQuoteMaskedId, $guestQuoteItemId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveItemFromAnotherCustomerCart()
{
$anotherCustomerQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$anotherCustomerQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute(
'test_quote',
'simple_product'
);
$query = $this->getQuery($anotherCustomerQuoteMaskedId, $anotherCustomerQuoteItemId);
try {
$this->graphQlMutation(
$query,
[],
'',
$this->getHeaderMap('customer2@search.example.com')
);
$this->fail('ResponseContainsErrorsException was not thrown');
} catch (ResponseContainsErrorsException $e) {
$this->assertStringContainsString(
"The current user cannot perform operations on cart \"$anotherCustomerQuoteMaskedId\"",
$e->getMessage()
);
$cartQuery = $this->getCartQuery($anotherCustomerQuoteMaskedId);
$cart = $this->graphQlQuery(
$cartQuery,
[],
'',
$this->getHeaderMap('customer@search.example.com')
);
$this->assertTrue(count($cart['cart']['items']) > 0, 'The cart is empty');
$this->assertTrue(
$cart['cart']['items'][0]['product']['sku'] === 'simple_product',
'The cart doesn\'t contain product'
);
}
}
/**
* @param string $maskedQuoteId
* @return string
*/
private function getCartQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "{$maskedQuoteId}") {
id
items {
id
quantity
product {
sku
}
}
}
}
QUERY;
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveItemWithEmptyCartId()
{
$cartId = "";
$cartItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');
$this->expectExceptionMessage("Required parameter \"cart_id\" is missing.");
$query = $this->getQuery($cartId, $cartItemId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testRemoveItemWithZeroCartItemId()
{
$cartId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$cartItemId = 0;
$this->expectExceptionMessage("Required parameter \"cart_item_id\" is missing.");
$query = $this->getQuery($cartId, $cartItemId);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @param string $maskedQuoteId
* @param int $itemId
* @return string
*/
private function getQuery(string $maskedQuoteId, int $itemId): string
{
return <<<QUERY
mutation {
removeItemFromCart(
input: {
cart_id: "{$maskedQuoteId}"
cart_item_id: {$itemId}
}
) {
cart {
items {
quantity
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,106 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for setGuestEmailOnCart mutation
*/
class SetGuestEmailOnCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*
*/
public function testSetGuestEmailOnCartForLoggedInCustomer()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The request is not allowed for logged in customers');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$email = 'some@user.com';
$query = $this->getQuery($maskedQuoteId, $email);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
*
*/
public function testSetGuestEmailOnGuestCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The request is not allowed for logged in customers');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$email = 'some@user.com';
$query = $this->getQuery($maskedQuoteId, $email);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* Returns GraphQl mutation query for setting email address for a guest
*
* @param string $maskedQuoteId
* @param string $email
* @return string
*/
private function getQuery(string $maskedQuoteId, string $email): string
{
return <<<QUERY
mutation {
setGuestEmailOnCart(input: {
cart_id: "$maskedQuoteId"
email: "$email"
}) {
cart {
email
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,183 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\OfflinePayments\Model\Banktransfer;
use Magento\OfflinePayments\Model\Cashondelivery;
use Magento\OfflinePayments\Model\Checkmo;
use Magento\OfflinePayments\Model\Purchaseorder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for setting offline payment methods on cart
*/
class SetOfflinePaymentMethodsOnCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
*
* @param string $methodCode
* @param string $methodTitle
* @dataProvider offlinePaymentMethodDataProvider
*/
public function testSetOfflinePaymentMethod(string $methodCode, string $methodTitle)
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
$selectedPaymentMethod = $response['setPaymentMethodOnCart']['cart']['selected_payment_method'];
self::assertArrayHasKey('code', $selectedPaymentMethod);
self::assertEquals($methodCode, $selectedPaymentMethod['code']);
self::assertArrayHasKey('title', $selectedPaymentMethod);
self::assertEquals($methodTitle, $selectedPaymentMethod['title']);
}
/**
* @return array
*/
public function offlinePaymentMethodDataProvider(): array
{
return [
'check_mo' => [Checkmo::PAYMENT_METHOD_CHECKMO_CODE, 'Check / Money order'],
'bank_transfer' => [Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE, 'Bank Transfer Payment'],
'cash_on_delivery' => [Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE, 'Cash On Delivery'],
];
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
*/
public function testSetPurchaseOrderPaymentMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$methodTitle = 'Purchase Order';
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$poNumber = 'abc123';
$query = <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "{$maskedQuoteId}",
payment_method: {
code: "{$methodCode}"
purchase_order_number: "{$poNumber}"
}
}) {
cart {
selected_payment_method {
code
title
purchase_order_number
}
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
$selectedPaymentMethod = $response['setPaymentMethodOnCart']['cart']['selected_payment_method'];
self::assertArrayHasKey('code', $selectedPaymentMethod);
self::assertEquals($methodCode, $selectedPaymentMethod['code']);
self::assertArrayHasKey('title', $selectedPaymentMethod);
self::assertEquals($methodTitle, $selectedPaymentMethod['title']);
self::assertArrayHasKey('purchase_order_number', $selectedPaymentMethod);
self::assertEquals($poNumber, $selectedPaymentMethod['purchase_order_number']);
}
/**
* @param string $maskedQuoteId
* @param string $methodCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $methodCode
) : string {
return <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "{$maskedQuoteId}",
payment_method: {
code: "{$methodCode}"
}
}) {
cart {
selected_payment_method {
code
title
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,180 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for setting offline shipping methods on cart
*/
class SetOfflineShippingMethodsOnCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
*
* @param string $carrierCode
* @param string $methodCode
* @param string $carrierTitle
* @param string $methodTitle
* @param array $amount
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @dataProvider offlineShippingMethodDataProvider
*/
public function testSetOfflineShippingMethod(
string $carrierCode,
string $methodCode,
string $carrierTitle,
string $methodTitle,
array $amount
) {
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery(
$maskedQuoteId,
$methodCode,
$carrierCode
);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']);
self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
$shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertEquals($carrierCode, $shippingAddress['selected_shipping_method']['carrier_code']);
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']);
self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
self::assertEquals($carrierTitle, $shippingAddress['selected_shipping_method']['carrier_title']);
self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
self::assertEquals($methodTitle, $shippingAddress['selected_shipping_method']['method_title']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
self::assertEquals($amount, $shippingAddress['selected_shipping_method']['amount']);
}
/**
* @return array
*/
public function offlineShippingMethodDataProvider(): array
{
return [
'flatrate_flatrate' => [
'flatrate',
'flatrate',
'Flat Rate',
'Fixed',
['value' => 10, 'currency' => 'USD'],
],
'tablerate_bestway' => [
'tablerate',
'bestway',
'Best Way',
'Table Rate',
['value' => 10, 'currency' => 'USD'],
],
'freeshipping_freeshipping' => [
'freeshipping',
'freeshipping',
'Free Shipping',
'Free',
['value' => 0, 'currency' => 'USD'],
],
];
}
/**
* @param string $maskedQuoteId
* @param string $shippingMethodCode
* @param string $shippingCarrierCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $shippingMethodCode,
string $shippingCarrierCode
): string {
return <<<QUERY
mutation {
setShippingMethodsOnCart(input:
{
cart_id: "$maskedQuoteId",
shipping_methods: [{
carrier_code: "$shippingCarrierCode"
method_code: "$shippingMethodCode"
}]
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
carrier_title
method_title
amount {
value
currency
}
}
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,451 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\Framework\Registry;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\OfflinePayments\Model\Checkmo;
use Magento\OfflinePayments\Model\Purchaseorder;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for setting payment method and placing order by customer
*/
class SetPaymentMethodAndPlaceOrderTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var CollectionFactory
*/
private $orderCollectionFactory;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var Registry
*/
private $registry;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->orderCollectionFactory = $objectManager->get(CollectionFactory::class);
$this->orderRepository = $objectManager->get(OrderRepositoryInterface::class);
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testSetPaymentOnCartWithSimpleProduct()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
self::assertArrayHasKey('order', $response['placeOrder']);
self::assertArrayHasKey('order_number', $response['placeOrder']['order']);
}
/**
* @magentoConfigFixture cataloginventory/options/enable_inventory_check 1
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php
*
* @dataProvider dataProviderSetPaymentOnCartWithException
* @param string $input
* @param string $message
* @throws \Exception
*/
public function testSetPaymentOnCartWithException(string $input, string $message)
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$input = str_replace('cart_id_value', $maskedQuoteId, $input);
$query = <<<QUERY
mutation {
setPaymentMethodAndPlaceOrder(
input: {
{$input}
}
) {
order {
order_number
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoConfigFixture cataloginventory/options/enable_inventory_check 0
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php
*
* @dataProvider dataProviderSetPaymentOnCartWithExceptionWithDisabledInventoryCheck
* @param string $input
* @param string $message
* @throws \Exception
*/
public function testSetPaymentOnCartWithExceptionWithDisabledInventoryCheck(string $input, string $message)
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$input = str_replace('cart_id_value', $maskedQuoteId, $input);
$query = <<<QUERY
mutation {
setPaymentMethodAndPlaceOrder(
input: {
{$input}
}
) {
order {
order_number
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @return array
*/
public function dataProviderSetPaymentOnCartWithException(): array
{
return [
'place_order_with_out_of_stock_products' => [
'cart_id: "cart_id_value"
payment_method: {
code: "' . Checkmo::PAYMENT_METHOD_CHECKMO_CODE . '"
}',
'Unable to place order: Some of the products are out of stock.',
],
];
}
/**
* @return array
*/
public function dataProviderSetPaymentOnCartWithExceptionWithDisabledInventoryCheck(): array
{
return [
'place_order_with_out_of_stock_products' => [
'cart_id: "cart_id_value"
payment_method: {
code: "' . Checkmo::PAYMENT_METHOD_CHECKMO_CODE . '"
}',
'Unable to place order: There are no source items with the in stock status',
],
];
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*
*/
public function testSetPaymentOnCartWithSimpleProductAndWithoutAddress()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The shipping address is missing. Set the address and try again.');
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_virtual_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
*/
public function testSetPaymentOnCartWithVirtualProduct()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
self::assertArrayHasKey('order', $response['placeOrder']);
self::assertArrayHasKey('order_number', $response['placeOrder']['order']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*
*/
public function testSetNonExistentPaymentMethod()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The requested Payment Method is not available.');
$methodCode = 'noway';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
*/
public function testSetPaymentOnNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetPaymentMethodToGuestCart()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetPaymentMethodToAnotherCustomerCart()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testSetDisabledPaymentOnCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The requested Payment Method is not available.');
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testPlaceOrderWitMissingCartId()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = "";
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->expectExceptionMessage(
"Required parameter \"cart_id\" is missing"
);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testPlaceOrderWithMissingPaymentMethod()
{
$methodCode = "";
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->expectExceptionMessage(
"Required parameter \"code\" for \"payment_method\" is missing."
);
$this->graphQlMutation($query);
}
/**
* @param string $maskedQuoteId
* @param string $methodCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $methodCode
) : string {
return <<<QUERY
mutation {
setPaymentMethodOnCart(
input: {
cart_id: "{$maskedQuoteId}"
payment_method: {
code: "{$methodCode}"
}
}
) {
cart {
selected_payment_method {
code
}
}
}
placeOrder(
input: {
cart_id: "{$maskedQuoteId}"
}
) {
order {
order_number
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', true);
$orderCollection = $this->orderCollectionFactory->create();
foreach ($orderCollection as $order) {
$this->orderRepository->delete($order);
}
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', false);
parent::tearDown();
}
}
@@ -0,0 +1,316 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\OfflinePayments\Model\Cashondelivery;
use Magento\OfflinePayments\Model\Checkmo;
use Magento\OfflinePayments\Model\Purchaseorder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for setting payment methods on cart by customer
*/
class SetPaymentMethodOnCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testSetPaymentOnCartWithSimpleProduct()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*
*/
public function testSetPaymentOnCartWithSimpleProductAndWithoutAddress()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The shipping address is missing. Set the address and try again.');
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_virtual_product.php
*/
public function testSetPaymentOnCartWithVirtualProduct()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*
*/
public function testSetNonExistentPaymentMethod()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The requested Payment Method is not available.');
$methodCode = 'noway';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
*/
public function testSetPaymentOnNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$maskedQuoteId = 'non_existent_masked_id';
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetPaymentMethodToGuestCart()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetPaymentMethodToAnotherCustomerCart()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*
* @param string $input
* @param string $message
* @throws Exception
* @dataProvider dataProviderSetPaymentMethodWithoutRequiredParameters
*/
public function testSetPaymentMethodWithoutRequiredParameters(string $input, string $message)
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$input = str_replace('cart_id_value', $maskedQuoteId, $input);
$query = <<<QUERY
mutation {
setPaymentMethodOnCart(
input: {
{$input}
}
) {
cart {
items {
quantity
}
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testSetDisabledPaymentOnCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The requested Payment Method is not available.');
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @return array
*/
public function dataProviderSetPaymentMethodWithoutRequiredParameters(): array
{
return [
'missed_cart_id' => [
'cart_id: "", payment_method: {code: "' . Checkmo::PAYMENT_METHOD_CHECKMO_CODE . '"}',
'Required parameter "cart_id" is missing.'
],
'missed_payment_method_code' => [
'cart_id: "cart_id_value", payment_method: {code: ""}',
'Required parameter "code" for "payment_method" is missing.'
],
];
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*/
public function testReSetPayment()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$methodCode = Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE;
$query = $this->getQuery($maskedQuoteId, $methodCode);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertArrayHasKey('code', $response['setPaymentMethodOnCart']['cart']['selected_payment_method']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
}
/**
* @param string $maskedQuoteId
* @param string $methodCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $methodCode
) : string {
return <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "$maskedQuoteId"
payment_method: {
code: "$methodCode"
}
}) {
cart {
selected_payment_method {
code
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,185 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\OfflinePayments\Model\Purchaseorder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for setting Purchase Order payment method on cart by customer
*/
class SetPurchaseOrderPaymentMethodOnCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
*/
public function testSetPurchaseOrderPaymentMethodOnCartWithSimpleProduct()
{
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$purchaseOrderNumber = '123456';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "$maskedQuoteId"
payment_method: {
code: "$methodCode"
purchase_order_number: "$purchaseOrderNumber"
}
}) {
cart {
selected_payment_method {
code
purchase_order_number
}
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
self::assertEquals(
$purchaseOrderNumber,
$response['setPaymentMethodOnCart']['cart']['selected_payment_method']['purchase_order_number']
);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoConfigFixture default_store payment/banktransfer/active 1
* @magentoConfigFixture default_store payment/cashondelivery/active 1
* @magentoConfigFixture default_store payment/checkmo/active 1
* @magentoConfigFixture default_store payment/purchaseorder/active 1
*
*/
public function testSetPurchaseOrderPaymentMethodOnCartWithoutPurchaseOrderNumber()
{
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "$maskedQuoteId"
payment_method: {
code: "$methodCode"
}
}) {
cart {
selected_payment_method {
code
}
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
self::assertEquals(
$methodCode,
$response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']
);
self::assertArrayNotHasKey(
'purchase_order_number',
$response['setPaymentMethodOnCart']['cart']['selected_payment_method']
);
}
/**
* @magentoConfigFixture default_store payment/purchaseorder/active 0
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*
*/
public function testSetDisabledPurchaseOrderPaymentMethodOnCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The requested Payment Method is not available.');
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$purchaseOrderNumber = '123456';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "$maskedQuoteId"
payment_method: {
code: "$methodCode"
purchase_order_number: "$purchaseOrderNumber"
}
}) {
cart {
selected_payment_method {
code
purchase_order_number
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,416 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Exception;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for setting shipping methods on cart for customer
*/
class SetShippingMethodsOnCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testSetShippingMethodOnCartWithSimpleProduct()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$carrierCode = 'flatrate';
$methodCode = 'flatrate';
$query = $this->getQuery(
$maskedQuoteId,
$methodCode,
$carrierCode
);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']);
self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
$shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
$availableShippingMethods = $shippingAddress['available_shipping_methods'];
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertArrayHasKey('available_shipping_methods', $shippingAddress);
self::assertCount(2, $availableShippingMethods);
self::assertEquals('freeshipping', $availableShippingMethods[0]['carrier_code']);
self::assertEquals($carrierCode, $availableShippingMethods[1]['carrier_code']);
self::assertEquals($availableShippingMethods[0]['amount']['value'], 0);
self::assertEquals($availableShippingMethods[1]['amount']['value'], 10);
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Flat Rate', $shippingAddress['selected_shipping_method']['carrier_title']);
self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
self::assertEquals('Fixed', $shippingAddress['selected_shipping_method']['method_title']);
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
$amount = $shippingAddress['selected_shipping_method']['amount'];
self::assertArrayHasKey('value', $amount);
self::assertEquals(10, $amount['value']);
self::assertArrayHasKey('currency', $amount);
self::assertEquals('USD', $amount['currency']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testReSetShippingMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$carrierCode = 'freeshipping';
$methodCode = 'freeshipping';
$query = $this->getQuery(
$maskedQuoteId,
$methodCode,
$carrierCode
);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']);
self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
$shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertEquals($carrierCode, $shippingAddress['selected_shipping_method']['carrier_code']);
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']);
}
/**
* @magentoConfigFixture default_store carriers/freeshipping/active 0
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*
* @param string $input
* @param string $message
* @dataProvider dataProviderSetShippingMethodWithWrongParameters
* @throws Exception
*/
public function testSetShippingMethodWithWrongParameters(string $input, string $message)
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$input = str_replace('cart_id_value', $maskedQuoteId, $input);
$query = <<<QUERY
mutation {
setShippingMethodsOnCart(input: {
{$input}
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
}
}
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function dataProviderSetShippingMethodWithWrongParameters(): array
{
return [
'shipping_methods_are_empty' => [
'cart_id: "cart_id_value" shipping_methods: []',
'Required parameter "shipping_methods" is missing'
],
'empty_carrier_code' => [
'cart_id: "cart_id_value", shipping_methods: [{
carrier_code: ""
method_code: "flatrate"
}]',
'Required parameter "carrier_code" is missing.'
],
'non_existent_carrier_code' => [
'cart_id: "cart_id_value", shipping_methods: [{
carrier_code: "wrong-carrier-code"
method_code: "flatrate"
}]',
'Carrier with such method not found: wrong-carrier-code, flatrate'
],
'empty_method_code' => [
'cart_id: "cart_id_value", shipping_methods: [{
carrier_code: "flatrate"
method_code: ""
}]',
'Required parameter "method_code" is missing.'
],
'non_existent_method_code' => [
'cart_id: "cart_id_value", shipping_methods: [{
carrier_code: "flatrate"
method_code: "wrong-carrier-code"
}]',
'Carrier with such method not found: flatrate, wrong-carrier-code'
],
'non_existent_shopping_cart' => [
'cart_id: "non_existent_masked_id", shipping_methods: [{
carrier_code: "flatrate"
method_code: "flatrate"
}]',
'Could not find a cart with ID "non_existent_masked_id"'
],
'disabled_shipping_method' => [
'cart_id: "cart_id_value", shipping_methods: [{
carrier_code: "freeshipping"
method_code: "freeshipping"
}]',
'Carrier with such method not found: freeshipping, freeshipping'
]
];
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoConfigFixture default_store carriers/flatrate/active 1
* @magentoConfigFixture default_store carriers/tablerate/active 1
* @magentoConfigFixture default_store carriers/freeshipping/active 1
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testSetMultipleShippingMethods()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('You cannot specify multiple shipping methods.');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = <<<QUERY
mutation {
setShippingMethodsOnCart(input: {
cart_id: "{$maskedQuoteId}",
shipping_methods: [
{
carrier_code: "flatrate"
method_code: "flatrate"
}
{
carrier_code: "flatrate"
method_code: "flatrate"
}
]
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
}
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*
*/
public function testSetShippingMethodToGuestCart()
{
$this->expectException(\Exception::class);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$carrierCode = 'flatrate';
$methodCode = 'flatrate';
$query = $this->getQuery(
$maskedQuoteId,
$methodCode,
$carrierCode
);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*
*/
public function testSetShippingMethodToAnotherCustomerCart()
{
$this->expectException(\Exception::class);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$carrierCode = 'flatrate';
$methodCode = 'flatrate';
$query = $this->getQuery(
$maskedQuoteId,
$methodCode,
$carrierCode
);
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
}
/**
* @param string $maskedQuoteId
* @param string $shippingMethodCode
* @param string $shippingCarrierCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $shippingMethodCode,
string $shippingCarrierCode
): string {
return <<<QUERY
mutation {
setShippingMethodsOnCart(input:
{
cart_id: "$maskedQuoteId",
shipping_methods: [{
carrier_code: "$shippingCarrierCode"
method_code: "$shippingMethodCode"
}]
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
carrier_title
method_title
amount {
value
currency
}
}
available_shipping_methods {
amount{
value
}
carrier_code
}
}
}
}
}
QUERY;
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*
*/
public function testSetShippingMethodOnAnEmptyCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'The shipping method can\'t be set for an empty cart. Add an item to cart and try again.'
);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$carrierCode = 'flatrate';
$methodCode = 'flatrate';
$query = $this->getQuery(
$maskedQuoteId,
$methodCode,
$carrierCode
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
@@ -0,0 +1,298 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Customer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for updating shopping cart items
*/
class UpdateCartItemsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var QuoteResource
*/
private $quoteResource;
/**
* @var QuoteFactory
*/
private $quoteFactory;
/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*/
public function testUpdateCartItemQuantity()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
$itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId();
$quantity = 2;
$query = $this->getQuery($maskedQuoteId, $itemId, $quantity);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('updateCartItems', $response);
$this->assertArrayHasKey('cart', $response['updateCartItems']);
$responseCart = $response['updateCartItems']['cart'];
$item = current($responseCart['items']);
$this->assertEquals($itemId, $item['id']);
$this->assertEquals($quantity, $item['quantity']);
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*/
public function testRemoveCartItemIfQuantityIsZero()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
$itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId();
$quantity = 0;
$query = $this->getQuery($maskedQuoteId, $itemId, $quantity);
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey('updateCartItems', $response);
$this->assertArrayHasKey('cart', $response['updateCartItems']);
$responseCart = $response['updateCartItems']['cart'];
$this->assertCount(0, $responseCart['items']);
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testUpdateItemInNonExistentCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
$query = $this->getQuery('non_existent_masked_id', 1, 2);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*/
public function testUpdateNonExistentItem()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
$notExistentItemId = 999;
$this->expectExceptionMessage("Could not find cart item with id: {$notExistentItemId}.");
$query = $this->getQuery($maskedQuoteId, $notExistentItemId, 2);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
*/
public function testUpdateItemIfItemIsNotBelongToCart()
{
$firstQuote = $this->quoteFactory->create();
$this->quoteResource->load($firstQuote, 'test_order_1', 'reserved_order_id');
$firstQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$firstQuote->getId());
$secondQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$secondQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$secondQuote->setCustomerId(1);
$this->quoteResource->save($secondQuote);
$secondQuoteItemId = (int)$secondQuote
->getItemByProduct($this->productRepository->get('virtual-product'))
->getId();
$this->expectExceptionMessage("Could not find cart item with id: {$secondQuoteItemId}.");
$query = $this->getQuery($firstQuoteMaskedId, $secondQuoteItemId, 2);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
*/
public function testUpdateItemInGuestCart()
{
$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$guestQuoteItemId = (int)$guestQuote
->getItemByProduct($this->productRepository->get('virtual-product'))
->getId();
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$guestQuoteMaskedId\""
);
$query = $this->getQuery($guestQuoteMaskedId, $guestQuoteItemId, 2);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
*/
public function testUpdateItemInAnotherCustomerCart()
{
$anotherCustomerQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$anotherCustomerQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$anotherCustomerQuote->setCustomerId(2);
$this->quoteResource->save($anotherCustomerQuote);
$anotherCustomerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$anotherCustomerQuote->getId());
$anotherCustomerQuoteItemId = (int)$anotherCustomerQuote
->getItemByProduct($this->productRepository->get('virtual-product'))
->getId();
$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$anotherCustomerQuoteMaskedId\""
);
$query = $this->getQuery($anotherCustomerQuoteMaskedId, $anotherCustomerQuoteItemId, 2);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @param string $input
* @param string $message
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*/
public function testUpdateWithMissedItemRequiredParameters(string $input, string $message)
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
$query = <<<QUERY
mutation {
updateCartItems(input: {
cart_id: "{$maskedQuoteId}"
{$input}
}) {
cart {
items {
id
quantity
}
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}
/**
* @return array
*/
public function dataProviderUpdateWithMissedRequiredParameters(): array
{
return [
'missed_cart_item_qty' => [
'cart_items: [{ cart_item_id: 1 }]',
'Required parameter "quantity" for "cart_items" is missing.'
],
];
}
/**
* @param string $maskedQuoteId
* @param int $itemId
* @param float $quantity
* @return string
*/
private function getQuery(string $maskedQuoteId, int $itemId, float $quantity): string
{
return <<<QUERY
mutation {
updateCartItems(input: {
cart_id: "{$maskedQuoteId}"
cart_items:[
{
cart_item_id: {$itemId}
quantity: {$quantity}
}
]
}) {
cart {
items {
id
quantity
}
}
}
}
QUERY;
}
/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}