function ElementTest::testOptions

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Form/ElementTest.php \Drupal\Tests\system\Functional\Form\ElementTest::testOptions()
  2. 10 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.

File

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

Class

ElementTest
Tests building and processing of core form elements.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testOptions() {
    $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.