|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\CatalogSearch\Model\Search; |
| 10 | + |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
| 14 | +use Magento\Eav\Api\AttributeRepositoryInterface; |
| 15 | +use Magento\Elasticsearch\SearchAdapter\ConnectionManager; |
| 16 | +use Magento\Elasticsearch6\Model\Client\Elasticsearch as ElasticsearchClient; |
| 17 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 18 | +use Magento\Framework\Exception\StateException; |
| 19 | +use Magento\Framework\Search\Request\Builder; |
| 20 | +use Magento\Framework\Search\Request\Config as RequestConfig; |
| 21 | +use Magento\Framework\Search\Response\QueryResponse; |
| 22 | +use Magento\Framework\Search\SearchEngineInterface; |
| 23 | +use Magento\Indexer\Model\Indexer; |
| 24 | +use Magento\TestFramework\Helper\Bootstrap; |
| 25 | +use Magento\TestFramework\Helper\CacheCleaner; |
| 26 | +use Magento\TestFramework\ObjectManager; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test for name over sku search weight of product attributes |
| 31 | + * |
| 32 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 33 | + * @magentoAppIsolation enabled |
| 34 | + * @magentoDbIsolation enabled |
| 35 | + */ |
| 36 | +class AttributeSearchWeightTest extends TestCase |
| 37 | +{ |
| 38 | + |
| 39 | + /** @var $objectManager ObjectManager */ |
| 40 | + private $objectManager; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ConnectionManager |
| 44 | + */ |
| 45 | + private $connectionManager; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var ElasticsearchClient |
| 49 | + */ |
| 50 | + private $client; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var ProductRepositoryInterface |
| 54 | + */ |
| 55 | + private $productRepository; |
| 56 | + |
| 57 | + protected function setUp() |
| 58 | + { |
| 59 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 60 | + $this->connectionManager = $this->objectManager->create(ConnectionManager::class); |
| 61 | + $this->client = $this->connectionManager->getConnection(); |
| 62 | + $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param string $attributeName |
| 67 | + * @param int $searchWeight |
| 68 | + * @throws NoSuchEntityException |
| 69 | + * @throws StateException |
| 70 | + */ |
| 71 | + private function setAttributeSearchWeight(string $attributeName, int $searchWeight) |
| 72 | + { |
| 73 | + /** @var AttributeRepositoryInterface $attributeRepository */ |
| 74 | + $attributeRepository = $this->objectManager->create(AttributeRepositoryInterface::class); |
| 75 | + |
| 76 | + /** @var Attribute $attribute */ |
| 77 | + $attribute = $attributeRepository->get('catalog_product', $attributeName); |
| 78 | + |
| 79 | + if ($attribute) { |
| 80 | + $attribute->setSearchWeight($searchWeight); |
| 81 | + $attributeRepository->save($attribute); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @throws \Throwable |
| 87 | + */ |
| 88 | + private function reindex() |
| 89 | + { |
| 90 | + CacheCleaner::cleanAll(); |
| 91 | + |
| 92 | + /** @var Indexer $indexer */ |
| 93 | + $indexer = $this->objectManager->create(Indexer::class); |
| 94 | + $indexer->load('catalogsearch_fulltext'); |
| 95 | + $indexer->reindexAll(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param string $query |
| 100 | + * @return array |
| 101 | + * @throws NoSuchEntityException |
| 102 | + */ |
| 103 | + private function findProducts(string $query): array |
| 104 | + { |
| 105 | + $config = $this->objectManager->create(RequestConfig::class); |
| 106 | + |
| 107 | + /** @var Builder $requestBuilder */ |
| 108 | + $requestBuilder = $this->objectManager->create( |
| 109 | + Builder::class, |
| 110 | + ['config' => $config] |
| 111 | + ); |
| 112 | + $requestBuilder->bind('search_term', $query); |
| 113 | + $requestBuilder->setRequestName('quick_search_container'); |
| 114 | + |
| 115 | + /** @var QueryResponse $searchResult */ |
| 116 | + $searchResults = $this->objectManager->create(SearchEngineInterface::class) |
| 117 | + ->search($requestBuilder->create()); |
| 118 | + |
| 119 | + $products = []; |
| 120 | + foreach ($searchResults as $searchResult) { |
| 121 | + $products [] = $this->productRepository->getById($searchResult->getId()); |
| 122 | + } |
| 123 | + |
| 124 | + return $products; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @dataProvider skuOverNameAttributeSearchWeightDataProvider |
| 129 | + * @magentoConfigFixture default/catalog/search/engine elasticsearch6 |
| 130 | + * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix composite_product_search |
| 131 | + * @magentoDataFixture Magento/CatalogSearch/_files/products_for_sku_search_weight_score.php |
| 132 | + * @param string $searchQuery |
| 133 | + * @param int $skuSearchWeight |
| 134 | + * @param int $nameSearchWeight |
| 135 | + * @param string $firstMatchProductName |
| 136 | + * @param string $secondMatchProductName |
| 137 | + * @throws NoSuchEntityException |
| 138 | + * @throws \Throwable |
| 139 | + */ |
| 140 | + public function testSkuOverNameAttributeSearchWeight( |
| 141 | + string $searchQuery, |
| 142 | + int $skuSearchWeight, |
| 143 | + int $nameSearchWeight, |
| 144 | + string $firstMatchProductName, |
| 145 | + string $secondMatchProductName |
| 146 | + ) { |
| 147 | + $this->setAttributeSearchWeight('sku', $skuSearchWeight); |
| 148 | + $this->setAttributeSearchWeight('name', $nameSearchWeight); |
| 149 | + $this->reindex(); |
| 150 | + |
| 151 | + /** @var Product $products [] */ |
| 152 | + $products = $this->findProducts($searchQuery); |
| 153 | + |
| 154 | + $this->assertCount( |
| 155 | + 2, |
| 156 | + $products, |
| 157 | + 'Expected to find 2 products, found ' . count($products) . '.' |
| 158 | + ); |
| 159 | + |
| 160 | + $this->assertEquals( |
| 161 | + $firstMatchProductName, |
| 162 | + $products[0]->getData('name'), |
| 163 | + 'Products order is not as expected.' |
| 164 | + ); |
| 165 | + $this->assertEquals( |
| 166 | + $secondMatchProductName, |
| 167 | + $products[1]->getData('name'), |
| 168 | + 'Products order is not as expected.' |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + public function skuOverNameAttributeSearchWeightDataProvider(): array |
| 173 | + { |
| 174 | + return [ |
| 175 | + ['1-2-3-4', 10, 5, 'test', '1-2-3-4'], |
| 176 | + ['1-2-3-4', 5, 10, '1-2-3-4', 'test'], |
| 177 | + ]; |
| 178 | + } |
| 179 | +} |
0 commit comments