'one', 2 => 'two', 3 => 'three'];
$field = new LookupField('test', 'test', $source);
$field->setValue(null);
$result = trim($field->Field()->getValue() ?? '');
$this->assertStringContainsString('(none)', $result);
$this->assertStringContainsString('', $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->assertStringContainsString('one', $result);
$this->assertStringContainsString('', $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->assertStringContainsString('w00t', $result);
$this->assertStringContainsString('', $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->assertStringContainsString('one val, two val', $result);
$this->assertStringContainsString('', $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->assertStringContainsString('one, two', $result);
$this->assertStringContainsString('', $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->assertStringContainsString('member1, member2', $result);
$this->assertStringContainsString(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->assertStringContainsString('Carrots', $result);
$this->assertStringContainsString('', $result);
$field->setValue([3, 9]);
$result = trim($field->Field()->getValue() ?? '');
$this->assertStringContainsString('Carrots, Vegan', $result);
$this->assertStringContainsString('', $result);
}
}