From 2a7ff1136d3ee9c11e47bd3ba71cf2e767bcbc00 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 5 Feb 2016 08:33:54 -0500
Subject: [PATCH] Fixed broken HideFacetValue feature; added test.

---
 .../Search/Solr/HideFacetValueListener.php    |  11 +-
 .../Solr/HideFacetValueListenerTest.php       | 153 ++++++++++++++++++
 .../Backend/Solr/Response/Json/NamedList.php  |  27 +++-
 3 files changed, 180 insertions(+), 11 deletions(-)
 create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/Search/Solr/HideFacetValueListenerTest.php

diff --git a/module/VuFind/src/VuFind/Search/Solr/HideFacetValueListener.php b/module/VuFind/src/VuFind/Search/Solr/HideFacetValueListener.php
index 0c19b79ddad..a059fa39f20 100644
--- a/module/VuFind/src/VuFind/Search/Solr/HideFacetValueListener.php
+++ b/module/VuFind/src/VuFind/Search/Solr/HideFacetValueListener.php
@@ -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;
diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Search/Solr/HideFacetValueListenerTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Search/Solr/HideFacetValueListenerTest.php
new file mode 100644
index 00000000000..f32f8e4254e
--- /dev/null
+++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Search/Solr/HideFacetValueListenerTest.php
@@ -0,0 +1,153 @@
+<?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()
+        );
+    }
+}
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/NamedList.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/NamedList.php
index 2e231544630..fd58045c661 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/NamedList.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/NamedList.php
@@ -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
-- 
GitLab