function FormTest::testColorValidation

Tests validation of #type 'color' elements.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php, line 438
Definition of Drupal\system\Tests\Form\FormTest.

Class

FormTest

Namespace

Drupal\system\Tests\Form

Code

function testColorValidation() {

  // Keys are inputs, values are expected results.
  $values = array(
    '' => '#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 = array(
      'color' => $input,
    );
    $result = json_decode($this
      ->drupalPost('form-test/color', $edit, 'Submit'));
    $this
      ->assertEqual($result->color, $expected);
  }

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