Skip to content

HTML API: Use assertEqualHTML() in wp_kses() tests. #9257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion tests/phpunit/tests/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->assertSame( '<a href="http://example.localhost/something.html" rel="nofollow ugc">click</a>', $comment->comment_content, 'Comment: ' . $comment->comment_content );
$this->assertEqualHTML(
'<a href="http://example.localhost/something.html" rel="nofollow ugc">click</a>',
$comment->comment_content,
'<body>',
'Comment: ' . $comment->comment_content
);
}

/**
Expand Down
61 changes: 31 additions & 30 deletions tests/phpunit/tests/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -232,7 +232,7 @@ public function test_feed_links() {
<a href="">CLICK ME</a>
EOF;

$this->assertSame( $expected, wp_kses( $content, $allowedposttags ) );
$this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) );
}

public function test_wp_kses_bad_protocol() {
Expand Down Expand Up @@ -546,8 +546,8 @@ public function test_hyphenated_tag() {
$expect_stripped_content = 'Alot of hyphens.';
$expect_valid_content = '<hyphenated-tag attribute="value">Alot of hyphens.</hyphenated-tag>';

$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 ) );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -917,7 +917,7 @@ public function test_bdo_tag_allowed() {

$content = '<p>This is <bdo dir="rtl">a BDO tag</bdo>. Weird, <bdo dir="ltr">right?</bdo></p>';

$this->assertSame( $content, wp_kses( $content, $allowedposttags ) );
$this->assertEqualHTML( $content, wp_kses( $content, $allowedposttags ) );
}

/**
Expand All @@ -928,7 +928,7 @@ public function test_ruby_tag_allowed() {

$content = '<ruby>✶<rp>: </rp><rt>Star</rt><rp>, </rp><rt lang="fr">Étoile</rt><rp>.</rp></ruby>';

$this->assertSame( $content, wp_kses( $content, $allowedposttags ) );
$this->assertEqualHTML( $content, wp_kses( $content, $allowedposttags ) );
}

/**
Expand All @@ -939,7 +939,7 @@ public function test_ol_reversed_attribute_allowed() {

$content = '<ol reversed="reversed"><li>Item 1</li><li>Item 2</li><li>Item 3</li></ol>';

$this->assertSame( $content, wp_kses( $content, $allowedposttags ) );
$this->assertEqualHTML( $content, wp_kses( $content, $allowedposttags ) );
}

/**
Expand All @@ -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() ) );
}

/**
Expand All @@ -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() ) );
}

/**
Expand All @@ -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() ) );
}

/**
Expand All @@ -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() ) );
}

/**
Expand Down Expand Up @@ -1423,7 +1423,7 @@ public function test_wp_kses_attr_data_attribute_is_allowed() {
$test = '<div data-foo="foo" data-bar="bar" datainvalid="gone" data-two-hyphens="remains">Pens and pencils</div>';
$expected = '<div data-foo="foo" data-bar="bar" data-two-hyphens="remains">Pens and pencils</div>';

$this->assertSame( $expected, wp_kses_post( $test ) );
$this->assertEqualHTML( $expected, wp_kses_post( $test ) );
}

/**
Expand All @@ -1435,7 +1435,7 @@ public function test_wp_kses_attr_data_attribute_hypens_allowed() {
$test = '<div data--leading="remains" data-trailing-="remains" data-middle--double="remains">Pens and pencils</div>';
$expected = '<div data--leading="remains" data-trailing-="remains" data-middle--double="remains">Pens and pencils</div>';

$this->assertSame( $expected, wp_kses_post( $test ) );
$this->assertEqualHTML( $expected, wp_kses_post( $test ) );
}

/**
Expand All @@ -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 );
}

/**
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand All @@ -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 ) );
}

/**
Expand All @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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() ),
'<body>',
'Failed to properly preserve HTML comment.'
);
}
Expand Down Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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 ) );
}

/**
Expand Down
Loading