Blame view

framework/tests/forms/LookupFieldTest.php 3.33 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  <?php
  
  /**
   * @package framework
   * @subpackage tests
   */
  
  class LookupFieldTest extends SapphireTest {
  	
  	protected static $fixture_file = 'LookupFieldTest.yml';
  
  	public function testNullValueWithNumericArraySource() {
  		$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()
  		);
  	}
  
  	public function testStringValueWithNumericArraySource() {
  		$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()
  		);
  	}
  	
  	public function testUnknownStringValueWithNumericArraySource() {
  		$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()
  		);
  	}
  
  	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('<span class="readonly" id="test">one val, two val</span>'
  			. '<input type="hidden" name="test" value="one, two" />', 
  			$f->Field()
  		);
  	}
  	
  	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(
  			'<span class="readonly" id="test">one, two</span><input type="hidden" name="test" value="1, 2" />', 
  			$f->Field()
  		);
  	}
  	
  	public function testArrayValueWithSqlMapSource() {
  		$member1 = $this->objFromFixture('Member', 'member1');
  		$member2 = $this->objFromFixture('Member', 'member2');
  		$member3 = $this->objFromFixture('Member', 'member3');
  
  		$source = DataObject::get('Member');
  		$f = new LookupField('test', 'test', $source->map('ID', 'FirstName'));
  		$f->setValue(array($member1->ID, $member2->ID));
  
  		$this->assertEquals(
  			sprintf(
  				'<span class="readonly" id="test">member1, member2</span>'
  					. '<input type="hidden" name="test" value="%s, %s" />', 
  				$member1->ID,
  				$member2->ID
  			),
  			$f->Field()
  		);
  	}
  
  	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(
  			'<span class="readonly" id="test">Carrots</span><input type="hidden" name="test" value="3" />', 
  			$f->Field()
  		);
  
  		$f->setValue(array(
  			3, 9
  		));
  
  		$this->assertEquals(
  			'<span class="readonly" id="test">Carrots, Vegan</span><input type="hidden" name="test" value="3, 9" />', 
  			$f->Field()
  		);
  	}
  }