Skip to content
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
2 changes: 1 addition & 1 deletion config/vufind/searches.ini
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ always_display_reset_filters = false
; default_parameters[search] = "fq=field%3A%2Fmatch%2F&fq=another%3A2"
;
; Parameters can be defined for the following search contexts:
; * All contexts
; * All contexts not otherwise defined here
Comment thread
maccabeelevine marked this conversation as resolved.
; search Search
; retrieve Retrieve records
; similar Find similar records
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,130 +91,80 @@ public function testAttach()
}

/**
* Test the listener with a * catch-all.
* Data provider for testSearch().
*
* @return void
* @return \Iterator
*/
public function testDefaultParametersWithCatchAll()
public static function searchProvider(): \Iterator
{
$params = new ParamBag(
[
'fq' => [
'foo:value',
],
]
);

$command = $this->getMockSearchCommand(
$params,
'search',
$this->backends['secondary']->getIdentifier()
);
$listener = new DefaultParametersListener(
$this->backends['primary'],
[
'search' => 'foo=1&foo=2',
'*' => 'bar=3&bar',
]
);

// Check that nothing fails if params element is missing:
$event = new Event(
'pre',
$this->backends['secondary'],
compact('command')
);
$listener->onSearchPre($event);

$event = new Event(
'pre',
$this->backends['secondary'],
compact('params', 'command')
);
$listener->onSearchPre($event);

$this->assertEquals(null, $params->get('foo'));
$this->assertEquals(null, $params->get('bar'));

$command = $this->getMockSearchCommand(
$params,
'search',
$this->backends['primary']->getIdentifier()
);
$event = new Event(
'pre',
$this->backends['primary'],
compact('params', 'command')
);
$listener->onSearchPre($event);

$this->assertEquals(['1', '2'], $params->get('foo'));
$this->assertEquals(null, $params->get('bar'));

$command = $this->getMockSearchCommand(
$params,
'retrieve',
$this->backends['primary']->getIdentifier()
);
$event = new Event(
'pre',
$this->backends['primary'],
compact('params', 'command')
);
$listener->onSearchPre($event);

$this->assertEquals(['3'], $params->get('bar'));
yield 'catch all, search, wrong backend' => [true, 'search', 'secondary', null, null];
yield 'catch all, search, matching backend' => [true, 'search', 'primary', ['1', '2'], null];
yield 'catch all, retrieve, matching backend' => [true, 'retrieve', 'primary', null, ['3']];
yield 'no catch all, search, wrong backend' => [false, 'search', 'secondary', null, null];
yield 'no catch all, search, matching backend' => [false, 'search', 'primary', ['1', '2'], null];
Comment thread
maccabeelevine marked this conversation as resolved.
yield 'no catch all, retrieve, matching backend' => [false, 'retrieve', 'primary', null, null];
}

/**
* Test the listener without a * catch-all.
* Test that search behaves as expected.
*
* @param bool $catchAllConfig Whether the config should include the * search context
* @param string $searchContext 'search', 'retrieve', etc.
* @param string $searchBackendId 'primary' or 'secondary' as defined above
* @param ?array $expectFoo Expected 'foo' params
* @param ?array $expectBar Expected 'bar' params
*
* @return void
*/
public function testDefaultParametersWithoutCatchAll()
{
$params = new ParamBag(
#[\PHPUnit\Framework\Attributes\DataProvider('searchProvider')]
public function testSearch(
bool $catchAllConfig,
string $searchContext,
string $searchBackendId,
?array $expectFoo,
?array $expectBar
): void {
// Set up search
$params = new ParamBag(
[
'fq' => [
'foo:value',
],
]
);

$searchBackend = $this->backends[$searchBackendId];
$command = $this->getMockSearchCommand(
$params,
'search',
$this->backends['primary']->getIdentifier()
);
$listener = new DefaultParametersListener(
$this->backends['primary'],
[
'search' => 'foo=1&foo=2',
]
$searchContext,
$searchBackend->getIdentifier()
);

// Set up listener
$listenerConfig = [
'search' => 'foo=1&foo=2',
];
if ($catchAllConfig) {
$listenerConfig['*'] = 'bar=3&bar';
}
$listener = new DefaultParametersListener($this->backends['primary'], $listenerConfig);

// Check that nothing fails if params element is missing:
$event = new Event(
'pre',
$this->backends['primary'],
compact('params', 'command')
$searchBackend,
compact('command')
);
$listener->onSearchPre($event);

$this->assertEquals(['1', '2'], $params->get('foo'));
$this->assertEquals(null, $params->get('bar'));

$command = $this->getMockSearchCommand(
$params,
'retrieve',
$this->backends['primary']->getIdentifier()
);
// Check with params element
$event = new Event(
'pre',
$this->backends['primary'],
$searchBackend,
compact('params', 'command')
);
$listener->onSearchPre($event);

$this->assertEquals(null, $params->get('bar'));
$this->assertEquals($expectFoo, $params->get('foo'));
$this->assertEquals($expectBar, $params->get('bar'));
}
}
Loading