From 73b39172893cfb118650c3d95657f75df0451b25 Mon Sep 17 00:00:00 2001 From: rominail Date: Mon, 29 Jul 2024 15:58:26 -0400 Subject: [PATCH] SearchMemory: refactor to add getLastSearchUrl method (#3845) --- .../VuFind/View/Helper/Root/SearchMemory.php | 23 ++- .../View/Helper/Root/SearchMemoryTest.php | 174 ++++++++++++++---- 2 files changed, 156 insertions(+), 41 deletions(-) diff --git a/module/VuFind/src/VuFind/View/Helper/Root/SearchMemory.php b/module/VuFind/src/VuFind/View/Helper/Root/SearchMemory.php index 3eb0a80d9547..c6928656be61 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/SearchMemory.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/SearchMemory.php @@ -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 . '' . $link . '' . $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); @@ -101,11 +114,9 @@ public function getLastSearchLink($link, $prefix = '', $suffix = '') $url .= $queryHelper->getParams(false); - $escaper = $this->getView()->plugin('escapeHtml'); - return $prefix . '' . $link . '' - . $suffix; + return $url; } - return ''; + return null; } /** diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/SearchMemoryTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/SearchMemoryTest.php index e1e8f8c95bcb..5c7b8a2486f9 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/SearchMemoryTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/SearchMemoryTest.php @@ -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; /** @@ -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 @@ -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( + 'prefixSolrsuffix', + $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() *