Skip to content

Commit 58c7344

Browse files
author
Eric Bohanon
committed
Merge remote-tracking branch 'remotes/origin/MAGETWO-85687-Graphql-Declarative-schema' into MAGETWO-86320-Graph-Ql-Schema-Implementation
2 parents b46732f + d5c6e36 commit 58c7344

File tree

5 files changed

+148
-31
lines changed

5 files changed

+148
-31
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public function resolve(array $args, ResolverContextInterface $context)
6161
$searchResult = $this->filterQuery->getResult($searchCriteria);
6262
}
6363

64-
//division by 0
64+
//possible division by 0
6565
if ($searchCriteria->getPageSize()) {
6666
$maxPages = ceil($searchResult->getTotalCount() / $searchCriteria->getPageSize());
6767
} else {
68-
$maxPages = 0 ;
68+
$maxPages = 0;
6969
}
7070
if ($searchCriteria->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
7171
throw new GraphQlInputException(

app/code/Magento/GraphQl/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<item name="/config/type/field" xsi:type="string">name</item>
6161
<item name="/config/type/item" xsi:type="string">name</item>
6262
<item name="/config/type/implements" xsi:type="string">interface</item>
63-
</argument>
63+
</argument>
6464
</arguments>
6565
</virtualType>
6666
<type name="Magento\Framework\GraphQl\Type\Output\OutputFactory">

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function resolve(array $args, ResolverContextInterface $context)
5858
}
5959

6060
/**
61-
* Recursively find the canonical url passing through all redirects
61+
* Find the canonical url passing through all redirects if any
6262
*
6363
* @param string $requestPath
6464
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|null
@@ -110,7 +110,7 @@ private function findUrlFromTargetPath(string $targetPath)
110110
}
111111

112112
/**
113-
* Sanitizes the type to fit schema specs
113+
* Sanitize the type to fit schema specifications
114114
*
115115
* @param string $type
116116
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|null

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

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,15 @@ class UrlResolverTest extends GraphQlAbstract
1919

2020
/** @var ObjectManager */
2121
private $objectManager;
22-
/**
23-
* @var StoreManagerInterface
24-
*/
25-
// private $storeManager;
26-
27-
/**
28-
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
29-
*/
30-
private $urlPathGenerator;
3122

3223
protected function setUp()
3324
{
3425
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
3526
}
3627

3728
/**
29+
* Tests if target_path(canonical_url) is resolved for Product entity
30+
*
3831
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
3932
*/
4033
public function testProductUrlResolver()
@@ -45,15 +38,50 @@ public function testProductUrlResolver()
4538
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
4639
$product = $productRepository->get($productSku, false, null, true);
4740
$storeId = $product->getStoreId();
48-
$product->getUrlKey();
4941

50-
/** @var UrlRewrite $productUrlRewrite */
51-
// $productUrlRewrite = $this->objectManager->get(UrlRewrite::class);
52-
/** @var ProductUrlRewriteGenerator $generator */
53-
// $generator = $this->objectManager->get(ProductUrlRewriteGenerator::class);
42+
/** @var UrlFinderInterface $urlFinder */
43+
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
44+
$actualUrls = $urlFinder->findOneByData(
45+
[
46+
'request_path' =>$urlPath,
47+
'store_id' => $storeId
48+
]
49+
);
50+
$targetPath = $actualUrls->getTargetPath();
51+
$expectedType = $actualUrls->getEntityType();
52+
$query
53+
= <<<QUERY
54+
{
55+
urlResolver(url:"{$urlPath}")
56+
{
57+
id
58+
canonical_url
59+
type
60+
}
61+
62+
}
63+
QUERY;
64+
$response = $this->graphQlQuery($query);
65+
$this->assertArrayHasKey('urlResolver', $response);
66+
$this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
67+
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
68+
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
69+
}
5470

55-
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $urls */
56-
// $urls = $generator->generate($product);
71+
/**
72+
* Tests the use case where canonical_url is provided as resolver input in the Query
73+
*
74+
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
75+
*/
76+
public function testProductUrlWithCanonicalUrlInput()
77+
{
78+
$productSku = 'p002';
79+
$urlPath = 'p002.html';
80+
/** @var ProductRepositoryInterface $productRepository */
81+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
82+
$product = $productRepository->get($productSku, false, null, true);
83+
$storeId = $product->getStoreId();
84+
$product->getUrlKey();
5785

5886
/** @var UrlFinderInterface $urlFinder */
5987
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
@@ -65,10 +93,11 @@ public function testProductUrlResolver()
6593
);
6694
$targetPath = $actualUrls->getTargetPath();
6795
$expectedType = $actualUrls->getEntityType();
96+
$canonicalPath = $actualUrls->getTargetPath();
6897
$query
6998
= <<<QUERY
7099
{
71-
urlResolver(url:"{$urlPath}")
100+
urlResolver(url:"{$canonicalPath}")
72101
{
73102
id
74103
canonical_url
@@ -85,7 +114,9 @@ public function testProductUrlResolver()
85114
}
86115

87116
/**
88-
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
117+
* Test for category entity
118+
*
119+
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
89120
*/
90121
public function testCategoryUrlResolver()
91122
{
@@ -96,14 +127,6 @@ public function testCategoryUrlResolver()
96127
$product = $productRepository->get($productSku, false, null, true);
97128
$storeId = $product->getStoreId();
98129

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

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)