tiyowan | a83f063 | 2012-03-16 23:52:43 +0400 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | require BASEPATH . 'core/Common.php'; |
| 4 | require BASEPATH . 'helpers/form_helper.php'; |
| 5 | |
| 6 | class Form_helper_test extends CI_TestCase |
| 7 | { |
| 8 | public function test_form_hidden() |
| 9 | { |
| 10 | $expected = <<<EOH |
| 11 | |
| 12 | <input type="hidden" name="username" value="johndoe" /> |
| 13 | |
| 14 | EOH; |
| 15 | |
| 16 | $this->assertEquals($expected, form_hidden('username', 'johndoe')); |
| 17 | } |
| 18 | |
| 19 | public function test_form_input() |
| 20 | { |
| 21 | $expected = <<<EOH |
| 22 | <input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" /> |
| 23 | |
| 24 | EOH; |
| 25 | |
| 26 | $data = array( |
| 27 | 'name' => 'username', |
| 28 | 'id' => 'username', |
| 29 | 'value' => 'johndoe', |
| 30 | 'maxlength' => '100', |
| 31 | 'size' => '50', |
| 32 | 'style' => 'width:50%', |
| 33 | ); |
| 34 | |
| 35 | $this->assertEquals($expected, form_input($data)); |
| 36 | } |
| 37 | |
| 38 | public function test_form_password() |
| 39 | { |
| 40 | $expected = <<<EOH |
| 41 | <input type="password" name="password" value="" /> |
| 42 | |
| 43 | EOH; |
| 44 | |
| 45 | $this->assertEquals($expected, form_password('password')); |
| 46 | } |
| 47 | |
| 48 | public function test_form_upload() |
| 49 | { |
| 50 | $expected = <<<EOH |
| 51 | <input type="file" name="attachment" value="" /> |
| 52 | |
| 53 | EOH; |
| 54 | |
| 55 | $this->assertEquals($expected, form_upload('attachment')); |
| 56 | } |
| 57 | |
| 58 | public function test_form_textarea() |
| 59 | { |
| 60 | $expected = <<<EOH |
| 61 | <textarea name="notes" cols="40" rows="10" >Notes</textarea> |
| 62 | |
| 63 | EOH; |
| 64 | |
| 65 | $this->assertEquals($expected, form_textarea('notes', 'Notes')); |
| 66 | } |
| 67 | |
| 68 | public function test_form_dropdown() |
| 69 | { |
| 70 | $expected = <<<EOH |
| 71 | <select name="shirts"> |
| 72 | <option value="small">Small Shirt</option> |
| 73 | <option value="med">Medium Shirt</option> |
| 74 | <option value="large" selected="selected">Large Shirt</option> |
| 75 | <option value="xlarge">Extra Large Shirt</option> |
| 76 | </select> |
| 77 | |
| 78 | EOH; |
| 79 | |
| 80 | $options = array( |
| 81 | 'small' => 'Small Shirt', |
| 82 | 'med' => 'Medium Shirt', |
| 83 | 'large' => 'Large Shirt', |
| 84 | 'xlarge' => 'Extra Large Shirt', |
| 85 | ); |
| 86 | |
| 87 | $this->assertEquals($expected, form_dropdown('shirts', $options, 'large')); |
| 88 | |
| 89 | $expected = <<<EOH |
| 90 | <select name="shirts" multiple="multiple"> |
| 91 | <option value="small" selected="selected">Small Shirt</option> |
| 92 | <option value="med">Medium Shirt</option> |
| 93 | <option value="large" selected="selected">Large Shirt</option> |
| 94 | <option value="xlarge">Extra Large Shirt</option> |
| 95 | </select> |
| 96 | |
| 97 | EOH; |
| 98 | |
| 99 | $shirts_on_sale = array('small', 'large'); |
| 100 | |
| 101 | $this->assertEquals($expected, form_dropdown('shirts', $options, $shirts_on_sale)); |
| 102 | |
| 103 | $options = array( |
| 104 | 'Swedish Cars' => array( |
| 105 | 'volvo' => 'Volvo', |
| 106 | 'saab' => 'Saab' |
| 107 | ), |
| 108 | 'German Cars' => array( |
| 109 | 'mercedes' => 'Mercedes', |
| 110 | 'audi' => 'Audi' |
| 111 | ) |
| 112 | ); |
| 113 | |
| 114 | $expected = <<<EOH |
| 115 | <select name="cars" multiple="multiple"> |
| 116 | <optgroup label="Swedish Cars"> |
| 117 | <option value="volvo" selected="selected">Volvo</option> |
| 118 | <option value="saab">Saab</option> |
| 119 | </optgroup> |
| 120 | <optgroup label="German Cars"> |
| 121 | <option value="mercedes">Mercedes</option> |
| 122 | <option value="audi" selected="selected">Audi</option> |
| 123 | </optgroup> |
| 124 | </select> |
| 125 | |
| 126 | EOH; |
| 127 | |
| 128 | $cars_on_sale = array('volvo', 'audi'); |
| 129 | |
| 130 | $this->assertEquals($expected, form_dropdown('cars', $options, $cars_on_sale)); |
| 131 | |
| 132 | } |
| 133 | |
| 134 | public function test_form_multiselect() |
| 135 | { |
| 136 | $expected = <<<EOH |
| 137 | <select name="shirts[]" multiple="multiple"> |
| 138 | <option value="small">Small Shirt</option> |
| 139 | <option value="med" selected="selected">Medium Shirt</option> |
| 140 | <option value="large" selected="selected">Large Shirt</option> |
| 141 | <option value="xlarge">Extra Large Shirt</option> |
| 142 | </select> |
| 143 | |
| 144 | EOH; |
| 145 | |
| 146 | $options = array( |
| 147 | 'small' => 'Small Shirt', |
| 148 | 'med' => 'Medium Shirt', |
| 149 | 'large' => 'Large Shirt', |
| 150 | 'xlarge' => 'Extra Large Shirt', |
| 151 | ); |
| 152 | |
| 153 | $this->assertEquals($expected, form_multiselect('shirts[]', $options, array('med', 'large'))); |
| 154 | } |
| 155 | |
| 156 | public function test_form_fieldset() |
| 157 | { |
| 158 | $expected = <<<EOH |
| 159 | <fieldset> |
| 160 | <legend>Address Information</legend> |
| 161 | |
| 162 | EOH; |
| 163 | |
| 164 | $this->assertEquals($expected, form_fieldset('Address Information')); |
| 165 | } |
| 166 | |
| 167 | public function test_form_fieldset_close() |
| 168 | { |
| 169 | $expected = <<<EOH |
| 170 | </fieldset></div></div> |
| 171 | EOH; |
| 172 | |
| 173 | $this->assertEquals($expected, form_fieldset_close('</div></div>')); |
| 174 | } |
| 175 | |
| 176 | public function test_form_checkbox() |
| 177 | { |
| 178 | $expected = <<<EOH |
| 179 | <input type="checkbox" name="newsletter" value="accept" checked="checked" /> |
| 180 | |
| 181 | EOH; |
| 182 | |
| 183 | $this->assertEquals($expected, form_checkbox('newsletter', 'accept', TRUE)); |
| 184 | } |
| 185 | |
| 186 | public function test_form_radio() |
| 187 | { |
| 188 | $expected = <<<EOH |
| 189 | <input type="radio" name="newsletter" value="accept" checked="checked" /> |
| 190 | |
| 191 | EOH; |
| 192 | |
| 193 | $this->assertEquals($expected, form_radio('newsletter', 'accept', TRUE)); |
| 194 | } |
| 195 | |
| 196 | public function test_form_submit() |
| 197 | { |
| 198 | $expected = <<<EOH |
| 199 | <input type="submit" name="mysubmit" value="Submit Post!" /> |
| 200 | |
| 201 | EOH; |
| 202 | |
| 203 | $this->assertEquals($expected, form_submit('mysubmit', 'Submit Post!')); |
| 204 | } |
| 205 | |
| 206 | public function test_form_label() |
| 207 | { |
| 208 | $expected = <<<EOH |
| 209 | <label for="username">What is your Name</label> |
| 210 | EOH; |
| 211 | |
| 212 | $this->assertEquals($expected, form_label('What is your Name', 'username')); |
| 213 | } |
| 214 | |
| 215 | public function test_form_reset() |
| 216 | { |
| 217 | $expected = <<<EOH |
| 218 | <input type="reset" name="myreset" value="Reset" /> |
| 219 | |
| 220 | EOH; |
| 221 | |
| 222 | $this->assertEquals($expected, form_reset('myreset', 'Reset')); |
| 223 | } |
| 224 | |
| 225 | public function test_form_button() |
| 226 | { |
| 227 | $expected = <<<EOH |
| 228 | <button name="name" type="button" >content</button> |
| 229 | |
| 230 | EOH; |
| 231 | |
| 232 | $this->assertEquals($expected, form_button('name','content')); |
| 233 | } |
| 234 | |
| 235 | public function test_form_close() |
| 236 | { |
| 237 | $expected = <<<EOH |
| 238 | </form></div></div> |
| 239 | EOH; |
| 240 | |
| 241 | $this->assertEquals($expected, form_close('</div></div>')); |
| 242 | } |
| 243 | |
| 244 | public function test_form_prep() |
| 245 | { |
| 246 | $expected = "Here is a string containing "quoted" text."; |
| 247 | |
| 248 | $this->assertEquals($expected, form_prep('Here is a string containing "quoted" text.')); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* End of file form_helper_test.php */ |