You are here

public function ChosenFieldWidgetsTest::testSelectListSingle in Chosen 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/chosen_field/tests/src/Functional/ChosenFieldWidgetsTest.php \Drupal\Tests\chosen_field\Functional\ChosenFieldWidgetsTest::testSelectListSingle()

Tests the 'chosen_select' widget (single select).

File

modules/chosen_field/tests/src/Functional/ChosenFieldWidgetsTest.php, line 100

Class

ChosenFieldWidgetsTest
Test the Chosen widgets.

Namespace

Drupal\Tests\chosen_field\Functional

Code

public function testSelectListSingle() {

  // Create an instance of the 'single value' field.
  $instance = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->create([
    'field_storage' => $this->card_1,
    'bundle' => 'entity_test',
  ]);
  $instance
    ->setRequired(TRUE);
  $instance
    ->save();
  \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('entity_test.entity_test.default')
    ->setComponent($this->card_1
    ->getName(), [
    'type' => 'chosen_select',
  ])
    ->save();

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

  // Display form.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');

  // A required field without any value has a "none" option.
  $this
    ->assertSession()
    ->elementExists('xpath', '//select[@id="edit-card-1"]//option[@value="_none" and text()="- Select a value -"]');

  // With no field data, nothing is selected.
  $options = [
    '_none',
    0,
    1,
    2,
  ];
  $id = 'edit-card-1';
  foreach ($options as $option) {
    $option_field = $this
      ->assertSession()
      ->optionExists($id, $option);
    $message = "Option {$option} for field {$id} is not selected.";
    $this
      ->assertEmpty($option_field
      ->hasAttribute('selected'), $message);
  }
  $this
    ->assertSession()
    ->responseContains('Some dangerous & unescaped markup');

  // Submit form: select invalid 'none' option.
  $edit = [
    'card_1' => '_none',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->responseContains((string) new FormattableMarkup('@title field is required.', [
    '@title' => $instance
      ->getName(),
  ]));

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

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');

  // A required field with a value has no 'none' option.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//select[@id="edit-card-1"]//option[@value="_none"]');
  $id = 'edit-card-1';
  $option = 0;
  $option_field = $this
    ->assertSession()
    ->optionExists($id, $option);
  $message = "Option {$option} for field {$id} is selected.";
  $this
    ->assertNotEmpty($option_field
    ->hasAttribute('selected'), $message);
  $options = [
    1,
    2,
  ];
  $id = 'edit-card-1';
  foreach ($options as $option) {
    $option_field = $this
      ->assertSession()
      ->optionExists($id, $option);
    $message = "Option {$option} for field {$id} is not selected.";
    $this
      ->assertEmpty($option_field
      ->hasAttribute('selected'), $message);
  }

  // Make the field non required.
  $instance
    ->setRequired(FALSE);
  $instance
    ->save();

  // Display form.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');

  // A non-required field has a 'none' option.
  $this
    ->assertSession()
    ->elementExists('xpath', '//select[@id="edit-card-1"]//option[@value="_none" and text()="- None -"]');

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

  // Test optgroups.
  $this->card_1
    ->setSetting('allowed_values', []);
  $this->card_1
    ->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
  $this->card_1
    ->save();

  // Display form: with no field data, nothing is selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $options = [
    0,
    1,
    2,
  ];
  $id = 'edit-card-1';
  foreach ($options as $option) {
    $option_field = $this
      ->assertSession()
      ->optionExists($id, $option);
    $message = "Option {$option} for field {$id} is not selected.";
    $this
      ->assertEmpty($option_field
      ->hasAttribute('selected'), $message);
  }
  $this
    ->assertSession()
    ->responseContains('Some dangerous & unescaped markup');
  $this
    ->assertSession()
    ->responseContains('Group 1');

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

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $id = 'edit-card-1';
  $option = 0;
  $option_field = $this
    ->assertSession()
    ->optionExists($id, $option);
  $message = "Option {$option} for field {$id} is selected.";
  $this
    ->assertNotEmpty($option_field
    ->hasAttribute('selected'), $message);
  $options = [
    1,
    2,
  ];
  $id = 'edit-card-1';
  foreach ($options as $option) {
    $option_field = $this
      ->assertSession()
      ->optionExists($id, $option);
    $message = "Option {$option} for field {$id} is not selected.";
    $this
      ->assertEmpty($option_field
      ->hasAttribute('selected'), $message);
  }

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