|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\GraphQl\UrlRewrite; |
| 7 | + |
| 8 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 9 | +use Magento\Catalog\Model\Product; |
| 10 | +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
| 11 | +use Magento\Store\Model\StoreManagerInterface; |
| 12 | +use Magento\TestFramework\ObjectManager; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | +use Magento\UrlRewrite\Model\UrlFinderInterface; |
| 15 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
| 16 | + |
| 17 | +class UrlResolverTest extends GraphQlAbstract |
| 18 | +{ |
| 19 | + |
| 20 | + /** @var ObjectManager */ |
| 21 | + private $objectManager; |
| 22 | + /** |
| 23 | + * @var StoreManagerInterface |
| 24 | + */ |
| 25 | + // private $storeManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator |
| 29 | + */ |
| 30 | + private $urlPathGenerator; |
| 31 | + |
| 32 | + protected function setUp() |
| 33 | + { |
| 34 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php |
| 39 | + */ |
| 40 | + public function testProductUrlResolver() |
| 41 | + { |
| 42 | + $productSku = 'p002'; |
| 43 | + $urlPath = 'p002.html'; |
| 44 | + /** @var ProductRepositoryInterface $productRepository */ |
| 45 | + $productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 46 | + $product = $productRepository->get($productSku, false, null, true); |
| 47 | + $storeId = $product->getStoreId(); |
| 48 | + $product->getUrlKey(); |
| 49 | + |
| 50 | + /** @var UrlRewrite $productUrlRewrite */ |
| 51 | + // $productUrlRewrite = $this->objectManager->get(UrlRewrite::class); |
| 52 | + /** @var ProductUrlRewriteGenerator $generator */ |
| 53 | + // $generator = $this->objectManager->get(ProductUrlRewriteGenerator::class); |
| 54 | + |
| 55 | + /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $urls */ |
| 56 | + // $urls = $generator->generate($product); |
| 57 | + |
| 58 | + /** @var UrlFinderInterface $urlFinder */ |
| 59 | + $urlFinder = $this->objectManager->get(UrlFinderInterface::class); |
| 60 | + $actualUrls = $urlFinder->findOneByData( |
| 61 | + [ |
| 62 | + 'request_path' =>$urlPath, |
| 63 | + 'store_id' => $storeId |
| 64 | + ] |
| 65 | + ); |
| 66 | + $targetPath = $actualUrls->getTargetPath(); |
| 67 | + $expectedType = $actualUrls->getEntityType(); |
| 68 | + $query |
| 69 | + = <<<QUERY |
| 70 | +{ |
| 71 | + urlResolver(url:"{$urlPath}") |
| 72 | + { |
| 73 | + id |
| 74 | + canonical_url |
| 75 | + type |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | +QUERY; |
| 80 | + $response = $this->graphQlQuery($query); |
| 81 | + $this->assertArrayHasKey('urlResolver', $response); |
| 82 | + $this->assertEquals($product->getEntityId(), $response['urlResolver']['id']); |
| 83 | + $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']); |
| 84 | + $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php |
| 89 | + */ |
| 90 | + public function testCategoryUrlResolver() |
| 91 | + { |
| 92 | + $productSku = 'p002'; |
| 93 | + $urlPath2 = 'cat-1.html'; |
| 94 | + /** @var ProductRepositoryInterface $productRepository */ |
| 95 | + $productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 96 | + $product = $productRepository->get($productSku, false, null, true); |
| 97 | + $storeId = $product->getStoreId(); |
| 98 | + |
| 99 | + /** @var UrlRewrite $productUrlRewrite */ |
| 100 | + // $productUrlRewrite = $this->objectManager->get(UrlRewrite::class); |
| 101 | + /** @var ProductUrlRewriteGenerator $generator */ |
| 102 | + // $generator = $this->objectManager->get(ProductUrlRewriteGenerator::class); |
| 103 | + |
| 104 | + /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $urls */ |
| 105 | + // $urls = $generator->generate($product); |
| 106 | + |
| 107 | + /** @var UrlFinderInterface $urlFinder */ |
| 108 | + $urlFinder = $this->objectManager->get(UrlFinderInterface::class); |
| 109 | + $actualUrls = $urlFinder->findOneByData( |
| 110 | + [ |
| 111 | + 'request_path' =>$urlPath2, |
| 112 | + 'store_id' => $storeId |
| 113 | + ] |
| 114 | + ); |
| 115 | + $categoryId = $actualUrls->getEntityId(); |
| 116 | + $targetPath = $actualUrls->getTargetPath(); |
| 117 | + $expectedType = $actualUrls->getEntityType(); |
| 118 | + $query |
| 119 | + = <<<QUERY |
| 120 | +{ |
| 121 | + urlResolver(url:"{$urlPath2}") |
| 122 | + { |
| 123 | + id |
| 124 | + canonical_url |
| 125 | + type |
| 126 | + } |
| 127 | + |
| 128 | +} |
| 129 | +QUERY; |
| 130 | + $response = $this->graphQlQuery($query); |
| 131 | + $this->assertArrayHasKey('urlResolver', $response); |
| 132 | + $this->assertEquals($categoryId, $response['urlResolver']['id']); |
| 133 | + $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']); |
| 134 | + $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']); |
| 135 | + } |
| 136 | +} |
0 commit comments