public function FormTest::testColorValidation in Drupal 9
Same name and namespace in other branches
- 8 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 693
Class
- FormTest
- Tests various form element validation mechanisms.
Namespace
Drupal\Tests\system\Functional\FormCode
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
->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.
$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.");
}
}