You are here

public function FormValidatorTest::testValidateInvalidFormToken in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php \Drupal\Tests\Core\Form\FormValidatorTest::testValidateInvalidFormToken()
  2. 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 111

Class

FormValidatorTest
@coversDefaultClass \Drupal\Core\Form\FormValidator @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testValidateInvalidFormToken() {
  $request_stack = new RequestStack();
  $request = new Request([], [], [], [], [], [
    'REQUEST_URI' => '/test/example?foo=bar',
  ]);
  $request_stack
    ->push($request);
  $this->csrfToken
    ->expects($this
    ->once())
    ->method('validate')
    ->will($this
    ->returnValue(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());
}