You are here

public function OptionsWidgetsTest::testCheckBoxes 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::testCheckBoxes()

Tests the 'options_buttons' widget (multiple select).

File

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

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\Tests\options\Functional

Code

public function testCheckBoxes() {

  // 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_buttons',
  ])
    ->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 checked.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-card-2-0');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-card-2-1');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-card-2-2');
  $this
    ->assertSession()
    ->responseContains('Some dangerous &amp; unescaped <strong>markup</strong>');

  // Submit form: select first and third options.
  $edit = [
    'card_2[0]' => TRUE,
    'card_2[1]' => FALSE,
    'card_2[2]' => TRUE,
  ];
  $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
    ->assertSession()
    ->checkboxChecked('edit-card-2-0');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-card-2-1');
  $this
    ->assertSession()
    ->checkboxChecked('edit-card-2-2');

  // Submit form: select only first option.
  $edit = [
    'card_2[0]' => TRUE,
    'card_2[1]' => FALSE,
    'card_2[2]' => FALSE,
  ];
  $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
    ->assertSession()
    ->checkboxChecked('edit-card-2-0');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-card-2-1');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-card-2-2');

  // Submit form: select the three options while the field accepts only 2.
  $edit = [
    'card_2[0]' => TRUE,
    'card_2[1]' => TRUE,
    'card_2[2]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('this field cannot hold more than 2 values');

  // Submit form: uncheck all options.
  $edit = [
    'card_2[0]' => FALSE,
    'card_2[1]' => FALSE,
    'card_2[2]' => FALSE,
  ];
  $this
    ->submitForm($edit, 'Save');

  // Check that the value was saved.
  $this
    ->assertFieldValues($entity_init, 'card_2', []);

  // Required checkbox with one option is auto-selected.
  $this->card2
    ->setSetting('allowed_values', [
    99 => 'Only allowed value',
  ]);
  $this->card2
    ->save();
  $field
    ->setRequired(TRUE);
  $field
    ->save();
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertSession()
    ->checkboxChecked('edit-card-2-99');
}