2011-09-14 10:58:17 +02:00
|
|
|
<?php
|
2013-05-10 14:01:39 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-09-14 10:58:17 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LookupFieldTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'LookupFieldTest.yml';
|
2011-09-14 10:58:17 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNullValueWithNumericArraySource() {
|
2011-09-14 10:58:17 +02:00
|
|
|
$source = array(1 => 'one', 2 => 'two', 3 => 'three');
|
|
|
|
$f = new LookupField('test', 'test', $source);
|
|
|
|
$f->setValue(null);
|
2013-10-17 23:28:17 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'<span class="readonly" id="test"><i>(none)</i></span><input type="hidden" name="test" value="" />',
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2011-09-14 10:58:17 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testStringValueWithNumericArraySource() {
|
2011-09-14 10:58:17 +02:00
|
|
|
$source = array(1 => 'one', 2 => 'two', 3 => 'three');
|
|
|
|
$f = new LookupField('test', 'test', $source);
|
|
|
|
$f->setValue(1);
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'<span class="readonly" id="test">one</span><input type="hidden" name="test" value="1" />',
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2011-09-14 10:58:17 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUnknownStringValueWithNumericArraySource() {
|
2011-09-14 10:58:17 +02:00
|
|
|
$source = array(1 => 'one', 2 => 'two', 3 => 'three');
|
|
|
|
$f = new LookupField('test', 'test', $source);
|
|
|
|
$f->setValue('<ins>w00t</ins>');
|
|
|
|
$f->dontEscape = true; // simulates CMSMain->compareversions()
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'<span class="readonly" id="test"><ins>w00t</ins></span><input type="hidden" name="test" value="" />',
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2011-09-14 10:58:17 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArrayValueWithAssociativeArraySource() {
|
2011-09-14 10:58:17 +02:00
|
|
|
// 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'));
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('<span class="readonly" id="test">one val, two val</span>'
|
2014-08-15 08:53:05 +02:00
|
|
|
. '<input type="hidden" name="test" value="one, two" />',
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2011-09-14 10:58:17 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArrayValueWithNumericArraySource() {
|
2011-09-14 10:58:17 +02:00
|
|
|
// 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(
|
2014-08-15 08:53:05 +02:00
|
|
|
'<span class="readonly" id="test">one, two</span><input type="hidden" name="test" value="1, 2" />',
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2011-09-14 10:58:17 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArrayValueWithSqlMapSource() {
|
2011-09-14 10:58:17 +02:00
|
|
|
$member1 = $this->objFromFixture('Member', 'member1');
|
|
|
|
$member2 = $this->objFromFixture('Member', 'member2');
|
|
|
|
$member3 = $this->objFromFixture('Member', 'member3');
|
2013-10-02 07:16:33 +02:00
|
|
|
|
2011-10-22 16:47:39 +02:00
|
|
|
$source = DataObject::get('Member');
|
2013-10-02 07:16:33 +02:00
|
|
|
$f = new LookupField('test', 'test', $source->map('ID', 'FirstName'));
|
2011-09-14 10:58:17 +02:00
|
|
|
$f->setValue(array($member1->ID, $member2->ID));
|
2013-10-02 07:16:33 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2012-09-26 23:34:00 +02:00
|
|
|
'<span class="readonly" id="test">member1, member2</span>'
|
2014-08-15 08:53:05 +02:00
|
|
|
. '<input type="hidden" name="test" value="%s, %s" />',
|
2011-09-14 10:58:17 +02:00
|
|
|
$member1->ID,
|
|
|
|
$member2->ID
|
|
|
|
),
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2011-09-14 10:58:17 +02:00
|
|
|
);
|
|
|
|
}
|
2013-05-10 14:01:39 +02:00
|
|
|
|
|
|
|
public function testWithMultiDimensionalSource() {
|
|
|
|
$choices = array(
|
|
|
|
"Non-vegetarian" => array(
|
|
|
|
0 => 'Carnivore',
|
|
|
|
),
|
|
|
|
"Vegetarian" => array(
|
2014-08-15 08:53:05 +02:00
|
|
|
3 => 'Carrots',
|
2013-05-10 14:01:39 +02:00
|
|
|
),
|
|
|
|
"Other" => array(
|
|
|
|
9 => 'Vegan'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$f = new LookupField('test', 'test', $choices);
|
|
|
|
$f->setValue(3);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'<span class="readonly" id="test">Carrots</span><input type="hidden" name="test" value="3" />',
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2013-05-10 14:01:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue(array(
|
|
|
|
3, 9
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'<span class="readonly" id="test">Carrots, Vegan</span><input type="hidden" name="test" value="3, 9" />',
|
2015-08-30 07:02:55 +02:00
|
|
|
trim($f->Field()->getValue())
|
2013-05-10 14:01:39 +02:00
|
|
|
);
|
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|