|
| 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\Catalog; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\TestFramework\ObjectManager; |
| 12 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 13 | +use Magento\UrlRewrite\Model\UrlFinderInterface; |
| 14 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite as UrlRewriteDTO; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test of getting child products info of configurable product on category request |
| 18 | + */ |
| 19 | +class UrlRewritesTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** |
| 22 | + * |
| 23 | + * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php |
| 24 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 25 | + */ |
| 26 | + public function testProductWithNoCategoriesAssigned() |
| 27 | + { |
| 28 | + |
| 29 | + $query |
| 30 | + = <<<QUERY |
| 31 | +{ |
| 32 | + products (search:"Virtual Product") { |
| 33 | + items { |
| 34 | + name, |
| 35 | + sku, |
| 36 | + description, |
| 37 | + url_rewrites { |
| 38 | + url, |
| 39 | + parameters { |
| 40 | + name, |
| 41 | + value |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +QUERY; |
| 48 | + |
| 49 | + $response = $this->graphQlQuery($query); |
| 50 | + |
| 51 | + /** @var ProductRepositoryInterface $productRepository */ |
| 52 | + $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class); |
| 53 | + $product = $productRepository->get('virtual-product', false, null, true); |
| 54 | + |
| 55 | + $urlFinder = ObjectManager::getInstance()->get(UrlFinderInterface::class); |
| 56 | + |
| 57 | + $rewritesCollection = $urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $product->getId()]); |
| 58 | + |
| 59 | + /* There should be only one rewrite */ |
| 60 | + /** @var UrlRewriteDTO $urlRewrite */ |
| 61 | + $urlRewrite = current($rewritesCollection); |
| 62 | + |
| 63 | + $this->assertArrayHasKey('url_rewrites', $response['products']['items'][0]); |
| 64 | + $this->assertCount(1, $response['products']['items'][0]['url_rewrites']); |
| 65 | + |
| 66 | + $this->assertResponseFields( |
| 67 | + $response['products']['items'][0]['url_rewrites'][0], |
| 68 | + [ |
| 69 | + "url" => $urlRewrite->getRequestPath(), |
| 70 | + "parameters" => $this->getUrlParameters($urlRewrite->getTargetPath()) |
| 71 | + ] |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parses target path and extracts parameters |
| 77 | + * |
| 78 | + * @param string $targetPath |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + private function getUrlParameters(string $targetPath): array |
| 82 | + { |
| 83 | + $urlParameters = []; |
| 84 | + $targetPathParts = explode('/', trim($targetPath, '/')); |
| 85 | + |
| 86 | + for ($i = 3; ($i < sizeof($targetPathParts) - 1); $i += 2) { |
| 87 | + $urlParameters[] = [ |
| 88 | + 'name' => $targetPathParts[$i], |
| 89 | + 'value' => $targetPathParts[$i + 1] |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + return $urlParameters; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param array $actualResponse |
| 98 | + * @param array $assertionMap |
| 99 | + */ |
| 100 | + private function assertResponseFields($actualResponse, $assertionMap) |
| 101 | + { |
| 102 | + foreach ($assertionMap as $key => $assertionData) { |
| 103 | + $expectedValue = isset($assertionData['expected_value']) |
| 104 | + ? $assertionData['expected_value'] |
| 105 | + : $assertionData; |
| 106 | + $responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key; |
| 107 | + self::assertNotNull( |
| 108 | + $expectedValue, |
| 109 | + "Value of '{$responseField}' field must not be NULL" |
| 110 | + ); |
| 111 | + self::assertEquals( |
| 112 | + $expectedValue, |
| 113 | + $actualResponse[$responseField], |
| 114 | + "Value of '{$responseField}' field in response does not match expected value: " |
| 115 | + . var_export($expectedValue, true) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments