Merge pull request #8621 from open-sausages/pulls/4.3/tweak-site-tree-search

FIX Explicity mark nodes when searching
This commit is contained in:
Guy Marriott 2018-11-23 16:14:46 +13:00 committed by GitHub
commit 84c8dace7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 0 deletions

View File

@ -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))) {

View File

@ -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);

View File

@ -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