1#!/usr/bin/perl -w 2 3use strict; 4 5use Test::More tests => 62; 6 7BEGIN 8{ 9 use_ok('Rose::HTML::Form::Field::OptionGroup'); 10} 11 12my $field = Rose::HTML::Form::Field::OptionGroup->new(name => 'fruits', 13 label => 'Group 1'); 14 15ok(ref $field eq 'Rose::HTML::Form::Field::OptionGroup', 'new()'); 16 17is(scalar @{ $field->children }, 0, 'children scalar 1'); 18is(scalar(() = $field->children), 0, 'children list 1'); 19 20$field->options(apple => 'Apple', 21 orange => 'Orange', 22 grape => 'Grape'); 23 24is(scalar @{ $field->children }, 3, 'children scalar 2'); 25is(scalar(() = $field->children), 3, 'children list 2'); 26 27$field->option('apple')->short_label('1.0'); 28$field->option('orange')->short_label('2.0'); 29$field->option('grape')->short_label('3.0'); 30 31is($field->html_field, 32 qq(<optgroup label="Group 1">\n) . 33 qq(<option label="1.0" value="apple">Apple</option>\n) . 34 qq(<option label="2.0" value="orange">Orange</option>\n) . 35 qq(<option label="3.0" value="grape">Grape</option>\n) . 36 qq(</optgroup>), 37 'html_field() 1'); 38 39is($field->label('Fruits'), 'Fruits', 'label()'); 40 41is($field->html_field, 42 qq(<optgroup label="Fruits">\n) . 43 qq(<option label="1.0" value="apple">Apple</option>\n) . 44 qq(<option label="2.0" value="orange">Orange</option>\n) . 45 qq(<option label="3.0" value="grape">Grape</option>\n) . 46 qq(</optgroup>), 47 'html_field() 2'); 48 49$field->option('apple')->label('<b>Apple</b>'); 50$field->escape_html(0); 51 52is($field->html_field, 53 qq(<optgroup label="Fruits">\n) . 54 qq(<option label="1.0" value="apple"><b>Apple</b></option>\n) . 55 qq(<option label="2.0" value="orange">Orange</option>\n) . 56 qq(<option label="3.0" value="grape">Grape</option>\n) . 57 qq(</optgroup>), 58 'escape_html() 1'); 59 60$field->escape_html(1); 61 62is($field->html_field, 63 qq(<optgroup label="Fruits">\n) . 64 qq(<option label="1.0" value="apple"><b>Apple</b></option>\n) . 65 qq(<option label="2.0" value="orange">Orange</option>\n) . 66 qq(<option label="3.0" value="grape">Grape</option>\n) . 67 qq(</optgroup>), 68 'escape_html() 2'); 69 70is($field->xhtml_field, 71 qq(<optgroup label="Fruits">\n) . 72 qq(<option label="1.0" value="apple"><b>Apple</b></option>\n) . 73 qq(<option label="2.0" value="orange">Orange</option>\n) . 74 qq(<option label="3.0" value="grape">Grape</option>\n) . 75 qq(</optgroup>), 76 'xhtml_field() 1'); 77 78$field->error('Whatever'); 79 80is($field->html, 81 qq(<optgroup label="Fruits">\n) . 82 qq(<option label="1.0" value="apple"><b>Apple</b></option>\n) . 83 qq(<option label="2.0" value="orange">Orange</option>\n) . 84 qq(<option label="3.0" value="grape">Grape</option>\n) . 85 qq(</optgroup>), 86 'html() 1'); 87 88is($field->xhtml, 89 qq(<optgroup label="Fruits">\n) . 90 qq(<option label="1.0" value="apple"><b>Apple</b></option>\n) . 91 qq(<option label="2.0" value="orange">Orange</option>\n) . 92 qq(<option label="3.0" value="grape">Grape</option>\n) . 93 qq(</optgroup>), 94 'xhtml() 1'); 95 96$field->option('apple')->label('Apple'); 97 98$field->default('apple'); 99 100is($field->html_field, 101 qq(<optgroup label="Fruits">\n) . 102 qq(<option label="1.0" selected value="apple">Apple</option>\n) . 103 qq(<option label="2.0" value="orange">Orange</option>\n) . 104 qq(<option label="3.0" value="grape">Grape</option>\n) . 105 qq(</optgroup>), 106 'default()'); 107 108is($field->value_label, 'Apple', 'value_label()'); 109 110$field->input_value('orange'); 111 112is($field->xhtml_field, 113 qq(<optgroup label="Fruits">\n) . 114 qq(<option label="1.0" value="apple">Apple</option>\n) . 115 qq(<option label="2.0" selected="selected" value="orange">Orange</option>\n) . 116 qq(<option label="3.0" value="grape">Grape</option>\n) . 117 qq(</optgroup>), 118 'value() 1'); 119 120$field->error(undef); 121 122$field->option('apple')->delete_html_attr('label'); 123$field->option('orange')->delete_html_attr('label'); 124$field->option('grape')->delete_html_attr('label'); 125 126$field->multiple(1); 127 128$field->add_value('apple'); 129 130is($field->html_field, 131 qq(<optgroup label="Fruits">\n) . 132 qq(<option selected value="apple">Apple</option>\n) . 133 qq(<option selected value="orange">Orange</option>\n) . 134 qq(<option value="grape">Grape</option>\n) . 135 qq(</optgroup>), 136 'add_value() 1'); 137 138is(join(',', $field->internal_value), 'apple,orange', 'internal_value() 1'); 139is(join(',', @{$field->output_value}), 'apple,orange', 'output_value() 1'); 140is(join(',', @{$field->values}), 'apple,orange', 'values() 1'); 141 142$field->input_value(undef); 143 144$field->add_values('orange', 'grape'); 145 146is($field->xhtml_field, 147 qq(<optgroup label="Fruits">\n) . 148 qq(<option value="apple">Apple</option>\n) . 149 qq(<option selected="selected" value="orange">Orange</option>\n) . 150 qq(<option selected="selected" value="grape">Grape</option>\n) . 151 qq(</optgroup>), 152 'add_values() 1'); 153 154is(join(',', $field->internal_value), 'grape,orange', 'internal_value() 2'); 155is(join(',', @{$field->output_value}), 'grape,orange', 'output_value() 2'); 156is(join(',', @{$field->values}), 'grape,orange', 'values() 2'); 157 158ok($field->is_selected('orange'), 'is_selected() 1'); 159ok($field->is_selected('grape'), 'is_selected() 2'); 160ok(!$field->is_selected('apple'), 'is_selected() 3'); 161ok(!$field->is_selected('foo'), 'is_selected() 4'); 162 163ok($field->has_value('orange'), 'has_value() 1'); 164ok($field->has_value('grape'), 'has_value() 2'); 165ok(!$field->has_value('apple'), 'has_value() 3'); 166ok(!$field->has_value('foo'), 'has_value() 4'); 167 168$field->add_options(pear => 'Pear', berry => 'Berry'); 169 170is($field->html_field, 171 qq(<optgroup label="Fruits">\n) . 172 qq(<option value="apple">Apple</option>\n) . 173 qq(<option selected value="orange">Orange</option>\n) . 174 qq(<option selected value="grape">Grape</option>\n) . 175 qq(<option value="pear">Pear</option>\n) . 176 qq(<option value="berry">Berry</option>\n) . 177 qq(</optgroup>), 178 'add_options() hash'); 179 180$field->add_options(Rose::HTML::Form::Field::Option->new(value => 'squash', label => 'Squash'), 181 Rose::HTML::Form::Field::Option->new(value => 'cherry', label => 'Cherry')); 182 183is($field->html_field, 184 qq(<optgroup label="Fruits">\n) . 185 qq(<option value="apple">Apple</option>\n) . 186 qq(<option selected value="orange">Orange</option>\n) . 187 qq(<option selected value="grape">Grape</option>\n) . 188 qq(<option value="pear">Pear</option>\n) . 189 qq(<option value="berry">Berry</option>\n) . 190 qq(<option value="squash">Squash</option>\n) . 191 qq(<option value="cherry">Cherry</option>\n) . 192 qq(</optgroup>), 193 'add_options() objects'); 194 195is($field->html_hidden_field, 196 qq(<input name="fruits" type="hidden" value="orange">\n) . 197 qq(<input name="fruits" type="hidden" value="grape">), 198 'html_hidden_field()'); 199 200is($field->html_hidden_fields, 201 qq(<input name="fruits" type="hidden" value="orange">\n) . 202 qq(<input name="fruits" type="hidden" value="grape">), 203 'html_hidden_fields()'); 204 205is(join("\n", map { $_->html } $field->hidden_field), 206 qq(<input name="fruits" type="hidden" value="orange">\n) . 207 qq(<input name="fruits" type="hidden" value="grape">), 208 'hidden_field()'); 209 210is(join("\n", map { $_->html } $field->hidden_fields), 211 qq(<input name="fruits" type="hidden" value="orange">\n) . 212 qq(<input name="fruits" type="hidden" value="grape">), 213 'hidden_fields()'); 214 215$field->clear; 216 217is(join('', $field->internal_value), '', 'clear() 1'); 218 219is($field->html_field, 220 qq(<optgroup label="Fruits">\n) . 221 qq(<option value="apple">Apple</option>\n) . 222 qq(<option value="orange">Orange</option>\n) . 223 qq(<option value="grape">Grape</option>\n) . 224 qq(<option value="pear">Pear</option>\n) . 225 qq(<option value="berry">Berry</option>\n) . 226 qq(<option value="squash">Squash</option>\n) . 227 qq(<option value="cherry">Cherry</option>\n) . 228 qq(</optgroup>), 229 'clear() 2'); 230 231$field->reset; 232 233is(join('', $field->internal_value), 'apple', 'reset() 1'); 234 235is($field->html_field, 236 qq(<optgroup label="Fruits">\n) . 237 qq(<option selected value="apple">Apple</option>\n) . 238 qq(<option value="orange">Orange</option>\n) . 239 qq(<option value="grape">Grape</option>\n) . 240 qq(<option value="pear">Pear</option>\n) . 241 qq(<option value="berry">Berry</option>\n) . 242 qq(<option value="squash">Squash</option>\n) . 243 qq(<option value="cherry">Cherry</option>\n) . 244 qq(</optgroup>), 245 'reset() 2'); 246 247$field->default_value(undef); 248 249is(join('', $field->internal_value), '', 'reset() 3'); 250 251is($field->html_field, 252 qq(<optgroup label="Fruits">\n) . 253 qq(<option value="apple">Apple</option>\n) . 254 qq(<option value="orange">Orange</option>\n) . 255 qq(<option value="grape">Grape</option>\n) . 256 qq(<option value="pear">Pear</option>\n) . 257 qq(<option value="berry">Berry</option>\n) . 258 qq(<option value="squash">Squash</option>\n) . 259 qq(<option value="cherry">Cherry</option>\n) . 260 qq(</optgroup>), 261 'reset() 4'); 262 263$field->add_value('pear'); 264 265is(join('', $field->internal_value), 'pear', 'add_value() 2'); 266 267is($field->html_field, 268 qq(<optgroup label="Fruits">\n) . 269 qq(<option value="apple">Apple</option>\n) . 270 qq(<option value="orange">Orange</option>\n) . 271 qq(<option value="grape">Grape</option>\n) . 272 qq(<option selected value="pear">Pear</option>\n) . 273 qq(<option value="berry">Berry</option>\n) . 274 qq(<option value="squash">Squash</option>\n) . 275 qq(<option value="cherry">Cherry</option>\n) . 276 qq(</optgroup>), 277 'add_value() 3'); 278 279$field->add_values('squash', 'cherry'); 280 281is(join(',', $field->internal_value), 'cherry,pear,squash', 'add_values() 2'); 282 283is($field->html_field, 284 qq(<optgroup label="Fruits">\n) . 285 qq(<option value="apple">Apple</option>\n) . 286 qq(<option value="orange">Orange</option>\n) . 287 qq(<option value="grape">Grape</option>\n) . 288 qq(<option selected value="pear">Pear</option>\n) . 289 qq(<option value="berry">Berry</option>\n) . 290 qq(<option selected value="squash">Squash</option>\n) . 291 qq(<option selected value="cherry">Cherry</option>\n) . 292 qq(</optgroup>), 293 'add_values() 3'); 294 295$field->reset; 296 297is(join(',', $field->internal_value), '', 'reset() 5'); 298 299is($field->html_field, 300 qq(<optgroup label="Fruits">\n) . 301 qq(<option value="apple">Apple</option>\n) . 302 qq(<option value="orange">Orange</option>\n) . 303 qq(<option value="grape">Grape</option>\n) . 304 qq(<option value="pear">Pear</option>\n) . 305 qq(<option value="berry">Berry</option>\n) . 306 qq(<option value="squash">Squash</option>\n) . 307 qq(<option value="cherry">Cherry</option>\n) . 308 qq(</optgroup>), 309 'reset() 6'); 310 311$field->default('orange'); 312 313is(join(',', $field->internal_value), 'orange', 'reset() 7'); 314 315is($field->html_field, 316 qq(<optgroup label="Fruits">\n) . 317 qq(<option value="apple">Apple</option>\n) . 318 qq(<option selected value="orange">Orange</option>\n) . 319 qq(<option value="grape">Grape</option>\n) . 320 qq(<option value="pear">Pear</option>\n) . 321 qq(<option value="berry">Berry</option>\n) . 322 qq(<option value="squash">Squash</option>\n) . 323 qq(<option value="cherry">Cherry</option>\n) . 324 qq(</optgroup>), 325 'reset() 8'); 326 327$field->clear; 328 329is(join(',', $field->internal_value), '', 'clear() 3'); 330 331is($field->html_field, 332 qq(<optgroup label="Fruits">\n) . 333 qq(<option value="apple">Apple</option>\n) . 334 qq(<option value="orange">Orange</option>\n) . 335 qq(<option value="grape">Grape</option>\n) . 336 qq(<option value="pear">Pear</option>\n) . 337 qq(<option value="berry">Berry</option>\n) . 338 qq(<option value="squash">Squash</option>\n) . 339 qq(<option value="cherry">Cherry</option>\n) . 340 qq(</optgroup>), 341 'clear() 4'); 342 343$field->option('apple')->short_label('1.0'); 344 345is($field->html_field, 346 qq(<optgroup label="Fruits">\n) . 347 qq(<option label="1.0" value="apple">Apple</option>\n) . 348 qq(<option value="orange">Orange</option>\n) . 349 qq(<option value="grape">Grape</option>\n) . 350 qq(<option value="pear">Pear</option>\n) . 351 qq(<option value="berry">Berry</option>\n) . 352 qq(<option value="squash">Squash</option>\n) . 353 qq(<option value="cherry">Cherry</option>\n) . 354 qq(</optgroup>), 355 'option short_label()'); 356 357$field->disabled(1); 358 359is($field->html_field, 360 qq(<optgroup disabled label="Fruits">\n) . 361 qq(<option label="1.0" value="apple">Apple</option>\n) . 362 qq(<option value="orange">Orange</option>\n) . 363 qq(<option value="grape">Grape</option>\n) . 364 qq(<option value="pear">Pear</option>\n) . 365 qq(<option value="berry">Berry</option>\n) . 366 qq(<option value="squash">Squash</option>\n) . 367 qq(<option value="cherry">Cherry</option>\n) . 368 qq(</optgroup>), 369 'html_field() 3'); 370 371is($field->xhtml_field, 372 qq(<optgroup disabled="disabled" label="Fruits">\n) . 373 qq(<option label="1.0" value="apple">Apple</option>\n) . 374 qq(<option value="orange">Orange</option>\n) . 375 qq(<option value="grape">Grape</option>\n) . 376 qq(<option value="pear">Pear</option>\n) . 377 qq(<option value="berry">Berry</option>\n) . 378 qq(<option value="squash">Squash</option>\n) . 379 qq(<option value="cherry">Cherry</option>\n) . 380 qq(</optgroup>), 381 'xhtml_field() 2'); 382 383my $id = ref($field)->localizer->add_localized_message( 384 name => 'ORANGE_LABEL', 385 text => 386 { 387 en => 'Orange EN', 388 xx => 'Le Orange', 389 }); 390 391$field->option('orange')->label_id($id); 392 393is($field->option('orange')->label->as_string, 'Orange EN', 'localized label 1'); 394is($field->xhtml_field, 395 qq(<optgroup disabled="disabled" label="Fruits">\n) . 396 qq(<option label="1.0" value="apple">Apple</option>\n) . 397 qq(<option value="orange">Orange EN</option>\n) . 398 qq(<option value="grape">Grape</option>\n) . 399 qq(<option value="pear">Pear</option>\n) . 400 qq(<option value="berry">Berry</option>\n) . 401 qq(<option value="squash">Squash</option>\n) . 402 qq(<option value="cherry">Cherry</option>\n) . 403 qq(</optgroup>), 404 'localized label 2'); 405 406$field->localizer->locale('xx'); 407 408is($field->option('orange')->label->as_string, 'Le Orange', 'localized label 3'); 409is($field->xhtml_field, 410 qq(<optgroup disabled="disabled" label="Fruits">\n) . 411 qq(<option label="1.0" value="apple">Apple</option>\n) . 412 qq(<option value="orange">Le Orange</option>\n) . 413 qq(<option value="grape">Grape</option>\n) . 414 qq(<option value="pear">Pear</option>\n) . 415 qq(<option value="berry">Berry</option>\n) . 416 qq(<option value="squash">Squash</option>\n) . 417 qq(<option value="cherry">Cherry</option>\n) . 418 qq(</optgroup>), 419 'localized label 4'); 420