Skip to content

Commit 99fed56

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 98602ab commit 99fed56

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,24 +3150,50 @@ public function test_comment_roundtrip_as_editor_unfiltered_html() {
31503150
}
31513151
}
31523152

3153+
/**
3154+
* Ensures that saving a comment as a super-admin does not corrupt the
3155+
* comment content when presented with common edge cases.
3156+
*
3157+
* Note that this test used to assert the wrong behavior due to a bug
3158+
* in {@see wp_html_split()}. Whereby the unescaped `<` used to be
3159+
* mistakenly identified as the start of an HTML tag or comment, this
3160+
* led to accidental replacement “inside” the mistaken tag. The test
3161+
* has been updated with `wp_html_split()` in accordance with the
3162+
* HTML5 living specification.
3163+
*
3164+
* @ticket {TICKET_NUMBER}
3165+
*/
31533166
public function test_comment_roundtrip_as_superadmin() {
31543167
wp_set_current_user( self::$superadmin_id );
31553168

3169+
$raw_content = <<<'HTML'
3170+
\\&\\ &amp; &invalid; < &lt; &amp;lt;
3171+
HTML;
3172+
$rendered = <<<'HTML'
3173+
<p>\\&#038;\\ &amp; &invalid; < &lt; &amp;lt;</p>
3174+
HTML;
3175+
$author_name = <<<'HTML'
3176+
\\&amp;\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;
3177+
HTML;
3178+
$author_user_agent = <<<'HTML'
3179+
\\&\\ &amp; &invalid; &lt; &lt; &amp;lt;
3180+
HTML;
3181+
31563182
$this->assertTrue( current_user_can( 'unfiltered_html' ) );
31573183
$this->verify_comment_roundtrip(
31583184
array(
3159-
'content' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3160-
'author_name' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3161-
'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3185+
'content' => $raw_content,
3186+
'author_name' => $raw_content,
3187+
'author_user_agent' => $raw_content,
31623188
'author' => self::$superadmin_id,
31633189
),
31643190
array(
31653191
'content' => array(
3166-
'raw' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
3167-
'rendered' => '<p>\\\&#038;\\\ &amp; &invalid; < &lt; &amp;lt;' . "\n</p>",
3192+
'raw' => $raw_content,
3193+
'rendered' => $rendered,
31683194
),
3169-
'author_name' => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
3170-
'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
3195+
'author_name' => $author_name,
3196+
'author_user_agent' => $author_user_agent,
31713197
'author' => self::$superadmin_id,
31723198
)
31733199
);

0 commit comments

Comments
 (0)