function FormValidatorTest::testValidateValidFormToken

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php \Drupal\Tests\Core\Form\FormValidatorTest::testValidateValidFormToken()
  2. 8.9.x core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php \Drupal\Tests\Core\Form\FormValidatorTest::testValidateValidFormToken()
  3. 11.x core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php \Drupal\Tests\Core\Form\FormValidatorTest::testValidateValidFormToken()

@covers ::validateForm

File

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

Class

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

Namespace

Drupal\Tests\Core\Form

Code

public function testValidateValidFormToken() : void {
    $request_stack = new RequestStack();
    $this->csrfToken
        ->expects($this->once())
        ->method('validate')
        ->willReturn(TRUE);
    $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->once())
        ->method('doValidateForm');
    $form['#token'] = 'test_form_id';
    $form_state = $this->getMockBuilder('Drupal\\Core\\Form\\FormState')
        ->onlyMethods([
        'setErrorByName',
    ])
        ->getMock();
    $form_state->expects($this->never())
        ->method('setErrorByName');
    $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.