'one', 2 => 'two', 3 => 'three'); $f = new LookupField('test', 'test', $source); $f->setValue(null); $this->assertEquals( '(none)', trim($f->Field()->getValue()) ); } public function testStringValueWithNumericArraySource() { $source = array(1 => 'one', 2 => 'two', 3 => 'three'); $f = new LookupField('test', 'test', $source); $f->setValue(1); $this->assertEquals( 'one', trim($f->Field()->getValue()) ); } public function testUnknownStringValueWithNumericArraySource() { $source = array(1 => 'one', 2 => 'two', 3 => 'three'); $f = new LookupField('test', 'test', $source); $f->setValue('w00t'); $f->dontEscape = true; // simulates CMSMain->compareversions() $this->assertEquals( 'w00t', trim($f->Field()->getValue()) ); } 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'); $f = new LookupField('test', 'test', $source); $f->setValue(array('one','two')); $this->assertEquals('one val, two val' . '', trim($f->Field()->getValue()) ); } public function testArrayValueWithNumericArraySource() { // Array values (= multiple selections) might be set e.g. from ListboxField $source = array(1 => 'one', 2 => 'two', 3 => 'three'); $f = new LookupField('test', 'test', $source); $f->setValue(array(1,2)); $this->assertEquals( 'one, two', trim($f->Field()->getValue()) ); } public function testArrayValueWithSqlMapSource() { $member1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'member1'); $member2 = $this->objFromFixture('SilverStripe\\Security\\Member', 'member2'); $member3 = $this->objFromFixture('SilverStripe\\Security\\Member', 'member3'); $source = DataObject::get('SilverStripe\\Security\\Member'); $f = new LookupField('test', 'test', $source->map('ID', 'FirstName')); $f->setValue(array($member1->ID, $member2->ID)); $this->assertEquals( sprintf( 'member1, member2' . '', $member1->ID, $member2->ID ), trim($f->Field()->getValue()) ); } public function testWithMultiDimensionalSource() { $choices = array( "Non-vegetarian" => array( 0 => 'Carnivore', ), "Vegetarian" => array( 3 => 'Carrots', ), "Other" => array( 9 => 'Vegan' ) ); $f = new LookupField('test', 'test', $choices); $f->setValue(3); $this->assertEquals( 'Carrots', trim($f->Field()->getValue()) ); $f->setValue(array( 3, 9 )); $this->assertEquals( 'Carrots, Vegan', trim($f->Field()->getValue()) ); } }