Skip to content

Commit 67f525d

Browse files
authored
Merge pull request magento#4938 from magento-honey-badgers/MC-21811-Test
[honey] MC-21811: Canonical_url displays the backend domain instead of relative
2 parents 71f9784 + 377f126 commit 67f525d

File tree

6 files changed

+247
-10
lines changed

6 files changed

+247
-10
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\CatalogGraphQl\Model\Resolver\Category;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Store\Api\Data\StoreInterface;
16+
use Magento\Catalog\Helper\Category as CategoryHelper;
17+
18+
/**
19+
* Resolve data for category canonical URL
20+
*/
21+
class CanonicalUrl implements ResolverInterface
22+
{
23+
/** @var CategoryHelper */
24+
private $categoryHelper;
25+
26+
/**
27+
* CanonicalUrl constructor.
28+
* @param CategoryHelper $categoryHelper
29+
*/
30+
public function __construct(CategoryHelper $categoryHelper)
31+
{
32+
$this->categoryHelper = $categoryHelper;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(
39+
Field $field,
40+
$context,
41+
ResolveInfo $info,
42+
array $value = null,
43+
array $args = null
44+
) {
45+
if (!isset($value['model'])) {
46+
throw new LocalizedException(__('"model" value should be specified'));
47+
}
48+
49+
/* @var Category $category */
50+
$category = $value['model'];
51+
/** @var StoreInterface $store */
52+
$store = $context->getExtensionAttributes()->getStore();
53+
if ($this->categoryHelper->canUseCanonicalTag($store)) {
54+
$baseUrl = $category->getUrlInstance()->getBaseUrl();
55+
return str_replace($baseUrl, '', $category->getUrl());
56+
}
57+
return null;
58+
}
59+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CanonicalUrl.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@
1212
use Magento\Framework\GraphQl\Config\Element\Field;
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Catalog\Helper\Product as ProductHelper;
16+
use Magento\Store\Api\Data\StoreInterface;
1517

1618
/**
1719
* Resolve data for product canonical URL
1820
*/
1921
class CanonicalUrl implements ResolverInterface
2022
{
23+
/** @var ProductHelper */
24+
private $productHelper;
25+
26+
/**
27+
* @param Product $productHelper
28+
*/
29+
public function __construct(ProductHelper $productHelper)
30+
{
31+
$this->productHelper = $productHelper;
32+
}
33+
2134
/**
2235
* @inheritdoc
2336
*/
@@ -32,10 +45,14 @@ public function resolve(
3245
throw new LocalizedException(__('"model" value should be specified'));
3346
}
3447

35-
/* @var $product Product */
48+
/* @var Product $product */
3649
$product = $value['model'];
37-
$url = $product->getUrlModel()->getUrl($product, ['_ignore_category' => true]);
38-
39-
return $url;
50+
/** @var StoreInterface $store */
51+
$store = $context->getExtensionAttributes()->getStore();
52+
if ($this->productHelper->canUseCanonicalTag($store)) {
53+
$product->getUrlModel()->getUrl($product, ['_ignore_category' => true]);
54+
return $product->getRequestPath();
55+
}
56+
return null;
4057
}
4158
}

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
109109
gift_message_available: String @doc(description: "Indicates whether a gift message is available.")
110110
manufacturer: Int @doc(description: "A number representing the product's manufacturer.")
111111
categories: [CategoryInterface] @doc(description: "The categories assigned to a product.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Categories") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoriesIdentity")
112-
canonical_url: String @doc(description: "Canonical URL.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CanonicalUrl")
112+
canonical_url: String @doc(description: "Relative canonical URL. This value is returned only if the system setting 'Use Canonical Link Meta Tag For Products' is enabled") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CanonicalUrl")
113113
media_gallery: [MediaGalleryInterface] @doc(description: "An array of Media Gallery objects.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\MediaGallery")
114114
}
115115

