Make WordPress Core


Ignore:
Timestamp:
05/23/2022 02:04:23 PM (22 months ago)
Author:
hellofromTonya
Message:

Build/Test Tools: Add tests and fix comments odd/even instabilities (test leaks).

[53353] Add unit test for Comment Template block.

[53353] The odd / even class attribute global variables are causing issues in comments tests when a new test is added or an existing test is modified. To stabilize the odd / even comment tests, the comment global variables are added to the base test class' tear_down() using the same patterns as the other global resets. This change ensures each comment test starts at the same state. In doing so, the expected odd / even class attributes are no longer affected by previous tests, i.e. test leaks.

Also moves the comment globals reset from the base test case to the test's tear_down(). Why? To avoid risks to extenders' tests as it's too late in the 6.0 cycle for a dev note.

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

Props bernhard-reiter, darerodz, gziolo, hellofromTonya, zieladam, peterwilsoncc.
Merges [53353] and [53430] to the 6.0 branch.
Fixes #54725,#55643.

Location:
branches/6.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0

  • branches/6.0/tests/phpunit/tests/blocks/renderCommentTemplate.php

    r53389 r53432  
    2121    private static $per_page = 5;
    2222
     23    /**
     24     * Array of the comments options and their original values.
     25     * Used to reset the options after each test.
     26     *
     27     * @var array
     28     */
     29    private static $original_options;
     30
     31    public static function set_up_before_class() {
     32        parent::set_up_before_class();
     33
     34        // Store the original option values.
     35        $options = array(
     36            'comment_order',
     37            'comments_per_page',
     38            'default_comments_page',
     39            'page_comments',
     40            'previous_default_page',
     41            'thread_comments_depth',
     42        );
     43        foreach ( $options as $option ) {
     44            static::$original_options[ $option ] = get_option( $option );
     45        }
     46
     47        // Reset comment globals.
     48        $comment_globals = array( 'comment_alt', 'comment_depth', 'comment_thread_alt' );
     49        foreach ( $comment_globals as $global ) {
     50            $GLOBALS[ $global ] = null;
     51        }
     52    }
     53
     54    public function tear_down() {
     55        // Reset the comment options to their original values.
     56        foreach ( static::$original_options as $option => $original_value ) {
     57            update_option( $option, $original_value );
     58        }
     59
     60        // Reset comment globals.
     61        $comment_globals = array( 'comment_alt', 'comment_depth', 'comment_thread_alt' );
     62        foreach ( $comment_globals as $global ) {
     63            $GLOBALS[ $global ] = null;
     64        }
     65
     66        parent::tear_down();
     67    }
     68
    2369    public function set_up() {
    2470        parent::set_up();
     
    2672        update_option( 'page_comments', true );
    2773        update_option( 'comments_per_page', self::$per_page );
    28         update_option( 'comment_order', 'ASC' );
    2974
    3075        self::$custom_post = self::factory()->post->create_and_get(
     
    110155            build_comment_query_vars_from_block( $block )
    111156        );
    112         update_option( 'page_comments', true );
    113157    }
    114158
     
    188232            build_comment_query_vars_from_block( $block )
    189233        );
    190 
    191         update_option( 'comments_per_page', $comments_per_page );
    192         update_option( 'default_comments_page', $default_comments_page );
    193234    }
    194235
     
    262303     * └─ comment 1
    263304     *    └─ comment 2
    264      *       └─ comment 3
    265305     *       └─ comment 4
    266      *    └─ comment 5
     306     *    └─ comment 3
    267307     *
    268308     * @ticket 55567
     
    310350            <<<END
    311351                <ol class="wp-block-comment-template">
    312                     <li id="comment-{$top_level_ids[0]}" class="comment odd alt thread-odd thread-alt depth-1">
     352                    <li id="comment-{$top_level_ids[0]}" class="comment even thread-even depth-1">
    313353                        <div class="wp-block-comment-author-name">
    314354                            <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    320360                        </div>
    321361                        <ol>
    322                             <li id="comment-{$first_level_ids[0]}" class="comment even depth-2">
     362                            <li id="comment-{$first_level_ids[0]}" class="comment odd alt depth-2">
    323363                                <div class="wp-block-comment-author-name">
    324364                                    <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    330370                                </div>
    331371                                <ol>
    332                                     <li id="comment-{$second_level_ids[0]}" class="comment odd alt depth-3">
     372                                    <li id="comment-{$second_level_ids[0]}" class="comment even depth-3">
    333373                                        <div class="wp-block-comment-author-name">
    334374                                            <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    342382                                </ol>
    343383                            </li>
    344                             <li id="comment-{$first_level_ids[1]}" class="comment even depth-2">
     384                            <li id="comment-{$first_level_ids[1]}" class="comment odd alt depth-2">
    345385                                <div class="wp-block-comment-author-name">
    346386                                    <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    365405
    366406    /**
     407     * Test that line and paragraph breaks are converted to HTML tags in a comment.
     408     *
     409     * @ticket 55643
     410     */
     411    function test_render_block_core_comment_content_converts_to_html() {
     412        $comment_id  = self::$comment_ids[0];
     413        $new_content = "Paragraph One\n\nP2L1\nP2L2\n\nhttps://example.com/";
     414        self::factory()->comment->update_object(
     415            $comment_id,
     416            array( 'comment_content' => $new_content )
     417        );
     418
     419        $parsed_blocks = parse_blocks(
     420            '<!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template -->'
     421        );
     422
     423        $block = new WP_Block(
     424            $parsed_blocks[0],
     425            array(
     426                'postId'           => self::$custom_post->ID,
     427                'comments/inherit' => true,
     428            )
     429        );
     430
     431        $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";
     432
     433        $this->assertSame(
     434            '<ol class="wp-block-comment-template"><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-even depth-1"><div class="wp-block-comment-content">' . $expected_content . '</div></li></ol>',
     435            $block->render()
     436        );
     437    }
     438
     439    /**
    367440     * Test that unapproved comments are included if it is a preview.
    368441     *
     
    405478        );
    406479    }
     480
     481    /**
     482     * Test rendering an unapproved comment preview.
     483     *
     484     * @ticket 55643
     485     */
     486    function test_rendering_comment_template_unmoderated_preview() {
     487        $parsed_blocks = parse_blocks(
     488            '<!-- wp:comment-template --><!-- wp:comment-author-name /--><!-- wp:comment-content /--><!-- /wp:comment-template -->'
     489        );
     490
     491        $unapproved_comment = self::factory()->comment->create_post_comments(
     492            self::$custom_post->ID,
     493            1,
     494            array(
     495                'comment_author'       => 'Visitor',
     496                'comment_author_email' => 'unapproved@example.org',
     497                'comment_author_url'   => 'http://example.com/unapproved/',
     498                'comment_content'      => 'Hi there! My comment needs moderation.',
     499                'comment_approved'     => 0,
     500            )
     501        );
     502
     503        $block = new WP_Block(
     504            $parsed_blocks[0],
     505            array(
     506                'postId' => self::$custom_post->ID,
     507            )
     508        );
     509
     510        $commenter_filter = static function () {
     511            return array(
     512                'comment_author_email' => 'unapproved@example.org',
     513            );
     514        };
     515
     516        add_filter( 'wp_get_current_commenter', $commenter_filter );
     517
     518        $this->assertSame(
     519            '<ol class="wp-block-comment-template"><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-even 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-odd thread-alt 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>',
     520            str_replace( array( "\n", "\t" ), '', $block->render() ),
     521            'Should include unapproved comments when filter applied'
     522        );
     523
     524        remove_filter( 'wp_get_current_commenter', $commenter_filter );
     525
     526        // Test it again and ensure the unmoderated comment doesn't leak out.
     527        $this->assertSame(
     528            '<ol class="wp-block-comment-template"><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-even 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>',
     529            str_replace( array( "\n", "\t" ), '', $block->render() ),
     530            'Should not include any unapproved comments after removing filter'
     531        );
     532    }
    407533}
Note: See TracChangeset for help on using the changeset viewer.