function OptionsWidgetsTest::testSelectListSingle

Same name in other branches
  1. 9 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
  2. 10 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
  3. 11.x core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()

Tests the 'options_select' widget (single select).

File

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

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\Tests\options\Functional

Code

public function testSelectListSingle() {
    // Create an instance of the 'single value' field.
    $field = FieldConfig::create([
        'field_storage' => $this->card1,
        'bundle' => 'entity_test',
        'required' => TRUE,
    ]);
    $field->save();
    \Drupal::service('entity_display.repository')->getFormDisplay('entity_test', 'entity_test')
        ->setComponent($this->card1
        ->getName(), [
        'type' => 'options_select',
    ])
        ->save();
    // Create an entity.
    $entity = EntityTest::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->assertNotEmpty($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [
        ':id' => 'edit-card-1',
        ':label' => '- Select a value -',
    ]), 'A required select list has a "Select a value" choice.');
    // With no field data, nothing is selected.
    $this->assertTrue($this->assertSession()
        ->optionExists('card_1', '_none')
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 0)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 1)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 2)
        ->isSelected());
    $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
    // Submit form: select invalid 'none' option.
    $edit = [
        'card_1' => '_none',
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('@title field is required.', [
        '@title' => $field->getName(),
    ]), 'Cannot save a required field when selecting "none" from the select list.');
    // Submit form: select first option.
    $edit = [
        'card_1' => 0,
    ];
    $this->drupalPostForm(NULL, $edit, t('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->assertEmpty($this->xpath('//select[@id=:id]//option[@value="_none"]', [
        ':id' => 'edit-card-1',
    ]), 'A required select list with an actual value has no "none" choice.');
    $this->assertTrue($this->assertSession()
        ->optionExists('card_1', 0)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 1)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 2)
        ->isSelected());
    // Make the field non required.
    $field->setRequired(FALSE);
    $field->save();
    // Display form.
    $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
    // A non-required field has a 'none' option.
    $this->assertNotEmpty($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [
        ':id' => 'edit-card-1',
        ':label' => '- None -',
    ]), 'A non-required select list has a "None" choice.');
    // Submit form: Unselect the option.
    $edit = [
        'card_1' => '_none',
    ];
    $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save'));
    $this->assertFieldValues($entity_init, 'card_1', []);
    // Test optgroups.
    $this->card1
        ->setSetting('allowed_values', []);
    $this->card1
        ->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
    $this->card1
        ->save();
    // Display form: with no field data, nothing is selected
    $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 0)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 1)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 2)
        ->isSelected());
    $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 = [
        'card_1' => 0,
    ];
    $this->drupalPostForm(NULL, $edit, t('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');
    $this->assertTrue($this->assertSession()
        ->optionExists('card_1', 0)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 1)
        ->isSelected());
    $this->assertFalse($this->assertSession()
        ->optionExists('card_1', 2)
        ->isSelected());
    // Submit form: Unselect the option.
    $edit = [
        'card_1' => '_none',
    ];
    $this->drupalPostForm('entity_test/manage/' . $entity->id() . '/edit', $edit, t('Save'));
    $this->assertFieldValues($entity_init, 'card_1', []);
}

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