You are here

public function ImsOptionsWidgetsTest::testSelectListMultiple in Improved Multi Select 8

Tests the 'ims_options_select' widget (multiple select).

File

modules/ims_options_widget/src/Tests/Functional/ImsOptionsWidgetsTest.php, line 80

Class

ImsOptionsWidgetsTest
Tests the IMS Options widgets.

Namespace

Drupal\ims_options_widget\Tests\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' => 'ims_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
    ->assertEmpty($this
    ->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this
    ->assertEmpty($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertEmpty($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, $this
    ->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
    ->assertEmpty($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());

  // Display form: check that the selected options are on the top.
  $option_elements = $this
    ->xpath('//select[@id="edit-card-2"]/option');
  $options = [];
  $expected = [
    0,
    2,
    '_none',
    1,
  ];
  foreach ($option_elements as $option_element) {
    $options[] = $option_element
      ->getValue();
  }
  $this
    ->assertEqual($expected, $options);

  // Submit form: select only first option.
  $edit = [
    'card_2[]' => [
      0 => 0,
    ],
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->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
    ->assertEmpty($this
    ->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this
    ->assertEmpty($this
    ->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());

  // Display form: check that the selected options are on the top.
  $option_elements = $this
    ->xpath('//select[@id="edit-card-2"]/option');
  $options = [];
  $expected = [
    0,
    '_none',
    1,
    2,
  ];
  foreach ($option_elements as $option_element) {
    $options[] = $option_element
      ->getValue();
  }
  $this
    ->assertEqual($expected, $options);

  // 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, $this
    ->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, $this
    ->t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', []);

  // Display form: check that the selected options from previous submission
  // are on the top.
  $option_elements = $this
    ->xpath('//select[@id="edit-card-2"]/option');
  $options = [];
  $expected = [
    '_none',
    0,
    1,
    2,
  ];
  foreach ($option_elements as $option_element) {
    $options[] = $option_element
      ->getValue();
  }
  $this
    ->assertEqual($expected, $options);

  // 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, $this
    ->t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);

  // Display form: check that the selected options are on the top.
  $option_elements = $this
    ->xpath('//select[@id="edit-card-2"]/option');
  $options = [];
  $expected = [
    0,
    '_none',
    1,
    2,
  ];
  foreach ($option_elements as $option_element) {
    $options[] = $option_element
      ->getValue();
  }
  $this
    ->assertEqual($expected, $options);

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

  // Display form: check that the selected options are on the top.
  $option_elements = $this
    ->xpath('//select[@id="edit-card-2"]/option');
  $options = [];
  $expected = [
    '_none',
    0,
    1,
    2,
  ];
  foreach ($option_elements as $option_element) {
    $options[] = $option_element
      ->getValue();
  }
  $this
    ->assertEqual($expected, $options);

  // 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.');
}