function FormValidatorTest::testExecuteValidateHandlers

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

Tests execute validate handlers.

File

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

Class

FormValidatorTest
Tests Drupal\Core\Form\FormValidator.

Namespace

Drupal\Tests\Core\Form

Code

public function testExecuteValidateHandlers() : void {
  $form_validator = new FormValidator(new RequestStack(), $this->getStringTranslationStub(), $this->csrfToken, $this->logger, $this->formErrorHandler, $this->callableResolver);
  $mock = $this->getMockBuilder(FormValidatorTestMockInterface::class)
    ->onlyMethods([
    'validate_handler',
    'hash_validate',
    'element_validate',
  ])
    ->getMock();
  $mock->expects($this->once())
    ->method('validate_handler')
    ->with($this->isArray(), $this->isInstanceOf('Drupal\\Core\\Form\\FormStateInterface'));
  $mock->expects($this->once())
    ->method('hash_validate')
    ->with($this->isArray(), $this->isInstanceOf('Drupal\\Core\\Form\\FormStateInterface'));
  $this->callableResolver
    ->method('getCallableFromDefinition')
    ->willReturn([
    $mock,
    'validate_handler',
  ], [
    $mock,
    'hash_validate',
  ], [
    $mock,
    'element_validate',
  ]);
  $form = [];
  $form_state = new FormState();
  $form_validator->executeValidateHandlers($form, $form_state);
  $form['#validate'][] = [
    $mock,
    'hash_validate',
  ];
  $form_validator->executeValidateHandlers($form, $form_state);
  // $form_state validate handlers will supersede $form handlers.
  $validate_handlers[] = [
    $mock,
    'validate_handler',
  ];
  $form_state->setValidateHandlers($validate_handlers);
  $form_validator->executeValidateHandlers($form, $form_state);
}

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