Project

General

Profile

Paste
Download (17.8 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / field / modules / list / tests / list.test @ b0dc3a2e

1
<?php
2

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

    
8
/**
9
 * Tests for the 'List' field types.
10
 */
11
class ListFieldTestCase extends FieldTestCase {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'List field',
15
      'description' => 'Test the List field type.',
16
      'group' => 'Field types',
17
    );
18
  }
19

    
20
  function setUp() {
21
    parent::setUp('field_test');
22

    
23
    $this->field_name = 'test_list';
24
    $this->field = array(
25
      'field_name' => $this->field_name,
26
      'type' => 'list_integer',
27
      'cardinality' => 1,
28
      'settings' => array(
29
        'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'),
30
      ),
31
    );
32
    $this->field = field_create_field($this->field);
33

    
34
    $this->instance = array(
35
      'field_name' => $this->field_name,
36
      'entity_type' => 'test_entity',
37
      'bundle' => 'test_bundle',
38
      'widget' => array(
39
        'type' => 'options_buttons',
40
      ),
41
    );
42
    $this->instance = field_create_instance($this->instance);
43
  }
44

    
45
  /**
46
   * Test that allowed values can be updated.
47
   */
48
  function testUpdateAllowedValues() {
49
    $langcode = LANGUAGE_NONE;
50

    
51
    // All three options appear.
52
    $entity = field_test_create_stub_entity();
53
    $form = drupal_get_form('field_test_entity_form', $entity);
54
    $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists');
55
    $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
56
    $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), 'Option 3 exists');
57

    
58
    // Use one of the values in an actual entity, and check that this value
59
    // cannot be removed from the list.
60
    $entity = field_test_create_stub_entity();
61
    $entity->{$this->field_name}[$langcode][0] = array('value' => 1);
62
    field_test_entity_save($entity);
63
    $this->field['settings']['allowed_values'] = array(2 => 'Two');
64
    try {
65
      field_update_field($this->field);
66
      $this->fail(t('Cannot update a list field to not include keys with existing data.'));
67
    }
68
    catch (FieldException $e) {
69
      $this->pass(t('Cannot update a list field to not include keys with existing data.'));
70
    }
71
    // Empty the value, so that we can actually remove the option.
72
    $entity->{$this->field_name}[$langcode] = array();
73
    field_test_entity_save($entity);
74

    
75
    // Removed options do not appear.
76
    $this->field['settings']['allowed_values'] = array(2 => 'Two');
77
    field_update_field($this->field);
78
    $entity = field_test_create_stub_entity();
79
    $form = drupal_get_form('field_test_entity_form', $entity);
80
    $this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist');
81
    $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
82
    $this->assertTrue(empty($form[$this->field_name][$langcode][3]), 'Option 3 does not exist');
83

    
84
    // Completely new options appear.
85
    $this->field['settings']['allowed_values'] = array(10 => 'Update', 20 => 'Twenty');
86
    field_update_field($this->field);
87
    $form = drupal_get_form('field_test_entity_form', $entity);
88
    $this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist');
89
    $this->assertTrue(empty($form[$this->field_name][$langcode][2]), 'Option 2 does not exist');
90
    $this->assertTrue(empty($form[$this->field_name][$langcode][3]), 'Option 3 does not exist');
91
    $this->assertTrue(!empty($form[$this->field_name][$langcode][10]), 'Option 10 exists');
92
    $this->assertTrue(!empty($form[$this->field_name][$langcode][20]), 'Option 20 exists');
93

    
94
    // Options are reset when a new field with the same name is created.
95
    field_delete_field($this->field_name);
96
    unset($this->field['id']);
97
    $this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three');
98
    $this->field = field_create_field($this->field);
99
    $this->instance = array(
100
      'field_name' => $this->field_name,
101
      'entity_type' => 'test_entity',
102
      'bundle' => 'test_bundle',
103
      'widget' => array(
104
        'type' => 'options_buttons',
105
      ),
106
    );
107
    $this->instance = field_create_instance($this->instance);
108
    $entity = field_test_create_stub_entity();
109
    $form = drupal_get_form('field_test_entity_form', $entity);
110
    $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists');
111
    $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
112
    $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), 'Option 3 exists');
113
  }
114
}
115

    
116
/**
117
 * Sets up a List field for testing allowed values functions.
118
 */
