2011-09-14 10:58:17 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-09-14 10:58:17 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LookupFieldTest extends SapphireTest {
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
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);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<span class="readonly" id="test"><i>(none)</i></span><input type="hidden" name="test" value="" />',
|
|
|
|
$f->Field()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
'<span class="readonly" id="test">one</span><input type="hidden" name="test" value="1" />',
|
|
|
|
$f->Field()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
'<span class="readonly" id="test"><ins>w00t</ins></span><input type="hidden" name="test" value="" />',
|
|
|
|
$f->Field()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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>'
|
|
|
|
. '<input type="hidden" name="test" value="one, two" />',
|
2011-09-14 10:58:17 +02:00
|
|
|
$f->Field()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
'<span class="readonly" id="test">one, two</span><input type="hidden" name="test" value="1, 2" />',
|
|
|
|
$f->Field()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
2011-10-22 16:47:39 +02:00
|
|
|
$source = DataObject::get('Member');
|
|
|
|
$f = new LookupField('test', 'test', $source->map());
|
2011-09-14 10:58:17 +02:00
|
|
|
$f->setValue(array($member1->ID, $member2->ID));
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2012-09-26 23:34:00 +02:00
|
|
|
'<span class="readonly" id="test">member1, member2</span>'
|
|
|
|
. '<input type="hidden" name="test" value="%s, %s" />',
|
2011-09-14 10:58:17 +02:00
|
|
|
$member1->ID,
|
|
|
|
$member2->ID
|
|
|
|
),
|
|
|
|
$f->Field()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|