Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / ColorTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\Color;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * Tests Color utility class conversions.
10  *
11  * @group Utility
12  */
13 class ColorTest extends TestCase {
14
15   /**
16    * Tests Color::hexToRgb().
17    *
18    * @param string $value
19    *   The hex color value.
20    * @param string $expected
21    *   The expected rgb color value.
22    * @param bool $invalid
23    *   Whether this value is invalid and exception should be expected.
24    *
25    * @dataProvider providerTestHexToRgb
26    */
27   public function testHexToRgb($value, $expected, $invalid = FALSE) {
28     if ($invalid) {
29       if (method_exists($this, 'expectException')) {
30         $this->expectException('InvalidArgumentException');
31       }
32       else {
33         $this->setExpectedException('InvalidArgumentException');
34       }
35     }
36     $this->assertSame($expected, Color::hexToRgb($value));
37   }
38
39   /**
40    * Data provider for testHexToRgb().
41    *
42    * @see testHexToRgb()
43    *
44    * @return array
45    *   An array of arrays containing:
46    *     - The hex color value.
47    *     - The rgb color array value.
48    *     - (optional) Boolean indicating invalid status. Defaults to FALSE.
49    */
50   public function providerTestHexToRgb() {
51     $invalid = [];
52     // Any invalid arguments should throw an exception.
53     foreach (['', '-1', '1', '12', '12345', '1234567', '123456789', '123456789a', 'foo'] as $value) {
54       $invalid[] = [$value, '', TRUE];
55     }
56     // Duplicate all invalid value tests with additional '#' prefix.
57     // The '#' prefix inherently turns the data type into a string.
58     foreach ($invalid as $value) {
59       $invalid[] = ['#' . $value[0], '', TRUE];
60     }
61     // Add invalid data types (hex value must be a string).
62     foreach ([
63       1, 12, 1234, 12345, 123456, 1234567, 12345678, 123456789, 123456789,
64       -1, PHP_INT_MAX, PHP_INT_MAX + 1, -PHP_INT_MAX, 0x0, 0x010,
65     ] as $value) {
66       $invalid[] = [$value, '', TRUE];
67     }
68     // And some valid values.
69     $valid = [
70       // Shorthands without alpha.
71       ['hex' => '#000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
72       ['hex' => '#fff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
73       ['hex' => '#abc', 'rgb' => ['red' => 170, 'green' => 187, 'blue' => 204]],
74       ['hex' => 'cba', 'rgb' => ['red' => 204, 'green' => 187, 'blue' => 170]],
75       // Full without alpha.
76       ['hex' => '#000000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
77       ['hex' => '#ffffff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
78       ['hex' => '#010203', 'rgb' => ['red' => 1, 'green' => 2, 'blue' => 3]],
79     ];
80     return array_merge($invalid, $valid);
81   }
82
83   /**
84    * Tests Color::rgbToHex().
85    *
86    * @param string $value
87    *   The rgb color value.
88    * @param string $expected
89    *   The expected hex color value.
90    *
91    * @dataProvider providerTestRbgToHex
92    */
93   public function testRgbToHex($value, $expected) {
94     $this->assertSame($expected, Color::rgbToHex($value));
95   }
96
97   /**
98    * Data provider for testRgbToHex().
99    *
100    * @see testRgbToHex()
101    *
102    * @return array
103    *   An array of arrays containing:
104    *     - The rgb color array value.
105    *     - The hex color value.
106    */
107   public function providerTestRbgToHex() {
108     // Input using named RGB array (e.g., as returned by Color::hexToRgb()).
109     $tests = [
110       [['red' => 0, 'green' => 0, 'blue' => 0], '#000000'],
111       [['red' => 255, 'green' => 255, 'blue' => 255], '#ffffff'],
112       [['red' => 119, 'green' => 119, 'blue' => 119], '#777777'],
113       [['red' => 1, 'green' => 2, 'blue' => 3], '#010203'],
114     ];
115     // Input using indexed RGB array (e.g.: array(10, 10, 10)).
116     foreach ($tests as $test) {
117       $tests[] = [array_values($test[0]), $test[1]];
118     }
119     // Input using CSS RGB string notation (e.g.: 10, 10, 10).
120     foreach ($tests as $test) {
121       $tests[] = [implode(', ', $test[0]), $test[1]];
122     }
123     return $tests;
124   }
125
126   /**
127    * Data provider for testNormalizeHexLength().
128    *
129    * @see testNormalizeHexLength()
130    *
131    * @return array
132    *   An array of arrays containing:
133    *     - The hex color value.
134    *     - The 6 character length hex color value.
135    */
136   public function providerTestNormalizeHexLength() {
137     $data = [
138       ['#000', '#000000'],
139       ['#FFF', '#FFFFFF'],
140       ['#abc', '#aabbcc'],
141       ['cba', '#ccbbaa'],
142       ['#000000', '#000000'],
143       ['ffffff', '#ffffff'],
144       ['#010203', '#010203'],
145     ];
146
147     return $data;
148   }
149
150   /**
151    * Tests Color::normalizeHexLength().
152    *
153    * @param string $value
154    *   The input hex color value.
155    * @param string $expected
156    *   The expected normalized hex color value.
157    *
158    * @dataProvider providerTestNormalizeHexLength
159    */
160   public function testNormalizeHexLength($value, $expected) {
161     $this->assertSame($expected, Color::normalizeHexLength($value));
162   }
163
164 }