From 06608043274034d8429653505bae672a4be04752 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 8 Dec 2019 18:00:23 +0000 Subject: [PATCH] Tests: Speed up comment submission unit tests by creating less fixtures and reusing them where possible. See #30017, #48145. git-svn-id: https://develop.svn.wordpress.org/trunk@46829 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/comment-submission.php | 244 ++++++++------------- 1 file changed, 96 insertions(+), 148 deletions(-) diff --git a/tests/phpunit/tests/comment-submission.php b/tests/phpunit/tests/comment-submission.php index 2ad7f49bf5..2d288b3e85 100644 --- a/tests/phpunit/tests/comment-submission.php +++ b/tests/phpunit/tests/comment-submission.php @@ -5,8 +5,35 @@ */ class Tests_Comment_Submission extends WP_UnitTestCase { + protected static $post; + protected static $author_id; + protected static $editor_id; + protected $preprocess_comment_data = array(); + public static function wpSetUpBeforeClass( $factory ) { + self::$post = $factory->post->create_and_get(); + + self::$author_id = $factory->user->create( + array( + 'role' => 'author', + ) + ); + + self::$editor_id = $factory->user->create( + array( + 'role' => 'editor', + ) + ); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post->ID, true ); + + self::delete_user( self::$author_id ); + self::delete_user( self::$editor_id ); + } + function setUp() { parent::setUp(); require_once ABSPATH . WPINC . '/class-phpass.php'; @@ -34,11 +61,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertSame( 0, did_action( $error ) ); - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'comment_status' => 'closed', ) ); + $data = array( 'comment_post_ID' => $post->ID, ); @@ -56,13 +84,15 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertSame( 0, did_action( $error ) ); - $post = self::factory()->post->create_and_get(); - wp_trash_post( $post->ID ); + wp_trash_post( self::$post->ID ); + $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, ); $comment = wp_handle_comment_submission( $data ); + wp_untrash_post( self::$post->ID ); + $this->assertSame( 1, did_action( $error ) ); $this->assertWPError( $comment ); $this->assertSame( $error, $comment->get_error_code() ); @@ -74,11 +104,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertSame( 0, did_action( $error ) ); - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_status' => 'draft', ) ); + $data = array( 'comment_post_ID' => $post->ID, ); @@ -97,22 +128,17 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_comment_to_draft_post_returns_error_message_for_user_with_correct_caps() { $error = 'comment_on_draft'; - $user = self::factory()->user->create_and_get( - array( - 'role' => 'author', - ) - ); - - wp_set_current_user( $user->ID ); + wp_set_current_user( self::$author_id ); $this->assertSame( 0, did_action( $error ) ); - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_status' => 'draft', - 'post_author' => $user->ID, + 'post_author' => self::$author_id, ) ); + $data = array( 'comment_post_ID' => $post->ID, ); @@ -156,11 +182,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertSame( 0, did_action( $error ) ); - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_password' => 'password', ) ); + $data = array( 'comment_post_ID' => $post->ID, ); @@ -179,11 +206,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = $hasher->HashPassword( $password ); - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_password' => $password, ) ); + $data = array( 'comment_post_ID' => $post->ID, 'comment' => 'Comment', @@ -209,9 +237,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { wp_set_current_user( $user->ID ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', ); $comment = wp_handle_comment_submission( $data ); @@ -229,9 +256,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_valid_comment_anonymously_succeeds() { - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', 'author' => 'Comment Author', 'email' => 'comment@example.org', @@ -257,9 +283,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { */ public function test_submitting_comment_handles_slashes_correctly_handles_slashes() { - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment with 1 slash: \\', 'author' => 'Comment Author with 1 slash: \\', 'email' => 'comment@example.org', @@ -279,11 +304,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $error = 'comment_id_not_found'; - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_status' => 'private', ) ); + $data = array( 'comment_post_ID' => $post->ID, ); @@ -299,12 +325,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $error = 'comment_id_not_found'; - $author = self::factory()->user->create_and_get( - array( - 'role' => 'author', - ) - ); - $user = self::factory()->user->create_and_get( + $user = self::factory()->user->create_and_get( array( 'role' => 'author', ) @@ -312,12 +333,13 @@ class Tests_Comment_Submission extends WP_UnitTestCase { wp_set_current_user( $user->ID ); - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_status' => 'private', - 'post_author' => $author->ID, + 'post_author' => self::$author_id, ) ); + $data = array( 'comment_post_ID' => $post->ID, ); @@ -333,12 +355,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $error = 'comment_id_not_found'; - $author = self::factory()->user->create_and_get( - array( - 'role' => 'author', - ) - ); - $user = self::factory()->user->create_and_get( + $user = self::factory()->user->create_and_get( array( 'role' => 'author', ) @@ -346,13 +363,14 @@ class Tests_Comment_Submission extends WP_UnitTestCase { wp_set_current_user( $user->ID ); - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_status' => 'private', - 'post_author' => $author->ID, + 'post_author' => self::$author_id, 'comment_status' => 'closed', ) ); + $data = array( 'comment_post_ID' => $post->ID, ); @@ -366,16 +384,15 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_comment_to_own_private_post_succeeds() { - $user = self::factory()->user->create_and_get(); + wp_set_current_user( self::$author_id ); - wp_set_current_user( $user->ID ); - - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_status' => 'private', - 'post_author' => $user->ID, + 'post_author' => self::$author_id, ) ); + $data = array( 'comment_post_ID' => $post->ID, 'comment' => 'Comment', @@ -390,25 +407,15 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_comment_to_accessible_private_post_succeeds() { - $author = self::factory()->user->create_and_get( - array( - 'role' => 'author', - ) - ); - $user = self::factory()->user->create_and_get( - array( - 'role' => 'editor', - ) - ); + wp_set_current_user( self::$editor_id ); - wp_set_current_user( $user->ID ); - - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_status' => 'private', - 'post_author' => $author->ID, + 'post_author' => self::$author_id, ) ); + $data = array( 'comment_post_ID' => $post->ID, 'comment' => 'Comment', @@ -423,9 +430,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_anonymous_user_cannot_comment_unfiltered_html() { - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment ', 'author' => 'Comment Author', 'email' => 'comment@example.org', @@ -440,18 +446,12 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_unprivileged_user_cannot_comment_unfiltered_html() { - $user = self::factory()->user->create_and_get( - array( - 'role' => 'author', - ) - ); - wp_set_current_user( $user->ID ); + wp_set_current_user( self::$author_id ); $this->assertFalse( current_user_can( 'unfiltered_html' ) ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment ', ); $comment = wp_handle_comment_submission( $data ); @@ -464,23 +464,17 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_unprivileged_user_cannot_comment_unfiltered_html_even_with_valid_nonce() { - $user = self::factory()->user->create_and_get( - array( - 'role' => 'author', - ) - ); - wp_set_current_user( $user->ID ); + wp_set_current_user( self::$author_id ); $this->assertFalse( current_user_can( 'unfiltered_html' ) ); - $post = self::factory()->post->create_and_get(); - $action = 'unfiltered-html-comment_' . $post->ID; + $action = 'unfiltered-html-comment_' . self::$post->ID; $nonce = wp_create_nonce( $action ); $this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) ); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment ', '_wp_unfiltered_html_comment' => $nonce, ); @@ -496,30 +490,23 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertFalse( defined( 'DISALLOW_UNFILTERED_HTML' ) ); - $user = self::factory()->user->create_and_get( - array( - 'role' => 'editor', - ) - ); - if ( is_multisite() ) { // In multisite, only Super Admins can post unfiltered HTML - $this->assertFalse( user_can( $user->ID, 'unfiltered_html' ) ); - grant_super_admin( $user->ID ); + $this->assertFalse( user_can( self::$editor_id, 'unfiltered_html' ) ); + grant_super_admin( self::$editor_id ); } - wp_set_current_user( $user->ID ); + wp_set_current_user( self::$editor_id ); $this->assertTrue( current_user_can( 'unfiltered_html' ) ); - $post = self::factory()->post->create_and_get(); - $action = 'unfiltered-html-comment_' . $post->ID; + $action = 'unfiltered-html-comment_' . self::$post->ID; $nonce = wp_create_nonce( $action ); $this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) ); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment ', '_wp_unfiltered_html_comment' => $nonce, ); @@ -533,25 +520,18 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_privileged_user_cannot_comment_unfiltered_html_without_valid_nonce() { - $user = self::factory()->user->create_and_get( - array( - 'role' => 'editor', - ) - ); - if ( is_multisite() ) { // In multisite, only Super Admins can post unfiltered HTML - $this->assertFalse( user_can( $user->ID, 'unfiltered_html' ) ); - grant_super_admin( $user->ID ); + $this->assertFalse( user_can( self::$editor_id, 'unfiltered_html' ) ); + grant_super_admin( self::$editor_id ); } - wp_set_current_user( $user->ID ); + wp_set_current_user( self::$editor_id ); $this->assertTrue( current_user_can( 'unfiltered_html' ) ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment ', ); $comment = wp_handle_comment_submission( $data ); @@ -569,9 +549,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $_comment_registration = get_option( 'comment_registration' ); update_option( 'comment_registration', '1' ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, ); $comment = wp_handle_comment_submission( $data ); @@ -589,9 +568,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $_require_name_email = get_option( 'require_name_email' ); update_option( 'require_name_email', '1' ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', 'email' => 'comment@example.org', ); @@ -611,9 +589,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $_require_name_email = get_option( 'require_name_email' ); update_option( 'require_name_email', '1' ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', 'author' => 'Comment Author', ); @@ -633,9 +610,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $_require_name_email = get_option( 'require_name_email' ); update_option( 'require_name_email', '1' ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', 'author' => 'Comment Author', 'email' => 'not_an_email', @@ -653,9 +629,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $error = 'require_valid_comment'; - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => '', 'author' => 'Comment Author', 'email' => 'comment@example.org', @@ -673,10 +648,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_comment_with_content_too_long_returns_error() { $error = 'comment_content_column_length'; - $post = self::factory()->post->create_and_get(); - $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => rand_long_str( 65536 ), 'author' => 'Comment Author', 'email' => 'comment@example.org', @@ -693,10 +666,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_comment_with_author_too_long_returns_error() { $error = 'comment_author_column_length'; - $post = self::factory()->post->create_and_get(); - $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', 'author' => rand_long_str( 255 ), 'email' => 'comment@example.org', @@ -713,10 +684,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_comment_with_email_too_long_returns_error() { $error = 'comment_author_email_column_length'; - $post = self::factory()->post->create_and_get(); - $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', 'author' => 'Comment Author', 'email' => rand_long_str( 90 ) . '@example.com', @@ -733,9 +702,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { public function test_submitting_comment_with_url_too_long_returns_error() { $error = 'comment_author_url_column_length'; - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', 'author' => 'Comment Author', 'email' => 'comment@example.org', @@ -752,16 +720,11 @@ class Tests_Comment_Submission extends WP_UnitTestCase { */ public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() { - $user = self::factory()->user->create_and_get( - array( - 'role' => 'author', - ) - ); + $user = get_userdata( self::$author_id ); wp_set_current_user( $user->ID ); - $post = self::factory()->post->create_and_get(); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Comment', ); @@ -774,7 +737,7 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertNotWPError( $comment ); $this->assertEquals( array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment_author' => $user->display_name, 'comment_author_email' => $user->user_email, 'comment_author_url' => $user->user_url, @@ -798,13 +761,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { * @ticket 36901 */ public function test_submitting_duplicate_comments() { - $post = self::factory()->post->create_and_get( - array( - 'post_status' => 'publish', - ) - ); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Did I say that?', 'author' => 'Repeat myself', 'email' => 'mail@example.com', @@ -819,13 +777,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { * @ticket 36901 */ public function test_comments_flood() { - $post = self::factory()->post->create_and_get( - array( - 'post_status' => 'publish', - ) - ); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Did I say that?', 'author' => 'Repeat myself', 'email' => 'mail@example.com', @@ -850,13 +803,8 @@ class Tests_Comment_Submission extends WP_UnitTestCase { ); wp_set_current_user( $user->ID ); - $post = self::factory()->post->create_and_get( - array( - 'post_status' => 'publish', - ) - ); $data = array( - 'comment_post_ID' => $post->ID, + 'comment_post_ID' => self::$post->ID, 'comment' => 'Did I say that?', 'author' => 'Repeat myself', 'email' => 'mail@example.com', @@ -867,6 +815,6 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $second_comment = wp_handle_comment_submission( $data ); $this->assertNotWPError( $second_comment ); - $this->assertEquals( $post->ID, $second_comment->comment_post_ID ); + $this->assertEquals( self::$post->ID, $second_comment->comment_post_ID ); } }