function FormTest::testColorValidation

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

Tests validation of #type 'color' elements.

File

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

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testColorValidation() : void {
  // Keys are inputs, values are expected results.
  $values = [
    '' => '#000000',
    '#000' => '#000000',
    'AAA' => '#aaaaaa',
    '#af0DEE' => '#af0dee',
    '#99ccBc' => '#99ccbc',
    '#aabbcc' => '#aabbcc',
    '123456' => '#123456',
  ];
  // Tests that valid values are properly normalized.
  foreach ($values as $input => $expected) {
    $edit = [
      'color' => $input,
    ];
    $this->drupalGet('form-test/color');
    $this->submitForm($edit, 'Submit');
    $result = json_decode($this->getSession()
      ->getPage()
      ->getContent());
    $this->assertEquals($expected, $result->color);
  }
  // Tests invalid values are rejected.
  // cspell:ignore fffffg
  $values = [
    '#0008',
    '#1234',
    '#fffffg',
    '#abcdef22',
    '17',
    '#uaa',
  ];
  foreach ($values as $input) {
    $edit = [
      'color' => $input,
    ];
    $this->drupalGet('form-test/color');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
      ->pageTextContains("Color must be a valid color.");
  }
}

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