Skip to content

Commit d04ea95

Browse files
committed
MC-29270: Storefront: Visible/Not-Visible child products of configurable product on storefront
1 parent 4acc3a5 commit d04ea95

File tree

6 files changed

+123
-104
lines changed

6 files changed

+123
-104
lines changed

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
namespace Magento\CatalogSearch\Model\Search;
99

1010
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
11-
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12-
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\CollectionFactory;
1311
use Magento\TestFramework\Catalog\Model\Layer\QuickSearchByQuery;
1412
use Magento\TestFramework\Helper\Bootstrap;
1513
use Magento\TestFramework\ObjectManager;
@@ -81,7 +79,7 @@ public function testAttributeSearchWeight(
8179
array $expectedProductNames
8280
): void {
8381
$this->updateAttributesWeight($attributeWeights);
84-
$actualProductNames = $this->collectProductsName($this->quickSearchByQuery->execute($searchQuery));
82+
$actualProductNames = $this->quickSearchByQuery->execute($searchQuery)->getColumnValues('name');
8583
$this->assertEquals($expectedProductNames, $actualProductNames, 'Products order is not as expected.');
8684
}
8785

@@ -158,30 +156,11 @@ protected function updateAttributesWeight(array $attributeWeights): void
158156
{
159157
foreach ($attributeWeights as $attributeCode => $weight) {
160158
$attribute = $this->productAttributeRepository->get($attributeCode);
161-
162-
if ($attribute) {
163-
$attribute->setSearchWeight($weight);
164-
$this->productAttributeRepository->save($attribute);
165-
}
159+
$attribute->setSearchWeight($weight);
160+
$this->productAttributeRepository->save($attribute);
166161
}
167162
}
168163

169-
/**
170-
* Get all names from founded products.
171-
*
172-
* @param Collection $productsCollection
173-
* @return array
174-
*/
175-
protected function collectProductsName(Collection $productsCollection): array
176-
{
177-
$result = [];
178-
foreach ($productsCollection as $product) {
179-
$result[] = $product->getName();
180-
}
181-
182-
return $result;
183-
}
184-
185164
/**
186165
* Collect weight of attributes which use in test.
187166
*

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Block/Product/View/Type/RenderConfigurableOptionsTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use Magento\Catalog\Model\Product\Visibility;
1313
use Magento\ConfigurableProduct\Helper\Data;
1414
use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
15-
use Magento\Framework\Json\EncoderInterface;
1615
use Magento\Framework\Registry;
16+
use Magento\Framework\Serialize\Serializer\Json;
1717
use Magento\Framework\View\Result\Page;
1818
use Magento\TestFramework\Helper\Bootstrap;
1919
use Magento\TestFramework\ObjectManager;
@@ -32,11 +32,6 @@ class RenderConfigurableOptionsTest extends TestCase
3232
*/
3333
private $objectManager;
3434

35-
/**
36-
* @var EncoderInterface
37-
*/
38-
private $jsonEncoder;
39-
4035
/**
4136
* @var Data
4237
*/
@@ -62,18 +57,23 @@ class RenderConfigurableOptionsTest extends TestCase
6257
*/
6358
private $page;
6459

60+
/**
61+
* @var Json
62+
*/
63+
private $json;
64+
6565
/**
6666
* @inheritdoc
6767
*/
6868
protected function setUp()
6969
{
7070
$this->objectManager = Bootstrap::getObjectManager();
71-
$this->jsonEncoder = $this->objectManager->get(EncoderInterface::class);
7271
$this->configurableHelper = $this->objectManager->get(Data::class);
7372
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
7473
$this->configurableAttributeData = $this->objectManager->get(ConfigurableAttributeData::class);
7574
$this->registry = $this->objectManager->get(Registry::class);
7675
$this->page = $this->objectManager->create(Page::class);
76+
$this->json = $this->objectManager->get(Json::class);
7777
parent::setUp();
7878
}
7979

