'one', 2 => 'two', 3 => 'three'];
$field = new LookupField('test', 'test', $source);
$field->setValue(null);
$result = trim($field->Field()->getValue());
$this->assertContains('(none)', $result);
$this->assertContains('', $result);
}
public function testStringValueWithNumericArraySource()
{
$source = [1 => 'one', 2 => 'two', 3 => 'three'];
$field = new LookupField('test', 'test', $source);
$field->setValue(1);
$result = trim($field->Field()->getValue());
$this->assertContains('one', $result);
$this->assertContains('', $result);
}
public function testUnknownStringValueWithNumericArraySource()
{
$source = [1 => 'one', 2 => 'two', 3 => 'three'];
$field = new LookupField('test', 'test', $source);
$field->setValue('w00t');
$result = trim($field->Field()->getValue());
$this->assertContains('w00t', $result);
$this->assertContains('', $result);
}
public function testArrayValueWithAssociativeArraySource()
{
// Array values (= multiple selections) might be set e.g. from ListboxField
$source = ['one' => 'one val', 'two' => 'two val', 'three' => 'three val'];
$field = new LookupField('test', 'test', $source);
$field->setValue(['one','two']);
$result = trim($field->Field()->getValue());
$this->assertContains('one val, two val', $result);
$this->assertContains('', $result);
}
public function testArrayValueWithNumericArraySource()
{
// Array values (= multiple selections) might be set e.g. from ListboxField
$source = [1 => 'one', 2 => 'two', 3 => 'three'];
$field = new LookupField('test', 'test', $source);
$field->setValue([1,2]);
$result = trim($field->Field()->getValue());
$this->assertContains('one, two', $result);
$this->assertContains('', $result);
}
public function testArrayValueWithSqlMapSource()
{
$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([$member1->ID, $member2->ID]);
$result = trim($field->Field()->getValue());
$this->assertContains('member1, member2', $result);
$this->assertContains(sprintf(
'',
$member1->ID,
$member2->ID
), $result);
}
public function testWithMultiDimensionalSource()
{
$choices = [
"Non-vegetarian" => [
0 => 'Carnivore',
],
"Vegetarian" => [
3 => 'Carrots',
],
"Other" => [
9 => 'Vegan'
]
];
$field = new LookupField('test', 'test', $choices);
$field->setValue(3);
$result = trim($field->Field()->getValue());
$this->assertContains('Carrots', $result);
$this->assertContains('', $result);
$field->setValue([3, 9]);
$result = trim($field->Field()->getValue());
$this->assertContains('Carrots, Vegan', $result);
$this->assertContains('', $result);
}
}