You are here

public function OptionsWidgetsTest::testSelectListMultiple in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListMultiple()

Tests the 'options_select' widget (multiple select).

File

core/modules/options/tests/src/Functional/OptionsWidgetsTest.php, line 373

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\Tests\options\Functional

Code

public function testSelectListMultiple() {

  // Create an instance of the 'multiple values' field.
  $field = FieldConfig::create([
    'field_storage' => $this->card2,
    'bundle' => 'entity_test',
  ]);
  $field
    ->save();
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($this->card2
    ->getName(), [
    'type' => 'options_select',
  ])
    ->save();

  // Create an entity.
  $entity = EntityTest::create([
    'user_id' => 1,
    'name' => $this
      ->randomMachineName(),
  ]);
  $entity
    ->save();
  $entity_init = clone $entity;

  // Display form: with no field data, nothing is selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('card_2', '_none')
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());
  $this
    ->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');

  // Submit form: select first and third options.
  $edit = [
    'card_2[]' => [
      0 => 0,
      2 => 2,
    ],
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', [
    0,
    2,
  ]);

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());

  // Submit form: select only first option.
  $edit = [
    'card_2[]' => [
      0 => 0,
    ],
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());

  // Submit form: select the three options while the field accepts only 2.
  $edit = [
    'card_2[]' => [
      0 => 0,
      1 => 1,
      2 => 2,
    ],
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');

  // Submit form: uncheck all options.
  $edit = [
    'card_2[]' => [],
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', []);

  // Test the 'None' option.
  // Check that the 'none' option has no effect if actual options are selected
  // as well.
  $edit = [
    'card_2[]' => [
      '_none' => '_none',
      0 => 0,
    ],
  ];
  $this
    ->drupalPostForm('entity_test/manage/' . $entity
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);

  // Check that selecting the 'none' option empties the field.
  $edit = [
    'card_2[]' => [
      '_none' => '_none',
    ],
  ];
  $this
    ->drupalPostForm('entity_test/manage/' . $entity
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', []);

  // A required select list does not have an empty key.
  $field
    ->setRequired(TRUE);
  $field
    ->save();
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertEmpty($this
    ->xpath('//select[@id=:id]//option[@value=""]', [
    ':id' => 'edit-card-2',
  ]), 'A required select list does not have an empty key.');

  // We do not have to test that a required select list with one option is
  // auto-selected because the browser does it for us.
  // Test optgroups.
  // Use a callback function defining optgroups.
  $this->card2
    ->setSetting('allowed_values', []);
  $this->card2
    ->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
  $this->card2
    ->save();
  $field
    ->setRequired(FALSE);
  $field
    ->save();

  // Display form: with no field data, nothing is selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());
  $this
    ->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
  $this
    ->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.');
  $this
    ->assertRaw('Group 1', 'Option groups are displayed.');

  // Submit form: select first option.
  $edit = [
    'card_2[]' => [
      0 => 0,
    ],
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());

  // Submit form: Unselect the option.
  $edit = [
    'card_2[]' => [
      '_none' => '_none',
    ],
  ];
  $this
    ->drupalPostForm('entity_test/manage/' . $entity
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', []);
}