Skip to content

Commit 088fe2f

Browse files
ENGCOM-8071: Always use canonical urls in the sitemap magento#29184
2 parents 0bdf1f0 + 652aea3 commit 088fe2f

File tree

7 files changed

+107
-49
lines changed

7 files changed

+107
-49
lines changed

app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
*/
66
namespace Magento\Sitemap\Model\ResourceModel\Catalog;
77

8-
use Magento\Catalog\Helper\Product as HelperProduct;
98
use Magento\Catalog\Model\Product\Image\UrlBuilder;
109
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
1110
use Magento\Framework\App\ObjectManager;
12-
use Magento\Store\Model\ScopeInterface;
1311
use Magento\Store\Model\Store;
1412

1513
/**
@@ -82,28 +80,11 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
8280
*/
8381
protected $_mediaConfig;
8482

85-
/**
86-
* @var \Magento\Catalog\Model\Product
87-
*/
88-
private $productModel;
89-
90-
/**
91-
* @var \Magento\Catalog\Helper\Image
92-
*/
93-
private $catalogImageHelper;
94-
9583
/**
9684
* @var UrlBuilder
9785
*/
9886
private $imageUrlBuilder;
9987

100-
/**
101-
* Scope Config
102-
*
103-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
104-
*/
105-
private $scopeConfig;
106-
10788
/**
10889
* Product constructor.
10990
*
@@ -122,6 +103,7 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
122103
* @param \Magento\Framework\App\Config\ScopeConfigInterface|null $scopeConfig
123104
* @param UrlBuilder $urlBuilder
124105
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
106+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
125107
*/
126108
public function __construct(
127109
\Magento\Framework\Model\ResourceModel\Db\Context $context,
@@ -147,13 +129,7 @@ public function __construct(
147129
$this->mediaGalleryReadHandler = $mediaGalleryReadHandler;
148130
$this->_mediaConfig = $mediaConfig;
149131
$this->_sitemapData = $sitemapData;
150-
$this->productModel = $productModel ?: ObjectManager::getInstance()->get(\Magento\Catalog\Model\Product::class);
151-
$this->catalogImageHelper = $catalogImageHelper;
152132
$this->imageUrlBuilder = $urlBuilder ?? ObjectManager::getInstance()->get(UrlBuilder::class);
153-
$this->catalogImageHelper = $catalogImageHelper ?: ObjectManager::getInstance()
154-
->get(\Magento\Catalog\Helper\Image::class);
155-
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()
156-
->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
157133

158134
parent::__construct($context, $connectionName);
159135
}
@@ -311,11 +287,6 @@ public function getCollection($storeId)
311287
}
312288

