Skip to content

Commit fab97fc

Browse files
committed
ACP2E-1783: Unable to create 301/302 redirect for product with a category path when Generate "category/product" URL Rewrites set to Yes
- implemented solution and MFTF
1 parent a047c76 commit fab97fc

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

app/code/Magento/UrlRewrite/Controller/Adminhtml/Url/Rewrite/Save.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1112
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\UrlRewrite\Model\UrlFinderInterface;
@@ -34,6 +35,11 @@ class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implemen
3435
*/
3536
protected $urlFinder;
3637

38+
/**
39+
* @var ProductRepositoryInterface
40+
*/
41+
protected ProductRepositoryInterface $productRepository;
42+
3743
/**
3844
* @param \Magento\Backend\App\Action\Context $context
3945
* @param \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator
@@ -46,13 +52,16 @@ public function __construct(
4652
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator,
4753
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
4854
\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator,
49-
UrlFinderInterface $urlFinder
55+
UrlFinderInterface $urlFinder,
56+
ProductRepositoryInterface $productRepository = null
5057
) {
5158
parent::__construct($context);
5259
$this->productUrlPathGenerator = $productUrlPathGenerator;
5360
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
5461
$this->cmsPageUrlPathGenerator = $cmsPageUrlPathGenerator;
5562
$this->urlFinder = $urlFinder;
63+
$this->productRepository = $productRepository ?:
64+
\Magento\Framework\App\ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
5665
}
5766

5867
/**
@@ -97,12 +106,18 @@ protected function getTargetPath($model)
97106
];
98107
$rewrite = $this->urlFinder->findOneByData($data);
99108
if (!$rewrite) {
100-
$message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT
101-
? __("The selected product isn't associated with the selected store or category.")
102-
: __("The selected category isn't associated with the selected store.");
103-
throw new LocalizedException($message);
109+
$check = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT ?
110+
$this->_getProduct()->canBeShowInCategory($this->_getCategory()->getId()) :
111+
$this->_getCategory()->getStoreId() == $model->getStoreId();
112+
if (false === $check) {
113+
$message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT
114+
? __("The selected product isn't associated with the selected store or category.")
115+
: __("The selected category isn't associated with the selected store.");
116+
throw new LocalizedException($message);
117+
}
118+
} else {
119+
$targetPath = $rewrite->getRequestPath();
104120
}
105-
$targetPath = $rewrite->getRequestPath();
106121
}
107122
return $targetPath;
108123
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/PageObject.xsd">
11+
<page name="AdminUrlRewriteProductCategoryPage" url="admin/url_rewrite/edit/product/{{productId}}/category/{{categoryId}}" area="admin" module="Magento_UrlRewrite">
12+
<section name="AdminUrlRewriteProductCategorySection"/>
13+
</page>
14+
</pages>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
9+
<test name="AdminDeleteCreateProductUrlRewriteTest">
10+
<annotations>
11+
<stories value="Delete Product and product-category URL rewrites and then create a new one"/>
12+
<title value="Delete Product and product-category URL rewrites and then create a new one"/>
13+
<description value="Delete automated URL rewrites and then create one"/>
14+
<testCaseId value="AC-8380"/>
15+
<severity value="MAJOR"/>
16+
<group value="url_rewrite"/>
17+
</annotations>
18+
<before>
19+
<magentoCLI command="config:set catalog/seo/generate_category_product_rewrites 1" stepKey="enableCategoryProductRewrites"/>
20+
<actionGroup ref="AdminLoginActionGroup" stepKey="login"/>
21+
<!-- Create the category to put the product in -->
22+
<createData entity="SimpleSubCategory" stepKey="createCategory"/>
23+
<createData entity="SimpleProduct" stepKey="createSimpleProduct">
24+
<requiredEntity createDataKey="createCategory"/>
25+
</createData>
26+
<magentoCLI command="indexer:reindex" stepKey="performReindex"/>
27+
<magentoCLI command="cache:flush" stepKey="cleanCache"/>
28+
</before>
29+
<after>
30+
<!-- Delete the category and product -->
31+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
32+
<deleteData createDataKey="createSimpleProduct" stepKey="deleteProduct"/>
33+
<magentoCLI command="config:set catalog/seo/generate_category_product_rewrites 0" stepKey="disableGenerateUrlRewrite"/>
34+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
35+
<magentoCLI command="indexer:reindex" stepKey="performReindex"/>
36+
</after>
37+
38+
<!--Delete created product url rewrite and verify AssertUrlRewriteDeletedMessage-->
39+
<actionGroup ref="AdminDeleteUrlRewriteActionGroup" stepKey="deleteProductUrlRewrite">
40+
<argument name="requestPath" value="$$createSimpleProduct.custom_attributes[url_key]$$.html"/>
41+
</actionGroup>
42+
<actionGroup ref="AdminDeleteUrlRewriteActionGroup" stepKey="deleteProductCategoryUrlRewrite">
43+
<argument name="requestPath" value="$$createSimpleProduct.custom_attributes[url_key]$$.html"/>
44+
</actionGroup>
45+
46+
<!--Search and verify AssertUrlRewriteNotInGrid-->
47+
<actionGroup ref="AdminSearchDeletedUrlRewriteActionGroup" stepKey="searchDeletedUrlRewriteInGrid">
48+
<argument name="requestPath" value="$$createSimpleProduct.custom_attributes[url_key]$$.html"/>
49+
</actionGroup>
50+
51+
<!--Filter Product in product page and get the Product ID -->
52+
<actionGroup ref="FilterAndSelectProductActionGroup" stepKey="filterProduct">
53+
<argument name="productSku" value="$$createSimpleProduct.sku$$"/>
54+
</actionGroup>
55+
<grabFromCurrentUrl stepKey="productId" regex="#\/([0-9]*)?\/$#"/>
56+
57+
<!-- Open Category Page and Get Category ID -->
58+
<actionGroup ref="OpenCategoryFromCategoryTreeActionGroup" stepKey="getCategoryId">
59+
<argument name="category" value="$$createCategory.name$$"/>
60+
</actionGroup>
61+
<grabFromCurrentUrl stepKey="categoryId" regex="#\/([0-9]*)?\/$#"/>
62+
63+
<!-- Create product redirect -->
64+
<amOnPage url="{{AdminUrlRewriteProductCategoryPage.url({$productId}, {$categoryId})}}" stepKey="openProductRedirectWithCategory"/>
65+
<click selector="{{AdminUrlRewriteEditSection.redirectTypeValue('Temporary (302)')}}" stepKey="clickOnRedirectTypeValue"/>
66+
<click selector="{{AdminUrlRewriteEditSection.saveButton}}" stepKey="clickOnSaveButton"/>
67+
68+
<!-- Assert Url Rewrite Save Message -->
69+
<actionGroup ref="AssertMessageInAdminPanelActionGroup" stepKey="assertSuccessMessage">
70+
<argument name="message" value="The URL Rewrite has been saved."/>
71+
</actionGroup>
72+
</test>
73+
</tests>

0 commit comments

Comments
 (0)