1<?php 2 3require_once DOKU_INC.'inc/init.php'; 4 5class form_test extends DokuWikiTest { 6 7 function _testform() { 8 $form = new Doku_Form(array('id' => 'dw__testform', 'action' => '/test')); 9 $form->startFieldset('Test'); 10 $form->addHidden('summary', 'changes &c'); 11 $form->addElement(form_makeTextField('t', 'v', 'Text', 'text__id', 'block')); 12 $form->addElement(form_makeCheckboxField('r', '1', 'Check', 'check__id', 'simple')); 13 $form->addElement(form_makeButton('submit', 'save', 'Save', array('accesskey'=>'s'))); 14 $form->addElement(form_makeButton('submit', 'cancel', 'Cancel')); 15 $form->endFieldset(); 16 return $form; 17 } 18 19 function _realoutput() { 20 global $lang; 21 $realoutput = '<form id="dw__testform" action="/test" method="post" '; 22 $realoutput .= 'accept-charset="'.$lang['encoding'].'">'; 23 $realoutput .= "\n"; 24 $realoutput .= '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" />'; 25 $realoutput .= '<input type="hidden" name="summary" value="changes &c" />'; 26 $realoutput .= "\n"; 27 $realoutput .= "<fieldset ><legend>Test</legend>\n"; 28 $realoutput .= '<label class="block" for="text__id"><span>Text</span> '; 29 $realoutput .= '<input type="text" id="text__id" name="t" value="v" class="edit" /></label><br />'; 30 $realoutput .= "\n"; 31 $realoutput .= '<label class="simple" for="check__id">'; 32 $realoutput .= '<input type="checkbox" id="check__id" name="r" value="1" /> '; 33 $realoutput .= '<span>Check</span></label>'; 34 $realoutput .= "\n"; 35 $realoutput .= '<input name="do[save]" type="submit" value="Save" class="button" accesskey="s" title="Save [S]" />'; 36 $realoutput .= "\n"; 37 $realoutput .= '<input name="do[cancel]" type="submit" value="Cancel" class="button" />'; 38 $realoutput .= "\n"; 39 $realoutput .= "</fieldset>\n</div></form>\n"; 40 return $realoutput; 41 } 42 43 function _ignoreTagWS($data){ 44 return preg_replace('/>\s+</','><',$data); 45 } 46 47 function test_form_print() { 48 $form = $this->_testform(); 49 ob_start(); 50 $form->printForm(); 51 $output = ob_get_contents(); 52 ob_end_clean(); 53 $form->addHidden('sectok', getSecurityToken()); 54 $this->assertEquals($this->_ignoreTagWS($output),$this->_ignoreTagWS($this->_realoutput())); 55 } 56 57 function test_get_element_at() { 58 $form = $this->_testform(); 59 $e1 =& $form->getElementAt(1); 60 $this->assertEquals($e1, array('_elem'=>'textfield', 61 '_text'=>'Text', 62 '_class'=>'block', 63 'id'=>'text__id', 64 'name'=>'t', 65 'value'=>'v', 66 'class'=>'edit')); 67 $e2 =& $form->getElementAt(99); 68 $this->assertEquals($e2, array('_elem'=>'closefieldset')); 69 } 70 71 function test_find_element_by_type() { 72 $form = $this->_testform(); 73 $this->assertEquals($form->findElementByType('button'), 3); 74 $this->assertFalse($form->findElementByType('text')); 75 } 76 77 function test_find_element_by_id() { 78 $form = $this->_testform(); 79 $this->assertEquals($form->findElementById('check__id'), 2); 80 $this->assertFalse($form->findElementById('dw__testform')); 81 } 82 83 function test_find_element_by_attribute() { 84 $form = $this->_testform(); 85 $this->assertEquals($form->findElementByAttribute('value','Cancel'), 4); 86 $this->assertFalse($form->findElementByAttribute('name','cancel')); 87 } 88 89 function test_close_fieldset() { 90 $form = new Doku_Form(array('id' => 'dw__testform', 'action' => '/test')); 91 $form->startFieldset('Test'); 92 $form->addHidden('summary', 'changes &c'); 93 $form->addElement(form_makeTextField('t', 'v', 'Text', 'text__id', 'block')); 94 $form->addElement(form_makeCheckboxField('r', '1', 'Check', 'check__id', 'simple')); 95 $form->addElement(form_makeButton('submit', 'save', 'Save', array('accesskey'=>'s'))); 96 $form->addElement(form_makeButton('submit', 'cancel', 'Cancel')); 97 ob_start(); 98 $form->printForm(); 99 $output = ob_get_contents(); 100 ob_end_clean(); 101 $this->assertEquals($this->_ignoreTagWS($output),$this->_ignoreTagWS($this->_realoutput())); 102 } 103 104} 105