From 59b83bd9fdad123f8c440d3a5e3ed07b8b7312e6 Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 13 Jun 2024 13:44:57 +0200 Subject: [PATCH 1/2] Add unit test confirming that there are no extra queries when inserting a post with no tag parameters --- tests/phpunit/tests/post/wpInsertPost.php | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/phpunit/tests/post/wpInsertPost.php b/tests/phpunit/tests/post/wpInsertPost.php index 701a48bdea311..a3c4db55149c6 100644 --- a/tests/phpunit/tests/post/wpInsertPost.php +++ b/tests/phpunit/tests/post/wpInsertPost.php @@ -1558,4 +1558,54 @@ public function test_scheduled_post_with_a_past_date_should_be_published() { $this->assertSame( 'future', get_post_status( $post_id ) ); } + + /** + * @ticket 59354 + * @covers ::wp_insert_post + */ + public function test_extra_queries_when_updating_tags() { + // First we creat a post with a tag, for the basis of our test. + $post_id = wp_insert_post( + array( + 'post_title' => 'title', + 'tags_input' => 'new_tag', + ) + ); + + $starting_number_of_queries = get_num_queries(); + + // We now update the post, while having tags as parameters. + $post_id = wp_insert_post( + array( + 'post_title' => 'title', + 'tags_input' => 'new_tag', + ) + ); + + $queries_added_by_updating_post_with_tag_parameters = get_num_queries() - $starting_number_of_queries; + + // We now update the post, again while having tags as parameters. + $post_id = wp_insert_post( + array( + 'post_title' => 'title', + 'tags_input' => 'new_tag', + ) + ); + + $queries_added_by_updating_post_with_tag_parameters_again = get_num_queries() - $queries_added_by_updating_post_with_tag_parameters - $starting_number_of_queries; + + $this->assertSame( $queries_added_by_updating_post_with_tag_parameters_again, $queries_added_by_updating_post_with_tag_parameters, 'The two inserts should perform the same amount of queries.' ); + + // We now update the post, but while NOT having tags as parameters. + $post_id = wp_insert_post( + array( + 'post_title' => 'title', + ) + ); + + $queries_added_by_updating_post_with_no_tag_parameters = get_num_queries() - $queries_added_by_updating_post_with_tag_parameters_again - $queries_added_by_updating_post_with_tag_parameters - $starting_number_of_queries; + + $this->assertLessThan( $queries_added_by_updating_post_with_tag_parameters, $queries_added_by_updating_post_with_no_tag_parameters, 'Inserts without tag parameters should perform less amount of queries with ones with tag parameters.' ); + + } } From c4d1e8ff399d715cdf7a04d6aa24f6f218eb6a6b Mon Sep 17 00:00:00 2001 From: Leonidas Milosis Date: Thu, 13 Jun 2024 13:50:26 +0200 Subject: [PATCH 2/2] Fix PHPCS --- tests/phpunit/tests/post/wpInsertPost.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/post/wpInsertPost.php b/tests/phpunit/tests/post/wpInsertPost.php index a3c4db55149c6..76e009669e13c 100644 --- a/tests/phpunit/tests/post/wpInsertPost.php +++ b/tests/phpunit/tests/post/wpInsertPost.php @@ -1593,7 +1593,7 @@ public function test_extra_queries_when_updating_tags() { ); $queries_added_by_updating_post_with_tag_parameters_again = get_num_queries() - $queries_added_by_updating_post_with_tag_parameters - $starting_number_of_queries; - + $this->assertSame( $queries_added_by_updating_post_with_tag_parameters_again, $queries_added_by_updating_post_with_tag_parameters, 'The two inserts should perform the same amount of queries.' ); // We now update the post, but while NOT having tags as parameters. @@ -1604,8 +1604,7 @@ public function test_extra_queries_when_updating_tags() { ); $queries_added_by_updating_post_with_no_tag_parameters = get_num_queries() - $queries_added_by_updating_post_with_tag_parameters_again - $queries_added_by_updating_post_with_tag_parameters - $starting_number_of_queries; - - $this->assertLessThan( $queries_added_by_updating_post_with_tag_parameters, $queries_added_by_updating_post_with_no_tag_parameters, 'Inserts without tag parameters should perform less amount of queries with ones with tag parameters.' ); + $this->assertLessThan( $queries_added_by_updating_post_with_tag_parameters, $queries_added_by_updating_post_with_no_tag_parameters, 'Inserts without tag parameters should perform less amount of queries with ones with tag parameters.' ); } }