Skip to content
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

[9.1] Fix conditional filter listener to check the backend id. #3513

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected function createListeners(Backend $backend)
isset($search->ConditionalHiddenFilters)
&& $search->ConditionalHiddenFilters->count() > 0
) {
$this->getInjectConditionalFilterListener($search)->attach($events);
$this->getInjectConditionalFilterListener($backend, $search)->attach($events);
}

// Spellcheck
Expand Down Expand Up @@ -693,13 +693,15 @@ protected function getInjectHighlightingListener(
/**
* Get a Conditional Filter Listener
*
* @param Config $search Search configuration
* @param BackendInterface $backend Search backend
* @param Config $search Search configuration
*
* @return InjectConditionalFilterListener
*/
protected function getInjectConditionalFilterListener(Config $search)
protected function getInjectConditionalFilterListener(BackendInterface $backend, Config $search)
{
$listener = new InjectConditionalFilterListener(
$backend,
$search->ConditionalHiddenFilters->toArray()
);
$listener->setAuthorizationService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Laminas\EventManager\EventInterface;
use Laminas\EventManager\SharedEventManagerInterface;
use LmcRbacMvc\Service\AuthorizationServiceAwareTrait;
use VuFindSearch\Backend\BackendInterface;
use VuFindSearch\Service;

use function is_array;
Expand Down Expand Up @@ -66,11 +67,12 @@ class InjectConditionalFilterListener
/**
* Constructor.
*
* @param array $searchConf Search configuration parameters
* @param BackendInterface $backend Backend
* @param array $searchConf Search configuration parameters
*
* @return void
*/
public function __construct($searchConf)
public function __construct(protected BackendInterface $backend, $searchConf)
{
$this->filters = $searchConf;
$this->filterList = [];
Expand Down Expand Up @@ -134,12 +136,17 @@ protected function addConditionalFilter($configOption)
*/
public function onSearchPre(EventInterface $event)
{
$command = $event->getParam('command');
if ($command->getTargetIdentifier() !== $this->backend->getIdentifier()) {
return $event;
}

// Add conditional filters
foreach ($this->filters as $fc) {
$this->addConditionalFilter($fc);
}

$params = $event->getParam('command')->getSearchParameters();
$params = $command->getSearchParameters();
$fq = $params->get('fq');
if (!is_array($fq)) {
$fq = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

namespace VuFindTest\Search\Solr;

use Laminas\Config\Config;
use Laminas\EventManager\Event;
use VuFind\Search\Solr\InjectConditionalFilterListener;
use VuFindSearch\Backend\BackendInterface;
use VuFindSearch\Backend\Solr\Backend;
use VuFindSearch\Backend\Solr\Connector;
use VuFindSearch\Backend\Solr\HandlerMap;
Expand Down Expand Up @@ -65,7 +67,7 @@ class ConditionalFilterListenerTest extends \PHPUnit\Framework\TestCase
*
* @var array
*/
protected static $emptySearchConfig = [ ];
protected static $emptySearchConfig = [];

/**
* Backend.
Expand All @@ -77,13 +79,14 @@ class ConditionalFilterListenerTest extends \PHPUnit\Framework\TestCase
/**
* Construct a mock search backend pre event.
*
* @param ParamBag $params Search backend parameters
* @param ParamBag $params Search backend parameters
* @param string $backendId Backend identifier
*
* @return Event
*/
protected function getMockPreEvent(ParamBag $params): Event
protected function getMockPreEvent(ParamBag $params, string $backendId = 'Solr'): Event
{
$command = $this->getMockSearchCommand($params);
$command = $this->getMockSearchCommand($params, null, $backendId);
return new Event(
Service::EVENT_PRE,
$this->backend,
Expand All @@ -98,15 +101,16 @@ protected function getMockPreEvent(ParamBag $params): Event
*/
protected function setUp(): void
{
$handlermap = new HandlerMap(['select' => ['fallback' => true]]);
$connector = new Connector(
$handlermap = new HandlerMap(['select' => ['fallback' => true]]);
$connector = new Connector(
'http://localhost/',
$handlermap,
function () {
return new \Laminas\Http\Client();
}
);
$this->backend = new Backend($connector);
$this->backend->setIdentifier('Solr');
}

/**
Expand All @@ -116,7 +120,7 @@ function () {
*/
public function testAttach()
{
$listener = new InjectConditionalFilterListener(self::$emptySearchConfig);
$listener = new InjectConditionalFilterListener($this->backend, self::$emptySearchConfig);
$mock = $this->createMock(\Laminas\EventManager\SharedEventManagerInterface::class);
$mock->expects($this->once())->method('attach')->with(
$this->equalTo('VuFind\Search'),
Expand All @@ -134,14 +138,14 @@ public function testAttach()
*/
public function testConditionalFilterWithoutAuthorizationService()
{
$params = new ParamBag([ ]);
$listener = new InjectConditionalFilterListener(self::$searchConfig);
$params = new ParamBag([]);
$listener = new InjectConditionalFilterListener($this->backend, self::$searchConfig);

$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$this->assertEquals([ ], $fq);
$fq = $params->get('fq');
$this->assertEquals([], $fq);
}

/**
Expand All @@ -153,17 +157,17 @@ public function testConditionalFilterWithoutAuthorizationService()
*/
public function testConditionalFilterWithoutAuthorizationServiceWithParams()
{
$params = new ParamBag(
$params = new ParamBag(
[
'fq' => ['fulltext:VuFind', 'field2:novalue'],
]
);
$listener = new InjectConditionalFilterListener(self::$searchConfig);
$listener = new InjectConditionalFilterListener($this->backend, self::$searchConfig);

$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$fq = $params->get('fq');
$this->assertEquals(
[0 => 'fulltext:VuFind',
1 => 'field2:novalue'],
Expand All @@ -178,18 +182,18 @@ public function testConditionalFilterWithoutAuthorizationServiceWithParams()
*/
public function testConditionalFilterEmptyConfig()
{
$params = new ParamBag([ ]);
$listener = new InjectConditionalFilterListener(self::$emptySearchConfig);
$params = new ParamBag([]);
$listener = new InjectConditionalFilterListener($this->backend, self::$emptySearchConfig);
$mockAuth = $this->getMockBuilder(\LmcRbacMvc\Service\AuthorizationService::class)
->disableOriginalConstructor()
->getMock();
$listener->setAuthorizationService($mockAuth);

$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$this->assertEquals([ ], $fq);
$fq = $params->get('fq');
$this->assertEquals([], $fq);
}

/**
Expand All @@ -200,21 +204,21 @@ public function testConditionalFilterEmptyConfig()
*/
public function testConditionalFilterEmptyConfigWithFQ()
{
$params = new ParamBag(
$params = new ParamBag(
[
'fq' => ['fulltext:VuFind', 'field2:novalue'],
]
);
$listener = new InjectConditionalFilterListener(self::$emptySearchConfig);
$listener = new InjectConditionalFilterListener($this->backend, self::$emptySearchConfig);
$mockAuth = $this->getMockBuilder(\LmcRbacMvc\Service\AuthorizationService::class)
->disableOriginalConstructor()
->getMock();
$listener->setAuthorizationService($mockAuth);

$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$fq = $params->get('fq');
$this->assertEquals(
[0 => 'fulltext:VuFind',
1 => 'field2:novalue'],
Expand All @@ -230,8 +234,8 @@ public function testConditionalFilterEmptyConfigWithFQ()
*/
public function testConditionalFilter()
{
$params = new ParamBag([ ]);
$listener = new InjectConditionalFilterListener(self::$searchConfig);
$params = new ParamBag([]);
$listener = new InjectConditionalFilterListener($this->backend, self::$searchConfig);
$mockAuth = $this->getMockBuilder(\LmcRbacMvc\Service\AuthorizationService::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -240,14 +244,20 @@ public function testConditionalFilter()
->will($this->returnValue(true));
$listener->setAuthorizationService($mockAuth);

$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$fq = $params->get('fq');
$this->assertEquals(
[0 => 'institution:"MyInst"'],
$fq
);

// Check that a filter is not added for wrong backend:
$params = new ParamBag([]);
$event = $this->getMockPreEvent($params, 'Other');
$listener->onSearchPre($event);
$this->assertEmpty($params->get('fq'));
}

/**
Expand All @@ -258,20 +268,20 @@ public function testConditionalFilter()
*/
public function testNegativeConditionalFilter()
{
$params = new ParamBag([ ]);
$params = new ParamBag([]);

$listener = new InjectConditionalFilterListener(self::$searchConfig);
$listener = new InjectConditionalFilterListener($this->backend, self::$searchConfig);
$mockAuth = $this->getMockBuilder(\LmcRbacMvc\Service\AuthorizationService::class)
->disableOriginalConstructor()
->getMock();
$mockAuth->expects($this->any())->method('isGranted')
->with($this->equalTo('conditionalFilter.sample'))
->will($this->returnValue(false));
$listener->setAuthorizationService($mockAuth);
$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$fq = $params->get('fq');
$this->assertEquals([0 => '(NOT institution:"MyInst")'], $fq);
}

Expand All @@ -283,24 +293,24 @@ public function testNegativeConditionalFilter()
*/
public function testNegativeConditionalFilterWithFQ()
{
$params = new ParamBag(
$params = new ParamBag(
[
'fq' => ['fulltext:VuFind', 'field2:novalue'],
]
);

$listener = new InjectConditionalFilterListener(self::$searchConfig);
$listener = new InjectConditionalFilterListener($this->backend, self::$searchConfig);
$mockAuth = $this->getMockBuilder(\LmcRbacMvc\Service\AuthorizationService::class)
->disableOriginalConstructor()
->getMock();
$mockAuth->expects($this->any())->method('isGranted')
->with($this->equalTo('conditionalFilter.sample'))
->will($this->returnValue(false));
$listener->setAuthorizationService($mockAuth);
$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$fq = $params->get('fq');
$this->assertEquals(
[0 => 'fulltext:VuFind',
1 => 'field2:novalue',
Expand All @@ -318,24 +328,24 @@ public function testNegativeConditionalFilterWithFQ()
*/
public function testConditionalFilterWithFQ()
{
$params = new ParamBag(
$params = new ParamBag(
[
'fq' => ['fulltext:VuFind', 'field2:novalue'],
]
);

$listener = new InjectConditionalFilterListener(self::$searchConfig);
$listener = new InjectConditionalFilterListener($this->backend, self::$searchConfig);
$mockAuth = $this->getMockBuilder(\LmcRbacMvc\Service\AuthorizationService::class)
->disableOriginalConstructor()
->getMock();
$mockAuth->expects($this->any())->method('isGranted')
->with($this->equalTo('conditionalFilter.sample'))
->will($this->returnValue(true));
$listener->setAuthorizationService($mockAuth);
$event = $this->getMockPreEvent($params);
$event = $this->getMockPreEvent($params);
$listener->onSearchPre($event);

$fq = $params->get('fq');
$fq = $params->get('fq');
$this->assertEquals(
[0 => 'fulltext:VuFind',
1 => 'field2:novalue',
Expand Down