Skip to content

Commit 636519d

Browse files
committed
Merge remote-tracking branch 'origin/MC-30331' into 2.4-develop-com-pr3
2 parents b8a23bd + 8bace19 commit 636519d

File tree

8 files changed

+831
-62
lines changed

8 files changed

+831
-62
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Catalog\Model\Layer;
9+
10+
use Magento\Catalog\Model\Layer\SearchFactory;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
13+
/**
14+
* Quick search products by query.
15+
*/
16+
class QuickSearchByQuery
17+
{
18+
/**
19+
* @var SearchFactory
20+
*/
21+
private $searchFactory;
22+
23+
/**
24+
* @param SearchFactory $searchFactory
25+
*/
26+
public function __construct(
27+
SearchFactory $searchFactory
28+
) {
29+
$this->searchFactory = $searchFactory;
30+
}
31+
32+
/**
33+
* Flush search instances cache and find products by search query.
34+
*
35+
* @param string $query
36+
* @param string $sortedField
37+
* @param string $sortOrder
38+
* @return Collection
39+
*/
40+
public function execute(
41+
string $query,
42+
string $sortedField = 'relevance',
43+
string $sortOrder = 'desc'
44+
): Collection {
45+
$productCollection = $this->searchFactory->create()->getProductCollection();
46+
$productCollection->addSearchFilter($query);
47+
$productCollection->setOrder($sortedField, $sortOrder);
48+
49+
return $productCollection;
50+
}
51+
}

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

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@
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\Layer\Search as CatalogLayerSearch;
13-
use Magento\Catalog\Model\Product;
14-
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\CollectionFactory;
15-
use Magento\Framework\Search\Request\Builder;
16-
use Magento\Framework\Search\Request\Config as RequestConfig;
17-
use Magento\Search\Model\Search;
11+
use Magento\TestFramework\Catalog\Model\Layer\QuickSearchByQuery;
1812
use Magento\TestFramework\Helper\Bootstrap;
1913
use Magento\TestFramework\ObjectManager;
2014
use PHPUnit\Framework\TestCase;
@@ -42,9 +36,9 @@ class AttributeSearchWeightTest extends TestCase
4236
private $collectedAttributesWeight = [];
4337

4438
/**
45-
* @var CatalogLayerSearch
39+
* @var QuickSearchByQuery
4640
*/
47-
private $catalogLayerSearch;
41+
private $quickSearchByQuery;
4842

