Skip to content

Commit 2620e4a

Browse files
committed
MC-29270: Storefront: Visible/Not-Visible child products of configurable product on storefront
1 parent 84fe1f9 commit 2620e4a

File tree

5 files changed

+265
-66
lines changed

5 files changed

+265
-66
lines changed

dev/tests/integration/framework/Magento/TestFramework/Catalog/Model/Layer/QuickSearchByQuery.php

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,41 @@
77

88
namespace Magento\TestFramework\Catalog\Model\Layer;
99

10-
use Magento\Catalog\Model\Layer\Search as CatalogLayerSearch;
10+
use Magento\Catalog\Model\Layer\SearchFactory;
1111
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12-
use Magento\Framework\ObjectManagerInterface;
13-
use Magento\Framework\Search\Request\Builder;
14-
use Magento\Framework\Search\Request\Config as RequestConfig;
15-
use Magento\Search\Model\Search;
1612

1713
/**
1814
* Quick search products by query.
1915
*/
2016
class QuickSearchByQuery
2117
{
2218
/**
23-
* @var ObjectManager
19+
* @var SearchFactory
2420
*/
25-
private $objectManager;
21+
private $searchFactory;
2622

2723
/**
28-
* @param ObjectManagerInterface $objectManager
24+
* @param SearchFactory $searchFactory
2925
*/
3026
public function __construct(
31-
ObjectManagerInterface $objectManager
27+
SearchFactory $searchFactory
3228
) {
33-
$this->objectManager = $objectManager;
29+
$this->searchFactory = $searchFactory;
3430
}
3531

3632
/**
3733
* Flush search instances cache and find products by search query.
3834
*
3935
* @param string $query
36+
* @param string $sortOrder
4037
* @return Collection
4138
*/
42-
public function execute(string $query): Collection
39+
public function execute(string $query, string $sortOrder = 'desc'): Collection
4340
{
44-
$this->removeInstancesCache();
45-
$productCollection = $this->getCatalogLayerSearch()->getProductCollection();
41+
$productCollection = $this->searchFactory->create()->getProductCollection();
4642
$productCollection->addSearchFilter($query);
47-
$productCollection->setOrder('relevance', 'desc');
43+
$productCollection->setOrder('relevance', $sortOrder);
4844

4945
return $productCollection;
5046
}
51-
52-
/**
53-
* Retrieve empty catalog layer search instance.
54-
*
55-
* @return CatalogLayerSearch
56-
*/
57-
private function getCatalogLayerSearch(): CatalogLayerSearch
58-
{
59-
return $this->objectManager->create(CatalogLayerSearch::class);
60-
}
61-
62-
/**
63-
* Remove instances cache which related to search.
64-
*
65-
* @return void
66-
*/
67-
private function removeInstancesCache(): void
68-
{
69-
$this->objectManager->removeSharedInstance(RequestConfig::class);
70-
$this->objectManager->removeSharedInstance(Builder::class);
71-
$this->objectManager->removeSharedInstance(Search::class);
72-
}
7347
}

dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Search/AttributeSearchWeightTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
declare(strict_types=1);
87

98
namespace Magento\CatalogSearch\Model\Search;
109

1110
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
12-
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1312
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\CollectionFactory;
1413
use Magento\TestFramework\Catalog\Model\Layer\QuickSearchByQuery;
1514
use Magento\TestFramework\Helper\Bootstrap;
1615
use Magento\TestFramework\ObjectManager;
17-
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1816
use PHPUnit\Framework\TestCase;
1917

2018
/**

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/FindByUrlRewriteTest.php

Lines changed: 179 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product\Visibility;
12+
use Magento\Store\Model\StoreManagerInterface;
1213
use Magento\TestFramework\Helper\Bootstrap;
1314
use Magento\TestFramework\ObjectManager;
15+
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
16+
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory;
1417
use Magento\UrlRewrite\Model\UrlFinderInterface;
18+
use Magento\UrlRewrite\Model\UrlRewrite as UrlRewriteItem;
1519
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1620
use PHPUnit\Framework\TestCase;
1721

@@ -30,11 +34,21 @@ class FindByUrlRewriteTest extends TestCase
3034
*/
3135
private $urlFinder;
3236

