2017-10-26 02:04:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2018-11-23 03:17:17 +01:00
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\FormTemplateHelper;
|
2017-10-26 02:04:30 +02:00
|
|
|
use SilverStripe\Forms\TreeMultiselectField;
|
2019-01-24 15:45:38 +01:00
|
|
|
use SilverStripe\ORM\Tests\HierarchyTest\TestObject;
|
|
|
|
use SilverStripe\View\SSViewer;
|
2017-10-26 02:04:30 +02:00
|
|
|
|
|
|
|
class TreeMultiselectFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'TreeDropdownFieldTest.yml';
|
|
|
|
|
2019-01-24 15:45:38 +01:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
TestObject::class,
|
|
|
|
];
|
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
protected $formId = 'TheFormID';
|
|
|
|
protected $fieldName = 'TestTree';
|
|
|
|
|
2018-11-29 00:13:56 +01:00
|
|
|
/**
|
|
|
|
* Mock object of a generic form
|
|
|
|
*
|
|
|
|
* @var Form
|
|
|
|
*/
|
|
|
|
protected $form;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of the TreeMultiselectField
|
|
|
|
*
|
|
|
|
* @var TreeMultiselectField
|
|
|
|
*/
|
|
|
|
protected $field;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The File objects of folders loaded from the fixture
|
|
|
|
*
|
|
|
|
* @var File[]
|
|
|
|
*/
|
|
|
|
protected $folders;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The array of folder ids
|
|
|
|
*
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
protected $folderIds;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatenated folder ids for use as a value for the field
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $fieldValue;
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2018-11-23 03:17:17 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-01-24 15:45:38 +01:00
|
|
|
// Don't let other themes interfere with these tests
|
|
|
|
SSViewer::set_themes([]);
|
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
$this->form = $this->buildFormMock();
|
|
|
|
$this->field = $this->buildField($this->form);
|
|
|
|
$this->folders = $this->loadFolders();
|
|
|
|
|
|
|
|
$this->folderIds = array_map(
|
|
|
|
static function ($f) {
|
|
|
|
return $f->ID;
|
|
|
|
},
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->folders ?? []
|
2018-11-23 03:17:17 +01:00
|
|
|
);
|
|
|
|
$this->fieldValue = implode(',', $this->folderIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a new mock object of a Form
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
protected function buildFormMock()
|
|
|
|
{
|
|
|
|
$form = $this->createMock(Form::class);
|
|
|
|
|
|
|
|
$form->method('getTemplateHelper')
|
|
|
|
->willReturn(FormTemplateHelper::singleton());
|
|
|
|
|
|
|
|
$form->method('getHTMLID')
|
|
|
|
->willReturn($this->formId);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a new instance of TreeMultiselectField
|
|
|
|
*
|
|
|
|
* @param Form $form The field form
|
|
|
|
*
|
|
|
|
* @return TreeMultiselectField
|
|
|
|
*/
|
|
|
|
protected function buildField(Form $form)
|
2018-11-20 02:04:12 +01:00
|
|
|
{
|
2018-11-23 03:17:17 +01:00
|
|
|
$field = new TreeMultiselectField($this->fieldName, 'Test tree', File::class);
|
|
|
|
$field->setForm($form);
|
2018-11-20 02:04:12 +01:00
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load several files from the fixtures and return them in an array
|
|
|
|
*
|
|
|
|
* @return File[]
|
|
|
|
*/
|
|
|
|
protected function loadFolders()
|
|
|
|
{
|
2018-11-20 02:04:12 +01:00
|
|
|
$asdf = $this->objFromFixture(File::class, 'asdf');
|
|
|
|
$subfolderfile1 = $this->objFromFixture(File::class, 'subfolderfile1');
|
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
return [$asdf, $subfolderfile1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the TreeMultiselectField behaviour with no selected values
|
|
|
|
*/
|
|
|
|
public function testEmpty()
|
|
|
|
{
|
|
|
|
$field = $this->field;
|
|
|
|
|
|
|
|
$fieldId = $field->ID();
|
|
|
|
$this->assertEquals($fieldId, sprintf('%s_%s', $this->formId, $this->fieldName));
|
|
|
|
|
2018-11-20 02:04:12 +01:00
|
|
|
$schemaStateDefaults = $field->getSchemaStateDefaults();
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame($fieldId, $schemaStateDefaults['id']);
|
|
|
|
$this->assertSame($this->fieldName, $schemaStateDefaults['name']);
|
|
|
|
$this->assertSame('unchanged', $schemaStateDefaults['value']);
|
2018-11-20 02:04:12 +01:00
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
$schemaDataDefaults = $field->getSchemaDataDefaults();
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame($fieldId, $schemaDataDefaults['id']);
|
|
|
|
$this->assertSame($this->fieldName, $schemaDataDefaults['name']);
|
|
|
|
$this->assertSame('text', $schemaDataDefaults['type']);
|
|
|
|
$this->assertSame('SingleSelect', $schemaDataDefaults['schemaType']);
|
|
|
|
$this->assertSame('TreeDropdownField', $schemaDataDefaults['component']);
|
|
|
|
$this->assertSame(sprintf('%s_Holder', $fieldId), $schemaDataDefaults['holderId']);
|
|
|
|
$this->assertSame('Test tree', $schemaDataDefaults['title']);
|
|
|
|
$this->assertSame('treemultiselect multiple searchable', $schemaDataDefaults['extraClass']);
|
|
|
|
$this->assertSame('field/TestTree/tree', $schemaDataDefaults['data']['urlTree']);
|
|
|
|
$this->assertSame(true, $schemaDataDefaults['data']['showSearch']);
|
|
|
|
$this->assertSame('(Search or choose File)', $schemaDataDefaults['data']['emptyString']);
|
|
|
|
$this->assertSame(false, $schemaDataDefaults['data']['hasEmptyDefault']);
|
|
|
|
$this->assertSame(true, $schemaDataDefaults['data']['multiple']);
|
2018-11-23 03:17:17 +01:00
|
|
|
|
|
|
|
$items = $field->getItems();
|
|
|
|
$this->assertCount(0, $items, 'there must be no items selected');
|
|
|
|
|
|
|
|
$html = $field->Field();
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString($field->ID(), $html);
|
|
|
|
$this->assertStringContainsString('unchanged', $html);
|
2018-11-20 02:04:12 +01:00
|
|
|
}
|
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the field with some values set
|
|
|
|
*/
|
|
|
|
public function testChanged()
|
2017-10-26 02:04:30 +02:00
|
|
|
{
|
2018-11-23 03:17:17 +01:00
|
|
|
$field = $this->field;
|
2018-11-20 02:04:12 +01:00
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
$field->setValue($this->fieldValue);
|
2018-11-20 02:04:12 +01:00
|
|
|
|
2018-11-23 03:17:17 +01:00
|
|
|
$schemaStateDefaults = $field->getSchemaStateDefaults();
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame($field->ID(), $schemaStateDefaults['id']);
|
|
|
|
$this->assertSame('TestTree', $schemaStateDefaults['name']);
|
|
|
|
$this->assertSame($this->folderIds, $schemaStateDefaults['value']);
|
2018-11-23 03:17:17 +01:00
|
|
|
|
|
|
|
$items = $field->getItems();
|
|
|
|
$this->assertCount(2, $items, 'there must be exactly 2 items selected');
|
|
|
|
|
|
|
|
$html = $field->Field();
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString($field->ID(), $html);
|
|
|
|
$this->assertStringContainsString($this->fieldValue, $html);
|
2018-11-23 03:17:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test empty field in readonly mode
|
|
|
|
*/
|
|
|
|
public function testEmptyReadonly()
|
|
|
|
{
|
|
|
|
$field = $this->field->performReadonlyTransformation();
|
2017-10-26 02:04:30 +02:00
|
|
|
|
2018-11-20 02:04:12 +01:00
|
|
|
$schemaStateDefaults = $field->getSchemaStateDefaults();
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame($field->ID(), $schemaStateDefaults['id']);
|
|
|
|
$this->assertSame('TestTree', $schemaStateDefaults['name']);
|
|
|
|
$this->assertSame('unchanged', $schemaStateDefaults['value']);
|
2018-11-23 03:17:17 +01:00
|
|
|
|
|
|
|
$schemaDataDefaults = $field->getSchemaDataDefaults();
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame($field->ID(), $schemaDataDefaults['id']);
|
|
|
|
$this->assertSame($this->fieldName, $schemaDataDefaults['name']);
|
|
|
|
$this->assertSame('text', $schemaDataDefaults['type']);
|
|
|
|
$this->assertSame('SingleSelect', $schemaDataDefaults['schemaType']);
|
|
|
|
$this->assertSame('TreeDropdownField', $schemaDataDefaults['component']);
|
|
|
|
$this->assertSame(sprintf('%s_Holder', $field->ID()), $schemaDataDefaults['holderId']);
|
|
|
|
$this->assertSame('Test tree', $schemaDataDefaults['title']);
|
2021-12-16 12:52:11 +01:00
|
|
|
$this->assertSame('treemultiselectfield_readonly multiple searchable', $schemaDataDefaults['extraClass']);
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame('field/TestTree/tree', $schemaDataDefaults['data']['urlTree']);
|
|
|
|
$this->assertSame(true, $schemaDataDefaults['data']['showSearch']);
|
|
|
|
$this->assertSame('(Search or choose File)', $schemaDataDefaults['data']['emptyString']);
|
|
|
|
$this->assertSame(false, $schemaDataDefaults['data']['hasEmptyDefault']);
|
|
|
|
$this->assertSame(true, $schemaDataDefaults['data']['multiple']);
|
2018-11-20 02:04:12 +01:00
|
|
|
|
2018-11-22 00:11:18 +01:00
|
|
|
$items = $field->getItems();
|
2018-11-23 03:17:17 +01:00
|
|
|
$this->assertCount(0, $items, 'there must be 0 selected items');
|
|
|
|
|
|
|
|
$html = $field->Field();
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString($field->ID(), $html);
|
2018-11-23 03:17:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test changed field in readonly mode
|
|
|
|
*/
|
|
|
|
public function testChangedReadonly()
|
|
|
|
{
|
|
|
|
$field = $this->field;
|
|
|
|
$field->setValue($this->fieldValue);
|
|
|
|
$field = $field->performReadonlyTransformation();
|
|
|
|
|
|
|
|
$schemaStateDefaults = $field->getSchemaStateDefaults();
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame($field->ID(), $schemaStateDefaults['id']);
|
|
|
|
$this->assertSame('TestTree', $schemaStateDefaults['name']);
|
|
|
|
$this->assertSame($this->folderIds, $schemaStateDefaults['value']);
|
2018-11-23 03:17:17 +01:00
|
|
|
|
|
|
|
$schemaDataDefaults = $field->getSchemaDataDefaults();
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame($field->ID(), $schemaDataDefaults['id']);
|
|
|
|
$this->assertSame($this->fieldName, $schemaDataDefaults['name']);
|
|
|
|
$this->assertSame('text', $schemaDataDefaults['type']);
|
|
|
|
$this->assertSame('SingleSelect', $schemaDataDefaults['schemaType']);
|
|
|
|
$this->assertSame('TreeDropdownField', $schemaDataDefaults['component']);
|
|
|
|
$this->assertSame(sprintf('%s_Holder', $field->ID()), $schemaDataDefaults['holderId']);
|
|
|
|
$this->assertSame('Test tree', $schemaDataDefaults['title']);
|
2021-12-16 12:52:11 +01:00
|
|
|
$this->assertSame('treemultiselectfield_readonly multiple searchable', $schemaDataDefaults['extraClass']);
|
2021-11-05 00:06:55 +01:00
|
|
|
$this->assertSame('field/TestTree/tree', $schemaDataDefaults['data']['urlTree']);
|
|
|
|
$this->assertSame(true, $schemaDataDefaults['data']['showSearch']);
|
|
|
|
$this->assertSame('(Search or choose File)', $schemaDataDefaults['data']['emptyString']);
|
|
|
|
$this->assertSame(false, $schemaDataDefaults['data']['hasEmptyDefault']);
|
|
|
|
$this->assertSame(true, $schemaDataDefaults['data']['multiple']);
|
2018-11-23 03:17:17 +01:00
|
|
|
|
|
|
|
$items = $field->getItems();
|
|
|
|
$this->assertCount(2, $items, 'there must be exactly 2 selected items');
|
|
|
|
|
|
|
|
$html = $field->Field();
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString($field->ID(), $html);
|
|
|
|
$this->assertStringContainsString($this->fieldValue, $html);
|
2017-10-26 02:04:30 +02:00
|
|
|
}
|
2020-09-11 01:52:36 +02:00
|
|
|
|
|
|
|
public function testGetItems()
|
|
|
|
{
|
|
|
|
// Default items scaffolded from 'unchanged' value (empty)
|
|
|
|
$field = $this->field;
|
|
|
|
$this->assertListEquals(
|
|
|
|
[],
|
|
|
|
$field->getItems()
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedItem = array_map(
|
|
|
|
function ($folder) {
|
|
|
|
return [
|
|
|
|
'Filename' => $folder->Filename,
|
|
|
|
];
|
|
|
|
},
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->loadFolders() ?? []
|
2020-09-11 01:52:36 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Set list of items by array of ids
|
|
|
|
$field->setValue($this->folderIds);
|
|
|
|
$this->assertListEquals(
|
|
|
|
$expectedItem,
|
|
|
|
$field->getItems()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set list of items by comma separated ids
|
|
|
|
$field->setValue($this->fieldValue);
|
|
|
|
$this->assertListEquals(
|
|
|
|
$expectedItem,
|
|
|
|
$field->getItems()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Handle legacy empty value (form submits 'unchanged')
|
|
|
|
$field->setValue('unchanged');
|
|
|
|
$this->assertListEquals(
|
|
|
|
[],
|
|
|
|
$field->getItems()
|
|
|
|
);
|
2021-01-20 00:23:09 +01:00
|
|
|
|
|
|
|
// Andle empty string none value
|
|
|
|
$field->setValue('');
|
|
|
|
$this->assertListEquals(
|
|
|
|
[],
|
|
|
|
$field->getItems()
|
|
|
|
);
|
2020-09-11 01:52:36 +02:00
|
|
|
}
|
2017-10-26 02:04:30 +02:00
|
|
|
}
|