diff --git a/admin/tool/brickfield/classes/local/htmlchecker/common/checks/img_alt_is_too_long.php b/admin/tool/brickfield/classes/local/htmlchecker/common/checks/img_alt_is_too_long.php index e19812c33c5..7963e14947b 100644 --- a/admin/tool/brickfield/classes/local/htmlchecker/common/checks/img_alt_is_too_long.php +++ b/admin/tool/brickfield/classes/local/htmlchecker/common/checks/img_alt_is_too_long.php @@ -16,6 +16,7 @@ namespace tool_brickfield\local\htmlchecker\common\checks; +use core_text; use tool_brickfield\local\htmlchecker\common\brickfield_accessibility_test; /** @@ -41,7 +42,7 @@ class img_alt_is_too_long extends brickfield_accessibility_test { foreach ($this->get_all_elements('img') as $img) { $alttextlengthlimit = 125; - if ($img->hasAttribute('alt') && strlen($img->getAttribute('alt')) > $alttextlengthlimit) { + if ($img->hasAttribute('alt') && core_text::strlen($img->getAttribute('alt')) > $alttextlengthlimit) { $this->add_report($img); } } diff --git a/admin/tool/brickfield/tests/local/htmlchecker/common/checks/img_alt_is_too_long_test.php b/admin/tool/brickfield/tests/local/htmlchecker/common/checks/img_alt_is_too_long_test.php index dd423ddd157..c2472cb37f9 100644 --- a/admin/tool/brickfield/tests/local/htmlchecker/common/checks/img_alt_is_too_long_test.php +++ b/admin/tool/brickfield/tests/local/htmlchecker/common/checks/img_alt_is_too_long_test.php @@ -30,49 +30,73 @@ require_once('all_checks.php'); /** * Class img_alt_is_too_long_testcase + * + * @covers \tool_brickfield\local\htmlchecker\common\checks\img_alt_is_too_long */ final class img_alt_is_too_long_test extends all_checks { /** @var string Check type */ protected $checktype = 'img_alt_is_too_long'; - /** @var string Html fail */ - private $htmlfail = << - + /** + * Get test HTML with an image tag. + * + * @param string $alttext + * @return string + */ + protected function get_test_html(string $alttext): string { + return << + Image alt attributes must not be too long - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent accumsan, ante varius viverra aliquam,
-     dolor risus scelerisque massa, ut lacinia ipsum felis id est. Nullam convallis odio ante, in commodo elit fermentum sed.
-     Vivamus ullamcorper tincidunt sagittis. Sed et semper sapien. Quisque malesuada lacus nec libero cursus, aliquam malesuada
-     neque ultricies. Cras sit amet enim vel orci tristique porttitor a vitae urna. Suspendisse mi leo, hendrerit et eleifend a,
-     mollis at ex. Maecenas eget magna nec sem dignissim pharetra vel nec ex. Donec in porta lectus. Aenean porttitor euismod
-     lectus, sodales eleifend ex egestas in. Donec sed metus sodales, lobortis velit quis, dictum arcu. + $alttext EOD; + } + + /** + * Image alt text data provider. + * + * @return array + */ + public static function img_alt_text_provider(): array { + return [ + 'Alt text <= 125 characters' => [ + true, + str_repeat("Hello world!", 10), + ], + 'Alt text > 125 characters' => [ + false, + str_repeat("Hello world!", 25), + ], + 'Multi-byte alt text <= 125 characters' => [ + true, + str_repeat('こんにちは、世界!', 13), + ], + 'Multi-byte alt text > 125 characters' => [ + false, + str_repeat('こんにちは、世界!', 30), + ], + ]; + } - /** @var string Html pass */ - private $htmlpass = << - - - Image alt attributes must not be too long - - - this is an image of rex - - -EOD; /** * Test for image alt attributes being too long + * + * @dataProvider img_alt_text_provider + * @param bool $expectedpass Whether the test is expected to pass or fail. + * @param string $alttext The alt text to test. */ - public function test_check(): void { - $results = $this->get_checker_results($this->htmlfail); - $this->assertTrue($results[0]->element->tagName == 'img'); - - $results = $this->get_checker_results($this->htmlpass); - $this->assertEmpty($results); + public function test_check(bool $expectedpass, string $alttext): void { + $html = $this->get_test_html($alttext); + $results = $this->get_checker_results($html); + if ($expectedpass) { + $this->assertEmpty($results); + } else { + $this->assertTrue($results[0]->element->tagName === 'img'); + } } }