2011-08-29 14:31:03 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\Tests\ListboxFieldTest\Article;
|
|
|
|
use SilverStripe\Forms\Tests\ListboxFieldTest\Tag;
|
|
|
|
use SilverStripe\Forms\Tests\ListboxFieldTest\TestObject;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\ListboxField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
class ListboxFieldTest extends SapphireTest {
|
2012-03-05 10:55:09 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'ListboxFieldTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
TestObject::class,
|
|
|
|
Article::class,
|
|
|
|
Tag::class
|
|
|
|
);
|
2012-03-05 10:55:09 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldWithManyManyRelationship() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$articleWithTags = $this->objFromFixture(Article::class, 'articlewithtags');
|
|
|
|
$tag1 = $this->objFromFixture(Tag::class, 'tag1');
|
|
|
|
$tag2 = $this->objFromFixture(Tag::class, 'tag2');
|
|
|
|
$tag3 = $this->objFromFixture(Tag::class, 'tag3');
|
|
|
|
$field = new ListboxField("Tags", "Test field", DataObject::get(Tag::class)->map()->toArray());
|
2012-03-05 10:55:09 +01:00
|
|
|
$field->setValue(null, $articleWithTags);
|
|
|
|
|
|
|
|
$p = new CSSContentParser($field->Field());
|
|
|
|
$tag1xml = $p->getByXpath('//option[@value=' . $tag1->ID . ']');
|
|
|
|
$tag2xml = $p->getByXpath('//option[@value=' . $tag2->ID . ']');
|
|
|
|
$tag3xml = $p->getByXpath('//option[@value=' . $tag3->ID . ']');
|
|
|
|
$this->assertEquals('selected', (string)$tag1xml[0]['selected']);
|
|
|
|
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
|
|
|
|
$this->assertNull($tag3xml[0]['selected']);
|
|
|
|
}
|
2012-03-05 12:27:55 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldWithDisabledItems() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$articleWithTags = $this->objFromFixture(Article::class, 'articlewithtags');
|
|
|
|
$tag1 = $this->objFromFixture(Tag::class, 'tag1');
|
|
|
|
$tag2 = $this->objFromFixture(Tag::class, 'tag2');
|
|
|
|
$tag3 = $this->objFromFixture(Tag::class, 'tag3');
|
|
|
|
$field = new ListboxField("Tags", "Test field", DataObject::get(Tag::class)->map()->toArray());
|
2012-03-05 12:27:55 +01:00
|
|
|
$field->setValue(null, $articleWithTags);
|
|
|
|
$field->setDisabledItems(array($tag1->ID, $tag3->ID));
|
|
|
|
|
|
|
|
$p = new CSSContentParser($field->Field());
|
|
|
|
$tag1xml = $p->getByXpath('//option[@value=' . $tag1->ID . ']');
|
|
|
|
$tag2xml = $p->getByXpath('//option[@value=' . $tag2->ID . ']');
|
|
|
|
$tag3xml = $p->getByXpath('//option[@value=' . $tag3->ID . ']');
|
|
|
|
$this->assertEquals('selected', (string)$tag1xml[0]['selected']);
|
|
|
|
$this->assertEquals('disabled', (string)$tag1xml[0]['disabled']);
|
|
|
|
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
|
|
|
|
$this->assertNull($tag2xml[0]['disabled']);
|
|
|
|
$this->assertNull($tag3xml[0]['selected']);
|
|
|
|
$this->assertEquals('disabled', (string)$tag3xml[0]['disabled']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveIntoNullValueWithMultipleOff() {
|
2011-08-29 14:31:03 +02:00
|
|
|
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
|
|
|
|
$field = new ListboxField('Choices', 'Choices', $choices);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new TestObject();
|
2011-08-29 14:31:03 +02:00
|
|
|
$field->setValue('a');
|
|
|
|
$field->saveInto($obj);
|
|
|
|
$field->setValue(null);
|
|
|
|
$field->saveInto($obj);
|
|
|
|
$this->assertNull($obj->Choices);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveIntoNullValueWithMultipleOn() {
|
2011-08-29 14:31:03 +02:00
|
|
|
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
|
|
|
|
$field = new ListboxField('Choices', 'Choices', $choices);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new TestObject();
|
2015-02-13 05:35:39 +01:00
|
|
|
$field->setValue(array('a', 'c'));
|
2011-08-29 14:31:03 +02:00
|
|
|
$field->saveInto($obj);
|
|
|
|
$field->setValue('');
|
|
|
|
$field->saveInto($obj);
|
|
|
|
$this->assertEquals('', $obj->Choices);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveInto() {
|
2011-08-29 14:31:03 +02:00
|
|
|
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
|
|
|
|
$field = new ListboxField('Choices', 'Choices', $choices);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new TestObject();
|
2011-08-29 14:31:03 +02:00
|
|
|
$field->setValue('a');
|
|
|
|
$field->saveInto($obj);
|
2015-02-13 05:35:39 +01:00
|
|
|
$this->assertEquals('["a"]', $obj->Choices);
|
2011-08-29 14:31:03 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveIntoMultiple() {
|
2011-08-29 14:31:03 +02:00
|
|
|
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
|
|
|
|
$field = new ListboxField('Choices', 'Choices', $choices);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
// As array
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj1 = new TestObject();
|
2011-08-29 14:31:03 +02:00
|
|
|
$field->setValue(array('a', 'c'));
|
|
|
|
$field->saveInto($obj1);
|
2015-02-13 05:35:39 +01:00
|
|
|
$this->assertEquals('["a","c"]', $obj1->Choices);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
// As string
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj2 = new TestObject();
|
2015-02-13 05:35:39 +01:00
|
|
|
$obj2->Choices = '["a","c"]';
|
|
|
|
$field->setValue(null, $obj2);
|
|
|
|
$this->assertEquals(array('a', 'c'), $field->Value());
|
2011-08-29 14:31:03 +02:00
|
|
|
$field->saveInto($obj2);
|
2015-02-13 05:35:39 +01:00
|
|
|
$this->assertEquals('["a","c"]', $obj2->Choices);
|
2011-08-29 14:31:03 +02:00
|
|
|
}
|
2012-03-05 10:55:09 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveIntoManyManyRelation() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$article = $this->objFromFixture(Article::class, 'articlewithouttags');
|
|
|
|
$articleWithTags = $this->objFromFixture(Article::class, 'articlewithtags');
|
|
|
|
$tag1 = $this->objFromFixture(Tag::class, 'tag1');
|
|
|
|
$tag2 = $this->objFromFixture(Tag::class, 'tag2');
|
|
|
|
$field = new ListboxField("Tags", "Test field", DataObject::get(Tag::class)->map()->toArray());
|
2012-03-05 10:55:09 +01:00
|
|
|
|
|
|
|
// Save new relations
|
|
|
|
$field->setValue(array($tag1->ID,$tag2->ID));
|
|
|
|
$field->saveInto($article);
|
2016-10-14 03:30:05 +02:00
|
|
|
$article = DataObject::get_by_id(Article::class, $article->ID, false);
|
2012-04-10 07:05:29 +02:00
|
|
|
$this->assertEquals(array($tag1->ID, $tag2->ID), $article->Tags()->sort('ID')->column('ID'));
|
2012-03-05 10:55:09 +01:00
|
|
|
|
|
|
|
// Remove existing relation
|
|
|
|
$field->setValue(array($tag1->ID));
|
|
|
|
$field->saveInto($article);
|
2016-10-14 03:30:05 +02:00
|
|
|
$article = DataObject::get_by_id(Article::class, $article->ID, false);
|
2012-04-10 07:05:29 +02:00
|
|
|
$this->assertEquals(array($tag1->ID), $article->Tags()->sort('ID')->column('ID'));
|
2012-03-05 10:55:09 +01:00
|
|
|
|
|
|
|
// Set NULL value
|
|
|
|
$field->setValue(null);
|
|
|
|
$field->saveInto($article);
|
2016-10-14 03:30:05 +02:00
|
|
|
$article = DataObject::get_by_id(Article::class, $article->ID, false);
|
2012-04-10 07:05:29 +02:00
|
|
|
$this->assertEquals(array(), $article->Tags()->sort('ID')->column('ID'));
|
2012-03-05 10:55:09 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldRenderingMultipleOff() {
|
2011-08-29 14:31:03 +02:00
|
|
|
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
|
|
|
|
$field = new ListboxField('Choices', 'Choices', $choices);
|
|
|
|
$field->setValue('a');
|
|
|
|
$parser = new CSSContentParser($field->Field());
|
|
|
|
$optEls = $parser->getBySelector('option');
|
|
|
|
$this->assertEquals(3, count($optEls));
|
|
|
|
$this->assertEquals('selected', (string)$optEls[0]['selected']);
|
|
|
|
$this->assertEquals('', (string)$optEls[1]['selected']);
|
|
|
|
$this->assertEquals('', (string)$optEls[2]['selected']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldRenderingMultipleOn() {
|
2011-08-29 14:31:03 +02:00
|
|
|
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
|
|
|
|
$field = new ListboxField('Choices', 'Choices', $choices);
|
2015-02-13 05:35:39 +01:00
|
|
|
$field->setValue(array('a', 'c'));
|
2011-08-29 14:31:03 +02:00
|
|
|
$parser = new CSSContentParser($field->Field());
|
|
|
|
$optEls = $parser->getBySelector('option');
|
|
|
|
$this->assertEquals(3, count($optEls));
|
|
|
|
$this->assertEquals('selected', (string)$optEls[0]['selected']);
|
|
|
|
$this->assertEquals('', (string)$optEls[1]['selected']);
|
|
|
|
$this->assertEquals('selected', (string)$optEls[2]['selected']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-11-12 03:19:12 +01:00
|
|
|
public function testValidationWithArray() {
|
|
|
|
//test with array input
|
|
|
|
$field = ListboxField::create('Test', 'Testing', array(
|
|
|
|
1 => "One",
|
|
|
|
2 => "Two",
|
|
|
|
3 => "Three"
|
|
|
|
));
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
|
|
|
|
$field->setValue(1);
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Validates values in source map'
|
|
|
|
);
|
|
|
|
$field->setValue(array(1));
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Validates values within source array'
|
|
|
|
);
|
|
|
|
//non valid value should fail
|
|
|
|
$field->setValue(4);
|
|
|
|
$this->assertFalse(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Does not validates values not within source array'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidationWithDataList() {
|
|
|
|
//test with datalist input
|
2016-10-14 03:30:05 +02:00
|
|
|
$tag1 = $this->objFromFixture(Tag::class, 'tag1');
|
|
|
|
$tag2 = $this->objFromFixture(Tag::class, 'tag2');
|
|
|
|
$tag3 = $this->objFromFixture(Tag::class, 'tag3');
|
|
|
|
$field = ListboxField::create('Test', 'Testing', DataObject::get(Tag::class)->map()->toArray());
|
2014-11-12 03:19:12 +01:00
|
|
|
$validator = new RequiredFields();
|
|
|
|
|
|
|
|
$field->setValue(
|
|
|
|
$tag1->ID
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Field validates values in source map'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo re-enable these tests when field validation is removed from {@link ListboxField::setValue()} and moved
|
|
|
|
* to the {@link ListboxField::validate()} function
|
|
|
|
*/
|
|
|
|
// $field->setValue(4);
|
|
|
|
// $this->assertFalse(
|
|
|
|
// $field->validate($validator),
|
|
|
|
// 'Field does not validate values outside of source map'
|
|
|
|
// );
|
|
|
|
$field->setValue(false, new ArrayData(array(
|
|
|
|
$tag1->ID => $tag1->ID,
|
|
|
|
$tag2->ID => $tag2->ID
|
|
|
|
)));
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Validates values in source map'
|
|
|
|
);
|
|
|
|
//invalid value should fail
|
|
|
|
$field->setValue(4);
|
|
|
|
$this->assertFalse(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Does not validate values not within source map'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
}
|