Make WordPress Core

Changeset 53353


Ignore:
Timestamp:
05/05/2022 03:27:01 PM (4 years ago)
Author:
hellofromTonya
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php

    r53336 r53353  
    262262         * └─ comment 1
    263263         *    └─ comment 2
    264          *       └─ comment 3
    265264         *       └─ comment 4
    266          *    └─ comment 5
     265         *    └─ comment 3
    267266         *
    268267         * @ticket 55567
    … …  
    365364
    366365        /**
     366         * Test that line and paragraph breaks are converted to HTML tags in a comment.
     367         *
     368         * @ticket 55643
     369         */
     370        function test_render_block_core_comment_content_converts_to_html() {
     371                $comment_id  = self::$comment_ids[0];
     372                $new_content = "Paragraph One\n\nP2L1\nP2L2\n\nhttps://example.com/";
     373                self::factory()->comment->update_object(
     374                        $comment_id,
     375                        array( 'comment_content' => $new_content )
     376                );
     377
     378                $parsed_blocks = parse_blocks(
     379                        '<!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template -->'
     380                );
     381
     382                $block = new WP_Block(
     383                        $parsed_blocks[0],
     384                        array(
     385                                'postId'           => self::$custom_post->ID,
     386                                'comments/inherit' => true,
     387                        )
     388                );
     389
     390                $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";
     391
     392                $this->assertSame(
     393                        '<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>',
     394                        $block->render()
     395                );
     396        }
     397
     398        /**
    367399         * Test that unapproved comments are included if it is a preview.
    368400         *
    … …  
    405437                );
    406438        }
     439
     440        /**
     441         * Test rendering an unapproved comment preview.
     442         *
     443         * @ticket 55643
     444         */
     445        function test_rendering_comment_template_unmoderated_preview() {
     446                $parsed_blocks = parse_blocks(
     447                        '<!-- wp:comment-template --><!-- wp:comment-author-name /--><!-- wp:comment-content /--><!-- /wp:comment-template -->'
     448                );
     449
     450                $unapproved_comment = self::factory()->comment->create_post_comments(
     451                        self::$custom_post->ID,
     452                        1,
     453                        array(
     454                                'comment_author'       => 'Visitor',
     455                                'comment_author_email' => 'unapproved@example.org',
     456                                'comment_author_url'   => 'http://example.com/unapproved/',
     457                                'comment_content'      => 'Hi there! My comment needs moderation.',
     458                                'comment_approved'     => 0,
     459                        )
     460                );
     461
     462                $block = new WP_Block(
     463                        $parsed_blocks[0],
     464                        array(
     465                                'postId' => self::$custom_post->ID,
     466                        )
     467                );
     468
     469                $commenter_filter = static function () {
     470                        return array(
     471                                'comment_author_email' => 'unapproved@example.org',
     472                        );
     473                };
     474
     475                add_filter( 'wp_get_current_commenter', $commenter_filter );
     476
     477                $this->assertSame(
     478                        '<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>',
     479                        str_replace( array( "\n", "\t" ), '', $block->render() ),
     480                        'Should include unapproved comments when filter applied'
     481                );
     482
     483                remove_filter( 'wp_get_current_commenter', $commenter_filter );
     484
     485                // Test it again and ensure the unmoderated comment doesn't leak out.
     486                $this->assertSame(
     487                        '<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>',
     488                        str_replace( array( "\n", "\t" ), '', $block->render() ),
     489                        'Should not include any unapproved comments after removing filter'
     490                );
     491        }
    407492}
Note: See TracChangeset for help on using the changeset viewer.