function ValidationTest::testValidate

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

Tests #element_validate and #validate.

File

core/modules/system/tests/src/Functional/Form/ValidationTest.php, line 31

Class

ValidationTest
Tests form processing and alteration via form validation handlers.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testValidate() {
    $this->drupalGet('form-test/validate');
    // Verify that #element_validate handlers can alter the form and submitted
    // form values.
    $edit = [
        'name' => 'element_validate',
    ];
    $this->drupalPostForm(NULL, $edit, 'Save');
    $this->assertFieldByName('name', '#value changed by #element_validate', 'Form element #value was altered.');
    $this->assertText('Name value: value changed by setValueForElement() in #element_validate', 'Form element value in $form_state was altered.');
    // Verify that #validate handlers can alter the form and submitted
    // form values.
    $edit = [
        'name' => 'validate',
    ];
    $this->drupalPostForm(NULL, $edit, 'Save');
    $this->assertFieldByName('name', '#value changed by #validate', 'Form element #value was altered.');
    $this->assertText('Name value: value changed by setValueForElement() in #validate', 'Form element value in $form_state was altered.');
    // Verify that #element_validate handlers can make form elements
    // inaccessible, but values persist.
    $edit = [
        'name' => 'element_validate_access',
    ];
    $this->drupalPostForm(NULL, $edit, 'Save');
    $this->assertNoFieldByName('name', 'Form element was hidden.');
    $this->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');
    // Verify that value for inaccessible form element persists.
    $this->drupalPostForm(NULL, [], 'Save');
    $this->assertNoFieldByName('name', 'Form element was hidden.');
    $this->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');
    // Verify that #validate handlers don't run if the CSRF token is invalid.
    $this->drupalLogin($this->drupalCreateUser());
    $this->drupalGet('form-test/validate');
    // $this->assertSession()->fieldExists() does not recognize hidden fields,
    // which breaks $this->drupalPostForm() if we try to change the value of a
    // hidden field such as form_token.
    $this->assertSession()
        ->elementExists('css', 'input[name="form_token"]')
        ->setValue('invalid_token');
    $this->drupalPostForm(NULL, [
        'name' => 'validate',
    ], 'Save');
    $this->assertNoFieldByName('name', '#value changed by #validate', 'Form element #value was not altered.');
    $this->assertNoText('Name value: value changed by setValueForElement() in #validate', 'Form element value in $form_state was not altered.');
    $this->assertText('The form has become outdated.');
}

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