Skip to content

Commit a5869d5

Browse files
ENGCOM-7220: Implement ActionInterface for /search/term/popular #27462
- Merge Pull Request #27462 from Bartlomiejsz/magento2:feature/action_interface_search_term_popular - Merged commits: 1. 89abdaf 2. 8e93b14 3. d073d72
2 parents 176ba2b + d073d72 commit a5869d5

File tree

2 files changed

+153
-31
lines changed

2 files changed

+153
-31
lines changed

app/code/Magento/Search/Controller/Term/Popular.php

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,78 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Search\Controller\Term;
79

8-
use Magento\Framework\App\Action\Action;
9-
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\App\Action\HttpGetActionInterface;
1011
use Magento\Framework\App\Config\ScopeConfigInterface;
11-
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Controller\Result\ForwardFactory as ResultForwardFactory;
13+
use Magento\Framework\View\Result\PageFactory as ResultPageFactory;
1214
use Magento\Store\Model\ScopeInterface;
13-
use Magento\Framework\Controller\ResultFactory;
1415

15-
class Popular extends Action
16+
/**
17+
* Popular search terms page
18+
*/
19+
class Popular implements HttpGetActionInterface
1620
{
21+
private const XML_PATH_SEO_SEARCH_TERMS = 'catalog/seo/search_terms';
22+
1723
/**
18-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
24+
* @var ResultForwardFactory
1925
*/
20-
protected $scopeConfig;
26+
private $resultForwardFactory;
2127

2228
/**
23-
* @param \Magento\Framework\App\Action\Context $context
24-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
29+
* @var ResultPageFactory
2530
*/
26-
public function __construct(Context $context, ScopeConfigInterface $scopeConfig)
27-
{
31+
private $resultPageFactory;
32+
33+
/**
34+
* @var ScopeConfigInterface
35+
*/
36+
private $scopeConfig;
37+
38+
/**
39+
* @param ResultForwardFactory $resultForwardFactory
40+
* @param ResultPageFactory $resultPageFactory
41+
* @param ScopeConfigInterface $scopeConfig
42+
*/
43+
public function __construct(
44+
ResultForwardFactory $resultForwardFactory,
45+
ResultPageFactory $resultPageFactory,
46+
ScopeConfigInterface $scopeConfig
47+
) {
48+
$this->resultForwardFactory = $resultForwardFactory;
49+
$this->resultPageFactory = $resultPageFactory;
2850
$this->scopeConfig = $scopeConfig;
29-
parent::__construct($context);
3051
}
3152

3253
/**
33-
* Dispatch request
34-
*
35-
* @param \Magento\Framework\App\RequestInterface $request
36-
* @return \Magento\Framework\App\ResponseInterface
37-
* @throws \Magento\Framework\Exception\NotFoundException
54+
* @inheritDoc
3855
*/
39-
public function dispatch(RequestInterface $request)
56+
public function execute()
4057
{
41-
$searchTerms = $this->scopeConfig->getValue(
42-
'catalog/seo/search_terms',
43-
ScopeInterface::SCOPE_STORE
44-
);
45-
if (!$searchTerms) {
46-
$this->_redirect('noroute');
47-
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
58+
if (!$this->checkEnabledSearchTerms()) {
59+
$resultForward = $this->resultForwardFactory->create();
60+
$resultForward->forward('noroute');
61+
62+
return $resultForward;
4863
}
49-
return parent::dispatch($request);
64+
65+
return $this->resultPageFactory->create();
5066
}
5167

5268
/**
53-
* @return \Magento\Framework\View\Result\Page
69+
* Check if search terms are enabled
70+
*
71+
* @return bool
5472
*/
55-
public function execute()
73+
private function checkEnabledSearchTerms(): bool
5674
{
57-
/** @var \Magento\Framework\View\Result\Page $resultPage */
58-
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
59-
return $resultPage;
75+
return $this->scopeConfig->isSetFlag(
76+
self::XML_PATH_SEO_SEARCH_TERMS,
77+
ScopeInterface::SCOPE_STORE
78+
);
6079
}
6180
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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\Search\Test\Unit\Controller\Term;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Controller\Result\Forward as ResultForward;
12+
use Magento\Framework\Controller\Result\ForwardFactory as ResultForwardFactory;
13+
use Magento\Framework\View\Result\Page as ResultPage;
14+
use Magento\Framework\View\Result\PageFactory as ResultPageFactory;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\Search\Controller\Term\Popular;
17+
use Magento\Store\Model\ScopeInterface;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class PopularTest extends TestCase
22+
{
23+
private const XML_PATH_SEO_SEARCH_TERMS = 'catalog/seo/search_terms';
24+
25+
/**
26+
* @var Popular
27+
*/
28+
private $action;
29+
30+
/**
31+
* @var ResultForwardFactory|MockObject
32+
*/
33+
private $resultForwardFactoryMock;
34+
35+
/**
36+
* @var ResultPageFactory|MockObject
37+
*/
38+
private $resultPageFactoryMock;
39+
40+
/**
41+
* @var ScopeConfigInterface|MockObject
42+
*/
43+
private $scopeConfigMock;
44+
45+
protected function setUp()
46+
{
47+
$this->resultForwardFactoryMock = $this->getMockBuilder(ResultForwardFactory::class)
48+
->disableOriginalConstructor()
49+
->getMock();
50+
51+
$this->resultPageFactoryMock = $this->getMockBuilder(ResultPageFactory::class)
52+
->disableOriginalConstructor()
53+
->getMock();
54+
55+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
56+
->getMockForAbstractClass();
57+
58+
$objectManager = new ObjectManager($this);
59+
$this->action = $objectManager->getObject(
60+
Popular::class,
61+
[
62+
'resultForwardFactory' => $this->resultForwardFactoryMock,
63+
'resultPageFactory' => $this->resultPageFactoryMock,
64+
'scopeConfig' => $this->scopeConfigMock
65+
]
66+
);
67+
}
68+
69+
public function testResult()
70+
{
71+
$this->scopeConfigMock->expects($this->once())
72+
->method('isSetFlag')
73+
->with(static::XML_PATH_SEO_SEARCH_TERMS, ScopeInterface::SCOPE_STORE)
74+
->willReturn(true);
75+
$resultPageMock = $this->getMockBuilder(ResultPage::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->resultPageFactoryMock->expects($this->once())
79+
->method('create')
80+
->willReturn($resultPageMock);
81+
82+
$this->assertSame($resultPageMock, $this->action->execute());
83+
}
84+
85+
public function testResultWithDisabledPage()
86+
{
87+
$this->scopeConfigMock->expects($this->once())
88+
->method('isSetFlag')
89+
->with(static::XML_PATH_SEO_SEARCH_TERMS, ScopeInterface::SCOPE_STORE)
90+
->willReturn(false);
91+
$resultForwardMock = $this->getMockBuilder(ResultForward::class)
92+
->disableOriginalConstructor()
93+
->getMock();
94+
$this->resultForwardFactoryMock->expects($this->once())
95+
->method('create')
96+
->willReturn($resultForwardMock);
97+
$resultForwardMock->expects($this->once())
98+
->method('forward')
99+
->with('noroute');
100+
101+
$this->assertSame($resultForwardMock, $this->action->execute());
102+
}
103+
}

0 commit comments

Comments
 (0)