Fixing #138
[akelos.git] / test / unit / lib / AkActionView / helpers / form_options_helper.php
blob4029253e7f08ed7bd72aa22d2d7c4804a1cb4ca7
1 <?php
3 require_once('_HelpersUnitTester.php');
4 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'form_options_helper.php');
7 class FormOptionsHelperTests extends HelpersUnitTester
9 function test_for_formOptionsHelper()
11 $FormOptionsHelper = & new FormOptionsHelper();
13 $this->assertEqual(trim(str_replace("\n",'',
14 $FormOptionsHelper->options_for_select(array('Admin','Moderator','Visitor','Demo'), 'Visitor'))),
15 trim(str_replace("\n",'',
16 '<option value="Admin">Admin</option>'.
17 '<option value="Moderator">Moderator</option>'.
18 '<option selected="selected" value="Visitor">Visitor</option>'.
19 '<option value="Demo">Demo</option>')));
21 $this->assertEqual(trim(str_replace("\n",'',
22 $FormOptionsHelper->options_for_select(array('Admin'=>'1','Moderator'=>'2','Visitor'=>'3','Demo'=>'4'), '3'))),
23 trim(str_replace("\n",'',
24 '<option value="1">Admin</option>'.
25 '<option value="2">Moderator</option>'.
26 '<option selected="selected" value="3">Visitor</option>'.
27 '<option value="4">Demo</option>')));
30 $Person =& new MockAkActiveRecord($this);
31 $Person->setReturnValue('get', 'Bermi', array('name'));
32 $Person->setReturnValue('get', '3', array('role'));
33 $Person->setReturnValue('get', '3', array('id'));
34 $Controller =& new MockAkActionController($this);
37 $AkFormHelperOptionsInstanceTag =& new AkFormHelperOptionsInstanceTag('person','role',&$FormOptionsHelper,null,&$Person);
39 $this->assertEqual($AkFormHelperOptionsInstanceTag->getValue(), '3');
41 $this->assertEqual(trim(str_replace("\n",'',
42 $AkFormHelperOptionsInstanceTag->to_select_tag(array('a','b')))),
43 '<select id="person_role" name="person[role]">'.
44 '<option value="a">a</option>'.
45 '<option value="b">b</option>'.
46 '</select>');
48 $Person1 =& new MockAkActiveRecord($this);
49 $Person1->setReturnValue('get', 'Admin', array('role'));
50 $Person1->setReturnValue('get', '1', array('id'));
51 $Person2 =& new MockAkActiveRecord($this);
52 $Person2->setReturnValue('get', 'Demo', array('role'));
53 $Person2->setReturnValue('get', '2', array('id'));
54 $Person3 =& new MockAkActiveRecord($this);
55 $Person3->setReturnValue('get', 'Visitor', array('role'));
56 $Person3->setReturnValue('get', '3', array('id'));
58 $collection = array(&$Person1,&$Person2,&$Person3);
59 $this->assertEqual(trim(str_replace("\n",'',
60 $AkFormHelperOptionsInstanceTag->to_collection_select_tag($collection, 'id', 'role',array('prompt'=>true)))),
61 '<select id="person_role" name="person[role]"><option value="1">Admin</option>'.
62 '<option value="2">Demo</option>'.
63 '<option selected="selected" value="3">Visitor</option>'.
64 '</select>');
66 ob_start();
67 $this->assertErrorPattern('/private methods/',$AkFormHelperOptionsInstanceTag->to_collection_select_tag($collection, '_id', 'role'));
68 ob_end_clean();
71 $Person =& new MockAkActiveRecord($this);
72 $AkFormHelperOptionsInstanceTag =& new AkFormHelperOptionsInstanceTag('person','role',&$FormOptionsHelper,null,&$Person);
74 $this->assertEqual(
75 $AkFormHelperOptionsInstanceTag->to_select_tag(array('a','b'),array(), array('prompt' => true)),
76 file_get_contents(AK_TEST_HELPERS_DIR.DS.'form_options_helper_to_select_prompt.txt')
79 $this->assertEqual(
80 $AkFormHelperOptionsInstanceTag->to_select_tag(array('a','b'), array(), array('include_blank' => true)),
81 file_get_contents(AK_TEST_HELPERS_DIR.DS.'form_options_helper_to_select_include_blank.txt')
84 $this->assertEqual(
85 $AkFormHelperOptionsInstanceTag->to_select_tag(array('a','b'), array(), array('include_blank' => true, 'prompt' => true)),
86 file_get_contents(AK_TEST_HELPERS_DIR.DS.'form_options_helper_to_select_include_blank_prompt.txt')
89 $Person =& new MockAkActiveRecord($this);
90 $Person->setReturnValue('get', 'USA', array('country'));
91 $AkFormHelperOptionsInstanceTag =& new AkFormHelperOptionsInstanceTag('person','country',&$FormOptionsHelper,null,&$Person);
93 $countrie_select = $AkFormHelperOptionsInstanceTag->to_country_select_tag(array('Spain'=>'ESP','United States'=>'USA'));
95 $this->assertTrue(strstr($countrie_select,'<select id="person_country" name="person[country]">'));
96 $this->assertTrue(strstr($countrie_select,'<option value="ESP">Spain</option>'));
97 $this->assertTrue(strstr($countrie_select,'<option selected="selected" value="USA">United States</option>'));
98 $this->assertTrue(strstr($countrie_select,'<option value="">-------------</option>'));
99 unset($countrie_select);
102 $Person =& new MockAkActiveRecord($this);
103 $Person->setReturnValue('get', array('(GMT+01:00) Europe/Madrid'=>'Europe/Madrid', '(GMT) Europe/London'=>'Europe/London'), array('timezone'));
104 $AkFormHelperOptionsInstanceTag =& new AkFormHelperOptionsInstanceTag('person','timezone',&$FormOptionsHelper,null,&$Person);
106 $timezone_select_html = $AkFormHelperOptionsInstanceTag->to_time_zone_select_tag(array('(GMT+01:00) Europe/Madrid'=>'Europe/Madrid'),array(),array('multiple'=>'multiple'));
107 $this->assertTrue(strstr($timezone_select_html,'<select id="person_timezone" multiple="multiple" name="person[timezone][]">'));
108 $this->assertTrue(strstr($timezone_select_html,'<option selected="selected" value="Europe/Madrid">(GMT+01:00) Europe/Madrid</option>'));
109 $this->assertTrue(strstr($timezone_select_html,'<option value="">-------------</option>'));
110 $this->assertTrue(strstr($timezone_select_html,'<option selected="selected" value="Europe/London">(GMT) Europe/London</option>'));
111 $this->assertFalse(strstr($timezone_select_html,'<option selected="selected" value="Europe/Dublin">(GMT) Europe/Dublin</option>'));
112 unset($timezone_select_html);
115 $Person =& new MockAkActiveRecord($this);
116 $Person->setReturnValue('get', 'Moderator', array('role'));
117 $FormOptionsHelper = & new FormOptionsHelper();
118 $FormOptionsHelper->addObject('person',$Person);
120 $this->assertEqual(trim(str_replace("\n",'',
121 $FormOptionsHelper->select('person', 'role', array('Admin','Moderator','Visitor'), array('onclick'=>'alert(\'Hola\')'), array('multiple'=>'multiple')))),
122 '<select id="person_role" multiple="multiple" name="person[role][]"><option onclick="alert(\'Hola\')" value="Admin">Admin</option>'.
123 '<option onclick="alert(\'Hola\')" selected="selected" value="Moderator">Moderator</option>'.
124 '<option onclick="alert(\'Hola\')" value="Visitor">Visitor</option>'.
125 '</select>');
128 $Category_1 =& new MockAkActiveRecord($this);
129 $Category_1->setReturnValue('get', 'Tech', array('description'));
130 $Category_1->setReturnValue('get', '23', array('id'));
131 $Category_2 =& new MockAkActiveRecord($this);
132 $Category_2->setReturnValue('get', 'News', array('description'));
133 $Category_2->setReturnValue('get', '3', array('id'));
134 $Category_3 =& new MockAkActiveRecord($this);
135 $Category_3->setReturnValue('get', 'Education', array('description'));
136 $Category_3->setReturnValue('get', '6', array('id'));
138 $Person->setReturnValue('get', '6', array('category'));
140 $Person->categories = array(&$Category_1,&$Category_2,&$Category_3);
142 $this->assertEqual(trim(str_replace("\n",'',
143 $FormOptionsHelper->collection_select('person', 'category', $Person->categories, 'id', 'description'))),
144 '<select id="person_category" name="person[category]"><option value="23">Tech</option>'.
145 '<option value="3">News</option>'.
146 '<option selected="selected" value="6">Education</option>'.
147 '</select>');
149 $this->assertEqual(trim(str_replace("\n",'',
150 $FormOptionsHelper->collection_select('person', 'category', $Person->categories, 'id', 'description',array('onclick'=>'alert(\'Coleccion\')'), array('multiple'=>'multiple')))),
151 '<select id="person_category" multiple="multiple" name="person[category][]"><option onclick="alert(\'Coleccion\')" value="23">Tech</option>'.
152 '<option onclick="alert(\'Coleccion\')" value="3">News</option>'.
153 '<option onclick="alert(\'Coleccion\')" selected="selected" value="6">Education</option>'.
154 '</select>');
156 $Person->setReturnValue('get', array('ESP','FRA'), array('country'));
158 $this->assertTrue(strstr(trim(str_replace("\n",'',
159 $FormOptionsHelper->country_select('person', 'country',
160 array('Spain'=>'ESP','Ireland'=>'IRL'),
161 array('onclick'=>'alert(this.value)'),
162 array('onblur'=>'alert(this.value)','multiple'=>'multiple')))),
163 '<select id="person_country" multiple="multiple" name="person[country][]" onblur="alert(this.value)">'.
164 '<option onclick="alert(this.value)" selected="selected" value="ESP">Spain</option>'.
165 '<option onclick="alert(this.value)" value="IRL">Ireland</option>'.
166 '<option value="">-------------</option>'));
168 $this->assertTrue(strstr(trim(str_replace("\n",'',
169 $FormOptionsHelper->country_select('person', 'country',
170 array('Spain'=>'ESP','Ireland'=>'IRL'),
171 array('onclick'=>'alert(this.value)'),
172 array('onblur'=>'alert(this.value)','multiple'=>'multiple')))),
173 '<option onclick="alert(this.value)" selected="selected" value="FRA">France</option>'));
176 $this->assertTrue(strstr(trim(str_replace("\n",'',
177 $FormOptionsHelper->time_zone_select(
178 'person',
179 'timezone',
180 array('(GMT+01:00) Europe/Madrid'=>'Europe/Madrid'),
181 array('prompt'=>true),
182 array('style'=>'border:5px solid red;')))),
183 '<select id="person_timezone" name="person[timezone]" style="border:5px solid red;">'.
184 '<option value="">Please select</option>'.
185 '<option value="Europe/Madrid">(GMT+01:00) Europe/Madrid</option>'.
186 '<option value="">-------------</option>'
189 $Person->setReturnValue('get', array('(GMT+01:00) Europe/Budapest'=>'Europe/Budapest'), array('timezone'));
190 $this->assertTrue(strstr(trim(str_replace("\n",'',
191 $FormOptionsHelper->time_zone_select(
192 'person',
193 'timezone',
194 array('(GMT+01:00) Madrid'=>3600),
195 array('prompt'=>true),
196 array('style'=>'border:5px solid red;')))),
197 '<option selected="selected" value="Europe/Budapest">(GMT+01:00) Europe/Budapest</option>'
203 $this->assertEqual(
204 $FormOptionsHelper->options_for_select(array('VISA', 'MasterCard'), 'MasterCard'),
205 '<option value="VISA">VISA</option>'."\n".
206 '<option selected="selected" value="MasterCard">MasterCard</option>'."\n");
208 $this->assertEqual(
209 $FormOptionsHelper->options_for_select(array('Dollar'=>'$', 'Kroner'=>'DKK')),
210 '<option value="$">Dollar</option>'."\n".
211 '<option value="DKK">Kroner</option>'."\n");
213 $this->assertEqual(
214 $FormOptionsHelper->options_for_select(array('Basic'=>'$20','Plus'=>'$40'), '$40'),
215 '<option value="$20">Basic</option>'."\n".
216 '<option selected="selected" value="$40">Plus</option>'."\n");
218 $this->assertEqual(
219 $FormOptionsHelper->options_for_select(array('VISA','MasterCard','Discover'), array('VISA','Discover')),
220 '<option selected="selected" value="VISA">VISA</option>'."\n".
221 '<option value="MasterCard">MasterCard</option>'."\n".
222 '<option selected="selected" value="Discover">Discover</option>'."\n");
226 $Project =& new MockAkActiveRecord($this);
228 $Person =& new MockAkActiveRecord($this);
229 $Person->setReturnValue('get', '100', array('id'));
230 $Person->setReturnValue('get', 'Bermi', array('name'));
232 $Person2 =& new MockAkActiveRecord($this);
233 $Person2->setReturnValue('get', '200', array('id'));
234 $Person2->setReturnValue('get', 'Hilario', array('name'));
236 $Person3 =& new MockAkActiveRecord($this);
237 $Person3->setReturnValue('get', '250', array('id'));
238 $Person3->setReturnValue('get', 'Salavert', array('name'));
240 $Project->People = array(&$Person, &$Person2, &$Person3);
242 $FormOptionsHelper = & new FormOptionsHelper();
244 $this->assertEqual(
245 $FormOptionsHelper->options_from_collection_for_select($Project->People,'id','name'),
246 '<option value="100">Bermi</option>'."\n".
247 '<option value="200">Hilario</option>'."\n".
248 '<option value="250">Salavert</option>'."\n");
250 $this->assertEqual(
251 $FormOptionsHelper->options_from_collection_for_select($Project->People,'id','name',array(100,200)),
252 '<option selected="selected" value="100">Bermi</option>'."\n".
253 '<option selected="selected" value="200">Hilario</option>'."\n".
254 '<option value="250">Salavert</option>'."\n");
257 $AkTestContinentsGroup = array(
258 new AkTestContinent(
259 'Africa', array(new AkTestCountry('EGP','Egipt'), new AkTestCountry('RWD','Rwanda'))
261 new AkTestContinent(
262 'Asia', array(new AkTestCountry('ZHN','China'), new AkTestCountry('IND','India'), new AkTestCountry('JPN','Japan'))
266 $this->assertEqual(str_replace("\n",'',
267 $FormOptionsHelper->option_groups_from_collection_for_select($AkTestContinentsGroup, 'getCountriesCollection', 'getContinentName', 'getCountryId', 'getCountryName', 'JPN')),
268 '<optgroup label="Africa">'.
269 '<option value="EGP">Egipt</option>'.
270 '<option value="RWD">Rwanda</option>'.
271 '</optgroup>'.
273 '<optgroup label="Asia">'.
274 '<option value="ZHN">China</option>'.
275 '<option value="IND">India</option>'.
276 '<option selected="selected" value="JPN">Japan</option>'.
277 '</optgroup>');
280 * @todo add tests for AkFormOptionsHelperBuilder
284 function test_numerical_indexes_for_select()
286 $Person =& new MockAkActiveRecord($this);
287 $Person->setReturnValue('get', '2', array('role'));
288 $FormOptionsHelper = & new FormOptionsHelper();
289 $FormOptionsHelper->addObject('person',$Person);
291 $this->assertEqual(trim(str_replace("\n",'',
292 $FormOptionsHelper->select('person', 'role', array('Admin'=>1,'Moderator'=>2,'Visitor'=>3)))),
293 '<select id="person_role" name="person[role]"><option value="1">Admin</option>'.
294 '<option selected="selected" value="2">Moderator</option>'.
295 '<option value="3">Visitor</option>'.
296 '</select>');
298 $this->assertEqual(trim(str_replace("\n",'',
299 $FormOptionsHelper->select('person', 'role', array('Admin'=>0,'Moderator'=>1,'Visitor'=>2)))),
300 '<select id="person_role" name="person[role]"><option value="0">Admin</option>'.
301 '<option value="1">Moderator</option>'.
302 '<option selected="selected" value="2">Visitor</option>'.
303 '</select>');
308 ak_test('FormOptionsHelperTests', true);