You are here

function OptionsWidgetsTest::testSelectListMultiple in Zircon Profile 8.0

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

Tests the 'options_select' widget (multiple select).

File

core/modules/options/src/Tests/OptionsWidgetsTest.php, line 335
Contains \Drupal\options\Tests\OptionsWidgetsTest.

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\options\Tests

Code

function testSelectListMultiple() {

  // Create an instance of the 'multiple values' field.
  $field = entity_create('field_config', array(
    'field_storage' => $this->card2,
    'bundle' => 'entity_test',
  ));
  $field
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->card2
    ->getName(), array(
    'type' => 'options_select',
  ))
    ->save();

  // Create an entity.
  $entity = entity_create('entity_test', array(
    '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
    ->assertOptionSelected("edit-card-2", '_none');
  $this
    ->assertNoOptionSelected('edit-card-2', 0);
  $this
    ->assertNoOptionSelected('edit-card-2', 1);
  $this
    ->assertNoOptionSelected('edit-card-2', 2);
  $this
    ->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');

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

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertOptionSelected('edit-card-2', 0);
  $this
    ->assertNoOptionSelected('edit-card-2', 1);
  $this
    ->assertOptionSelected('edit-card-2', 2);

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

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertOptionSelected('edit-card-2', 0);
  $this
    ->assertNoOptionSelected('edit-card-2', 1);
  $this
    ->assertNoOptionSelected('edit-card-2', 2);

  // Submit form: select the three options while the field accepts only 2.
  $edit = array(
    'card_2[]' => array(
      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 = array(
    'card_2[]' => array(),
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', array());

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

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

  // A required select list does not have an empty key.
  $field
    ->setRequired(TRUE);
  $field
    ->save();
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertFalse($this
    ->xpath('//select[@id=:id]//option[@value=""]', array(
    ':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
    ->assertNoOptionSelected('edit-card-2', 0);
  $this
    ->assertNoOptionSelected('edit-card-2', 1);
  $this
    ->assertNoOptionSelected('edit-card-2', 2);
  $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 = array(
    'card_2[]' => array(
      0 => 0,
    ),
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', array(
    0,
  ));

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertOptionSelected('edit-card-2', 0);
  $this
    ->assertNoOptionSelected('edit-card-2', 1);
  $this
    ->assertNoOptionSelected('edit-card-2', 2);

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