You are here

public function FormTest::testSelect in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testSelect()

Tests validation of #type 'select' elements.

File

core/modules/system/tests/src/Functional/Form/FormTest.php, line 408

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testSelect() {
  $form = \Drupal::formBuilder()
    ->getForm('Drupal\\form_test\\Form\\FormTestSelectForm');
  $this
    ->drupalGet('form-test/select');

  // Verify that the options are escaped as expected.
  $this
    ->assertSession()
    ->assertEscaped('<strong>four</strong>');
  $this
    ->assertSession()
    ->responseNotContains('<strong>four</strong>');

  // Posting without any values should throw validation errors.
  $this
    ->submitForm([], 'Submit');
  $no_errors = [
    'select',
    'select_required',
    'select_optional',
    'empty_value',
    'empty_value_one',
    'no_default_optional',
    'no_default_empty_option_optional',
    'no_default_empty_value_optional',
    'multiple',
    'multiple_no_default',
  ];
  foreach ($no_errors as $key) {
    $this
      ->assertSession()
      ->pageTextNotContains($form[$key]['#title'] . ' field is required.');
  }
  $expected_errors = [
    'no_default',
    'no_default_empty_option',
    'no_default_empty_value',
    'no_default_empty_value_one',
    'multiple_no_default_required',
  ];
  foreach ($expected_errors as $key) {
    $this
      ->assertSession()
      ->pageTextContains($form[$key]['#title'] . ' field is required.');
  }

  // Post values for required fields.
  $edit = [
    'no_default' => 'three',
    'no_default_empty_option' => 'three',
    'no_default_empty_value' => 'three',
    'no_default_empty_value_one' => 'three',
    'multiple_no_default_required[]' => 'three',
  ];
  $this
    ->submitForm($edit, 'Submit');
  $values = Json::decode($this
    ->getSession()
    ->getPage()
    ->getContent());

  // Verify expected values.
  $expected = [
    'select' => 'one',
    'empty_value' => 'one',
    'empty_value_one' => 'one',
    'no_default' => 'three',
    'no_default_optional' => 'one',
    'no_default_optional_empty_value' => '',
    'no_default_empty_option' => 'three',
    'no_default_empty_option_optional' => '',
    'no_default_empty_value' => 'three',
    'no_default_empty_value_one' => 'three',
    'no_default_empty_value_optional' => 0,
    'multiple' => [
      'two' => 'two',
    ],
    'multiple_no_default' => [],
    'multiple_no_default_required' => [
      'three' => 'three',
    ],
  ];
  foreach ($expected as $key => $value) {
    $this
      ->assertSame($value, $values[$key], new FormattableMarkup('@name: @actual is equal to @expected.', [
      '@name' => $key,
      '@actual' => var_export($values[$key], TRUE),
      '@expected' => var_export($value, TRUE),
    ]));
  }
}