| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Entity; | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-16 18:27:22 +08:00
										 |  |  | use BookStack\Search\SearchOptions; | 
					
						
							| 
									
										
										
										
											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}'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals(['cat'], $options->searches); | 
					
						
							|  |  |  |         $this->assertEquals(['dog'], $options->exacts); | 
					
						
							|  |  |  |         $this->assertEquals(['tag=good'], $options->tags); | 
					
						
							|  |  |  |         $this->assertEquals(['is_tree' => ''], $options->filters); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 20:41:10 +08:00
										 |  |  |         $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts); | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $expected = 'cat "dog" [tag=good] {is_tree}'; | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $options = new SearchOptions(); | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  |         $options->searches = ['cat']; | 
					
						
							|  |  |  |         $options->exacts = ['dog']; | 
					
						
							|  |  |  |         $options->tags = ['tag=good']; | 
					
						
							|  |  |  |         $options->filters = ['is_tree' => '']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $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(); | 
					
						
							| 
									
										
										
										
											2023-09-23 20:41:10 +08:00
										 |  |  |         $options->exacts = ['"cat"', '""', '"donkey', '"', '\\', '\\"']; | 
					
						
							| 
									
										
										
										
											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', | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  |         ], $opts->filters); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-20 03:09:33 +08:00
										 |  |  |     public function test_it_cannot_parse_out_empty_exacts() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $options = SearchOptions::fromString('"" test ""'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEmpty($options->exacts); | 
					
						
							|  |  |  |         $this->assertCount(1, $options->searches); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_from_request_properly_parses_exacts_from_search_terms() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $request = new Request([ | 
					
						
							|  |  |  |             'search' => 'biscuits "cheese" "" "baked beans"' | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $options = SearchOptions::fromRequest($request); | 
					
						
							|  |  |  |         $this->assertEquals(["biscuits"], $options->searches); | 
					
						
							|  |  |  |         $this->assertEquals(['"cheese"', '""', '"baked',  'beans"'], $options->exacts); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-27 20:29:00 +08:00
										 |  |  | } |