Skip to content

Commit 9501fe6

Browse files
committed
Merge remote-tracking branch 'origin/MC-34483' into 2.4-develop-pr28
2 parents 751fdbc + e4ea0d0 commit 9501fe6

File tree

4 files changed

+125
-45
lines changed
  • app/code/Magento
    • CatalogUrlRewrite
    • CmsUrlRewrite/Plugin/Cms/Model/Store
  • dev/tests/integration/testsuite/Magento/CmsUrlRewrite/Plugin/Cms/Model/Store

4 files changed

+125
-45
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Store;
79

810
use Magento\Catalog\Model\Category;
911
use Magento\Catalog\Model\CategoryFactory;
12+
use Magento\Catalog\Model\Product;
1013
use Magento\Catalog\Model\ProductFactory;
1114
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
1215
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
1316
use Magento\Framework\Model\AbstractModel;
17+
use Magento\Store\Model\ResourceModel\Store;
1418
use Magento\UrlRewrite\Model\UrlPersistInterface;
1519
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1620

@@ -23,17 +27,17 @@
2327
class View
2428
{
2529
/**
26-
* @var \Magento\UrlRewrite\Model\UrlPersistInterface
30+
* @var UrlPersistInterface
2731
*/
2832
protected $urlPersist;
2933

3034
/**
31-
* @var \Magento\Catalog\Model\CategoryFactory
35+
* @var CategoryFactory
3236
*/
3337
protected $categoryFactory;
3438

3539
/**
36-
* @var \Magento\Catalog\Model\ProductFactory
40+
* @var ProductFactory
3741
*/
3842
protected $productFactory;
3943

@@ -43,7 +47,7 @@ class View
4347
protected $categoryUrlRewriteGenerator;
4448

4549
/**
46-
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator
50+
* @var ProductUrlRewriteGenerator
4751
*/
4852
protected $productUrlRewriteGenerator;
4953

@@ -76,70 +80,62 @@ public function __construct(
7680
/**
7781
* Setter for Orig Store data
7882
*
79-
* @param \Magento\Store\Model\ResourceModel\Store $object
83+
* @param Store $object
8084
* @param AbstractModel $store
8185
* @return void
8286
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8387
*/
8488
public function beforeSave(
85-
\Magento\Store\Model\ResourceModel\Store $object,
89+
Store $object,
8690
AbstractModel $store
87-
) {
91+
): void {
8892
$this->origStore = $store;
8993
}
9094

9195
/**
9296
* Regenerate urls on store after save
9397
*
94-
* @param \Magento\Store\Model\ResourceModel\Store $object
95-
* @param \Magento\Store\Model\ResourceModel\Store $store
96-
* @return \Magento\Store\Model\ResourceModel\Store
98+
* @param Store $object
99+
* @param Store $store
100+
* @return Store
97101
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
98102
*/
99103
public function afterSave(
100-
\Magento\Store\Model\ResourceModel\Store $object,
101-
\Magento\Store\Model\ResourceModel\Store $store
102-
) {
104+
Store $object,
105+
Store $store
106+
): Store {
103107
if ($this->origStore->isObjectNew() || $this->origStore->dataHasChangedFor('group_id')) {
104108
$categoryRewriteUrls = $this->generateCategoryUrls(
105-
$this->origStore->getRootCategoryId(),
106-
$this->origStore->getId()
109+
(int)$this->origStore->getRootCategoryId(),
110+
(int)$this->origStore->getId()
107111
);
108112

109113
$this->urlPersist->replace($categoryRewriteUrls);
110114

111115
$this->urlPersist->replace(
112-
$this->generateProductUrls(
113-
$this->origStore->getWebsiteId(),
114-
$this->origStore->getOrigData('website_id'),
115-
$this->origStore->getId()
116-
)
116+
$this->generateProductUrls((int)$this->origStore->getId())
117117
);
118118
}
119+
119120
return $store;
120121
}
121122

122123
/**
123-
* Generate url rewrites for products assigned to website
124+
* Generate url rewrites for products assigned to store
124125
*
125-
* @param int $websiteId
126-
* @param int $originWebsiteId
127126
* @param int $storeId
128127
* @return array
129128
*/
130-
protected function generateProductUrls($websiteId, $originWebsiteId, $storeId)
129+
protected function generateProductUrls(int $storeId): array
131130
{
132131
$urls = [];
133-
$websiteIds = $websiteId != $originWebsiteId && $originWebsiteId !== null
134-
? [$websiteId, $originWebsiteId]
135-
: [$websiteId];
136132
$collection = $this->productFactory->create()
137133
->getCollection()
138134
->addCategoryIds()
139135
->addAttributeToSelect(['name', 'url_path', 'url_key', 'visibility'])
140-
->addWebsiteFilter($websiteIds);
136+
->addStoreFilter($storeId);
141137
foreach ($collection as $product) {
142-
/** @var \Magento\Catalog\Model\Product $product */
138+
/** @var Product $product */
143139
$product->setStoreId($storeId);
144140
$urls[] = $this->productUrlRewriteGenerator->generate($product);
145141
}
@@ -149,13 +145,13 @@ protected function generateProductUrls($websiteId, $originWebsiteId, $storeId)
149145
}
150146

151147
/**
152-
* Generate url rewrites for categories
148+
* Generate url rewrites for categories assigned to store
153149
*
154150
* @param int $rootCategoryId
155151
* @param int $storeId
156152
* @return array
157153
*/
158-
protected function generateCategoryUrls($rootCategoryId, $storeId)
154+
protected function generateCategoryUrls(int $rootCategoryId, int $storeId): array
159155
{
160156
$urls = [];
161157
$categories = $this->categoryFactory->create()->getCategories($rootCategoryId, 1, false, true, false);
@@ -173,17 +169,17 @@ protected function generateCategoryUrls($rootCategoryId, $storeId)
173169
/**
174170
* Delete unused url rewrites
175171
*
176-
* @param \Magento\Store\Model\ResourceModel\Store $subject
177-
* @param \Magento\Store\Model\ResourceModel\Store $result
172+
* @param Store $subject
173+
* @param Store $result
178174
* @param AbstractModel $store
179-
* @return \Magento\Store\Model\ResourceModel\Store
175+
* @return Store
180176
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
181177
*/
182178
public function afterDelete(
183-
\Magento\Store\Model\ResourceModel\Store $subject,
184-
\Magento\Store\Model\ResourceModel\Store $result,
179+
Store $subject,
180+
Store $result,
185181
AbstractModel $store
186-
) {
182+
): Store {
187183
$this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $store->getId()]);
188184

189185
return $result;

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function setUp(): void
125125
->getMock();
126126
$this->productCollectionMock = $this->getMockBuilder(ProductCollection::class)
127127
->disableOriginalConstructor()
128-
->setMethods(['addCategoryIds', 'addAttributeToSelect', 'addWebsiteFilter', 'getIterator'])
128+
->setMethods(['addCategoryIds', 'addAttributeToSelect', 'getIterator', 'addStoreFilter'])
129129
->getMock();
130130
$this->productMock = $this->getMockBuilder(Product::class)
131131
->disableOriginalConstructor()
@@ -190,7 +190,7 @@ public function testAfterSave(): void
190190
->method('addAttributeToSelect')
191191
->willReturn($this->productCollectionMock);
192192
$this->productCollectionMock->expects($this->once())
193-
->method('addWebsiteFilter')
193+
->method('addStoreFilter')
194194
->willReturn($this->productCollectionMock);
195195
$iterator = new \ArrayIterator([$this->productMock]);
196196
$this->productCollectionMock->expects($this->once())

app/code/Magento/CmsUrlRewrite/Plugin/Cms/Model/Store/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __construct(
7272
*/
7373
public function afterSave(ResourceStore $object, ResourceStore $result, AbstractModel $store): void
7474
{
75-
if ($store->isObjectNew() || $store->dataHasChangedFor('group_id')) {
75+
if ($store->isObjectNew()) {
7676
$this->urlPersist->replace(
7777
$this->generateCmsPagesUrls((int)$store->getId())
7878
);

dev/tests/integration/testsuite/Magento/CmsUrlRewrite/Plugin/Cms/Model/Store/ViewTest.php

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,30 @@
77

88
namespace Magento\CmsUrlRewrite\Plugin\Cms\Model\Store;
99

10+
use Magento\Cms\Api\Data\PageInterface;
11+
use Magento\Cms\Api\PageRepositoryInterface;
12+
use Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator;
13+
use Magento\CmsUrlRewrite\Model\CmsPageUrlRewriteGenerator;
14+
use Magento\Framework\Api\Filter;
15+
use Magento\Framework\Api\Search\FilterGroup;
16+
use Magento\Framework\Api\SearchCriteriaInterface;
1017
use Magento\Framework\ObjectManagerInterface;
1118
use Magento\Store\Model\Store;
1219
use Magento\Store\Model\StoreFactory;
1320
use Magento\TestFramework\Helper\Bootstrap;
1421
use Magento\UrlRewrite\Model\UrlFinderInterface;
22+
use Magento\UrlRewrite\Model\UrlPersistInterface;
1523
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
24+
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
25+
use PHPUnit\Framework\TestCase;
1626

1727
/**
1828
* Test for plugin which is listening store resource model and on save replace cms page url rewrites
1929
*
2030
* @magentoAppArea adminhtml
31+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2132
*/
22-
class ViewTest extends \PHPUnit\Framework\TestCase
33+
class ViewTest extends TestCase
2334
{
2435
/**
2536
* @var UrlFinderInterface
@@ -36,6 +47,26 @@ class ViewTest extends \PHPUnit\Framework\TestCase
3647
*/
3748
private $storeFactory;
3849

50+
/**
51+
* @var UrlPersistInterface
52+
*/
53+
private $urlPersist;
54+
55+
/**
56+
* @var UrlRewriteFactory
57+
*/
58+
private $urlRewriteFactory;
59+
60+
/**
61+
* @var PageRepositoryInterface
62+
*/
63+
private $pageRepository;
64+
65+
/**
66+
* @var CmsPageUrlPathGenerator
67+
*/
68+
private $cmsPageUrlPathGenerator;
69+
3970
/**
4071
* @inheritdoc
4172
*/
@@ -44,6 +75,10 @@ protected function setUp(): void
4475
$this->objectManager = Bootstrap::getObjectManager();
4576
$this->urlFinder = $this->objectManager->create(UrlFinderInterface::class);
4677
$this->storeFactory = $this->objectManager->create(StoreFactory::class);
78+
$this->urlPersist = $this->objectManager->create(UrlPersistInterface::class);
79+
$this->urlRewriteFactory = $this->objectManager->create(UrlRewriteFactory::class);
80+
$this->pageRepository = $this->objectManager->create(PageRepositoryInterface::class);
81+
$this->cmsPageUrlPathGenerator = $this->objectManager->create(CmsPageUrlPathGenerator::class);
4782
}
4883

4984
/**
@@ -54,21 +89,25 @@ protected function setUp(): void
5489
public function testUrlRewritesChangesAfterStoreSave()
5590
{
5691
$storeId = $this->createStore();
57-
$this->assertUrlRewritesCount($storeId, 1);
92+
$this->assertUrlRewritesCount($storeId, 'page100', 1);
93+
$this->editUrlRewrite($storeId, 'page100');
94+
$this->saveStore($storeId);
95+
$this->assertUrlRewritesCount($storeId, 'page100-test', 1);
5896
$this->deleteStore($storeId);
59-
$this->assertUrlRewritesCount($storeId, 0);
97+
$this->assertUrlRewritesCount($storeId, 'page100', 0);
6098
}
6199

62100
/**
63101
* Assert url rewrites count by store id
64102
*
65103
* @param int $storeId
104+
* @param string $pageIdentifier
66105
* @param int $expectedCount
67106
*/
68-
private function assertUrlRewritesCount(int $storeId, int $expectedCount): void
107+
private function assertUrlRewritesCount(int $storeId, string $pageIdentifier, int $expectedCount): void
69108
{
70109
$data = [
71-
UrlRewrite::REQUEST_PATH => 'page100',
110+
UrlRewrite::REQUEST_PATH => $pageIdentifier,
72111
UrlRewrite::STORE_ID => $storeId
73112
];
74113
$urlRewrites = $this->urlFinder->findAllByData($data);
@@ -105,4 +144,49 @@ private function deleteStore(int $storeId): void
105144
$store->delete();
106145
}
107146
}
147+
148+
/**
149+
* Edit url rewrite
150+
*
151+
* @param int $storeId
152+
* @param string $pageIdentifier
153+
*/
154+
private function editUrlRewrite(int $storeId, string $pageIdentifier): void
155+
{
156+
$filter = $this->objectManager->create(Filter::class);
157+
$filter->setField('identifier')->setValue($pageIdentifier);
158+
$filterGroup = $this->objectManager->create(FilterGroup::class);
159+
$filterGroup->setFilters([$filter]);
160+
$searchCriteria = $this->objectManager->create(SearchCriteriaInterface::class);
161+
$searchCriteria->setFilterGroups([$filterGroup]);
162+
$pageSearchResults = $this->pageRepository->getList($searchCriteria);
163+
$pages = $pageSearchResults->getItems();
164+
/** @var PageInterface $page */
165+
$cmsPage = array_values($pages)[0];
166+
167+
$urlRewrite = $this->urlRewriteFactory->create()->setStoreId($storeId)
168+
->setEntityType(CmsPageUrlRewriteGenerator::ENTITY_TYPE)
169+
->setEntityId($cmsPage->getId())
170+
->setRequestPath($cmsPage->getIdentifier() . '-test')
171+
->setTargetPath($this->cmsPageUrlPathGenerator->getCanonicalUrlPath($cmsPage))
172+
->setIsAutogenerated(0)
173+
->setRedirectType(0);
174+
175+
$this->urlPersist->replace([$urlRewrite]);
176+
}
177+
178+
/**
179+
* Edit test store
180+
*
181+
* @param int $storeId
182+
* @return void
183+
*/
184+
private function saveStore(int $storeId): void
185+
{
186+
$store = $this->storeFactory->create();
187+
$store->load($storeId);
188+
if ($store !== null) {
189+
$store->save();
190+
}
191+
}
108192
}

0 commit comments

Comments
 (0)