Skip to content

Commit 9567e02

Browse files
committed
MAGETWO-86325: Create functional tests
- updated added tests for url resolver service
1 parent 650dbba commit 9567e02

File tree

2 files changed

+140
-21
lines changed

2 files changed

+140
-21
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite/UrlResolverTest.php

Lines changed: 130 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ class UrlResolverTest extends GraphQlAbstract
1919

2020
/** @var ObjectManager */
2121
private $objectManager;
22-
/**
23-
* @var StoreManagerInterface
24-
*/
25-
// private $storeManager;
2622

2723
/**
2824
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
@@ -35,6 +31,8 @@ protected function setUp()
3531
}
3632

3733
/**
34+
* Tests if the target_path(canonical_url) when a Product entity with a valid url_key (request_path) is provided
35+
*
3836
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
3937
*/
4038
public function testProductUrlResolver()
@@ -45,15 +43,6 @@ public function testProductUrlResolver()
4543
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
4644
$product = $productRepository->get($productSku, false, null, true);
4745
$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);
5746

5847
/** @var UrlFinderInterface $urlFinder */
5948
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
@@ -87,22 +76,58 @@ public function testProductUrlResolver()
8776
/**
8877
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
8978
*/
90-
public function testCategoryUrlResolver()
79+
public function testProductUrlWithCanonicalUrlInput()
9180
{
9281
$productSku = 'p002';
93-
$urlPath2 = 'cat-1.html';
82+
$urlPath = 'p002.html';
9483
/** @var ProductRepositoryInterface $productRepository */
9584
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
9685
$product = $productRepository->get($productSku, false, null, true);
9786
$storeId = $product->getStoreId();
87+
$product->getUrlKey();
9888

99-
/** @var UrlRewrite $productUrlRewrite */
100-
// $productUrlRewrite = $this->objectManager->get(UrlRewrite::class);
101-
/** @var ProductUrlRewriteGenerator $generator */
102-
// $generator = $this->objectManager->get(ProductUrlRewriteGenerator::class);
89+
/** @var UrlFinderInterface $urlFinder */
90+
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
91+
$actualUrls = $urlFinder->findOneByData(
92+
[
93+
'request_path' =>$urlPath,
94+
'store_id' => $storeId
95+
]
96+
);
97+
$targetPath = $actualUrls->getTargetPath();
98+
$expectedType = $actualUrls->getEntityType();
99+
$canonicalPath = $actualUrls->getTargetPath();
100+
$query
101+
= <<<QUERY
102+
{
103+
urlResolver(url:"{$canonicalPath}")
104+
{
105+
id
106+
canonical_url
107+
type
108+
}
109+
110+
}
111+
QUERY;
112+
$response = $this->graphQlQuery($query);
113+
$this->assertArrayHasKey('urlResolver', $response);
114+
$this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
115+
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
116+
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
117+
}
103118

104-
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $urls */
105-
// $urls = $generator->generate($product);
119+
/**
120+
*Test the
121+
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
122+
*/
123+
public function testCategoryUrlResolver()
124+
{
125+
$productSku = 'p002';
126+
$urlPath2 = 'cat-1.html';
127+
/** @var ProductRepositoryInterface $productRepository */
128+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
129+
$product = $productRepository->get($productSku, false, null, true);
130+
$storeId = $product->getStoreId();
106131

107132
/** @var UrlFinderInterface $urlFinder */
108133
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
@@ -133,4 +158,88 @@ public function testCategoryUrlResolver()
133158
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
134159
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
135160
}
161+
162+
/**
163+
* Tests the use case where the url_key of the existing product is changed
164+
*
165+
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
166+
*/
167+
public function testProductUrlRewriteResolver()
168+
{
169+
$productSku = 'p002';
170+
/** @var ProductRepositoryInterface $productRepository */
171+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
172+
$product = $productRepository->get($productSku, false, null, true);
173+
$storeId = $product->getStoreId();
174+
$product->setUrlKey('p002-new')->save();
175+
$urlPath = $product->getUrlKey() .'.html';
176+
$this->assertEquals($urlPath, 'p002-new.html');
177+
178+
/** @var UrlFinderInterface $urlFinder */
179+
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
180+
$actualUrls = $urlFinder->findOneByData(
181+
[
182+
'request_path' =>$urlPath,
183+
'store_id' => $storeId
184+
]
185+
);
186+
$targetPath = $actualUrls->getTargetPath();
187+
$expectedType = $actualUrls->getEntityType();
188+
$query
189+
= <<<QUERY
190+
{
191+
urlResolver(url:"{$urlPath}")
192+
{
193+
id
194+
canonical_url
195+
type
196+
}
197+
198+
}
199+
QUERY;
200+
$response = $this->graphQlQuery($query);
201+
$this->assertArrayHasKey('urlResolver', $response);
202+
$this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
203+
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
204+
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
205+
}
206+
207+
/**
208+
* Tests if null is returned when an invalid request_path is provided as input to urlResolver
209+
*
210+
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
211+
*/
212+
public function testInvalidUrlResolverInput()
213+
{
214+
$productSku = 'p002';
215+
$urlPath = 'p002';
216+
/** @var ProductRepositoryInterface $productRepository */
217+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
218+
$product = $productRepository->get($productSku, false, null, true);
219+
$storeId = $product->getStoreId();
220+
221+
/** @var UrlFinderInterface $urlFinder */
222+
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
223+
$urlFinder->findOneByData(
224+
[
225+
'request_path' =>$urlPath,
226+
'store_id' => $storeId
227+
]
228+
);
229+
$query
230+
= <<<QUERY
231+
{
232+
urlResolver(url:"{$urlPath}")
233+
{
234+
id
235+
canonical_url
236+
type
237+
}
238+
239+
}
240+
QUERY;
241+
$response = $this->graphQlQuery($query);
242+
$this->assertArrayHasKey('urlResolver', $response);
243+
$this->assertNull($response['urlResolver']);
244+
}
136245
}

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
->setAttributeSetId(4)
6161
->setWebsiteIds([1])
6262
->setPrice(10)
63+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
64+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
65+
->setStockData(
66+
[
67+
'use_config_manage_stock' => 1,
68+
'qty' => 100,
69+
'is_qty_decimal' => 0,
70+
'is_in_stock' => 1,
71+
]
72+
)
6373
->setQty(100)
6474
->setWeight(1);
6575

0 commit comments

Comments
 (0)