@@ -97,7 +97,7 @@ public function testRenderConfigurableOptionsBlockWithOneVisibleOption(): void
9797
$attributesJson = str_replace(
9898
['[', ']'],
9999
['\[', '\]'],
100-
$this->jsonEncoder->encode($confAttrData['attributes'])
100+
$this->json->serialize($confAttrData['attributes'])
101101
);
102102
$optionsHtml = $this->getConfigurableOptionsHtml('Configurable product');
103103
$this->assertRegExp("/\"spConfig\": {\"attributes\":{$attributesJson}/", $optionsHtml);

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product\Visibility;
12-
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1313
use Magento\TestFramework\Helper\Bootstrap;
1414
use Magento\TestFramework\ObjectManager;
1515
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
@@ -35,9 +35,9 @@ class FindByUrlRewriteTest extends TestCase
3535
private $urlFinder;
3636

3737
/**
38-
* @var StoreManagerInterface
38+
* @var ProductResource
3939
*/
40-
private $storeManager;
40+
private $productResource;
4141

4242
/**
4343
* @var ProductRepositoryInterface
@@ -56,8 +56,8 @@ protected function setUp()
5656
{
5757
$this->objectManger = Bootstrap::getObjectManager();
5858
$this->urlFinder = $this->objectManger->get(UrlFinderInterface::class);
59+
$this->productResource = $this->objectManger->get(ProductResource::class);
5960
$this->productRepository = $this->objectManger->get(ProductRepositoryInterface::class);
60-
$this->storeManager = $this->objectManger->get(StoreManagerInterface::class);
6161
$this->urlRewriteCollectionFactory = $this->objectManger->get(UrlRewriteCollectionFactory::class);
6262
parent::setUp();
6363
}
@@ -72,25 +72,23 @@ protected function setUp()
7272
* @param array $productsData
7373
* @return void
7474
*/
75-
public function testCheckIsUrlRewriteForChildrenProductsAreCreated(array $productsData): void
75+
public function testCheckIsUrlRewriteForChildrenProductsHasCreated(array $productsData): void
7676
{
7777
$this->checkConfigurableUrlRewriteWasCreated();
7878
$this->updateProductsVisibility($productsData);
79-
$urlRewritesCollection = $this->getUrlRewritesCollectionByPaths([
80-
'configurable-option-1.html',
81-
'configurable-option-2.html',
82-
]);
79+
$productIdsBySkus = $this->getProductIdsBySkus($productsData);
80+
$urlRewritesCollection = $this->getUrlRewritesCollectionByProductIds($productIdsBySkus);
8381
$expectedCount = 0;
8482
foreach ($productsData as $productData) {
85-
$product = $this->productRepository->get($productData['sku']);
83+
$productId = $productIdsBySkus[$productData['sku']];
8684
/** @var UrlRewriteItem $urlRewrite */
8785
$urlRewrite = $urlRewritesCollection->getItemByColumnValue(
8886
UrlRewrite::TARGET_PATH,
89-
"catalog/product/view/id/{$product->getId()}"
87+
"catalog/product/view/id/{$productId}"
9088
);
9189
if ($productData['url_rewrite_created']) {
9290
$this->assertNotNull($urlRewrite);
93-
$this->assertEquals($product->getId(), $urlRewrite->getEntityId());
91+
$this->assertEquals($productId, $urlRewrite->getEntityId());
9492
$this->assertEquals('product', $urlRewrite->getEntityType());
9593
$expectedCount++;
9694
} else {
@@ -227,19 +225,20 @@ private function updateProductsVisibility(array $productsData): void
227225
}
228226

229227
/**
230-
* Get URL rewrite collection by requested products paths.
228+
* Get URL rewrite collection by product ids.
231229
*
232-
* @param array $requestPaths
230+
* @param int[] $productIds
233231
* @param string $storeCode
234232
* @return UrlRewriteCollection
235233
*/
236-
private function getUrlRewritesCollectionByPaths(
237-
array $requestPaths,
234+
private function getUrlRewritesCollectionByProductIds(
235+
array $productIds,
238236
string $storeCode = 'default'
239237
): UrlRewriteCollection {
240238
$collection = $this->urlRewriteCollectionFactory->create();
241239
$collection->addStoreFilter($storeCode);
242-
$collection->addFieldToFilter(UrlRewrite::REQUEST_PATH, ['in' => $requestPaths]);
240+
$collection->addFieldToFilter(UrlRewrite::ENTITY_TYPE, ['eq' => 'product']);
241+
$collection->addFieldToFilter(UrlRewrite::ENTITY_ID, ['in' => $productIds]);
243242

244243
return $collection;
245244
}
@@ -254,7 +253,8 @@ private function checkConfigurableUrlRewriteWasCreated(): void
254253
$configurableProduct = $this->productRepository->get('Configurable product');
255254
$configurableUrlRewrite = $this->urlFinder->findOneByData(
256255
[
257-
UrlRewrite::REQUEST_PATH => 'configurable-product-with-two-child.html'
256+
UrlRewrite::ENTITY_TYPE => 'product',
257+
UrlRewrite::ENTITY_ID => $configurableProduct->getId(),
258258
]
259259
);
260260
$this->assertNotNull($configurableUrlRewrite);
@@ -263,4 +263,17 @@ private function checkConfigurableUrlRewriteWasCreated(): void
263263
"catalog/product/view/id/{$configurableProduct->getId()}"
264264
);
265265
}
266+
267+
/**
268+
* Load all product ids by skus.
269+
*
270+
* @param array $productsData
271+
* @return array
272+
*/
273+
private function getProductIdsBySkus(array $productsData): array
274+
{
275+
$skus = array_column($productsData, 'sku');
276+
277+
return $this->productResource->getProductsIdsBySkus($skus);
278+
}
266279
}

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

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHPUnit\Framework\TestCase;
1717

