You are here

public function VarnishPurgerFormTestBase::testFormValidation in Varnish purger 8.2

Same name and namespace in other branches
  1. 8 src/Tests/VarnishPurgerFormTestBase.php \Drupal\varnish_purger\Tests\VarnishPurgerFormTestBase::testFormValidation()

Test validating the data.

File

src/Tests/VarnishPurgerFormTestBase.php, line 63

Class

VarnishPurgerFormTestBase
Testbase for testing \Drupal\varnish_purger\Form\VarnishPurgerFormBase.

Namespace

Drupal\varnish_purger\Tests

Code

public function testFormValidation() {

  // Assert that valid timeout values don't cause validation errors.
  $form_state = $this
    ->getFormStateInstance();
  $form_state
    ->addBuildInfo('args', [
    $this->formArgs,
  ]);
  $form_state
    ->setValues([
    'connect_timeout' => 0.3,
    'timeout' => 0.1,
    'name' => 'foobar',
  ]);
  $form = $this
    ->getFormInstance();
  $this->formBuilder
    ->submitForm($form, $form_state);
  $this
    ->assertEquals(0, count($form_state
    ->getErrors()));
  $form_state = $this
    ->getFormStateInstance();
  $form_state
    ->addBuildInfo('args', [
    $this->formArgs,
  ]);
  $form_state
    ->setValues([
    'connect_timeout' => 2.3,
    'timeout' => 7.7,
    'name' => 'foobar',
  ]);
  $form = $this
    ->getFormInstance();
  $this->formBuilder
    ->submitForm($form, $form_state);
  $this
    ->assertEquals(0, count($form_state
    ->getErrors()));

  // Submit timeout values that are too low and confirm the validation error.
  $form_state = $this
    ->getFormStateInstance();
  $form_state
    ->addBuildInfo('args', [
    $this->formArgs,
  ]);
  $form_state
    ->setValues([
    'connect_timeout' => 0.0,
    'timeout' => 0.0,
    'name' => 'foobar',
  ]);
  $form = $this
    ->getFormInstance();
  $this->formBuilder
    ->submitForm($form, $form_state);
  $errors = $form_state
    ->getErrors();
  $this
    ->assertEquals(2, count($errors));
  $this
    ->assertTrue(isset($errors['timeout']));
  $this
    ->assertTrue(isset($errors['connect_timeout']));

  // Submit timeout values that are too high and confirm the validation error.
  $form_state = $this
    ->getFormStateInstance();
  $form_state
    ->addBuildInfo('args', [
    $this->formArgs,
  ]);
  $form_state
    ->setValues([
    'connect_timeout' => 2.4,
    'timeout' => 7.7,
    'name' => 'foobar',
  ]);
  $form = $this
    ->getFormInstance();
  $this->formBuilder
    ->submitForm($form, $form_state);
  $errors = $form_state
    ->getErrors();
  $this
    ->assertEquals(2, count($errors));
  $this
    ->assertTrue(isset($errors['timeout']));
  $this
    ->assertTrue(isset($errors['connect_timeout']));
}