@@ -223,6 +223,7 @@ interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model
223223
path_in_store: String @doc(description: "Category path in store.")
224224
url_key: String @doc(description: "The url key assigned to the category.")
225225
url_path: String @doc(description: "The url path assigned to the category.")
226+
canonical_url: String @doc(description: "Relative canonical URL. This value is returned only if the system setting 'Use Canonical Link Meta Tag For Categories' is enabled") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CanonicalUrl")
226227
position: Int @doc(description: "The position of the category relative to other categories at the same level in tree.")
227228
level: Int @doc(description: "Indicates the depth of the category within the tree.")
228229
created_at: String @doc(description: "Timestamp indicating when the category was created.")
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\Model\ResourceModel\Category\Collection as CategoryCollection;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\ObjectManager;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
use Magento\Catalog\Api\Data\CategoryInterface;
15+
16+
/**
17+
* Test for getting canonical url data from category
18+
*/
19+
class CategoryCanonicalUrlTest extends GraphQlAbstract
20+
{
21+
/** @var ObjectManager $objectManager */
22+
private $objectManager;
23+
24+
/**
25+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
26+
* @magentoConfigFixture default_store catalog/seo/category_canonical_tag 1
27+
*/
28+
public function testCategoryWithCanonicalLinksMetaTagSettingsEnabled()
29+
{
30+
$this->objectManager = Bootstrap::getObjectManager();
31+
/** @var CategoryCollection $categoryCollection */
32+
$categoryCollection = $this->objectManager->create(CategoryCollection::class);
33+
$categoryCollection->addFieldToFilter('name', 'Category 1.1.1');
34+
/** @var CategoryInterface $category */
35+
$category = $categoryCollection->getFirstItem();
36+
$categoryId = $category->getId();
37+
$query = <<<QUERY
38+
{
39+
categoryList(filters: {ids: {in: ["$categoryId"]}}) {
40+
id
41+
name
42+
url_key
43+
url_suffix
44+
canonical_url
45+
}
46+
}
47+
QUERY;
48+
49+
$response = $this->graphQlQuery($query);
50+
$this->assertNotEmpty($response['categoryList'], 'Category list should not be empty');
51+
$this->assertEquals('.html', $response['categoryList'][0]['url_suffix']);
52+
$this->assertEquals(
53+
'category-1/category-1-1/category-1-1-1.html',
54+
$response['categoryList'][0]['canonical_url']
55+
);
56+
}
57+
58+
/**
59+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
60+
* @magentoConfigFixture default_store catalog/seo/category_canonical_tag 0
61+
*/
62+
public function testCategoryWithCanonicalLinksMetaTagSettingsDisabled()
63+
{
64+
$this->objectManager = Bootstrap::getObjectManager();
65+
/** @var CategoryCollection $categoryCollection */
66+
$categoryCollection = $this->objectManager->create(CategoryCollection::class);
67+
$categoryCollection->addFieldToFilter('name', 'Category 1.1');
68+
/** @var CategoryInterface $category */
69+
$category = $categoryCollection->getFirstItem();
70+
$categoryId = $category->getId();
71+
$query = <<<QUERY
72+
{
73+
categoryList(filters: {ids: {in: ["$categoryId"]}}) {
74+
id
75+
name
76+
url_key
77+
canonical_url
78+
}
79+
}
80+
QUERY;
81+
82+
$response = $this->graphQlQuery($query);
83+
$this->assertNotEmpty($response['categoryList'], 'Category list should not be empty');
84+
$this->assertNull(
85+
$response['categoryList'][0]['canonical_url']
86+
);
87+
$this->assertEquals('category-1-1', $response['categoryList'][0]['url_key']);
88+
}
89+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\TestFramework\TestCase\GraphQlAbstract;
11+
12+
/**
13+
* Test for getting canonical_url for products
14+
*/
15+
class ProductCanonicalUrlTest extends GraphQlAbstract
16+
{
17+
/**
18+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
19+
* @magentoConfigFixture default_store catalog/seo/product_canonical_tag 1
20+
*
21+
*/
22+
public function testProductWithCanonicalLinksMetaTagSettingsEnabled()
23+
{
24+
$productSku = 'simple';
25+
$query
26+
= <<<QUERY
27+
{
28+
products (filter: {sku: {eq: "{$productSku}"}}) {
29+
items {
30+
name
31+
sku
32+
canonical_url
33+
}
34+
}
35+
}
36+
QUERY;
37+
38+
$response = $this->graphQlQuery($query);
39+
$this->assertNotEmpty($response['products']['items']);
40+
41+
$this->assertEquals(
42+
'simple-product.html',
43+
$response['products']['items'][0]['canonical_url']
44+
);
45+
$this->assertEquals('simple', $response['products']['items'][0]['sku']);
46+
}
47+
48+
/**
49+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
50+
* @magentoConfigFixture default_store catalog/seo/product_canonical_tag 0
51+
*/
52+
public function testProductWithCanonicalLinksMetaTagSettingsDisabled()
53+
{
54+
$productSku = 'simple';
55+
$query
56+
= <<<QUERY
57+
{
58+
products (filter: {sku: {eq: "{$productSku}"}}) {
59+
items {
60+
name
61+
sku
62+
canonical_url
63+
}
64+
}
65+
}
66+
QUERY;
67+
68+
$response = $this->graphQlQuery($query);
69+
$this->assertNull(
70+
$response['products']['items'][0]['canonical_url']
71+
);
72+
$this->assertEquals('simple', $response['products']['items'][0]['sku']);
73+
}
74+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductViewTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,8 @@ public function testQueryAllFieldsSimpleProduct()
292292
'Filter category',
293293
$responseObject->getData('products/items/0/categories/1/name')
294294
);
295-
$storeManager = ObjectManager::getInstance()->get(\Magento\Store\Model\StoreManagerInterface::class);
296-
self::assertEquals(
297-
$storeManager->getStore()->getBaseUrl() . 'simple-product.html',
298-
$responseObject->getData('products/items/0/canonical_url')
299-
);
295+
//canonical_url will be null unless the admin setting catalog/seo/product_canonical_tag is turned ON
296+
self::assertNull($responseObject->getData('products/items/0/canonical_url'));
300297
}
301298

302299
/**

0 commit comments

Comments
 (0)