From 054b6f2d4cb760b4c542d3362cc97e6500373035 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 4 Jun 2021 16:34:21 -0600 Subject: [PATCH 1/8] Changes from 30854.3.diff --- src/wp-includes/default-filters.php | 1 + src/wp-includes/theme.php | 11 +--- tests/phpunit/tests/post/revisions.php | 69 +++++++++++++++++++------- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 5b312b54f1825..88032893f1553 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -397,6 +397,7 @@ add_action( 'shutdown', 'wp_ob_end_flush_all', 1 ); // Create a revision whenever a post is updated. add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); +add_action( 'new_to_publish', 'wp_save_post_revision', 10, 1 ); add_action( 'publish_post', '_publish_post_hook', 5, 1 ); add_action( 'transition_post_status', '_transition_post_status', 5, 3 ); add_action( 'transition_post_status', '_update_term_count_on_transition_post_status', 10, 3 ); diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 458b8e6b3a403..afd0e0d86f211 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -2002,15 +2002,8 @@ function wp_update_custom_css_post( $css, $args = array() ) { } else { $r = wp_insert_post( wp_slash( $post_data ), true ); - if ( ! is_wp_error( $r ) ) { - if ( get_stylesheet() === $args['stylesheet'] ) { - set_theme_mod( 'custom_css_post_id', $r ); - } - - // Trigger creation of a revision. This should be removed once #30854 is resolved. - if ( 0 === count( wp_get_post_revisions( $r ) ) ) { - wp_save_post_revision( $r ); - } + if ( ! is_wp_error( $r ) && get_stylesheet() === $args['stylesheet'] ) { + set_theme_mod( 'custom_css_post_id', $r ); } } diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 5f0131835f802..c6f1ac8cf70ff 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -229,7 +229,7 @@ function test_revision_view_caps_post() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) ); foreach ( $revisions as $revision ) { @@ -262,7 +262,7 @@ function test_revision_restore_caps_post() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) ); } @@ -300,7 +300,7 @@ function test_revision_diff_caps_post() { // Diff checks if you can read both left and right revisions. $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 2, $revisions ); + $this->assertCount( 3, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) ); } @@ -340,7 +340,7 @@ function test_revision_view_caps_cpt() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) ); foreach ( $revisions as $revision ) { @@ -387,7 +387,7 @@ function test_revision_restore_caps_cpt() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) ); } @@ -451,7 +451,7 @@ function test_revision_restore_caps_before_publish() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 2, $revisions ); + $this->assertCount( 3, $revisions ); foreach ( $revisions as $revision ) { $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) ); $this->assertFalse( current_user_can( 'edit_post', $revision->ID ) ); @@ -512,20 +512,25 @@ function test_revision_diff_caps_cpt() { function test_wp_get_post_revisions_should_order_by_post_date() { global $wpdb; + $now = time(); + $date = date( 'Y-m-d H:i:s', $now ); $post = self::factory()->post->create_and_get( array( - 'post_title' => 'some-post', - 'post_type' => 'post', - 'post_content' => 'some_content', + 'post_title' => 'some-post', + 'post_type' => 'post', + 'post_content' => 'some_content', + 'post_date' => $date, + 'post_date_gmt' => $date ) ); $post = (array) $post; + $revision_ids = array(); + $revisions = wp_get_post_revisions( $post['ID'] ); + $revision_ids[] = current( $revisions )->ID; $post_revision_fields = _wp_post_revision_data( $post ); $post_revision_fields = wp_slash( $post_revision_fields ); - $revision_ids = array(); - $now = time(); for ( $j = 1; $j < 3; $j++ ) { // Manually modify dates to ensure they're different. $date = gmdate( 'Y-m-d H:i:s', $now - ( $j * 10 ) ); @@ -546,20 +551,24 @@ function test_wp_get_post_revisions_should_order_by_post_date() { * @ticket 26042 */ function test_wp_get_post_revisions_should_order_by_ID_when_post_date_matches() { - $post = self::factory()->post->create_and_get( + $revision_ids = array(); + $date = date( 'Y-m-d H:i:s', time() - 10 ); + $post = self::factory()->post->create_and_get( array( - 'post_title' => 'some-post', - 'post_type' => 'post', - 'post_content' => 'some_content', + 'post_title' => 'some-post', + 'post_type' => 'post', + 'post_content' => 'some_content', + 'post_date' => $date, + 'post_date_gmt' => $date ) ); $post = (array) $post; $post_revision_fields = _wp_post_revision_data( $post ); $post_revision_fields = wp_slash( $post_revision_fields ); + $revisions = wp_get_post_revisions( $post['ID'] ); + $revision_ids[] = current( $revisions )->ID; - $revision_ids = array(); - $date = gmdate( 'Y-m-d H:i:s', time() - 10 ); for ( $j = 1; $j < 3; $j++ ) { // Manually modify dates to ensure they're the same. $post_revision_fields['post_date'] = $date; @@ -635,4 +644,30 @@ function () use ( $expected ) { ); $this->assertSame( $expected, wp_revisions_to_keep( $post ) ); } + + /** + * @ticket 30854 + */ + function test_wp_first_revision_is_not_lost() { + $post = self::factory()->post->create_and_get( array( + 'post_title' => 'some-post', + 'post_type' => 'post', + 'post_content' => 'Initial Content', + ) ); + + wp_update_post( array( + 'ID' => $post->ID, + 'post_content' => 'Update #1', + ) ); + + wp_update_post( array( + 'ID' => $post->ID, + 'post_content' => 'Update #2', + ) ); + + $revisions = wp_get_post_revisions( $post->ID ); + $earliest_revision = end( $revisions ); + + $this->assertEquals( 'Initial Content', $earliest_revision->post_content ); + } } From 0ffb5cb2a42ae66c2374b55aff31cc3961942dd5 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 9 Nov 2021 15:00:58 -0700 Subject: [PATCH 2/8] phpcs fixes --- tests/phpunit/tests/post/revisions.php | 38 +++++++++++++++----------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 4b43e8b380681..59c353d516a04 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -659,23 +659,29 @@ public function test_wp_save_post_revision_error() { * @ticket 30854 */ function test_wp_first_revision_is_not_lost() { - $post = self::factory()->post->create_and_get( array( - 'post_title' => 'some-post', - 'post_type' => 'post', - 'post_content' => 'Initial Content', - ) ); - - wp_update_post( array( - 'ID' => $post->ID, - 'post_content' => 'Update #1', - ) ); - - wp_update_post( array( - 'ID' => $post->ID, - 'post_content' => 'Update #2', - ) ); + $post = self::factory()->post->create_and_get( + array( + 'post_title' => 'some-post', + 'post_type' => 'post', + 'post_content' => 'Initial Content', + ) + ); - $revisions = wp_get_post_revisions( $post->ID ); + wp_update_post( + array( + 'ID' => $post->ID, + 'post_content' => 'Update #1', + ) + ); + + wp_update_post( + array( + 'ID' => $post->ID, + 'post_content' => 'Update #2', + ) + ); + + $revisions = wp_get_post_revisions( $post->ID ); $earliest_revision = end( $revisions ); $this->assertEquals( 'Initial Content', $earliest_revision->post_content ); From 7929ba686989605b62ffd6ca47c57cd449effaf2 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 9 Nov 2021 15:13:22 -0700 Subject: [PATCH 3/8] Add doc block indicating source of hook. --- src/wp-includes/default-filters.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 9db8eef968356..9c65467080d29 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -401,6 +401,7 @@ add_action( 'shutdown', 'wp_ob_end_flush_all', 1 ); // Create a revision whenever a post is updated. add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); +// Fired in `wp_transition_post_status` for newly created posts with status 'publish'. add_action( 'new_to_publish', 'wp_save_post_revision', 10, 1 ); add_action( 'publish_post', '_publish_post_hook', 5, 1 ); add_action( 'transition_post_status', '_transition_post_status', 5, 3 ); From df45e705b87f48eba40bba32c0b71aaef6c7cc2f Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 9 Nov 2021 16:09:53 -0700 Subject: [PATCH 4/8] Adjust some failing unit tests. --- tests/phpunit/tests/post/revisions.php | 4 ++++ tests/phpunit/tests/xmlrpc/wp/getRevisions.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 59c353d516a04..0cb0c6bffd160 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -539,6 +539,8 @@ public function test_wp_get_post_revisions_should_order_by_post_date() { $revisions = wp_get_post_revisions( $post['ID'] ); + array_unshift( $revision_ids, reset( $revisions )->ID ); + $this->assertSame( $revision_ids, array_values( wp_list_pluck( $revisions, 'ID' ) ) ); } @@ -574,6 +576,8 @@ public function test_wp_get_post_revisions_should_order_by_ID_when_post_date_mat $revisions = wp_get_post_revisions( $post['ID'] ); + array_unshift( $revision_ids, reset( $revisions )->ID ); + $this->assertSame( $revision_ids, array_values( wp_list_pluck( $revisions, 'ID' ) ) ); } diff --git a/tests/phpunit/tests/xmlrpc/wp/getRevisions.php b/tests/phpunit/tests/xmlrpc/wp/getRevisions.php index b7a4667165c4c..03814bc7bf1fa 100644 --- a/tests/phpunit/tests/xmlrpc/wp/getRevisions.php +++ b/tests/phpunit/tests/xmlrpc/wp/getRevisions.php @@ -42,7 +42,7 @@ public function test_revision_count() { $result = $this->myxmlrpcserver->wp_getRevisions( array( 1, 'editor', 'editor', $post_id ) ); $this->assertIsArray( $result ); - $this->assertCount( 1, $result ); + $this->assertCount( 2, $result ); wp_insert_post( array( From 993d8745c0c9da6139af6a874dc612be3210e808 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 9 Nov 2021 16:33:31 -0700 Subject: [PATCH 5/8] Fix up some addiitonal unit tests that now have an initial revision --- tests/phpunit/tests/post/revisions.php | 12 ++++++------ tests/phpunit/tests/xmlrpc/wp/getRevisions.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 0cb0c6bffd160..39ce7380b8b65 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -229,7 +229,7 @@ public function test_revision_view_caps_post() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) ); foreach ( $revisions as $revision ) { @@ -262,7 +262,7 @@ public function test_revision_restore_caps_post() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) ); } @@ -300,7 +300,7 @@ public function test_revision_diff_caps_post() { // Diff checks if you can read both left and right revisions. $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 2, $revisions ); + $this->assertCount( 3, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) ); } @@ -340,7 +340,7 @@ public function test_revision_view_caps_cpt() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) ); foreach ( $revisions as $revision ) { @@ -387,7 +387,7 @@ public function test_revision_restore_caps_cpt() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) ); } @@ -495,7 +495,7 @@ public function test_revision_diff_caps_cpt() { // Diff checks if you can read both left and right revisions. $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 2, $revisions ); + $this->assertCount( 3, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) ); } diff --git a/tests/phpunit/tests/xmlrpc/wp/getRevisions.php b/tests/phpunit/tests/xmlrpc/wp/getRevisions.php index 03814bc7bf1fa..6f4c80f74c1c6 100644 --- a/tests/phpunit/tests/xmlrpc/wp/getRevisions.php +++ b/tests/phpunit/tests/xmlrpc/wp/getRevisions.php @@ -53,7 +53,7 @@ public function test_revision_count() { $result = $this->myxmlrpcserver->wp_getRevisions( array( 1, 'editor', 'editor', $post_id ) ); $this->assertIsArray( $result ); - $this->assertCount( 2, $result ); + $this->assertCount( 3, $result ); } /** From e77d6e518475e632ac73779c5e3ef7420d3d2239 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Wed, 17 Nov 2021 10:35:45 +1100 Subject: [PATCH 6/8] Use assertSame. --- tests/phpunit/tests/post/revisions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 0b127b6d8fea1..2852d70da04ce 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -688,7 +688,7 @@ function test_wp_first_revision_is_not_lost() { $revisions = wp_get_post_revisions( $post->ID ); $earliest_revision = end( $revisions ); - $this->assertEquals( 'Initial Content', $earliest_revision->post_content ); + $this->assertSame( 'Initial Content', $earliest_revision->post_content ); } /** From cad91fc126472642061444c51b42db836c989f91 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Wed, 17 Nov 2021 10:57:12 +1100 Subject: [PATCH 7/8] Supress first revision by creating posts as draft. --- tests/phpunit/tests/post/revisions.php | 31 ++++++++++++++++++++------ 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 2852d70da04ce..7422a5c2581f7 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -705,7 +705,12 @@ function test_wp_first_revision_is_not_lost() { public function test_wp_get_post_revisions_url( $revisions ) { wp_set_current_user( self::$admin_user_id ); - $post_id = self::factory()->post->create( array( 'post_title' => 'Some Post' ) ); + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Some Post', + 'post_status' => 'draft', + ) + ); $latest_revision_id = null; if ( 0 !== $revisions ) { @@ -714,8 +719,9 @@ public function test_wp_get_post_revisions_url( $revisions ) { for ( $i = 0; $i < $revisions; ++$i ) { wp_update_post( array( - 'ID' => $post_id, - 'post_title' => 'Some Post ' . $i, + 'ID' => $post_id, + 'post_title' => 'Some Post ' . $i, + 'post_status' => 'publish', ) ); @@ -753,7 +759,12 @@ public function test_wp_get_post_revisions_url( $revisions ) { public function test_wp_get_post_revisions_url_with_post_object( $revisions ) { wp_set_current_user( self::$admin_user_id ); - $post = self::factory()->post->create_and_get( array( 'post_title' => 'Some Post' ) ); + $post = self::factory()->post->create_and_get( + array( + 'post_title' => 'Some Post', + 'post_status' => 'draft', + ) + ); $latest_revision_id = null; if ( 0 !== $revisions ) { @@ -762,8 +773,9 @@ public function test_wp_get_post_revisions_url_with_post_object( $revisions ) { for ( $i = 0; $i < $revisions; ++$i ) { wp_update_post( array( - 'ID' => $post->ID, - 'post_title' => 'Some Post ' . $i, + 'ID' => $post->ID, + 'post_title' => 'Some Post ' . $i, + 'post_status' => 'publish', ) ); @@ -820,7 +832,12 @@ public function test_wp_get_post_revisions_url_returns_null_when_post_does_not_e */ public function test_wp_get_post_revisions_url_returns_null_with_no_revisions() { wp_set_current_user( self::$admin_user_id ); - $post_id = self::factory()->post->create( array( 'post_title' => 'Some Post' ) ); + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Some Post', + 'post_status' => 'draft', + ) + ); $this->assertNull( wp_get_post_revisions_url( $post_id ) ); } From 9de66ec1d153c66a806f285635ee63f0f4aaf903 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sat, 7 Nov 2026 20:10:32 -0500 Subject: [PATCH 8/8] Declare visibility on the first revision test method --- tests/phpunit/tests/post/revisions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 5a133b0001bf6..174558fbce573 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -712,7 +712,7 @@ public function test_wp_get_latest_revision_id_and_total_count_no_revisions() { /** * @ticket 30854 */ - function test_wp_first_revision_is_not_lost() { + public function test_wp_first_revision_is_not_lost() { $post = self::factory()->post->create_and_get( array( 'post_title' => 'some-post',