Make WordPress Core

Changeset 53430


Ignore:
Timestamp:
05/23/2022 11:54:28 AM (23 months ago)
Author:
hellofromTonya
Message:

Build/Test Tools: Fix comments odd/even instabilities (test leaks).

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.

Follow-up to [53172].

Props hellofromTonya, zieladam, peterwilsoncc.
See #54725.

Location:
trunk/tests/phpunit
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r53349 r53430  
    172172        $current_screen_globals = array( 'current_screen', 'taxnow', 'typenow' );
    173173        foreach ( $current_screen_globals as $global ) {
     174            $GLOBALS[ $global ] = null;
     175        }
     176
     177        // Reset comment globals.
     178        $comment_globals = array( 'comment_alt', 'comment_depth', 'comment_thread_alt' );
     179        foreach ( $comment_globals as $global ) {
    174180            $GLOBALS[ $global ] = null;
    175181        }
  • trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php

    r53388 r53430  
    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
     48    public function tear_down() {
     49        // Reset the comment options to their original values.
     50        foreach ( static::$original_options as $option => $original_value ) {
     51            update_option( $option, $original_value );
     52        }
     53
     54        parent::tear_down();
     55    }
     56
    2357    public function set_up() {
    2458        parent::set_up();
     
    2660        update_option( 'page_comments', true );
    2761        update_option( 'comments_per_page', self::$per_page );
    28         update_option( 'comment_order', 'ASC' );
    2962
    3063        self::$custom_post = self::factory()->post->create_and_get(
     
    110143            build_comment_query_vars_from_block( $block )
    111144        );
    112         update_option( 'page_comments', true );
    113145    }
    114146
     
    188220            build_comment_query_vars_from_block( $block )
    189221        );
    190 
    191         update_option( 'comments_per_page', $comments_per_page );
    192         update_option( 'default_comments_page', $default_comments_page );
    193222    }
    194223
     
    309338            <<<END
    310339                <ol class="wp-block-comment-template">
    311                     <li id="comment-{$top_level_ids[0]}" class="comment odd alt thread-odd thread-alt depth-1">
     340                    <li id="comment-{$top_level_ids[0]}" class="comment even thread-even depth-1">
    312341                        <div class="wp-block-comment-author-name">
    313342                            <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    319348                        </div>
    320349                        <ol>
    321                             <li id="comment-{$first_level_ids[0]}" class="comment even depth-2">
     350                            <li id="comment-{$first_level_ids[0]}" class="comment odd alt depth-2">
    322351                                <div class="wp-block-comment-author-name">
    323352                                    <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    329358                                </div>
    330359                                <ol>
    331                                     <li id="comment-{$second_level_ids[0]}" class="comment odd alt depth-3">
     360                                    <li id="comment-{$second_level_ids[0]}" class="comment even depth-3">
    332361                                        <div class="wp-block-comment-author-name">
    333362                                            <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    341370                                </ol>
    342371                            </li>
    343                             <li id="comment-{$first_level_ids[1]}" class="comment even depth-2">
     372                            <li id="comment-{$first_level_ids[1]}" class="comment odd alt depth-2">
    344373                                <div class="wp-block-comment-author-name">
    345374                                    <a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >
     
    391420
    392421        $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>',
     422            '<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>',
    394423            $block->render()
    395424        );
     
    476505
    477506        $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>',
     507            '<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>',
    479508            str_replace( array( "\n", "\t" ), '', $block->render() ),
    480509            'Should include unapproved comments when filter applied'
     
    485514        // Test it again and ensure the unmoderated comment doesn't leak out.
    486515        $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>',
     516            '<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>',
    488517            str_replace( array( "\n", "\t" ), '', $block->render() ),
    489518            'Should not include any unapproved comments after removing filter'
Note: See TracChangeset for help on using the changeset viewer.