public function FormTest::testInputWithInvalidToken in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Form/FormTest.php \Drupal\system\Tests\Form\FormTest::testInputWithInvalidToken()
Tests that input is retained for safe elements even with an invalid token.
Submits a test form containing several types of form elements.
File
- core/
modules/ system/ src/ Tests/ Form/ FormTest.php, line 241 - Contains \Drupal\system\Tests\Form\FormTest.
Class
- FormTest
- Tests various form element validation mechanisms.
Namespace
Drupal\system\Tests\FormCode
public function testInputWithInvalidToken() {
// We need to be logged in to have CSRF tokens.
$account = $this
->createUser();
$this
->drupalLogin($account);
// Submit again with required fields set but an invalid form token and
// verify that all the values are retained.
$edit = array(
'textfield' => $this
->randomString(),
'checkboxes[bar]' => TRUE,
'select' => 'bar',
'radios' => 'foo',
'form_token' => 'invalid token',
);
$this
->drupalPostForm(Url::fromRoute('form_test.validate_required'), $edit, 'Submit');
$this
->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
$this
->assertText('The form has become outdated. Copy any unsaved work in the form below');
// Verify that input elements retained the posted values.
$this
->assertFieldByName('textfield', $edit['textfield']);
$this
->assertNoFieldChecked('edit-checkboxes-foo');
$this
->assertFieldChecked('edit-checkboxes-bar');
$this
->assertOptionSelected('edit-select', 'bar');
$this
->assertFieldChecked('edit-radios-foo');
// Check another form that has a textarea input.
$edit = array(
'textfield' => $this
->randomString(),
'textarea' => $this
->randomString() . "\n",
'form_token' => 'invalid token',
);
$this
->drupalPostForm(Url::fromRoute('form_test.required'), $edit, 'Submit');
$this
->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
$this
->assertText('The form has become outdated. Copy any unsaved work in the form below');
$this
->assertFieldByName('textfield', $edit['textfield']);
$this
->assertFieldByName('textarea', $edit['textarea']);
// Check another form that has a number input.
$edit = array(
'integer_step' => mt_rand(1, 100),
'form_token' => 'invalid token',
);
$this
->drupalPostForm(Url::fromRoute('form_test.number'), $edit, 'Submit');
$this
->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
$this
->assertText('The form has become outdated. Copy any unsaved work in the form below');
$this
->assertFieldByName('integer_step', $edit['integer_step']);
// Check a form with a Url field
$edit = array(
'url' => $this
->randomString(),
'form_token' => 'invalid token',
);
$this
->drupalPostForm(Url::fromRoute('form_test.url'), $edit, 'Submit');
$this
->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
$this
->assertText('The form has become outdated. Copy any unsaved work in the form below');
$this
->assertFieldByName('url', $edit['url']);
}