Skip to content

Commit ea58fee

Browse files
author
Oleksii Korshenko
authored
MAGETWO-85637: [Backport] magento#10743: Magento 2 is not showing Popular Search Terms magento#1024
2 parents aa965b9 + 41e33fd commit ea58fee

File tree

6 files changed

+209
-5
lines changed

6 files changed

+209
-5
lines changed

app/code/Magento/Search/Block/Term.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ protected function _loadTerms()
9595
continue;
9696
}
9797
$term->setRatio(($term->getPopularity() - $this->_minPopularity) / $range);
98-
$temp[$term->getName()] = $term;
99-
$termKeys[] = $term->getName();
98+
$temp[$term->getData('query_text')] = $term;
99+
$termKeys[] = $term->getData('query_text');
100100
}
101101
natcasesort($termKeys);
102102

@@ -128,7 +128,7 @@ public function getSearchUrl($obj)
128128
* url encoding will be done in Url.php http_build_query
129129
* so no need to explicitly called urlencode for the text
130130
*/
131-
$url->setQueryParam('q', $obj->getName());
131+
$url->setQueryParam('q', $obj->getData('query_text'));
132132
return $url->getUrl('catalogsearch/result');
133133
}
134134

app/code/Magento/Search/view/frontend/templates/term.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<li class="item">
1414
<a href="<?= /* @escapeNotVerified */ $block->getSearchUrl($_term) ?>"
1515
style="font-size:<?= /* @escapeNotVerified */ $_term->getRatio()*70+75 ?>%;">
16-
<?= $block->escapeHtml($_term->getName()) ?>
16+
<?= $block->escapeHtml($_term->getData('query_text')) ?>
1717
</a>
1818
</li>
1919
<?php endforeach; ?>

dev/tests/integration/testsuite/Magento/CatalogSearch/Block/TermTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function setUp()
2424
public function testGetSearchUrl()
2525
{
2626
$query = uniqid();
27-
$obj = new \Magento\Framework\DataObject(['name' => $query]);
27+
$obj = new \Magento\Framework\DataObject(['query_text' => $query]);
2828
$this->assertStringEndsWith("/catalogsearch/result/?q={$query}", $this->_block->getSearchUrl($obj));
2929
}
3030
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Search\Block;
8+
9+
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Framework\View\LayoutInterface;
11+
12+
/**
13+
* Tests Magento\Search\Block\Term.
14+
*
15+
* @magentoAppIsolation enabled
16+
* @magentoDbIsolation enabled
17+
*/
18+
class TermTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var ObjectManagerInterface
22+
*/
23+
private $objectManager;
24+
25+
/**
26+
* @var Term
27+
*/
28+
private $term;
29+
30+
/**
31+
* Tests Magento\Search\Block\Term::GetTerms.
32+
*
33+
* @magentoDataFixture Magento/Search/_files/query.php
34+
* @dataProvider getTermsDataProvider
35+
* @param array $expected
36+
*/
37+
public function testGetTerms(array $expected)
38+
{
39+
$result = $this->term->getTerms();
40+
$actual = array_map(
41+
function ($object) {
42+
return $object->setUpdatedAt(null)->getData();
43+
},
44+
$result
45+
);
46+
47+
self::assertEquals(
48+
$expected,
49+
$actual
50+
);
51+
}
52+
53+
/**
54+
* Data provider for testGetTerms.
55+
*
56+
* @return array
57+
*/
58+
public function getTermsDataProvider()
59+
{
60+
return [
61+
[
62+
[
63+
'1st query' =>
64+
[
65+
'query_id' => '1',
66+
'query_text' => '1st query',
67+
'num_results' => '1',
68+
'popularity' => '5',
69+
'redirect' => null,
70+
'store_id' => '1',
71+
'display_in_terms' => '1',
72+
'is_active' => '1',
73+
'is_processed' => '1',
74+
'updated_at' => null,
75+
'ratio' => 0.44444444444444,
76+
],
77+
'2nd query' =>
78+
[
79+
'query_id' => '2',
80+
'query_text' => '2nd query',
81+
'num_results' => '1',
82+
'popularity' => '10',
83+
'redirect' => null,
84+
'store_id' => '1',
85+
'display_in_terms' => '1',
86+
'is_active' => '1',
87+
'is_processed' => '1',
88+
'updated_at' => null,
89+
'ratio' => 1,
90+
],
91+
'3rd query' =>
92+
[
93+
'query_id' => '3',
94+
'query_text' => '3rd query',
95+
'num_results' => '1',
96+
'popularity' => '1',
97+
'redirect' => null,
98+
'store_id' => '1',
99+
'display_in_terms' => '1',
100+
'is_active' => '1',
101+
'is_processed' => '1',
102+
'updated_at' => null,
103+
'ratio' => 0,
104+
],
105+
],
106+
],
107+
];
108+
}
109+
110+
protected function setUp()
111+
{
112+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
113+
$this->term = $this->objectManager->get(
114+
LayoutInterface::class
115+
)->createBlock(
116+
Term::class
117+
);
118+
}
119+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
7+
8+
$queries = [
9+
[
10+
'text' => '1st query',
11+
'results' => 1,
12+
'popularity' => 5,
13+
'display' => 1,
14+
'active' => 1,
15+
'processed' => 1
16+
],
17+
[
18+
'text' => '2nd query',
19+
'results' => 1,
20+
'popularity' => 10,
21+
'display' => 1,
22+
'active' => 1,
23+
'processed' => 1
24+
],
25+
[
26+
'text' => '3rd query',
27+
'results' => 1,
28+
'popularity' => 1,
29+
'display' => 1,
30+
'active' => 1,
31+
'processed' => 1
32+
],
33+
[
34+
'text' => '4th query',
35+
'results' => 0,
36+
'popularity' => 1,
37+
'display' => 1,
38+
'active' => 1,
39+
'processed' => 1
40+
],
41+
];
42+
43+
foreach ($queries as $queryData) {
44+
/** @var $queryData \Magento\Search\Model\Query */
45+
$query = $objectManager->create(\Magento\Search\Model\Query::class);
46+
$query->setStoreId(1);
47+
$query->setQueryText(
48+
$queryData['text']
49+
)->setNumResults(
50+
$queryData['results']
51+
)->setPopularity(
52+
$queryData['popularity']
53+
)->setDisplayInTerms(
54+
$queryData['display']
55+
)->setIsActive(
56+
$queryData['active']
57+
)->setIsProcessed(
58+
$queryData['processed']
59+
);
60+
$query->save();
61+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
7+
8+
/** @var $query \Magento\Search\Model\Query */
9+
$query = $objectManager->get(\Magento\Search\Model\Query::class);
10+
11+
$queries = [
12+
'1st query',
13+
'2nd query',
14+
'3rd query',
15+
'4th query',
16+
];
17+
18+
foreach ($queries as $queryText) {
19+
try {
20+
$query->loadByQueryText($queryText);
21+
$query->delete();
22+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
23+
}
24+
}

0 commit comments

Comments
 (0)