FormValidator example

$this->kernel = $this->getMockBuilder('\Drupal\Core\DrupalKernel')
      ->disableOriginalConstructor()
      ->getMock();
    $this->account = $this->createMock('Drupal\Core\Session\AccountInterface');
    $this->themeManager = $this->createMock('Drupal\Core\Theme\ThemeManagerInterface');
    $this->request = Request::createFromGlobals();
    $this->eventDispatcher = $this->createMock('Symfony\Contracts\EventDispatcher\EventDispatcherInterface');
    $this->requestStack = new RequestStack();
    $this->requestStack->push($this->request);
    $this->logger = $this->createMock('Drupal\Core\Logger\LoggerChannelInterface');
    $form_error_handler = $this->createMock('Drupal\Core\Form\FormErrorHandlerInterface');
    $this->formValidator = new FormValidator($this->requestStack, $this->getStringTranslationStub()$this->csrfToken, $this->logger, $form_error_handler);
    $this->formSubmitter = $this->getMockBuilder('Drupal\Core\Form\FormSubmitter')
      ->setConstructorArgs([$this->requestStack, $this->urlGenerator])
      ->onlyMethods(['batchGet'])
      ->getMock();
    $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);

    $this->formBuilder = new FormBuilder($this->formValidator, $this->formSubmitter, $this->formCache, $this->moduleHandler, $this->eventDispatcher, $this->requestStack, $this->classResolver, $this->elementInfo, $this->themeManager, $this->csrfToken);
  }

  /** * {@inheritdoc} */
$constraint = new Form();

        $this->validator->initialize($context);
        $this->validator->validate($form$constraint);

        $this->assertCount(1, $context->getViolations());
        $this->assertSame($constraint$context->getViolations()->get(0)->getConstraint());
    }

    protected function createValidator(): FormValidator
    {
        return new FormValidator();
    }

    private function getBuilder(string $name = 'name', string $dataClass = null, array $options = []): FormBuilder
    {
        $options = array_replace([
            'constraints' => [],
            'invalid_message_parameters' => [],
        ]$options);

        return new FormBuilder($name$dataClass$this->dispatcher, $this->factory, $options);
    }

    
->getMock();
    $this->formErrorHandler = $this->createMock('Drupal\Core\Form\FormErrorHandlerInterface');
  }

  /** * Tests the 'validation_complete' $form_state flag. * * @covers ::validateForm * @covers ::finalizeValidation */
  public function testValidationComplete() {
    $form_validator = new FormValidator(new RequestStack()$this->getStringTranslationStub()$this->csrfToken, $this->logger, $this->formErrorHandler);

    $form = [];
    $form_state = new FormState();
    $this->assertFalse($form_state->isValidationComplete());
    $form_validator->validateForm('test_form_id', $form$form_state);
    $this->assertTrue($form_state->isValidationComplete());
  }

  /** * Tests the 'must_validate' $form_state flag. * * @covers ::validateForm */
Home | Imprint | This part of the site doesn't use cookies.