From 91ea2d8104d182ec6f06362e4f13d16dfd40adf8 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 20 Aug 2013 13:49:12 -0400
Subject: [PATCH] Added trimming of slashes/whitespace to query builder.

---
 .../src/VuFindSearch/Backend/Solr/QueryBuilder.php            | 4 ++++
 .../src/VuFindTest/Backend/Solr/QueryBuilderTest.php          | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php
index 016b7ccbaac..1175ab33433 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php
@@ -547,6 +547,10 @@ class QueryBuilder implements QueryBuilderInterface
             $input = preg_replace($parenRegex, '', $input);
         }
 
+        // Remove surrounding slashes and whitespace -- these serve no purpose
+        // and can cause problems.
+        $input = trim($input, '/ ');
+
         return $input;
     }
 
diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php
index eeb4b82b371..d2a41ba6e60 100644
--- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php
+++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php
@@ -136,7 +136,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase
             array("()", "*:*"),                     // empty parens
             array("((()))", "*:*"),                 // nested empty parens
             array("((())", "*:*"),                  // mismatched parens
-            array("this that ()", "this that "),    // text mixed w/ empty parens
+            array("this that ()", "this that"),     // text mixed w/ empty parens
             array('"()"', '"()"'),                  // empty parens in quotes
             array('title - sub', 'title sub'),      // freestanding hyphen
             array('"title - sub"', '"title - sub"'),// freestanding hyphen in quotes
@@ -154,6 +154,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase
             array('^10', '10'),                     // invalid boosts
             array('test^ test^6', 'test test6'),    // invalid boosts
             array('test^1 test^2', 'test^1 test^2'),// valid boosts
+            array('title /', 'title'),              // trailing slash
         );
         // @codingStandardsIgnoreEnd
 
-- 
GitLab