Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

GraphQL-80: rename canonical_url to relative_url and fix covered test #185

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function resolve(
if ($urlRewrite) {
$result = [
'id' => $urlRewrite->getEntityId(),
'canonical_url' => $urlRewrite->getTargetPath(),
'relative_url' => $urlRewrite->getTargetPath(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
];
}
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/UrlRewriteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type EntityUrl @doc(description: "EntityUrl is an output object containing the `id`, `canonical_url`, and `type` attributes") {
type EntityUrl @doc(description: "EntityUrl is an output object containing the `id`, `relative_url`, and `type` attributes") {
id: Int @doc(description: "The ID assigned to the object associated with the specified url. This could be a product ID, category ID, or page ID.")
canonical_url: String @doc(description: "The internal relative URL. If the specified url is a redirect, the query returns the redirected URL, not the original.")
relative_url: String @doc(description: "The internal relative URL. If the specified url is a redirect, the query returns the redirected URL, not the original.")
type: UrlRewriteEntityTypeEnum @doc(description: "One of PRODUCT, CATEGORY, or CMS_PAGE.")
}

type Query {
urlResolver(url: String!): EntityUrl @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite") @doc(description: "The urlResolver query returns the canonical URL for a specified product, category or CMS page")
urlResolver(url: String!): EntityUrl @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite") @doc(description: "The urlResolver query returns the relative URL for a specified product, category or CMS page")
}

enum UrlRewriteEntityTypeEnum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\Cms\Helper\Page as PageHelper;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Test the GraphQL endpoint's URLResolver query to verify canonical URL's are correctly returned.
Expand All @@ -28,7 +31,7 @@ protected function setUp()
}

/**
* Tests if target_path(canonical_url) is resolved for Product entity
* Tests if target_path(relative_url) is resolved for Product entity
*
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
*/
Expand Down Expand Up @@ -57,20 +60,20 @@ public function testProductUrlResolver()
urlResolver(url:"{$urlPath}")
{
id
canonical_url
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('urlResolver', $response);
$this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
}

/**
* Tests the use case where canonical_url is provided as resolver input in the Query
* Tests the use case where relative_url is provided as resolver input in the Query
*
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
*/
Expand Down Expand Up @@ -101,15 +104,15 @@ public function testProductUrlWithCanonicalUrlInput()
urlResolver(url:"{$canonicalPath}")
{
id
canonical_url
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('urlResolver', $response);
$this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
}

Expand Down Expand Up @@ -144,15 +147,15 @@ public function testCategoryUrlResolver()
urlResolver(url:"{$urlPath2}")
{
id
canonical_url
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('urlResolver', $response);
$this->assertEquals($categoryId, $response['urlResolver']['id']);
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
}

Expand Down Expand Up @@ -180,14 +183,14 @@ public function testCMSPageUrlResolver()
urlResolver(url:"{$requestPath}")
{
id
canonical_url
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);
$this->assertEquals($cmsPageId, $response['urlResolver']['id']);
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
$this->assertEquals(strtoupper(str_replace('-', '_', $expectedEntityType)), $response['urlResolver']['type']);
}

Expand Down Expand Up @@ -223,15 +226,15 @@ public function testProductUrlRewriteResolver()
urlResolver(url:"{$urlPath}")
{
id
canonical_url
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('urlResolver', $response);
$this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
}

Expand Down Expand Up @@ -263,7 +266,7 @@ public function testInvalidUrlResolverInput()
urlResolver(url:"{$urlPath}")
{
id
canonical_url
relative_url
type
}
}
Expand Down Expand Up @@ -304,15 +307,15 @@ public function testCategoryUrlWithLeadingSlash()
urlResolver(url:"/{$urlPath}")
{
id
canonical_url
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('urlResolver', $response);
$this->assertEquals($categoryId, $response['urlResolver']['id']);
$this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
$this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
}

Expand All @@ -321,22 +324,35 @@ public function testCategoryUrlWithLeadingSlash()
*/
public function testResolveSlash()
{
/** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface */
$scopeConfigInterface = $this->objectManager->get(ScopeConfigInterface::class);
$homePageIdentifier = $scopeConfigInterface->getValue(
PageHelper::XML_PATH_HOME_PAGE,
ScopeInterface::SCOPE_STORE
);
/** @var \Magento\Cms\Model\Page $page */
$page = $this->objectManager->get(\Magento\Cms\Model\Page::class);
$page->load($homePageIdentifier);
$homePageId = $page->getId();
/** @var \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $urlPathGenerator */
$urlPathGenerator = $this->objectManager->get(\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator::class);
/** @param \Magento\Cms\Api\Data\PageInterface $page */
$targetPath = $urlPathGenerator->getCanonicalUrlPath($page);
$query
= <<<QUERY
{
urlResolver(url:"/")
{
id
canonical_url
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);

$this->assertArrayHasKey('urlResolver', $response);
$this->assertEquals(2, $response['urlResolver']['id']);
$this->assertEquals('cms/page/view/page_id/2', $response['urlResolver']['canonical_url']);
$this->assertEquals($homePageId, $response['urlResolver']['id']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
$this->assertEquals('CMS_PAGE', $response['urlResolver']['type']);
}
}