Skip to content
Snippets Groups Projects
Commit 7846ad6f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1938184 by Xano, ParisLiakos, franskuipers, clemens.tolboom: Convert...

Issue #1938184 by Xano, ParisLiakos, franskuipers, clemens.tolboom: Convert Drupal\Core\Utility\Color\ColorTest to PHPUnit.
parent 74b5b36c
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -7,6 +7,8 @@
namespace Drupal\Core\Utility;
use Drupal\Component\Utility\Unicode;
/**
* Performs color conversions.
*/
......@@ -28,7 +30,7 @@ public static function validateHex($hex) {
// Hash prefix is optional.
$hex = ltrim($hex, '#');
// Must be either RGB or RRGGBB.
$length = drupal_strlen($hex);
$length = Unicode::strlen($hex);
$valid = $valid && ($length === 3 || $length === 6);
// Must be a valid hex value.
$valid = $valid && ctype_xdigit($hex);
......@@ -98,4 +100,5 @@ public static function rgbToHex($input) {
return '#' . str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}
}
......@@ -2,18 +2,18 @@
/**
* @file
* Definition of Drupal\system\Tests\Common\ColorTest.
* Contains \Drupal\Tests\Core\Utility\ColorTest.
*/
namespace Drupal\system\Tests\Common;
namespace Drupal\Tests\Core\Utility;
use Drupal\Core\Utility\Color;
use Drupal\simpletest\UnitTestBase;
use Drupal\Tests\UnitTestCase;
/**
* Tests color conversion functions.
*/
class ColorTest extends UnitTestBase {
class ColorTest extends UnitTestCase {
public static function getInfo() {
return array(
......@@ -25,37 +25,54 @@ public static function getInfo() {
/**
* Tests Color::hexToRgb().
*
* @param string $value
* The hex color value.
* @param string $expected
* The expected rgb color value.
* @param bool $invalid
* Whether this value is invalid and exception should be expected.
*
* @dataProvider providerTestHexToRgb
*/
function testHexToRgb() {
public function testHexToRgb($value, $expected, $invalid = FALSE) {
if ($invalid) {
$this->setExpectedException('InvalidArgumentException');
}
$this->assertSame($expected, Color::hexToRgb($value));
}
/**
* Data provider for testHexToRgb().
*
* @see testHexToRgb()
*
* @return array
* An array of arrays containing:
* - The hex color value.
* - The rgb color array value.
* - (optional) Boolean indicating invalid status. Defaults to FALSE.
*/
public function providerTestHexToRgb() {
$invalid = array();
// Any invalid arguments should throw an exception.
$values = array('', '-1', '1', '12', '12345', '1234567', '123456789', '123456789a', 'foo');
foreach (array('', '-1', '1', '12', '12345', '1234567', '123456789', '123456789a', 'foo') as $value) {
$invalid[] = array($value, '', TRUE);
}
// Duplicate all invalid value tests with additional '#' prefix.
// The '#' prefix inherently turns the data type into a string.
foreach ($values as $value) {
$values[] = '#' . $value;
foreach ($invalid as $value) {
$invalid[] = array('#' . $value[0], '', TRUE);
}
// Add invalid data types (hex value must be a string).
$values = array_merge($values, array(
foreach (array(
1, 12, 1234, 12345, 123456, 1234567, 12345678, 123456789, 123456789,
-1, PHP_INT_MAX, PHP_INT_MAX + 1, -PHP_INT_MAX,
0x0, 0x010,
));
foreach ($values as $test) {
$this->assertFalse(Color::validateHex($test), var_export($test, TRUE) . ' is invalid.');
try {
Color::hexToRgb($test);
$this->fail('Color::hexToRgb(' . var_export($test, TRUE) . ') did not throw an exception.');
}
catch (\InvalidArgumentException $e) {
$this->pass('Color::hexToRgb(' . var_export($test, TRUE) . ') threw an exception.');
}
-1, PHP_INT_MAX, PHP_INT_MAX + 1, -PHP_INT_MAX, 0x0, 0x010
) as $value) {
$invalid[] = array($value, '', TRUE);
}
// PHP automatically casts a numeric array key into an integer.
// Since hex values may consist of 0-9 only, they need to be defined as
// array values.
$tests = array(
// And some valid values.
$valid = array(
// Shorthands without alpha.
array('hex' => '#000', 'rgb' => array('red' => 0, 'green' => 0, 'blue' => 0)),
array('hex' => '#fff', 'rgb' => array('red' => 255, 'green' => 255, 'blue' => 255)),
......@@ -66,35 +83,50 @@ function testHexToRgb() {
array('hex' => '#ffffff', 'rgb' => array('red' => 255, 'green' => 255, 'blue' => 255)),
array('hex' => '#010203', 'rgb' => array('red' => 1, 'green' => 2, 'blue' => 3)),
);
foreach ($tests as $test) {
$result = Color::hexToRgb($test['hex']);
$this->assertIdentical($result, $test['rgb']);
}
return array_merge($invalid, $valid);
}
/**
* Tests Color::rgbToHex().
*
* @param string $value
* The rgb color value.
* @param string $expected
* The expected hex color value.
*
* @dataProvider providerTestRbgToHex
*/
function testRgbToHex() {
public function testRgbToHex($value, $expected) {
$this->assertSame($expected, Color::rgbToHex($value));
}
/**
* Data provider for testRgbToHex().
*
* @see testRgbToHex()
*
* @return array
* An array of arrays containing:
* - The rgb color array value.
* - The hex color value.
*/
public function providerTestRbgToHex() {
// Input using named RGB array (e.g., as returned by Color::hexToRgb()).
$tests = array(
'#000000' => array('red' => 0, 'green' => 0, 'blue' => 0),
'#ffffff' => array('red' => 255, 'green' => 255, 'blue' => 255),
'#777777' => array('red' => 119, 'green' => 119, 'blue' => 119),
'#010203' => array('red' => 1, 'green' => 2, 'blue' => 3),
array(array('red' => 0, 'green' => 0, 'blue' => 0), '#000000'),
array(array('red' => 255, 'green' => 255, 'blue' => 255), '#ffffff'),
array(array('red' => 119, 'green' => 119, 'blue' => 119), '#777777'),
array(array('red' => 1, 'green' => 2, 'blue' => 3), '#010203'),
);
// Input using named RGB array (e.g., as returned by Color::hexToRgb()).
foreach ($tests as $expected => $rgb) {
$this->assertIdentical(Color::rgbToHex($rgb), $expected);
}
// Input using indexed RGB array (e.g.: array(10, 10, 10)).
foreach ($tests as $expected => $rgb) {
$rgb = array_values($rgb);
$this->assertIdentical(Color::rgbToHex($rgb), $expected);
foreach ($tests as $test) {
$tests[] = array(array_values($test[0]), $test[1]);
}
// Input using CSS RGB string notation (e.g.: 10, 10, 10).
foreach ($tests as $expected => $rgb) {
$rgb = implode(', ', $rgb);
$this->assertIdentical(Color::rgbToHex($rgb), $expected);
foreach ($tests as $test) {
$tests[] = array(implode(', ', $test[0]), $test[1]);
}
return $tests;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment