| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Entity; | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  | use BookStack\Search\Options\ExactSearchOption; | 
					
						
							|  |  |  | use BookStack\Search\Options\FilterSearchOption; | 
					
						
							|  |  |  | use BookStack\Search\Options\TagSearchOption; | 
					
						
							|  |  |  | use BookStack\Search\Options\TermSearchOption; | 
					
						
							| 
									
										
										
										
											2022-08-16 18:27:22 +08:00
										 |  |  | use BookStack\Search\SearchOptions; | 
					
						
							| 
									
										
										
										
											2024-10-03 00:31:45 +08:00
										 |  |  | use BookStack\Search\SearchOptionSet; | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  | 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}'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 00:31:45 +08:00
										 |  |  |         $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()); | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  |     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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     public function test_from_string_properly_parses_escaped_quotes() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-23 20:41:10 +08:00
										 |  |  |         $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"'); | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 00:31:45 +08:00
										 |  |  |         $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts->toValueArray()); | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  |     public function test_to_string_includes_all_items_in_the_correct_format() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  |         $expected = 'cat "dog" [tag=good] {is_tree} {beans:valid}'; | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $options = new SearchOptions(); | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  |         $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), | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $output = $options->toString(); | 
					
						
							|  |  |  |         foreach (explode(' ', $expected) as $term) { | 
					
						
							|  |  |  |             $this->assertStringContainsString($term, $output); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 20:41:10 +08:00
										 |  |  |     public function test_to_string_escapes_as_expected() | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $options = new SearchOptions(); | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  |         $options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"'], ExactSearchOption::class); | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $output = $options->toString(); | 
					
						
							| 
									
										
										
										
											2023-09-23 20:41:10 +08:00
										 |  |  |         $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output); | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  |     public function test_correct_filter_values_are_set_from_string() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $opts = SearchOptions::fromString('{is_tree} {name:dan} {cat:happy}'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals([ | 
					
						
							|  |  |  |             'is_tree' => '', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'    => 'dan', | 
					
						
							|  |  |  |             'cat'     => 'happy', | 
					
						
							| 
									
										
										
										
											2024-10-03 00:31:45 +08:00
										 |  |  |         ], $opts->filters->toValueMap()); | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     public function test_it_cannot_parse_out_empty_exacts() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $options = SearchOptions::fromString('"" test ""'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 00:31:45 +08:00
										 |  |  |         $this->assertEmpty($options->exacts->toValueArray()); | 
					
						
							|  |  |  |         $this->assertCount(1, $options->searches->toValueArray()); | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_from_request_properly_parses_exacts_from_search_terms() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $request = new Request([ | 
					
						
							|  |  |  |             'search' => 'biscuits "cheese" "" "baked beans"' | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $options = SearchOptions::fromRequest($request); | 
					
						
							| 
									
										
										
										
											2024-10-03 00:31:45 +08:00
										 |  |  |         $this->assertEquals(["biscuits"], $options->searches->toValueArray()); | 
					
						
							|  |  |  |         $this->assertEquals(['"cheese"', '""', '"baked',  'beans"'], $options->exacts->toValueArray()); | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-05 21:47:00 +08:00
										 |  |  |     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'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  |     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()); | 
					
						
							| 
									
										
										
										
											2024-10-04 02:38:07 +08:00
										 |  |  |         $this->assertEquals('b=c', $options->tags->negated()->all()[0]->value); | 
					
						
							| 
									
										
										
										
											2024-10-03 22:59:50 +08:00
										 |  |  |         $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); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  | } |