list.test 7.18 KB
Newer Older
1
2
3
<?php
// $Id$

4
5
6
7
8
9
10
11
12
/**
 * @file
 * Tests for the 'List' field types.
 */

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

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

24
    $this->field_name = 'test_list';
25
26
    $this->field = array(
      'field_name' => $this->field_name,
27
28
29
30
31
32
      'type' => 'list',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => "1|One\n2|Two\n3|Three\n",
      ),
    );
33
    $this->field = field_create_field($this->field);
34

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

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

52
    // All three options appear.
53
    $entity = field_test_create_stub_entity();
54
    $form = drupal_get_form('field_test_entity_form', $entity);
55
56
57
    $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'));
58
59

    // Removed options do not appear.
60
61
    $this->field['settings']['allowed_values'] = "2|Two";
    field_update_field($this->field);
62
    $entity = field_test_create_stub_entity();
63
    $form = drupal_get_form('field_test_entity_form', $entity);
64
65
66
    $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'));
67
68

    // Completely new options appear.
69
70
    $this->field['settings']['allowed_values'] = "10|Update\n20|Twenty";
    field_update_field($this->field);
71
    $form = drupal_get_form('field_test_entity_form', $entity);
72
73
74
75
76
    $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'));
77
78

    // Options are reset when a new field with the same name is created.
79
80
81
82
83
84
    field_delete_field($this->field_name);
    unset($this->field['id']);
    $this->field['settings']['allowed_values'] = "1|One\n2|Two\n3|Three\n";
    $this->field = field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
85
      'object_type' => 'test_entity',
86
      'bundle' => 'test_bundle',
87
88
89
90
      'widget' => array(
        'type' => 'options_buttons',
      ),
    );
91
    $this->instance = field_create_instance($this->instance);
92
    $entity = field_test_create_stub_entity();
93
    $form = drupal_get_form('field_test_entity_form', $entity);
94
95
96
    $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'));
97
  }
98
99
100
101
102
}

/**
* List module UI tests.
*/
103
class ListFieldUITestCase extends FieldTestCase {
104
  public static function getInfo() {
105
    return array(
106
      'name' => 'List field UI',
107
108
109
110
      'description' => 'Test the List field UI functionality.',
      'group' => 'Field types',
    );
  }
111

112
113
114
115
116
117
118
119
  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.
120
    $type_name = 'test_' . strtolower($this->randomName());
121
122
123
124
125
126
    $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);

    // Create random field name.
127
128
    $this->field_label = $this->randomString();
    $this->field_name = strtolower($this->randomName());
129
130
  }

131
132
133
134
135
  /**
   * Tests that allowed values are properly validated in the UI.
   */
  function testAllowedValues() {
    $element_name = "field[settings][allowed_values]";
136

137
138
139
140
141
    //Test 'List' field type.
    $admin_path = $this->createListFieldAndEdit('list');
    //Check that non-integer keys are rejected.
    $edit = array($element_name => "1.1|one\n");
    $this->drupalPost($admin_path, $edit, t('Save settings'));
142
    $this->assertText("keys must be integers", t('Form validation failed.'));
143

144
145
146
147
148
    // Test 'List (number)' field type.
    $admin_path = $this->createListFieldAndEdit('list_number');
    //Check that non-numeric keys are rejected.
    $edit = array($element_name => "1|one\nB|two");
    $this->drupalPost($admin_path, $edit, t('Save settings'));
149
    $this->assertText("each key must be a valid integer or decimal", t('Form validation failed.'));
150
151
152
153

    //Test 'List (text)' field type.
    $admin_path = $this->createListFieldAndEdit('list_text');
    //Check that over long keys are rejected.
154
    $edit = array($element_name => "1|one\n" . $this->randomName(256) . "|two");
155
    $this->drupalPost($admin_path, $edit, t('Save settings'));
156
    $this->assertText("each key must be a string at most 255 characters long", t('Form validation failed.'));
157

158
    // Test 'List (boolean)' field type.
159
    $admin_path = $this->createListFieldAndEdit('list_boolean');
160
161
162
    // Check that invalid option keys are rejected.
    $edit = array($element_name => "1|one\n2|two");
    $this->drupalPost($admin_path, $edit, t('Save settings'));
163
    $this->assertText("keys must be either 0 or 1", t('Form validation failed.'));
164

165
166
167
    //Check that missing option causes failure.
    $edit = array($element_name => "1|one");
    $this->drupalPost($admin_path, $edit, t('Save settings'));
168
    $this->assertText("two values are required", t('Form validation failed.'));
169
  }
170

171
172
  /**
   * Helper function to create list field of a given type and get the edit page.
173
   *
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
   * @param string $type
   *   'list', 'list_boolean', 'list_number', or 'list_text'
   */
  private function createListFieldAndEdit($type) {
    // Create a test field and instance.
    $field_name = 'test_' . $type;
    $field = array(
      'field_name' => $field_name,
      'type' => $type,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field_name,
      'object_type' => 'node',
      'bundle' => $this->type,
    );
    field_create_instance($instance);

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

196
197
}