Skip to content

Commit a033552

Browse files
committed
Merge branch 'ACP2E-3362' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-31-10-2024
2 parents 2ff9b8b + da48186 commit a033552

File tree

5 files changed

+146
-16
lines changed

5 files changed

+146
-16
lines changed

app/code/Magento/Search/Model/ResourceModel/Query/Collection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Search\Model\ResourceModel\Query;
77

@@ -171,7 +171,6 @@ public function isTopSearchResult(string $term, int $storeId, int $maxCountCache
171171
$select = $this->getSelect();
172172
$select->reset(\Magento\Framework\DB\Select::FROM);
173173
$select->reset(\Magento\Framework\DB\Select::COLUMNS);
174-
$select->distinct(true);
175174
$select->from(['main_table' => $this->getTable('search_query')], ['query_text']);
176175
$select->where('main_table.store_id IN (?)', $storeId);
177176
$select->where('main_table.num_results > 0');
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Search\Test\Unit\Model\ResourceModel\Query;
9+
10+
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
11+
use Magento\Framework\Data\Collection\EntityFactoryInterface;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\DB\Select;
14+
use Magento\Framework\Event\ManagerInterface;
15+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
16+
use Magento\Search\Model\ResourceModel\Query\Collection;
17+
use Magento\Framework\DB\Helper;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
use PHPUnit\Framework\MockObject\Exception;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
use Psr\Log\LoggerInterface;
23+
24+
class CollectionTest extends TestCase
25+
{
26+
/**
27+
* @var StoreManagerInterface|MockObject
28+
*/
29+
private StoreManagerInterface $_storeManager;
30+
31+
/**
32+
* @var Helper|MockObject
33+
*/
34+
private Helper $_resourceHelper;
35+
36+
/**
37+
* @var EntityFactoryInterface
38+
*/
39+
private EntityFactoryInterface $entityFactory;
40+
41+
/**
42+
* @var LoggerInterface
43+
*/
44+
private LoggerInterface $logger;
45+
46+
/**
47+
* @var FetchStrategyInterface
48+
*/
49+
private FetchStrategyInterface $fetchStrategy;
50+
51+
/**
52+
* @var ManagerInterface
53+
*/
54+
private ManagerInterface $eventManager;
55+
56+
/**
57+
* @var AdapterInterface
58+
*/
59+
private AdapterInterface $connection;
60+
61+
/**
62+
* @var AbstractDb
63+
*/
64+
private AbstractDb $resource;
65+
66+
/**
67+
* @inheritDoc
68+
* @throws Exception
69+
*/
70+
protected function setUp(): void
71+
{
72+
$this->_storeManager = $this->createMock(StoreManagerInterface::class);
73+
$this->_resourceHelper = $this->createMock(Helper::class);
74+
$this->entityFactory = $this->createMock(EntityFactoryInterface::class);
75+
$this->logger = $this->createMock(LoggerInterface::class);
76+
$this->fetchStrategy = $this->createMock(FetchStrategyInterface::class);
77+
$this->eventManager = $this->createMock(ManagerInterface::class);
78+
$this->connection = $this->createMock(AdapterInterface::class);
79+
$this->resource = $this->createMock(AbstractDb::class);
80+
}
81+
82+
/**
83+
* @return void
84+
*/
85+
public function testIsTopSearchResult(): void
86+
{
87+
$term = 'test';
88+
$storeId = 1;
89+
$maxCountCacheableSearchTerms = 10;
90+
91+
$this->resource->expects($this->once())->method('getConnection')->willReturn($this->connection);
92+
$select = $this->getMockBuilder(Select::class)
93+
->disableOriginalConstructor()
94+
->getMock();
95+
$select->expects($this->exactly(7))->method('reset')->willReturnSelf();
96+
$select->expects($this->exactly(3))->method('from')->willReturnSelf();
97+
$select->expects($this->exactly(3))->method('where')->willReturnSelf();
98+
$select->expects($this->once())->method('order')->with(['main_table.popularity desc'])->willReturnSelf();
99+
$select->expects($this->once())->method('limit')->with($maxCountCacheableSearchTerms)->willReturnSelf();
100+
$select->expects($this->once())->method('assemble')->willReturn(
101+
"SELECT COUNT(*) FROM (SELECT DISTINCT `main_table`.`query_text` FROM `search_query` AS `main_table`" .
102+
" WHERE (main_table.store_id IN (1)) AND (main_table.num_results > 0) " .
103+
" ORDER BY `main_table`.`popularity` desc LIMIT {$maxCountCacheableSearchTerms}) AS `result`
104+
WHERE (result.query_text = '{$term}')"
105+
);
106+
$select->expects($this->never())->method('distinct');
107+
$this->connection->expects($this->any())->method('select')->willReturn($select);
108+
109+
$collection = new Collection(
110+
$this->entityFactory,
111+
$this->logger,
112+
$this->fetchStrategy,
113+
$this->eventManager,
114+
$this->_storeManager,
115+
$this->_resourceHelper,
116+
$this->connection,
117+
$this->resource
118+
);
119+
$collection->isTopSearchResult($term, $storeId, $maxCountCacheableSearchTerms);
120+
}
121+
}

app/code/Magento/Search/etc/db_schema.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2018 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -35,7 +35,7 @@
3535
<column name="query_text"/>
3636
<column name="store_id"/>
3737
</constraint>
38-
<index referenceId="SEARCH_QUERY_QUERY_TEXT_STORE_ID_POPULARITY" indexType="btree">
38+
<index referenceId="SEARCH_QUERY_QUERY_TEXT_STORE_ID_POPULARITY" indexType="btree" disabled="true">
3939
<column name="query_text"/>
4040
<column name="store_id"/>
4141
<column name="popularity"/>
@@ -48,6 +48,12 @@
4848
<column name="num_results"/>
4949
<column name="popularity"/>
5050
</index>
51+
<index referenceId="SEARCH_QUERY_QUERY_TEXT_STORE_ID_NUM_RESULTS_POPULARITY" indexType="btree">
52+
<column name="query_text"/>
53+
<column name="store_id"/>
54+
<column name="num_results"/>
55+
<column name="popularity"/>
56+
</index>
5157
<index referenceId="SEARCH_QUERY_STORE_ID_POPULARITY" indexType="btree" disabled="true">
5258
<column name="store_id"/>
5359
<column name="popularity"/>

app/code/Magento/Search/etc/db_schema_whitelist.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
"updated_at": true
1515
},
1616
"index": {
17-
"SEARCH_QUERY_QUERY_TEXT_STORE_ID_POPULARITY": true,
17+
"SEARCH_QUERY_QUERY_TEXT_STORE_ID_NUM_RESULTS_POPULARITY": true,
1818
"SEARCH_QUERY_IS_PROCESSED": true,
1919
"SEARCH_QUERY_SYNONYM_FOR": true,
20+
"SEARCH_QUERY_QUERY_TEXT_STORE_ID_POPULARITY": false,
2021
"SEARCH_QUERY_STORE_ID_NUM_RESULTS_POPULARITY": true,
2122
"SEARCH_QUERY_STORE_ID": false,
2223
"SEARCH_QUERY_STORE_ID_POPULARITY": false
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Search\Model\ResourceModel\Query;
99

1010
use Magento\Search\Model\ResourceModel\Query;
11-
use Magento\Framework\ObjectManagerInterface;
1211
use Magento\TestFramework\Helper\Bootstrap;
1312
use PHPUnit\Framework\TestCase;
14-
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1513

16-
class CollectionTest extends \PHPUnit\Framework\TestCase
14+
class CollectionTest extends TestCase
1715
{
1816
/**
1917
* @var Query
2018
*/
21-
private $queryResource;
19+
private Query $queryResource;
2220

2321
/**
2422
* @inheritDoc
@@ -29,12 +27,17 @@ protected function setUp(): void
2927
$this->queryResource = $objectManager->get(Query::class);
3028
}
3129

32-
public function testSearchQueryTableHasProperIndex()
30+
/**
31+
* @return void
32+
*/
33+
public function testSearchQueryTableHasProperIndex(): void
3334
{
3435
$table = $this->queryResource->getTable('search_query');
35-
$indexName = 'SEARCH_QUERY_STORE_ID_NUM_RESULTS_POPULARITY';
36+
$indexQueryStoreNumPopularity = 'SEARCH_QUERY_STORE_ID_NUM_RESULTS_POPULARITY';
37+
$indexQueryTextStoreNumPopularity = 'SEARCH_QUERY_QUERY_TEXT_STORE_ID_NUM_RESULTS_POPULARITY';
3638
$connection = $this->queryResource->getConnection();
3739
$tableIndexes = $connection->getIndexList($table);
38-
$this->assertArrayHasKey($indexName, $tableIndexes);
40+
$this->assertArrayHasKey($indexQueryStoreNumPopularity, $tableIndexes);
41+
$this->assertArrayHasKey($indexQueryTextStoreNumPopularity, $tableIndexes);
3942
}
4043
}

0 commit comments

Comments
 (0)