1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Tests for options.module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
class OptionsWidgetsTestCase extends FieldTestCase {
|
9 |
|
|
public static function getInfo() {
|
10 |
|
|
return array(
|
11 |
|
|
'name' => 'Options widgets',
|
12 |
|
|
'description' => "Test the Options widgets.",
|
13 |
|
|
'group' => 'Field types'
|
14 |
|
|
);
|
15 |
|
|
}
|
16 |
|
|
|
17 |
|
|
function setUp() {
|
18 |
|
|
parent::setUp('field_test', 'list_test');
|
19 |
|
|
|
20 |
|
|
// Field with cardinality 1.
|
21 |
|
|
$this->card_1 = array(
|
22 |
|
|
'field_name' => 'card_1',
|
23 |
|
|
'type' => 'list_integer',
|
24 |
|
|
'cardinality' => 1,
|
25 |
|
|
'settings' => array(
|
26 |
|
|
// Make sure that 0 works as an option.
|
27 |
30d5b9c5
|
Mathieu Schiano
|
'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>', 3 => 'Some HTML encoded markup with < & >'),
|
28 |
85ad3d82
|
Assos Assos
|
),
|
29 |
|
|
);
|
30 |
|
|
$this->card_1 = field_create_field($this->card_1);
|
31 |
|
|
|
32 |
|
|
// Field with cardinality 2.
|
33 |
|
|
$this->card_2 = array(
|
34 |
|
|
'field_name' => 'card_2',
|
35 |
|
|
'type' => 'list_integer',
|
36 |
|
|
'cardinality' => 2,
|
37 |
|
|
'settings' => array(
|
38 |
|
|
// Make sure that 0 works as an option.
|
39 |
|
|
'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
|
40 |
|
|
),
|
41 |
|
|
);
|
42 |
|
|
$this->card_2 = field_create_field($this->card_2);
|
43 |
|
|
|
44 |
|
|
// Boolean field.
|
45 |
|
|
$this->bool = array(
|
46 |
|
|
'field_name' => 'bool',
|
47 |
|
|
'type' => 'list_boolean',
|
48 |
|
|
'cardinality' => 1,
|
49 |
|
|
'settings' => array(
|
50 |
|
|
// Make sure that 0 works as a 'on' value'.
|
51 |
|
|
'allowed_values' => array(1 => 'Zero', 0 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
|
52 |
|
|
),
|
53 |
|
|
);
|
54 |
|
|
$this->bool = field_create_field($this->bool);
|
55 |
|
|
|
56 |
|
|
// Create a web user.
|
57 |
b0dc3a2e
|
Julien Enselme
|
$this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer fields'));
|
58 |
85ad3d82
|
Assos Assos
|
$this->drupalLogin($this->web_user);
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* Tests the 'options_buttons' widget (single select).
|
63 |
|
|
*/
|
64 |
|
|
function testRadioButtons() {
|
65 |
|
|
// Create an instance of the 'single value' field.
|
66 |
|
|
$instance = array(
|
67 |
|
|
'field_name' => $this->card_1['field_name'],
|
68 |
|
|
'entity_type' => 'test_entity',
|
69 |
|
|
'bundle' => 'test_bundle',
|
70 |
|
|
'widget' => array(
|
71 |
|
|
'type' => 'options_buttons',
|
72 |
|
|
),
|
73 |
|
|
);
|
74 |
|
|
$instance = field_create_instance($instance);
|
75 |
|
|
$langcode = LANGUAGE_NONE;
|
76 |
|
|
|
77 |
|
|
// Create an entity.
|
78 |
|
|
$entity_init = field_test_create_stub_entity();
|
79 |
|
|
$entity = clone $entity_init;
|
80 |
|
|
$entity->is_new = TRUE;
|
81 |
|
|
field_test_entity_save($entity);
|
82 |
|
|
|
83 |
|
|
// With no field data, no buttons are checked.
|
84 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
85 |
|
|
$this->assertNoFieldChecked("edit-card-1-$langcode-0");
|
86 |
|
|
$this->assertNoFieldChecked("edit-card-1-$langcode-1");
|
87 |
|
|
$this->assertNoFieldChecked("edit-card-1-$langcode-2");
|
88 |
|
|
$this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
|
89 |
|
|
|
90 |
|
|
// Select first option.
|
91 |
|
|
$edit = array("card_1[$langcode]" => 0);
|
92 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
93 |
|
|
$this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
|
94 |
|
|
|
95 |
|
|
// Check that the selected button is checked.
|
96 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
97 |
|
|
$this->assertFieldChecked("edit-card-1-$langcode-0");
|
98 |
|
|
$this->assertNoFieldChecked("edit-card-1-$langcode-1");
|
99 |
|
|
$this->assertNoFieldChecked("edit-card-1-$langcode-2");
|
100 |
|
|
|
101 |
|
|
// Unselect option.
|
102 |
|
|
$edit = array("card_1[$langcode]" => '_none');
|
103 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
104 |
|
|
$this->assertFieldValues($entity_init, 'card_1', $langcode, array());
|
105 |
|
|
|
106 |
|
|
// Check that required radios with one option is auto-selected.
|
107 |
|
|
$this->card_1['settings']['allowed_values'] = array(99 => 'Only allowed value');
|
108 |
|
|
field_update_field($this->card_1);
|
109 |
|
|
$instance['required'] = TRUE;
|
110 |
|
|
field_update_instance($instance);
|
111 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
112 |
|
|
$this->assertFieldChecked("edit-card-1-$langcode-99");
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Tests the 'options_buttons' widget (multiple select).
|
117 |
|
|
*/
|
118 |
|
|
function testCheckBoxes() {
|
119 |
|
|
// Create an instance of the 'multiple values' field.
|
120 |
|
|
$instance = array(
|
121 |
|
|
'field_name' => $this->card_2['field_name'],
|
122 |
|
|
'entity_type' => 'test_entity',
|
123 |
|
|
'bundle' => 'test_bundle',
|
124 |
|
|
'widget' => array(
|
125 |
|
|
'type' => 'options_buttons',
|
126 |
|
|
),
|
127 |
|
|
);
|
128 |
|
|
$instance = field_create_instance($instance);
|
129 |
|
|
$langcode = LANGUAGE_NONE;
|
130 |
|
|
|
131 |
|
|
// Create an entity.
|
132 |
|
|
$entity_init = field_test_create_stub_entity();
|
133 |
|
|
$entity = clone $entity_init;
|
134 |
|
|
$entity->is_new = TRUE;
|
135 |
|
|
field_test_entity_save($entity);
|
136 |
|
|
|
137 |
|
|
// Display form: with no field data, nothing is checked.
|
138 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
139 |
|
|
$this->assertNoFieldChecked("edit-card-2-$langcode-0");
|
140 |
|
|
$this->assertNoFieldChecked("edit-card-2-$langcode-1");
|
141 |
|
|
$this->assertNoFieldChecked("edit-card-2-$langcode-2");
|
142 |
|
|
$this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
|
143 |
|
|
|
144 |
|
|
// Submit form: select first and third options.
|
145 |
|
|
$edit = array(
|
146 |
|
|
"card_2[$langcode][0]" => TRUE,
|
147 |
|
|
"card_2[$langcode][1]" => FALSE,
|
148 |
|
|
"card_2[$langcode][2]" => TRUE,
|
149 |
|
|
);
|
150 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
151 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
|
152 |
|
|
|
153 |
|
|
// Display form: check that the right options are selected.
|
154 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
155 |
|
|
$this->assertFieldChecked("edit-card-2-$langcode-0");
|
156 |
|
|
$this->assertNoFieldChecked("edit-card-2-$langcode-1");
|
157 |
|
|
$this->assertFieldChecked("edit-card-2-$langcode-2");
|
158 |
|
|
|
159 |
|
|
// Submit form: select only first option.
|
160 |
|
|
$edit = array(
|
161 |
|
|
"card_2[$langcode][0]" => TRUE,
|
162 |
|
|
"card_2[$langcode][1]" => FALSE,
|
163 |
|
|
"card_2[$langcode][2]" => FALSE,
|
164 |
|
|
);
|
165 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
166 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
167 |
|
|
|
168 |
|
|
// Display form: check that the right options are selected.
|
169 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
170 |
|
|
$this->assertFieldChecked("edit-card-2-$langcode-0");
|
171 |
|
|
$this->assertNoFieldChecked("edit-card-2-$langcode-1");
|
172 |
|
|
$this->assertNoFieldChecked("edit-card-2-$langcode-2");
|
173 |
|
|
|
174 |
|
|
// Submit form: select the three options while the field accepts only 2.
|
175 |
|
|
$edit = array(
|
176 |
|
|
"card_2[$langcode][0]" => TRUE,
|
177 |
|
|
"card_2[$langcode][1]" => TRUE,
|
178 |
|
|
"card_2[$langcode][2]" => TRUE,
|
179 |
|
|
);
|
180 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
181 |
|
|
$this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
|
182 |
|
|
|
183 |
|
|
// Submit form: uncheck all options.
|
184 |
|
|
$edit = array(
|
185 |
|
|
"card_2[$langcode][0]" => FALSE,
|
186 |
|
|
"card_2[$langcode][1]" => FALSE,
|
187 |
|
|
"card_2[$langcode][2]" => FALSE,
|
188 |
|
|
);
|
189 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
190 |
|
|
// Check that the value was saved.
|
191 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
192 |
|
|
|
193 |
|
|
// Required checkbox with one option is auto-selected.
|
194 |
|
|
$this->card_2['settings']['allowed_values'] = array(99 => 'Only allowed value');
|
195 |
|
|
field_update_field($this->card_2);
|
196 |
|
|
$instance['required'] = TRUE;
|
197 |
|
|
field_update_instance($instance);
|
198 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
199 |
|
|
$this->assertFieldChecked("edit-card-2-$langcode-99");
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/**
|
203 |
|
|
* Tests the 'options_select' widget (single select).
|
204 |
|
|
*/
|
205 |
|
|
function testSelectListSingle() {
|
206 |
|
|
// Create an instance of the 'single value' field.
|
207 |
|
|
$instance = array(
|
208 |
|
|
'field_name' => $this->card_1['field_name'],
|
209 |
|
|
'entity_type' => 'test_entity',
|
210 |
|
|
'bundle' => 'test_bundle',
|
211 |
|
|
'required' => TRUE,
|
212 |
|
|
'widget' => array(
|
213 |
|
|
'type' => 'options_select',
|
214 |
|
|
),
|
215 |
|
|
);
|
216 |
|
|
$instance = field_create_instance($instance);
|
217 |
|
|
$langcode = LANGUAGE_NONE;
|
218 |
|
|
|
219 |
|
|
// Create an entity.
|
220 |
|
|
$entity_init = field_test_create_stub_entity();
|
221 |
|
|
$entity = clone $entity_init;
|
222 |
|
|
$entity->is_new = TRUE;
|
223 |
|
|
field_test_entity_save($entity);
|
224 |
|
|
|
225 |
|
|
// Display form.
|
226 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
227 |
|
|
// A required field without any value has a "none" option.
|
228 |
|
|
$this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- Select a value -'))), 'A required select list has a "Select a value" choice.');
|
229 |
|
|
|
230 |
|
|
// With no field data, nothing is selected.
|
231 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", '_none');
|
232 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 0);
|
233 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
234 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
235 |
|
|
$this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
|
236 |
30d5b9c5
|
Mathieu Schiano
|
$this->assertRaw('Some HTML encoded markup with < & >', 'HTML entities in option text were properly handled and not double-encoded');
|
237 |
85ad3d82
|
Assos Assos
|
|
238 |
|
|
// Submit form: select invalid 'none' option.
|
239 |
|
|
$edit = array("card_1[$langcode]" => '_none');
|
240 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
241 |
|
|
$this->assertRaw(t('!title field is required.', array('!title' => $instance['field_name'])), 'Cannot save a required field when selecting "none" from the select list.');
|
242 |
|
|
|
243 |
|
|
// Submit form: select first option.
|
244 |
|
|
$edit = array("card_1[$langcode]" => 0);
|
245 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
246 |
|
|
$this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
|
247 |
|
|
|
248 |
|
|
// Display form: check that the right options are selected.
|
249 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
250 |
|
|
// A required field with a value has no 'none' option.
|
251 |
|
|
$this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1-' . $langcode)), 'A required select list with an actual value has no "none" choice.');
|
252 |
|
|
$this->assertOptionSelected("edit-card-1-$langcode", 0);
|
253 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
254 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
255 |
|
|
|
256 |
|
|
// Make the field non required.
|
257 |
|
|
$instance['required'] = FALSE;
|
258 |
|
|
field_update_instance($instance);
|
259 |
|
|
|
260 |
|
|
// Display form.
|
261 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
262 |
|
|
// A non-required field has a 'none' option.
|
263 |
|
|
$this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- None -'))), 'A non-required select list has a "None" choice.');
|
264 |
|
|
// Submit form: Unselect the option.
|
265 |
|
|
$edit = array("card_1[$langcode]" => '_none');
|
266 |
|
|
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
267 |
|
|
$this->assertFieldValues($entity_init, 'card_1', $langcode, array());
|
268 |
|
|
|
269 |
|
|
// Test optgroups.
|
270 |
|
|
|
271 |
|
|
$this->card_1['settings']['allowed_values'] = array();
|
272 |
|
|
$this->card_1['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
|
273 |
|
|
field_update_field($this->card_1);
|
274 |
|
|
|
275 |
|
|
// Display form: with no field data, nothing is selected
|
276 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
277 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 0);
|
278 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
279 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
280 |
|
|
$this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
|
281 |
|
|
$this->assertRaw('Group 1', 'Option groups are displayed.');
|
282 |
|
|
|
283 |
|
|
// Submit form: select first option.
|
284 |
|
|
$edit = array("card_1[$langcode]" => 0);
|
285 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
286 |
|
|
$this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
|
287 |
|
|
|
288 |
|
|
// Display form: check that the right options are selected.
|
289 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
290 |
|
|
$this->assertOptionSelected("edit-card-1-$langcode", 0);
|
291 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
292 |
|
|
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
293 |
|
|
|
294 |
|
|
// Submit form: Unselect the option.
|
295 |
|
|
$edit = array("card_1[$langcode]" => '_none');
|
296 |
|
|
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
297 |
|
|
$this->assertFieldValues($entity_init, 'card_1', $langcode, array());
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
/**
|
301 |
|
|
* Tests the 'options_select' widget (multiple select).
|
302 |
|
|
*/
|
303 |
|
|
function testSelectListMultiple() {
|
304 |
|
|
// Create an instance of the 'multiple values' field.
|
305 |
|
|
$instance = array(
|
306 |
|
|
'field_name' => $this->card_2['field_name'],
|
307 |
|
|
'entity_type' => 'test_entity',
|
308 |
|
|
'bundle' => 'test_bundle',
|
309 |
|
|
'widget' => array(
|
310 |
|
|
'type' => 'options_select',
|
311 |
|
|
),
|
312 |
|
|
);
|
313 |
|
|
$instance = field_create_instance($instance);
|
314 |
|
|
$langcode = LANGUAGE_NONE;
|
315 |
|
|
|
316 |
|
|
// Create an entity.
|
317 |
|
|
$entity_init = field_test_create_stub_entity();
|
318 |
|
|
$entity = clone $entity_init;
|
319 |
|
|
$entity->is_new = TRUE;
|
320 |
|
|
field_test_entity_save($entity);
|
321 |
|
|
|
322 |
|
|
// Display form: with no field data, nothing is selected.
|
323 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
324 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 0);
|
325 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
326 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
327 |
|
|
$this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
|
328 |
|
|
|
329 |
|
|
// Submit form: select first and third options.
|
330 |
|
|
$edit = array("card_2[$langcode][]" => array(0 => 0, 2 => 2));
|
331 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
332 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
|
333 |
|
|
|
334 |
|
|
// Display form: check that the right options are selected.
|
335 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
336 |
|
|
$this->assertOptionSelected("edit-card-2-$langcode", 0);
|
337 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
338 |
|
|
$this->assertOptionSelected("edit-card-2-$langcode", 2);
|
339 |
|
|
|
340 |
|
|
// Submit form: select only first option.
|
341 |
|
|
$edit = array("card_2[$langcode][]" => array(0 => 0));
|
342 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
343 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
344 |
|
|
|
345 |
|
|
// Display form: check that the right options are selected.
|
346 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
347 |
|
|
$this->assertOptionSelected("edit-card-2-$langcode", 0);
|
348 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
349 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
350 |
|
|
|
351 |
|
|
// Submit form: select the three options while the field accepts only 2.
|
352 |
|
|
$edit = array("card_2[$langcode][]" => array(0 => 0, 1 => 1, 2 => 2));
|
353 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
354 |
|
|
$this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
|
355 |
|
|
|
356 |
|
|
// Submit form: uncheck all options.
|
357 |
|
|
$edit = array("card_2[$langcode][]" => array());
|
358 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
359 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
360 |
|
|
|
361 |
|
|
// Test the 'None' option.
|
362 |
|
|
|
363 |
|
|
// Check that the 'none' option has no effect if actual options are selected
|
364 |
|
|
// as well.
|
365 |
|
|
$edit = array("card_2[$langcode][]" => array('_none' => '_none', 0 => 0));
|
366 |
|
|
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
367 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
368 |
|
|
|
369 |
|
|
// Check that selecting the 'none' option empties the field.
|
370 |
|
|
$edit = array("card_2[$langcode][]" => array('_none' => '_none'));
|
371 |
|
|
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
372 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
373 |
|
|
|
374 |
|
|
// A required select list does not have an empty key.
|
375 |
|
|
$instance['required'] = TRUE;
|
376 |
|
|
field_update_instance($instance);
|
377 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
378 |
|
|
$this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2-' . $langcode)), 'A required select list does not have an empty key.');
|
379 |
|
|
|
380 |
|
|
// We do not have to test that a required select list with one option is
|
381 |
|
|
// auto-selected because the browser does it for us.
|
382 |
|
|
|
383 |
|
|
// Test optgroups.
|
384 |
|
|
|
385 |
|
|
// Use a callback function defining optgroups.
|
386 |
|
|
$this->card_2['settings']['allowed_values'] = array();
|
387 |
|
|
$this->card_2['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
|
388 |
|
|
field_update_field($this->card_2);
|
389 |
|
|
$instance['required'] = FALSE;
|
390 |
|
|
field_update_instance($instance);
|
391 |
|
|
|
392 |
|
|
// Display form: with no field data, nothing is selected.
|
393 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
394 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 0);
|
395 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
396 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
397 |
|
|
$this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
|
398 |
|
|
$this->assertRaw('Group 1', 'Option groups are displayed.');
|
399 |
|
|
|
400 |
|
|
// Submit form: select first option.
|
401 |
|
|
$edit = array("card_2[$langcode][]" => array(0 => 0));
|
402 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
403 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
404 |
|
|
|
405 |
|
|
// Display form: check that the right options are selected.
|
406 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
407 |
|
|
$this->assertOptionSelected("edit-card-2-$langcode", 0);
|
408 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
409 |
|
|
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
410 |
|
|
|
411 |
|
|
// Submit form: Unselect the option.
|
412 |
|
|
$edit = array("card_2[$langcode][]" => array('_none' => '_none'));
|
413 |
|
|
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
414 |
|
|
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
/**
|
418 |
|
|
* Tests the 'options_onoff' widget.
|
419 |
|
|
*/
|
420 |
|
|
function testOnOffCheckbox() {
|
421 |
|
|
// Create an instance of the 'boolean' field.
|
422 |
|
|
$instance = array(
|
423 |
|
|
'field_name' => $this->bool['field_name'],
|
424 |
|
|
'entity_type' => 'test_entity',
|
425 |
|
|
'bundle' => 'test_bundle',
|
426 |
|
|
'widget' => array(
|
427 |
|
|
'type' => 'options_onoff',
|
428 |
|
|
),
|
429 |
|
|
);
|
430 |
|
|
$instance = field_create_instance($instance);
|
431 |
|
|
$langcode = LANGUAGE_NONE;
|
432 |
|
|
|
433 |
|
|
// Create an entity.
|
434 |
|
|
$entity_init = field_test_create_stub_entity();
|
435 |
|
|
$entity = clone $entity_init;
|
436 |
|
|
$entity->is_new = TRUE;
|
437 |
|
|
field_test_entity_save($entity);
|
438 |
|
|
|
439 |
|
|
// Display form: with no field data, option is unchecked.
|
440 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
441 |
|
|
$this->assertNoFieldChecked("edit-bool-$langcode");
|
442 |
|
|
$this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
|
443 |
|
|
|
444 |
|
|
// Submit form: check the option.
|
445 |
|
|
$edit = array("bool[$langcode]" => TRUE);
|
446 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
447 |
|
|
$this->assertFieldValues($entity_init, 'bool', $langcode, array(0));
|
448 |
|
|
|
449 |
|
|
// Display form: check that the right options are selected.
|
450 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
451 |
|
|
$this->assertFieldChecked("edit-bool-$langcode");
|
452 |
|
|
|
453 |
|
|
// Submit form: uncheck the option.
|
454 |
|
|
$edit = array("bool[$langcode]" => FALSE);
|
455 |
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
456 |
|
|
$this->assertFieldValues($entity_init, 'bool', $langcode, array(1));
|
457 |
|
|
|
458 |
|
|
// Display form: with 'off' value, option is unchecked.
|
459 |
|
|
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
460 |
|
|
$this->assertNoFieldChecked("edit-bool-$langcode");
|
461 |
|
|
|
462 |
|
|
// Create admin user.
|
463 |
b0dc3a2e
|
Julien Enselme
|
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy', 'administer fields'));
|
464 |
85ad3d82
|
Assos Assos
|
$this->drupalLogin($admin_user);
|
465 |
|
|
|
466 |
|
|
// Create a test field instance.
|
467 |
|
|
$fieldUpdate = $this->bool;
|
468 |
|
|
$fieldUpdate['settings']['allowed_values'] = array(0 => 0, 1 => 'MyOnValue');
|
469 |
|
|
field_update_field($fieldUpdate);
|
470 |
|
|
$instance = array(
|
471 |
|
|
'field_name' => $this->bool['field_name'],
|
472 |
|
|
'entity_type' => 'node',
|
473 |
|
|
'bundle' => 'page',
|
474 |
|
|
'widget' => array(
|
475 |
|
|
'type' => 'options_onoff',
|
476 |
|
|
'module' => 'options',
|
477 |
|
|
),
|
478 |
|
|
);
|
479 |
|
|
field_create_instance($instance);
|
480 |
|
|
|
481 |
|
|
// Go to the edit page and check if the default settings works as expected
|
482 |
|
|
$fieldEditUrl = 'admin/structure/types/manage/page/fields/bool';
|
483 |
|
|
$this->drupalGet($fieldEditUrl);
|
484 |
|
|
|
485 |
|
|
$this->assertText(
|
486 |
|
|
'Use field label instead of the "On value" as label ',
|
487 |
|
|
'Display setting checkbox available.'
|
488 |
|
|
);
|
489 |
|
|
|
490 |
|
|
$this->assertFieldByXPath(
|
491 |
|
|
'*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="MyOnValue "]',
|
492 |
|
|
TRUE,
|
493 |
|
|
'Default case shows "On value"'
|
494 |
|
|
);
|
495 |
|
|
|
496 |
|
|
// Enable setting
|
497 |
|
|
$edit = array('instance[widget][settings][display_label]' => 1);
|
498 |
|
|
// Save the new Settings
|
499 |
|
|
$this->drupalPost($fieldEditUrl, $edit, t('Save settings'));
|
500 |
|
|
|
501 |
|
|
// Go again to the edit page and check if the setting
|
502 |
|
|
// is stored and has the expected effect
|
503 |
|
|
$this->drupalGet($fieldEditUrl);
|
504 |
|
|
$this->assertText(
|
505 |
|
|
'Use field label instead of the "On value" as label ',
|
506 |
|
|
'Display setting checkbox is available'
|
507 |
|
|
);
|
508 |
|
|
$this->assertFieldChecked(
|
509 |
|
|
'edit-instance-widget-settings-display-label',
|
510 |
|
|
'Display settings checkbox checked'
|
511 |
|
|
);
|
512 |
|
|
$this->assertFieldByXPath(
|
513 |
|
|
'*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="' . $this->bool['field_name'] . ' "]',
|
514 |
|
|
TRUE,
|
515 |
|
|
'Display label changes label of the checkbox'
|
516 |
|
|
);
|
517 |
|
|
}
|
518 |
|
|
}
|
519 |
|
|
|
520 |
|
|
/**
|
521 |
|
|
* Test an options select on a list field with a dynamic allowed values function.
|
522 |
|
|
*/
|
523 |
|
|
class OptionsSelectDynamicValuesTestCase extends ListDynamicValuesTestCase {
|
524 |
|
|
public static function getInfo() {
|
525 |
|
|
return array(
|
526 |
|
|
'name' => 'Options select dynamic values',
|
527 |
|
|
'description' => 'Test an options select on a list field with a dynamic allowed values function.',
|
528 |
|
|
'group' => 'Field types',
|
529 |
|
|
);
|
530 |
|
|
}
|
531 |
|
|
|
532 |
|
|
/**
|
533 |
|
|
* Tests the 'options_select' widget (single select).
|
534 |
|
|
*/
|
535 |
|
|
function testSelectListDynamic() {
|
536 |
|
|
// Create an entity.
|
537 |
|
|
$this->entity->is_new = TRUE;
|
538 |
|
|
field_test_entity_save($this->entity);
|
539 |
|
|
// Create a web user.
|
540 |
|
|
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
|
541 |
|
|
$this->drupalLogin($web_user);
|
542 |
|
|
|
543 |
|
|
// Display form.
|
544 |
|
|
$this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit');
|
545 |
|
|
$options = $this->xpath('//select[@id="edit-test-list-und"]/option');
|
546 |
|
|
$this->assertEqual(count($options), count($this->test) + 1);
|
547 |
|
|
foreach ($options as $option) {
|
548 |
|
|
$value = (string) $option['value'];
|
549 |
|
|
if ($value != '_none') {
|
550 |
|
|
$this->assertTrue(array_search($value, $this->test));
|
551 |
|
|
}
|
552 |
|
|
}
|
553 |
|
|
}
|
554 |
|
|
} |