Skip to content

Warning when Search Terms page is opened by clicking option at the footer #25246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions app/code/Magento/Search/Block/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
*/
namespace Magento\Search\Block;

use Magento\Framework\DataObject;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlFactory;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Search\Model\ResourceModel\Query\CollectionFactory;

/**
* Terms and conditions block
*
* @api
* @since 100.0.2
*/
Expand All @@ -37,15 +41,11 @@ class Term extends Template
protected $_maxPopularity;

/**
* Url factory
*
* @var UrlFactory
*/
protected $_urlFactory;

/**
* Query collection factory
*
* @var CollectionFactory
*/
protected $_queryCollectionFactory;
Expand All @@ -71,17 +71,17 @@ public function __construct(
* Load terms and try to sort it by names
*
* @return $this
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
protected function _loadTerms()
{
if (empty($this->_terms)) {
$this->_terms = [];
$terms = $this->_queryCollectionFactory->create()->setPopularQueryFilter(
$this->_storeManager->getStore()->getId()
)->setPageSize(
100
)->load()->getItems();
$terms = $this->_queryCollectionFactory->create()
->setPopularQueryFilter($this->_storeManager->getStore()->getId())
->setPageSize(100)
->load()
->getItems();

if (count($terms) == 0) {
return $this;
Expand All @@ -91,6 +91,7 @@ protected function _loadTerms()
$this->_minPopularity = end($terms)->getPopularity();
$range = $this->_maxPopularity - $this->_minPopularity;
$range = $range == 0 ? 1 : $range;
$termKeys = [];
foreach ($terms as $term) {
if (!$term->getPopularity()) {
continue;
Expand All @@ -99,6 +100,7 @@ protected function _loadTerms()
$temp[$term->getQueryText()] = $term;
$termKeys[] = $term->getQueryText();
}

natcasesort($termKeys);

foreach ($termKeys as $termKey) {
Expand All @@ -109,8 +111,10 @@ protected function _loadTerms()
}

/**
* Load and return terms
*
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
public function getTerms()
{
Expand All @@ -119,7 +123,9 @@ public function getTerms()
}

/**
* @param \Magento\Framework\DataObject $obj
* Return search url
*
* @param DataObject $obj
* @return string
*/
public function getSearchUrl($obj)
Expand All @@ -135,6 +141,8 @@ public function getSearchUrl($obj)
}

/**
* Return max popularity
*
* @return int
*/
public function getMaxPopularity()
Expand All @@ -143,6 +151,8 @@ public function getMaxPopularity()
}

/**
* Return min popularity
*
* @return int
*/
public function getMinPopularity()
Expand Down
173 changes: 173 additions & 0 deletions app/code/Magento/Search/Test/Unit/Block/TermsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Search\Test\Unit\Block;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Url;
use Magento\Framework\UrlFactory;
use Magento\Framework\View\Element\Template\Context;
use Magento\Search\Block\Term;
use Magento\Search\Model\Query;
use Magento\Search\Model\ResourceModel\Query\Collection;
use Magento\Search\Model\ResourceModel\Query\CollectionFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Tests for Terms block
*/
class TermsTest extends TestCase
{
/**
* @var Context|MockObject
*/
private $contextMock;

/**
* @var CollectionFactory|MockObject
*/
private $collectionFactoryMock;

/**
* @var UrlFactory|MockObject
*/
private $urlFactoryMock;

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

/**
* @var StoreManager
*/
private $storeManagerMock;

/**
* @inheritdoc
*/
public function setUp()
{
$objectManager = new ObjectManager($this);

$this->contextMock = $this->createMock(Context::class);
$this->collectionFactoryMock = $this->createMock(CollectionFactory::class);
$this->urlFactoryMock = $this->createMock(UrlFactory::class);
$this->storeManagerMock = $this->createMock(StoreManager::class);

$this->contextMock->expects($this->once())
->method('getStoreManager')
->willReturn($this->storeManagerMock);
$this->termsModel = $objectManager->getObject(
Term::class,
[
'context' => $this->contextMock,
'_queryCollectionFactory' => $this->collectionFactoryMock,
'_urlFactory' => $this->urlFactoryMock
]
);
}

/**
* Verify terms
*
* @dataProvider termKeysProvider
* @param string $termKey
* @param bool $popularity
*/
public function testGetTerms(string $termKey, bool $popularity): void
{
$terms = $this->createMock(Collection::class);
$dataObjectMock = $this->getMockBuilder(Query::class)
->disableOriginalConstructor()
->setMethods(['getPopularity', 'getQueryText'])
->getMock();
$storeMock = $this->createMock(Store::class);

$this->storeManagerMock->expects($this->once())
->method('getStore')
->willReturn($storeMock);
$storeMock->expects($this->once())
->method('getId')
->willReturn(1);

$this->collectionFactoryMock->expects($this->once())
->method('create')
->willReturn($terms);
$terms->expects($this->once())
->method('setPopularQueryFilter')
->willReturnSelf();
$terms->expects($this->once())
->method('setPageSize')
->willReturnSelf();
$terms->expects($this->once())
->method('load')
->willReturnSelf();
$terms->expects($this->once())
->method('getItems')
->willReturn([$dataObjectMock]);
$dataObjectMock->expects($this->exactly(!$popularity ? 3 : 4))
->method('getPopularity')
->willReturn($popularity);
$dataObjectMock->expects($this->exactly(!$popularity ? 0 : 2))
->method('getQueryText')
->willReturn($termKey);

$this->assertEquals(!$popularity ? [] : [$termKey => $dataObjectMock], $this->termsModel->getTerms());
}

/**
* Verify get search Url
*
* @return void
*/
public function testGetSearchResult(): void
{
$urlMock = $this->getMockBuilder(Url::class)
->disableOriginalConstructor()
->setMethods(['setQueryParam', 'getUrl'])
->getMock();

$dataObjectMock = $this->getMockBuilder(Query::class)
->disableOriginalConstructor()
->setMethods(['getPopularity', 'getQueryText'])
->getMock();
$this->urlFactoryMock->expects($this->once())
->method('create')
->willReturn($urlMock);
$dataObjectMock->expects($this->once())
->method('getQueryText')
->willReturn('url');
$urlMock->expects($this->once())->method('setQueryParam');
$urlMock->expects($this->once())
->method('getUrl')
->with('catalogsearch/result')
->willReturn('url');

$this->assertEquals('url', $this->termsModel->getSearchUrl($dataObjectMock));
}

/**
* Terms data key provider
*
* @return array
*/
public function termKeysProvider(): array
{
return [
[
'search',
true
],
[
'',
false
]
];
}
}