function FormTest::testNumber

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testNumber()
  2. 8.9.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testNumber()
  3. 10 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testNumber()

Tests validation of #type 'number' and 'range' elements.

File

core/modules/system/tests/src/Functional/Form/FormTest.php, line 609

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testNumber() : void {
  $form = \Drupal::formBuilder()->getForm('\\Drupal\\form_test\\Form\\FormTestNumberForm');
  // The expected errors.
  $expected = [
    'integer_no_number' => 'no_number',
    'integer_no_step' => 0,
    'integer_no_step_step_error' => 'step_mismatch',
    'integer_step' => 0,
    'integer_step_error' => 'step_mismatch',
    'integer_step_min' => 0,
    'integer_step_min_error' => 'too_low',
    'integer_step_max' => 0,
    'integer_step_max_error' => 'too_high',
    'integer_step_min_border' => 0,
    'integer_step_max_border' => 0,
    'integer_step_based_on_min' => 0,
    'integer_step_based_on_min_error' => 'step_mismatch',
    'float_small_step' => 0,
    'float_step_no_error' => 0,
    'float_step_error' => 'step_mismatch',
    'float_step_hard_no_error' => 0,
    'float_step_hard_error' => 'step_mismatch',
    'float_step_any_no_error' => 0,
  ];
  // First test the number element type, then range.
  foreach ([
    'form-test/number',
    'form-test/number/range',
  ] as $path) {
    // Post form and show errors.
    $this->drupalGet($path);
    $this->submitForm([], 'Submit');
    foreach ($expected as $element => $error) {
      // Array with all the error messages to be checked.
      $name = $form[$element]['#title'];
      $min = $form[$element]['#min'] ?? '0';
      $max = $form[$element]['#max'] ?? '0';
      $error_messages = [
        'no_number' => "<em class=\"placeholder\">{$name}</em> must be a number.",
        'too_low' => "<em class=\"placeholder\">{$name}</em> must be higher than or equal to <em class=\"placeholder\">{$min}</em>.",
        'too_high' => "<em class=\"placeholder\">{$name}</em> must be lower than or equal to <em class=\"placeholder\">{$max}</em>.",
        'step_mismatch' => "<em class=\"placeholder\">{$name}</em> is not a valid number.",
      ];
      foreach ($error_messages as $id => $message) {
        // Check if the error exists on the page, if the current message ID is
        // expected. Otherwise ensure that the error message is not present.
        if ($id === $error) {
          $this->assertSession()
            ->responseContains($message);
        }
        else {
          $this->assertSession()
            ->responseNotContains($message);
        }
      }
    }
  }
}

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