119
class ListDynamicValuesTestCase extends FieldTestCase {
120
  function setUp() {
121
    parent::setUp(array('list', 'field_test', 'list_test'));
122

    
123
    $this->field_name = 'test_list';
124
    $this->field = array(
125
      'field_name' => $this->field_name,
126
      'type' => 'list_text',
127
      'cardinality' => 1,
128
      'settings' => array(
129
        'allowed_values_function' => 'list_test_dynamic_values_callback',
130
      ),
131
    );
132
    $this->field = field_create_field($this->field);
133

    
134
    $this->instance = array(
135
      'field_name' => $this->field_name,
136
      'entity_type' => 'test_entity',
137
      'bundle' => 'test_bundle',
138
      'required' => TRUE,
139
      'widget' => array(
140
        'type' => 'options_select',
141
      ),
142
    );
143
    $this->instance = field_create_instance($this->instance);
144
    $this->test = array(
145
      'id' => mt_rand(1, 10),
146
      // Make sure this does not equal the ID so that
147
      // list_test_dynamic_values_callback() always returns 4 values.
148
      'vid' => mt_rand(20, 30),
149
      'bundle' => 'test_bundle',
150
      'label' => $this->randomName(),
151
    );
152
    $this->entity = call_user_func_array('field_test_create_stub_entity', $this->test);
153
  }
154
}
155

    
156
/**
157
 * Tests the List field allowed values function.
158
 */
159
class ListDynamicValuesValidationTestCase extends ListDynamicValuesTestCase {
160
  public static function getInfo() {
161
    return array(
162
      'name' => 'List field dynamic values',
163
      'description' => 'Test the List field allowed values function.',
164
      'group' => 'Field types',
165
    );
166
  }
167

    
168
  /**
169
   * Test that allowed values function gets the entity.
170
   */
171
  function testDynamicAllowedValues() {
172
    // Verify that the test passes against every value we had.
173
    foreach ($this->test as $key => $value) {
174
      $this->entity->test_list[LANGUAGE_NONE][0]['value'] = $value;
175
      try {
176
        field_attach_validate('test_entity', $this->entity);
177
        $this->pass("$key should pass");
178
      }
179
      catch (FieldValidationException $e) {
180
        // This will display as an exception, no need for a separate error.
181
        throw($e);
182
      }
183
    }
184
    // Now verify that the test does not pass against anything else.
185
    foreach ($this->test as $key => $value) {
186
      $this->entity->test_list[LANGUAGE_NONE][0]['value'] = is_numeric($value) ? (100 - $value) : ('X' . $value);
187
      $pass = FALSE;
188
      try {
189
        field_attach_validate('test_entity', $this->entity);
190
      }
191
      catch (FieldValidationException $e) {
192
        $pass = TRUE;
193
      }
194
      $this->assertTrue($pass, $key . ' should not pass');
195
    }
196
  }
197
}
198

    
199
/**
200
 * List module UI tests.
201
 */
