]> BookStack Code Mirror - bookstack/blob - tests/Entity/SearchOptionsTest.php
Uploads: Explicitly disabled s3 streaming in config
[bookstack] / tests / Entity / SearchOptionsTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Search\SearchOptions;
6 use Illuminate\Http\Request;
7 use Tests\TestCase;
8
9 class SearchOptionsTest extends TestCase
10 {
11     public function test_from_string_parses_a_search_string_properly()
12     {
13         $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
14
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);
19     }
20
21     public function test_from_string_properly_parses_escaped_quotes()
22     {
23         $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"');
24
25         $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts);
26     }
27
28     public function test_to_string_includes_all_items_in_the_correct_format()
29     {
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' => ''];
36
37         $output = $options->toString();
38         foreach (explode(' ', $expected) as $term) {
39             $this->assertStringContainsString($term, $output);
40         }
41     }
42
43     public function test_to_string_escapes_as_expected()
44     {
45         $options = new SearchOptions();
46         $options->exacts = ['"cat"', '""', '"donkey', '"', '\\', '\\"'];
47
48         $output = $options->toString();
49         $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
50     }
51
52     public function test_correct_filter_values_are_set_from_string()
53     {
54         $opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}');
55
56         $this->assertEquals([
57             'is_tree' => '',
58             'name'    => 'dan',
59             'cat'     => 'happy',
60         ], $opts->filters);
61     }
62     public function test_it_cannot_parse_out_empty_exacts()
63     {
64         $options = SearchOptions::fromString('"" test ""');
65
66         $this->assertEmpty($options->exacts);
67         $this->assertCount(1, $options->searches);
68     }
69
70     public function test_from_request_properly_parses_exacts_from_search_terms()
71     {
72         $request = new Request([
73             'search' => 'biscuits "cheese" "" "baked beans"'
74         ]);
75
76         $options = SearchOptions::fromRequest($request);
77         $this->assertEquals(["biscuits"], $options->searches);
78         $this->assertEquals(['"cheese"', '""', '"baked',  'beans"'], $options->exacts);
79     }
80 }