Editor: Add unit test for Comment Template block.

Backport of 2 tests from Gutenberg for the Comment Template block:

* test line and paragraph breaks are converted into HTML tags.
* test rendering of unapproved comment preview.

Follow-up to [53298], [53172], [53138].

Props bernhard-reiter, darerodz, gziolo, peterwilsoncc, hellofromTonya. 
Fixes #55643.
See #55634.

git-svn-id: https://develop.svn.wordpress.org/trunk@53353 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
hellofromTonya 2022-05-05 15:27:01 +00:00
parent b27069117e
commit 4f2a98929e

View File

@ -261,9 +261,8 @@ class Tests_Blocks_RenderReusableCommentTemplate extends WP_UnitTestCase {
*
* └─ comment 1
*   └─ comment 2
*    └─ comment 3
*    └─ comment 4
*   └─ comment 5
*   └─ comment 3
*
* @ticket 55567
*/
@ -363,6 +362,39 @@ END
);
}
/**
* Test that line and paragraph breaks are converted to HTML tags in a comment.
*
* @ticket 55643
*/
function test_render_block_core_comment_content_converts_to_html() {
$comment_id = self::$comment_ids[0];
$new_content = "Paragraph One\n\nP2L1\nP2L2\n\nhttps://example.com/";
self::factory()->comment->update_object(
$comment_id,
array( 'comment_content' => $new_content )
);
$parsed_blocks = parse_blocks(
'<!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template -->'
);
$block = new WP_Block(
$parsed_blocks[0],
array(
'postId' => self::$custom_post->ID,
'comments/inherit' => true,
)
);
$expected_content = "<p>Paragraph One</p>\n<p>P2L1<br />\nP2L2</p>\n<p><a href=\"https://example.com/\" rel=\"nofollow ugc\">https://example.com/</a></p>\n";
$this->assertSame(
'<ol class="wp-block-comment-template"><li id="comment-' . self::$comment_ids[0] . '" class="comment odd alt thread-even depth-1"><div class="wp-block-comment-content">' . $expected_content . '</div></li></ol>',
$block->render()
);
}
/**
* Test that unapproved comments are included if it is a preview.
*
@ -404,4 +436,57 @@ END
)
);
}
/**
* Test rendering an unapproved comment preview.
*
* @ticket 55643
*/
function test_rendering_comment_template_unmoderated_preview() {
$parsed_blocks = parse_blocks(
'<!-- wp:comment-template --><!-- wp:comment-author-name /--><!-- wp:comment-content /--><!-- /wp:comment-template -->'
);
$unapproved_comment = self::factory()->comment->create_post_comments(
self::$custom_post->ID,
1,
array(
'comment_author' => 'Visitor',
'comment_author_email' => 'unapproved@example.org',
'comment_author_url' => 'http://example.com/unapproved/',
'comment_content' => 'Hi there! My comment needs moderation.',
'comment_approved' => 0,
)
);
$block = new WP_Block(
$parsed_blocks[0],
array(
'postId' => self::$custom_post->ID,
)
);
$commenter_filter = static function () {
return array(
'comment_author_email' => 'unapproved@example.org',
);
};
add_filter( 'wp_get_current_commenter', $commenter_filter );
$this->assertSame(
'<ol class="wp-block-comment-template"><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-odd thread-alt depth-1"><div class="wp-block-comment-author-name"><a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >Test</a></div><div class="wp-block-comment-content"><p>Hello world</p></div></li><li id="comment-' . $unapproved_comment[0] . '" class="comment odd alt thread-even depth-1"><div class="wp-block-comment-author-name">Visitor</div><div class="wp-block-comment-content"><p><em class="comment-awaiting-moderation">Your comment is awaiting moderation.</em></p>Hi there! My comment needs moderation.</div></li></ol>',
str_replace( array( "\n", "\t" ), '', $block->render() ),
'Should include unapproved comments when filter applied'
);
remove_filter( 'wp_get_current_commenter', $commenter_filter );
// Test it again and ensure the unmoderated comment doesn't leak out.
$this->assertSame(
'<ol class="wp-block-comment-template"><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-odd thread-alt depth-1"><div class="wp-block-comment-author-name"><a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >Test</a></div><div class="wp-block-comment-content"><p>Hello world</p></div></li></ol>',
str_replace( array( "\n", "\t" ), '', $block->render() ),
'Should not include any unapproved comments after removing filter'
);
}
}