OptionsFieldUITest.php 11.5 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Definition of Drupal\options\Tests\OptionsFieldUITest.
6 7
 */

8
namespace Drupal\options\Tests;
9

10
use Drupal\field\Tests\FieldTestBase;
11

12
/**
13
 * Options module UI tests.
14
 */
15
class OptionsFieldUITest extends FieldTestBase {
16 17 18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('options', 'field_test', 'taxonomy', 'field_ui');

24
  public static function getInfo() {
25
    return array(
26 27
      'name' => 'Options field UI',
      'description' => 'Test the Options field UI functionality.',
28 29 30
      'group' => 'Field types',
    );
  }
31

32
  function setUp() {
33
    parent::setUp();
34 35

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

    // Create content type, with underscores.
40
    $type_name = 'test_' . strtolower($this->randomName());
41 42
    $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
    $this->type = $type->type;
43 44 45
  }

  /**
46
   * Options (integer) : test 'allowed values' input.
47
   */
48 49 50
  function testOptionsAllowedValuesInteger() {
    $this->field_name = 'field_options_integer';
    $this->createOptionsField('list_integer');
51 52 53 54

    // Flat list of textual values.
    $string = "Zero\nOne";
    $array = array('0' => 'Zero', '1' => 'One');
55
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
56 57 58
    // Explicit integer keys.
    $string = "0|Zero\n2|Two";
    $array = array('0' => 'Zero', '2' => 'Two');
59
    $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
60 61 62
    // Check that values can be added and removed.
    $string = "0|Zero\n1|One";
    $array = array('0' => 'Zero', '1' => 'One');
63
    $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
64
    // Non-integer keys.
65 66
    $this->assertAllowedValuesInput("1.1|One", 'keys must be integers', 'Non integer keys are rejected.');
    $this->assertAllowedValuesInput("abc|abc", 'keys must be integers', 'Non integer keys are rejected.');
67
    // Mixed list of keyed and unkeyed values.
68
    $this->assertAllowedValuesInput("Zero\n1|One", 'invalid input', 'Mixed lists are rejected.');
69 70 71 72

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

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

    // 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');
83
    $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
84 85
    $string = "0|Zero\n1|One";
    $array = array('0' => 'Zero', '1' => 'One');
86 87
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
    $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
88 89 90 91 92

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

  /**
97
   * Options (float) : test 'allowed values' input.
98
   */
99 100 101
  function testOptionsAllowedValuesFloat() {
    $this->field_name = 'field_options_float';
    $this->createOptionsField('list_float');
102 103 104 105

    // Flat list of textual values.
    $string = "Zero\nOne";
    $array = array('0' => 'Zero', '1' => 'One');
106
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
107 108 109
    // Explicit numeric keys.
    $string = "0|Zero\n.5|Point five";
    $array = array('0' => 'Zero', '0.5' => 'Point five');
110
    $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
111 112 113
    // 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');
114
    $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
115
    // Non-numeric keys.
116
    $this->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', 'Non numeric keys are rejected.');
117
    // Mixed list of keyed and unkeyed values.
118
    $this->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', 'Mixed lists are rejected.');
119 120 121 122

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

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

    // 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');
133
    $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
134 135
    $string = "0|Zero\n.5|Point five";
    $array = array('0' => 'Zero', '0.5' => 'Point five');
136 137
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
    $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
138

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

146
  /**
147
   * Options (text) : test 'allowed values' input.
148
   */
149 150 151
  function testOptionsAllowedValuesText() {
    $this->field_name = 'field_options_text';
    $this->createOptionsField('list_text');
152 153 154 155

    // Flat list of textual values.
    $string = "Zero\nOne";
    $array = array('Zero' => 'Zero', 'One' => 'One');
156
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
157 158 159
    // Explicit keys.
    $string = "zero|Zero\none|One";
    $array = array('zero' => 'Zero', 'one' => 'One');
160
    $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted.');
161 162 163
    // Check that values can be added and removed.
    $string = "zero|Zero\ntwo|Two";
    $array = array('zero' => 'Zero', 'two' => 'Two');
164
    $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
165 166 167
    // Mixed list of keyed and unkeyed values.
    $string = "zero|Zero\nOne\n";
    $array = array('zero' => 'Zero', 'One' => 'One');
168
    $this->assertAllowedValuesInput($string, $array, 'Mixed lists are accepted.');
169
    // Overly long keys.
170
    $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.');
171 172 173 174

    // Create a node with actual data for the field.
    $settings = array(
      'type' => $this->type,
175
      $this->field_name => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 'One'))),
176 177 178 179 180 181 182
    );
    $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');
183
    $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are still accepted once the field has data.');
184 185 186 187

    // 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');
188
    $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
189 190
    $string = "Zero\nOne";
    $array = array('Zero' => 'Zero', 'One' => 'One');
191 192
    $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
    $this->assertAllowedValuesInput("Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
193 194 195 196 197

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

  /**
202
   * Options (boolean) : test 'On/Off' values input.
203
   */
204 205 206
  function testOptionsAllowedValuesBoolean() {
    $this->field_name = 'field_options_boolean';
    $this->createOptionsField('list_boolean');
207

208
    // Check that the separate 'On' and 'Off' form fields work.
209 210
    $on = $this->randomName();
    $off = $this->randomName();
211
    $allowed_values = array(1 => $on, 0 => $off);
212 213 214 215
    $edit = array(
      'on' => $on,
      'off' => $off,
    );
216 217
    $this->drupalPost($this->admin_path, $edit, t('Save field settings'));
    $this->assertRaw(t('Updated field %label field settings.', array('%label' => $this->field_name)));
218
    // Test the allowed_values on the field settings form.
219
    $this->drupalGet($this->admin_path);
220 221
    $this->assertFieldByName('on', $on, t("The 'On' value is stored correctly."));
    $this->assertFieldByName('off', $off, t("The 'Off' value is stored correctly."));
222
    $field = field_info_field($this->field_name);
223 224 225
    $this->assertEqual($field['settings']['allowed_values'], $allowed_values, 'The allowed value is correct');
    $this->assertFalse(isset($field['settings']['on']), 'The on value is not saved into settings');
    $this->assertFalse(isset($field['settings']['off']), 'The off value is not saved into settings');
226
  }
227

228 229 230 231 232 233 234 235 236 237 238 239 240
  /**
   * Options (text) : test 'trimmed values' input.
   */
  function testOptionsTrimmedValuesText() {
    $this->field_name = 'field_options_trimmed_text';
    $this->createOptionsField('list_text');

    // Explicit keys.
    $string = "zero |Zero\none | One";
    $array = array('zero' => 'Zero', 'one' => 'One');
    $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted and trimmed.');
  }

241
  /**
242
   * Helper function to create list field of a given type.
243
   *
244
   * @param string $type
245
   *   'list_integer', 'list_float', 'list_text' or 'list_boolean'
246
   */
247
  protected function createOptionsField($type) {
248 249
    // Create a test field and instance.
    $field = array(
250
      'field_name' => $this->field_name,
251 252 253 254
      'type' => $type,
    );
    field_create_field($field);
    $instance = array(
255
      'field_name' => $this->field_name,
256
      'entity_type' => 'node',
257 258 259 260
      'bundle' => $this->type,
    );
    field_create_instance($instance);

261
    $this->admin_path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $this->field_name . '/field-settings';
262
  }
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277
  /**
   * 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);
278
    $this->drupalPost($this->admin_path, $edit, t('Save field settings'));
279 280 281 282 283 284 285 286 287 288

    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);
    }
  }
289
}