]> BookStack Code Mirror - bookstack/commitdiff
Fixed single word quoted search terms
authorDan Brown <redacted>
Thu, 25 Aug 2016 16:17:26 +0000 (17:17 +0100)
committerDan Brown <redacted>
Thu, 25 Aug 2016 16:17:26 +0000 (17:17 +0100)
Fixes #170

app/Entity.php
app/Repos/EntityRepo.php
tests/Entity/EntitySearchTest.php

index 1342c29971e8a1ec21d56c461a07c6dc2bc652d4..496d20a333fa1069cd7c1ac19491df94b3f82b29 100644 (file)
@@ -167,7 +167,8 @@ class Entity extends Ownable
             foreach ($terms as $key => $term) {
                 $term = htmlentities($term, ENT_QUOTES);
                 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
-                if (preg_match('/\s/', $term)) {
+                if (preg_match('/&quot;.*?&quot;/', $term)) {
+                    $term = str_replace('&quot;', '', $term);
                     $exactTerms[] = '%' . $term . '%';
                     $term = '"' . $term . '"';
                 } else {
@@ -206,5 +207,5 @@ class Entity extends Ownable
 
         return $search->orderBy($orderBy, 'desc');
     }
-    
+
 }
index 012a649676b7c8c9df8b87292c6adeacd4161345..28f4c0ae8466a805b456cb8ba2eb08e5e7b98038 100644 (file)
@@ -168,15 +168,16 @@ class EntityRepo
      * @param $termString
      * @return array
      */
-    protected function prepareSearchTerms($termString)
+    public function prepareSearchTerms($termString)
     {
         $termString = $this->cleanSearchTermString($termString);
-        preg_match_all('/"(.*?)"/', $termString, $matches);
+        preg_match_all('/(".*?")/', $termString, $matches);
+        $terms = [];
         if (count($matches[1]) > 0) {
-            $terms = $matches[1];
+            foreach ($matches[1] as $match) {
+                $terms[] = $match;
+            }
             $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
-        } else {
-            $terms = [];
         }
         if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));
         return $terms;
index a9d9bc04775c89f4b6ba3161c6bf09fd51c0ccea..8adfd35a3e3c59037c2cb8e8e1f7574bf46adc66 100644 (file)
@@ -76,6 +76,14 @@ class EntitySearchTest extends TestCase
             ->see('Chapter Search Results')->seeInElement('.entity-list', $chapter->name);
     }
 
+    public function test_search_quote_term_preparation()
+    {
+        $termString = '"192" cat "dog hat"';
+        $repo = $this->app[\BookStack\Repos\EntityRepo::class];
+        $preparedTerms = $repo->prepareSearchTerms($termString);
+        $this->assertTrue($preparedTerms === ['"192"','"dog hat"', 'cat']);
+    }
+
     public function test_books_search_listing()
     {
         $book = \BookStack\Book::all()->last();