From edf3f48e0499519b4c5be4fed04d056527ddf999 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 23 May 2022 11:54:28 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@53430 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 6 +++ .../tests/blocks/renderCommentTemplate.php | 53 ++++++++++++++----- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index c0dc30b189..d50e4e7d52 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -174,6 +174,12 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { $GLOBALS[ $global ] = null; } + // Reset comment globals. + $comment_globals = array( 'comment_alt', 'comment_depth', 'comment_thread_alt' ); + foreach ( $comment_globals as $global ) { + $GLOBALS[ $global ] = null; + } + /* * Reset $wp_sitemap global so that sitemap-related dynamic $wp->public_query_vars * are added when the next test runs. diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php index f5b78ca92a..848b2caf38 100644 --- a/tests/phpunit/tests/blocks/renderCommentTemplate.php +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -20,12 +20,45 @@ class Tests_Blocks_RenderReusableCommentTemplate extends WP_UnitTestCase { private static $comment_ids; private static $per_page = 5; + /** + * Array of the comments options and their original values. + * Used to reset the options after each test. + * + * @var array + */ + private static $original_options; + + public static function set_up_before_class() { + parent::set_up_before_class(); + + // Store the original option values. + $options = array( + 'comment_order', + 'comments_per_page', + 'default_comments_page', + 'page_comments', + 'previous_default_page', + 'thread_comments_depth', + ); + foreach ( $options as $option ) { + static::$original_options[ $option ] = get_option( $option ); + } + } + + public function tear_down() { + // Reset the comment options to their original values. + foreach ( static::$original_options as $option => $original_value ) { + update_option( $option, $original_value ); + } + + parent::tear_down(); + } + public function set_up() { parent::set_up(); update_option( 'page_comments', true ); update_option( 'comments_per_page', self::$per_page ); - update_option( 'comment_order', 'ASC' ); self::$custom_post = self::factory()->post->create_and_get( array( @@ -109,7 +142,6 @@ class Tests_Blocks_RenderReusableCommentTemplate extends WP_UnitTestCase { ), build_comment_query_vars_from_block( $block ) ); - update_option( 'page_comments', true ); } /** @@ -187,9 +219,6 @@ class Tests_Blocks_RenderReusableCommentTemplate extends WP_UnitTestCase { ), build_comment_query_vars_from_block( $block ) ); - - update_option( 'comments_per_page', $comments_per_page ); - update_option( 'default_comments_page', $default_comments_page ); } @@ -308,7 +337,7 @@ class Tests_Blocks_RenderReusableCommentTemplate extends WP_UnitTestCase { '', << -
  • +
    1. -
    2. +
      1. -
      2. +
    3. -
    4. +
    5. Test @@ -390,7 +419,7 @@ END $expected_content = "

      Paragraph One

      \n

      P2L1
      \nP2L2

      \n

      https://example.com/

      \n"; $this->assertSame( - '
      1. ' . $expected_content . '
      ', + '
      1. ' . $expected_content . '
      ', $block->render() ); } @@ -475,7 +504,7 @@ END 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.
      ', + '
      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' ); @@ -484,7 +513,7 @@ END // Test it again and ensure the unmoderated comment doesn't leak out. $this->assertSame( - '
      1. Hello world

      ', + '
      1. Hello world

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