2015-02-09 22:38:24 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\SS_HTTPRequest;
|
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
|
|
|
|
|
|
|
|
2015-02-09 22:38:24 +01:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class TreeDropdownFieldTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected static $fixture_file = 'TreeDropdownFieldTest.yml';
|
|
|
|
|
|
|
|
public function testTreeSearch(){
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', 'SilverStripe\\Assets\\Folder');
|
2015-02-09 22:38:24 +01:00
|
|
|
|
|
|
|
// case insensitive search against keyword 'sub' for folders
|
|
|
|
$request = new SS_HTTPRequest('GET','url',array('search'=>'sub'));
|
|
|
|
$tree = $field->tree($request);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$folder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder','folder1');
|
|
|
|
$folder1Subfolder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder','folder1-subfolder1');
|
2015-02-09 22:38:24 +01:00
|
|
|
|
|
|
|
$parser = new CSSContentParser($tree);
|
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-'.$folder1->ID.' li#selector-TestTree-'.$folder1Subfolder1->ID.' a span.item';
|
|
|
|
$firstResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
(string)$firstResult[0],
|
|
|
|
$folder1Subfolder1->Name,
|
2015-03-01 21:55:29 +01:00
|
|
|
$folder1Subfolder1->Name.' is found, nested under '.$folder1->Name
|
2015-02-09 22:38:24 +01:00
|
|
|
);
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$subfolder = $this->objFromFixture('SilverStripe\\Assets\\Folder','subfolder');
|
2015-02-09 22:38:24 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-'.$subfolder->ID.' a span.item';
|
|
|
|
$secondResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
(string)$secondResult[0],
|
|
|
|
$subfolder->Name,
|
2015-03-01 21:55:29 +01:00
|
|
|
$subfolder->Name.' is found at root level'
|
2015-02-09 22:38:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// other folders which don't contain the keyword 'sub' are not returned in search results
|
2016-08-19 00:51:35 +02:00
|
|
|
$folder2 = $this->objFromFixture('SilverStripe\\Assets\\Folder','folder2');
|
2015-02-09 22:38:24 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-'.$folder2->ID.' a span.item';
|
|
|
|
$noResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
$noResult,
|
|
|
|
array(),
|
2015-03-01 21:55:29 +01:00
|
|
|
$folder2.' is not found'
|
2015-02-09 22:38:24 +01:00
|
|
|
);
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$field = new TreeDropdownField('TestTree', 'Test tree', 'SilverStripe\\Assets\\File');
|
2015-02-09 22:38:24 +01:00
|
|
|
|
|
|
|
// case insensitive search against keyword 'sub' for files
|
|
|
|
$request = new SS_HTTPRequest('GET','url',array('search'=>'sub'));
|
|
|
|
$tree = $field->tree($request);
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($tree);
|
|
|
|
|
|
|
|
// Even if we used File as the source object, folders are still returned because Folder is a File
|
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-'.$folder1->ID.' li#selector-TestTree-'.$folder1Subfolder1->ID.' a span.item';
|
|
|
|
$firstResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
(string)$firstResult[0],
|
|
|
|
$folder1Subfolder1->Name,
|
2015-03-01 21:55:29 +01:00
|
|
|
$folder1Subfolder1->Name.' is found, nested under '.$folder1->Name
|
2015-02-09 22:38:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Looking for two files with 'sub' in their name, both under the same folder
|
2016-08-19 00:51:35 +02:00
|
|
|
$file1 = $this->objFromFixture('SilverStripe\\Assets\\File','subfolderfile1');
|
|
|
|
$file2 = $this->objFromFixture('SilverStripe\\Assets\\File','subfolderfile2');
|
2015-02-09 22:38:24 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-'.$subfolder->ID.' li#selector-TestTree-'.$file1->ID.' a';
|
|
|
|
$firstResult = $parser->getBySelector($cssPath);
|
2015-03-01 21:55:29 +01:00
|
|
|
$this->assertGreaterThan(
|
2016-01-06 00:34:58 +01:00
|
|
|
0,
|
|
|
|
count($firstResult),
|
2015-03-01 21:55:29 +01:00
|
|
|
$file1->Name.' with ID '.$file1->ID.' is in search results'
|
|
|
|
);
|
2015-02-09 22:38:24 +01:00
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
(string)$firstResult[0],
|
|
|
|
$file1->Name,
|
2015-03-01 21:55:29 +01:00
|
|
|
$file1->Name.' is found nested under '.$subfolder->Name
|
2015-02-09 22:38:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-'.$subfolder->ID.' li#selector-TestTree-'.$file2->ID.' a';
|
|
|
|
$secondResult = $parser->getBySelector($cssPath);
|
2015-03-01 21:55:29 +01:00
|
|
|
$this->assertGreaterThan(
|
|
|
|
0,
|
2016-01-06 00:34:58 +01:00
|
|
|
count($secondResult),
|
2015-03-01 21:55:29 +01:00
|
|
|
$file2->Name.' with ID '.$file2->ID.' is in search results'
|
|
|
|
);
|
2015-02-09 22:38:24 +01:00
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
(string)$secondResult[0],
|
|
|
|
$file2->Name,
|
2015-03-01 21:55:29 +01:00
|
|
|
$file2->Name.' is found nested under '.$subfolder->Name
|
2015-02-09 22:38:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// other files which don't include 'sub' are not returned in search results
|
2016-08-19 00:51:35 +02:00
|
|
|
$file3 = $this->objFromFixture('SilverStripe\\Assets\\File','asdf');
|
2015-02-09 22:38:24 +01:00
|
|
|
$cssPath = 'ul.tree li#selector-TestTree-'.$file3->ID;
|
|
|
|
$noResult = $parser->getBySelector($cssPath);
|
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
$noResult,
|
|
|
|
array(),
|
2015-03-01 21:55:29 +01:00
|
|
|
$file3->Name.' is not found'
|
2015-02-09 22:38:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|