function OptionsWidgetsTest::testSelectListRequiredErrorAttribute

Same name in other branches
  1. 10 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListRequiredErrorAttribute()

Tests the '#required_error' attribute for the select list.

File

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

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\Tests\options\Functional

Code

public function testSelectListRequiredErrorAttribute() : void {
    // Enable form alter hook.
    \Drupal::state()->set('options_test.form_alter_enable', TRUE);
    // 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();
    $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
    // A required field without any value has a "none" option.
    $option = $this->assertSession()
        ->optionExists('edit-card-1', '_none');
    $this->assertSame('- Select a value -', $option->getText());
    // Submit form: select invalid 'none' option.
    $edit = [
        'card_1' => '_none',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->responseContains(t('This is custom message for required field.'));
}

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