|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\QuoteGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 11 | +use Magento\Customer\Test\Fixture\Customer; |
| 12 | +use Magento\Indexer\Test\Fixture\Indexer; |
| 13 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 14 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 15 | +use Magento\Quote\Test\Fixture\CustomerCart; |
| 16 | +use Magento\Quote\Test\Fixture\QuoteIdMask; |
| 17 | +use Magento\TestFramework\Fixture\DataFixture; |
| 18 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; |
| 21 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 22 | +use Magento\Translation\Test\Fixture\Translation; |
| 23 | + |
| 24 | +class PlaceOrderTest extends GraphQlAbstract |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var CustomerTokenServiceInterface |
| 28 | + */ |
| 29 | + private CustomerTokenServiceInterface $customerTokenService; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritdoc |
| 33 | + */ |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + $objectManager = Bootstrap::getObjectManager(); |
| 37 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 38 | + |
| 39 | + parent::setUp(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test translated error message in non default store |
| 44 | + * |
| 45 | + * @magentoApiDataFixture Magento/Store/_files/second_store.php |
| 46 | + * @magentoConfigFixture fixture_second_store_store general/locale/code nl_NL |
| 47 | + */ |
| 48 | + #[ |
| 49 | + DataFixture( |
| 50 | + Translation::class, |
| 51 | + [ |
| 52 | + 'string' => 'Unable to place order: %message', |
| 53 | + 'translate' => 'Kan geen bestelling plaatsen: %message', |
| 54 | + 'locale' => 'nl_NL', |
| 55 | + ] |
| 56 | + ), |
| 57 | + DataFixture( |
| 58 | + Translation::class, |
| 59 | + [ |
| 60 | + 'string' => 'Some addresses can\'t be used due to the configurations for specific countries.', |
| 61 | + 'translate' => 'Sommige adressen kunnen niet worden ' . |
| 62 | + 'gebruikt vanwege de configuraties van specifieke landen.', |
| 63 | + 'locale' => 'nl_NL', |
| 64 | + ] |
| 65 | + ), |
| 66 | + DataFixture(ProductFixture::class, as: 'product'), |
| 67 | + DataFixture(Indexer::class, as: 'indexer'), |
| 68 | + DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'), |
| 69 | + DataFixture( |
| 70 | + CustomerCart::class, |
| 71 | + [ |
| 72 | + 'customer_id' => '$customer.id$', |
| 73 | + 'reserved_order_id' => 'test_quote' |
| 74 | + ], |
| 75 | + 'cart' |
| 76 | + ), |
| 77 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']), |
| 78 | + DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'), |
| 79 | + ] |
| 80 | + public function testPlaceOrderErrorTranslation() |
| 81 | + { |
| 82 | + $storeCode = "fixture_second_store"; |
| 83 | + $maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId(); |
| 84 | + $query = $this->placeOrderQuery($maskedQuoteId); |
| 85 | + try { |
| 86 | + $headers = ['Store' => $storeCode]; |
| 87 | + $this->graphQlMutation($query, [], '', array_merge($headers, $this->getHeaderMap())); |
| 88 | + } catch (ResponseContainsErrorsException $exception) { |
| 89 | + $exceptionData = $exception->getResponseData(); |
| 90 | + self::assertEquals(1, count($exceptionData['errors'])); |
| 91 | + self::assertStringContainsString('Kan geen bestelling plaatsen:', $exceptionData['errors'][0]['message']); |
| 92 | + self::assertStringContainsString( |
| 93 | + 'Sommige adressen kunnen niet worden', |
| 94 | + $exceptionData['errors'][0]['message'] |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get place order mutation |
| 101 | + * |
| 102 | + * @param string $maskedQuoteId |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + private function placeOrderQuery(string $maskedQuoteId): string |
| 106 | + { |
| 107 | + return <<<QUERY |
| 108 | +mutation { |
| 109 | + placeOrder(input: {cart_id: "{$maskedQuoteId}"}) { |
| 110 | + order { |
| 111 | + order_number |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +QUERY; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get bearer authorization header |
| 120 | + * |
| 121 | + * @param string $username |
| 122 | + * @param string $password |
| 123 | + * @return array |
| 124 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 125 | + */ |
| 126 | + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array |
| 127 | + { |
| 128 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 129 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 130 | + return $headerMap; |
| 131 | + } |
| 132 | +} |
0 commit comments