From 6608c9f6f2df1a58e2479f14f4c203501179a651 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Sun, 13 Jul 2025 13:52:42 -0500 Subject: [PATCH] HTML API: Use `assertEqualHTML()` in `wp_kses()` tests. (#9257) Prep work for #9248. # Conflicts: # tests/phpunit/tests/comment.php --- tests/phpunit/tests/comment.php | 7 +++- tests/phpunit/tests/kses.php | 61 +++++++++++++++++---------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index e1d3fd0613e4b..a8f15e1deaec3 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -168,7 +168,12 @@ public function test_update_comment_from_unprivileged_user_by_privileged_user() wp_set_current_user( 0 ); $comment = get_comment( $comment_id ); - $this->assertEqualHTML( 'click', $comment->comment_content, '', 'Comment: ' . $comment->comment_content ); + $this->assertEqualHTML( + 'click', + $comment->comment_content, + '', + 'Comment: ' . $comment->comment_content + ); } /** diff --git a/tests/phpunit/tests/kses.php b/tests/phpunit/tests/kses.php index 61baf0d0a1863..e3bd074511d2c 100644 --- a/tests/phpunit/tests/kses.php +++ b/tests/phpunit/tests/kses.php @@ -17,7 +17,7 @@ class Tests_Kses extends WP_UnitTestCase { public function test_wp_filter_post_kses_address( $content, $expected ) { global $allowedposttags; - $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) ); + $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) ); } /** @@ -65,7 +65,7 @@ public function data_wp_filter_post_kses_address() { public function test_wp_filter_post_kses_a( $content, $expected ) { global $allowedposttags; - $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) ); + $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) ); } /** @@ -120,7 +120,7 @@ public function data_wp_filter_post_kses_a() { * @param string $expected Expected output following KSES parsing. */ public function test_wp_kses_video( $source, $context, $expected ) { - $this->assertSame( $expected, wp_kses( $source, $context ) ); + $this->assertEqualHTML( $expected, wp_kses( $source, $context ) ); } /** @@ -171,7 +171,7 @@ public function data_wp_kses_video() { public function test_wp_filter_post_kses_abbr( $content, $expected ) { global $allowedposttags; - $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) ); + $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) ); } /** @@ -232,7 +232,7 @@ public function test_feed_links() { CLICK ME EOF; - $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) ); + $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) ); } public function test_wp_kses_bad_protocol() { @@ -546,8 +546,8 @@ public function test_hyphenated_tag() { $expect_stripped_content = 'Alot of hyphens.'; $expect_valid_content = 'Alot of hyphens.'; - $this->assertSame( $expect_stripped_content, wp_kses_post( $content ) ); - $this->assertSame( $expect_valid_content, wp_kses( $content, $custom_tags ) ); + $this->assertEqualHTML( $expect_stripped_content, wp_kses_post( $content ) ); + $this->assertEqualHTML( $expect_valid_content, wp_kses( $content, $custom_tags ) ); } /** @@ -613,7 +613,7 @@ public static function data_normalize_entities(): array { * @dataProvider data_normalize_entities */ public function test_wp_kses_normalize_entities( string $input, string $expected ) { - $this->assertSame( $expected, wp_kses_normalize_entities( $input ) ); + $this->assertEqualHTML( $expected, wp_kses_normalize_entities( $input ) ); } /** @@ -625,7 +625,7 @@ public function test_wp_kses_normalize_entities( string $input, string $expected public function test_ctrl_removal( $content, $expected ) { global $allowedposttags; - return $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) ); + return $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) ); } public function data_ctrl_removal() { @@ -662,7 +662,7 @@ public function data_ctrl_removal() { public function test_slash_zero_removal( $content, $expected ) { global $allowedposttags; - return $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) ); + return $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) ); } public function data_slash_zero_removal() { @@ -917,7 +917,7 @@ public function test_bdo_tag_allowed() { $content = '

This is a BDO tag. Weird, right?

'; - $this->assertSame( $content, wp_kses( $content, $allowedposttags ) ); + $this->assertEqualHTML( $content, wp_kses( $content, $allowedposttags ) ); } /** @@ -928,7 +928,7 @@ public function test_ruby_tag_allowed() { $content = ': Star, Étoile.'; - $this->assertSame( $content, wp_kses( $content, $allowedposttags ) ); + $this->assertEqualHTML( $content, wp_kses( $content, $allowedposttags ) ); } /** @@ -939,7 +939,7 @@ public function test_ol_reversed_attribute_allowed() { $content = '
  1. Item 1
  2. Item 2
  3. Item 3
'; - $this->assertSame( $content, wp_kses( $content, $allowedposttags ) ); + $this->assertEqualHTML( $content, wp_kses( $content, $allowedposttags ) ); } /** @@ -949,7 +949,7 @@ public function test_wp_kses_attr_no_attributes_allowed_with_empty_array() { $element = 'foo'; $attribute = 'title="foo" class="bar"'; - $this->assertSame( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => array() ), array() ) ); + $this->assertEqualHTML( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => array() ), array() ) ); } /** @@ -959,7 +959,7 @@ public function test_wp_kses_attr_no_attributes_allowed_with_true() { $element = 'foo'; $attribute = 'title="foo" class="bar"'; - $this->assertSame( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => true ), array() ) ); + $this->assertEqualHTML( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => true ), array() ) ); } /** @@ -969,7 +969,7 @@ public function test_wp_kses_attr_single_attribute_is_allowed() { $element = 'foo'; $attribute = 'title="foo" class="bar"'; - $this->assertSame( "<{$element} title=\"foo\">", wp_kses_attr( $element, $attribute, array( 'foo' => array( 'title' => true ) ), array() ) ); + $this->assertEqualHTML( "<{$element} title=\"foo\">", wp_kses_attr( $element, $attribute, array( 'foo' => array( 'title' => true ) ), array() ) ); } /** @@ -979,7 +979,7 @@ public function test_wp_kses_attr_no_attributes_allowed_with_false() { $element = 'foo'; $attribute = 'title="foo" class="bar"'; - $this->assertSame( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => false ), array() ) ); + $this->assertEqualHTML( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => false ), array() ) ); } /** @@ -1423,7 +1423,7 @@ public function test_wp_kses_attr_data_attribute_is_allowed() { $test = '
Pens and pencils
'; $expected = '
Pens and pencils
'; - $this->assertSame( $expected, wp_kses_post( $test ) ); + $this->assertEqualHTML( $expected, wp_kses_post( $test ) ); } /** @@ -1435,7 +1435,7 @@ public function test_wp_kses_attr_data_attribute_hypens_allowed() { $test = '
Pens and pencils
'; $expected = '
Pens and pencils
'; - $this->assertSame( $expected, wp_kses_post( $test ) ); + $this->assertEqualHTML( $expected, wp_kses_post( $test ) ); } /** @@ -1456,7 +1456,7 @@ public function test_wildcard_requires_hyphen_after_prefix() { $actual = wp_kses( $content, $allowed_html ); - $this->assertSame( $expected, $actual ); + $this->assertEqualHTML( $expected, $actual ); } /** @@ -1476,7 +1476,7 @@ public function test_wildcard_allows_two_hyphens() { $actual = wp_kses( $content, $allowed_html ); - $this->assertSame( $expected, $actual ); + $this->assertEqualHTML( $expected, $actual ); } /** @@ -1761,7 +1761,7 @@ public function test_wp_kses_img_tag_standard_attributes() { $html = implode( ' ', $html ); - $this->assertSame( $html, wp_kses_post( $html ) ); + $this->assertEqualHTML( $html, wp_kses_post( $html ) ); } /** @@ -1779,7 +1779,7 @@ public function test_wp_kses_main_tag_standard_attributes() { $html = implode( ' ', $test ); - $this->assertSame( $html, wp_kses_post( $html ) ); + $this->assertEqualHTML( $html, wp_kses_post( $html ) ); } /** @@ -1793,7 +1793,7 @@ public function test_wp_kses_main_tag_standard_attributes() { * @param string $expected The expected result from KSES. */ public function test_wp_kses_object_tag_allowed( $html, $expected ) { - $this->assertSame( $expected, wp_kses_post( $html ) ); + $this->assertEqualHTML( $expected, wp_kses_post( $html ) ); } /** @@ -1904,7 +1904,7 @@ public function data_wp_kses_object_tag_allowed() { */ public function test_wp_kses_object_data_url_with_port_number_allowed( $html, $expected ) { add_filter( 'upload_dir', array( $this, 'wp_kses_upload_dir_filter' ), 10, 2 ); - $this->assertSame( $expected, wp_kses_post( $html ) ); + $this->assertEqualHTML( $expected, wp_kses_post( $html ) ); } /** @@ -1970,7 +1970,7 @@ public function test_wp_kses_object_added_in_html_filter() { remove_filter( 'wp_kses_allowed_html', array( $this, 'filter_wp_kses_object_added_in_html_filter' ) ); - $this->assertSame( $html, $filtered_html ); + $this->assertEqualHTML( $html, $filtered_html ); } public function filter_wp_kses_object_added_in_html_filter( $tags, $context ) { @@ -2001,9 +2001,10 @@ public function filter_wp_kses_object_added_in_html_filter( $tags, $context ) { * @param string $expected_output How `wp_kses()` ought to transform the comment. */ public function test_wp_kses_preserves_html_comments( $html_comment, $expected_output ) { - $this->assertSame( + $this->assertEqualHTML( $expected_output, wp_kses( $html_comment, array() ), + '', 'Failed to properly preserve HTML comment.' ); } @@ -2033,7 +2034,7 @@ public static function data_html_containing_various_kinds_of_html_comments() { * @param array $allowed_html The allowed HTML to pass to KSES. */ public function test_wp_kses_allowed_values_list( $content, $expected, $allowed_html ) { - $this->assertSame( $expected, wp_kses( $content, $allowed_html ) ); + $this->assertEqualHTML( $expected, wp_kses( $content, $allowed_html ) ); } /** @@ -2091,7 +2092,7 @@ static function ( $datum ) { * @param array $allowed_html The allowed HTML to pass to KSES. */ public function test_wp_kses_required_attribute( $content, $expected, $allowed_html ) { - $this->assertSame( $expected, wp_kses( $content, $allowed_html ) ); + $this->assertEqualHTML( $expected, wp_kses( $content, $allowed_html ) ); } /** @@ -2312,7 +2313,7 @@ public function data_kses_globals_are_defined() { public function test_target_attribute_preserved_in_context( $context, $input, $expected ) { $allowed = wp_kses_allowed_html( $context ); $this->assertTrue( isset( $allowed['a']['target'] ), "Target attribute not allowed in {$context}" ); - $this->assertEquals( $expected, wp_kses( $input, $context ) ); + $this->assertEqualHTML( $expected, wp_kses( $input, $context ) ); } /**