Skip to content

Issue 8131 - Use Redirect Factory to Allow Error Message Display on Advanced Search #16952

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 3 commits into from
Jul 27, 2018
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
7 changes: 4 additions & 3 deletions app/code/Magento/CatalogSearch/Controller/Advanced/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace Magento\CatalogSearch\Controller\Advanced;

use Magento\Catalog\Model\Layer\Resolver;
use Magento\CatalogSearch\Model\Advanced as ModelAdvanced;
use Magento\Framework\App\Action\Context;
use Magento\Framework\UrlFactory;
Expand Down Expand Up @@ -45,7 +44,7 @@ public function __construct(
}

/**
* @return void
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
Expand All @@ -58,7 +57,9 @@ public function execute()
$defaultUrl = $this->_urlFactory->create()
->addQueryParams($this->getRequest()->getQueryValue())
->getUrl('*/*/');
$this->getResponse()->setRedirect($this->_redirect->error($defaultUrl));
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setUrl($this->_redirect->error($defaultUrl));
return $resultRedirect;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\CatalogSearch\Test\Unit\Controller\Advanced;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ResultTest extends \PHPUnit\Framework\TestCase
{
public function testResultActionFiltersSetBeforeLoadLayout()
Expand Down Expand Up @@ -49,4 +52,90 @@ function ($added) use (&$filters) {
);
$instance->execute();
}

public function testUrlSetOnException()
{
$redirectResultMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
$redirectResultMock->expects($this->once())
->method('setUrl');

$redirectFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();

$redirectFactoryMock->expects($this->any())
->method('create')
->willReturn($redirectResultMock);

$catalogSearchAdvanced = $this->createPartialMock(
\Magento\CatalogSearch\Model\Advanced::class,
['addFilters']
);

$catalogSearchAdvanced->expects($this->once())->method('addFilters')->will(
$this->throwException(new \Magento\Framework\Exception\LocalizedException(
new \Magento\Framework\Phrase("Test Exception")
))
);

$responseMock = $this->createMock(\Magento\Framework\Webapi\Response::class);
$requestMock = $this->createPartialMock(
\Magento\Framework\App\Request\Http::class,
['getQueryValue']
);
$requestMock->expects($this->any())->method('getQueryValue')->willReturn(['key' => 'value']);

$redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
$redirectMock->expects($this->any())->method('error')->with('urlstring');

$messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);

$eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);

$contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
$contextMock->expects($this->any())
->method('getRequest')
->willReturn($requestMock);
$contextMock->expects($this->any())
->method('getResponse')
->willReturn($responseMock);
$contextMock->expects($this->any())
->method('getRedirect')
->willReturn($redirectMock);
$contextMock->expects($this->any())
->method('getMessageManager')
->willReturn($messageManagerMock);
$contextMock->expects($this->any())
->method('getEventManager')
->willReturn($eventManagerMock);
$contextMock->expects($this->any())
->method('getResultRedirectFactory')
->willReturn($redirectFactoryMock);

$urlMock = $this->createMock(\Magento\Framework\Url::class);
$urlMock->expects($this->once())
->method('addQueryParams')
->willReturnSelf();
$urlMock->expects($this->once())
->method('getUrl')
->willReturn("urlstring");

$urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
$urlFactoryMock->expects($this->once())
->method('create')
->will($this->returnValue($urlMock));

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

/** @var \Magento\CatalogSearch\Controller\Advanced\Result $instance */
$instance = $objectManager->getObject(
\Magento\CatalogSearch\Controller\Advanced\Result::class,
['context' => $contextMock,
'catalogSearchAdvanced' => $catalogSearchAdvanced,
'urlFactory' => $urlFactoryMock
]
);
$this->assertEquals($redirectResultMock, $instance->execute());
}
}