4943
/**
5044
* @inheritdoc
@@ -53,7 +47,7 @@ protected function setUp()
5347
{
5448
$this->objectManager = Bootstrap::getObjectManager();
5549
$this->productAttributeRepository = $this->objectManager->get(ProductAttributeRepositoryInterface::class);
56-
$this->catalogLayerSearch = $this->objectManager->get(CatalogLayerSearch::class);
50+
$this->quickSearchByQuery = $this->objectManager->get(QuickSearchByQuery::class);
5751
$this->collectCurrentProductAttributesWeights();
5852
}
5953

@@ -85,9 +79,7 @@ public function testAttributeSearchWeight(
8579
array $expectedProductNames
8680
): void {
8781
$this->updateAttributesWeight($attributeWeights);
88-
$this->removeInstancesCache();
89-
$products = $this->findProducts($searchQuery);
90-
$actualProductNames = $this->collectProductsName($products);
82+
$actualProductNames = $this->quickSearchByQuery->execute($searchQuery)->getColumnValues('name');
9183
$this->assertEquals($expectedProductNames, $actualProductNames, 'Products order is not as expected.');
9284
}
9385

@@ -164,58 +156,11 @@ protected function updateAttributesWeight(array $attributeWeights): void
164156
{
165157
foreach ($attributeWeights as $attributeCode => $weight) {
166158
$attribute = $this->productAttributeRepository->get($attributeCode);
167-
168-
if ($attribute) {
169-
$attribute->setSearchWeight($weight);
170-
$this->productAttributeRepository->save($attribute);
171-
}
159+
$attribute->setSearchWeight($weight);
160+
$this->productAttributeRepository->save($attribute);
172161
}
173162
}
174163

175-
/**
176-
* Get all names from founded products.
177-
*
178-
* @param Product[] $products
179-
* @return array
180-
*/
181-
protected function collectProductsName(array $products): array
182-
{
183-
$result = [];
184-
foreach ($products as $product) {
185-
$result[] = $product->getName();
186-
}
187-
188-
return $result;
189-
}
190-
191-
/**
192-
* Reindex catalogsearch fulltext index.
193-
*
194-
* @return void
195-
*/
196-
protected function removeInstancesCache(): void
197-
{
198-
$this->objectManager->removeSharedInstance(RequestConfig::class);
199-
$this->objectManager->removeSharedInstance(Builder::class);
200-
$this->objectManager->removeSharedInstance(Search::class);
201-
$this->objectManager->removeSharedInstance(CatalogLayerSearch::class);
202-
}
203-
204-
/**
205-
* Find products by search query.
206-
*
207-
* @param string $query
208-
* @return Product[]
209-
*/
210-
protected function findProducts(string $query): array
211-
{
212-
$testProductCollection = $this->catalogLayerSearch->getProductCollection();
213-
$testProductCollection->addSearchFilter($query);
214-
$testProductCollection->setOrder('relevance', 'desc');
215-
216-
return $testProductCollection->getItems();
217-
}
218-
219164
/**
220165
* Collect weight of attributes which use in test.
221166
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableProduct\Block\Product\View\Type;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Block\Product\View;
12+
use Magento\Catalog\Model\Product\Visibility;
13+
use Magento\ConfigurableProduct\Helper\Data;
14+
use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
15+
use Magento\Framework\Registry;
16+
use Magento\Framework\Serialize\Serializer\Json;
17+
use Magento\Framework\View\Result\Page;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
use Magento\TestFramework\ObjectManager;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* Test cases related to render configurable options.
24+
*
25+
* @magentoAppArea frontend
26+
* @magentoDbIsolation enabled
27+
*/
28+
class RenderConfigurableOptionsTest extends TestCase
29+
{
30+
/**
31+
* @var ObjectManager
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* @var Data
37+
*/
38+
private $configurableHelper;
39+
40+
/**
41+
* @var ProductRepositoryInterface
42+
*/
43+
private $productRepository;
44+
45+
/**
46+
* @var ConfigurableAttributeData
47+
*/
48+
private $configurableAttributeData;
49+
50+
/**
51+
* @var Registry
52+
*/
53+
private $registry;
54+
55+
/**
56+
* @var Page
57+
*/
58+
private $page;
59+
60+
/**
61+
* @var Json
62+
*/
63+
private $json;
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
protected function setUp()
69+
{
70+
$this->objectManager = Bootstrap::getObjectManager();
71+
$this->configurableHelper = $this->objectManager->get(Data::class);
72+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
73+
$this->configurableAttributeData = $this->objectManager->get(ConfigurableAttributeData::class);
74+
$this->registry = $this->objectManager->get(Registry::class);
75+
$this->page = $this->objectManager->create(Page::class);
76+
$this->json = $this->objectManager->get(Json::class);
77+
parent::setUp();
78+
}
79+
80+
/**
81+
* Assert that all configurable options was rendered correctly if one of
82+
* child product is visible on catalog\search.
83+
*
84+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
85+
*
86+
* @return void
87+
*/
88+
public function testRenderConfigurableOptionsBlockWithOneVisibleOption(): void
89+
{
90+
$configurableProduct = $this->productRepository->get('Configurable product');
91+
$childProduct = $this->productRepository->get('Simple option 1');
92+
$childProduct->setVisibility(Visibility::VISIBILITY_BOTH);
93+
$this->productRepository->save($childProduct);
94+
$allProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct, null);
95+
$options = $this->configurableHelper->getOptions($configurableProduct, $allProducts);
96+
$confAttrData = $this->configurableAttributeData->getAttributesData($configurableProduct, $options);
97+
$attributesJson = str_replace(
98+
['[', ']'],
99+
['\[', '\]'],
100+
$this->json->serialize($confAttrData['attributes'])
101+
);
102+
$optionsHtml = $this->getConfigurableOptionsHtml('Configurable product');
103+
$this->assertRegExp("/\"spConfig\": {\"attributes\":{$attributesJson}/", $optionsHtml);
104+
}
105+
106+
/**
107+
* Render configurable options block.
108+
*
109+
* @param string $configurableSku
110+
* @return string
111+
*/
112+
private function getConfigurableOptionsHtml(string $configurableSku): string
113+
{
114+
$product = $this->productRepository->get($configurableSku);
115+
$this->registry->unregister('product');
116+
$this->registry->register('product', $product);
117+
$optionsBlock = $this->getOptionsWrapperBlockWithOnlyConfigurableBlock();
118+
$optionHtml = $optionsBlock->toHtml();
119+
$this->registry->unregister('product');
120+
121+
return $optionHtml;
122+
}
123+
124+
/**
125+
* Get options wrapper without extra blocks(only configurable child block).
126+
*
127+
* @return View
128+
*/
129+
private function getOptionsWrapperBlockWithOnlyConfigurableBlock(): View
130+
{
131+
$this->page->addHandle([
132+
'default',
133+
'catalog_product_view',
134+
'catalog_product_view_type_configurable',
135+
]);
136+
$this->page->getLayout()->generateXml();
137+
138+
/** @var View $productInfoOptionsWrapper */
139+
$productInfoOptionsWrapper = $this->page->getLayout()->getBlock('product.info.options.wrapper');
140+
$productInfoOptionsWrapper->unsetChild('product_options');
141+
$productInfoOptionsWrapper->unsetChild('html_calendar');
142+
143+
return $productInfoOptionsWrapper;
144+
}
145+
}

0 commit comments

Comments
 (0)