-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathSearchOptionsTest.php
More file actions
200 lines (166 loc) · 7.59 KB
/
SearchOptionsTest.php
File metadata and controls
200 lines (166 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Tests\Search;
use BookStack\Search\Options\ExactSearchOption;
use BookStack\Search\Options\FilterSearchOption;
use BookStack\Search\Options\TagSearchOption;
use BookStack\Search\Options\TermSearchOption;
use BookStack\Search\SearchOptions;
use BookStack\Search\SearchOptionSet;
use Illuminate\Http\Request;
use Tests\TestCase;
class SearchOptionsTest extends TestCase
{
public function test_from_string_parses_a_search_string_properly()
{
$options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
$this->assertEquals(['cat'], $options->searches->toValueArray());
$this->assertEquals(['dog'], $options->exacts->toValueArray());
$this->assertEquals(['tag=good'], $options->tags->toValueArray());
$this->assertEquals(['is_tree' => ''], $options->filters->toValueMap());
}
public function test_from_string_parses_negations()
{
$options = SearchOptions::fromString('cat -"dog" -[tag=good] -{is_tree}');
$this->assertEquals(['cat'], $options->searches->toValueArray());
$this->assertTrue($options->exacts->all()[0]->negated);
$this->assertTrue($options->tags->all()[0]->negated);
$this->assertTrue($options->filters->all()[0]->negated);
}
public function test_from_string_properly_parses_escaped_quotes()
{
$options = SearchOptions::fromString('"\"cat\"" surprise');
$this->assertEquals(['"cat"'], $options->exacts->toValueArray());
$options = SearchOptions::fromString('"\"\"" "\"donkey"');
$this->assertEquals(['""', '"donkey'], $options->exacts->toValueArray());
$options = SearchOptions::fromString('"\"" "\\\\"');
$this->assertEquals(['"', '\\'], $options->exacts->toValueArray());
}
public function test_to_string_includes_all_items_in_the_correct_format()
{
$expected = 'cat "dog" [tag=good] {is_tree} {beans:valid}';
$options = new SearchOptions();
$options->searches = SearchOptionSet::fromValueArray(['cat'], TermSearchOption::class);
$options->exacts = SearchOptionSet::fromValueArray(['dog'], ExactSearchOption::class);
$options->tags = SearchOptionSet::fromValueArray(['tag=good'], TagSearchOption::class);
$options->filters = new SearchOptionSet([
new FilterSearchOption('', 'is_tree'),
new FilterSearchOption('valid', 'beans'),
]);
$output = $options->toString();
foreach (explode(' ', $expected) as $term) {
$this->assertStringContainsString($term, $output);
}
}
public function test_to_string_handles_negations_as_expected()
{
$expected = 'cat -"dog" -[tag=good] -{is_tree}';
$options = new SearchOptions();
$options->searches = new SearchOptionSet([new TermSearchOption('cat')]);
$options->exacts = new SearchOptionSet([new ExactSearchOption('dog', true)]);
$options->tags = new SearchOptionSet([new TagSearchOption('tag=good', true)]);
$options->filters = new SearchOptionSet([
new FilterSearchOption('', 'is_tree', true),
]);
$output = $options->toString();
foreach (explode(' ', $expected) as $term) {
$this->assertStringContainsString($term, $output);
}
}
public function test_to_string_escapes_as_expected()
{
$options = new SearchOptions();
$options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"'], ExactSearchOption::class);
$output = $options->toString();
$this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
}
public function test_correct_filter_values_are_set_from_string()
{
$opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}');
$this->assertEquals([
'is_tree' => '',
'name' => 'dan',
'cat' => 'happy',
], $opts->filters->toValueMap());
}
public function test_it_cannot_parse_out_empty_exacts()
{
$options = SearchOptions::fromString('"" test ""');
$this->assertEmpty($options->exacts->toValueArray());
$this->assertCount(1, $options->searches->toValueArray());
}
public function test_from_request_properly_parses_exacts_from_search_terms()
{
$this->asEditor();
$request = new Request([
'search' => 'biscuits "cheese" "" "baked beans"'
]);
$options = SearchOptions::fromRequest($request);
$this->assertEquals(["biscuits"], $options->searches->toValueArray());
$this->assertEquals(['"cheese"', '""', '"baked', 'beans"'], $options->exacts->toValueArray());
}
public function test_from_request_properly_parses_provided_types()
{
$request = new Request([
'search' => '',
'types' => ['page', 'book'],
]);
$options = SearchOptions::fromRequest($request);
$filters = $options->filters->toValueMap();
$this->assertCount(1, $filters);
$this->assertEquals('page|book', $filters['type'] ?? 'notfound');
}
public function test_from_request_properly_parses_out_extras_as_string()
{
$request = new Request([
'search' => '',
'tags' => ['a=b'],
'extras' => '-[b=c] -{viewed_by_me} -"dino"'
]);
$options = SearchOptions::fromRequest($request);
$this->assertCount(2, $options->tags->all());
$this->assertEquals('b=c', $options->tags->negated()->all()[0]->value);
$this->assertEquals('viewed_by_me', $options->filters->all()[0]->getKey());
$this->assertTrue($options->filters->all()[0]->negated);
$this->assertEquals('dino', $options->exacts->all()[0]->value);
$this->assertTrue($options->exacts->all()[0]->negated);
}
public function test_from_string_results_are_count_limited_and_larger_for_logged_in_users()
{
$terms = [
...array_fill(0, 40, 'cat'),
...array_fill(0, 50, '"bees"'),
...array_fill(0, 50, '{is_template}'),
...array_fill(0, 50, '[a=b]'),
];
$options = SearchOptions::fromString(implode(' ', $terms));
$this->assertCount(5, $options->searches->all());
$this->assertCount(2, $options->exacts->all());
$this->assertCount(4, $options->tags->all());
$this->assertCount(5, $options->filters->all());
$this->asEditor();
$options = SearchOptions::fromString(implode(' ', $terms));
$this->assertCount(10, $options->searches->all());
$this->assertCount(4, $options->exacts->all());
$this->assertCount(8, $options->tags->all());
$this->assertCount(10, $options->filters->all());
}
public function test_from_request_results_are_count_limited_and_larger_for_logged_in_users()
{
$request = new Request([
'search' => str_repeat('hello ', 20),
'tags' => array_fill(0, 20, 'a=b'),
'extras' => str_repeat('-[b=c] -{viewed_by_me} -"dino"', 20),
]);
$options = SearchOptions::fromRequest($request);
$this->assertCount(5, $options->searches->all());
$this->assertCount(2, $options->exacts->all());
$this->assertCount(4, $options->tags->all());
$this->assertCount(5, $options->filters->all());
$this->asEditor();
$options = SearchOptions::fromRequest($request);
$this->assertCount(10, $options->searches->all());
$this->assertCount(4, $options->exacts->all());
$this->assertCount(8, $options->tags->all());
$this->assertCount(10, $options->filters->all());
}
}