Skip to content

Commit

Permalink
[9.1] Fix conditional filter listener to check the backend id. (#3513)
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Mar 14, 2024
1 parent 736a645 commit 9b102f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 47 deletions.
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

0 comments on commit 9b102f2

Please sign in to comment.