Dan Brown

Fixed single word quoted search terms

Fixes #170
...@@ -167,7 +167,8 @@ class Entity extends Ownable ...@@ -167,7 +167,8 @@ class Entity extends Ownable
167 foreach ($terms as $key => $term) { 167 foreach ($terms as $key => $term) {
168 $term = htmlentities($term, ENT_QUOTES); 168 $term = htmlentities($term, ENT_QUOTES);
169 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term); 169 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
170 - if (preg_match('/\s/', $term)) { 170 + if (preg_match('/&quot;.*?&quot;/', $term)) {
171 + $term = str_replace('&quot;', '', $term);
171 $exactTerms[] = '%' . $term . '%'; 172 $exactTerms[] = '%' . $term . '%';
172 $term = '"' . $term . '"'; 173 $term = '"' . $term . '"';
173 } else { 174 } else {
...@@ -206,5 +207,5 @@ class Entity extends Ownable ...@@ -206,5 +207,5 @@ class Entity extends Ownable
206 207
207 return $search->orderBy($orderBy, 'desc'); 208 return $search->orderBy($orderBy, 'desc');
208 } 209 }
209 - 210 +
210 } 211 }
......
...@@ -168,15 +168,16 @@ class EntityRepo ...@@ -168,15 +168,16 @@ class EntityRepo
168 * @param $termString 168 * @param $termString
169 * @return array 169 * @return array
170 */ 170 */
171 - protected function prepareSearchTerms($termString) 171 + public function prepareSearchTerms($termString)
172 { 172 {
173 $termString = $this->cleanSearchTermString($termString); 173 $termString = $this->cleanSearchTermString($termString);
174 - preg_match_all('/"(.*?)"/', $termString, $matches); 174 + preg_match_all('/(".*?")/', $termString, $matches);
175 + $terms = [];
175 if (count($matches[1]) > 0) { 176 if (count($matches[1]) > 0) {
176 - $terms = $matches[1]; 177 + foreach ($matches[1] as $match) {
178 + $terms[] = $match;
179 + }
177 $termString = trim(preg_replace('/"(.*?)"/', '', $termString)); 180 $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
178 - } else {
179 - $terms = [];
180 } 181 }
181 if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString)); 182 if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));
182 return $terms; 183 return $terms;
......
...@@ -76,6 +76,14 @@ class EntitySearchTest extends TestCase ...@@ -76,6 +76,14 @@ class EntitySearchTest extends TestCase
76 ->see('Chapter Search Results')->seeInElement('.entity-list', $chapter->name); 76 ->see('Chapter Search Results')->seeInElement('.entity-list', $chapter->name);
77 } 77 }
78 78
79 + public function test_search_quote_term_preparation()
80 + {
81 + $termString = '"192" cat "dog hat"';
82 + $repo = $this->app[\BookStack\Repos\EntityRepo::class];
83 + $preparedTerms = $repo->prepareSearchTerms($termString);
84 + $this->assertTrue($preparedTerms === ['"192"','"dog hat"', 'cat']);
85 + }
86 +
79 public function test_books_search_listing() 87 public function test_books_search_listing()
80 { 88 {
81 $book = \BookStack\Book::all()->last(); 89 $book = \BookStack\Book::all()->last();
......