3 require_once('_HelpersUnitTester.php');
4 require_once(AK_LIB_DIR
.DS
.'AkActionView'.DS
.'helpers'.DS
.'form_helper.php');
7 class FormHelperTests
extends HelpersUnitTester
11 $this->test_value
= "Akelos";
12 $this->controller
= &new MockAkActionController($this);
13 $this->controller
->setReturnValue('urlFor', '/url/for/test');
14 $this->active_record
= &new MockAkActiveRecord($this);
15 $this->active_record
->setReturnValue('get', $this->test_value
);
17 $this->mock
= new stdClass();
18 $this->mock
->_controller
->person
=& $this->active_record
;
19 $this->ak_form_helper_instance_tag
=& new AkFormHelperInstanceTag('person', 'name', $this->mock
);
22 function test_add_default_name_and_id()
25 $this->ak_form_helper_instance_tag
->add_default_name_and_id($options);
26 $this->assertEqual($options,array('name'=>'person[name]','id'=>'person_name'));
28 $options = array('index'=>3);
29 $this->ak_form_helper_instance_tag
->add_default_name_and_id($options);
30 $this->assertEqual($options,array('name'=>'person[3][name]','id'=>'person_3_name'));
33 function test_get_object()
35 $this->assertReference($this->ak_form_helper_instance_tag
->getObject(), $this->active_record
);
38 function test_get_value()
40 $this->assertEqual($this->ak_form_helper_instance_tag
->getValue(), $this->test_value
);
43 function test_value_before_type_cast()
45 $this->assertEqual($this->ak_form_helper_instance_tag
->value_before_type_cast(), $this->test_value
);
46 $this->active_record
->name_before_type_cast
= 'test_akelos';
47 $this->assertEqual($this->ak_form_helper_instance_tag
->value_before_type_cast(), 'test_akelos');
50 function test_to_input_field_tag()
52 $this->assertEqual($this->ak_form_helper_instance_tag
->to_input_field_tag('text'), '<input id="person_name" name="person[name]" size="30" type="text" value="'.$this->test_value
.'" />');
53 $this->assertEqual($this->ak_form_helper_instance_tag
->to_input_field_tag('hidden'), '<input id="person_name" name="person[name]" type="hidden" value="'.$this->test_value
.'" />');
54 $this->assertEqual($this->ak_form_helper_instance_tag
->to_input_field_tag('file'), '<input id="person_name" name="person[name]" size="30" type="file" />');
57 function test_to_radio_button_tag()
59 $this->assertEqual($this->ak_form_helper_instance_tag
->to_radio_button_tag('Bermi'), '<input id="person_name_bermi" name="person[name]" type="radio" value="Bermi" />');
60 $this->assertEqual($this->ak_form_helper_instance_tag
->to_radio_button_tag('Hilario'), '<input id="person_name_hilario" name="person[name]" type="radio" value="Hilario" />');
63 function test_to_text_area_tag()
65 $this->active_record
->name_before_type_cast
= 'Something "NEW"';
68 $this->ak_form_helper_instance_tag
->to_text_area_tag(array('class'=>'wysiwyg')),
69 '<textarea class="wysiwyg" cols="40" id="person_name" name="person[name]" rows="20">Something "NEW"</textarea>'
73 function test_to_check_box_tag()
75 $this->assertEqual($this->ak_form_helper_instance_tag
->to_check_box_tag(array(),'Bermi'),'<input name="person[name]" type="hidden" value="0" /><input id="person_name" name="person[name]" type="checkbox" value="Bermi" />');
76 $this->assertEqual($this->ak_form_helper_instance_tag
->to_check_box_tag(array(),'si','no'),'<input name="person[name]" type="hidden" value="no" /><input id="person_name" name="person[name]" type="checkbox" value="si" />');
79 function test_to_boolean_select_tag()
81 $this->assertEqual($this->ak_form_helper_instance_tag
->to_boolean_select_tag(),'<select id="person_name" name="person[name]"><option value="false">False</option><option value="true" selected>True</option></select>');
82 $this->assertEqual($this->ak_form_helper_instance_tag
->to_boolean_select_tag(),'<select id="person_name" name="person[name]"><option value="false">False</option><option value="true" selected>True</option></select>');
85 function test_to_content_tag()
87 $this->assertEqual($this->ak_form_helper_instance_tag
->to_content_tag('h1'),'<h1>'.$this->test_value
.'</h1>');
90 function test_to_date_tag()
92 $active_record = &new MockAkActiveRecord($this);
93 $active_record->setReturnValue('get', '1978-06-16');
94 $ak_form_helper_instance_tag =& new AkFormHelperInstanceTag('person', 'join_date', $active_record, null, $active_record);
95 $this->assertEqual($ak_form_helper_instance_tag->to_date_tag(), file_get_contents(AK_TEST_HELPERS_DIR
.DS
.'form_helper_to_date_tag.txt'));
98 function test_to_date_select_tag()
100 $active_record = &new MockAkActiveRecord($this);
101 $active_record->setReturnValue('get', '1978-06-16');
102 $ak_form_helper_instance_tag =& new AkFormHelperInstanceTag('person', 'join_date', $active_record, null, $active_record);
103 $this->assertEqual($ak_form_helper_instance_tag->to_date_select_tag(), file_get_contents(AK_TEST_HELPERS_DIR
.DS
.'form_helper_to_date_select_tag.txt'));
106 function test_to_datetime_select_tag()
108 $active_record = &new MockAkActiveRecord($this);
109 $active_record->setReturnValue('get', '1978-06-16');
110 $ak_form_helper_instance_tag =& new AkFormHelperInstanceTag('person', 'join_date', $active_record, null, $active_record);
111 $this->assertEqual($ak_form_helper_instance_tag->to_datetime_select_tag(), file_get_contents(AK_TEST_HELPERS_DIR
.DS
.'form_helper_to_datetime_select_tag.txt'));
114 function test_tag_name()
116 $this->assertEqual($this->ak_form_helper_instance_tag
->tag_name(),'person[name]');
119 function test_tag_name_with_index()
121 $this->assertEqual($this->ak_form_helper_instance_tag
->tag_name_with_index(42),'person[42][name]');
124 function test_tag_id()
126 $this->assertEqual($this->ak_form_helper_instance_tag
->tag_id(),'person_name');
129 function test_tag_id_with_index()
131 $this->assertEqual($this->ak_form_helper_instance_tag
->tag_id_with_index(42),'person_42_name');
134 function test_for_form_helpers()
137 $controller = &new MockAkActionController($this);
138 $controller->setReturnValue('urlFor', '/url/for/test');
139 $controller->form_tag_helper
= new FormTagHelper();
140 $controller->form_tag_helper
->setController($controller);
142 $person = &new MockAkActiveRecord($this);
143 $person->setReturnValue('get', 'Bermi', array('name'));
145 $task = &new MockAkActiveRecord($this);
146 $task->setReturnValue('get', 'Do the testing');
148 $form_helper = new FormHelper(array('person' => &$person));
149 $form_helper->setController($controller);
151 $this->assertReference($form_helper->getObject('person'), $person);
154 $f = $form_helper->form_for('person', $person, array('url' => array('action' => 'update')));
155 $this->assertEqual(ob_get_clean(),'<form action="/url/for/test" method="post">');
158 $form_helper->text_field('task', 'description'),
159 '<input id="task_description" name="task[description]" size="30" type="text" />'
163 $form_helper->text_field('task', 'description', array('object' => &$task)),
164 '<input id="task_description" name="task[description]" size="30" type="text" value="Do the testing" />'
168 $f->text_field('person', 'name'),
169 '<input id="person_name" name="person[name]" size="30" type="text" value="Bermi" />'
172 $person->setReturnValue('get', 'Alicia', array('first_name'));
175 $f->text_field('person', 'first_name', array('size'=>80)),
176 '<input id="person_first_name" name="person[first_name]" size="80" type="text" value="Alicia" />'
180 $form_helper->password_field('person','password'),
181 '<input id="person_password" name="person[password]" size="30" type="password" />'
185 $form_helper->file_field('person','photo'),
186 '<input id="person_photo" name="person[photo]" size="30" type="file" />'
190 $form_helper->hidden_field('person','referer'),
191 '<input id="person_referer" name="person[referer]" type="hidden" />'
195 $form_helper->text_area('person','notes'),
196 '<textarea cols="40" id="person_notes" name="person[notes]" rows="20"></textarea>'
200 $form_helper->text_field('person','name'),
201 '<input id="person_name" name="person[name]" size="30" type="text" />'
205 $person->setReturnValue('get', '1234', array('password'));
206 $person->setReturnValue('get', 'no_value_on_file_types', array('photo'));
207 $person->setReturnValue('get', 'http://www.example.com', array('referer'));
208 $person->setReturnValue('get', 'Check this "NOTES"', array('notes'));
211 $f->password_field('person','password'),
212 '<input id="person_password" name="person[password]" size="30" type="password" value="1234" />'
216 $f->file_field('person','photo'),
217 '<input id="person_photo" name="person[photo]" size="30" type="file" />'
221 $f->hidden_field('person','referer'),
222 '<input id="person_referer" name="person[referer]" type="hidden" value="http://www.example.com" />'
226 $f->text_area('person','notes'),
227 '<textarea cols="40" id="person_notes" name="person[notes]" rows="20">Check this "NOTES"</textarea>'
231 $f->text_field('person','name'),
232 '<input id="person_name" name="person[name]" size="30" type="text" value="Bermi" />'
235 $person->setReturnValue('get', 1, array('validate'));
238 $f->check_box("post", "validate"),
239 '<input name="post[validate]" type="hidden" value="0" />'.
240 '<input checked="checked" id="post_validate" name="post[validate]" type="checkbox" value="1" />'
244 $f->radio_button('post', 'validate','si'),
245 '<input id="post_validate_si" name="post[validate]" type="radio" value="si" />'
249 $f->radio_button('post', 'validate','1'),
250 '<input checked="checked" id="post_validate_1" name="post[validate]" type="radio" value="1" />'
255 ak_test('FormHelperTests', true);