| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 10 require_once('limb/wact/tests/cases/WactTemplateTestCase.class.php'); 11 require_once('limb/wact/src/components/form/form.inc.php'); 12 13 class WactFormTagTest extends WactTemplateTestCase 14 { 15 function testHasNoErrors() 16 { 17 $form = new WactFormComponent('my_id'); 18 $this->assertFalse($form->hasErrors()); 19 } 20 21 function testHasErrors() 22 { 23 $error_list = new WactFormErrorList(); 24 $errorFields = array('x'=>'Input1', 'z'=>'Input3'); 25 $error_list->addError('message', $errorFields); 26 27 $form = new WactFormComponent('my_id'); 28 $form->setErrors($error_list); 29 $this->assertTrue($form->hasErrors()); 30 } 31 32 function testGetFieldErrorsForField() 33 { 34 $error_list = new WactFormErrorList(); 35 $error_list->addError('message1', array('x'=>'Input1')); 36 $error_list->addError('message2', array('x'=>'Input1', 'z'=>'Input2')); 37 38 $form = new WactFormComponent('my_id'); 39 $form->setErrors($error_list); 40 41 $errors = $form->getFieldErrorsDataSet('Input1'); 42 $errors->rewind(); 43 44 $error = $errors->current(); 45 $this->assertEqual($error['message'], 'message1'); 46 $errors->next(); 47 $error = $errors->current(); 48 $this->assertEqual($error['message'], 'message2'); 49 50 $errors = $form->getFieldErrorsDataSet('Input2'); 51 $errors->rewind(); 52 $error = $errors->current(); 53 $this->assertEqual($error['message'], 'message2'); 54 $errors->next(); 55 $this->assertFalse($errors->valid()); 56 } 57 58 function testPrepare() 59 { 60 $form = new WactFormComponent('my_id'); 61 $form->prepare(); 62 $this->assertIsA($form->getDataSource(), 'WactArrayObject'); 63 } 64 65 /** 66 * Should test registerFilter, prepare and registerDataSource but lazy right now... 67 */ 68 69 function testFindLabel() 70 { 71 $template = '<form id="testForm" runat="server"> 72 <label id="testLabel" for="testId" runat="server">A label</label> 73 </form>'; 74 $this->registerTestingTemplate('/tags/form/form/findlabel.html', $template); 75 76 $page = $this->initTemplate('/tags/form/form/findlabel.html'); 77 78 $form = $page->getChild('testForm'); 79 $label = $page->getChild('testLabel'); 80 81 $this->assertReference($form->findLabel('testId', $form), $label); 82 } 83 84 function testFormTagGeneratesLocalPHPVariableReferencingToForm() 85 { 86 $template = '<form id="testForm" runat="server">'. 87 '<?php echo $testForm->get("test_value"); ?>'. 88 '</form>'; 89 $this->registerTestingTemplate('/tags/form/form/form_tag_generates_php_variable.html', $template); 90 91 $page = $this->initTemplate('/tags/form/form/form_tag_generates_php_variable.html'); 92 93 $form = $page->getChild('testForm'); 94 $form->registerDatasource(array('test_value' => "my_value")); 95 96 $expected = '<form id="testForm">my_value</form>'; 97 $this->assertEqual($page->capture(), $expected); 98 } 99 100 function testFindLabelNotFound() 101 { 102 $template = '<form id="testForm" runat="server"> 103 <label id="testLabel" for="testId" runat="server">A label</label> 104 </form>'; 105 $this->registerTestingTemplate('/tags/form/form/findlabelnotfound.html', $template); 106 107 $page = $this->initTemplate('/tags/form/form/findlabelnotfound.html'); 108 109 $form = $page->getChild('testForm'); 110 111 $this->assertFalse($form->findLabel('foo',$form)); 112 } 113 114 function testSetErrorsReference() 115 { 116 $error_list = new WactFormErrorList(); 117 $form = new WactFormComponent('my_id'); 118 $form->setErrors($error_list); 119 $this->assertReference($error_list, $form->getErrorsDataSet()); 120 } 121 122 function testSetErrors() 123 { 124 $error_list = new WactFormErrorList(); 125 $errorFields = array( 126 'x'=>'Input1', 127 'z'=>'Input3' 128 ); 129 130 $error_list->addError('message', $errorFields); 131 132 $template = '<form id="testForm" runat="server"> 133 <label id="Label1" for="Input1" class="Normal" errorclass="Error" runat="server">A label</label> 134 <input id="Input1" type="text" runat="server"> 135 <label id="Label2" for="Input2" class="Normal" errorclass="Error" runat="server">A label</label> 136 <input id="Input2" type="text" runat="server"> 137 <label id="Label3" for="Input3" class="Normal" errorclass="Error" runat="server">A label</label> 138 <input name="Input3" type="text" runat="server"> 139 </form>'; 140 141 $this->registerTestingTemplate('/tags/form/form/seterrors.html', $template); 142 143 $page = $this->initTemplate('/tags/form/form/seterrors.html'); 144 145 $form = $page->getChild('testForm'); 146 147 $form->setErrors($error_list); 148 149 $label1 = $page->getChild('Label1'); 150 $this->assertEqual($label1->getAttribute('class'),'Error'); 151 $Input1 = $page->getChild('Input1'); 152 $this->assertTrue($Input1->hasErrors()); 153 154 $label2 = $page->getChild('Label2'); 155 $this->assertEqual($label2->getAttribute('class'),'Normal'); 156 $Input2 = $page->getChild('Input2'); 157 $this->assertFalse($Input2->hasErrors()); 158 159 $label3 = $page->getChild('Label3'); 160 $this->assertEqual($label3->getAttribute('class'),'Normal'); 161 $Input3 = $page->getChild('Input3'); 162 $this->assertTrue($Input3->hasErrors()); 163 } 164 165 function testSetErrorsTricky() 166 { 167 $error_list = new WactFormErrorList(); 168 $errorFields = array( 169 'a' => 'InputText', 170 'b' => 'Select1', 171 'c' => 'Select2', 172 'd' => 'Select3', 173 'e' => 'Select4', 174 'f' => 'InputCheckbox2', 175 ); 176 $error_list->addError('message',$errorFields); 177 178 $template = '<form id="testForm" runat="server"> 179 180 <label id="Label1" for="InputText" class="Normal" errorclass="Error" runat="server">A label</label> 181 <input id="InputText" type="text" runat="server"/> 182 183 <label id="Label2" for="Select1" class="Normal" errorclass="Error" runat="server">A label</label> 184 <select id="Select1" name="mySelect1" type="text" runat="server"></select> 185 186 <label id="Label3" for="Select2" class="Normal" errorclass="Error" runat="server">A label</label> 187 <select name="Select2" type="text" runat="server"></select> 188 189 <label id="Label4" for="Select3" class="Normal" errorclass="Error" runat="server">A label</label> 190 <select id="Select3" name="Test[]" type="text" runat="server" multiple="true"></select> 191 192 <label id="Label5" for="Select4" class="Normal" errorclass="Error" runat="server">A label</label> 193 <select name="Select4[]" type="text" runat="server" multiple="true"></select> 194 195 <label id="Label6" for="InputCheckbox1" class="Normal" errorclass="Error" runat="server">A label</label> 196 <label id="Label7" for="InputCheckbox2" class="Normal" errorclass="Error" runat="server">A label</label> 197 <label id="Label8" for="InputCheckbox3" class="Normal" errorclass="Error" runat="server">A label</label> 198 <input id="InputCheckbox1" type="checkbox" name="test" value="x" checked="true" runat="server"/> 199 <input id="InputCheckbox2" type="checkbox" name="test" value="y" runat="server"/> 200 <input id="InputCheckbox3" type="checkbox" name="test" value="z" runat="server"/> 201 202 </form>'; 203 204 $this->registerTestingTemplate('/tags/form/form/seterrorstricky.html', $template); 205 206 $page = $this->initTemplate('/tags/form/form/seterrorstricky.html'); 207 208 $form = $page->getChild('testForm'); 209 210 $form->setErrors($error_list); 211 212 $label1 = $page->getChild('Label1'); 213 $this->assertEqual($label1->getAttribute('class'),'Error'); 214 $InputText = $page->getChild('InputText'); 215 $this->assertTrue($InputText->hasErrors()); 216 217 $label2 = $page->getChild('Label2'); 218 $this->assertEqual($label2->getAttribute('class'),'Error'); 219 $Select1 = $page->getChild('Select1'); 220 $this->assertTrue($Select1->hasErrors()); 221 222 $label3 = $page->getChild('Label3'); 223 $this->assertEqual($label3->getAttribute('class'),'Normal'); 224 $Select2 = $page->getChild('Select2'); 225 $this->assertTrue($Select2->hasErrors()); 226 227 $label4 = $page->getChild('Label4'); 228 $this->assertEqual($label4->getAttribute('class'),'Error'); 229 $Select3 = $page->getChild('Select3'); 230 $this->assertTrue($Select3->hasErrors()); 231 232 $label5 = $page->getChild('Label5'); 233 $this->assertEqual($label5->getAttribute('class'),'Normal'); 234 $Select4 = $page->getChild('Select4'); 235 $this->assertTrue($Select4->hasErrors()); 236 237 $label6 = $page->getChild('Label6'); 238 $this->assertEqual($label6->getAttribute('class'),'Normal'); 239 $InputCheckbox1 = $page->getChild('InputCheckbox1'); 240 $this->assertFalse($InputCheckbox1->hasErrors()); 241 242 $label7 = $page->getChild('Label7'); 243 $this->assertEqual($label7->getAttribute('class'),'Error'); 244 $InputCheckbox2 = $page->getChild('InputCheckbox2'); 245 $this->assertTrue($InputCheckbox2->hasErrors()); 246 247 $label8 = $page->getChild('Label8'); 248 $this->assertEqual($label8->getAttribute('class'),'Normal'); 249 $InputCheckbox3 = $page->getChild('InputCheckbox3'); 250 $this->assertFalse($InputCheckbox3->hasErrors()); 251 } 252 253 function testPreserveState() 254 { 255 $data = new WactArrayObject(); 256 $data->set('x','a'); 257 $data->set('y','b'); 258 $data->set('z','x < z'); 259 260 $form = new WactFormComponent('my_id'); 261 $form->registerDataSource($data); 262 263 $form->preserveState('x'); 264 $form->preserveState('z'); 265 266 ob_start(); 267 $form->renderState(); 268 $result = ob_get_contents(); 269 ob_end_clean(); 270 271 $test = '<input type="hidden" name="x" value="a"/><input type="hidden" name="z" value="x < z"/>'; 272 273 $this->assertEqual($result,$test); 274 } 275 276 function testKnownChildren() 277 { 278 $template = '<form id="test" runat="server"><input id="submit" type="submit" name="submit" value="hey"/></form>'; 279 $this->registerTestingTemplate('/tags/form/form/knownchildren.html', $template); 280 281 $page = $this->initTemplate('/tags/form/form/knownchildren.html'); 282 $this->assertIsA($page->findChild('test'),'WactFormComponent'); 283 $this->assertIsA($page->findChild('submit'),'WactFormElementComponent'); 284 $output = $page->capture(); 285 $this->assertEqual($output, '<form id="test"><input id="submit" type="submit" name="submit" value="hey" /></form>'); 286 } 287 288 function testKnownChildrenReuseRunatTrue() 289 { 290 $template = '<form id="test" runat="server" children_reuse_runat="true"><input id="submit" type="submit" name="submit" value="hey"></form>'; 291 $this->registerTestingTemplate('/tags/form/form/knownchildrenchildren_reuse_runattrue.html', $template); 292 293 $page = $this->initTemplate('/tags/form/form/knownchildrenchildren_reuse_runattrue.html'); 294 $this->assertIsA($page->findChild('test'),'WactFormComponent'); 295 $this->assertIsA($page->findChild('submit'),'WactFormElementComponent'); 296 $output = $page->capture(); 297 $this->assertEqual($output, '<form id="test"><input id="submit" type="submit" name="submit" value="hey"></form>'); 298 } 299 300 function testKnownChildrenReuseRunatFalse() 301 { 302 $template = '<form id="test" runat="server" children_reuse_runat="fAlSe"><input id="submit" type="submit" name="submit" value="hey" /></form>'; 303 $this->registerTestingTemplate('/tags/form/form/knownchildrenchildren_reuse_runatfalse.html', $template); 304 305 $page = $this->initTemplate('/tags/form/form/knownchildrenchildren_reuse_runatfalse.html'); 306 $this->assertIsA($page->findChild('test'),'WactFormComponent'); 307 $this->assertFalse($page->findChild('submit')); 308 $output = $page->capture(); 309 $this->assertEqual($output, '<form id="test"><input id="submit" type="submit" name="submit" value="hey" /></form>'); 310 } 311 312 function testKnownChildrenRunatClient() 313 { 314 $template = '<form id="test" runat="server"><input id="submit" type="submit" name="submit" value="hey" runat="client" /></form>'; 315 $this->registerTestingTemplate('/tags/form/form/knownchildrenuserunatclient.html', $template); 316 317 $page = $this->initTemplate('/tags/form/form/knownchildrenuserunatclient.html'); 318 $this->assertIsA($page->findChild('test'),'WactFormComponent'); 319 $this->assertFalse($page->findChild('submit')); 320 $output = $page->capture(); 321 $this->assertEqual($output, '<form id="test"><input id="submit" type="submit" name="submit" value="hey" /></form>'); 322 } 323 324 function testNestedKnownChildren() 325 { 326 $template = '<form id="test" runat="server"><core:block name="block"><input id="submit" type="submit" name="submit" value="hey"/></core:block></form>'; 327 $this->registerTestingTemplate('/tags/form/form/nestedknownchildren.html', $template); 328 329 $page = $this->initTemplate('/tags/form/form/nestedknownchildren.html'); 330 $this->assertIsA($page->findChild('test'),'WactFormComponent'); 331 $this->assertIsA($page->findChild('submit'),'WactFormElementComponent'); 332 $output = $page->capture(); 333 $this->assertEqual($output, '<form id="test"><input id="submit" type="submit" name="submit" value="hey" /></form>'); 334 } 335 336 function testIncludedKnownChildren() 337 { 338 $template = '<input id="submit" type="submit" name="submit" value="hey"/>'; 339 $this->registerTestingTemplate('/tags/form/form/knowninclude.html', $template); 340 341 $template = '<form id="test" runat="server"><core:include file="/tags/form/form/knowninclude.html"/></form>'; 342 $this->registerTestingTemplate('/tags/form/form/includedknownchildren.html', $template); 343 344 $page = $this->initTemplate('/tags/form/form/includedknownchildren.html'); 345 $this->assertIsA($page->findChild('test'),'WactFormComponent'); 346 $this->assertIsA($page->findChild('submit'),'WactFormElementComponent'); 347 $output = $page->capture(); 348 $this->assertEqual($output, '<form id="test"><input id="submit" type="submit" name="submit" value="hey" /></form>'); 349 } 350 351 function testGetServerIdWithID() 352 { 353 $template = '<form id="test" name="foo" runat="server"></form>'; 354 $this->registerTestingTemplate('/tags/form/form/getserveridwithid.html', $template); 355 356 $page = $this->initTemplate('/tags/form/form/getserveridwithid.html'); 357 $page->getChild('test'); 358 $this->assertNoErrors(); 359 } 360 361 function testGetServerIdWithName() 362 { 363 $template = '<form name="foo" runat="server"></form>'; 364 $this->registerTestingTemplate('/tags/form/form/getserveridwithname.html', $template); 365 366 $page = $this->initTemplate('/tags/form/form/getserveridwithname.html'); 367 $page->getChild('foo'); 368 $this->assertNoErrors(); 369 } 370 371 function testIsDataSource() 372 { 373 $FormTag = new WactFormTag(null, null, null); 374 $this->assertTrue($FormTag->isDataSource()); 375 } 376 377 function testFromAttribute() 378 { 379 $template = 380 '<form name="my_form" from="{$^middle}" runat="server">' . 381 '{$Var}:{$^Var}:{$#Var}' . 382 '</form>'; 383 384 $this->registerTestingTemplate('/tags/form/form/from_attribute.html', $template); 385 $page = $this->initTemplate('/tags/form/form/from_attribute.html'); 386 $page->set('Var', 'outer'); 387 $page->set('middle', array('Var' => 'middle')); 388 389 $output = $page->capture(); 390 $this->assertEqual($output, '<form name="my_form">middle:outer:outer</form>'); 391 } 392 393 function testFromAttributeWithOldSyntaxDataTakenFromParent() 394 { 395 $template = 396 '<form name="my_form" from="middle" runat="server">' . 397 '{