comparison core/modules/options/tests/src/Functional/OptionsWidgetsTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
2 2
3 namespace Drupal\Tests\options\Functional; 3 namespace Drupal\Tests\options\Functional;
4 4
5 use Drupal\entity_test\Entity\EntityTest; 5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\field\Entity\FieldConfig; 6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Tests\FieldTestBase;
8 use Drupal\field\Entity\FieldStorageConfig; 7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\field\Functional\FieldTestBase;
9 9
10 /** 10 /**
11 * Tests the Options widgets. 11 * Tests the Options widgets.
12 * 12 *
13 * @group options 13 * @group options
32 * A field storage with cardinality 2 to use in this test class. 32 * A field storage with cardinality 2 to use in this test class.
33 * 33 *
34 * @var \Drupal\field\Entity\FieldStorageConfig 34 * @var \Drupal\field\Entity\FieldStorageConfig
35 */ 35 */
36 protected $card2; 36 protected $card2;
37
38 37
39 protected function setUp() { 38 protected function setUp() {
40 parent::setUp(); 39 parent::setUp();
41 40
42 // Field storage with cardinality 1. 41 // Field storage with cardinality 1.
253 $entity_init = clone $entity; 252 $entity_init = clone $entity;
254 253
255 // Display form. 254 // Display form.
256 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 255 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
257 // A required field without any value has a "none" option. 256 // A required field without any value has a "none" option.
258 $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-1', ':label' => t('- Select a value -')]), 'A required select list has a "Select a value" choice.'); 257 $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-1', ':label' => '- Select a value -']), 'A required select list has a "Select a value" choice.');
259 258
260 // With no field data, nothing is selected. 259 // With no field data, nothing is selected.
261 $this->assertNoOptionSelected('edit-card-1', '_none'); 260 $this->assertTrue($this->assertSession()->optionExists('card_1', '_none')->isSelected());
262 $this->assertNoOptionSelected('edit-card-1', 0); 261 $this->assertFalse($this->assertSession()->optionExists('card_1', 0)->isSelected());
263 $this->assertNoOptionSelected('edit-card-1', 1); 262 $this->assertFalse($this->assertSession()->optionExists('card_1', 1)->isSelected());
264 $this->assertNoOptionSelected('edit-card-1', 2); 263 $this->assertFalse($this->assertSession()->optionExists('card_1', 2)->isSelected());
265 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.'); 264 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
266 265
267 // Submit form: select invalid 'none' option. 266 // Submit form: select invalid 'none' option.
268 $edit = ['card_1' => '_none']; 267 $edit = ['card_1' => '_none'];
269 $this->drupalPostForm(NULL, $edit, t('Save')); 268 $this->drupalPostForm(NULL, $edit, t('Save'));
276 275
277 // Display form: check that the right options are selected. 276 // Display form: check that the right options are selected.
278 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 277 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
279 // A required field with a value has no 'none' option. 278 // A required field with a value has no 'none' option.
280 $this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', [':id' => 'edit-card-1']), 'A required select list with an actual value has no "none" choice.'); 279 $this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', [':id' => 'edit-card-1']), 'A required select list with an actual value has no "none" choice.');
281 $this->assertOptionSelected('edit-card-1', 0); 280 $this->assertTrue($this->assertSession()->optionExists('card_1', 0)->isSelected());
282 $this->assertNoOptionSelected('edit-card-1', 1); 281 $this->assertFalse($this->assertSession()->optionExists('card_1', 1)->isSelected());
283 $this->assertNoOptionSelected('edit-card-1', 2); 282 $this->assertFalse($this->assertSession()->optionExists('card_1', 2)->isSelected());
284 283
285 // Make the field non required. 284 // Make the field non required.
286 $field->setRequired(FALSE); 285 $field->setRequired(FALSE);
287 $field->save(); 286 $field->save();
288 287
289 // Display form. 288 // Display form.
290 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 289 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
291 // A non-required field has a 'none' option. 290 // A non-required field has a 'none' option.
292 $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-1', ':label' => t('- None -')]), 'A non-required select list has a "None" choice.'); 291 $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-1', ':label' => '- None -']), 'A non-required select list has a "None" choice.');
293 // Submit form: Unselect the option. 292 // Submit form: Unselect the option.
294 $edit = ['card_1' => '_none']; 293 $edit = ['card_1' => '_none'];
295 $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save')); 294 $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save'));
296 $this->assertFieldValues($entity_init, 'card_1', []); 295 $this->assertFieldValues($entity_init, 'card_1', []);
297 296
301 $this->card1->setSetting('allowed_values_function', 'options_test_allowed_values_callback'); 300 $this->card1->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
302 $this->card1->save(); 301 $this->card1->save();
303 302
304 // Display form: with no field data, nothing is selected 303 // Display form: with no field data, nothing is selected
305 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 304 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
306 $this->assertNoOptionSelected('edit-card-1', 0); 305 $this->assertFalse($this->assertSession()->optionExists('card_1', 0)->isSelected());
307 $this->assertNoOptionSelected('edit-card-1', 1); 306 $this->assertFalse($this->assertSession()->optionExists('card_1', 1)->isSelected());
308 $this->assertNoOptionSelected('edit-card-1', 2); 307 $this->assertFalse($this->assertSession()->optionExists('card_1', 2)->isSelected());
309 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.'); 308 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
310 $this->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.'); 309 $this->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.');
311 $this->assertRaw('Group 1', 'Option groups are displayed.'); 310 $this->assertRaw('Group 1', 'Option groups are displayed.');
312 311
313 // Submit form: select first option. 312 // Submit form: select first option.
315 $this->drupalPostForm(NULL, $edit, t('Save')); 314 $this->drupalPostForm(NULL, $edit, t('Save'));
316 $this->assertFieldValues($entity_init, 'card_1', [0]); 315 $this->assertFieldValues($entity_init, 'card_1', [0]);
317 316
318 // Display form: check that the right options are selected. 317 // Display form: check that the right options are selected.
319 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 318 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
320 $this->assertOptionSelected('edit-card-1', 0); 319 $this->assertTrue($this->assertSession()->optionExists('card_1', 0)->isSelected());
321 $this->assertNoOptionSelected('edit-card-1', 1); 320 $this->assertFalse($this->assertSession()->optionExists('card_1', 1)->isSelected());
322 $this->assertNoOptionSelected('edit-card-1', 2); 321 $this->assertFalse($this->assertSession()->optionExists('card_1', 2)->isSelected());
323 322
324 // Submit form: Unselect the option. 323 // Submit form: Unselect the option.
325 $edit = ['card_1' => '_none']; 324 $edit = ['card_1' => '_none'];
326 $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save')); 325 $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save'));
327 $this->assertFieldValues($entity_init, 'card_1', []); 326 $this->assertFieldValues($entity_init, 'card_1', []);
351 $entity->save(); 350 $entity->save();
352 $entity_init = clone $entity; 351 $entity_init = clone $entity;
353 352
354 // Display form: with no field data, nothing is selected. 353 // Display form: with no field data, nothing is selected.
355 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 354 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
356 $this->assertOptionSelected("edit-card-2", '_none'); 355 $this->assertTrue($this->assertSession()->optionExists('card_2', '_none')->isSelected());
357 $this->assertNoOptionSelected('edit-card-2', 0); 356 $this->assertFalse($this->assertSession()->optionExists('card_2', 0)->isSelected());
358 $this->assertNoOptionSelected('edit-card-2', 1); 357 $this->assertFalse($this->assertSession()->optionExists('card_2', 1)->isSelected());
359 $this->assertNoOptionSelected('edit-card-2', 2); 358 $this->assertFalse($this->assertSession()->optionExists('card_2', 2)->isSelected());
360 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.'); 359 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
361 360
362 // Submit form: select first and third options. 361 // Submit form: select first and third options.
363 $edit = ['card_2[]' => [0 => 0, 2 => 2]]; 362 $edit = ['card_2[]' => [0 => 0, 2 => 2]];
364 $this->drupalPostForm(NULL, $edit, t('Save')); 363 $this->drupalPostForm(NULL, $edit, t('Save'));
365 $this->assertFieldValues($entity_init, 'card_2', [0, 2]); 364 $this->assertFieldValues($entity_init, 'card_2', [0, 2]);
366 365
367 // Display form: check that the right options are selected. 366 // Display form: check that the right options are selected.
368 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 367 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
369 $this->assertOptionSelected('edit-card-2', 0); 368 $this->assertTrue($this->assertSession()->optionExists('card_2', 0)->isSelected());
370 $this->assertNoOptionSelected('edit-card-2', 1); 369 $this->assertFalse($this->assertSession()->optionExists('card_2', 1)->isSelected());
371 $this->assertOptionSelected('edit-card-2', 2); 370 $this->assertTrue($this->assertSession()->optionExists('card_2', 2)->isSelected());
372 371
373 // Submit form: select only first option. 372 // Submit form: select only first option.
374 $edit = ['card_2[]' => [0 => 0]]; 373 $edit = ['card_2[]' => [0 => 0]];
375 $this->drupalPostForm(NULL, $edit, t('Save')); 374 $this->drupalPostForm(NULL, $edit, t('Save'));
376 $this->assertFieldValues($entity_init, 'card_2', [0]); 375 $this->assertFieldValues($entity_init, 'card_2', [0]);
377 376
378 // Display form: check that the right options are selected. 377 // Display form: check that the right options are selected.
379 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 378 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
380 $this->assertOptionSelected('edit-card-2', 0); 379 $this->assertTrue($this->assertSession()->optionExists('card_2', 0)->isSelected());
381 $this->assertNoOptionSelected('edit-card-2', 1); 380 $this->assertFalse($this->assertSession()->optionExists('card_2', 1)->isSelected());
382 $this->assertNoOptionSelected('edit-card-2', 2); 381 $this->assertFalse($this->assertSession()->optionExists('card_2', 2)->isSelected());
383 382
384 // Submit form: select the three options while the field accepts only 2. 383 // Submit form: select the three options while the field accepts only 2.
385 $edit = ['card_2[]' => [0 => 0, 1 => 1, 2 => 2]]; 384 $edit = ['card_2[]' => [0 => 0, 1 => 1, 2 => 2]];
386 $this->drupalPostForm(NULL, $edit, t('Save')); 385 $this->drupalPostForm(NULL, $edit, t('Save'));
387 $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.'); 386 $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
422 $field->setRequired(FALSE); 421 $field->setRequired(FALSE);
423 $field->save(); 422 $field->save();
424 423
425 // Display form: with no field data, nothing is selected. 424 // Display form: with no field data, nothing is selected.
426 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 425 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
427 $this->assertNoOptionSelected('edit-card-2', 0); 426 $this->assertFalse($this->assertSession()->optionExists('card_2', 0)->isSelected());
428 $this->assertNoOptionSelected('edit-card-2', 1); 427 $this->assertFalse($this->assertSession()->optionExists('card_2', 1)->isSelected());
429 $this->assertNoOptionSelected('edit-card-2', 2); 428 $this->assertFalse($this->assertSession()->optionExists('card_2', 2)->isSelected());
430 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.'); 429 $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
431 $this->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.'); 430 $this->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.');
432 $this->assertRaw('Group 1', 'Option groups are displayed.'); 431 $this->assertRaw('Group 1', 'Option groups are displayed.');
433 432
434 // Submit form: select first option. 433 // Submit form: select first option.
436 $this->drupalPostForm(NULL, $edit, t('Save')); 435 $this->drupalPostForm(NULL, $edit, t('Save'));
437 $this->assertFieldValues($entity_init, 'card_2', [0]); 436 $this->assertFieldValues($entity_init, 'card_2', [0]);
438 437
439 // Display form: check that the right options are selected. 438 // Display form: check that the right options are selected.
440 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 439 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
441 $this->assertOptionSelected('edit-card-2', 0); 440 $this->assertTrue($this->assertSession()->optionExists('card_2', 0)->isSelected());
442 $this->assertNoOptionSelected('edit-card-2', 1); 441 $this->assertFalse($this->assertSession()->optionExists('card_2', 1)->isSelected());
443 $this->assertNoOptionSelected('edit-card-2', 2); 442 $this->assertFalse($this->assertSession()->optionExists('card_2', 2)->isSelected());
444 443
445 // Submit form: Unselect the option. 444 // Submit form: Unselect the option.
446 $edit = ['card_2[]' => ['_none' => '_none']]; 445 $edit = ['card_2[]' => ['_none' => '_none']];
447 $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save')); 446 $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save'));
448 $this->assertFieldValues($entity_init, 'card_2', []); 447 $this->assertFieldValues($entity_init, 'card_2', []);
486 ->save(); 485 ->save();
487 486
488 // Display form: check that _none options are present and has label. 487 // Display form: check that _none options are present and has label.
489 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit'); 488 $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
490 // A required field without any value has a "none" option. 489 // A required field without any value has a "none" option.
491 $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-1', ':label' => t('- None -')]), 'A test select has a "None" choice.'); 490 $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-1', ':label' => '- None -']), 'A test select has a "None" choice.');
492 } 491 }
493 492
494 } 493 }