Skip to content

Commit dbe51c5

Browse files
committed
Fix broken comments controller test: wp_replace_in_html_tags()
Was detecting a non-escaped `<` as the start of an “element” and then replaced a newline in the text as `<!-- wpnl -->` since it thought it was replacing inside a tag. In the end that translated into a raw `\n` again in the end.
1 parent f91452e commit dbe51c5

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,24 +3107,50 @@ public function test_comment_roundtrip_as_editor_unfiltered_html() {
31073107
}
31083108
}
31093109

3110+
/**
3111+
* Ensures that saving a comment as a super-admin does not corrupt the
3112+
* comment content when presented with common edge cases.
3113+
*
3114+
* Note that this test used to assert the wrong behavior due to a bug
3115+
* in {@see wp_html_split()}. Whereby the unescaped `<` used to be
3116+
* mistakenly identified as the start of an HTML tag or comment, this
3117+
* led to accidental replacement “inside” the mistaken tag. The test
3118+
* has been updated with `wp_html_split()` in accordance with the
3119+
* HTML5 living specification.
3120+
*
3121+
* @ticket {TICKET_NUMBER}
3122+
*/
31103123
public function test_comment_roundtrip_as_superadmin() {
31113124
wp_set_current_user( self::$superadmin_id );
31123125

3126+
$raw_content = <<<'HTML'
3127+
\\&\\ &amp; &invalid; < &lt; &amp;lt;
3128+
HTML;
3129+
$rendered = <<<'HTML'
3130+
<p>\\&#038;\\ &amp; &invalid; < &lt; &amp;lt;</p>
3131+
HTML;
3132+
$author_name = <<<'HTML'
3133+
\\&amp;\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;
3134+
HTML;
3135+
$author_user_agent = <<<'HTML'
3136+
\\&\\ &amp; &invalid; &lt; &lt; &amp;lt;
3137+
HTML;
3138+
31133139
$this->assertTrue( current_user_can( 'unfiltered_html' ) );
31143140
$this->verify_comment_roundtrip(
31153141
array(
3116-
'content' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3117-
'author_name' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3118-
'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3142+
'content' => $raw_content,
3143+
'author_name' => $raw_content,
3144+
'author_user_agent' => $raw_content,
31193145
'author' => self::$superadmin_id,
31203146
),
31213147
array(
31223148
'content' => array(
3123-
'raw' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3124-
'rendered' => '<p>\\\&#038;\\\ &amp; &invalid; < &lt; &amp;lt;' . "\n</p>",
3149+
'raw' => $raw_content,
3150+
'rendered' => $rendered,
31253151
),
3126-
'author_name' => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
3127-
'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
3152+
'author_name' => $author_name,
3153+
'author_user_agent' => $author_user_agent,
31283154
'author' => self::$superadmin_id,
31293155
)
31303156
);

0 commit comments

Comments
 (0)