2011-09-14 10:58:17 +02:00
|
|
|
<?php
|
2013-05-10 14:01:39 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\LookupField;
|
2018-06-01 07:47:03 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class LookupFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'LookupFieldTest.yml';
|
|
|
|
|
|
|
|
public function testNullValueWithNumericArraySource()
|
|
|
|
{
|
|
|
|
$source = array(1 => 'one', 2 => 'two', 3 => 'three');
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue(null);
|
|
|
|
$result = trim($field->Field()->getValue());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-06-01 07:47:03 +02:00
|
|
|
$this->assertContains('<span class="readonly" id="test"><i>(none)</i></span>', $result);
|
|
|
|
$this->assertContains('<input type="hidden" name="test" value="" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testStringValueWithNumericArraySource()
|
|
|
|
{
|
|
|
|
$source = array(1 => 'one', 2 => 'two', 3 => 'three');
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue(1);
|
|
|
|
$result = trim($field->Field()->getValue());
|
|
|
|
$this->assertContains('<span class="readonly" id="test">one</span>', $result);
|
|
|
|
$this->assertContains('<input type="hidden" name="test" value="1" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnknownStringValueWithNumericArraySource()
|
|
|
|
{
|
|
|
|
$source = array(1 => 'one', 2 => 'two', 3 => 'three');
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue('w00t');
|
|
|
|
$result = trim($field->Field()->getValue());
|
|
|
|
|
|
|
|
$this->assertContains('<span class="readonly" id="test">w00t</span>', $result);
|
|
|
|
$this->assertContains('<input type="hidden" name="test" value="" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayValueWithAssociativeArraySource()
|
|
|
|
{
|
|
|
|
// Array values (= multiple selections) might be set e.g. from ListboxField
|
|
|
|
$source = array('one' => 'one val', 'two' => 'two val', 'three' => 'three val');
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue(array('one','two'));
|
|
|
|
$result = trim($field->Field()->getValue());
|
|
|
|
|
|
|
|
$this->assertContains('<span class="readonly" id="test">one val, two val</span>', $result);
|
|
|
|
$this->assertContains('<input type="hidden" name="test" value="one, two" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayValueWithNumericArraySource()
|
|
|
|
{
|
|
|
|
// Array values (= multiple selections) might be set e.g. from ListboxField
|
|
|
|
$source = array(1 => 'one', 2 => 'two', 3 => 'three');
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue(array(1,2));
|
|
|
|
$result = trim($field->Field()->getValue());
|
|
|
|
|
|
|
|
$this->assertContains('<span class="readonly" id="test">one, two</span>', $result);
|
|
|
|
$this->assertContains('<input type="hidden" name="test" value="1, 2" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayValueWithSqlMapSource()
|
|
|
|
{
|
2018-06-01 07:47:03 +02:00
|
|
|
$member1 = $this->objFromFixture(Member::class, 'member1');
|
|
|
|
$member2 = $this->objFromFixture(Member::class, 'member2');
|
|
|
|
$member3 = $this->objFromFixture(Member::class, 'member3');
|
|
|
|
|
|
|
|
$source = DataObject::get(Member::class);
|
|
|
|
$field = new LookupField('test', 'test', $source->map('ID', 'FirstName'));
|
|
|
|
$field->setValue(array($member1->ID, $member2->ID));
|
|
|
|
$result = trim($field->Field()->getValue());
|
|
|
|
|
|
|
|
$this->assertContains('<span class="readonly" id="test">member1, member2</span>', $result);
|
|
|
|
$this->assertContains(sprintf(
|
|
|
|
'<input type="hidden" name="test" value="%s, %s" />',
|
|
|
|
$member1->ID,
|
|
|
|
$member2->ID
|
|
|
|
), $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithMultiDimensionalSource()
|
|
|
|
{
|
|
|
|
$choices = array(
|
|
|
|
"Non-vegetarian" => array(
|
|
|
|
0 => 'Carnivore',
|
|
|
|
),
|
|
|
|
"Vegetarian" => array(
|
|
|
|
3 => 'Carrots',
|
|
|
|
),
|
|
|
|
"Other" => array(
|
|
|
|
9 => 'Vegan'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $choices);
|
|
|
|
$field->setValue(3);
|
|
|
|
$result = trim($field->Field()->getValue());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-06-01 07:47:03 +02:00
|
|
|
$this->assertContains('<span class="readonly" id="test">Carrots</span>', $result);
|
|
|
|
$this->assertContains('<input type="hidden" name="test" value="3" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-06-01 07:47:03 +02:00
|
|
|
$field->setValue([3, 9]);
|
|
|
|
$result = trim($field->Field()->getValue());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-06-01 07:47:03 +02:00
|
|
|
$this->assertContains('<span class="readonly" id="test">Carrots, Vegan</span>', $result);
|
|
|
|
$this->assertContains('<input type="hidden" name="test" value="3, 9" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|