You are here

public function FormValidatorTest::testPerformRequiredValidation 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::testPerformRequiredValidation()
  2. 9 core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php \Drupal\Tests\Core\Form\FormValidatorTest::testPerformRequiredValidation()

@covers ::performRequiredValidation

@dataProvider providerTestPerformRequiredValidation

File

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

Class

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

Namespace

Drupal\Tests\Core\Form

Code

public function testPerformRequiredValidation($element, $expected_message, $call_watchdog) {
  $form_validator = $this
    ->getMockBuilder('Drupal\\Core\\Form\\FormValidator')
    ->setConstructorArgs([
    new RequestStack(),
    $this
      ->getStringTranslationStub(),
    $this->csrfToken,
    $this->logger,
    $this->formErrorHandler,
  ])
    ->addMethods([
    'setError',
  ])
    ->getMock();
  if ($call_watchdog) {
    $this->logger
      ->expects($this
      ->once())
      ->method('error')
      ->with($this
      ->isType('string'), $this
      ->isType('array'));
  }
  $form = [];
  $form['test'] = $element + [
    '#title' => 'Test',
    '#needs_validation' => TRUE,
    '#required' => FALSE,
    '#parents' => [
      'test',
    ],
  ];
  $form_state = $this
    ->getMockBuilder('Drupal\\Core\\Form\\FormState')
    ->onlyMethods([
    'setError',
  ])
    ->getMock();
  $form_state
    ->expects($this
    ->once())
    ->method('setError')
    ->with($this
    ->isType('array'), $expected_message);
  $form_validator
    ->validateForm('test_form_id', $form, $form_state);
}