diff --git a/admin/tool/brickfield/classes/local/htmlchecker/common/brickfield_accessibility_color_test.php b/admin/tool/brickfield/classes/local/htmlchecker/common/brickfield_accessibility_color_test.php index 82f6e013968..fb12c17f962 100644 --- a/admin/tool/brickfield/classes/local/htmlchecker/common/brickfield_accessibility_color_test.php +++ b/admin/tool/brickfield/classes/local/htmlchecker/common/brickfield_accessibility_color_test.php @@ -25,7 +25,7 @@ namespace tool_brickfield\local\htmlchecker\common; */ class brickfield_accessibility_color_test extends brickfield_accessibility_test { - /** @var string[] Mapping of colours to hex codes. */ + /** @var string[] Define colour codes. */ public $colornames = [ 'aliceblue' => 'f0f8ff', 'antiquewhite' => 'faebd7', @@ -183,6 +183,12 @@ class brickfield_accessibility_color_test extends brickfield_accessibility_test } $forergb = $this->get_rgb($foreground); $backrgb = $this->get_rgb($background); + + // If get_rgb returns null for either, return 0. + if ($forergb === null || $backrgb === null) { + return 0; + } + return $this->luminosity($forergb['r'], $backrgb['r'], $forergb['g'], $backrgb['g'], $forergb['b'], $backrgb['b']); @@ -227,15 +233,15 @@ class brickfield_accessibility_color_test extends brickfield_accessibility_test /** - * Returns the decimal equivalents for a HEX color + * Returns the decimal equivalents for a HEX color. Returns null if it cannot be determined. * @param string $color The hex color value - * @return array An array where 'r' is the Red value, 'g' is Green, and 'b' is Blue + * @return array|null An array where 'r' is the Red value, 'g' is Green, and 'b' is Blue */ - public function get_rgb(string $color) { + public function get_rgb(string $color): ?array { $color = $this->convert_color($color); $c = str_split($color, 2); if (count($c) != 3) { - return false; + return null; } $results = ['r' => hexdec($c[0]), 'g' => hexdec($c[1]), 'b' => hexdec($c[2])]; return $results; @@ -307,6 +313,12 @@ class brickfield_accessibility_color_test extends brickfield_accessibility_test public function get_wai_ert_contrast(string $foreground, string $background): array { $forergb = $this->get_rgb($foreground); $backrgb = $this->get_rgb($background); + + // If get_rgb returns null for either, return 0. + if ($forergb === null || $backrgb === null) { + return []; + } + $diffs = $this->get_wai_diffs($forergb, $backrgb); return $diffs['red'] + $diffs['green'] + $diffs['blue']; @@ -321,12 +333,18 @@ class brickfield_accessibility_color_test extends brickfield_accessibility_test public function get_wai_ert_brightness(string $foreground, string $background): float { $forergb = $this->get_rgb($foreground); $backrgb = $this->get_rgb($background); + + // If get_rgb returns null for either, return 0. + if ($forergb === null || $backrgb === null) { + return 0; + } + $color = $this->get_wai_diffs($forergb, $backrgb); return (($color['red'] * 299) + ($color['green'] * 587) + ($color['blue'] * 114)) / 1000; } /** - * Get wai diffs. + * Get the wai differences. * @param array $forergb * @param array $backrgb * @return array diff --git a/admin/tool/brickfield/tests/local/htmlchecker/common/checks/css_text_has_contrast_test.php b/admin/tool/brickfield/tests/local/htmlchecker/common/checks/css_text_has_contrast_test.php index f5c1fa59f2f..ddc6763a054 100644 --- a/admin/tool/brickfield/tests/local/htmlchecker/common/checks/css_text_has_contrast_test.php +++ b/admin/tool/brickfield/tests/local/htmlchecker/common/checks/css_text_has_contrast_test.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * tool_brickfield check test. + * Class test_css_text_has_contrast test * * @package tool_brickfield * @copyright 2020 onward: Brickfield Education Labs, https://www.brickfield.ie @@ -29,14 +29,79 @@ defined('MOODLE_INTERNAL') || die(); require_once('all_checks.php'); /** - * Class test_css_text_has_contrast_testcase + * Class test_css_text_has_contrast_test */ class css_text_has_contrast_test extends all_checks { - /** @var string Check type */ + /** @var string The check type. */ protected $checktype = 'css_text_has_contrast'; - /** @var string Html fail */ - private $htmlfail = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML that should get flagged. */ + private $htmlfail2 = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML that should get flagged. */ + private $htmlfail3 = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML that should get flagged. */ + private $htmlfail4 = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML that should get flagged. */ + private $htmlfail5 = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML that should get flagged. */ + private $htmlfail6 = << @@ -48,7 +113,7 @@ class css_text_has_contrast_test extends all_checks { EOD; - /** @var string Html pass */ + /** @var string HTML that should not get flagged. */ private $htmlpass = << @@ -61,14 +126,113 @@ EOD; EOD; + /** @var string HTML that should get flagged. */ + private $namecolours = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML with invalid colour names. */ + private $invalidcolours = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML with invalid colour numeric values. */ + private $invalidvalue = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + + /** @var string HTML with empty colour values. */ + private $emptyvalue = << + + + OAC Testfile - Check #6 - Positive + + +

This is not contrasty enough.

+ + +EOD; + /** * Test for the area assign intro */ public function test_check() { - $results = $this->get_checker_results($this->htmlfail); + $results = $this->get_checker_results($this->htmlfail1); + $this->assertTrue($results[0]->element->tagName == 'p'); + + $results = $this->get_checker_results($this->htmlfail2); + $this->assertTrue($results[0]->element->tagName == 'p'); + + $results = $this->get_checker_results($this->htmlfail3); + $this->assertTrue($results[0]->element->tagName == 'p'); + + $results = $this->get_checker_results($this->htmlfail4); + $this->assertTrue($results[0]->element->tagName == 'p'); + + $results = $this->get_checker_results($this->htmlfail5); + $this->assertTrue($results[0]->element->tagName == 'p'); + + $results = $this->get_checker_results($this->htmlfail6); $this->assertTrue($results[0]->element->tagName == 'p'); $results = $this->get_checker_results($this->htmlpass); $this->assertEmpty($results); } + + /** + * Test with valid colour names. + */ + public function test_check_for_namedcolours() { + $results = $this->get_checker_results($this->namecolours); + $this->assertTrue($results[0]->element->tagName == 'p'); + } + + /** + * Test with invalid colour names. + */ + public function test_check_for_invalidcolours() { + $results = $this->get_checker_results($this->invalidcolours); + $this->assertTrue($results[0]->element->tagName == 'p'); + } + + /** + * Test with invalid colour numeric values. + */ + public function test_check_for_invalidvalues() { + $results = $this->get_checker_results($this->invalidvalue); + $this->assertTrue($results[0]->element->tagName == 'p'); + } + + /** + * Test with empty colour values. + */ + public function test_check_for_emptyvalues() { + $results = $this->get_checker_results($this->emptyvalue); + $this->assertEmpty($results); + } } diff --git a/admin/tool/brickfield/upgrade.txt b/admin/tool/brickfield/upgrade.txt new file mode 100644 index 00000000000..c3459ce38ba --- /dev/null +++ b/admin/tool/brickfield/upgrade.txt @@ -0,0 +1,6 @@ +This files describes API changes in /admin/tool/brickfield/*. + +=== 4.0 === + +* classes/local/htmlchecker/common/brickfield_accessibility_color_test::get_rgb() has been modified to return either an + array or null. Previously it returned either an array or false.