3 namespace Tests\Search;
5 use BookStack\Search\Options\ExactSearchOption;
6 use BookStack\Search\Options\FilterSearchOption;
7 use BookStack\Search\Options\TagSearchOption;
8 use BookStack\Search\Options\TermSearchOption;
9 use BookStack\Search\SearchOptions;
10 use BookStack\Search\SearchOptionSet;
11 use Illuminate\Http\Request;
14 class SearchOptionsTest extends TestCase
16 public function test_from_string_parses_a_search_string_properly()
18 $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
20 $this->assertEquals(['cat'], $options->searches->toValueArray());
21 $this->assertEquals(['dog'], $options->exacts->toValueArray());
22 $this->assertEquals(['tag=good'], $options->tags->toValueArray());
23 $this->assertEquals(['is_tree' => ''], $options->filters->toValueMap());
26 public function test_from_string_parses_negations()
28 $options = SearchOptions::fromString('cat -"dog" -[tag=good] -{is_tree}');
30 $this->assertEquals(['cat'], $options->searches->toValueArray());
31 $this->assertTrue($options->exacts->all()[0]->negated);
32 $this->assertTrue($options->tags->all()[0]->negated);
33 $this->assertTrue($options->filters->all()[0]->negated);
36 public function test_from_string_properly_parses_escaped_quotes()
38 $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"');
40 $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts->toValueArray());
43 public function test_to_string_includes_all_items_in_the_correct_format()
45 $expected = 'cat "dog" [tag=good] {is_tree} {beans:valid}';
46 $options = new SearchOptions();
47 $options->searches = SearchOptionSet::fromValueArray(['cat'], TermSearchOption::class);
48 $options->exacts = SearchOptionSet::fromValueArray(['dog'], ExactSearchOption::class);
49 $options->tags = SearchOptionSet::fromValueArray(['tag=good'], TagSearchOption::class);
50 $options->filters = new SearchOptionSet([
51 new FilterSearchOption('', 'is_tree'),
52 new FilterSearchOption('valid', 'beans'),
55 $output = $options->toString();
56 foreach (explode(' ', $expected) as $term) {
57 $this->assertStringContainsString($term, $output);
61 public function test_to_string_handles_negations_as_expected()
63 $expected = 'cat -"dog" -[tag=good] -{is_tree}';
64 $options = new SearchOptions();
65 $options->searches = new SearchOptionSet([new TermSearchOption('cat')]);
66 $options->exacts = new SearchOptionSet([new ExactSearchOption('dog', true)]);
67 $options->tags = new SearchOptionSet([new TagSearchOption('tag=good', true)]);
68 $options->filters = new SearchOptionSet([
69 new FilterSearchOption('', 'is_tree', true),
72 $output = $options->toString();
73 foreach (explode(' ', $expected) as $term) {
74 $this->assertStringContainsString($term, $output);
78 public function test_to_string_escapes_as_expected()
80 $options = new SearchOptions();
81 $options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"'], ExactSearchOption::class);
83 $output = $options->toString();
84 $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
87 public function test_correct_filter_values_are_set_from_string()
89 $opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}');
95 ], $opts->filters->toValueMap());
97 public function test_it_cannot_parse_out_empty_exacts()
99 $options = SearchOptions::fromString('"" test ""');
101 $this->assertEmpty($options->exacts->toValueArray());
102 $this->assertCount(1, $options->searches->toValueArray());
105 public function test_from_request_properly_parses_exacts_from_search_terms()
107 $request = new Request([
108 'search' => 'biscuits "cheese" "" "baked beans"'
111 $options = SearchOptions::fromRequest($request);
112 $this->assertEquals(["biscuits"], $options->searches->toValueArray());
113 $this->assertEquals(['"cheese"', '""', '"baked', 'beans"'], $options->exacts->toValueArray());
116 public function test_from_request_properly_parses_provided_types()
118 $request = new Request([
120 'types' => ['page', 'book'],
123 $options = SearchOptions::fromRequest($request);
124 $filters = $options->filters->toValueMap();
125 $this->assertCount(1, $filters);
126 $this->assertEquals('page|book', $filters['type'] ?? 'notfound');
129 public function test_from_request_properly_parses_out_extras_as_string()
131 $request = new Request([
134 'extras' => '-[b=c] -{viewed_by_me} -"dino"'
137 $options = SearchOptions::fromRequest($request);
138 $this->assertCount(2, $options->tags->all());
139 $this->assertEquals('b=c', $options->tags->negated()->all()[0]->value);
140 $this->assertEquals('viewed_by_me', $options->filters->all()[0]->getKey());
141 $this->assertTrue($options->filters->all()[0]->negated);
142 $this->assertEquals('dino', $options->exacts->all()[0]->value);
143 $this->assertTrue($options->exacts->all()[0]->negated);