3 namespace Tests\Entity;
5 use BookStack\Search\SearchOptions;
6 use Illuminate\Http\Request;
9 class SearchOptionsTest extends TestCase
11 public function test_from_string_parses_a_search_string_properly()
13 $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
15 $this->assertEquals(['cat'], $options->searches);
16 $this->assertEquals(['dog'], $options->exacts);
17 $this->assertEquals(['tag=good'], $options->tags);
18 $this->assertEquals(['is_tree' => ''], $options->filters);
21 public function test_from_string_properly_parses_escaped_quotes()
23 $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\""');
25 $this->assertEquals(['"cat"', '""', '"donkey', '"'], $options->exacts);
28 public function test_to_string_includes_all_items_in_the_correct_format()
30 $expected = 'cat "dog" [tag=good] {is_tree}';
31 $options = new SearchOptions();
32 $options->searches = ['cat'];
33 $options->exacts = ['dog'];
34 $options->tags = ['tag=good'];
35 $options->filters = ['is_tree' => ''];
37 $output = $options->toString();
38 foreach (explode(' ', $expected) as $term) {
39 $this->assertStringContainsString($term, $output);
43 public function test_to_string_escapes_quotes_as_expected()
45 $options = new SearchOptions();
46 $options->exacts = ['"cat"', '""', '"donkey', '"'];
48 $output = $options->toString();
49 $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\""', $output);
52 public function test_correct_filter_values_are_set_from_string()
54 $opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}');
62 public function test_it_cannot_parse_out_empty_exacts()
64 $options = SearchOptions::fromString('"" test ""');
66 $this->assertEmpty($options->exacts);
67 $this->assertCount(1, $options->searches);
70 public function test_from_request_properly_parses_exacts_from_search_terms()
72 $request = new Request([
73 'search' => 'biscuits "cheese" "" "baked beans"'
76 $options = SearchOptions::fromRequest($request);
77 $this->assertEquals(["biscuits"], $options->searches);
78 $this->assertEquals(['"cheese"', '""', '"baked', 'beans"'], $options->exacts);