From d74af1c17e0b72ae119abc00fd7ef0aca6dd4498 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 21 Nov 2018 11:43:21 +1300 Subject: [PATCH] FIX Explicity mark nodes when searching nodes in TreeDropdownField #8621 --- src/Forms/TreeDropdownField.php | 11 +++++++ tests/php/Forms/TreeDropdownFieldTest.php | 37 ++++++++++++++++++++++ tests/php/Forms/TreeDropdownFieldTest.yml | 38 +++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/src/Forms/TreeDropdownField.php b/src/Forms/TreeDropdownField.php index ca4e11ea9..b5a1cfe84 100644 --- a/src/Forms/TreeDropdownField.php +++ b/src/Forms/TreeDropdownField.php @@ -499,6 +499,17 @@ class TreeDropdownField extends FormField // Begin marking $markingSet->markPartialTree(); + // Explicitely mark our search results if necessary + foreach ($this->searchIds as $id => $marked) { + if ($marked) { + $object = $this->objectForKey($id); + if (!$object) { + continue; + } + $markingSet->markToExpose($object); + } + } + // Allow to pass values to be selected within the ajax request $value = $request->requestVar('forceValue') ?: $this->value; if ($value && ($values = preg_split('/,\s*/', $value))) { diff --git a/tests/php/Forms/TreeDropdownFieldTest.php b/tests/php/Forms/TreeDropdownFieldTest.php index 5b5e22fd1..9af392f4b 100644 --- a/tests/php/Forms/TreeDropdownFieldTest.php +++ b/tests/php/Forms/TreeDropdownFieldTest.php @@ -9,12 +9,17 @@ use SilverStripe\Dev\CSSContentParser; use SilverStripe\Dev\SapphireTest; use SilverStripe\Control\HTTPRequest; use SilverStripe\Forms\TreeDropdownField; +use SilverStripe\ORM\Tests\HierarchyTest\TestObject; class TreeDropdownFieldTest extends SapphireTest { protected static $fixture_file = 'TreeDropdownFieldTest.yml'; + protected static $extra_dataobjects = [ + TestObject::class + ]; + public function testSchemaStateDefaults() { $field = new TreeDropdownField('TestTree', 'Test tree', Folder::class); @@ -97,6 +102,38 @@ class TreeDropdownFieldTest extends SapphireTest ); } + public function testTreeSearchJsonFlatlistWithLowNodeThreshold() + { + // Initialise our TreeDropDownField + $field = new TreeDropdownField('TestTree', 'Test tree', TestObject::class); + $field->config()->set('node_threshold_total', 2); + + // Search for all Test object matching our criteria + $request = new HTTPRequest( + 'GET', + 'url', + ['search' => 'MatchSearchCriteria', 'format' => 'json', 'flatList' => '1'] + ); + $request->setSession(new Session([])); + $response = $field->tree($request); + $tree = json_decode($response->getBody(), true); + $actualNodeIDs = array_column($tree['children'], 'id'); + + + // Get the list of expected node IDs from the YML Fixture + $expectedNodeIDs = array_map( + function ($key) { + return $this->objFromFixture(TestObject::class, $key)->ID; + }, + ['zero', 'oneA', 'twoAi', 'three'] // Those are the identifiers of the object we expect our search to find + ); + + sort($actualNodeIDs); + sort($expectedNodeIDs); + + $this->assertEquals($expectedNodeIDs, $actualNodeIDs); + } + public function testTreeSearch() { $field = new TreeDropdownField('TestTree', 'Test tree', Folder::class); diff --git a/tests/php/Forms/TreeDropdownFieldTest.yml b/tests/php/Forms/TreeDropdownFieldTest.yml index 7546885b3..2207cc300 100644 --- a/tests/php/Forms/TreeDropdownFieldTest.yml +++ b/tests/php/Forms/TreeDropdownFieldTest.yml @@ -8,6 +8,7 @@ SilverStripe\Assets\Folder: folder1-subfolder1: Name: FileTest-folder1-subfolder1 ParentID: =>SilverStripe\Assets\Folder.folder1 + SilverStripe\Assets\File: asdf: Filename: assets/FileTest.txt @@ -24,3 +25,40 @@ SilverStripe\Assets\File: Filename: assets/FileTest-folder1/File1.txt Name: File1.txt ParentID: =>SilverStripe\Assets\Folder.folder1 + +SilverStripe\ORM\Tests\HierarchyTest\TestObject: + zero: + Title: Zero MatchSearchCriteria + zeroA: + Title: Child A of Zero + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.zero + zeroB: + Title: Child B of Zero + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.zero + zeroC: + Title: Child C of Zero + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.zero + one: + Title: One + oneA: + Title: Child A of One MatchSearchCriteria + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.one + oneB: + Title: Child B of One + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.one + oneC: + Title: Child C of One + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.one + oneD: + Title: Child C of One + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.one + two: + Title: Two + twoA: + Title: Child A of Two + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.two + twoAi: + Title: Grandchild i of Child A of Two MatchSearchCriteria + ParentID: =>SilverStripe\ORM\Tests\HierarchyTest\TestObject.twoA + three: + Title: Three MatchSearchCriteria