function ElementTest::testRadiosChecked

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

Tests correct checked attribute for radios element.

File

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

Class

ElementTest
Tests building and processing of core form elements.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testRadiosChecked() {
    // Verify that there is only one radio option checked.
    $this->drupalGet('form-test/radios-checked');
    $elements = $this->xpath('//input[@name="radios" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('0', $elements[0]->getValue());
    $elements = $this->xpath('//input[@name="radios-string" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('bar', $elements[0]->getValue());
    $elements = $this->xpath('//input[@name="radios-boolean-true" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('1', $elements[0]->getValue());
    // A default value of FALSE indicates that nothing is set.
    $elements = $this->xpath('//input[@name="radios-boolean-false" and @checked]');
    $this->assertCount(0, $elements);
    $elements = $this->xpath('//input[@name="radios-boolean-any" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('All', $elements[0]->getValue());
    $elements = $this->xpath('//input[@name="radios-string-zero" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('0', $elements[0]->getValue());
    $elements = $this->xpath('//input[@name="radios-int-non-zero" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('10', $elements[0]->getValue());
    $elements = $this->xpath('//input[@name="radios-int-non-zero-as-string" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('100', $elements[0]->getValue());
    $elements = $this->xpath('//input[@name="radios-empty-string" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('0', $elements[0]->getValue());
    $elements = $this->xpath('//input[@name="radios-empty-array" and @checked]');
    $this->assertCount(0, $elements);
    $elements = $this->xpath('//input[@name="radios-key-FALSE" and @checked]');
    $this->assertCount(1, $elements);
    $this->assertSame('0', $elements[0]->getValue());
}

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