Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Offline Payment Method Tests #632

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

return [
'code' => $payment->getMethod(),
'title' => $payment->getMethodInstance()->getTitle(),
'purchase_order_number' => $payment->getPoNumber(),
];
}
Expand Down
7 changes: 4 additions & 3 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,13 @@ type AvailableShippingMethod {
}

type AvailablePaymentMethod {
code: String @doc(description: "The payment method code")
title: String @doc(description: "The payment method title.")
code: String! @doc(description: "The payment method code")
title: String! @doc(description: "The payment method title.")
}

type SelectedPaymentMethod {
code: String @doc(description: "The payment method code")
code: String! @doc(description: "The payment method code")
title: String! @doc(description: "The payment method title.")
additional_data: SelectedPaymentMethodAdditionalData @doc(description: "Additional payment data")
purchase_order_number: String @doc(description: "The purchase order number.")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?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()
{
$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/GraphQl/Quote/_files/enable_offline_payment_methods.php
*
* @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
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Guest;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
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;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
* @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/enable_offline_payment_methods.php
*
* @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);

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/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/enable_offline_payment_methods.php
*/
public function testSetPurchaseOrderPaymentMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
$methodTitle = 'Purchase Order';
$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);

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;
}
}