Projet

Général

Profil

Paste
Télécharger (22,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / field / modules / options / options.test @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for options.module.
6
 */
7

    
8
class OptionsWidgetsTestCase extends FieldTestCase {
9
  public static function getInfo() {
10
    return array(
11
      'name'  => 'Options widgets',
12
      'description'  => "Test the Options widgets.",
13
      'group' => 'Field types'
14
    );
15
  }
16

    
17
  function setUp() {
18
    parent::setUp('field_test', 'list_test');
19

    
20
    // Field with cardinality 1.
21
    $this->card_1 = array(
22
      'field_name' => 'card_1',
23
      'type' => 'list_integer',
24
      'cardinality' => 1,
25
      'settings' => array(
26
        // Make sure that 0 works as an option.
27
        'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
28
      ),
29
    );
30
    $this->card_1 = field_create_field($this->card_1);
31

    
32
    // Field with cardinality 2.
33
    $this->card_2 = array(
34
      'field_name' => 'card_2',
35
      'type' => 'list_integer',
36
      'cardinality' => 2,
37
      'settings' => array(
38
        // Make sure that 0 works as an option.
39
        'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
40
      ),
41
    );
42
    $this->card_2 = field_create_field($this->card_2);
43

    
44
    // Boolean field.
45
    $this->bool = array(
46
      'field_name' => 'bool',
47
      'type' => 'list_boolean',
48
      'cardinality' => 1,
49
      'settings' => array(
50
        // Make sure that 0 works as a 'on' value'.
51
        'allowed_values' => array(1 => 'Zero', 0 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
52
      ),
53
    );
54
    $this->bool = field_create_field($this->bool);
55

    
56
    // Create a web user.
57
    $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
58
    $this->drupalLogin($this->web_user);
59
  }
60

    
61
  /**
62
   * Tests the 'options_buttons' widget (single select).
63
   */
64
  function testRadioButtons() {
65
    // Create an instance of the 'single value' field.
66
    $instance = array(
67
      'field_name' => $this->card_1['field_name'],
68
      'entity_type' => 'test_entity',
69
      'bundle' => 'test_bundle',
70
      'widget' => array(
71
        'type' => 'options_buttons',
72
      ),
73
    );
74
    $instance = field_create_instance($instance);
75
    $langcode = LANGUAGE_NONE;
76

    
77
    // Create an entity.
78
    $entity_init = field_test_create_stub_entity();
79
    $entity = clone $entity_init;
80
    $entity->is_new = TRUE;
81
    field_test_entity_save($entity);
82

    
83
    // With no field data, no buttons are checked.
84
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
85
    $this->assertNoFieldChecked("edit-card-1-$langcode-0");
86
    $this->assertNoFieldChecked("edit-card-1-$langcode-1");
87
    $this->assertNoFieldChecked("edit-card-1-$langcode-2");
88
    $this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
89

    
90
    // Select first option.
91
    $edit = array("card_1[$langcode]" => 0);
92
    $this->drupalPost(NULL, $edit, t('Save'));
93
    $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
94

    
95
    // Check that the selected button is checked.
96
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
97
    $this->assertFieldChecked("edit-card-1-$langcode-0");
98
    $this->assertNoFieldChecked("edit-card-1-$langcode-1");
99
    $this->assertNoFieldChecked("edit-card-1-$langcode-2");
100

    
101
    // Unselect option.
102
    $edit = array("card_1[$langcode]" => '_none');
103
    $this->drupalPost(NULL, $edit, t('Save'));
104
    $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
105

    
106
    // Check that required radios with one option is auto-selected.
107
    $this->card_1['settings']['allowed_values'] = array(99 => 'Only allowed value');
108
    field_update_field($this->card_1);
109
    $instance['required'] = TRUE;
110
    field_update_instance($instance);
111
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
112
    $this->assertFieldChecked("edit-card-1-$langcode-99");
113
  }
114

    
115
  /**
116
   * Tests the 'options_buttons' widget (multiple select).
117
   */
118
  function testCheckBoxes() {
119
    // Create an instance of the 'multiple values' field.
120
    $instance = array(
121
      'field_name' => $this->card_2['field_name'],
122
      'entity_type' => 'test_entity',
123
      'bundle' => 'test_bundle',
124
      'widget' => array(
125
        'type' => 'options_buttons',
126
      ),
127
    );
128
    $instance = field_create_instance($instance);
129
    $langcode = LANGUAGE_NONE;
130

    
131
    // Create an entity.
132
    $entity_init = field_test_create_stub_entity();
133
    $entity = clone $entity_init;
134
    $entity->is_new = TRUE;
135
    field_test_entity_save($entity);
136

    
137
    // Display form: with no field data, nothing is checked.
138
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
139
    $this->assertNoFieldChecked("edit-card-2-$langcode-0");
140
    $this->assertNoFieldChecked("edit-card-2-$langcode-1");
141
    $this->assertNoFieldChecked("edit-card-2-$langcode-2");
142
    $this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
143

    
144
    // Submit form: select first and third options.
145
    $edit = array(
146
      "card_2[$langcode][0]" => TRUE,
147
      "card_2[$langcode][1]" => FALSE,
148
      "card_2[$langcode][2]" => TRUE,
149
    );
150
    $this->drupalPost(NULL, $edit, t('Save'));
151
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
152

    
153
    // Display form: check that the right options are selected.
154
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
155
    $this->assertFieldChecked("edit-card-2-$langcode-0");
156
    $this->assertNoFieldChecked("edit-card-2-$langcode-1");
157
    $this->assertFieldChecked("edit-card-2-$langcode-2");
158

    
159
    // Submit form: select only first option.
160
    $edit = array(
161
      "card_2[$langcode][0]" => TRUE,
162
      "card_2[$langcode][1]" => FALSE,
163
      "card_2[$langcode][2]" => FALSE,
164
    );
165
    $this->drupalPost(NULL, $edit, t('Save'));
166
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
167

    
168
    // Display form: check that the right options are selected.
169
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
170
    $this->assertFieldChecked("edit-card-2-$langcode-0");
171
    $this->assertNoFieldChecked("edit-card-2-$langcode-1");
172
    $this->assertNoFieldChecked("edit-card-2-$langcode-2");
173

    
174
    // Submit form: select the three options while the field accepts only 2.
175
    $edit = array(
176
      "card_2[$langcode][0]" => TRUE,
177
      "card_2[$langcode][1]" => TRUE,
178
      "card_2[$langcode][2]" => TRUE,
179
    );
180
    $this->drupalPost(NULL, $edit, t('Save'));
181
    $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
182

    
183
    // Submit form: uncheck all options.
184
    $edit = array(
185
      "card_2[$langcode][0]" => FALSE,
186
      "card_2[$langcode][1]" => FALSE,
187
      "card_2[$langcode][2]" => FALSE,
188
    );
189
    $this->drupalPost(NULL, $edit, t('Save'));
190
    // Check that the value was saved.
191
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
192

    
193
    // Required checkbox with one option is auto-selected.
194
    $this->card_2['settings']['allowed_values'] = array(99 => 'Only allowed value');
195
    field_update_field($this->card_2);
196
    $instance['required'] = TRUE;
197
    field_update_instance($instance);
198
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
199
    $this->assertFieldChecked("edit-card-2-$langcode-99");
200
  }
201

    
202
  /**
203
   * Tests the 'options_select' widget (single select).
204
   */
205
  function testSelectListSingle() {
206
    // Create an instance of the 'single value' field.
207
    $instance = array(
208
      'field_name' => $this->card_1['field_name'],
209
      'entity_type' => 'test_entity',
210
      'bundle' => 'test_bundle',
211
      'required' => TRUE,
212
      'widget' => array(
213
        'type' => 'options_select',
214
      ),
215
    );
216
    $instance = field_create_instance($instance);
217
    $langcode = LANGUAGE_NONE;
218

    
219
    // Create an entity.
220
    $entity_init = field_test_create_stub_entity();
221
    $entity = clone $entity_init;
222
    $entity->is_new = TRUE;
223
    field_test_entity_save($entity);
224

    
225
    // Display form.
226
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
227
    // A required field without any value has a "none" option.
228
    $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- Select a value -'))), 'A required select list has a "Select a value" choice.');
229

    
230
    // With no field data, nothing is selected.
231
    $this->assertNoOptionSelected("edit-card-1-$langcode", '_none');
232
    $this->assertNoOptionSelected("edit-card-1-$langcode", 0);
233
    $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
234
    $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
235
    $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
236

    
237
    // Submit form: select invalid 'none' option.
238
    $edit = array("card_1[$langcode]" => '_none');
239
    $this->drupalPost(NULL, $edit, t('Save'));
240
    $this->assertRaw(t('!title field is required.', array('!title' => $instance['field_name'])), 'Cannot save a required field when selecting "none" from the select list.');
241

    
242
    // Submit form: select first option.
243
    $edit = array("card_1[$langcode]" => 0);
244
    $this->drupalPost(NULL, $edit, t('Save'));
245
    $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
246

    
247
    // Display form: check that the right options are selected.
248
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
249
    // A required field with a value has no 'none' option.
250
    $this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1-' . $langcode)), 'A required select list with an actual value has no "none" choice.');
251
    $this->assertOptionSelected("edit-card-1-$langcode", 0);
252
    $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
253
    $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
254

    
255
    // Make the field non required.
256
    $instance['required'] = FALSE;
257
    field_update_instance($instance);
258

    
259
    // Display form.
260
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
261
    // A non-required field has a 'none' option.
262
    $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- None -'))), 'A non-required select list has a "None" choice.');
263
    // Submit form: Unselect the option.
264
    $edit = array("card_1[$langcode]" => '_none');
265
    $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
266
    $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
267

    
268
    // Test optgroups.
269

    
270
    $this->card_1['settings']['allowed_values'] = array();
271
    $this->card_1['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
272
    field_update_field($this->card_1);
273

    
274
    // Display form: with no field data, nothing is selected
275
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
276
    $this->assertNoOptionSelected("edit-card-1-$langcode", 0);
277
    $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
278
    $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
279
    $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
280
    $this->assertRaw('Group 1', 'Option groups are displayed.');
281

    
282
    // Submit form: select first option.
283
    $edit = array("card_1[$langcode]" => 0);
284
    $this->drupalPost(NULL, $edit, t('Save'));
285
    $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
286

    
287
    // Display form: check that the right options are selected.
288
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
289
    $this->assertOptionSelected("edit-card-1-$langcode", 0);
290
    $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
291
    $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
292

    
293
    // Submit form: Unselect the option.
294
    $edit = array("card_1[$langcode]" => '_none');
295
    $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
296
    $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
297
  }
298

    
299
  /**
300
   * Tests the 'options_select' widget (multiple select).
301
   */
302
  function testSelectListMultiple() {
303
    // Create an instance of the 'multiple values' field.
304
    $instance = array(
305
      'field_name' => $this->card_2['field_name'],
306
      'entity_type' => 'test_entity',
307
      'bundle' => 'test_bundle',
308
      'widget' => array(
309
        'type' => 'options_select',
310
      ),
311
    );
312
    $instance = field_create_instance($instance);
313
    $langcode = LANGUAGE_NONE;
314

    
315
    // Create an entity.
316
    $entity_init = field_test_create_stub_entity();
317
    $entity = clone $entity_init;
318
    $entity->is_new = TRUE;
319
    field_test_entity_save($entity);
320

    
321
    // Display form: with no field data, nothing is selected.
322
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
323
    $this->assertNoOptionSelected("edit-card-2-$langcode", 0);
324
    $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
325
    $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
326
    $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
327

    
328
    // Submit form: select first and third options.
329
    $edit = array("card_2[$langcode][]" => array(0 => 0, 2 => 2));
330
    $this->drupalPost(NULL, $edit, t('Save'));
331
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
332

    
333
    // Display form: check that the right options are selected.
334
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
335
    $this->assertOptionSelected("edit-card-2-$langcode", 0);
336
    $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
337
    $this->assertOptionSelected("edit-card-2-$langcode", 2);
338

    
339
    // Submit form: select only first option.
340
    $edit = array("card_2[$langcode][]" => array(0 => 0));
341
    $this->drupalPost(NULL, $edit, t('Save'));
342
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
343

    
344
    // Display form: check that the right options are selected.
345
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
346
    $this->assertOptionSelected("edit-card-2-$langcode", 0);
347
    $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
348
    $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
349

    
350
    // Submit form: select the three options while the field accepts only 2.
351
    $edit = array("card_2[$langcode][]" => array(0 => 0, 1 => 1, 2 => 2));
352
    $this->drupalPost(NULL, $edit, t('Save'));
353
    $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
354

    
355
    // Submit form: uncheck all options.
356
    $edit = array("card_2[$langcode][]" => array());
357
    $this->drupalPost(NULL, $edit, t('Save'));
358
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
359

    
360
    // Test the 'None' option.
361

    
362
    // Check that the 'none' option has no effect if actual options are selected
363
    // as well.
364
    $edit = array("card_2[$langcode][]" => array('_none' => '_none', 0 => 0));
365
    $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
366
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
367

    
368
    // Check that selecting the 'none' option empties the field.
369
    $edit = array("card_2[$langcode][]" => array('_none' => '_none'));
370
    $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
371
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
372

    
373
    // A required select list does not have an empty key.
374
    $instance['required'] = TRUE;
375
    field_update_instance($instance);
376
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
377
    $this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2-' . $langcode)), 'A required select list does not have an empty key.');
378

    
379
    // We do not have to test that a required select list with one option is
380
    // auto-selected because the browser does it for us.
381

    
382
    // Test optgroups.
383

    
384
    // Use a callback function defining optgroups.
385
    $this->card_2['settings']['allowed_values'] = array();
386
    $this->card_2['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
387
    field_update_field($this->card_2);
388
    $instance['required'] = FALSE;
389
    field_update_instance($instance);
390

    
391
    // Display form: with no field data, nothing is selected.
392
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
393
    $this->assertNoOptionSelected("edit-card-2-$langcode", 0);
394
    $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
395
    $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
396
    $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
397
    $this->assertRaw('Group 1', 'Option groups are displayed.');
398

    
399
    // Submit form: select first option.
400
    $edit = array("card_2[$langcode][]" => array(0 => 0));
401
    $this->drupalPost(NULL, $edit, t('Save'));
402
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
403

    
404
    // Display form: check that the right options are selected.
405
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
406
    $this->assertOptionSelected("edit-card-2-$langcode", 0);
407
    $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
408
    $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
409

    
410
    // Submit form: Unselect the option.
411
    $edit = array("card_2[$langcode][]" => array('_none' => '_none'));
412
    $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
413
    $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
414
  }
415

    
416
  /**
417
   * Tests the 'options_onoff' widget.
418
   */
419
  function testOnOffCheckbox() {
420
    // Create an instance of the 'boolean' field.
421
    $instance = array(
422
      'field_name' => $this->bool['field_name'],
423
      'entity_type' => 'test_entity',
424
      'bundle' => 'test_bundle',
425
      'widget' => array(
426
        'type' => 'options_onoff',
427
      ),
428
    );
429
    $instance = field_create_instance($instance);
430
    $langcode = LANGUAGE_NONE;
431

    
432
    // Create an entity.
433
    $entity_init = field_test_create_stub_entity();
434
    $entity = clone $entity_init;
435
    $entity->is_new = TRUE;
436
    field_test_entity_save($entity);
437

    
438
    // Display form: with no field data, option is unchecked.
439
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
440
    $this->assertNoFieldChecked("edit-bool-$langcode");
441
    $this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
442

    
443
    // Submit form: check the option.
444
    $edit = array("bool[$langcode]" => TRUE);
445
    $this->drupalPost(NULL, $edit, t('Save'));
446
    $this->assertFieldValues($entity_init, 'bool', $langcode, array(0));
447

    
448
    // Display form: check that the right options are selected.
449
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
450
    $this->assertFieldChecked("edit-bool-$langcode");
451

    
452
    // Submit form: uncheck the option.
453
    $edit = array("bool[$langcode]" => FALSE);
454
    $this->drupalPost(NULL, $edit, t('Save'));
455
    $this->assertFieldValues($entity_init, 'bool', $langcode, array(1));
456

    
457
    // Display form: with 'off' value, option is unchecked.
458
    $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
459
    $this->assertNoFieldChecked("edit-bool-$langcode");
460

    
461
    // Create admin user.
462
    $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy'));
463
    $this->drupalLogin($admin_user);
464

    
465
    // Create a test field instance.
466
    $fieldUpdate = $this->bool;
467
    $fieldUpdate['settings']['allowed_values'] = array(0 => 0, 1 => 'MyOnValue');
468
    field_update_field($fieldUpdate);
469
    $instance = array(
470
      'field_name' => $this->bool['field_name'],
471
      'entity_type' => 'node',
472
      'bundle' => 'page',
473
      'widget' => array(
474
            'type' => 'options_onoff',
475
            'module' => 'options',
476
        ),
477
    );
478
    field_create_instance($instance);
479

    
480
    // Go to the edit page and check if the default settings works as expected
481
    $fieldEditUrl = 'admin/structure/types/manage/page/fields/bool';
482
    $this->drupalGet($fieldEditUrl);
483

    
484
    $this->assertText(
485
      'Use field label instead of the "On value" as label ',
486
      'Display setting checkbox available.'
487
    );
488

    
489
    $this->assertFieldByXPath(
490
      '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="MyOnValue "]',
491
      TRUE,
492
      'Default case shows "On value"'
493
    );
494

    
495
    // Enable setting
496
    $edit = array('instance[widget][settings][display_label]' => 1);
497
    // Save the new Settings
498
    $this->drupalPost($fieldEditUrl, $edit, t('Save settings'));
499

    
500
    // Go again to the edit page and check if the setting
501
    // is stored and has the expected effect
502
    $this->drupalGet($fieldEditUrl);
503
    $this->assertText(
504
      'Use field label instead of the "On value" as label ',
505
      'Display setting checkbox is available'
506
    );
507
    $this->assertFieldChecked(
508
      'edit-instance-widget-settings-display-label',
509
      'Display settings checkbox checked'
510
    );
511
    $this->assertFieldByXPath(
512
      '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="' . $this->bool['field_name'] . ' "]',
513
      TRUE,
514
      'Display label changes label of the checkbox'
515
    );
516
  }
517
}
518

    
519
/**
520
 * Test an options select on a list field with a dynamic allowed values function.
521
 */
522
class OptionsSelectDynamicValuesTestCase extends ListDynamicValuesTestCase {
523
  public static function getInfo() {
524
    return array(
525
      'name' => 'Options select dynamic values',
526
      'description' => 'Test an options select on a list field with a dynamic allowed values function.',
527
      'group' => 'Field types',
528
    );
529
  }
530

    
531
  /**
532
   * Tests the 'options_select' widget (single select).
533
   */
534
  function testSelectListDynamic() {
535
    // Create an entity.
536
    $this->entity->is_new = TRUE;
537
    field_test_entity_save($this->entity);
538
    // Create a web user.
539
    $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
540
    $this->drupalLogin($web_user);
541

    
542
    // Display form.
543
    $this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit');
544
    $options = $this->xpath('//select[@id="edit-test-list-und"]/option');
545
    $this->assertEqual(count($options), count($this->test) + 1);
546
    foreach ($options as $option) {
547
      $value = (string) $option['value'];
548
      if ($value != '_none') {
549
        $this->assertTrue(array_search($value, $this->test));
550
      }
551
    }
552
  }
553
}