ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -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,
],
],
];
}
}
@@ -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'] : [];
}
}
@@ -0,0 +1,271 @@
<?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\Indexer\Model\Indexer;
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();
$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);
}
/**
* 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();
}
}