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