3 namespace Tests\Entity;
5 use BookStack\Search\SearchOptions;
6 use BookStack\Search\SearchOptionSet;
7 use Illuminate\Http\Request;
10 class SearchOptionsTest extends TestCase
12 public function test_from_string_parses_a_search_string_properly()
14 $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
16 $this->assertEquals(['cat'], $options->searches->toValueArray());
17 $this->assertEquals(['dog'], $options->exacts->toValueArray());
18 $this->assertEquals(['tag=good'], $options->tags->toValueArray());
19 $this->assertEquals(['is_tree' => ''], $options->filters->toValueMap());
22 public function test_from_string_properly_parses_escaped_quotes()
24 $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"');
26 $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts->toValueArray());
29 public function test_to_string_includes_all_items_in_the_correct_format()
31 $expected = 'cat "dog" [tag=good] {is_tree}';
32 $options = new SearchOptions();
33 $options->searches = SearchOptionSet::fromValueArray(['cat']);
34 $options->exacts = SearchOptionSet::fromValueArray(['dog']);
35 $options->tags = SearchOptionSet::fromValueArray(['tag=good']);
36 $options->filters = SearchOptionSet::fromMapArray(['is_tree' => '']);
38 $output = $options->toString();
39 foreach (explode(' ', $expected) as $term) {
40 $this->assertStringContainsString($term, $output);
44 public function test_to_string_escapes_as_expected()
46 $options = new SearchOptions();
47 $options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"']);
49 $output = $options->toString();
50 $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
53 public function test_correct_filter_values_are_set_from_string()
55 $opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}');
61 ], $opts->filters->toValueMap());
63 public function test_it_cannot_parse_out_empty_exacts()
65 $options = SearchOptions::fromString('"" test ""');
67 $this->assertEmpty($options->exacts->toValueArray());
68 $this->assertCount(1, $options->searches->toValueArray());
71 public function test_from_request_properly_parses_exacts_from_search_terms()
73 $request = new Request([
74 'search' => 'biscuits "cheese" "" "baked beans"'
77 $options = SearchOptions::fromRequest($request);
78 $this->assertEquals(["biscuits"], $options->searches->toValueArray());
79 $this->assertEquals(['"cheese"', '""', '"baked', 'beans"'], $options->exacts->toValueArray());