37+
/**
38+
* @var StoreManagerInterface
39+
*/
40+
private $storeManager;
41+
3342
/**
3443
* @var ProductRepositoryInterface
3544
*/
3645
private $productRepository;
3746

47+
/**
48+
* @var UrlRewriteCollectionFactory
49+
*/
50+
private $urlRewriteCollectionFactory;
51+
3852
/**
3953
* @inheritdoc
4054
*/
@@ -43,42 +57,191 @@ protected function setUp()
4357
$this->objectManger = Bootstrap::getObjectManager();
4458
$this->urlFinder = $this->objectManger->get(UrlFinderInterface::class);
4559
$this->productRepository = $this->objectManger->get(ProductRepositoryInterface::class);
60+
$this->storeManager = $this->objectManger->get(StoreManagerInterface::class);
61+
$this->urlRewriteCollectionFactory = $this->objectManger->get(UrlRewriteCollectionFactory::class);
4662
parent::setUp();
4763
}
4864

4965
/**
50-
* Assert that URL rewrite for child product of configurable was not created.
66+
* Assert that product is available by URL rewrite with different visibility.
5167
*
5268
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
69+
* @dataProvider visibilityWithExpectedResultDataProvider
5370
* @magentoDbIsolation enabled
5471
*
72+
* @param array $productsData
5573
* @return void
5674
*/
57-
public function testCheckUrlRewriteForChildWasNotCreated(): void
75+
public function testCheckIsUrlRewriteForChildrenProductsAreCreated(array $productsData): void
5876
{
5977
$this->checkConfigurableUrlRewriteWasCreated();
60-
$this->assertNull($this->urlFinder->findOneByData([UrlRewrite::REQUEST_PATH => 'configurable-option-1.html']));
61-
$this->assertNull($this->urlFinder->findOneByData([UrlRewrite::REQUEST_PATH => 'configurable-option-2.html']));
78+
$this->updateProductsVisibility($productsData);
79+
$urlRewritesCollection = $this->getUrlRewritesCollectionByPaths([
80+
'configurable-option-1.html',
81+
'configurable-option-2.html',
82+
]);
83+
$expectedCount = 0;
84+
foreach ($productsData as $productData) {
85+
$product = $this->productRepository->get($productData['sku']);
86+
/** @var UrlRewriteItem $urlRewrite */
87+
$urlRewrite = $urlRewritesCollection->getItemByColumnValue(
88+
UrlRewrite::TARGET_PATH,
89+
"catalog/product/view/id/{$product->getId()}"
90+
);
91+
if ($productData['url_rewrite_created']) {
92+
$this->assertNotNull($urlRewrite);
93+
$this->assertEquals($product->getId(), $urlRewrite->getEntityId());
94+
$this->assertEquals('product', $urlRewrite->getEntityType());
95+
$expectedCount++;
96+
} else {
97+
$this->assertNull($urlRewrite);
98+
}
99+
}
100+
$this->assertCount($expectedCount, $urlRewritesCollection);
62101
}
63102

