From 4f2a98929e543d6ff4e12a48bf8078417ac1b2b9 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Thu, 5 May 2022 15:27:01 +0000 Subject: [PATCH] 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 --- .../tests/blocks/renderCommentTemplate.php | 89 ++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index 4199537f0c..49769ee6e0 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -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( + '' + ); + + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$custom_post->ID, + 'comments/inherit' => true, + ) + ); + + $expected_content = "

Paragraph One

\n

P2L1
\nP2L2

\n

https://example.com/

\n"; + + $this->assertSame( + '
  1. ' . $expected_content . '
', + $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( + '' + ); + + $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( + '
  1. Hello world

  2. Visitor

    Your comment is awaiting moderation.

    Hi there! My comment needs moderation.
', + 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( + '
  1. Hello world

', + str_replace( array( "\n", "\t" ), '', $block->render() ), + 'Should not include any unapproved comments after removing filter' + ); + } }