summaryrefslogtreecommitdiff
path: root/modules/field/modules/list/tests/list.test
blob: 941d2b4cb02d8df66bb4e0cc36e4e2ec06f40adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php

/**
 * @file
 * Tests for list.module.
 */

/**
 * Tests for the 'List' field types.
 */
class ListFieldTestCase extends FieldTestCase {
  public static function getInfo() {
    return array(
      'name' => 'List field',
      'description' => 'Test the List field type.',
      'group' => 'Field types',
    );
  }

  function setUp() {
    parent::setUp('field_test');

    $this->field_name = 'test_list';
    $this->field = array(
      'field_name' => $this->field_name,
      'type' => 'list_integer',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'),
      ),
    );
    $this->field = field_create_field($this->field);

    $this->instance = array(
      'field_name' => $this->field_name,
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'widget' => array(
        'type' => 'options_buttons',
      ),
    );
    $this->instance = field_create_instance($this->instance);
  }

  /**
   * Test that allowed values can be updated.
   */
  function testUpdateAllowedValues() {
    $langcode = LANGUAGE_NONE;

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

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

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

    // Options are reset when a new field with the same name is created.
    field_delete_field($this->field_name);
    unset($this->field['id']);
    $this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three');
    $this->field = field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'widget' => array(
        'type' => 'options_buttons',
      ),
    );
    $this->instance = field_create_instance($this->instance);
    $entity = field_test_create_stub_entity();
    $form = drupal_get_form('field_test_entity_form', $entity);
    $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), t('Option 1 exists'));
    $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
    $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), t('Option 3 exists'));
  }
}

/**
 * List module UI tests.
 */
class ListFieldUITestCase extends FieldTestCase {
  public static function getInfo() {
    return array(
      'name' => 'List field UI',
      'description' => 'Test the List field UI functionality.',
      'group' => 'Field types',
    );
  }

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

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

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

  /**
   * List (integer) : test 'allowed values' input.
   */
  function testListAllowedValuesInteger() {
    $this->field_name = 'field_list_integer';
    $this->createListField('list_integer');

    // Flat list of textual values.
    $string = "Zero\nOne";
    $array = array('0' => 'Zero', '1' => 'One');
    $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
    // Explicit integer keys.
    $string = "0|Zero\n2|Two";
    $array = array('0' => 'Zero', '2' => 'Two');
    $this->assertAllowedValuesInput($string, $array, t('Integer keys are accepted.'));
    // Check that values can be added and removed.
    $string = "0|Zero\n1|One";
    $array = array('0' => 'Zero', '1' => 'One');
    $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
    // Non-integer keys.
    $this->assertAllowedValuesInput("1.1|One", 'keys must be integers', t('Non integer keys are rejected.'));
    $this->assertAllowedValuesInput("abc|abc", 'keys must be integers', t('Non integer keys are rejected.'));
    // Mixed list of keyed and unkeyed values.
    $this->assertAllowedValuesInput("Zero\n1|One", 'invalid input', t('Mixed lists are rejected.'));

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

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

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

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

  /**
   * List (float) : test 'allowed values' input.
   */
  function testListAllowedValuesFloat() {
    $this->field_name = 'field_list_float';
    $this->createListField('list_float');

    // Flat list of textual values.
    $string = "Zero\nOne";
    $array = array('0' => 'Zero', '1' => 'One');
    $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
    // Explicit numeric keys.
    $string = "0|Zero\n.5|Point five";
    $array = array('0' => 'Zero', '0.5' => 'Point five');
    $this->assertAllowedValuesInput($string, $array, t('Integer keys are accepted.'));
    // Check that values can be added and removed.
    $string = "0|Zero\n.5|Point five\n1.0|One";
    $array = array('0' => 'Zero', '0.5' => 'Point five', '1' => 'One');
    $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
    // Non-numeric keys.
    $this->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', t('Non numeric keys are rejected.'));
    // Mixed list of keyed and unkeyed values.
    $this->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', t('Mixed lists are rejected.'));

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

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

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

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

  /**
   * List (text) : test 'allowed values' input.
   */
  function testListAllowedValuesText() {
    $this->field_name = 'field_list_text';
    $this->createListField('list_text');

    // Flat list of textual values.
    $string = "Zero\nOne";
    $array = array('Zero' => 'Zero', 'One' => 'One');
    $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
    // Explicit keys.
    $string = "zero|Zero\none|One";
    $array = array('zero' => 'Zero', 'one' => 'One');
    $this->assertAllowedValuesInput($string, $array, t('Explicit keys are accepted.'));
    // Check that values can be added and removed.
    $string = "zero|Zero\ntwo|Two";
    $array = array('zero' => 'Zero', 'two' => 'Two');
    $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
    // Mixed list of keyed and unkeyed values.
    $string = "zero|Zero\nOne\n";
    $array = array('zero' => 'Zero', 'One' => 'One');
    $this->assertAllowedValuesInput($string, $array, t('Mixed lists are accepted.'));
    // Overly long keys.
    $this->assertAllowedValuesInput("zero|Zero\n" . $this->randomName(256) . "|One", 'each key must be a string at most 255 characters long', t('Overly long keys are rejected.'));

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

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

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

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

  /**
   * List (boolen) : test 'On/Off' values input.
   */
  function testListAllowedValuesBoolean() {
    $this->field_name = 'field_list_boolean';
    $this->createListField('list_boolean');

    // Check that the seperate 'On' and 'Off' form fields work.
    $on = $this->randomName();
    $off = $this->randomName();
    $allowed_values = array(1 => $on, 0 => $off);
    $edit = array(
      'on' => $on,
      'off' => $off,
    );
    $this->drupalPost($this->admin_path, $edit, t('Save settings'));
    $this->assertText("Saved field_list_boolean configuration.", t("The 'On' and 'Off' form fields work for boolean fields."));
    // Test the allowed_values on the field settings form.
    $this->drupalGet($this->admin_path);
    $this->assertFieldByName('on', $on, t("The 'On' value is stored correctly."));
    $this->assertFieldByName('off', $off, t("The 'Off' value is stored correctly."));
    $field = field_info_field($this->field_name);
    $this->assertEqual($field['settings']['allowed_values'], $allowed_values, t('The allowed value is correct'));
    $this->assertFalse(isset($field['settings']['on']), t('The on value is not saved into settings'));
    $this->assertFalse(isset($field['settings']['off']), t('The off value is not saved into settings'));
  }

  /**
   * Helper function to create list field of a given type.
   *
   * @param string $type
   *   'list_integer', 'list_float', 'list_text' or 'list_boolean'
   */
  protected function createListField($type) {
    // Create a test field and instance.
    $field = array(
      'field_name' => $this->field_name,
      'type' => $type,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $this->field_name,
      'entity_type' => 'node',
      'bundle' => $this->type,
    );
    field_create_instance($instance);

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

  /**
   * Tests a string input for the 'allowed values' form element.
   *
   * @param $input_string
   *   The input string, in the pipe-linefeed format expected by the form
   *   element.
   * @param $result
   *   Either an expected resulting array in
   *   $field['settings']['allowed_values'], or an expected error message.
   * @param $message
   *   Message to display.
   */
  function assertAllowedValuesInput($input_string, $result, $message) {
    $edit = array('field[settings][allowed_values]' => $input_string);
    $this->drupalPost($this->admin_path, $edit, t('Save settings'));

    if (is_string($result)) {
      $this->assertText($result, $message);
    }
    else {
      field_info_cache_clear();
      $field = field_info_field($this->field_name);
      $this->assertIdentical($field['settings']['allowed_values'], $result, $message);
    }
  }
}