function ElementTest::testOptions

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/ElementTest.php \Drupal\Tests\system\Functional\Form\ElementTest::testOptions()
  2. 8.9.x core/modules/system/tests/src/Functional/Form/ElementTest.php \Drupal\Tests\system\Functional\Form\ElementTest::testOptions()
  3. 11.x core/modules/system/tests/src/Functional/Form/ElementTest.php \Drupal\Tests\system\Functional\Form\ElementTest::testOptions()

Tests expansion of #options for #type checkboxes and radios.

1 call to ElementTest::testOptions()
ElementTest::testFormElements in core/modules/system/tests/src/Functional/Form/ElementTest.php
Test form elements.

File

core/modules/system/tests/src/Functional/Form/ElementTest.php, line 56

Class

ElementTest
Tests building and processing of core form elements.

Namespace

Drupal\Tests\system\Functional\Form

Code

protected function testOptions() : void {
  $this->drupalGet('form-test/checkboxes-radios');
  // Verify that all options appear in their defined order.
  foreach ([
    'checkbox',
    'radio',
  ] as $type) {
    $elements = $this->xpath('//input[@type=:type]', [
      ':type' => $type,
    ]);
    $expected_values = [
      '0',
      'foo',
      '1',
      'bar',
      '>',
    ];
    foreach ($elements as $element) {
      $expected = array_shift($expected_values);
      $this->assertSame($expected, (string) $element->getAttribute('value'));
    }
  }
  // Verify that the choices are admin filtered as expected.
  $this->assertSession()
    ->responseContains("<em>Special Char</em>alert('checkboxes');");
  $this->assertSession()
    ->responseContains("<em>Special Char</em>alert('radios');");
  $this->assertSession()
    ->responseContains('<em>Bar - checkboxes</em>');
  $this->assertSession()
    ->responseContains('<em>Bar - radios</em>');
  // Enable customized option sub-elements.
  $this->drupalGet('form-test/checkboxes-radios/customize');
  // Verify that all options appear in their defined order, taking a custom
  // #weight into account.
  foreach ([
    'checkbox',
    'radio',
  ] as $type) {
    $elements = $this->xpath('//input[@type=:type]', [
      ':type' => $type,
    ]);
    $expected_values = [
      '0',
      'foo',
      'bar',
      '>',
      '1',
    ];
    foreach ($elements as $element) {
      $expected = array_shift($expected_values);
      $this->assertSame($expected, (string) $element->getAttribute('value'));
    }
  }
  // Verify that custom #description properties are output.
  foreach ([
    'checkboxes',
    'radios',
  ] as $type) {
    $this->assertSession()
      ->elementExists('xpath', "//input[@id='edit-{$type}-foo']/following-sibling::div[@class='description']");
  }
}

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