You are here

public function FormTest::testColorValidation in Drupal 8

Same name and namespace in other branches
  1. 9 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 683

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testColorValidation() {

  // 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
      ->drupalPostForm('form-test/color', $edit, 'Submit');
    $result = json_decode($this
      ->getSession()
      ->getPage()
      ->getContent());
    $this
      ->assertEqual($result->color, $expected);
  }

  // Tests invalid values are rejected.
  $values = [
    '#0008',
    '#1234',
    '#fffffg',
    '#abcdef22',
    '17',
    '#uaa',
  ];
  foreach ($values as $input) {
    $edit = [
      'color' => $input,
    ];
    $this
      ->drupalPostForm('form-test/color', $edit, 'Submit');
    $this
      ->assertRaw(t('%name must be a valid color.', [
      '%name' => 'Color',
    ]));
  }
}