You are here

public function OptionsWidgetsTest::testSelectListMultiple in Drupal 9

Same name and namespace in other branches
  1. 8 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 377

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
    ->assertSession()
    ->responseContains('Some dangerous & unescaped markup');

  // Submit form: select first and third options.
  $edit = [
    'card_2[]' => [
      0 => 0,
      2 => 2,
    ],
  ];
  $this
    ->submitForm($edit, '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
    ->submitForm($edit, '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
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('this field cannot hold more than 2 values');

  // Submit form: uncheck all options.
  $edit = [
    'card_2[]' => [],
  ];
  $this
    ->submitForm($edit, '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
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);

  // Check that selecting the 'none' option empties the field.
  $edit = [
    'card_2[]' => [
      '_none' => '_none',
    ],
  ];
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->submitForm($edit, '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
    ->assertSession()
    ->selectExists('edit-card-2')
    ->find('xpath', 'option[@value=""]'));

  // 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
    ->assertSession()
    ->responseContains('Some dangerous & unescaped markup');
  $this
    ->assertSession()
    ->responseContains('More <script>dangerous</script> markup');
  $this
    ->assertSession()
    ->responseContains('Group 1');

  // Submit form: select first option.
  $edit = [
    'card_2[]' => [
      0 => 0,
    ],
  ];
  $this
    ->submitForm($edit, '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
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertFieldValues($entity_init, 'card_2', []);
}