|
| 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 Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test for getting selected payment method from cart |
| 17 | + */ |
| 18 | +class GetSelectedPaymentMethodTest extends GraphQlAbstract |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 22 | + */ |
| 23 | + private $getMaskedQuoteIdByReservedOrderId; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var CustomerTokenServiceInterface |
| 27 | + */ |
| 28 | + private $customerTokenService; |
| 29 | + |
| 30 | + /** |
| 31 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 32 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 33 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php |
| 34 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 35 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 36 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 37 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php |
| 38 | + */ |
| 39 | + public function testGetSelectedPaymentMethod() |
| 40 | + { |
| 41 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 42 | + |
| 43 | + $query = $this->getQuery($maskedQuoteId); |
| 44 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 45 | + |
| 46 | + $this->assertArrayHasKey('cart', $response); |
| 47 | + $this->assertArrayHasKey('selected_payment_method', $response['cart']); |
| 48 | + $this->assertArrayHasKey('code', $response['cart']['selected_payment_method']); |
| 49 | + $this->assertEquals('checkmo', $response['cart']['selected_payment_method']['code']); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 54 | + * @expectedException \Exception |
| 55 | + */ |
| 56 | + public function testGetSelectedPaymentMethodFromNonExistentCart() |
| 57 | + { |
| 58 | + $maskedQuoteId = 'non_existent_masked_id'; |
| 59 | + $query = $this->getQuery($maskedQuoteId); |
| 60 | + |
| 61 | + $this->expectExceptionMessage( |
| 62 | + 'Could not find a cart with ID "non_existent_masked_id"' |
| 63 | + ); |
| 64 | + |
| 65 | + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * _security |
| 70 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 71 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 72 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 73 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php |
| 74 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 75 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 76 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php |
| 77 | + */ |
| 78 | + public function testGetSelectedPaymentMethodFromGuestCart() |
| 79 | + { |
| 80 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 81 | + $query = $this->getQuery($maskedQuoteId); |
| 82 | + |
| 83 | + $this->expectExceptionMessage( |
| 84 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 85 | + ); |
| 86 | + |
| 87 | + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * _security |
| 92 | + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php |
| 93 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 94 | + */ |
| 95 | + public function testGetSelectedPaymentMethodFromAnotherCustomerCart() |
| 96 | + { |
| 97 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 98 | + $query = $this->getQuery($maskedQuoteId); |
| 99 | + |
| 100 | + $this->expectExceptionMessage( |
| 101 | + "The current user cannot perform operations on cart \"{$maskedQuoteId}\"" |
| 102 | + ); |
| 103 | + |
| 104 | + $this-> graphQlQuery( $query, [], '', $this-> getHeaderMap( '[email protected]')); |
| 105 | + } |
| 106 | + |
| 107 | + protected function setUp() |
| 108 | + { |
| 109 | + $objectManager = Bootstrap::getObjectManager(); |
| 110 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 111 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param string $maskedQuoteId |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + private function getQuery(string $maskedQuoteId): string |
| 119 | + { |
| 120 | + return <<<QUERY |
| 121 | +{ |
| 122 | + cart(cart_id:"$maskedQuoteId") { |
| 123 | + selected_payment_method { |
| 124 | + code |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +QUERY; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param string $username |
| 133 | + * @param string $password |
| 134 | + * @return array |
| 135 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 136 | + */ |
| 137 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 138 | + { |
| 139 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 140 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 141 | + return $headerMap; |
| 142 | + } |
| 143 | +} |
0 commit comments