function ValidationTest::testValidate
Same name in other branches
- 8.9.x core/modules/system/tests/src/Functional/Form/ValidationTest.php \Drupal\Tests\system\Functional\Form\ValidationTest::testValidate()
- 10 core/modules/system/tests/src/Functional/Form/ValidationTest.php \Drupal\Tests\system\Functional\Form\ValidationTest::testValidate()
- 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 30
Class
- ValidationTest
- Tests form processing and alteration via form validation handlers.
Namespace
Drupal\Tests\system\Functional\FormCode
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->submitForm($edit, 'Save');
$this->assertSession()
->fieldValueEquals('name', '#value changed by #element_validate');
$this->assertSession()
->pageTextContains('Name value: value changed by setValueForElement() in #element_validate');
// Verify that #validate handlers can alter the form and submitted
// form values.
$edit = [
'name' => 'validate',
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->fieldValueEquals('name', '#value changed by #validate');
$this->assertSession()
->pageTextContains('Name value: value changed by setValueForElement() in #validate');
// Verify that #element_validate handlers can make form elements
// inaccessible, but values persist.
$edit = [
'name' => 'element_validate_access',
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->fieldNotExists('name');
$this->assertSession()
->pageTextContains('Name value: element_validate_access');
// Verify that value for inaccessible form element persists.
$this->submitForm([], 'Save');
$this->assertSession()
->fieldValueNotEquals('name', 'Form element was hidden.');
$this->assertSession()
->pageTextContains('Name value: element_validate_access');
// 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->submitForm() 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->submitForm([
'name' => 'validate',
], 'Save');
$this->assertSession()
->fieldValueNotEquals('name', '#value changed by #validate');
$this->assertSession()
->pageTextNotContains('Name value: value changed by setValueForElement() in #validate');
$this->assertSession()
->pageTextContains('The form has become outdated.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.