64103
/**
65-
* Assert that URL rewrite for one of child product of configurable was created.
104+
* Return products visibility, expected result and other product additional data.
66105
*
67-
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
68-
* @magentoDbIsolation enabled
106+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
69107
*
108+
* @return array
109+
*/
110+
public function visibilityWithExpectedResultDataProvider(): array
111+
{
112+
return [
113+
'visibility_for_both_product_only_catalog' => [
114+
[
115+
[
116+
'sku' => 'Simple option 1',
117+
'visibility' => Visibility::VISIBILITY_IN_CATALOG,
118+
'url_rewrite_created' => true,
119+
],
120+
[
121+
'sku' => 'Simple option 2',
122+
'visibility' => Visibility::VISIBILITY_IN_CATALOG,
123+
'url_rewrite_created' => true,
124+
],
125+
],
126+
],
127+
'visibility_for_both_product_catalog_search' => [
128+
[
129+
[
130+
'sku' => 'Simple option 1',
131+
'visibility' => Visibility::VISIBILITY_BOTH,
132+
'url_rewrite_created' => true,
133+
],
134+
[
135+
'sku' => 'Simple option 2',
136+
'visibility' => Visibility::VISIBILITY_BOTH,
137+
'url_rewrite_created' => true,
138+
],
139+
],
140+
],
141+
'visibility_for_both_product_only_search' => [
142+
[
143+
[
144+
'sku' => 'Simple option 1',
145+
'visibility' => Visibility::VISIBILITY_IN_SEARCH,
146+
'url_rewrite_created' => true,
147+
],
148+
[
149+
'sku' => 'Simple option 2',
150+
'visibility' => Visibility::VISIBILITY_IN_SEARCH,
151+
'url_rewrite_created' => true,
152+
],
153+
],
154+
],
155+
'visibility_for_both_product_not_visible_individuality' => [
156+
[
157+
[
158+
'sku' => 'Simple option 1',
159+
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
160+
'url_rewrite_created' => false,
161+
],
162+
[
163+
'sku' => 'Simple option 2',
164+
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
165+
'url_rewrite_created' => false,
166+
],
167+
],
168+
],
169+
'visibility_for_one_product_only_catalog' => [
170+
[
171+
[
172+
'sku' => 'Simple option 1',
173+
'visibility' => Visibility::VISIBILITY_IN_CATALOG,
174+
'url_rewrite_created' => true,
175+
],
176+
[
177+
'sku' => 'Simple option 2',
178+
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
179+
'url_rewrite_created' => false,
180+
],
181+
],
182+
],
183+
'visibility_for_one_product_catalog_search' => [
184+
[
185+
[
186+
'sku' => 'Simple option 1',
187+
'visibility' => Visibility::VISIBILITY_BOTH,
188+
'url_rewrite_created' => true,
189+
],
190+
[
191+
'sku' => 'Simple option 2',
192+
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
193+
'url_rewrite_created' => false,
194+
],
195+
],
196+
],
197+
'visibility_for_one_product_only_search' => [
198+
[
199+
[
200+
'sku' => 'Simple option 1',
201+
'visibility' => Visibility::VISIBILITY_IN_SEARCH,
202+
'url_rewrite_created' => true,
203+
],
204+
[
205+
'sku' => 'Simple option 2',
206+
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
207+
'url_rewrite_created' => false,
208+
],
209+
],
210+
],
211+
];
212+
}
213+
214+
/**
215+
* Update products visibility.
216+
*
217+
* @param array $productsData
70218
* @return void
71219
*/
72-
public function testCheckUrlRewriteForOneOfChildWasCreated(): void
220+
private function updateProductsVisibility(array $productsData): void
73221
{
74-
$this->checkConfigurableUrlRewriteWasCreated();
75-
$childProduct = $this->productRepository->get('Simple option 1');
76-
$childProduct->setVisibility(Visibility::VISIBILITY_BOTH);
77-
$this->productRepository->save($childProduct);
78-
$childUrlRewrite = $this->urlFinder->findOneByData([UrlRewrite::REQUEST_PATH => 'configurable-option-1.html']);
79-
$this->assertNotNull($childUrlRewrite);
80-
$this->assertEquals($childUrlRewrite->getTargetPath(), "catalog/product/view/id/{$childProduct->getId()}");
81-
$this->assertNull($this->urlFinder->findOneByData([UrlRewrite::REQUEST_PATH => 'configurable-option-2.html']));
222+
foreach ($productsData as $productData) {
223+
$product = $this->productRepository->get($productData['sku']);
224+
$product->setVisibility($productData['visibility']);
225+
$this->productRepository->save($product);
226+
}
227+
}
228+
229+
/**
230+
* Get URL rewrite collection by requested products paths.
231+
*
232+
* @param array $requestPaths
233+
* @param string $storeCode
234+
* @return UrlRewriteCollection
235+
*/
236+
private function getUrlRewritesCollectionByPaths(
237+
array $requestPaths,
238+
string $storeCode = 'default'
239+
): UrlRewriteCollection {
240+
$collection = $this->urlRewriteCollectionFactory->create();
241+
$collection->addStoreFilter($storeCode);
242+
$collection->addFieldToFilter(UrlRewrite::REQUEST_PATH, ['in' => $requestPaths]);
243+
244+
return $collection;
82245
}
83246

84247
/**

0 commit comments

Comments
 (0)