init
This commit is contained in:
+256
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Elasticsearch\Model\Adapter;
|
||||
|
||||
use Magento\AdvancedSearch\Model\Client\ClientInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
|
||||
use Magento\Catalog\Setup\CategorySetup;
|
||||
use Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend;
|
||||
use Magento\Elasticsearch\Model\Adapter\Index\BuilderInterface;
|
||||
use Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver;
|
||||
use Magento\Elasticsearch\Model\Indexer\IndexerHandler;
|
||||
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
|
||||
use Magento\Framework\Indexer\DimensionFactory;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Stdlib\ArrayManager;
|
||||
use Magento\Indexer\Model\Indexer;
|
||||
use Magento\Store\Model\StoreDimensionProvider;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Elasticsearch adapter model test class
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class ElasticsearchTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var IndexNameResolver
|
||||
*/
|
||||
private $indexNameResolver;
|
||||
|
||||
/**
|
||||
* @var ClientInterface
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var Elasticsearch
|
||||
*/
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @var BuilderInterface
|
||||
*/
|
||||
private $indexBuilder;
|
||||
|
||||
/**
|
||||
* @var ArrayManager
|
||||
*/
|
||||
private $arrayManager;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $newIndex;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->indexNameResolver = $this->objectManager->get(IndexNameResolver::class);
|
||||
$this->adapter = $this->objectManager->get(Elasticsearch::class);
|
||||
$this->storeManager = $this->objectManager->create(StoreManagerInterface::class);
|
||||
$connectionManager = $this->objectManager->create(ConnectionManager::class);
|
||||
$this->client = $connectionManager->getConnection();
|
||||
$this->indexBuilder = $this->objectManager->get(BuilderInterface::class);
|
||||
$this->arrayManager = $this->objectManager->get(ArrayManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function tearDown(): void
|
||||
{
|
||||
if ($this->newIndex) {
|
||||
$this->deleteIndex($this->newIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests possibility to create mapping if adapter has obsolete index name in cache
|
||||
*
|
||||
* @magentoDataFixture Magento/Elasticsearch/_files/select_attribute.php
|
||||
* @return void
|
||||
*/
|
||||
public function testRetryOnIndexNotFoundException(): void
|
||||
{
|
||||
$this->reindex();
|
||||
$this->updateElasticsearchIndex();
|
||||
$this->createNewAttribute();
|
||||
$mapping = $this->client->getMapping(['index' => $this->newIndex]);
|
||||
$pathField = $this->arrayManager->findPath('properties', $mapping);
|
||||
$attributes = $this->arrayManager->get($pathField, $mapping, []);
|
||||
$this->assertArrayHasKey('multiselect_attribute', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that new fields are not added during document indexing that were not explicitly defined in the mapping
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoDbIsolation disabled
|
||||
*/
|
||||
public function testMappingShouldNotChangeAfterReindex(): void
|
||||
{
|
||||
$index = 'catalogsearch_fulltext';
|
||||
$storeId = (int) $this->storeManager->getStore('fixture_second_store')->getId();
|
||||
// Create empty index and save the initial mapping
|
||||
$dimensionFactory = $this->objectManager->get(DimensionFactory::class);
|
||||
$dimensions = [
|
||||
StoreDimensionProvider::DIMENSION_NAME => $dimensionFactory->create(
|
||||
StoreDimensionProvider::DIMENSION_NAME,
|
||||
(string) $storeId
|
||||
)
|
||||
];
|
||||
$indexHandlerFactory = $this->objectManager->get(IndexerHandlerFactory::class);
|
||||
/** @var IndexerHandler $indexHandler */
|
||||
$indexHandler = $indexHandlerFactory->create(
|
||||
[
|
||||
'data' => [
|
||||
'indexer_id' => $index
|
||||
]
|
||||
]
|
||||
);
|
||||
$indexHandler->cleanIndex($dimensions);
|
||||
$indexHandler->saveIndex($dimensions, new \ArrayIterator([]));
|
||||
$propertiesBefore = $this->getIndexMapping($storeId);
|
||||
$this->reindex();
|
||||
$propertiesAfter = $this->getIndexMapping($storeId);
|
||||
$this->assertEquals($propertiesBefore, $propertiesAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and save new attribute
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createNewAttribute(): void
|
||||
{
|
||||
/** @var CategorySetup $installer */
|
||||
$installer = $this->objectManager->get(CategorySetup::class);
|
||||
/** @var Attribute $attribute */
|
||||
$multiselectAttribute = $this->objectManager->get(Attribute::class);
|
||||
$multiselectAttribute->setData(
|
||||
[
|
||||
'attribute_code' => 'multiselect_attribute',
|
||||
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
|
||||
'is_global' => 1,
|
||||
'is_user_defined' => 1,
|
||||
'frontend_input' => 'multiselect',
|
||||
'is_unique' => 0,
|
||||
'is_required' => 0,
|
||||
'is_searchable' => 1,
|
||||
'is_visible_in_advanced_search' => 0,
|
||||
'is_comparable' => 0,
|
||||
'is_filterable' => 1,
|
||||
'is_filterable_in_search' => 0,
|
||||
'is_used_for_promo_rules' => 0,
|
||||
'is_html_allowed_on_front' => 1,
|
||||
'is_visible_on_front' => 0,
|
||||
'used_in_product_listing' => 0,
|
||||
'used_for_sort_by' => 0,
|
||||
'frontend_label' => ['Multiselect Attribute'],
|
||||
'backend_type' => 'text',
|
||||
'backend_model' => ArrayBackend::class,
|
||||
'option' => [
|
||||
'value' => [
|
||||
'dog' => ['Dog'],
|
||||
'cat' => ['Cat'],
|
||||
],
|
||||
'order' => [
|
||||
'dog' => 1,
|
||||
'cat' => 2,
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
$multiselectAttribute->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare new index and delete old. Keep cache alive.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function updateElasticsearchIndex(): void
|
||||
{
|
||||
$storeId = (int)$this->storeManager->getDefaultStoreView()->getId();
|
||||
$mappedIndexerId = 'product';
|
||||
$this->adapter->updateIndexMapping($storeId, $mappedIndexerId, 'select_attribute');
|
||||
$oldIndex = $this->indexNameResolver->getIndexFromAlias($storeId, $mappedIndexerId);
|
||||
$this->newIndex = $oldIndex . '1';
|
||||
$this->deleteIndex($this->newIndex);
|
||||
$this->indexBuilder->setStoreId($storeId);
|
||||
$this->client->createIndex($this->newIndex, ['settings' => $this->indexBuilder->build()]);
|
||||
$this->client->updateAlias(
|
||||
$this->indexNameResolver->getIndexNameForAlias($storeId, $mappedIndexerId),
|
||||
$this->newIndex,
|
||||
$oldIndex
|
||||
);
|
||||
$this->client->deleteIndex($oldIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete index by name if exists
|
||||
*
|
||||
* @param $newIndex
|
||||
*/
|
||||
private function deleteIndex($newIndex): void
|
||||
{
|
||||
if ($this->client->indexExists($newIndex)) {
|
||||
$this->client->deleteIndex($newIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function reindex(): void
|
||||
{
|
||||
$indexer = $this->objectManager->create(Indexer::class);
|
||||
$indexer->load('catalogsearch_fulltext');
|
||||
$indexer->reindexAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $storeId
|
||||
* @return array
|
||||
*/
|
||||
private function getIndexMapping(int $storeId): array
|
||||
{
|
||||
$indexName = $this->indexNameResolver->getIndexName($storeId, 'product', []);
|
||||
$mapping = $this->client->getMapping(['index' => $indexName]);
|
||||
$pathField = $this->arrayManager->findPath('properties', $mapping);
|
||||
return $this->arrayManager->get($pathField, $mapping, []);
|
||||
}
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Elasticsearch\Model\CatalogSearch;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\CatalogSearch\Model\Advanced;
|
||||
use Magento\Catalog\Model\Product\Visibility;
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Check catalog Advanced Search
|
||||
*/
|
||||
class AdvancedTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var Registry
|
||||
*/
|
||||
private $registry;
|
||||
|
||||
/**
|
||||
* @var Visibility
|
||||
*/
|
||||
private $productVisibility;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->registry = $this->objectManager->get(Registry::class);
|
||||
$this->productVisibility = $this->objectManager->get(Visibility::class);
|
||||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that Advanced Search does NOT return products that do NOT have search visibility.
|
||||
*
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
|
||||
* @return void
|
||||
*/
|
||||
public function testAddFilters(): void
|
||||
{
|
||||
$this->assertResultsAfterRequest(1);
|
||||
|
||||
/** @var ProductInterface $configurableProductOption */
|
||||
$configurableProductOption = $this->productRepository->get('Simple option 1');
|
||||
$configurableProductOption->setVisibility(Visibility::VISIBILITY_IN_SEARCH);
|
||||
$this->productRepository->save($configurableProductOption);
|
||||
|
||||
$this->registry->unregister('advanced_search_conditions');
|
||||
$this->assertResultsAfterRequest(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do search query and assert results.
|
||||
*
|
||||
* @param int $count
|
||||
* @return void
|
||||
*/
|
||||
private function assertResultsAfterRequest(int $count): void
|
||||
{
|
||||
/** @var Advanced $advancedSearch */
|
||||
$advancedSearch = $this->objectManager->create(Advanced::class);
|
||||
$advancedSearch->addFilters(['name' => 'Configurable']);
|
||||
|
||||
/** @var ProductInterface[] $itemsResult */
|
||||
$itemsResult = $advancedSearch->getProductCollection()
|
||||
->addAttributeToSelect(ProductInterface::VISIBILITY)
|
||||
->getItems();
|
||||
|
||||
$this->assertCount($count, $itemsResult);
|
||||
foreach ($itemsResult as $product) {
|
||||
$this->assertStringContainsString('Configurable', $product->getName());
|
||||
$this->assertContains((int)$product->getVisibility(), $this->productVisibility->getVisibleInSearchIds());
|
||||
}
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Elasticsearch\Model\Client;
|
||||
|
||||
use Magento\AdvancedSearch\Model\Client\ClientInterface;
|
||||
use Magento\Indexer\Model\Indexer;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
|
||||
use Magento\Elasticsearch\Model\Config;
|
||||
use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
|
||||
use Magento\TestModuleCatalogSearch\Model\SearchEngineVersionReader;
|
||||
use Magento\Framework\Search\EngineResolverInterface;
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Elasticsearch/_files/configurable_products.php
|
||||
*/
|
||||
class ElasticsearchTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var ConnectionManager
|
||||
*/
|
||||
private $connectionManager;
|
||||
|
||||
/**
|
||||
* @var ClientInterface
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $clientConfig;
|
||||
|
||||
/**
|
||||
* @var SearchIndexNameResolver
|
||||
*/
|
||||
private $searchIndexNameResolver;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->connectionManager = $objectManager->create(ConnectionManager::class);
|
||||
$this->client = $this->connectionManager->getConnection();
|
||||
$this->storeManager = $objectManager->create(StoreManagerInterface::class);
|
||||
$this->clientConfig = $objectManager->create(Config::class);
|
||||
$this->searchIndexNameResolver = $objectManager->create(SearchIndexNameResolver::class);
|
||||
$indexer = $objectManager->create(Indexer::class);
|
||||
$indexer->load('catalogsearch_fulltext');
|
||||
$indexer->reindexAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that correct engine is set
|
||||
*/
|
||||
protected function assertPreConditions(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$currentEngine = $objectManager->get(EngineResolverInterface::class)->getCurrentSearchEngine();
|
||||
$installedEngine = $objectManager->get(SearchEngineVersionReader::class)->getFullVersion();
|
||||
$this->assertEquals(
|
||||
$installedEngine,
|
||||
$currentEngine,
|
||||
sprintf(
|
||||
'Search engine configuration "%s" is not compatible with the installed version',
|
||||
$currentEngine
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @return array
|
||||
*/
|
||||
private function search($text)
|
||||
{
|
||||
$storeId = $this->storeManager->getDefaultStoreView()->getId();
|
||||
$searchQuery = [
|
||||
'index' => $this->searchIndexNameResolver->getIndexName($storeId, 'catalogsearch_fulltext'),
|
||||
'type' => $this->clientConfig->getEntityType(),
|
||||
'body' => [
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'minimum_should_match' => 1,
|
||||
'should' => [
|
||||
[
|
||||
'match' => [
|
||||
'_all' => $text,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$queryResult = $this->client->query($searchQuery);
|
||||
return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSearchConfigurableProductBySimpleProductName()
|
||||
{
|
||||
$this->assertProductWithSkuFound('configurable', $this->search('Configurable Option'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSearchConfigurableProductBySimpleProductAttributeMultiselect()
|
||||
{
|
||||
$this->assertProductWithSkuFound('configurable', $this->search('dog'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSearchConfigurableProductBySimpleProductAttributeSelect()
|
||||
{
|
||||
$this->assertProductWithSkuFound('configurable', $this->search('chair'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSearchConfigurableProductBySimpleProductAttributeShortDescription()
|
||||
{
|
||||
$this->assertProductWithSkuFound('configurable', $this->search('simpledescription'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that product with SKU is present in response
|
||||
*
|
||||
* @param string $sku
|
||||
* @param array $result
|
||||
* @return bool
|
||||
*/
|
||||
private function assertProductWithSkuFound($sku, array $result)
|
||||
{
|
||||
foreach ($result as $item) {
|
||||
if ($item['_source']['sku'] == $sku) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Elasticsearch\Model\Indexer\Fulltext\Plugin\Category\Product;
|
||||
|
||||
use Magento\AdvancedSearch\Model\Client\ClientInterface;
|
||||
use Magento\Catalog\Api\Data\EavAttributeInterface;
|
||||
use Magento\Catalog\Api\Data\ProductAttributeInterface;
|
||||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
|
||||
use Magento\Catalog\Setup\CategorySetup;
|
||||
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
|
||||
use Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver;
|
||||
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
|
||||
use Magento\Framework\Stdlib\ArrayManager;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Check Search engine indexer mapping when working with attributes.
|
||||
*/
|
||||
class AttributeTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ClientInterface
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @var ArrayManager
|
||||
*/
|
||||
private $arrayManager;
|
||||
|
||||
/**
|
||||
* @var IndexNameResolver
|
||||
*/
|
||||
private $indexNameResolver;
|
||||
|
||||
/**
|
||||
* @var Processor
|
||||
*/
|
||||
private $indexerProcessor;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var CategorySetup
|
||||
*/
|
||||
private $installer;
|
||||
|
||||
/**
|
||||
* @var AttributeFactory
|
||||
*/
|
||||
private $attributeFactory;
|
||||
|
||||
/**
|
||||
* @var ProductAttributeRepositoryInterface
|
||||
*/
|
||||
private $attributeRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$connectionManager = Bootstrap::getObjectManager()->get(ConnectionManager::class);
|
||||
$this->client = $connectionManager->getConnection();
|
||||
$this->arrayManager = Bootstrap::getObjectManager()->get(ArrayManager::class);
|
||||
$this->indexNameResolver = Bootstrap::getObjectManager()->get(IndexNameResolver::class);
|
||||
$this->indexerProcessor = Bootstrap::getObjectManager()->get(Processor::class);
|
||||
$this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
|
||||
$this->installer = Bootstrap::getObjectManager()->get(CategorySetup::class);
|
||||
$this->attributeFactory = Bootstrap::getObjectManager()->get(AttributeFactory::class);
|
||||
$this->attributeRepository = Bootstrap::getObjectManager()->get(ProductAttributeRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
/** @var ProductAttributeInterface $attribute */
|
||||
$attribute = $this->attributeRepository->get('dropdown_attribute');
|
||||
$this->attributeRepository->delete($attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check Search engine indexer mapping is updated after creating attribute.
|
||||
*
|
||||
* @return void
|
||||
* @magentoDataFixture Magento/CatalogSearch/_files/full_reindex.php
|
||||
*/
|
||||
public function testCheckElasticsearchMappingAfterUpdateAttributeToSearchable(): void
|
||||
{
|
||||
$mappedAttributesBefore = $this->getMappingProperties();
|
||||
$expectedResult = [
|
||||
'dropdown_attribute' => [
|
||||
'type' => 'integer',
|
||||
'index' => false,
|
||||
],
|
||||
'dropdown_attribute_value' => [
|
||||
'type' => 'text',
|
||||
'copy_to' => ['_search'],
|
||||
],
|
||||
];
|
||||
|
||||
/** @var ProductAttributeInterface $dropDownAttribute */
|
||||
$dropDownAttribute = $this->attributeFactory->create();
|
||||
$dropDownAttribute->setData($this->getAttributeData());
|
||||
$this->attributeRepository->save($dropDownAttribute);
|
||||
$this->assertTrue($this->indexerProcessor->getIndexer()->isValid());
|
||||
|
||||
$mappedAttributesAfter = $this->getMappingProperties();
|
||||
$this->assertEquals($expectedResult, array_diff_key($mappedAttributesAfter, $mappedAttributesBefore));
|
||||
|
||||
$dropDownAttribute->setData(EavAttributeInterface::IS_SEARCHABLE, true);
|
||||
$this->attributeRepository->save($dropDownAttribute);
|
||||
$this->assertTrue($this->indexerProcessor->getIndexer()->isInvalid());
|
||||
|
||||
$this->assertEquals($mappedAttributesAfter, $this->getMappingProperties());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve Search engine indexer mapping.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getMappingProperties(): array
|
||||
{
|
||||
$storeId = $this->storeManager->getStore()->getId();
|
||||
$mappedIndexerId = $this->indexNameResolver->getIndexMapping(Processor::INDEXER_ID);
|
||||
$indexName = $this->indexNameResolver->getIndexFromAlias($storeId, $mappedIndexerId);
|
||||
$mappedAttributes = $this->client->getMapping(['index' => $indexName]);
|
||||
$pathField = $this->arrayManager->findPath('properties', $mappedAttributes);
|
||||
|
||||
return $this->arrayManager->get($pathField, $mappedAttributes, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve drop-down attribute data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getAttributeData(): array
|
||||
{
|
||||
$entityTypeId = $this->installer->getEntityTypeId(ProductAttributeInterface::ENTITY_TYPE_CODE);
|
||||
|
||||
return [
|
||||
'attribute_code' => 'dropdown_attribute',
|
||||
'entity_type_id' => $entityTypeId,
|
||||
'is_global' => 0,
|
||||
'is_user_defined' => 1,
|
||||
'frontend_input' => 'select',
|
||||
'is_unique' => 0,
|
||||
'is_required' => 0,
|
||||
'is_searchable' => 0,
|
||||
'is_visible_in_advanced_search' => 0,
|
||||
'is_comparable' => 0,
|
||||
'is_filterable' => 0,
|
||||
'is_filterable_in_search' => 0,
|
||||
'is_used_for_promo_rules' => 0,
|
||||
'is_html_allowed_on_front' => 1,
|
||||
'is_visible_on_front' => 1,
|
||||
'used_in_product_listing' => 1,
|
||||
'used_for_sort_by' => 0,
|
||||
'frontend_label' => ['Drop-Down Attribute'],
|
||||
'backend_type' => 'varchar',
|
||||
'option' => [
|
||||
'value' => [
|
||||
'option_1' => ['Option 1'],
|
||||
'option_2' => ['Option 2'],
|
||||
'option_3' => ['Option 3'],
|
||||
],
|
||||
'order' => [
|
||||
'option_1' => 1,
|
||||
'option_2' => 2,
|
||||
'option_3' => 3,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+297
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Elasticsearch\Model\Indexer;
|
||||
|
||||
use Magento\AdvancedSearch\Model\Client\ClientInterface;
|
||||
use Magento\Catalog\Model\Product\Action as ProductAction;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\CatalogInventory\Api\StockRegistryInterface;
|
||||
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
|
||||
use Magento\CatalogSearch\Model\Indexer\Fulltext as CatalogSearchFulltextIndexer;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
|
||||
use Magento\Elasticsearch\Model\Config;
|
||||
use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
|
||||
use Magento\Indexer\Model\Indexer;
|
||||
use Magento\Framework\Search\EngineResolverInterface;
|
||||
use Magento\TestModuleCatalogSearch\Model\SearchEngineVersionReader;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Important: Please make sure that each integration test file works with unique search index. In order to
|
||||
* achieve this, use @magentoConfigFixture to pass unique value for index_prefix for every test
|
||||
* method. E.g. '@magentoConfigFixture current_store catalog/search/elasticsearch7_index_prefix indexerhandlertest'
|
||||
*
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoDataFixture Magento/Elasticsearch/_files/indexer.php
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class IndexHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var ClientInterface
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private $storeIds;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $entityType;
|
||||
|
||||
/**
|
||||
* @var Indexer
|
||||
*/
|
||||
private $indexer;
|
||||
|
||||
/**
|
||||
* @var SearchIndexNameResolver
|
||||
*/
|
||||
private $searchIndexNameResolver;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$connectionManager = Bootstrap::getObjectManager()->create(ConnectionManager::class);
|
||||
$this->client = $connectionManager->getConnection();
|
||||
|
||||
$this->storeManager = Bootstrap::getObjectManager()->create(StoreManagerInterface::class);
|
||||
$this->storeIds = array_keys($this->storeManager->getStores());
|
||||
|
||||
$clientConfig = Bootstrap::getObjectManager()->create(Config::class);
|
||||
$this->entityType = $clientConfig->getEntityType();
|
||||
|
||||
$this->indexer = Bootstrap::getObjectManager()->create(Indexer::class);
|
||||
$this->indexer->load(CatalogSearchFulltextIndexer::INDEXER_ID);
|
||||
$this->indexer->reindexAll();
|
||||
|
||||
$this->searchIndexNameResolver = Bootstrap::getObjectManager()->create(SearchIndexNameResolver::class);
|
||||
$this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that correct engine is set
|
||||
*/
|
||||
protected function assertPreConditions(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$currentEngine = $objectManager->get(EngineResolverInterface::class)->getCurrentSearchEngine();
|
||||
$installedEngine = $objectManager->get(SearchEngineVersionReader::class)->getFullVersion();
|
||||
$this->assertEquals(
|
||||
$installedEngine,
|
||||
$currentEngine,
|
||||
sprintf(
|
||||
'Search engine configuration "%s" is not compatible with the installed version',
|
||||
$currentEngine
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReindexAll(): void
|
||||
{
|
||||
$productApple = $this->productRepository->get('fulltext-1');
|
||||
foreach ($this->storeIds as $storeId) {
|
||||
$products = $this->searchByName('Apple', $storeId);
|
||||
$this->assertCount(1, $products);
|
||||
$this->assertEquals($productApple->getId(), $products[0]['_id']);
|
||||
|
||||
$products = $this->searchByName('Simple Product', $storeId);
|
||||
$this->assertCount(5, $products);
|
||||
|
||||
$this->assertCount(2, $this->searchByBoolAttribute(0, $storeId));
|
||||
$this->assertCount(3, $this->searchByBoolAttribute(1, $storeId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @return void
|
||||
*/
|
||||
public function testReindexRowAfterEdit(): void
|
||||
{
|
||||
$this->storeManager->setCurrentStore('admin');
|
||||
$productApple = $this->productRepository->get('fulltext-1');
|
||||
$productApple->setName('Simple Product Cucumber');
|
||||
$this->productRepository->save($productApple);
|
||||
|
||||
foreach ($this->storeIds as $storeId) {
|
||||
$products = $this->searchByName('Apple', $storeId);
|
||||
$this->assertCount(0, $products);
|
||||
|
||||
$products = $this->searchByName('Cucumber', $storeId);
|
||||
$this->assertCount(1, $products);
|
||||
$this->assertEquals($productApple->getId(), $products[0]['_id']);
|
||||
|
||||
$products = $this->searchByName('Simple Product', $storeId);
|
||||
$this->assertCount(5, $products);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReindexRowAfterMassAction(): void
|
||||
{
|
||||
$productApple = $this->productRepository->get('fulltext-1');
|
||||
$productBanana = $this->productRepository->get('fulltext-2');
|
||||
$productIds = [
|
||||
$productApple->getId(),
|
||||
$productBanana->getId(),
|
||||
];
|
||||
$attrData = [
|
||||
'name' => 'Simple Product Common',
|
||||
];
|
||||
/** @var ProductAction $action */
|
||||
$action = Bootstrap::getObjectManager()->get(ProductAction::class);
|
||||
|
||||
foreach ($this->storeIds as $storeId) {
|
||||
$action->updateAttributes($productIds, $attrData, $storeId);
|
||||
|
||||
$products = $this->searchByName('Apple', $storeId);
|
||||
$this->assertCount(0, $products);
|
||||
|
||||
$products = $this->searchByName('Banana', $storeId);
|
||||
$this->assertCount(0, $products);
|
||||
|
||||
$products = $this->searchByName('Unknown', $storeId);
|
||||
$this->assertCount(0, $products);
|
||||
|
||||
$products = $this->searchByName('Common', $storeId);
|
||||
$this->assertCount(2, $products);
|
||||
|
||||
$products = $this->searchByName('Simple Product', $storeId);
|
||||
$this->assertCount(5, $products);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @return void
|
||||
*/
|
||||
public function testReindexRowAfterDelete(): void
|
||||
{
|
||||
$productBanana = $this->productRepository->get('fulltext-2');
|
||||
$this->productRepository->delete($productBanana);
|
||||
|
||||
foreach ($this->storeIds as $storeId) {
|
||||
$products = $this->searchByName('Banana', $storeId);
|
||||
$this->assertEmpty($products);
|
||||
|
||||
$products = $this->searchByName('Simple Product', $storeId);
|
||||
$this->assertCount(4, $products);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDataFixture Magento/Elasticsearch/_files/configurable_products.php
|
||||
* @return void
|
||||
*/
|
||||
public function testReindexRowAfterUpdateStockStatus(): void
|
||||
{
|
||||
foreach ($this->storeIds as $storeId) {
|
||||
$products = $this->searchByName('ProductOption1', $storeId);
|
||||
$this->assertNotEmpty($products);
|
||||
}
|
||||
$product = $this->productRepository->get('simple_10');
|
||||
/** @var StockRegistryInterface $stockRegistry */
|
||||
$stockRegistry = Bootstrap::getObjectManager()->create(StockRegistryInterface::class);
|
||||
$stockItem = $stockRegistry->getStockItem($product->getId());
|
||||
$stockItem->setIsInStock(false);
|
||||
/** @var StockItemRepositoryInterface $stockRepository */
|
||||
$stockRepository = Bootstrap::getObjectManager()->create(StockItemRepositoryInterface::class);
|
||||
$stockRepository->save($stockItem);
|
||||
|
||||
foreach ($this->storeIds as $storeId) {
|
||||
$products = $this->searchByName('ProductOption1', $storeId);
|
||||
$this->assertEmpty($products);
|
||||
|
||||
$products = $this->searchByName('Configurable', $storeId);
|
||||
$this->assertNotEmpty($products);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search docs in search engine by name.
|
||||
*
|
||||
* @param string $text
|
||||
* @param int $storeId
|
||||
* @return array
|
||||
*/
|
||||
private function searchByName(string $text, int $storeId): array
|
||||
{
|
||||
$index = $this->searchIndexNameResolver->getIndexName($storeId, $this->indexer->getId());
|
||||
$searchQuery = [
|
||||
'index' => $index,
|
||||
'type' => $this->entityType,
|
||||
'body' => [
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'minimum_should_match' => 1,
|
||||
'should' => [
|
||||
[
|
||||
'match' => [
|
||||
'name' => $text,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$queryResult = $this->client->query($searchQuery);
|
||||
$products = isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
|
||||
|
||||
return $products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search docs in search engine by boolean attribute.
|
||||
*
|
||||
* @param int $value
|
||||
* @param int $storeId
|
||||
* @return array
|
||||
*/
|
||||
private function searchByBoolAttribute(int $value, int $storeId): array
|
||||
{
|
||||
$index = $this->searchIndexNameResolver->getIndexName($storeId, $this->indexer->getId());
|
||||
$searchQuery = [
|
||||
'index' => $index,
|
||||
'type' => $this->entityType,
|
||||
'body' => [
|
||||
'query' => [
|
||||
'query_string' => [
|
||||
'query' => $value,
|
||||
'default_field' => 'boolean_attribute',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$queryResult = $this->client->query($searchQuery);
|
||||
return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
|
||||
}
|
||||
}
|
||||
+297
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Elasticsearch\Model\Indexer;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
use Magento\Indexer\Model\Indexer;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
|
||||
use Magento\AdvancedSearch\Model\Client\ClientInterface as ElasticsearchClient;
|
||||
use Magento\Elasticsearch\Model\Config;
|
||||
use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
|
||||
use Magento\Framework\Search\EngineResolverInterface;
|
||||
use Magento\TestModuleCatalogSearch\Model\SearchEngineVersionReader;
|
||||
|
||||
/**
|
||||
* Important: Please make sure that each integration test file works with unique search index. In order to
|
||||
* achieve this, use @magentoConfigFixture to pass unique value for index_prefix for every test
|
||||
* method.
|
||||
* E.g. '@magentoConfigFixture current_store catalog/search/elasticsearch7_index_prefix indexerhandlertest_configurable'
|
||||
*
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class ReindexAllTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var ConnectionManager
|
||||
*/
|
||||
private $connectionManager;
|
||||
|
||||
/**
|
||||
* @var ElasticsearchClient
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $clientConfig;
|
||||
|
||||
/**
|
||||
* @var SearchIndexNameResolver
|
||||
*/
|
||||
private $searchIndexNameResolver;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->connectionManager = Bootstrap::getObjectManager()->create(ConnectionManager::class);
|
||||
$this->client = $this->connectionManager->getConnection();
|
||||
$this->storeManager = Bootstrap::getObjectManager()->create(StoreManagerInterface::class);
|
||||
$this->clientConfig = Bootstrap::getObjectManager()->create(Config::class);
|
||||
$this->searchIndexNameResolver = Bootstrap::getObjectManager()->create(SearchIndexNameResolver::class);
|
||||
$this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that correct engine is set
|
||||
*/
|
||||
protected function assertPreConditions(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$currentEngine = $objectManager->get(EngineResolverInterface::class)->getCurrentSearchEngine();
|
||||
// phpstan:ignore "Class Magento\TestModuleCatalogSearch\Model\SearchEngineVersionReader not found."
|
||||
$installedEngine = $objectManager->get(SearchEngineVersionReader::class)->getFullVersion();
|
||||
$this->assertEquals(
|
||||
$installedEngine,
|
||||
$currentEngine,
|
||||
sprintf(
|
||||
'Search engine configuration "%s" is not compatible with the installed version',
|
||||
$currentEngine
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test search of all products after full reindex
|
||||
*
|
||||
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
|
||||
*/
|
||||
public function testSearchAll()
|
||||
{
|
||||
$this->reindexAll();
|
||||
$result = $this->searchByName('Configurable Product');
|
||||
self::assertGreaterThanOrEqual(2, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sorting of all products after full reindex
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
|
||||
*/
|
||||
public function testSort()
|
||||
{
|
||||
/** @var $productFifth \Magento\Catalog\Model\Product */
|
||||
$productSimple = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
|
||||
$productSimple->setTypeId('simple')
|
||||
->setAttributeSetId(4)
|
||||
->setWebsiteIds([1])
|
||||
->setName('ABC')
|
||||
->setSku('abc-first-in-sort')
|
||||
->setPrice(20)
|
||||
->setMetaTitle('meta title')
|
||||
->setMetaKeyword('meta keyword')
|
||||
->setMetaDescription('meta description')
|
||||
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
|
||||
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
|
||||
->setStockData(['use_config_manage_stock' => 0])
|
||||
->save();
|
||||
$productConfigurableOption = $this->productRepository->get('simple_10');
|
||||
$productConfigurableOption->setName('1ABC');
|
||||
$this->productRepository->save($productConfigurableOption);
|
||||
$this->reindexAll();
|
||||
$productSimple = $this->productRepository->get('abc-first-in-sort');
|
||||
$result = $this->sortByName();
|
||||
$firstInSearchResults = (int) $result[0]['_id'];
|
||||
$productSimpleId = (int) $productSimple->getId();
|
||||
$this->assertEquals($productSimpleId, $firstInSearchResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sorting of products with lower and upper case names after full reindex
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/Elasticsearch/_files/case_sensitive.php
|
||||
*/
|
||||
public function testSortCaseSensitive(): void
|
||||
{
|
||||
$productFirst = $this->productRepository->get('fulltext-1');
|
||||
$productSecond = $this->productRepository->get('fulltext-2');
|
||||
$productThird = $this->productRepository->get('fulltext-3');
|
||||
$productFourth = $this->productRepository->get('fulltext-4');
|
||||
$productFifth = $this->productRepository->get('fulltext-5');
|
||||
|
||||
$this->reindexAll();
|
||||
$result = $this->sortByName();
|
||||
$firstInSearchResults = (int) $result[0]['_id'];
|
||||
$secondInSearchResults = (int) $result[1]['_id'];
|
||||
$thirdInSearchResults = (int) $result[2]['_id'];
|
||||
$fourthInSearchResults = (int) $result[3]['_id'];
|
||||
$fifthInSearchResults = (int) $result[4]['_id'];
|
||||
|
||||
self::assertCount(5, $result);
|
||||
self::assertEqualsCanonicalizing(
|
||||
[$productFirst->getId(), $productFourth->getId()],
|
||||
[$firstInSearchResults, $secondInSearchResults]
|
||||
);
|
||||
self::assertEqualsCanonicalizing(
|
||||
[$productSecond->getId(), $productFifth->getId()],
|
||||
[$thirdInSearchResults, $fourthInSearchResults]
|
||||
);
|
||||
self::assertEquals($productThird->getId(), $fifthInSearchResults);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(ProductFixture::class, ['sku' => 'p1', 'name' => 'A']),
|
||||
DataFixture(ProductFixture::class, ['sku' => 'p2', 'name' => 'Ç']),
|
||||
DataFixture(ProductFixture::class, ['sku' => 'p3', 'name' => 'D']),
|
||||
DataFixture(ProductFixture::class, ['sku' => 'p4', 'name' => 'Ü']),
|
||||
DataFixture(ProductFixture::class, ['sku' => 'p5', 'name' => 'Z']),
|
||||
]
|
||||
public function testSortAccentedCharacters(): void
|
||||
{
|
||||
$expectedOrder = [
|
||||
(int) $this->productRepository->get('p1')->getId(),
|
||||
(int) $this->productRepository->get('p2')->getId(),
|
||||
(int) $this->productRepository->get('p3')->getId(),
|
||||
(int) $this->productRepository->get('p4')->getId(),
|
||||
(int) $this->productRepository->get('p5')->getId(),
|
||||
];
|
||||
$this->reindexAll();
|
||||
|
||||
$result = $this->sortByName();
|
||||
$actualOrder = array_map(fn ($id) => (int) $id, array_column($result, '_id'));
|
||||
self::assertEquals($expectedOrder, $actualOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test search of specific product after full reindex
|
||||
*
|
||||
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
* @dataProvider searchSpecificProductDataProvider
|
||||
* @param string $searchName
|
||||
* @param string $sku
|
||||
* @param int $expectedCount
|
||||
*/
|
||||
public function testSearchSpecificProduct(string $searchName, string $sku, int $expectedCount)
|
||||
{
|
||||
$this->reindexAll();
|
||||
$result = $this->searchByName($searchName);
|
||||
self::assertCount($expectedCount, $result);
|
||||
|
||||
$specificProduct = $this->productRepository->get($sku);
|
||||
self::assertEquals($specificProduct->getId(), $result[0]['_id']);
|
||||
}
|
||||
|
||||
public function searchSpecificProductDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'search by numeric name' => ['12345', 'configurable_12345', 1],
|
||||
'search by name with diacritics' => ['Cùstöm Dèsign', 'custom-design-simple-product', 1],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @return array
|
||||
*/
|
||||
private function searchByName($text)
|
||||
{
|
||||
$storeId = $this->storeManager->getDefaultStoreView()->getId();
|
||||
$searchQuery = [
|
||||
'index' => $this->searchIndexNameResolver->getIndexName($storeId, 'catalogsearch_fulltext'),
|
||||
'type' => $this->clientConfig->getEntityType(),
|
||||
'body' => [
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'minimum_should_match' => 1,
|
||||
'should' => [
|
||||
[
|
||||
'match' => [
|
||||
'name' => $text,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$queryResult = $this->client->query($searchQuery);
|
||||
return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function sortByName()
|
||||
{
|
||||
$storeId = $this->storeManager->getDefaultStoreView()->getId();
|
||||
$searchQuery = [
|
||||
'index' => $this->searchIndexNameResolver->getIndexName($storeId, 'catalogsearch_fulltext'),
|
||||
'type' => $this->clientConfig->getEntityType(),
|
||||
'body' => [
|
||||
'sort' => [
|
||||
'name.sort_name' => [
|
||||
'order' => 'asc'
|
||||
],
|
||||
],
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'must' => [
|
||||
[
|
||||
'terms' => [
|
||||
'visibility' => [2, 4],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$queryResult = $this->client->query($searchQuery);
|
||||
return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Make fulltext catalog search reindex
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function reindexAll()
|
||||
{
|
||||
// Perform full reindex
|
||||
/** @var Indexer $indexer */
|
||||
$indexer = Bootstrap::getObjectManager()->create(Indexer::class);
|
||||
$indexer->load('catalogsearch_fulltext');
|
||||
$indexer->reindexAll();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user