Skip to content

Commit

Permalink
added - edac_max_alt_length filter and edac_rule_img_alt_long tests #95
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveJonesDev committed Mar 27, 2024
1 parent 4b37736 commit 6d3f898
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
10 changes: 6 additions & 4 deletions includes/rules/img_alt_long.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
*/
function edac_rule_img_alt_long( $content, $post ) { // phpcs:ignore -- $post is reserved for future use or for compliance with a specific interface.

$dom = $content['html'];
$errors = array();
$images = $dom->find( 'img' );
$dom = $content['html'];
$errors = array();
$images = $dom->find( 'img' );
$max_alt_length = absint( apply_filters( 'edac_max_alt_length', 300 ) );
$max_alt_length = max( 1, $max_alt_length );

foreach ( $images as $image ) {
if ( isset( $image ) && $image->hasAttribute( 'alt' ) && $image->getAttribute( 'alt' ) !== '' ) {
$alt = $image->getAttribute( 'alt' );
if ( strlen( $alt ) > 300 ) {
if ( strlen( $alt ) > $max_alt_length ) {
$image_code = $image;
$errors[] = $image_code;
}
Expand Down
72 changes: 72 additions & 0 deletions tests/phpunit/EDACRuleImgAltLongTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Class EDACRuleImgAltLongTest
*
* @package Accessibility_Checker
*/

/**
* Admin Notices test case.
*/
class EDACRuleImgAltLongTest extends WP_UnitTestCase {

/**
* Test that edac_rule_img_alt_long function returns an empty array when all images have alt text within the allowed length.
*
* @return void
*/
public function test_empty_array_when_alt_text_is_within_allowed_length() {
$html = '<img src="test.jpg" alt="This is a test image">';
$dom = str_get_html( $html );
$content = array(
'html' => $dom,
);
$post = null;
$errors = edac_rule_img_alt_long( $content, $post );
$this->assertEmpty( $errors );
}

/**
* Test that edac_rule_img_alt_long function returns an array with errors when images have alt text longer than the allowed length.
*
* @return void
*/
public function test_returns_errors_when_alt_text_is_longer_than_allowed_length() {
$html = '<img src="test.jpg" alt="' . str_repeat( 'a', 301 ) . '">';
$dom = str_get_html( $html );
$content = array(
'html' => $dom,
);
$post = null;
$errors = edac_rule_img_alt_long( $content, $post );
$this->assertNotEmpty( $errors );
}

/**
* Test that the edac_max_alt_length filter modifies the maximum allowed alt text length.
*
* @return void
*/
public function test_edac_max_alt_length_filter_modifies_max_alt_length() {
// Add a filter to modify the maximum alt text length.
add_filter(
'edac_max_alt_length',
function () {
return 10;
}
);

$html = '<img src="test.jpg" alt="This is a long alt text">';
$dom = str_get_html( $html );
$content = array(
'html' => $dom,
);
$post = null;
$errors = edac_rule_img_alt_long( $content, $post );

// Remove the filter after the test.
remove_filter( 'edac_max_alt_length', '__return_true' );

$this->assertNotEmpty( $errors );
}
}

0 comments on commit 6d3f898

Please sign in to comment.