function FormTest::testCheckboxProcessing

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

Test default value handling for checkboxes.

See also

_form_test_checkbox()

File

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

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testCheckboxProcessing() {
    // First, try to submit without the required checkbox.
    $edit = [];
    $this->drupalPostForm('form-test/checkbox', $edit, t('Submit'));
    $this->assertRaw(t('@name field is required.', [
        '@name' => 'required_checkbox',
    ]), 'A required checkbox is actually mandatory');
    // Now try to submit the form correctly.
    $this->drupalPostForm(NULL, [
        'required_checkbox' => 1,
    ], t('Submit'));
    $values = Json::decode($this->getSession()
        ->getPage()
        ->getContent());
    $expected_values = [
        'disabled_checkbox_on' => 'disabled_checkbox_on',
        'disabled_checkbox_off' => 0,
        'checkbox_on' => 'checkbox_on',
        'checkbox_off' => 0,
        'zero_checkbox_on' => '0',
        'zero_checkbox_off' => 0,
    ];
    foreach ($expected_values as $widget => $expected_value) {
        $this->assertSame($values[$widget], $expected_value, new FormattableMarkup('Checkbox %widget returns expected value (expected: %expected, got: %value)', [
            '%widget' => var_export($widget, TRUE),
            '%expected' => var_export($expected_value, TRUE),
            '%value' => var_export($values[$widget], TRUE),
        ]));
    }
}

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