Skip to content

Commit 71088ef

Browse files
committed
ACP2E-72: Search results suggestion give total count of 0 even though there are products
- Fixed the CR comments
1 parent e264e9b commit 71088ef

File tree

5 files changed

+217
-145
lines changed

5 files changed

+217
-145
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\AdvancedSearch\Plugin\Model;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Search\Model\QueryResult;
12+
use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
15+
/**
16+
* Get the search suggestion results count.
17+
*/
18+
class SearchSuggestionResultsCount
19+
{
20+
/**
21+
* @var QueryCollectionFactory
22+
*/
23+
private QueryCollectionFactory $queryCollectionFactory;
24+
25+
/**
26+
* @var StoreManagerInterface
27+
*/
28+
private StoreManagerInterface $storeManager;
29+
30+
/**
31+
* Construct Method for get the search suggestion results count.
32+
*
33+
* @param QueryCollectionFactory $queryCollectionFactory
34+
* @param StoreManagerInterface $storeManager
35+
*/
36+
public function __construct(
37+
QueryCollectionFactory $queryCollectionFactory,
38+
StoreManagerInterface $storeManager
39+
) {
40+
$this->queryCollectionFactory = $queryCollectionFactory;
41+
$this->storeManager = $storeManager;
42+
}
43+
44+
/**
45+
* Get the search suggestion results count.
46+
*
47+
* @param QueryResult $subject
48+
* @param array $result
49+
* @return array
50+
* @throws NoSuchEntityException
51+
*/
52+
public function afterGetResultsCount(QueryResult $subject, $result)
53+
{
54+
$collection = $this->queryCollectionFactory->create()->setStoreId(
55+
$this->storeManager->getStore()->getId()
56+
)->setQueryFilter(
57+
$subject->getQueryText()
58+
);
59+
foreach ($collection as $item) {
60+
$result = $item->getData('num_results');
61+
}
62+
return $result;
63+
}
64+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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\AdvancedSearch\Test\Unit\Plugin\Model;
9+
10+
use Magento\Search\Model\QueryResult;
11+
use Magento\Search\Model\ResourceModel\Query\Collection;
12+
use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
13+
use Magento\AdvancedSearch\Plugin\Model\SearchSuggestionResultsCount;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Framework\Data\Collection\AbstractDb;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class SearchSuggestionResultsCountTest extends TestCase
20+
{
21+
/**
22+
* @var SearchSuggestionResultsCount
23+
*/
24+
private SearchSuggestionResultsCount $model;
25+
26+
/**
27+
* @var QueryCollectionFactory
28+
*/
29+
private QueryCollectionFactory $queryCollectionFactoryMock;
30+
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private StoreManagerInterface $storeManagerMock;
35+
36+
/**
37+
* @var QueryResult
38+
*/
39+
private QueryResult $queryResultMock;
40+
41+
/**
42+
* @var Collection
43+
*/
44+
private Collection $queryCollectionMock;
45+
46+
/**
47+
* @var StoreInterface
48+
*/
49+
private StoreInterface $storeInterfaceMock;
50+
51+
/**
52+
* @var AbstractDb
53+
*/
54+
private AbstractDb $abstractDbMock;
55+
56+
/**
57+
* @return void
58+
*/
59+
protected function setUp(): void
60+
{
61+
$this->queryCollectionFactoryMock = $this->getMockBuilder(QueryCollectionFactory::class)
62+
->disableOriginalConstructor()
63+
->onlyMethods(['create'])
64+
->getMock();
65+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->queryResultMock = $this->getMockBuilder(QueryResult::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->queryCollectionMock = $this->getMockBuilder(Collection::class)
72+
->disableOriginalConstructor()
73+
->getMock();
74+
$this->storeInterfaceMock = $this->getMockBuilder(StoreInterface::class)
75+
->disableOriginalConstructor()
76+
->onlyMethods(['getId'])
77+
->getMockForAbstractClass();
78+
$this->abstractDbMock = $this->getMockBuilder(AbstractDb::class)
79+
->disableOriginalConstructor()
80+
->onlyMethods(['getData'])
81+
->getMockForAbstractClass();
82+
$this->model = new SearchSuggestionResultsCount(
83+
$this->queryCollectionFactoryMock,
84+
$this->storeManagerMock
85+
);
86+
}
87+
88+
/**
89+
* @return void
90+
* @throws \Magento\Framework\Exception\NoSuchEntityException
91+
*/
92+
public function testAfterGetResultsCount()
93+
{
94+
$expectedArr = ['num_results' => 5];
95+
$this->queryCollectionFactoryMock->expects($this->once())
96+
->method('create')
97+
->willReturn($this->queryCollectionMock);
98+
$this->storeManagerMock->expects($this->once())
99+
->method('getStore')
100+
->willReturn($this->storeInterfaceMock);
101+
$this->storeInterfaceMock->expects($this->once())
102+
->method('getId')
103+
->willReturn(1);
104+
$this->queryCollectionMock->expects($this->once())
105+
->method('setStoreId')
106+
->willReturnSelf();
107+
$this->queryResultMock->expects($this->once())
108+
->method('getQueryText')
109+
->willReturn('mint');
110+
$this->queryCollectionMock->expects($this->once())
111+
->method('setQueryFilter')
112+
->willReturn($this->queryCollectionMock);
113+
$this->abstractDbMock->expects($this->once())
114+
->method('getData')
115+
->willReturn(['num_results' => 5]);
116+
$this->queryCollectionMock->expects($this->once())
117+
->method('getIterator')
118+
->willReturn(new \ArrayIterator([$this->abstractDbMock]));
119+
$this->assertEquals(
120+
$expectedArr,
121+
$this->model->afterGetResultsCount(
122+
$this->queryResultMock,
123+
0
124+
)
125+
);
126+
}
127+
}

app/code/Magento/AdvancedSearch/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@
4040
</arguments>
4141
</type>
4242
<preference for="Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface" type="Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProvider" />
43+
<type name="Magento\Search\Model\QueryResult">
44+
<plugin name="getSearchSuggestionResultsCount" type="Magento\AdvancedSearch\Plugin\Model\SearchSuggestionResultsCount" sortOrder="1"/>
45+
</type>
4346
</config>

app/code/Magento/Search/Model/QueryResult.php

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
*/
66
namespace Magento\Search\Model;
77

8-
use Magento\Framework\Exception\NoSuchEntityException;
9-
use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
10-
use Magento\Store\Model\StoreManagerInterface;
11-
128
/**
139
* @api
1410
* @since 100.0.2
@@ -18,63 +14,36 @@ class QueryResult
1814
/**
1915
* @var string
2016
*/
21-
private string $queryText;
17+
private $queryText;
2218

2319
/**
2420
* @var int
2521
*/
2622
private $resultsCount;
2723

2824
/**
29-
* @var QueryCollectionFactory
30-
*/
31-
private QueryCollectionFactory $queryCollectionFactory;
32-
33-
/**
34-
* @var StoreManagerInterface
25+
* @param string $queryText
26+
* @param string $resultsCount
3527
*/
36-
private StoreManagerInterface $storeManager;
37-
38-
/**
39-
* @param $queryText
40-
* @param $resultsCount
41-
* @param QueryCollectionFactory $queryCollectionFactory
42-
* @param StoreManagerInterface $storeManager
43-
*/
44-
public function __construct(
45-
$queryText,
46-
$resultsCount,
47-
QueryCollectionFactory $queryCollectionFactory,
48-
StoreManagerInterface $storeManager
49-
) {
28+
public function __construct($queryText, $resultsCount)
29+
{
5030
$this->queryText = $queryText;
5131
$this->resultsCount = $resultsCount;
52-
$this->queryCollectionFactory = $queryCollectionFactory;
53-
$this->storeManager = $storeManager;
5432
}
5533

5634
/**
5735
* @return string
5836
*/
59-
public function getQueryText(): string
37+
public function getQueryText()
6038
{
6139
return $this->queryText;
6240
}
6341

6442
/**
6543
* @return int
66-
* @throws NoSuchEntityException
6744
*/
68-
public function getResultsCount(): int
45+
public function getResultsCount()
6946
{
70-
$collection = $this->queryCollectionFactory->create()->setStoreId(
71-
$this->storeManager->getStore()->getId()
72-
)->setQueryFilter(
73-
$this->getQueryText()
74-
);
75-
foreach ($collection as $item) {
76-
$this->resultsCount = $item->getData('num_results');
77-
}
7847
return $this->resultsCount;
7948
}
8049
}

0 commit comments

Comments
 (0)