Skip to content

Commit

Permalink
#10734: Magento 2 is not showing Popular Search Terms [backport]
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bystritsky committed Dec 13, 2017
1 parent 321278b commit 1f77aaa
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 5 deletions.
6 changes: 3 additions & 3 deletions app/code/Magento/Search/Block/Term.php
Expand Up @@ -95,8 +95,8 @@ protected function _loadTerms()
continue;
}
$term->setRatio(($term->getPopularity() - $this->_minPopularity) / $range);
$temp[$term->getName()] = $term;
$termKeys[] = $term->getName();
$temp[$term->getData('query_text')] = $term;
$termKeys[] = $term->getData('query_text');
}
natcasesort($termKeys);

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

Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Search/view/frontend/templates/term.phtml
Expand Up @@ -13,7 +13,7 @@
<li class="item">
<a href="<?= /* @escapeNotVerified */ $block->getSearchUrl($_term) ?>"
style="font-size:<?= /* @escapeNotVerified */ $_term->getRatio()*70+75 ?>%;">
<?= $block->escapeHtml($_term->getName()) ?>
<?= $block->escapeHtml($_term->getData('query_text')) ?>
</a>
</li>
<?php endforeach; ?>
Expand Down
Expand Up @@ -24,7 +24,7 @@ protected function setUp()
public function testGetSearchUrl()
{
$query = uniqid();
$obj = new \Magento\Framework\DataObject(['name' => $query]);
$obj = new \Magento\Framework\DataObject(['query_text' => $query]);
$this->assertStringEndsWith("/catalogsearch/result/?q={$query}", $this->_block->getSearchUrl($obj));
}
}
117 changes: 117 additions & 0 deletions dev/tests/integration/testsuite/Magento/Search/Block/TermTest.php
@@ -0,0 +1,117 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Search\Block;

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;

/**
* Tests Magento\Search\Block\Term.
*
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
class TermTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var Term
*/
private $term;

/**
* Tests Magento\Search\Block\Term::GetTerms.
*
* @magentoDataFixture Magento/Search/_files/query.php
* @dataProvider getTermsDataProvider
* @param array $expected
*/
public function testGetTerms(array $expected)
{
$result = $this->term->getTerms();
$actual = array_map(function ($object) {
return $object->setUpdatedAt(null)->getData();
},
$result);

self::assertEquals(
$expected,
$actual
);
}

/**
* Data provider for testGetTerms.
*
* @return array
*/
public function getTermsDataProvider()
{
return [
[
[
'1st query' =>
[
'query_id' => '1',
'query_text' => '1st query',
'num_results' => '1',
'popularity' => '5',
'redirect' => null,
'store_id' => '1',
'display_in_terms' => '1',
'is_active' => '1',
'is_processed' => '1',
'updated_at' => null,
'ratio' => 0.44444444444444,
],
'2nd query' =>
[
'query_id' => '2',
'query_text' => '2nd query',
'num_results' => '1',
'popularity' => '10',
'redirect' => null,
'store_id' => '1',
'display_in_terms' => '1',
'is_active' => '1',
'is_processed' => '1',
'updated_at' => null,
'ratio' => 1,
],
'3rd query' =>
[
'query_id' => '3',
'query_text' => '3rd query',
'num_results' => '1',
'popularity' => '1',
'redirect' => null,
'store_id' => '1',
'display_in_terms' => '1',
'is_active' => '1',
'is_processed' => '1',
'updated_at' => null,
'ratio' => 0,
],
],
],
];
}

protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->term = $this->objectManager->get(
LayoutInterface::class
)->createBlock(
Term::class
);
}
}
61 changes: 61 additions & 0 deletions dev/tests/integration/testsuite/Magento/Search/_files/query.php
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

$queries = [
[
'text' => '1st query',
'results' => 1,
'popularity' => 5,
'display' => 1,
'active' => 1,
'processed' => 1
],
[
'text' => '2nd query',
'results' => 1,
'popularity' => 10,
'display' => 1,
'active' => 1,
'processed' => 1
],
[
'text' => '3rd query',
'results' => 1,
'popularity' => 1,
'display' => 1,
'active' => 1,
'processed' => 1
],
[
'text' => '4th query',
'results' => 0,
'popularity' => 1,
'display' => 1,
'active' => 1,
'processed' => 1
],
];

foreach ($queries as $queryData) {
/** @var $queryData \Magento\Search\Model\Query */
$query = $objectManager->create(\Magento\Search\Model\Query::class);
$query->setStoreId(1);
$query->setQueryText(
$queryData['text']
)->setNumResults(
$queryData['results']
)->setPopularity(
$queryData['popularity']
)->setDisplayInTerms(
$queryData['display']
)->setIsActive(
$queryData['active']
)->setIsProcessed(
$queryData['processed']
);
$query->save();
}
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var $query \Magento\Search\Model\Query */
$query = $objectManager->get(\Magento\Search\Model\Query::class);

$queries = [
'1st query',
'2nd query',
'3rd query',
'4th query',
];

foreach ($queries as $queryText) {
try {
$query->loadByQueryText($queryText);
$query->delete();
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
}
}

0 comments on commit 1f77aaa

Please sign in to comment.