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()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$source = [1 => 'one', 2 => 'two', 3 => 'three'];
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue(null);
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test"><i>(none)</i></span>', $result);
|
|
|
|
$this->assertStringContainsString('<input type="hidden" name="test" value="" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testStringValueWithNumericArraySource()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$source = [1 => 'one', 2 => 'two', 3 => 'three'];
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue(1);
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test">one</span>', $result);
|
|
|
|
$this->assertStringContainsString('<input type="hidden" name="test" value="1" />', $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnknownStringValueWithNumericArraySource()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$source = [1 => 'one', 2 => 'two', 3 => 'three'];
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
|
|
|
$field->setValue('w00t');
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2018-06-01 07:47:03 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test">w00t</span>', $result);
|
|
|
|
$this->assertStringContainsString('<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
|
2020-04-20 19:58:09 +02:00
|
|
|
$source = ['one' => 'one val', 'two' => 'two val', 'three' => 'three val'];
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
2020-04-20 19:58:09 +02:00
|
|
|
$field->setValue(['one','two']);
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2018-06-01 07:47:03 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test">one val, two val</span>', $result);
|
|
|
|
$this->assertStringContainsString('<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
|
2020-04-20 19:58:09 +02:00
|
|
|
$source = [1 => 'one', 2 => 'two', 3 => 'three'];
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $source);
|
2020-04-20 19:58:09 +02:00
|
|
|
$field->setValue([1,2]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2018-06-01 07:47:03 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test">one, two</span>', $result);
|
|
|
|
$this->assertStringContainsString('<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'));
|
2020-04-20 19:58:09 +02:00
|
|
|
$field->setValue([$member1->ID, $member2->ID]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2018-06-01 07:47:03 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test">member1, member2</span>', $result);
|
|
|
|
$this->assertStringContainsString(sprintf(
|
2018-06-01 07:47:03 +02:00
|
|
|
'<input type="hidden" name="test" value="%s, %s" />',
|
|
|
|
$member1->ID,
|
|
|
|
$member2->ID
|
|
|
|
), $result);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithMultiDimensionalSource()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$choices = [
|
|
|
|
"Non-vegetarian" => [
|
2016-12-16 05:34:21 +01:00
|
|
|
0 => 'Carnivore',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
"Vegetarian" => [
|
2016-12-16 05:34:21 +01:00
|
|
|
3 => 'Carrots',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
"Other" => [
|
2016-12-16 05:34:21 +01:00
|
|
|
9 => 'Vegan'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-06-01 07:47:03 +02:00
|
|
|
$field = new LookupField('test', 'test', $choices);
|
|
|
|
$field->setValue(3);
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test">Carrots</span>', $result);
|
|
|
|
$this->assertStringContainsString('<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]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = trim($field->Field()->getValue() ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('<span class="readonly" id="test">Carrots, Vegan</span>', $result);
|
|
|
|
$this->assertStringContainsString('<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
|
|
|
}
|