|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote\Customer; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 12 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 13 | +use Magento\OfflinePayments\Model\Purchaseorder; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for setting Purchase Order payment method on cart by customer |
| 19 | + */ |
| 20 | +class SetPurchaseOrderPaymentMethodOnCartTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 24 | + */ |
| 25 | + private $getMaskedQuoteIdByReservedOrderId; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var CustomerTokenServiceInterface |
| 29 | + */ |
| 30 | + private $customerTokenService; |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritdoc |
| 34 | + */ |
| 35 | + protected function setUp() |
| 36 | + { |
| 37 | + $objectManager = Bootstrap::getObjectManager(); |
| 38 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 39 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 44 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 45 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 46 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 47 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 48 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php |
| 49 | + */ |
| 50 | + public function testSetPurchaseOrderPaymentMethodOnCartWithSimpleProduct() |
| 51 | + { |
| 52 | + $methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE; |
| 53 | + $purchaseOrderNumber = '123456'; |
| 54 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 55 | + |
| 56 | + $query = <<<QUERY |
| 57 | +mutation { |
| 58 | + setPaymentMethodOnCart(input: { |
| 59 | + cart_id: "$maskedQuoteId" |
| 60 | + payment_method: { |
| 61 | + code: "$methodCode" |
| 62 | + purchase_order_number: "$purchaseOrderNumber" |
| 63 | + } |
| 64 | + }) { |
| 65 | + cart { |
| 66 | + selected_payment_method { |
| 67 | + code |
| 68 | + purchase_order_number |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +QUERY; |
| 74 | + $response = $this->graphQlMutation($query, [], '', $this->getHeaderMap()); |
| 75 | + |
| 76 | + self::assertArrayHasKey('setPaymentMethodOnCart', $response); |
| 77 | + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); |
| 78 | + self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); |
| 79 | + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); |
| 80 | + self::assertEquals( |
| 81 | + $purchaseOrderNumber, |
| 82 | + $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['purchase_order_number'] |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 88 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 89 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 90 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 91 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 92 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php |
| 93 | + * |
| 94 | + * @expectedException Exception |
| 95 | + * @expectedExceptionMessage Purchase order number is a required field. |
| 96 | + */ |
| 97 | + public function testSetPurchaseOrderPaymentMethodOnCartWithoutPurchaseOrderNumber() |
| 98 | + { |
| 99 | + $methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE; |
| 100 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 101 | + |
| 102 | + $query = <<<QUERY |
| 103 | +mutation { |
| 104 | + setPaymentMethodOnCart(input: { |
| 105 | + cart_id: "$maskedQuoteId" |
| 106 | + payment_method: { |
| 107 | + code: "$methodCode" |
| 108 | + } |
| 109 | + }) { |
| 110 | + cart { |
| 111 | + selected_payment_method { |
| 112 | + code |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +QUERY; |
| 118 | + $this->graphQlMutation($query, [], '', $this->getHeaderMap()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 123 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 124 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 125 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 126 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 127 | + * |
| 128 | + * @expectedException Exception |
| 129 | + * @expectedExceptionMessage The requested Payment Method is not available. |
| 130 | + */ |
| 131 | + public function testSetDisabledPurchaseOrderPaymentMethodOnCart() |
| 132 | + { |
| 133 | + $methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE; |
| 134 | + $purchaseOrderNumber = '123456'; |
| 135 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 136 | + |
| 137 | + $query = <<<QUERY |
| 138 | +mutation { |
| 139 | + setPaymentMethodOnCart(input: { |
| 140 | + cart_id: "$maskedQuoteId" |
| 141 | + payment_method: { |
| 142 | + code: "$methodCode" |
| 143 | + purchase_order_number: "$purchaseOrderNumber" |
| 144 | + } |
| 145 | + }) { |
| 146 | + cart { |
| 147 | + selected_payment_method { |
| 148 | + code |
| 149 | + purchase_order_number |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | +QUERY; |
| 155 | + $this->graphQlMutation($query, [], '', $this->getHeaderMap()); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param string $username |
| 160 | + * @param string $password |
| 161 | + * @return array |
| 162 | + */ |
| 163 | + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array |
| 164 | + { |
| 165 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 166 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 167 | + return $headerMap; |
| 168 | + } |
| 169 | +} |
0 commit comments