function FormValidatorTest::testValidateInvalidFormToken

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php \Drupal\Tests\Core\Form\FormValidatorTest::testValidateInvalidFormToken()

@covers ::validateForm

File

core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php, line 114

Class

FormValidatorTest
@coversDefaultClass \Drupal\Core\Form\FormValidator[[api-linebreak]] @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testValidateInvalidFormToken() : void {
  $request_stack = new RequestStack();
  $request = new Request([], [], [], [], [], [
    'REQUEST_URI' => '/test/example?foo=bar',
  ]);
  $request_stack->push($request);
  $this->csrfToken
    ->expects($this->once())
    ->method('validate')
    ->willReturn(FALSE);
  $form_validator = $this->getMockBuilder('Drupal\\Core\\Form\\FormValidator')
    ->setConstructorArgs([
    $request_stack,
    $this->getStringTranslationStub(),
    $this->csrfToken,
    $this->logger,
    $this->formErrorHandler,
  ])
    ->onlyMethods([
    'doValidateForm',
  ])
    ->getMock();
  $form_validator->expects($this->never())
    ->method('doValidateForm');
  $form['#token'] = 'test_form_id';
  $form_state = $this->getMockBuilder('Drupal\\Core\\Form\\FormState')
    ->onlyMethods([
    'setErrorByName',
  ])
    ->getMock();
  $form_state->expects($this->once())
    ->method('setErrorByName')
    ->with('form_token', 'The form has become outdated. Press the back button, copy any unsaved work in the form, and then reload the page.');
  $form_state->setValue('form_token', 'some_random_token');
  $form_validator->validateForm('test_form_id', $form, $form_state);
  $this->assertTrue($form_state->isValidationComplete());
}

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