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. 8.9.x 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()

Tests default value handling for checkboxes.

See also

_form_test_checkbox()

File

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

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testCheckboxProcessing() : void {
    // First, try to submit without the required checkbox.
    $edit = [];
    $this->drupalGet('form-test/checkbox');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->pageTextContains("required_checkbox field is required.");
    // Now try to submit the form correctly.
    $this->submitForm([
        'required_checkbox' => 1,
    ], '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, sprintf('Checkbox %s returns expected value (expected: %s, got: %s)', var_export($widget, TRUE), var_export($expected_value, TRUE), var_export($values[$widget], TRUE)));
    }
}

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