313289
$connection = $this->getConnection();
314-
$urlRewriteMetaDataCondition = '';
315-
if (!$this->isCategoryProductURLsConfig($storeId)) {
316-
$urlRewriteMetaDataCondition = ' AND url_rewrite.metadata IS NULL';
317-
}
318-
319290
$this->_select = $connection->select()->from(
320291
['e' => $this->getMainTable()],
321292
[$this->getIdFieldName(), $this->_productResource->getLinkField(), 'updated_at']
@@ -326,7 +297,7 @@ public function getCollection($storeId)
326297
)->joinLeft(
327298
['url_rewrite' => $this->getTable('url_rewrite')],
328299
'e.entity_id = url_rewrite.entity_id AND url_rewrite.is_autogenerated = 1'
329-
. $urlRewriteMetaDataCondition
300+
. ' AND url_rewrite.metadata IS NULL'
330301
. $connection->quoteInto(' AND url_rewrite.store_id = ?', $store->getId())
331302
. $connection->quoteInto(' AND url_rewrite.entity_type = ?', ProductUrlRewriteGenerator::ENTITY_TYPE),
332303
['url' => 'request_path']
@@ -491,20 +462,4 @@ private function getProductImageUrl($image)
491462
{
492463
return $this->imageUrlBuilder->getUrl($image, 'product_page_image_large');
493464
}
494-
495-
/**
496-
* Return Use Categories Path for Product URLs config value
497-
*
498-
* @param null|string $storeId
499-
*
500-
* @return bool
501-
*/
502-
private function isCategoryProductURLsConfig($storeId)
503-
{
504-
return $this->scopeConfig->isSetFlag(
505-
HelperProduct::XML_PATH_PRODUCT_URL_USE_CATEGORY,
506-
ScopeInterface::SCOPE_STORE,
507-
$storeId
508-
);
509-
}
510465
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AdminMarketingSiteMapFillFormSaveAndGenerateActionGroup">
12+
<annotations>
13+
<description>Fill data to Site Map form and click `Save and Generate`</description>
14+
</annotations>
15+
<arguments>
16+
<argument name="sitemap" type="entity" defaultValue="DefaultSiteMap"/>
17+
</arguments>
18+
19+
<fillField selector="{{AdminMarketingSiteMapEditActionSection.filename}}" userInput="{{sitemap.filename}}" stepKey="fillFilename"/>
20+
<fillField selector="{{AdminMarketingSiteMapEditActionSection.path}}" userInput="{{sitemap.path}}" stepKey="fillPath"/>
21+
<click selector="{{AdminMarketingSiteMapEditActionSection.saveAndGenerate}}" stepKey="saveAndGenerateSiteMap"/>
22+
<waitForPageLoad stepKey="waitForPageLoad"/>
23+
</actionGroup>
24+
</actionGroups>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AdminSiteMapGridClickFirstRowLinkActionGroup">
12+
<annotations>
13+
<description>Click first row link in Site Map grid</description>
14+
</annotations>
15+
16+
<click selector="{{AdminMarketingSiteMapGridSection.firstRowLink}}" stepKey="clickFirstLink"/>
17+
</actionGroup>
18+
</actionGroups>

app/code/Magento/Sitemap/Test/Mftf/Section/AdminMarketingSiteMapGridSection.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
<element name="searchButton" type="button" selector=".admin__filter-actions [title='Search']"/>
1616
<element name="firstSearchResult" type="text" selector="#sitemapGrid_table>tbody>tr:nth-child(1)"/>
1717
<element name="fileNameTextField" type="input" selector="#sitemapGrid_filter_sitemap_filename" timeout="90"/>
18+
<element name="firstRowLink" type="button" selector="//td[contains(@class, 'col-link')][1]/a"/>
1819
</section>
1920
</sections>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="StorefrontSitemapUseCanonicalUrlProductTest">
12+
<annotations>
13+
<group value="Rss"/>
14+
<stories value="Sitemap use canonical product url"/>
15+
<title value="Sitemap use canonical for product url"/>
16+
<description value="RSS Feed always use canonical url for product"/>
17+
<severity value="MAJOR"/>
18+
</annotations>
19+
<before>
20+
<magentoCLI command="config:set catalog/seo/product_use_categories 1" stepKey="enableUseCategoryPathForProductUrl"/>
21+
<magentoCLI command="config:set catalog/seo/product_canonical_tag 1" stepKey="enableUseCanonicalForProduct"/>
22+
<actionGroup ref="CliCacheCleanActionGroup" stepKey="cleanCache">
23+
<argument name="tags" value="config"/>
24+
</actionGroup>
25+
<createData entity="_defaultCategory" stepKey="createCategory"/>
26+
<createData entity="_defaultProduct" stepKey="createSimpleProductApi">
27+
<requiredEntity createDataKey="createCategory"/>
28+
</createData>
29+
30+
<actionGroup ref="AdminLoginActionGroup" stepKey="login"/>
31+
<actionGroup ref="AdminNavigateMenuActionGroup" stepKey="navigateToMarketingSiteMapPage">
32+
<argument name="menuUiId" value="{{AdminMenuMarketing.dataUiId}}"/>
33+
<argument name="submenuUiId" value="{{AdminMenuSEOAndSearchSiteMap.dataUiId}}"/>
34+
</actionGroup>
35+
<actionGroup ref="AdminMarketingSiteMapNavigateNewActionGroup" stepKey="navigateToNewSitemapPage"/>
36+
<actionGroup ref="AdminMarketingSiteMapFillFormSaveAndGenerateActionGroup" stepKey="createAndGenerateSitemap">
37+
<argument name="sitemap" value="UniqueSitemapName"/>
38+
</actionGroup>
39+
</before>
40+
<after>
41+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
42+
43+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
44+
<deleteData createDataKey="createSimpleProductApi" stepKey="deleteSimple"/>
45+
<magentoCLI command="config:set catalog/seo/product_use_categories 0" stepKey="disableUseCategoryPathForProductUrl"/>
46+
<magentoCLI command="config:set catalog/seo/product_canonical_tag 0" stepKey="disableUseCanonicalForProduct"/>
47+
<actionGroup ref="CliCacheCleanActionGroup" stepKey="cleanCache">
48+
<argument name="tags" value="config"/>
49+
</actionGroup>
50+
</after>
51+
52+
<actionGroup ref="AdminSiteMapGridClickFirstRowLinkActionGroup" stepKey="clickFirstLinkGrid"/>
53+
<see userInput="{{_ENV.MAGENTO_BASE_URL}}$$createSimpleProductApi.custom_attributes[url_key]$$" stepKey="seeCanonicalUrl" />
54+
</test>
55+
</tests>

app/code/Magento/UrlRewrite/etc/db_schema.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@
4040
<index referenceId="URL_REWRITE_ENTITY_ID" indexType="btree">
4141
<column name="entity_id"/>
4242
</index>
43+
<index referenceId="URL_REWRITE_IS_AUTOGENERATED_METADATA" indexType="btree">
44+
<column name="is_autogenerated"/>
45+
<column name="metadata"/>
46+
</index>
4347
</table>
4448
</schema>

app/code/Magento/UrlRewrite/etc/db_schema_whitelist.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
"index": {
1616
"URL_REWRITE_TARGET_PATH": true,
1717
"URL_REWRITE_STORE_ID_ENTITY_ID": true,
18-
"URL_REWRITE_ENTITY_ID": true
18+
"URL_REWRITE_ENTITY_ID": true,
19+
"URL_REWRITE_IS_AUTOGENERATED_METADATA": true
1920
},
2021
"constraint": {
2122
"PRIMARY": true,
2223
"URL_REWRITE_REQUEST_PATH_STORE_ID": true
2324
}
2425
}
25-
}
26+
}

0 commit comments

Comments
 (0)