1818
/**
19-
* Test cases related to find configurable product via quick search.
19+
* Test cases related to find configurable product via quick search using mysql search engine.
2020
*
2121
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
2222
* @magentoDataFixture Magento/CatalogSearch/_files/full_reindex.php
@@ -59,19 +59,7 @@ protected function setUp()
5959
*
6060
* @return void
6161
*/
62-
public function testChildProductsHasNotFoundedByQueryUsingMysql(): void
63-
{
64-
$this->checkThatOnlyConfigurableProductIsAvailableBySearch('Configurable Option');
65-
}
66-
67-
/**
68-
* Assert that configurable child products has not found by query using Elasticsearch 6.0+ search engine.
69-
*
70-
* @magentoConfigFixture default/catalog/search/engine elasticsearch6
71-
*
72-
* @return void
73-
*/
74-
public function testChildProductsHasNotFoundedByQueryUsingElasticsearch(): void
62+
public function testChildProductsHasNotFoundedByQuery(): void
7563
{
7664
$this->checkThatOnlyConfigurableProductIsAvailableBySearch('Configurable Option');
7765
}
@@ -87,26 +75,7 @@ public function testChildProductsHasNotFoundedByQueryUsingElasticsearch(): void
8775
* @param bool $expectedResult
8876
* @return void
8977
*/
90-
public function testOneOfChildIsAvailableBySearchUsingMysql(int $visibility, bool $expectedResult): void
91-
{
92-
$this->checkThatOnlyConfigurableProductIsAvailableBySearch('Configurable Option');
93-
$this->updateProductVisibility($visibility);
94-
$this->checkProductAvailabilityInSearch($expectedResult);
95-
$this->checkThatOnlyConfigurableProductIsAvailableBySearch('White');
96-
}
97-
98-
/**
99-
* Assert that child product of configurable will be available by search after
100-
* set to product visibility by catalog and search using Elasticsearch 6.0+ search engine.
101-
*
102-
* @magentoConfigFixture default/catalog/search/engine elasticsearch6
103-
* @dataProvider productAvailabilityInSearchByVisibilityDataProvider
104-
*
105-
* @param int $visibility
106-
* @param bool $expectedResult
107-
* @return void
108-
*/
109-
public function testOneOfChildIsAvailableBySearchUsingElasticsearch(int $visibility, bool $expectedResult): void
78+
public function testOneOfChildIsAvailableBySearch(int $visibility, bool $expectedResult): void
11079
{
11180
$this->checkThatOnlyConfigurableProductIsAvailableBySearch('Configurable Option');
11281
$this->updateProductVisibility($visibility);
@@ -148,19 +117,7 @@ public function productAvailabilityInSearchByVisibilityDataProvider(): array
148117
*
149118
* @return void
150119
*/
151-
public function testSearchByOptionValueUsingMysql(): void
152-
{
153-
$this->checkThatOnlyConfigurableProductIsAvailableBySearch('Option 1');
154-
}
155-
156-
/**
157-
* Assert that configurable product was found by option value using Elasticsearch 6.0+ search engine.
158-
*
159-
* @magentoConfigFixture default/catalog/search/engine elasticsearch6
160-
*
161-
* @return void
162-
*/
163-
public function testSearchByOptionValueUsingElasticsearch(): void
120+
public function testSearchByOptionValue(): void
164121
{
165122
$this->checkThatOnlyConfigurableProductIsAvailableBySearch('Option 1');
166123
}
@@ -172,7 +129,7 @@ public function testSearchByOptionValueUsingElasticsearch(): void
172129
*
173130
* @return void
174131
*/
175-
private function checkThatOnlyConfigurableProductIsAvailableBySearch(string $searchQuery): void
132+
protected function checkThatOnlyConfigurableProductIsAvailableBySearch(string $searchQuery): void
176133
{
177134
$searchResult = $this->quickSearchByQuery->execute($searchQuery);
178135
$this->assertCount(1, $searchResult->getItems());
@@ -187,7 +144,7 @@ private function checkThatOnlyConfigurableProductIsAvailableBySearch(string $sea
187144
* @param int $visibility
188145
* @return void
189146
*/
190-
private function updateProductVisibility(int $visibility): void
147+
protected function updateProductVisibility(int $visibility): void
191148
{
192149
$childProduct = $this->productRepository->get('Simple option 1');
193150
$childProduct->setVisibility($visibility);
@@ -200,7 +157,7 @@ private function updateProductVisibility(int $visibility): void
200157
* @param bool $firstChildIsVisible
201158
* @return void
202159
*/
203-
private function checkProductAvailabilityInSearch(bool $firstChildIsVisible): void
160+
protected function checkProductAvailabilityInSearch(bool $firstChildIsVisible): void
204161
{
205162
$searchResult = $this->quickSearchByQuery->execute('Black');
206163
$this->assertNotNull($searchResult->getItemByColumnValue(Product::SKU, 'Configurable product'));

dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;
99
use Magento\Catalog\Api\ProductRepositoryInterface;
10-
use Magento\Catalog\Model\Product;
1110
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1211
use Magento\Catalog\Model\Product\Type;
1312
use Magento\Catalog\Model\Product\Visibility;
13+
use Magento\Catalog\Model\ProductFactory;
1414
use Magento\CatalogInventory\Model\Stock\ItemFactory;
1515
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
1616
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
@@ -27,6 +27,8 @@
2727
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
2828
/** @var ProductExtensionInterfaceFactory $productExtensionFactory */
2929
$productExtensionFactory = $objectManager->get(ProductExtensionInterfaceFactory::class);
30+
/** @var ProductFactory $productFactory */
31+
$productFactory = $objectManager->get(ProductFactory::class);
3032
/** @var WebsiteRepositoryInterface $websiteRepository */
3133
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
3234
$baseWebsite = $websiteRepository->get('base');
@@ -42,8 +44,7 @@
4244
continue;
4345
}
4446
[$productSku, $productPrice, $productDescription] = array_shift($simpleProductsData);
45-
/** @var Product $product */
46-
$product = $objectManager->create(Product::class);
47+
$product = $productFactory->create();
4748
$product->isObjectNew(true);
4849
$product->setTypeId(Type::TYPE_SIMPLE)
4950
->setAttributeSetId($product->getDefaultAttributeSetId())
@@ -64,7 +65,7 @@
6465
];
6566
$associatedProductIds[] = $product->getId();
6667
}
67-
$product = $objectManager->create(Product::class);
68+
$product = $productFactory->create();
6869
$product->isObjectNew(true);
6970
$product->setTypeId(Configurable::TYPE_CODE)
7071
->setAttributeSetId($product->getDefaultAttributeSetId())

0 commit comments

Comments
 (0)