Skip to content

Commit 6b62770

Browse files
committed
Merge branch 'ACP2E-84' of https://github.com/magento-l3/magento2ce into L3-PR-20220202
2 parents 2e79ec2 + 32cadca commit 6b62770

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Products/AdaptUrlRewritesToVisibilityAttribute.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ public function __construct(
6565
*
6666
* @param array $productIds
6767
* @param int $visibility
68+
* @param int $storeId
6869
* @throws UrlAlreadyExistsException
6970
*/
70-
public function execute(array $productIds, int $visibility): void
71+
public function execute(array $productIds, int $visibility, int $storeId): void
7172
{
72-
$products = $this->getProductsByIds($productIds);
73+
$products = $this->getProductsByIds($productIds, $storeId);
7374

7475
/** @var Product $product */
7576
foreach ($products as $product) {
@@ -110,11 +111,13 @@ public function execute(array $productIds, int $visibility): void
110111
* Get Product Models by Id's
111112
*
112113
* @param array $productIds
114+
* @param int $storeId
113115
* @return array
114116
*/
115-
private function getProductsByIds(array $productIds): array
117+
private function getProductsByIds(array $productIds, int $storeId): array
116118
{
117119
$productCollection = $this->productCollectionFactory->create();
120+
$productCollection->setStoreId($storeId);
118121
$productCollection->addAttributeToSelect(ProductInterface::VISIBILITY);
119122
$productCollection->addAttributeToSelect('url_key');
120123
$productCollection->addFieldToFilter(

app/code/Magento/CatalogUrlRewrite/Observer/ProcessUrlRewriteOnChangeProductVisibilityObserver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ public function execute(Observer $observer)
4343
$event = $observer->getEvent();
4444
$attrData = $event->getAttributesData();
4545
$productIds = $event->getProductIds();
46+
$storeIdFromScope = $event->getStoreId();
4647
$visibility = $attrData[ProductInterface::VISIBILITY] ?? 0;
4748

4849
if (!$visibility || !$productIds) {
4950
return;
5051
}
5152

52-
$this->adaptUrlRewritesToVisibility->execute($productIds, (int)$visibility);
53+
$this->adaptUrlRewritesToVisibility->execute($productIds, (int)$visibility, (int)$storeIdFromScope);
5354
}
5455
}

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/ProcessUrlRewriteOnChangeVisibilityObserverTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
use Magento\Catalog\Api\ProductRepositoryInterface;
1010
use Magento\Catalog\Model\Product\Visibility;
1111
use Magento\Framework\Event\ManagerInterface;
12+
use Magento\Store\Model\Store;
1213
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\TestFramework\Fixture\DataFixtureStorage;
15+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
16+
use Magento\TestFramework\Helper\Bootstrap;
1317
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
1418
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1519

1620
/**
1721
* @magentoAppArea adminhtml
1822
* @magentoDbIsolation disabled
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1924
*/
2025
class ProcessUrlRewriteOnChangeVisibilityObserverTest extends \PHPUnit\Framework\TestCase
2126
{
@@ -34,6 +39,11 @@ class ProcessUrlRewriteOnChangeVisibilityObserverTest extends \PHPUnit\Framework
3439
*/
3540
private $eventManager;
3641

42+
/**
43+
* @var DataFixtureStorage
44+
*/
45+
private $fixtures;
46+
3747
/**
3848
* Set up
3949
*/
@@ -42,6 +52,8 @@ protected function setUp(): void
4252
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
4353
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
4454
$this->eventManager = $this->objectManager->create(ManagerInterface::class);
55+
56+
$this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage();
4557
}
4658

4759
/**
@@ -148,6 +160,78 @@ public function testMakeProductVisibleViaMassAction()
148160
}
149161
}
150162

163+
/**
164+
* Test for multistore properties of the product to be respected in generated UrlRewrites
165+
* during the mass update for visibility change
166+
*
167+
* phpcs:disable Generic.Files.LineLength.TooLong
168+
* @magentoDataFixture Magento\Store\Test\Fixture\Website as:w1
169+
* @magentoDataFixture Magento\Store\Test\Fixture\Store as:s1
170+
* @magentoDataFixture Magento\Store\Test\Fixture\Group as:g1 with:{"website_id": "$w1.id$", "default_store_id": "$s1.id$"}
171+
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category as:c1
172+
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"category_ids":["$c1.id$"], "visibility": "1", "extension_attributes": {"website_ids": [1, "$w1.id$"]}} as:p1
173+
* @magentoAppIsolation enabled
174+
* phpcs:enable Generic.Files.LineLength.TooLong
175+
*/
176+
public function testMassActionUrlRewriteForStore()
177+
{
178+
$product = $this->fixtures->get('p1');
179+
$category = $this->fixtures->get('c1');
180+
$store = $this->fixtures->get('s1');
181+
182+
$productFilter = [
183+
UrlRewrite::ENTITY_TYPE => 'product',
184+
];
185+
186+
$beforeUpdate = $this->getActualResults($productFilter);
187+
$this->assertCount(0, $beforeUpdate);
188+
189+
$this->eventManager->dispatch(
190+
'catalog_product_attribute_update_before',
191+
[
192+
'attributes_data' => [ ProductInterface::VISIBILITY => Visibility::VISIBILITY_BOTH ],
193+
'product_ids' => [$product->getId()],
194+
'store_id' => Store::DEFAULT_STORE_ID
195+
]
196+
);
197+
198+
$expected = [
199+
[
200+
'request_path' => $product->getUrlKey() . ".html",
201+
'target_path' => "catalog/product/view/id/" . $product->getId(),
202+
'is_auto_generated' => 1,
203+
'redirect_type' => 0,
204+
'store_id' => (String) Store::DISTRO_STORE_ID
205+
],
206+
[
207+
'request_path' => $category->getUrlKey() . '/' . $product->getUrlKey() . ".html",
208+
'target_path' => "catalog/product/view/id/" . $product->getId() . '/category/' . $category->getId(),
209+
'is_auto_generated' => 1,
210+
'redirect_type' => 0,
211+
'store_id' => (String) Store::DISTRO_STORE_ID
212+
],
213+
[
214+
'request_path' => $product->getUrlKey() . ".html",
215+
'target_path' => "catalog/product/view/id/" . $product->getId(),
216+
'is_auto_generated' => 1,
217+
'redirect_type' => 0,
218+
'store_id' => (String) $store->getId()
219+
],
220+
[
221+
'request_path' => $category->getUrlKey() . '/' . $product->getUrlKey() . ".html",
222+
'target_path' => "catalog/product/view/id/" . $product->getId() . '/category/' . $category->getId(),
223+
'is_auto_generated' => 1,
224+
'redirect_type' => 0,
225+
'store_id' => (String) $store->getId()
226+
],
227+
];
228+
229+
$actual = $this->getActualResults($productFilter);
230+
foreach ($expected as $row) {
231+
$this->assertContains($row, $actual);
232+
}
233+
}
234+
151235
/**
152236
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/products_invisible.php
153237
* @magentoAppIsolation enabled

0 commit comments

Comments
 (0)