From 280354df08f3a28e487ede1f58b9bdc14b7de7ae Mon Sep 17 00:00:00 2001 From: elliot sawyer Date: Sun, 26 Mar 2023 21:04:27 +1300 Subject: [PATCH 1/2] NEW: Allow different search filters on TreeDropdownField --- src/Forms/TreeDropdownField.php | 6 +++- tests/php/Forms/TreeDropdownFieldTest.php | 43 +++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Forms/TreeDropdownField.php b/src/Forms/TreeDropdownField.php index 8c8af7be4..e61e5411d 100644 --- a/src/Forms/TreeDropdownField.php +++ b/src/Forms/TreeDropdownField.php @@ -61,6 +61,8 @@ class TreeDropdownField extends FormField protected $schemaComponent = 'TreeDropdownField'; + private static $search_filter = 'PartialMatch'; + private static $url_handlers = [ '$Action!/$ID' => '$Action' ]; @@ -791,9 +793,11 @@ class TreeDropdownField extends FormField 'Title', 'Name' ]); + + $searchFilter = $this->config()->get('search_filter') ?? 'PartialMatch'; foreach ($candidates as $candidate) { if ($sourceObjectInstance->hasDatabaseField($candidate)) { - $filters["{$candidate}:PartialMatch"] = $this->search; + $filters["{$candidate}:{$searchFilter}"] = $this->search; } } diff --git a/tests/php/Forms/TreeDropdownFieldTest.php b/tests/php/Forms/TreeDropdownFieldTest.php index 81f3a576d..61efe19a1 100644 --- a/tests/php/Forms/TreeDropdownFieldTest.php +++ b/tests/php/Forms/TreeDropdownFieldTest.php @@ -363,4 +363,47 @@ class TreeDropdownFieldTest extends SapphireTest 'TreeBaseId is included in the default schema data' ); } + + public function testSearchFilter() + { + $field = new TreeDropdownField('TestTree', 'Test tree', TestObject::class); + + // case-insensitive search against keyword 'zero' for pages + $request = new HTTPRequest('GET', 'url', ['search' => 'zero', 'format' => 'json', 'flatList' => '1' ]); + $request->setSession(new Session([])); + $response = $field->tree($request); + $tree = $response->getBody(); + $json = json_decode($tree); + $children1Before = count($json->children); + $this->assertEquals($children1Before, 4, 'PartialMatch search for zero has 4 results in fixture'); + + TreeDropdownField::config()->set('search_filter', 'StartsWith'); + $request = new HTTPRequest('GET', 'url', ['search' => 'zero', 'format' => 'json', 'flatList' => '1' ]); + $request->setSession(new Session([])); + $response = $field->tree($request); + $tree = $response->getBody(); + $json = json_decode($tree); + $children1After = count($json->children); + $this->assertEquals($children1After, 1, 'StartsWith search for zero has 1 result in fixture'); + + //change search_filter back and repeat the test + TreeDropdownField::config()->set('search_filter', 'PartialMatch'); + // case-insensitive search against keyword 'child' for pages + $request = new HTTPRequest('GET', 'url', ['search' => 'child', 'format' => 'json', 'flatList' => '1' ]); + $request->setSession(new Session([])); + $response = $field->tree($request); + $tree = $response->getBody(); + $json = json_decode($tree); + $children2Before = count($json->children); + $this->assertEquals($children2Before, 9, 'PartialMatch search for child has 9 results in fixture'); + + TreeDropdownField::config()->set('search_filter', 'StartsWith'); + $request = new HTTPRequest('GET', 'url', ['search' => 'child', 'format' => 'json', 'flatList' => '1' ]); + $request->setSession(new Session([])); + $response = $field->tree($request); + $tree = $response->getBody(); + $json = json_decode($tree); + $children2After = count($json->children); + $this->assertEquals($children2After, 8, 'PartialMatch search for child has 8 results in fixture (excludes grandchild)'); + } } From 0d9724c70804255ca6ee8ba7bc7941edf2f7e0de Mon Sep 17 00:00:00 2001 From: elliot sawyer <354793+elliot-sawyer@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:43:47 +1200 Subject: [PATCH 2/2] Update tests/php/Forms/TreeDropdownFieldTest.php Co-authored-by: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> --- tests/php/Forms/TreeDropdownFieldTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/Forms/TreeDropdownFieldTest.php b/tests/php/Forms/TreeDropdownFieldTest.php index 61efe19a1..7636480c7 100644 --- a/tests/php/Forms/TreeDropdownFieldTest.php +++ b/tests/php/Forms/TreeDropdownFieldTest.php @@ -404,6 +404,6 @@ class TreeDropdownFieldTest extends SapphireTest $tree = $response->getBody(); $json = json_decode($tree); $children2After = count($json->children); - $this->assertEquals($children2After, 8, 'PartialMatch search for child has 8 results in fixture (excludes grandchild)'); + $this->assertEquals($children2After, 8, 'StartsWith search for child has 8 results in fixture (excludes grandchild)'); } }