function OptionsWidgetsTest::testCheckBoxes

Same name in other branches
  1. 9 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testCheckBoxes()
  2. 10 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testCheckBoxes()
  3. 11.x 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->assertNoFieldChecked('edit-card-2-0');
    $this->assertNoFieldChecked('edit-card-2-1');
    $this->assertNoFieldChecked('edit-card-2-2');
    $this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
    // Submit form: select first and third options.
    $edit = [
        'card_2[0]' => TRUE,
        'card_2[1]' => FALSE,
        'card_2[2]' => TRUE,
    ];
    $this->drupalPostForm(NULL, $edit, 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->assertFieldChecked('edit-card-2-0');
    $this->assertNoFieldChecked('edit-card-2-1');
    $this->assertFieldChecked('edit-card-2-2');
    // Submit form: select only first option.
    $edit = [
        'card_2[0]' => TRUE,
        'card_2[1]' => FALSE,
        'card_2[2]' => FALSE,
    ];
    $this->drupalPostForm(NULL, $edit, 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->assertFieldChecked('edit-card-2-0');
    $this->assertNoFieldChecked('edit-card-2-1');
    $this->assertNoFieldChecked('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->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 = [
        'card_2[0]' => FALSE,
        'card_2[1]' => FALSE,
        'card_2[2]' => FALSE,
    ];
    $this->drupalPostForm(NULL, $edit, t('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->assertFieldChecked('edit-card-2-99');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.