From ff0386b5c1bb70672135b779ef37b62190bc49a3 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 26 Aug 2016 17:41:51 +0000 Subject: [PATCH] Use shared fixture in `comment_exists()` tests. Props Frank Klein. Fixes #37842. git-svn-id: https://develop.svn.wordpress.org/trunk@38372 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/admin/includesComment.php | 106 +++++++++--------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/tests/phpunit/tests/admin/includesComment.php b/tests/phpunit/tests/admin/includesComment.php index 36f9897794..ceb6e9956c 100644 --- a/tests/phpunit/tests/admin/includesComment.php +++ b/tests/phpunit/tests/admin/includesComment.php @@ -5,82 +5,86 @@ * @group comment */ class Tests_Admin_IncludesComment extends WP_UnitTestCase { + /** + * Post ID to add comments to. + * + * @var int + */ + public static $post_id; + + /** + * Comment IDs. + * + * @var array + */ + public static $comment_ids = array(); + + /** + * Create the post and comments for the tests. + * + * @param WP_UnitTest_Factory $factory + */ + public static function wpSetUpBeforeClass( $factory ) { + self::$post_id = $factory->post->create(); + + self::$comment_ids[] = $factory->comment->create( array( + 'comment_author' => 1, + 'comment_date' => '2014-05-06 12:00:00', + 'comment_date_gmt' => '2014-05-06 07:00:00', + 'comment_post_ID' => self::$post_id, + ) ); + + self::$comment_ids[] = $factory->comment->create( array( + 'comment_author' => 2, + 'comment_date' => '2004-01-02 12:00:00', + 'comment_post_ID' => self::$post_id, + ) ); + } + + /** + * Delete the post and comments for the tests. + */ + public static function wpTearDownAfterClass() { + foreach ( self::$comment_ids as $comment_id ) { + wp_delete_comment( $comment_id, true ); + } + + wp_delete_post( self::$post_id, true ); + } + + /** + * Verify that both the comment date and author must match for a comment to exist. + */ public function test_must_match_date_and_author() { - $p1 = self::factory()->post->create(); - $c1 = self::factory()->comment->create( array( - 'comment_author' => 1, - 'comment_date' => '2014-05-06 12:00:00', - 'comment_post_ID' => $p1, - ) ); - - $p2 = self::factory()->post->create(); - $c2 = self::factory()->comment->create( array( - 'comment_author' => 2, - 'comment_date' => '2004-01-02 12:00:00', - 'comment_post_ID' => $p2, - ) ); - $this->assertNull( comment_exists( 1, '2004-01-02 12:00:00' ) ); - $this->assertEquals( $p1, comment_exists( 1, '2014-05-06 12:00:00' ) ); + $this->assertEquals( self::$post_id, comment_exists( 1, '2014-05-06 12:00:00' ) ); } /** * @ticket 33871 */ public function test_default_value_of_timezone_should_be_blog() { - $p = self::factory()->post->create(); - $c = self::factory()->comment->create( array( - 'comment_author' => 1, - 'comment_post_ID' => $p, - 'comment_date' => '2014-05-06 12:00:00', - 'comment_date_gmt' => '2014-05-06 07:00:00', - ) ); - - $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00' ) ); + $this->assertEquals( self::$post_id, comment_exists( 1, '2014-05-06 12:00:00' ) ); } /** * @ticket 33871 */ public function test_should_respect_timezone_blog() { - $p = self::factory()->post->create(); - $c = self::factory()->comment->create( array( - 'comment_author' => 1, - 'comment_post_ID' => $p, - 'comment_date' => '2014-05-06 12:00:00', - 'comment_date_gmt' => '2014-05-06 07:00:00', - ) ); - - $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00', 'blog' ) ); + $this->assertEquals( self::$post_id, comment_exists( 1, '2014-05-06 12:00:00', 'blog' ) ); } /** * @ticket 33871 */ public function test_should_respect_timezone_gmt() { - $p = self::factory()->post->create(); - $c = self::factory()->comment->create( array( - 'comment_author' => 1, - 'comment_post_ID' => $p, - 'comment_date' => '2014-05-06 12:00:00', - 'comment_date_gmt' => '2014-05-06 07:00:00', - ) ); - - $this->assertEquals( $p, comment_exists( 1, '2014-05-06 07:00:00', 'gmt' ) ); + $this->assertEquals( self::$post_id, comment_exists( 1, '2014-05-06 07:00:00', 'gmt' ) ); } /** * @ticket 33871 */ public function test_invalid_timezone_should_fall_back_on_blog() { - $p = self::factory()->post->create(); - $c = self::factory()->comment->create( array( - 'comment_author' => 1, - 'comment_post_ID' => $p, - 'comment_date' => '2014-05-06 12:00:00', - 'comment_date_gmt' => '2014-05-06 07:00:00', - ) ); - - $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00', 'not_a_valid_value' ) ); + $this->assertEquals( self::$post_id, comment_exists( 1, '2014-05-06 12:00:00', 'not_a_valid_value' ) ); } }