]> BookStack Code Mirror - bookstack/blob - tests/Search/SearchOptionsTest.php
Deps & Tests: Updated PHP deps, fixed test namespaces
[bookstack] / tests / Search / SearchOptionsTest.php
1 <?php
2
3 namespace Tests\Search;
4
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;
12 use Tests\TestCase;
13
14 class SearchOptionsTest extends TestCase
15 {
16     public function test_from_string_parses_a_search_string_properly()
17     {
18         $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
19
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());
24     }
25
26     public function test_from_string_parses_negations()
27     {
28         $options = SearchOptions::fromString('cat -"dog" -[tag=good] -{is_tree}');
29
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);
34     }
35
36     public function test_from_string_properly_parses_escaped_quotes()
37     {
38         $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"');
39
40         $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts->toValueArray());
41     }
42
43     public function test_to_string_includes_all_items_in_the_correct_format()
44     {
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'),
53         ]);
54
55         $output = $options->toString();
56         foreach (explode(' ', $expected) as $term) {
57             $this->assertStringContainsString($term, $output);
58         }
59     }
60
61     public function test_to_string_handles_negations_as_expected()
62     {
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),
70         ]);
71
72         $output = $options->toString();
73         foreach (explode(' ', $expected) as $term) {
74             $this->assertStringContainsString($term, $output);
75         }
76     }
77
78     public function test_to_string_escapes_as_expected()
79     {
80         $options = new SearchOptions();
81         $options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"'], ExactSearchOption::class);
82
83         $output = $options->toString();
84         $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
85     }
86
87     public function test_correct_filter_values_are_set_from_string()
88     {
89         $opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}');
90
91         $this->assertEquals([
92             'is_tree' => '',
93             'name'    => 'dan',
94             'cat'     => 'happy',
95         ], $opts->filters->toValueMap());
96     }
97     public function test_it_cannot_parse_out_empty_exacts()
98     {
99         $options = SearchOptions::fromString('"" test ""');
100
101         $this->assertEmpty($options->exacts->toValueArray());
102         $this->assertCount(1, $options->searches->toValueArray());
103     }
104
105     public function test_from_request_properly_parses_exacts_from_search_terms()
106     {
107         $request = new Request([
108             'search' => 'biscuits "cheese" "" "baked beans"'
109         ]);
110
111         $options = SearchOptions::fromRequest($request);
112         $this->assertEquals(["biscuits"], $options->searches->toValueArray());
113         $this->assertEquals(['"cheese"', '""', '"baked',  'beans"'], $options->exacts->toValueArray());
114     }
115
116     public function test_from_request_properly_parses_provided_types()
117     {
118         $request = new Request([
119             'search' => '',
120             'types' => ['page', 'book'],
121         ]);
122
123         $options = SearchOptions::fromRequest($request);
124         $filters = $options->filters->toValueMap();
125         $this->assertCount(1, $filters);
126         $this->assertEquals('page|book', $filters['type'] ?? 'notfound');
127     }
128
129     public function test_from_request_properly_parses_out_extras_as_string()
130     {
131         $request = new Request([
132             'search' => '',
133             'tags' => ['a=b'],
134             'extras' => '-[b=c] -{viewed_by_me} -"dino"'
135         ]);
136
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);
144     }
145 }