function ElementTest::testOptions

Same name in other branches
  1. 9 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 53

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->assertIdentical((string) $element->getAttribute('value'), $expected);
        }
    }
    // Verify that the choices are admin filtered as expected.
    $this->assertRaw("<em>Special Char</em>alert('checkboxes');");
    $this->assertRaw("<em>Special Char</em>alert('radios');");
    $this->assertRaw('<em>Bar - checkboxes</em>');
    $this->assertRaw('<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->assertIdentical((string) $element->getAttribute('value'), $expected);
        }
    }
    // Verify that custom #description properties are output.
    foreach ([
        'checkboxes',
        'radios',
    ] as $type) {
        $elements = $this->xpath('//input[@id=:id]/following-sibling::div[@class=:class]', [
            ':id' => 'edit-' . $type . '-foo',
            ':class' => 'description',
        ]);
        $this->assertGreaterThan(0, count($elements), new FormattableMarkup('Custom %type option description found.', [
            '%type' => $type,
        ]));
    }
}

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