You are here

public function FormValidatorTest::testValidateInvalidFormToken in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 118
Contains \Drupal\Tests\Core\Form\FormValidatorTest.

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(array(), array(), array(), array(), array(), array(
    '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,
  ])
    ->setMethods(array(
    'doValidateForm',
  ))
    ->getMock();
  $form_validator
    ->expects($this
    ->never())
    ->method('doValidateForm');
  $form['#token'] = 'test_form_id';
  $form_state = $this
    ->getMockBuilder('Drupal\\Core\\Form\\FormState')
    ->setMethods(array(
    'setErrorByName',
  ))
    ->getMock();
  $form_state
    ->expects($this
    ->once())
    ->method('setErrorByName')
    ->with('form_token', 'The form has become outdated. Copy any unsaved work in the form below and then <a href="/test/example?foo=bar">reload this page</a>.');
  $form_state
    ->setValue('form_token', 'some_random_token');
  $form_validator
    ->validateForm('test_form_id', $form, $form_state);
  $this
    ->assertTrue($form_state
    ->isValidationComplete());
}