Skip to content

Commit 0d4507b

Browse files
committed
Merge remote-tracking branch 'l3/MC-41550' into PR-L3-20210405
2 parents 66da081 + 118808a commit 0d4507b

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

app/code/Magento/CatalogUrlRewrite/Plugin/Catalog/Model/Product/UpdateProductWebsiteUrlRewrites.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\CatalogUrlRewrite\Plugin\Catalog\Model\Product;
99

1010
use Magento\Catalog\Model\Product\Action as ProductAction;
11-
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1212
use Magento\CatalogUrlRewrite\Model\Products\AppendUrlRewritesToProducts;
1313
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
1414
use Magento\Store\Api\StoreWebsiteRelationInterface;
@@ -27,9 +27,9 @@ class UpdateProductWebsiteUrlRewrites
2727
private $urlPersist;
2828

2929
/**
30-
* @var Collection
30+
* @var CollectionFactory
3131
*/
32-
private $productCollection;
32+
private $productCollectionFactory;
3333

3434
/**
3535
* @var AppendUrlRewritesToProducts
@@ -43,18 +43,18 @@ class UpdateProductWebsiteUrlRewrites
4343

4444
/**
4545
* @param UrlPersistInterface $urlPersist
46-
* @param Collection $productCollection
46+
* @param CollectionFactory $productCollectionFactory
4747
* @param AppendUrlRewritesToProducts $appendRewrites
4848
* @param GetStoresListByWebsiteIds $getStoresList
4949
*/
5050
public function __construct(
5151
UrlPersistInterface $urlPersist,
52-
Collection $productCollection,
52+
CollectionFactory $productCollectionFactory,
5353
AppendUrlRewritesToProducts $appendRewrites,
5454
GetStoresListByWebsiteIds $getStoresList
5555
) {
5656
$this->urlPersist = $urlPersist;
57-
$this->productCollection = $productCollection;
57+
$this->productCollectionFactory = $productCollectionFactory;
5858
$this->appendRewrites = $appendRewrites;
5959
$this->getStoresList = $getStoresList;
6060
}
@@ -90,8 +90,9 @@ public function afterUpdateWebsites(
9090
]
9191
);
9292
} else {
93-
$collection = $this->productCollection->addFieldToFilter('entity_id', ['in' => implode(',', $productIds)]);
94-
$this->appendRewrites->execute($collection->getItems(), $storeIds);
93+
$productCollection = $this->productCollectionFactory->create();
94+
$productCollection->addFieldToFilter('entity_id', ['in' => implode(',', $productIds)]);
95+
$this->appendRewrites->execute($productCollection->getItems(), $storeIds);
9596
}
9697
}
9798
}

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Plugin/Catalog/Model/Product/UpdateProductWebsiteUrlRewritesTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\CatalogUrlRewrite\Plugin\Catalog\Model\Product;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
1112
use Magento\Catalog\Api\ProductRepositoryInterface;
1213
use Magento\Catalog\Model\Product\Action;
1314
use Magento\Store\Api\StoreWebsiteRelationInterface;
@@ -69,4 +70,49 @@ public function testUpdateUrlRewrites()
6970
$url
7071
);
7172
}
73+
74+
/**
75+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
76+
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
77+
*/
78+
public function testUpdateUrlRewritesForSecondProduct()
79+
{
80+
/** @var Website $website */
81+
$websiteRepository = Bootstrap::getObjectManager()->get(WebsiteRepository::class);
82+
$website = $websiteRepository->get('test');
83+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
84+
// test the first product
85+
$product = $productRepository->get('simple1', false, null, true);
86+
// store filter condition about the first product in collection
87+
$productCollection = Bootstrap::getObjectManager()->get(ProductCollection::class);
88+
$productCollection->addFieldToFilter('entity_id', $product->getId());
89+
$this->action->updateWebsites([$product->getId()], [$website->getId()], 'add');
90+
$storeIds = $this->storeWebsiteRelation->getStoreByWebsiteId($website->getId());
91+
$this->assertStringContainsString(
92+
$product->getUrlKey() . '.html',
93+
$product->setStoreId(reset($storeIds))->getProductUrl()
94+
);
95+
$this->action->updateWebsites([$product->getId()], [$website->getId()], 'remove');
96+
$product->setRequestPath('');
97+
$url = $product->setStoreId(reset($storeIds))->getProductUrl();
98+
$this->assertStringNotContainsString(
99+
$product->getUrlKey() . '.htmll',
100+
$url
101+
);
102+
// test the second product
103+
$product = $productRepository->get('simple2', false, null, true);
104+
$this->action->updateWebsites([$product->getId()], [$website->getId()], 'add');
105+
$storeIds = $this->storeWebsiteRelation->getStoreByWebsiteId($website->getId());
106+
$this->assertStringContainsString(
107+
$product->getUrlKey() . '.html',
108+
$product->setStoreId(reset($storeIds))->getProductUrl()
109+
);
110+
$this->action->updateWebsites([$product->getId()], [$website->getId()], 'remove');
111+
$product->setRequestPath('');
112+
$url = $product->setStoreId(reset($storeIds))->getProductUrl();
113+
$this->assertStringNotContainsString(
114+
$product->getUrlKey() . '.html',
115+
$url
116+
);
117+
}
72118
}

0 commit comments

Comments
 (0)