|
| 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\PaypalGraphQl\Model\Resolver\Guest; |
| 9 | + |
| 10 | +use Magento\Framework\App\Request\Http; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 12 | +use Magento\Framework\Serialize\SerializerInterface; |
| 13 | +use Magento\GraphQl\Controller\GraphQl; |
| 14 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 15 | +use Magento\Paypal\Model\Payflow\Request; |
| 16 | +use Magento\Paypal\Model\Payflow\Service\Gateway; |
| 17 | +use Magento\Quote\Model\Quote; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\Framework\UrlInterface; |
| 20 | +use Magento\TestFramework\ObjectManager; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * End to end place order test using payflow_link via graphql endpoint for guest |
| 26 | + * |
| 27 | + * @magentoAppArea graphql |
| 28 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 29 | + */ |
| 30 | +class SetPaymentMethodAsPayflowLinkTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var Http |
| 34 | + */ |
| 35 | + private $request; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var SerializerInterface |
| 39 | + */ |
| 40 | + private $json; |
| 41 | + |
| 42 | + /** @var GetMaskedQuoteIdByReservedOrderId */ |
| 43 | + private $getMaskedQuoteIdByReservedOrderId; |
| 44 | + |
| 45 | + /** @var ObjectManager */ |
| 46 | + protected $objectManager; |
| 47 | + |
| 48 | + /** @var GraphQl */ |
| 49 | + protected $graphqlController; |
| 50 | + |
| 51 | + /** @var Gateway|MockObject */ |
| 52 | + private $gateway; |
| 53 | + |
| 54 | + /** @var Request|MockObject */ |
| 55 | + private $payflowRequest; |
| 56 | + |
| 57 | + protected function setUp() |
| 58 | + { |
| 59 | + parent::setUp(); |
| 60 | + |
| 61 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 62 | + $this->request = $this->objectManager->create(Http::class); |
| 63 | + $this->json = $this->objectManager->get(SerializerInterface::class); |
| 64 | + $this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 65 | + $this->graphqlController = $this->objectManager->get(GraphQl::class); |
| 66 | + $this->gateway = $this->getMockBuilder(Gateway::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->setMethods(['postRequest']) |
| 69 | + ->getMock(); |
| 70 | + |
| 71 | + $requestFactory = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\RequestFactory::class) |
| 72 | + ->setMethods(['create']) |
| 73 | + ->disableOriginalConstructor() |
| 74 | + ->getMock(); |
| 75 | + |
| 76 | + $this->payflowRequest = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Request::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->getMock(); |
| 79 | + $requestFactory->expects($this->any())->method('create')->will($this->returnValue($this->payflowRequest)); |
| 80 | + $this->objectManager->addSharedInstance($this->gateway, Gateway::class); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test SetPayment method for payflow_link and validate that the additional information is set on the quote |
| 85 | + * |
| 86 | + * @magentoConfigFixture default_store payment/payflow_link/active 1 |
| 87 | + * @magentoConfigFixture default_store payment/payflow_link/sandbox_flag 1 |
| 88 | + * @magentoDataFixture Magento/Sales/_files/default_rollback.php |
| 89 | + * @magentoDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 90 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 91 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 92 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php |
| 93 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 94 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 95 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php |
| 96 | + * @return void |
| 97 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 98 | + */ |
| 99 | + public function testSetPayflowLinkAsPaymentMethod(): void |
| 100 | + { |
| 101 | + $paymentMethod = 'payflow_link'; |
| 102 | + $cartId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 103 | + |
| 104 | + $url = $this->objectManager->get(UrlInterface::class); |
| 105 | + $baseUrl = $url->getBaseUrl(); |
| 106 | + |
| 107 | + $query |
| 108 | + = <<<QUERY |
| 109 | + mutation { |
| 110 | + setPaymentMethodOnCart(input: { |
| 111 | + cart_id: "$cartId" |
| 112 | + payment_method: { |
| 113 | + code: "$paymentMethod" |
| 114 | + additional_data: { |
| 115 | + payflow_link: |
| 116 | + { |
| 117 | + return_url:"{$baseUrl}paypal/payflow/returnUrl" |
| 118 | + cancel_url:"{$baseUrl}paypal/payflow/cancelPayment" |
| 119 | + error_url:"{$baseUrl}paypal/payflow/errorUrl" |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + }) { |
| 124 | + cart { |
| 125 | + selected_payment_method { |
| 126 | + code |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +QUERY; |
| 132 | + |
| 133 | + $postData = $this->json->serialize(['query' => $query]); |
| 134 | + $this->request->setPathInfo('/graphql'); |
| 135 | + $this->request->setMethod('POST'); |
| 136 | + $this->request->setContent($postData); |
| 137 | + $headers = $this->objectManager->create(\Zend\Http\Headers::class) |
| 138 | + ->addHeaders(['Content-Type' => 'application/json']); |
| 139 | + $this->request->setHeaders($headers); |
| 140 | + |
| 141 | + $response = $this->graphqlController->dispatch($this->request); |
| 142 | + $responseData = $this->json->unserialize($response->getContent()); |
| 143 | + |
| 144 | + $this->assertArrayNotHasKey('errors', $responseData); |
| 145 | + $this->assertArrayHasKey('data', $responseData); |
| 146 | + |
| 147 | + $this->assertEquals( |
| 148 | + $paymentMethod, |
| 149 | + $responseData['data']['setPaymentMethodOnCart']['cart']['selected_payment_method']['code'] |
| 150 | + ); |
| 151 | + /** @var Quote $quote */ |
| 152 | + $quote = $this->objectManager->get(Quote::class); |
| 153 | + |
| 154 | + $quote->load('test_quote', 'reserved_order_id'); |
| 155 | + $payment = $quote->getPayment(); |
| 156 | + $actualPaymentAdditionalInformation = $payment->getAdditionalInformation('payflow_link'); |
| 157 | + $this->assertEquals("{$baseUrl}paypal/payflow/cancelPayment", $payment->getAdditionalInformation('cancel_url')); |
| 158 | + $this->assertEquals("{$baseUrl}paypal/payflow/returnUrl", $payment->getAdditionalInformation('return_url')); |
| 159 | + $this->assertEquals("{$baseUrl}paypal/payflow/errorUrl", $payment->getAdditionalInformation('error_url')); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Test invalid redirect url |
| 164 | + * |
| 165 | + * @magentoConfigFixture default_store payment/payflow_link/active 1 |
| 166 | + * @magentoConfigFixture default_store payment/payflow_link/sandbox_flag 1 |
| 167 | + * @magentoDataFixture Magento/Sales/_files/default_rollback.php |
| 168 | + * @magentoDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 169 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 170 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 171 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php |
| 172 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 173 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 174 | + * @magentoDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php |
| 175 | + * @return void |
| 176 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 177 | + */ |
| 178 | + public function testInvalidUrl(): void |
| 179 | + { |
| 180 | + $paymentMethod = 'payflow_link'; |
| 181 | + $cartId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 182 | + |
| 183 | + $url = $this->objectManager->get(UrlInterface::class); |
| 184 | + $baseUrl = $url->getBaseUrl(); |
| 185 | + |
| 186 | + $query |
| 187 | + = <<<QUERY |
| 188 | + mutation { |
| 189 | + setPaymentMethodOnCart(input: { |
| 190 | + cart_id: "$cartId" |
| 191 | + payment_method: { |
| 192 | + code: "$paymentMethod" |
| 193 | + additional_data: { |
| 194 | + payflow_link: |
| 195 | + { |
| 196 | + return_url:"{$baseUrl}paypal/payflow/returnUrl" |
| 197 | + cancel_url:"{$baseUrl}paypal/payflow/cancelPayment" |
| 198 | + error_url:"/not/a/validUrl" |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + }) { |
| 203 | + cart { |
| 204 | + selected_payment_method { |
| 205 | + code |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
| 210 | +QUERY; |
| 211 | + |
| 212 | + $postData = $this->json->serialize(['query' => $query]); |
| 213 | + $this->request->setPathInfo('/graphql'); |
| 214 | + $this->request->setMethod('POST'); |
| 215 | + $this->request->setContent($postData); |
| 216 | + $headers = $this->objectManager->create(\Zend\Http\Headers::class) |
| 217 | + ->addHeaders(['Content-Type' => 'application/json']); |
| 218 | + $this->request->setHeaders($headers); |
| 219 | + |
| 220 | + $expectedExceptionMessage = "Invalid URL '/not/a/validUrl'."; |
| 221 | + |
| 222 | + $response = $this->graphqlController->dispatch($this->request); |
| 223 | + $responseData = $this->json->unserialize($response->getContent()); |
| 224 | + $this->assertArrayHasKey('errors', $responseData); |
| 225 | + $actualError = $responseData['errors'][0]; |
| 226 | + $this->assertEquals($expectedExceptionMessage, $actualError['message']); |
| 227 | + $this->assertEquals(GraphQlInputException::EXCEPTION_CATEGORY, $actualError['category']); |
| 228 | + } |
| 229 | +} |
0 commit comments