Skip to content
Snippets Groups Projects
Commit 2a7ff113 authored by Demian Katz's avatar Demian Katz
Browse files

Fixed broken HideFacetValue feature; added test.

parent 0fd1646f
No related merge requests found
......@@ -60,7 +60,8 @@ class HideFacetValueListener
* Constructor.
*
* @param BackendInterface $backend Search backend
* @param array $hideFacetValues Facet config file id
* @param array $hideFacetValues Associative array of field name
* to array of facet values to hide.
*/
public function __construct(
BackendInterface $backend,
......@@ -118,13 +119,7 @@ class HideFacetValueListener
foreach ($this->hideFacets as $facet => $value) {
if (isset($facets[$facet])) {
foreach ((array)$value as $configValue) {
foreach (array_keys($facets[$facet]) as $facetValue) {
if ($facetValue == $configValue) {
$facets[$facet]->remove();
}
}
}
$facets[$facet]->removeKeys((array)$value);
}
}
return null;
......
<?php
/**
* Unit tests for Hide Facet Value Listener.
*
* PHP version 5
*
* Copyright (C) Villanova University 2015.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Search
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
namespace VuFindTest\Search\Solr;
use VuFindTest\Unit\TestCase;
use VuFind\Search\Solr\HideFacetValueListener;
use VuFindSearch\Backend\Solr\Response\Json\Facets;
use Zend\EventManager\Event;
/**
* Unit tests for Hide Facet Value Listener.
*
* @category VuFind2
* @package Search
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
class HideFacetValueListenerTest extends TestCase
{
/**
* Get a mock backend
*
* @param string $id ID of fake backend.
*
* @return \VuFindSearch\Backend\Solr\Backend
*/
protected function getMockBackend($id = 'Solr')
{
$backend = $this->getMockBuilder('VuFindSearch\Backend\Solr\Backend')
->disableOriginalConstructor()->getMock();
$backend->expects($this->any())->method('getIdentifier')->will(
$this->returnValue($id)
);
return $backend;
}
/**
* Get a facet object for testing.
*
* @return Facets
*/
protected function getFacets()
{
$data = [
'facet_fields' => [
'format' => [
['Book', 124],
['Unknown', 16],
['Fake', 3],
]
]
];
return new Facets($data);
}
/**
* Construct a mock Solr result object.
*
* @return \VuFindSearch\Backend\Solr\Response\Json\RecordCollection
*/
protected function getMockResult()
{
$result = $this->getMockBuilder(
'VuFindSearch\Backend\Solr\Response\Json\RecordCollection'
)->disableOriginalConstructor()->getMock();
$result->expects($this->any())->method('getFacets')
->will($this->returnValue($this->getFacets()));
return $result;
}
/**
* Construct a listener for testing.
*
* @param array $config Configuration (null for default)
*
* @return HideFacetValueListener
*/
protected function getListener($config = null)
{
// Set default config if necessary
if (null === $config) {
$config = ['format' => ['Unknown']];
}
return new HideFacetValueListener($this->getMockBackend(), $config);
}
/**
* Test attaching listener.
*
* @return void
*/
public function testAttach()
{
$listener = $this->getListener();
$mock = $this->getMock('Zend\EventManager\SharedEventManagerInterface');
$mock->expects($this->once())->method('attach')->with(
$this->equalTo('VuFind\Search'),
$this->equalTo('post'),
$this->equalTo([$listener, 'onSearchPost'])
);
$listener->attach($mock);
}
/**
* Test actual functionality of listener.
*
* @return void
*/
public function testHideFacet()
{
$listener = $this->getListener();
$result = $this->getMockResult();
$facets = $result->getFacets()->getFieldFacets();
$params = ['backend' => 'Solr', 'context' => 'search'];
$event = new Event(null, $result, $params);
$this->assertEquals(
['Book' => 124, 'Unknown' => 16, 'Fake' => 3],
$facets['format']->toArray()
);
$listener->onSearchPost($event);
$this->assertEquals(
['Book' => 124, 'Fake' => 3], $facets['format']->toArray()
);
}
}
......@@ -151,12 +151,33 @@ class NamedList implements Countable, Iterator
}
/**
* Remove element from list.
* Remove single element from list.
*
* @param string $key Key to remove
*
* @return void
*/
public function removeKey($key)
{
return $this->removeKeys([$key]);
}
/**
* Remove elements from list.
*
* @param array $keys Keys to remove
*
* @return void
*/
public function remove()
public function removeKeys(array $keys)
{
unset($this->list[key($this->list)]);
$newList = [];
foreach ($this->list as $current) {
if (!in_array($current[0], $keys)) {
$newList[] = $current;
}
}
$this->list = $newList;
$this->rewind();
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment