function 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/tests/src/Functional/Form/FormTest.php, line 244

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

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.
  $this->drupalGet(Url::fromRoute('form_test.validate_required'));
  $this->assertSession()
    ->elementExists('css', 'input[name="form_token"]')
    ->setValue('invalid token');
  $random_string = $this->randomString();
  $edit = [
    'textfield' => $random_string,
    'checkboxes[bar]' => TRUE,
    'select' => 'bar',
    'radios' => 'foo',
  ];
  $this->drupalPostForm(NULL, $edit, 'Submit');
  $this->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
  $assert = $this->assertSession();
  $element = $assert->fieldExists('textfield');
  $this->assertEmpty($element->getValue());
  $assert->responseNotContains($random_string);
  $this->assertText('The form has become outdated.');
  // Ensure that we don't use the posted values.
  $this->assertFieldByName('textfield', '');
  $this->assertNoFieldChecked('edit-checkboxes-foo');
  $this->assertNoFieldChecked('edit-checkboxes-bar');
  $this->assertOptionSelected('edit-select', '');
  $this->assertNoFieldChecked('edit-radios-foo');
  // Check another form that has a textarea input.
  $this->drupalGet(Url::fromRoute('form_test.required'));
  $this->assertSession()
    ->elementExists('css', 'input[name="form_token"]')
    ->setValue('invalid token');
  $edit = [
    'textfield' => $this->randomString(),
    'textarea' => $this->randomString() . "\n",
  ];
  $this->drupalPostForm(NULL, $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.');
  $this->assertFieldByName('textfield', '');
  $this->assertFieldByName('textarea', '');
  // Check another form that has a number input.
  $this->drupalGet(Url::fromRoute('form_test.number'));
  $this->assertSession()
    ->elementExists('css', 'input[name="form_token"]')
    ->setValue('invalid token');
  $edit = [
    // We choose a random value which is higher than the default value,
    // so we don't accidentally generate the default value.
'integer_step' => mt_rand(6, 100),
  ];
  $this->drupalPostForm(NULL, $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.');
  $this->assertFieldByName('integer_step', 5);
  // Check a form with a Url field
  $this->drupalGet(Url::fromRoute('form_test.url'));
  $this->assertSession()
    ->elementExists('css', 'input[name="form_token"]')
    ->setValue('invalid token');
  $edit = [
    'url' => $this->randomString(),
  ];
  $this->drupalPostForm(NULL, $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.');
  $this->assertFieldByName('url', '');
}

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