Skip to content

Commit

Permalink
SearchMemory: refactor to add getLastSearchUrl method (vufind-org#3845)
Browse files Browse the repository at this point in the history
  • Loading branch information
rominail authored and LuomaJuha committed Oct 1, 2024
1 parent 608fbcc commit 73b3917
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 41 deletions.
23 changes: 17 additions & 6 deletions module/VuFind/src/VuFind/View/Helper/Root/SearchMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,27 @@ public function __construct(Memory $memory)
* @return string
*/
public function getLastSearchLink($link, $prefix = '', $suffix = '')
{
if ($url = $this->getLastSearchUrl()) {
$escaper = $this->getView()->plugin('escapeHtml');
return $prefix . '<a href="' . $escaper($url) . '">' . $link . '</a>' . $suffix;
}
return '';
}

/**
* If a previous search is recorded in the session, return its URL
*
* @return string|null
*/
public function getLastSearchUrl(): ?string
{
if ($lastSearch = $this->getLastSearch()) {
$searchClassId = $lastSearch->getBackendId();
$params = $lastSearch->getParams();
// Use last settings for params that are not stored in the search:
foreach (['limit', 'view', 'sort'] as $setting) {
$value
= $this->memory->retrieveLastSetting($searchClassId, $setting);
$value = $this->memory->retrieveLastSetting($searchClassId, $setting);
if ($value) {
$method = 'set' . ucfirst($setting);
$params->$method($value);
Expand All @@ -101,11 +114,9 @@ public function getLastSearchLink($link, $prefix = '', $suffix = '')

$url .= $queryHelper->getParams(false);

$escaper = $this->getView()->plugin('escapeHtml');
return $prefix . '<a href="' . $escaper($url) . '">' . $link . '</a>'
. $suffix;
return $url;
}
return '';
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@

use Laminas\Stdlib\Parameters;
use Laminas\View\Helper\Url;
use Laminas\View\Renderer\PhpRenderer;
use PHPUnit\Framework\MockObject\MockObject;
use VuFind\Search\Memory;
use VuFind\Search\Solr\Options;
use VuFind\Search\Solr\Params;
use VuFind\View\Helper\Root\SearchMemory;
use VuFind\Search\Solr\Results;
use VuFind\Search\UrlQueryHelper;
use VuFind\View\Helper\Root\SearchParams;

/**
Expand All @@ -51,7 +54,75 @@ class SearchMemoryTest extends \PHPUnit\Framework\TestCase
use \VuFindTest\Feature\ViewTrait;

/**
* Test search memory helper
* Fake base path to use during tests.
*
* @var string
*/
protected $searchBasePath = '/foo/bar';

/**
* Fake route name to use during tests.
*
* @var string
*/
protected $searchRoute = 'foo-bar';

/**
* Get a mock Solr Params object.
*
* @param array $requestArray Request array to populate Params from.
*
* @return MockObject&Params
*/
protected function getMockSolrParams(array $requestArray = []): MockObject&Params
{
$solrOptions = $this->createMock(Options::class);
$solrOptions->expects($this->once())->method('getSearchAction')->willReturn($this->searchRoute);
$solrParams = $this->createMock(Params::class);
$solrParams->expects($this->any())->method('getOptions')->willReturn($solrOptions);
return $solrParams;
}

/**
* Get a mock Solr Results object.
*
* @return MockObject&Results
*/
protected function getMockSolrResults(): MockObject&Results
{
$solrParams = $this->getMockSolrParams();
$solrOptions = $solrParams->getOptions();
$solrOptions->expects($this->once())->method('getSearchAction')->willReturn($this->searchRoute);
$mockQueryHelper = $this->createMock(UrlQueryHelper::class);
$results = $this->createMock(Results::class);
$results->expects($this->any())->method('getOptions')->willReturn($solrOptions);
$results->expects($this->any())->method('getParams')->willReturn($solrParams);
$results->expects($this->any())->method('getUrlQuery')->willReturn($mockQueryHelper);
return $results;
}

/**
* Get a configured view object with relevant helpers for testing.
*
* @param Params $solrParams Configured Solr Params object
*
* @return PhpRenderer
*/
protected function getConfiguredView(Params $solrParams): PhpRenderer
{
$url = $this->createMock(Url::class);
$url->expects($this->any())->method('__invoke')
->with($this->equalTo($this->searchRoute))
->willReturn($this->searchBasePath);
$searchParams = $this->createMock(SearchParams::class);
$searchParams->expects($this->any())->method('__invoke')
->with($this->equalTo('Solr'))->willReturn($solrParams);
$plugins = compact('searchParams', 'url');
return $this->getPhpRenderer($plugins);
}

/**
* Test search memory helper's getLastSearchParams() method.
*
* @param string $query Query to parse
* @param array $expectedRequestArray Expected request parameters to parse
Expand All @@ -64,45 +135,78 @@ public function testGetLastSearchParams(
string $query,
array $expectedRequestArray
): void {
$expectedRequest = new Parameters($expectedRequestArray);
$searchRoute = 'foo-bar';
$searchBasePath = '/foo/bar';
$lastSearchUrl = $searchBasePath . $query;

$memory = $this->getMockBuilder(Memory::class)
->disableOriginalConstructor()
->getMock();
$memory->expects($this->once())->method('retrieveSearch')
->will($this->returnValue($lastSearchUrl));
$memory = $this->createMock(Memory::class);
$memory->expects($this->once())->method('retrieveSearch')->willReturn($this->searchBasePath . $query);
$helper = $this->getSearchMemoryViewHelper($memory);
$url = $this->getMockBuilder(Url::class)
->disableOriginalConstructor()
->getMock();
$url->expects($this->once())->method('__invoke')
->with($this->equalTo($searchRoute))
->will($this->returnValue($searchBasePath));
$solrOptions = $this->getMockBuilder(Options::class)
->disableOriginalConstructor()
->getMock();
$solrOptions->expects($this->once())->method('getSearchAction')
->will($this->returnValue($searchRoute));
$solrParams = $this->getMockBuilder(Params::class)
->disableOriginalConstructor()
->getMock();
$solrParams->expects($this->once())->method('getOptions')
->will($this->returnValue($solrOptions));
$solrParams = $this->getMockSolrParams($expectedRequestArray);
$expectedRequest = new Parameters($expectedRequestArray);
$solrParams->expects($this->once())->method('initFromRequest')
->with($this->equalTo($expectedRequest));
$searchParams = $this->getMockBuilder(SearchParams::class)
->disableOriginalConstructor()
->getMock();
$searchParams->expects($this->once())->method('__invoke')
->with($this->equalTo('Solr'))->will($this->returnValue($solrParams));
$plugins = compact('searchParams', 'url');
$helper->setView($this->getPhpRenderer($plugins));
$helper->setView($this->getConfiguredView($solrParams));
$this->assertEquals($solrParams, $helper->getLastSearchParams('Solr'));
}

/**
* Test search memory helper's getLastSearchUrl() method with a saved search.
*
* @return void
*/
public function testGetLastSearchUrlWithSavedSearch(): void
{
$results = $this->getMockSolrResults();
$memory = $this->createMock(Memory::class);
$memory->expects($this->once())->method('getLastSearch')->willReturn($results);
$helper = $this->getSearchMemoryViewHelper($memory);
$helper->setView($this->getConfiguredView($results->getParams()));
$this->assertEquals('/foo/bar', $helper->getLastSearchUrl('Solr'));
}

/**
* Test search memory helper's getLastSearchUrl() method with no saved search.
*
* @return void
*/
public function testGetLastSearchUrlWithoutSavedSearch(): void
{
$memory = $this->createMock(Memory::class);
$memory->expects($this->once())->method('getLastSearch')->willReturn(null);
$helper = $this->getSearchMemoryViewHelper($memory);
$helper->setView($this->getConfiguredView($this->createMock(Params::class)));
$this->assertNull($helper->getLastSearchUrl('Solr'));
}

/**
* Test search memory helper's getLastSearchLink() method with a saved search.
*
* @return void
*/
public function testGetLastSearchLinkWithSavedSearch(): void
{
$results = $this->getMockSolrResults();
$memory = $this->createMock(Memory::class);
$memory->expects($this->once())->method('getLastSearch')->willReturn($results);
$helper = $this->getSearchMemoryViewHelper($memory);
$helper->setView($this->getConfiguredView($results->getParams()));
$this->assertEquals(
'prefix<a href="/foo/bar">Solr</a>suffix',
$helper->getLastSearchLink('Solr', 'prefix', 'suffix')
);
}

/**
* Test search memory helper's getLastSearchLink() method with no saved search.
*
* @return void
*/
public function testGetLastSearchLinkWithoutSavedSearch(): void
{
$memory = $this->createMock(Memory::class);
$memory->expects($this->once())->method('getLastSearch')->willReturn(null);
$helper = $this->getSearchMemoryViewHelper($memory);
$helper->setView($this->getConfiguredView($this->createMock(Params::class)));
$this->assertEquals('', $helper->getLastSearchLink('Solr', 'prefix', 'suffix'));
}

/**
* Data provider for testGetLastSearchParams()
*
Expand Down

0 comments on commit 73b3917

Please sign in to comment.