202
class ListFieldUITestCase extends FieldTestCase {
203
  public static function getInfo() {
204
    return array(
205
      'name' => 'List field UI',
206
      'description' => 'Test the List field UI functionality.',
207
      'group' => 'Field types',
208
    );
209
  }
210

    
211
  function setUp() {
212
    parent::setUp('field_test', 'field_ui');
213

    
214
    // Create test user.
215
    $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy', 'administer fields'));
216
    $this->drupalLogin($admin_user);
217

    
218
    // Create content type, with underscores.
219
    $type_name = 'test_' . strtolower($this->randomName());
220
    $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
221
    $this->type = $type->type;
222
    // Store a valid URL name, with hyphens instead of underscores.
223
    $this->hyphen_type = str_replace('_', '-', $this->type);
224
  }
225

    
226
  /**
227
   * List (integer) : test 'allowed values' input.
228
   */
229
  function testListAllowedValuesInteger() {
230
    $this->field_name = 'field_list_integer';
231
    $this->createListField('list_integer');
232

    
233
    // Flat list of textual values.
234
    $string = "Zero\nOne";
235
    $array = array('0' => 'Zero', '1' => 'One');
236
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
237
    // Explicit integer keys.
238
    $string = "0|Zero\n2|Two";
239
    $array = array('0' => 'Zero', '2' => 'Two');
240
    $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
241
    // Check that values can be added and removed.
242
    $string = "0|Zero\n1|One";
243
    $array = array('0' => 'Zero', '1' => 'One');
244
    $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
245
    // Non-integer keys.
246
    $this->assertAllowedValuesInput("1.1|One", 'keys must be integers', 'Non integer keys are rejected.');
247
    $this->assertAllowedValuesInput("abc|abc", 'keys must be integers', 'Non integer keys are rejected.');
248
    // Mixed list of keyed and unkeyed values.
249
    $this->assertAllowedValuesInput("Zero\n1|One", 'invalid input', 'Mixed lists are rejected.');
250

    
251
    // Create a node with actual data for the field.
252
    $settings = array(
253
      'type' => $this->type,
254
      $this->field_name => array(LANGUAGE_NONE => array(array('value' => 1))),
255
    );
256
    $node = $this->drupalCreateNode($settings);
257

    
258
    // Check that a flat list of values is rejected once the field has data.
259
    $this->assertAllowedValuesInput( "Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');
260

    
261
    // Check that values can be added but values in use cannot be removed.
262
    $string = "0|Zero\n1|One\n2|Two";
263
    $array = array('0' => 'Zero', '1' => 'One', '2' => 'Two');
264
    $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
265
    $string = "0|Zero\n1|One";
266
    $array = array('0' => 'Zero', '1' => 'One');
267
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
268
    $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
269

    
270
    // Delete the node, remove the value.
271
    node_delete($node->nid);
272
    $string = "0|Zero";
273
    $array = array('0' => 'Zero');
274
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
275
  }
276

    
277
  /**
278
   * List (float) : test 'allowed values' input.
279
   */
280
  function testListAllowedValuesFloat() {
281
    $this->field_name = 'field_list_float';
282
    $this->createListField('list_float');
283

    
284
    // Flat list of textual values.
285
    $string = "Zero\nOne";
286
    $array = array('0' => 'Zero', '1' => 'One');
287
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
288
    // Explicit numeric keys.
289
    $string = "0|Zero\n.5|Point five";
290
    $array = array('0' => 'Zero', '0.5' => 'Point five');
291
    $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
292
    // Check that values can be added and removed.
293
    $string = "0|Zero\n.5|Point five\n1.0|One";
294
    $array = array('0' => 'Zero', '0.5' => 'Point five', '1' => 'One');
295
    $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
296
    // Non-numeric keys.
297
    $this->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', 'Non numeric keys are rejected.');
298
    // Mixed list of keyed and unkeyed values.
299
    $this->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', 'Mixed lists are rejected.');
300

    
301
    // Create a node with actual data for the field.
302
    $settings = array(
303
      'type' => $this->type,
304
      $this->field_name => array(LANGUAGE_NONE => array(array('value' => .5))),
305
    );
306
    $node = $this->drupalCreateNode($settings);
307

    
308
    // Check that a flat list of values is rejected once the field has data.
309
    $this->assertAllowedValuesInput("Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');
310

    
311
    // Check that values can be added but values in use cannot be removed.
312
    $string = "0|Zero\n.5|Point five\n2|Two";
313
    $array = array('0' => 'Zero', '0.5' => 'Point five', '2' => 'Two');
314
    $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
315
    $string = "0|Zero\n.5|Point five";
316
    $array = array('0' => 'Zero', '0.5' => 'Point five');
317
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
318
    $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
319

    
320
    // Delete the node, remove the value.
321
    node_delete($node->nid);
322
    $string = "0|Zero";
323
    $array = array('0' => 'Zero');
324
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
325
  }
326

    
327
  /**
328
   * List (text) : test 'allowed values' input.
329
   */
330
  function testListAllowedValuesText() {
331
    $this->field_name = 'field_list_text';
332
    $this->createListField('list_text');
333

    
334
    // Flat list of textual values.
335
    $string = "Zero\nOne";
336
    $array = array('Zero' => 'Zero', 'One' => 'One');
337
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
338
    // Explicit keys.
339
    $string = "zero|Zero\none|One";
340
    $array = array('zero' => 'Zero', 'one' => 'One');
341
    $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted.');
342
    // Check that values can be added and removed.
343
    $string = "zero|Zero\ntwo|Two";
344
    $array = array('zero' => 'Zero', 'two' => 'Two');
345
    $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
346
    // Mixed list of keyed and unkeyed values.
347
    $string = "zero|Zero\nOne\n";
348
    $array = array('zero' => 'Zero', 'One' => 'One');
349
    $this->assertAllowedValuesInput($string, $array, 'Mixed lists are accepted.');
350
    // Overly long keys.
351
    $this->assertAllowedValuesInput("zero|Zero\n" . $this->randomName(256) . "|One", 'each key must be a string at most 255 characters long', 'Overly long keys are rejected.');
352

    
353
    // Create a node with actual data for the field.
354
    $settings = array(
355
      'type' => $this->type,
356
      $this->field_name => array(LANGUAGE_NONE => array(array('value' => 'One'))),
357
    );
358
    $node = $this->drupalCreateNode($settings);
359

    
360
    // Check that flat lists of values are still accepted once the field has
361
    // data.
362
    $string = "Zero\nOne";
363
    $array = array('Zero' => 'Zero', 'One' => 'One');
364
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are still accepted once the field has data.');
365

    
366
    // Check that values can be added but values in use cannot be removed.
367
    $string = "Zero\nOne\nTwo";
368
    $array = array('Zero' => 'Zero', 'One' => 'One', 'Two' => 'Two');
369
    $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
370
    $string = "Zero\nOne";
371
    $array = array('Zero' => 'Zero', 'One' => 'One');
372
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
373
    $this->assertAllowedValuesInput("Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
374

    
375
    // Delete the node, remove the value.
376
    node_delete($node->nid);
377
    $string = "Zero";
378
    $array = array('Zero' => 'Zero');
379
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
380
  }
381

    
382
  /**
383
   * List (boolen) : test 'On/Off' values input.
384
   */
385
  function testListAllowedValuesBoolean() {
386
    $this->field_name = 'field_list_boolean';
387
    $this->createListField('list_boolean');
388

    
389
    // Check that the separate 'On' and 'Off' form fields work.
390
    $on = $this->randomName();
391
    $off = $this->randomName();
392
    $allowed_values = array(1 => $on, 0 => $off);
393
    $edit = array(
394
      'on' => $on,
395
      'off' => $off,
396
    );
397
    $this->drupalPost($this->admin_path, $edit, t('Save settings'));
398
    $this->assertText("Saved field_list_boolean configuration.", "The 'On' and 'Off' form fields work for boolean fields.");
399
    // Test the allowed_values on the field settings form.
400
    $this->drupalGet($this->admin_path);
401
    $this->assertFieldByName('on', $on, "The 'On' value is stored correctly.");
402
    $this->assertFieldByName('off', $off, "The 'Off' value is stored correctly.");
403
    $field = field_info_field($this->field_name);
404
    $this->assertEqual($field['settings']['allowed_values'], $allowed_values, 'The allowed value is correct');
405
    $this->assertFalse(isset($field['settings']['on']), 'The on value is not saved into settings');
406
    $this->assertFalse(isset($field['settings']['off']), 'The off value is not saved into settings');
407
  }
408

    
409
  /**
410
   * Helper function to create list field of a given type.
411
   *
412
   * @param string $type
413
   *   'list_integer', 'list_float', 'list_text' or 'list_boolean'
414
   */
415
  protected function createListField($type) {
416
    // Create a test field and instance.
417
    $field = array(
418
      'field_name' => $this->field_name,
419
      'type' => $type,
420
    );
421
    field_create_field($field);
422
    $instance = array(
423
      'field_name' => $this->field_name,
424
      'entity_type' => 'node',
425
      'bundle' => $this->type,
426
    );
427
    field_create_instance($instance);
428

    
429
    $this->admin_path = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields/' . $this->field_name;
430
  }
431

    
432
  /**
433
   * Tests a string input for the 'allowed values' form element.
434
   *
435
   * @param $input_string
436
   *   The input string, in the pipe-linefeed format expected by the form
437
   *   element.
438
   * @param $result
439
   *   Either an expected resulting array in
440
   *   $field['settings']['allowed_values'], or an expected error message.
441
   * @param $message
442
   *   Message to display.
443
   */
444
  function assertAllowedValuesInput($input_string, $result, $message) {
445
    $edit = array('field[settings][allowed_values]' => $input_string);
446
    $this->drupalPost($this->admin_path, $edit, t('Save settings'));
447

    
448
    if (is_string($result)) {
449
      $this->assertText($result, $message);
450
    }
451
    else {
452
      field_info_cache_clear();
453
      $field = field_info_field($this->field_name);
454
      $this->assertIdentical($field['settings']['allowed_values'], $result, $message);
455
    }
456
  }
457
}