2015-02-09 22:38:24 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Folder;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\Session;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class TreeDropdownFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'TreeDropdownFieldTest.yml';
|
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
public function testSchemaStateDefaults()
|
|
|
|
{
|
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
|
|
|
$folder = $this->objFromFixture(Folder::class, 'folder1-subfolder1');
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
$schema = $field->getSchemaStateDefaults();
|
|
|
|
$this->assertFalse(isset($schema['data']['valueObject']));
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
$field->setValue($folder->ID);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
$schema = $field->getSchemaStateDefaults();
|
|
|
|
$this->assertEquals($folder->ID, $schema['data']['valueObject']['id']);
|
|
|
|
$this->assertTrue(isset($schema['data']['valueObject']));
|
|
|
|
$this->assertFalse($schema['data']['showSelectedPath']);
|
|
|
|
$this->assertEquals('', $schema['data']['valueObject']['titlePath']);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
$field->setShowSelectedPath(true);
|
|
|
|
$schema = $field->getSchemaStateDefaults();
|
|
|
|
$this->assertTrue($schema['data']['showSelectedPath']);
|
|
|
|
$this->assertEquals(
|
|
|
|
'FileTest-folder1/FileTest-folder1-subfolder1/',
|
|
|
|
$schema['data']['valueObject']['titlePath']
|
|
|
|
);
|
|
|
|
}
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
public function testTreeSearchJson()
|
|
|
|
{
|
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
// case insensitive search against keyword 'sub' for folders
|
|
|
|
$request = new HTTPRequest('GET', 'url', array('search'=>'sub', 'format' => 'json'));
|
|
|
|
$request->setSession(new Session([]));
|
|
|
|
$response = $field->tree($request);
|
|
|
|
$tree = json_decode($response->getBody(), true);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
$folder1 = $this->objFromFixture(Folder::class, 'folder1');
|
|
|
|
$folder1Subfolder1 = $this->objFromFixture(Folder::class, 'folder1-subfolder1');
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
$this->assertContains(
|
|
|
|
$folder1->Name,
|
|
|
|
array_column($tree['children'], 'title'),
|
2018-01-16 19:39:30 +01:00
|
|
|
$folder1->Name . ' is found in the json'
|
2017-07-11 06:38:55 +02:00
|
|
|
);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
$filtered = array_filter($tree['children'], function ($entry) use ($folder1) {
|
|
|
|
return $folder1->Name === $entry['title'];
|
|
|
|
});
|
|
|
|
$folder1Tree = array_pop($filtered);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
$this->assertContains(
|
|
|
|
$folder1Subfolder1->Name,
|
|
|
|
array_column($folder1Tree['children'], 'title'),
|
2018-01-16 19:39:30 +01:00
|
|
|
$folder1Subfolder1->Name . ' is found in the folder1 entry in the json'
|
2017-07-11 06:38:55 +02:00
|
|
|
);
|
|
|
|
}
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
public function testTreeSearchJsonFlatlist()
|
|
|
|
{
|
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
// case insensitive search against keyword 'sub' for folders
|
|
|
|
$request = new HTTPRequest('GET', 'url', array('search'=>'sub', 'format' => 'json', 'flatList' => '1'));
|
|
|
|
$request->setSession(new Session([]));
|
|
|
|
$response = $field->tree($request);
|
|
|
|
$tree = json_decode($response->getBody(), true);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
$folder1 = $this->objFromFixture(Folder::class, 'folder1');
|
|
|
|
$folder1Subfolder1 = $this->objFromFixture(Folder::class, 'folder1-subfolder1');
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
$this->assertNotContains(
|
|
|
|
$folder1->Name,
|
|
|
|
array_column($tree['children'], 'title'),
|
2018-01-16 19:39:30 +01:00
|
|
|
$folder1->Name . ' is not found in the json'
|
2017-07-11 06:38:55 +02:00
|
|
|
);
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
$this->assertContains(
|
|
|
|
$folder1Subfolder1->Name,
|
|
|
|
array_column($tree['children'], 'title'),
|
2018-01-16 19:39:30 +01:00
|
|
|
$folder1Subfolder1->Name . ' is found in the json'
|
2017-07-11 06:38:55 +02:00
|
|
|
);
|
|
|
|
}
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testTreeSearch()
|
|
|
|
{
|
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
|
|
|
|
|
|
|
// case insensitive search against keyword 'sub' for folders
|
|
|
|
$request = new HTTPRequest('GET', 'url', array('search'=>'sub'));
|
2017-06-22 12:50:45 +02:00
|
|
|
$request->setSession(new Session([]));
|
2017-03-29 06:23:49 +02:00
|
|
|
$response = $field->tree($request);
|
|
|
|
$tree = $response->getBody();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$folder1 = $this->objFromFixture(Folder::class, 'folder1');
|
|
|
|
$folder1Subfolder1 = $this->objFromFixture(Folder::class, 'folder1-subfolder1');
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($tree);
|
2018-01-16 19:39:30 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-' . $folder1->ID . ' li#selector-TestTree-' . $folder1Subfolder1->ID . ' a span.item';
|
2016-12-16 05:34:21 +01:00
|
|
|
$firstResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
|
|
|
$folder1Subfolder1->Name,
|
2017-01-31 16:17:29 +01:00
|
|
|
(string)$firstResult[0],
|
2018-01-16 19:39:30 +01:00
|
|
|
$folder1Subfolder1->Name . ' is found, nested under ' . $folder1->Name
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$subfolder = $this->objFromFixture(Folder::class, 'subfolder');
|
2018-01-16 19:39:30 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' a span.item';
|
2016-12-16 05:34:21 +01:00
|
|
|
$secondResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
|
|
|
$subfolder->Name,
|
2017-01-31 16:17:29 +01:00
|
|
|
(string)$secondResult[0],
|
2018-01-16 19:39:30 +01:00
|
|
|
$subfolder->Name . ' is found at root level'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// other folders which don't contain the keyword 'sub' are not returned in search results
|
|
|
|
$folder2 = $this->objFromFixture(Folder::class, 'folder2');
|
2018-01-16 19:39:30 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-' . $folder2->ID . ' a span.item';
|
2016-12-16 05:34:21 +01:00
|
|
|
$noResult = $parser->getBySelector($cssPath);
|
2017-01-31 16:17:29 +01:00
|
|
|
$this->assertEmpty(
|
2016-12-16 05:34:21 +01:00
|
|
|
$noResult,
|
2018-01-16 19:39:30 +01:00
|
|
|
$folder2 . ' is not found'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', File::class);
|
|
|
|
|
|
|
|
// case insensitive search against keyword 'sub' for files
|
|
|
|
$request = new HTTPRequest('GET', 'url', array('search'=>'sub'));
|
2017-06-22 12:50:45 +02:00
|
|
|
$request->setSession(new Session([]));
|
2017-03-29 06:23:49 +02:00
|
|
|
$response = $field->tree($request);
|
|
|
|
$tree = $response->getBody();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$parser = new CSSContentParser($tree);
|
|
|
|
|
|
|
|
// Even if we used File as the source object, folders are still returned because Folder is a File
|
2018-01-16 19:39:30 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-' . $folder1->ID . ' li#selector-TestTree-' . $folder1Subfolder1->ID . ' a span.item';
|
2016-12-16 05:34:21 +01:00
|
|
|
$firstResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
|
|
|
$folder1Subfolder1->Name,
|
2017-01-31 16:17:29 +01:00
|
|
|
(string)$firstResult[0],
|
2018-01-16 19:39:30 +01:00
|
|
|
$folder1Subfolder1->Name . ' is found, nested under ' . $folder1->Name
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Looking for two files with 'sub' in their name, both under the same folder
|
|
|
|
$file1 = $this->objFromFixture(File::class, 'subfolderfile1');
|
|
|
|
$file2 = $this->objFromFixture(File::class, 'subfolderfile2');
|
2018-01-16 19:39:30 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' li#selector-TestTree-' . $file1->ID . ' a';
|
2016-12-16 05:34:21 +01:00
|
|
|
$firstResult = $parser->getBySelector($cssPath);
|
2017-01-31 16:17:29 +01:00
|
|
|
$this->assertNotEmpty(
|
|
|
|
$firstResult,
|
2018-01-16 19:39:30 +01:00
|
|
|
$file1->Name . ' with ID ' . $file1->ID . ' is in search results'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$file1->Name,
|
2017-01-31 16:17:29 +01:00
|
|
|
(string)$firstResult[0],
|
2018-01-16 19:39:30 +01:00
|
|
|
$file1->Name . ' is found nested under ' . $subfolder->Name
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2018-01-16 19:39:30 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' li#selector-TestTree-' . $file2->ID . ' a';
|
2016-12-16 05:34:21 +01:00
|
|
|
$secondResult = $parser->getBySelector($cssPath);
|
2017-01-31 16:17:29 +01:00
|
|
|
$this->assertNotEmpty(
|
|
|
|
$secondResult,
|
2018-01-16 19:39:30 +01:00
|
|
|
$file2->Name . ' with ID ' . $file2->ID . ' is in search results'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$file2->Name,
|
2017-01-31 16:17:29 +01:00
|
|
|
(string)$secondResult[0],
|
2018-01-16 19:39:30 +01:00
|
|
|
$file2->Name . ' is found nested under ' . $subfolder->Name
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// other files which don't include 'sub' are not returned in search results
|
|
|
|
$file3 = $this->objFromFixture(File::class, 'asdf');
|
2018-01-16 19:39:30 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-' . $file3->ID;
|
2016-12-16 05:34:21 +01:00
|
|
|
$noResult = $parser->getBySelector($cssPath);
|
2017-01-31 16:17:29 +01:00
|
|
|
$this->assertEmpty(
|
2016-12-16 05:34:21 +01:00
|
|
|
$noResult,
|
2018-01-16 19:39:30 +01:00
|
|
|
$file3->Name . ' is not found'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2017-10-26 02:04:30 +02:00
|
|
|
|
|
|
|
public function testReadonly()
|
|
|
|
{
|
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', File::class);
|
2018-06-01 07:47:03 +02:00
|
|
|
$fileMock = $this->objFromFixture(File::class, 'asdf');
|
|
|
|
$field->setValue($fileMock->ID);
|
2017-10-26 02:04:30 +02:00
|
|
|
$readonlyField = $field->performReadonlyTransformation();
|
2018-06-01 07:47:03 +02:00
|
|
|
$result = (string) $readonlyField->Field();
|
|
|
|
$this->assertContains(
|
|
|
|
'<span class="readonly" id="TestTree"><Special & characters></span>',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'<input type="hidden" name="TestTree" value="' . $fileMock->ID . '" />',
|
|
|
|
$result
|
2017-10-26 02:04:30 +02:00
|
|
|
);
|
|
|
|
}
|
2015-02-09 22:38:24 +01:00
|
|
|
}
|