Skip to content

Commit

Permalink
Refactor to use str_get_html() directly instead of through EDAC_Dom c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
pattonwebz committed Mar 29, 2024
1 parent 2370bac commit ab49cbd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 82 deletions.
31 changes: 0 additions & 31 deletions tests/phpunit/TestHelpers/GetDomHelperTrait.php

This file was deleted.

28 changes: 5 additions & 23 deletions tests/phpunit/includes/rules/AriaHiddenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,14 @@ public function test_edac_rule_aria_hidden_allows_hidden_with_parent_that_has_sc
);
}

/**
* Wrapper to generate dom objects that match the shape of the object in the plugin.
*
* @param string $html_string HTML string.
* @return EDAC_Dom
*/
private function get_DOM( string $html_string = '' ) {
return new EDAC_Dom(
$html_string,
true,
true,
DEFAULT_TARGET_CHARSET,
true,
DEFAULT_BR_TEXT,
DEFAULT_SPAN_TEXT
);
}

/**
* Wrapper to produce $dom nodes and run the rule check.
*
* @param string $html_string HTML string.
* @return array
*/
private function get_errors_from_rule_check( string $html_string = '' ): array {
$dom = $this->get_DOM( $html_string );
$dom = str_get_html( $html_string );
$content['html'] = $dom;
$post = $this->factory()->post->create_and_get();

Expand All @@ -143,7 +125,7 @@ public function test_screen_reader_text_classes( string $sibling_class, bool $pa
<div class="$sibling_class">Some text maybe for screenreaders</div>
</div>;
EOT;
$dom = $this->get_DOM( $markup );
$dom = str_get_html( $markup );
$siblings = $dom->find( '.parent > *' );
$this->assertEquals( $pass, edac_rule_aria_hidden_siblings_are_screen_reader_text_elements( $siblings ) );
}
Expand All @@ -156,7 +138,7 @@ public function test_parent_with_label_that_is_not_link_or_button_errors() {
$test_elements = array( 'div', 'span', 'section' );
foreach ( $test_elements as $element ) {
$markup = "<$element aria-label='label'><div aria-hidden='true'></div></$element>";
$dom = $this->get_DOM( $markup );
$dom = str_get_html( $markup );
$errors = $this->get_errors_from_rule_check( $dom );
$this->assertNotEmpty( $errors );
}
Expand All @@ -166,12 +148,12 @@ public function test_parent_with_label_that_is_not_link_or_button_errors() {
* Test that parents with likely visible text pass.
*/
public function test_parent_with_likely_visible_text_passes() {
$link_dom = $this->get_DOM( $this->get_test_markup( 'link_with_visible_text' ) );
$link_dom = str_get_html( $this->get_test_markup( 'link_with_visible_text' ) );
$link_element = $link_dom->find( '[aria-hidden="true"]' );
$link_parent = $link_element[0]->parent();
$this->assertNotEmpty( edac_rule_aria_hidden_strip_markup_and_return_text( $link_parent ) );

$button_dom = $this->get_DOM( $this->get_test_markup( 'button_with_visible_text' ) );
$button_dom = str_get_html( $this->get_test_markup( 'button_with_visible_text' ) );
$button_element = $button_dom->find( '[aria-hidden="true"]' );
$button_parent = $button_element[0]->parent();
$this->assertNotEmpty( edac_rule_aria_hidden_strip_markup_and_return_text( $button_parent ) );
Expand Down
11 changes: 5 additions & 6 deletions tests/phpunit/includes/rules/ImgAltRedundantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
* @group rules
*/
class ImgAltRedundantTest extends WP_UnitTestCase {
use GetDomHelperTrait;

/**
* Test the rule catches redundant alt attributes on images.
*/
public function test_does_not_flag_duplicate_alt_when_alt_is_not_present() {
$html = '<img src="image.jpg">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$errors = edac_rule_img_alt_redundant( array( 'html' => $dom ), null );
$this->assertEmpty( $errors );
}
Expand All @@ -30,7 +29,7 @@ public function test_does_not_flag_duplicate_alt_when_alt_is_not_present() {
*/
public function test_idoes_not_flag_duplicate_alt_when_alt_is_empty_string() {
$html = '<img src="image.jpg" alt="">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$errors = edac_rule_img_alt_redundant( array( 'html' => $dom ), null );
$this->assertEmpty( $errors );
}
Expand All @@ -42,7 +41,7 @@ public function test_idoes_not_flag_duplicate_alt_when_alt_is_empty_string() {
*/
public function test_does_not_flag_duplicate_alt_when_alt_and_title_are_empty_strings() {
$html = '<img src="image.jpg" alt="" title="">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$errors = edac_rule_img_alt_redundant( array( 'html' => $dom ), null );
$this->assertEmpty( $errors );
}
Expand All @@ -52,7 +51,7 @@ public function test_does_not_flag_duplicate_alt_when_alt_and_title_are_empty_st
*/
public function test_flags_images_where_alt_and_title_are_the_same() {
$html = '<img src="image.jpg" alt="test" title="test">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$errors = edac_rule_img_alt_redundant( array( 'html' => $dom ), null );
$this->assertNotEmpty( $errors );
}
Expand All @@ -62,7 +61,7 @@ public function test_flags_images_where_alt_and_title_are_the_same() {
*/
public function test_img_alt_redundant_rule_does_not_flag_duplicate_alt_when_alt_and_title_are_different() {
$html = '<img src="image.jpg" alt="test" title="different">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$errors = edac_rule_img_alt_redundant( array( 'html' => $dom ), null );
$this->assertEmpty( $errors );
}
Expand Down
26 changes: 4 additions & 22 deletions tests/phpunit/includes/rules/ImgAnimatedGifTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testAnimatedWebpDetectionWithStaticWebp() {
*/
public function testRuleWithAnimatedGifInContent() {
$html = '<img src="' . EDAC_TEST_ASSETS_DIR . 'animated.gif">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$content = array( 'html' => $dom );
$post = new stdClass();
$errors = edac_rule_img_animated_gif( $content, $post );
Expand All @@ -62,7 +62,7 @@ public function testRuleWithAnimatedGifInContent() {
*/
public function testRuleWithStaticGifInContent() {
$html = '<img src="' . EDAC_TEST_ASSETS_DIR . 'static.gif">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$content = array( 'html' => $dom );
$post = new stdClass();
$errors = edac_rule_img_animated_gif( $content, $post );
Expand All @@ -74,7 +74,7 @@ public function testRuleWithStaticGifInContent() {
*/
public function testRuleWithAnimatedWebpInContent() {
$html = '<img src="' . EDAC_TEST_ASSETS_DIR . 'animated.webp">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$content = array( 'html' => $dom );
$post = new stdClass();
$errors = edac_rule_img_animated_gif( $content, $post );
Expand All @@ -87,28 +87,10 @@ public function testRuleWithAnimatedWebpInContent() {
public function testRuleWithStaticWebpInContent() {

$html = '<img src="' . EDAC_TEST_ASSETS_DIR . 'static.webp">';
$dom = $this->get_DOM( $html );
$dom = str_get_html( $html );
$content = array( 'html' => $dom );
$post = new stdClass();
$errors = edac_rule_img_animated_gif( $content, $post );
$this->assertEmpty( $errors );
}

/**
* Wrapper to generate dom objects that match the shape of the object in the plugin.
*
* @param string $html_string HTML string.
* @return EDAC_Dom
*/
private function get_DOM( string $html_string = '' ) {
return new EDAC_Dom(
$html_string,
true,
true,
DEFAULT_TARGET_CHARSET,
true,
DEFAULT_BR_TEXT,
DEFAULT_SPAN_TEXT
);
}
}

0 comments on commit ab49cbd

Please sign in to comment.