summaryrefslogtreecommitdiff
path: root/modules/field/modules/options/options.test
blob: 101b3befe43cd3c1e7fca9f97322cbd3bdab2dec (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
<?php
// $Id$

class OptionsWidgetsTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name'  => 'Options widgets',
      'description'  => "Test the Options widgets.",
      'group' => 'Field'
    );
  }

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

    $this->card_1 = array(
      'field_name' => 'card_1',
      'type' => 'list',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => "1|One\n2|Two\n3|Three\n",
      ),
    );
    $this->card_1 = field_create_field($this->card_1);

    $this->card_2 = array(
      'field_name' => 'card_2',
      'type' => 'list',
      'cardinality' => 2,
      'settings' => array(
        'allowed_values' => "1|One\n2|Two\n3|Three\n",
      ),
    );
    $this->card_2 = field_create_field($this->card_2);
  }

  /**
   * Test widgets
   */

  /**
   * Return an element from rendered HTML by id, or '' if id is not found.
   */
  function getTagById($html, $id) {
    // @todo: ids sometimes have an extra -n after them; why?
    if (preg_match('@(<[^>]*id="' . $id . '(?:-\d+)?"[^>]*/>)@i', $html, $m)) {
      return $m[0];
    }
    return '';
  }

  /**
   * Assert that a checkbox identified by $id is checked.
   */
  function assertIsChecked($html, $id) {
    $input = $this->getTagById($html, $id);
    $this->assertTrue(preg_match('@checked="checked"@', $input), t('Checkbox %id is checked', array('%id' => $id)));
  }

  /**
   * Assert that a checkbox identified by $id is found but is not checked.
   */
  function assertIsNotChecked($html, $id) {
    $input = $this->getTagById($html, $id);
    if (!empty($input)) {
      $this->assertFalse(preg_match('@checked@', $input), t('Checkbox %id is not checked', array('%id' => $id)));
    }
    else {
      $this->fail(t('Checkbox %id is not found', array('%id' => $id)));
    }
  }

  /**
   * Return an <option> element by value from rendered HTML, or
   * '' if it is not found. 
   */
  function getOptionByValue($html, $value) {
    if (preg_match('@(<option[^>]*value="' . $value . '"[^>]*>[^<]*</option>)@i', $html, $m)) {
      return $m[0];
    }
    return '';
  }

  /**
   * Assert that an <option> for value $value is selected.
   */
  function assertIsSelected($html, $value) {
    $input = $this->getOptionByValue($html, $value);
    $this->assertTrue(preg_match('@selected="selected"@', $input), t('Option %value is selected', array('%value' => $value)));
  }

  /**
   * Assert that an <option> for value $value is found but is not selected.
   */
  function assertIsNotSelected($html, $value) {
    $input = $this->getOptionByValue($html, $value);
    if (!empty($input)) {
      $this->assertFalse(preg_match('@selected@', $input), t('Option %value is not selected', array('%value' => $value)));
    }
    else {
      $this->fail(t('Option %value is not found', array('%value' => $value)));
    }
  }

  function testRadioButtons() {
    $instance = array(
      'field_name' => $this->card_1['field_name'],
      'object_type' => 'test_entity',
      'bundle' => FIELD_TEST_BUNDLE,
      'widget' => array(
        'type' => 'options_buttons',
      ),
    );
    $instance = field_create_instance($instance);

    $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);

    // With no field data, no buttons are checked.
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsNotChecked($render, 'edit-card-1-zxx-value-1');
    $this->assertIsNotChecked($render, 'edit-card-1-zxx-value-2');
    $this->assertIsNotChecked($render, 'edit-card-1-zxx-value-3');

    // With field data, the selected button is checked.
    $entity->card_1[FIELD_LANGUAGE_NONE][0]['value'] = '2';
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsNotChecked($render, 'edit-card-1-zxx-value-1');
    $this->assertIsChecked($render, 'edit-card-1-zxx-value-2');
    $this->assertIsNotChecked($render, 'edit-card-1-zxx-value-3');

    // Required radios with one option is auto-selected.
    $instance['required'] = TRUE;
    field_update_instance($instance);
    $this->card_1['settings']['allowed_values'] = '99|Only allowed value for radios';
    field_update_field($this->card_1);
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsChecked($render, 'edit-card-1-zxx-value-99');
  }

  function testCheckBoxes() {
    $instance = array(
      'field_name' => $this->card_2['field_name'],
      'object_type' => 'test_entity',
      'bundle' => FIELD_TEST_BUNDLE,
      'widget' => array(
        'type' => 'options_buttons',
      ),
    );
    $instance = field_create_instance($instance);

    $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);

    // With no field data, nothing is checked.
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsNotChecked($render, 'edit-card-2-zxx-value-1');
    $this->assertIsNotChecked($render, 'edit-card-2-zxx-value-2');
    $this->assertIsNotChecked($render, 'edit-card-2-zxx-value-3');

    // With field data, the specified items are checked.
    $entity->card_2[FIELD_LANGUAGE_NONE][0]['value'] = '2';
    $entity->card_2[FIELD_LANGUAGE_NONE][1]['value'] = '3';
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsNotChecked($render, 'edit-card-2-zxx-value-1');
    $this->assertIsChecked($render, 'edit-card-2-zxx-value-2');
    $this->assertIsChecked($render, 'edit-card-2-zxx-value-3');

    // Required checkbox with one option is auto-selected.
    $instance['required'] = TRUE;
    field_update_instance($instance);
    $this->card_2['settings']['allowed_values'] = '99|Only allowed value for checkboxes';
    field_update_field($this->card_2);
    unset($entity->card_2);
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsChecked($render, 'edit-card-2-zxx-value-99');
  }

  function testSelectList() {
    $instance = array(
      'field_name' => $this->card_2['field_name'],
      'object_type' => 'test_entity',
      'bundle' => FIELD_TEST_BUNDLE,
      'widget' => array(
        'type' => 'options_select',
      ),
    );
    $instance = field_create_instance($instance);

    $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);

    // With no field data, no options are selected.
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsNotSelected($render, 1);
    $this->assertIsNotSelected($render, 2);
    $this->assertIsNotSelected($render, 3);

    // With field data, the specified options are selected.
    $entity->card_2[FIELD_LANGUAGE_NONE][0]['value'] = '2';
    $entity->card_2[FIELD_LANGUAGE_NONE][1]['value'] = '1';
    $form = drupal_get_form('field_test_entity_form', $entity);
    $render = drupal_render($form);
    $this->assertIsSelected($render, 1);
    $this->assertIsSelected($render, 2);
    $this->assertIsNotSelected($render, 3);

    // A non-required select list has an empty key.
    $form = drupal_get_form('field_test_entity_form', $entity);
    $this->assertTrue(isset($form['card_2'][FIELD_LANGUAGE_NONE]['value']['#options']['']), 'A non-required select list has an empty key.');

    // A required select list does not have an empty key.
    $instance['required'] = TRUE;
    field_update_instance($instance);
    $form = drupal_get_form('field_test_entity_form', $entity);
    $this->assertTrue(!isset($form['card_2'][FIELD_LANGUAGE_NONE]['value']['#options']['']), 'A required select list does not have an empty key.');

    // We don't have to test that a required select list with one
    // option is auto-selected because the browser does it for us.
  }

  /**
   * @todo: Test formatters.
   */
}