|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Catalog; |
| 9 | + |
| 10 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 11 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 12 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 13 | +use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 14 | +use Magento\TestFramework\Fixture\DataFixture; |
| 15 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 16 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 19 | + |
| 20 | +class ProductNameWithSpecialCharactersTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 24 | + */ |
| 25 | + private $quoteIdToMaskedQuoteId; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var DataFixtureStorage |
| 29 | + */ |
| 30 | + private $fixtures; |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritdoc |
| 34 | + */ |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->quoteIdToMaskedQuoteId = Bootstrap::getObjectManager()->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 38 | + $this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Test product name with special characters |
| 43 | + * |
| 44 | + * @param string $sku |
| 45 | + * @param string $expectedName |
| 46 | + * @throws NoSuchEntityException |
| 47 | + * @dataProvider productNameProvider |
| 48 | + */ |
| 49 | + #[ |
| 50 | + DataFixture(ProductFixture::class, [ |
| 51 | + 'sku' => 'test-product-1', |
| 52 | + 'name' => 'Test Product© 1' |
| 53 | + ]), |
| 54 | + DataFixture(ProductFixture::class, [ |
| 55 | + 'sku' => 'test-product-2', |
| 56 | + 'name' => 'Test Product™ 2' |
| 57 | + ]), |
| 58 | + DataFixture(ProductFixture::class, [ |
| 59 | + 'sku' => 'test-product-3', |
| 60 | + 'name' => 'Sample Product© 3' |
| 61 | + ]), |
| 62 | + DataFixture(ProductFixture::class, [ |
| 63 | + 'sku' => 'test-product-4', |
| 64 | + 'name' => 'Sample Product™ 4' |
| 65 | + ]), |
| 66 | + DataFixture(ProductFixture::class, [ |
| 67 | + 'sku' => 'test-product-5', |
| 68 | + 'name' => 'Test Product 5' |
| 69 | + ]), |
| 70 | + DataFixture(GuestCartFixture::class, as: 'cart') |
| 71 | + ] |
| 72 | + public function testProductName(string $sku, string $expectedName): void |
| 73 | + { |
| 74 | + $cart = $this->fixtures->get('cart'); |
| 75 | + $maskedQuoteId = $this->quoteIdToMaskedQuoteId->execute((int) $cart->getId()); |
| 76 | + |
| 77 | + $query = $this->getAddToCartMutation($maskedQuoteId, $sku); |
| 78 | + $response = $this->graphQlMutation($query); |
| 79 | + $result = $response['addProductsToCart']; |
| 80 | + |
| 81 | + self::assertCount(1, $result['cart']['items']); |
| 82 | + self::assertEquals(1, $result['cart']['items'][0]['quantity']); |
| 83 | + self::assertEquals($expectedName, $result['cart']['items'][0]['product']['name']); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Data provider for product name test cases |
| 88 | + * |
| 89 | + * @return array[] |
| 90 | + */ |
| 91 | + public static function productNameProvider(): array |
| 92 | + { |
| 93 | + return [ |
| 94 | + ['test-product-1', 'Test Product© 1'], |
| 95 | + ['test-product-2', 'Test Product™ 2'], |
| 96 | + ['test-product-3', 'Sample Product© 3'], |
| 97 | + ['test-product-4', 'Sample Product™ 4'], |
| 98 | + ['test-product-5', 'Test Product 5'] |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns GraphQl mutation |
| 104 | + * |
| 105 | + * @param string $maskedQuoteId |
| 106 | + * @param string $sku |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + private function getAddToCartMutation(string $maskedQuoteId, string $sku): string |
| 110 | + { |
| 111 | + return <<<MUTATION |
| 112 | +mutation { |
| 113 | + addProductsToCart( |
| 114 | + cartId: "{$maskedQuoteId}", |
| 115 | + cartItems: [ |
| 116 | + { |
| 117 | + sku: "{$sku}" |
| 118 | + quantity: 1 |
| 119 | + } |
| 120 | + ] |
| 121 | + ) { |
| 122 | + cart { |
| 123 | + id |
| 124 | + items { |
| 125 | + uid |
| 126 | + quantity |
| 127 | + product { |
| 128 | + sku |
| 129 | + name |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +MUTATION; |
| 136 | + } |
| 137 | +} |
0 commit comments