diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index 179a32c2832c5..db38ffdc79785 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -102,15 +102,11 @@ function _wp_post_revision_data( $post = array(), $autosave = false ) { * * @since 6.4.0 * - * @param int $post_id The post id that was inserted. - * @param WP_Post $post The post object that was inserted. + * @param int $post_id The post ID that was inserted or updated. + * @param WP_Post $post The post object that was inserted or updated. * @param bool $update Whether this insert is updating an existing post. */ function wp_save_post_revision_on_insert( $post_id, $post, $update ) { - if ( ! $update ) { - return; - } - if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) { return; } @@ -216,7 +212,16 @@ function wp_save_post_revision( $post_id ) { } } - $return = _wp_put_post_revision( $post ); + /* + * With no current user (eg. imports or cron), credit the first revision + * to the post author instead of user 0. + */ + $revision_author = null; + if ( ! $revisions && 0 === get_current_user_id() ) { + $revision_author = (int) $post->post_author; + } + + $return = _wp_put_post_revision( $post, false, $revision_author ); /* * If a limit for the number of revisions to keep has been set, @@ -348,14 +353,16 @@ function wp_is_post_autosave( $post ) { * Inserts post data into the posts table as a post revision. * * @since 2.6.0 + * @since 7.3.0 The `$post_author` parameter was added. * @access private * - * @param int|WP_Post|array|null $post Post ID, post object OR post array. - * @param bool $autosave Optional. Whether the revision is an autosave or not. - * Default false. + * @param int|WP_Post|array|null $post Post ID, post object OR post array. + * @param bool $autosave Optional. Whether the revision is an autosave or not. + * Default false. + * @param int|null $post_author Optional. The revision author ID. Default null. * @return int|WP_Error WP_Error or 0 if error, new revision ID if success. */ -function _wp_put_post_revision( $post = null, $autosave = false ) { +function _wp_put_post_revision( $post = null, $autosave = false, $post_author = null ) { if ( is_object( $post ) ) { $post = get_object_vars( $post ); } elseif ( ! is_array( $post ) ) { @@ -371,6 +378,11 @@ function _wp_put_post_revision( $post = null, $autosave = false ) { } $post = _wp_post_revision_data( $post, $autosave ); + + if ( null !== $post_author ) { + $post['post_author'] = absint( $post_author ); + } + $post = wp_slash( $post ); // Since data is from DB. $revision_id = wp_insert_post( $post, true ); diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index e27f3e15003d5..0487336ea59af 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -2149,16 +2149,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. - $revisions = wp_get_latest_revision_id_and_total_count( $r ); - if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) { - 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/metaRevisions.php b/tests/phpunit/tests/post/metaRevisions.php index f7b64bddedd7c..c95beaa0fecae 100644 --- a/tests/phpunit/tests/post/metaRevisions.php +++ b/tests/phpunit/tests/post/metaRevisions.php @@ -38,7 +38,7 @@ public function test_revisions_stores_meta_values_with_slashes( $passed, $expect // Set up a new post. $post_id = self::factory()->post->create(); - // And update to store an initial revision. + // And update to store another revision. wp_update_post( array( 'post_content' => 'some initial content', @@ -120,7 +120,7 @@ public function test_revisions_stores_meta_values() { $post_id = self::factory()->post->create(); $original_post_id = $post_id; - // And update to store an initial revision. + // And update to store another revision. wp_update_post( array( 'post_content' => 'some initial content', @@ -128,9 +128,9 @@ public function test_revisions_stores_meta_values() { ) ); - // One revision so far. + // Two revisions so far, including the initial revision. $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); /* * First set up a meta value. @@ -148,7 +148,7 @@ public function test_revisions_stores_meta_values() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 2, $revisions ); + $this->assertCount( 3, $revisions ); // Next, store some updated meta values for the same key. update_post_meta( $post_id, 'meta_revision_test', 'update1' ); @@ -162,7 +162,7 @@ public function test_revisions_stores_meta_values() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 3, $revisions ); + $this->assertCount( 4, $revisions ); /* * Now restore the original revision. @@ -180,7 +180,7 @@ public function test_revisions_stores_meta_values() { wp_update_post( array( 'ID' => $post_id ) ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 4, $revisions ); + $this->assertCount( 5, $revisions ); /* * Check the meta values to verify they are NOT revisioned - they are not revisioned by default. @@ -207,7 +207,7 @@ public function test_revisions_stores_meta_values() { ); $revisions = array_values( wp_get_post_revisions( $post_id ) ); - $this->assertCount( 5, $revisions ); + $this->assertCount( 6, $revisions ); $this->assertSame( 'update2', get_post_meta( $revisions[0]->ID, 'meta_revision_test', true ) ); // Store custom meta values, which should now be revisioned. @@ -227,7 +227,7 @@ public function test_revisions_stores_meta_values() { // This revision contains the existing post meta ('update3'). $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 6, $revisions ); + $this->assertCount( 7, $revisions ); // Verify that previous post meta is set. $this->assertSame( 'update3', get_post_meta( $post_id, 'meta_revision_test', true ) ); diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index c208ccfd37c05..2c75bbe6e9801 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -227,7 +227,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 ) ); $this->assertNotEmpty( $revisions ); @@ -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 ) ); } @@ -436,7 +436,7 @@ public function test_revision_restore_caps_before_publish() { ); $revisions = wp_get_post_revisions( $post_id ); - $this->assertCount( 1, $revisions ); + $this->assertCount( 2, $revisions ); foreach ( $revisions as $revision ) { $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) ); $this->assertTrue( current_user_can( 'edit_post', $revision->ID ) ); @@ -451,7 +451,7 @@ public 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 ) ); @@ -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 ) ); } @@ -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' ) ) ); } @@ -650,6 +654,149 @@ public function test_wp_save_post_revision_error() { $this->assertSame( 'invalid_post', $revision->get_error_code() ); } + /** + * Tests that wp_insert_post() saves the initial revision. + * + * @ticket 30854 + */ + public function test_wp_insert_post_saves_initial_revision() { + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Initial title', + 'post_content' => 'Initial content', + 'post_excerpt' => 'Initial excerpt', + 'post_status' => 'publish', + ) + ); + + $revisions = wp_get_post_revisions( $post_id ); + + $this->assertCount( 1, $revisions, 'Initial revision should be created.' ); + $this->assertContains( + 'Initial content', + wp_list_pluck( $revisions, 'post_content' ), + 'Initial revision should preserve the inserted content.' + ); + } + + /** + * Tests that wp_insert_post() saves the initial revision for custom post statuses. + * + * @ticket 30854 + */ + public function test_wp_insert_post_saves_initial_revision_for_custom_post_status() { + global $wp_post_statuses; + + register_post_status( 'test-revision-status' ); + + try { + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Initial title', + 'post_content' => 'Initial content', + 'post_status' => 'test-revision-status', + ) + ); + } finally { + unset( $wp_post_statuses['test-revision-status'] ); + } + + $revisions = wp_get_post_revisions( $post_id ); + + $this->assertCount( 1, $revisions, 'Initial revision should be created.' ); + } + + /** + * Tests the author for initial revisions. + * + * @ticket 30854 + */ + public function test_wp_insert_post_saves_initial_revision_author() { + $old_user_id = get_current_user_id(); + + wp_set_current_user( 0 ); + $no_current_user_post_id = self::factory()->post->create( + array( + 'post_author' => self::$author_user_id, + 'post_title' => 'Initial title', + 'post_content' => 'Initial content', + 'post_status' => 'publish', + ) + ); + $no_current_user_revisions = wp_get_post_revisions( $no_current_user_post_id ); + $no_current_user_revision = reset( $no_current_user_revisions ); + + wp_set_current_user( self::$admin_user_id ); + $current_user_post_id = self::factory()->post->create( + array( + 'post_author' => self::$author_user_id, + 'post_title' => 'Initial title', + 'post_content' => 'Initial content', + 'post_status' => 'publish', + ) + ); + $current_user_revisions = wp_get_post_revisions( $current_user_post_id ); + $current_user_revision = reset( $current_user_revisions ); + + wp_set_current_user( $old_user_id ); + + $this->assertSame( + self::$author_user_id, + (int) $no_current_user_revision->post_author, + 'Initial revision author should match the post author when there is no current user.' + ); + $this->assertSame( + self::$admin_user_id, + (int) $current_user_revision->post_author, + 'Initial revision author should match the current user.' + ); + } + + /** + * Tests that wp_insert_post() does not save initial revisions when revisions should not be saved. + * + * @ticket 30854 + */ + public function test_wp_insert_post_does_not_save_initial_revision_when_revisions_should_not_be_saved() { + $auto_draft_post_id = self::factory()->post->create( + array( + 'post_title' => 'Auto draft title', + 'post_content' => 'Auto draft content', + 'post_status' => 'auto-draft', + ) + ); + + remove_post_type_support( 'post', 'revisions' ); + try { + $unsupported_post_id = self::factory()->post->create( + array( + 'post_title' => 'No revision title', + 'post_content' => 'No revision content', + 'post_status' => 'publish', + ) + ); + } finally { + add_post_type_support( 'post', 'revisions' ); + } + + remove_action( 'post_updated', 'wp_save_post_revision', 10 ); + try { + $action_removed_post_id = self::factory()->post->create( + array( + 'post_title' => 'No revision title', + 'post_content' => 'No revision content', + 'post_status' => 'publish', + ) + ); + } finally { + add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); + } + + $this->assertCount( 0, wp_get_post_revisions( $auto_draft_post_id ), 'Auto-drafts should not save initial revisions.' ); + $this->assertCount( 0, wp_get_post_revisions( $unsupported_post_id ), 'Post types without revision support should not save initial revisions.' ); + $this->assertCount( 0, wp_get_post_revisions( $action_removed_post_id ), 'Removing the default revision action should disable initial revisions.' ); + } + /** * Tests that wp_get_latest_revision_id_and_total_count() returns the latest revision ID and total count. * @@ -719,25 +866,20 @@ public function test_wp_get_latest_revision_id_and_total_count_no_revisions() { 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' ) ); - $latest_revision_id = null; - - if ( 0 !== $revisions ) { - $latest_revision_id = $post_id; - - for ( $i = 0; $i < $revisions; ++$i ) { - wp_update_post( - array( - 'ID' => $post_id, - 'post_title' => 'Some Post ' . $i, - ) - ); + $post_id = self::factory()->post->create( array( 'post_title' => 'Some Post' ) ); - ++$latest_revision_id; - } + for ( $i = 0; $i < $revisions; ++$i ) { + wp_update_post( + array( + 'ID' => $post_id, + 'post_title' => 'Some Post ' . $i, + ) + ); } - $expected = admin_url( 'revision.php?revision=' . $latest_revision_id ); + $post_revisions = wp_get_post_revisions( $post_id ); + $latest_revision = reset( $post_revisions ); + $expected = admin_url( 'revision.php?revision=' . $latest_revision->ID ); $this->assertSame( $expected, @@ -747,7 +889,7 @@ public function test_wp_get_post_revisions_url( $revisions ) { $this->assertSame( $expected, - wp_get_post_revisions_url( $latest_revision_id ), + wp_get_post_revisions_url( $latest_revision->ID ), 'Failed when passed the latest revision ID' ); } @@ -767,25 +909,20 @@ 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' ) ); - $latest_revision_id = null; + $post = self::factory()->post->create_and_get( array( 'post_title' => 'Some Post' ) ); - if ( 0 !== $revisions ) { - $latest_revision_id = $post->ID; - - for ( $i = 0; $i < $revisions; ++$i ) { - wp_update_post( - array( - 'ID' => $post->ID, - 'post_title' => 'Some Post ' . $i, - ) - ); - - ++$latest_revision_id; - } + for ( $i = 0; $i < $revisions; ++$i ) { + wp_update_post( + array( + 'ID' => $post->ID, + 'post_title' => 'Some Post ' . $i, + ) + ); } - $expected = admin_url( 'revision.php?revision=' . $latest_revision_id ); + $post_revisions = wp_get_post_revisions( $post->ID ); + $latest_revision = reset( $post_revisions ); + $expected = admin_url( 'revision.php?revision=' . $latest_revision->ID ); $this->assertSame( $expected, @@ -795,7 +932,7 @@ public function test_wp_get_post_revisions_url_with_post_object( $revisions ) { $this->assertSame( $expected, - wp_get_post_revisions_url( $latest_revision_id ), + wp_get_post_revisions_url( $latest_revision->ID ), 'Failed when passed the latest revision ID' ); } @@ -834,7 +971,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' => 'auto-draft', + ) + ); $this->assertNull( wp_get_post_revisions_url( $post_id ) ); } @@ -920,7 +1062,7 @@ static function ( $revisions ) { ); $this->assertSame( - 'Test 57320 Update 1', + 'Test 57320', $second->post_title, 'The title of the second revision was incorrect.' ); @@ -934,7 +1076,10 @@ public function test_wp_save_post_revision_with_array_post_meta() { // This filter is true by default, but this is explicitly to test looking for differences among non-scalar fields. add_filter( 'wp_save_post_revision_check_for_changes', '__return_true' ); - $post_id = self::factory()->post->create(); + remove_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert', 9 ); + $post_id = self::factory()->post->create(); + add_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert', 9, 3 ); + $meta_key = 'favorite_things'; // Ensure the post meta is saved with each revision. diff --git a/tests/phpunit/tests/post/wpDeletePost.php b/tests/phpunit/tests/post/wpDeletePost.php index 3c2a40fa92bec..44e80089370a0 100644 --- a/tests/phpunit/tests/post/wpDeletePost.php +++ b/tests/phpunit/tests/post/wpDeletePost.php @@ -127,7 +127,13 @@ static function () use ( $action, &$captured_action_args ) { ); } - $post_id = self::factory()->post->create(); + remove_post_type_support( 'post', 'revisions' ); + try { + $post_id = self::factory()->post->create(); + } finally { + add_post_type_support( 'post', 'revisions' ); + } + $deleted_post = wp_delete_post( (string) $post_id, true ); $this->assertInstanceOf( WP_Post::class, $deleted_post ); diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php index 469a5203f9a7e..6f9190364a439 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php @@ -211,17 +211,17 @@ public static function wpTearDownAfterClass() { public function set_up() { parent::set_up(); switch_theme( 'tt1-blocks' ); - $revisions = wp_get_post_revisions( self::$global_styles_id ); + $revisions = array_values( wp_get_post_revisions( self::$global_styles_id ) ); $this->total_revisions = count( $revisions ); - $this->revision_1 = array_pop( $revisions ); - $this->revision_1_id = $this->revision_1->ID; + $this->revision_3 = $revisions[0]; + $this->revision_3_id = $this->revision_3->ID; - $this->revision_2 = array_pop( $revisions ); + $this->revision_2 = $revisions[1]; $this->revision_2_id = $this->revision_2->ID; - $this->revision_3 = array_pop( $revisions ); - $this->revision_3_id = $this->revision_3->ID; + $this->revision_1 = $revisions[2]; + $this->revision_1_id = $this->revision_1->ID; } /** @@ -961,8 +961,8 @@ public function test_get_global_styles_revisions_pagination() { $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 1, $data ); - $this->assertSame( 3, $response->get_headers()['X-WP-Total'] ); - $this->assertSame( 3, $response->get_headers()['X-WP-TotalPages'] ); + $this->assertSame( 4, $response->get_headers()['X-WP-Total'] ); + $this->assertSame( 4, $response->get_headers()['X-WP-TotalPages'] ); // Test paged. $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' ); @@ -971,8 +971,8 @@ public function test_get_global_styles_revisions_pagination() { $response = rest_get_server()->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertCount( 1, $data ); - $this->assertSame( 3, $response->get_headers()['X-WP-Total'] ); + $this->assertCount( 2, $data ); + $this->assertSame( 4, $response->get_headers()['X-WP-Total'] ); $this->assertSame( 2, $response->get_headers()['X-WP-TotalPages'] ); // Test out of bounds. @@ -1347,7 +1347,7 @@ public function test_get_items_preserves_block_style_variations() { $data = $response->get_data(); $this->assertSame( 200, $response->get_status(), 'Response status should be 200.' ); - $this->assertCount( 2, $data, 'Should have 2 revisions.' ); + $this->assertCount( 3, $data, 'Should have 3 revisions.' ); // Verify first revision (most recent - orange). $this->assertArrayHasKey( diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 7f4f09d78b308..46653685e4688 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -2262,9 +2262,12 @@ public function test_get_item_links() { $replies_url = add_query_arg( 'post', self::$post_id, $replies_url ); $this->assertSame( $replies_url, $links['replies'][0]['href'] ); + $revisions = wp_get_latest_revision_id_and_total_count( self::$post_id ); + $this->assertSame( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] ); - $this->assertSame( 0, $links['version-history'][0]['attributes']['count'] ); - $this->assertArrayNotHasKey( 'predecessor-version', $links ); + $this->assertSame( 1, $links['version-history'][0]['attributes']['count'] ); + $this->assertSame( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions/' . $revisions['latest_id'] ), $links['predecessor-version'][0]['href'] ); + $this->assertSame( $revisions['latest_id'], $links['predecessor-version'][0]['attributes']['id'] ); $attachments_url = rest_url( '/wp/v2/media' ); $attachments_url = add_query_arg( 'parent', self::$post_id, $attachments_url ); @@ -2301,8 +2304,8 @@ public function test_get_item_links_predecessor() { 'ID' => self::$post_id, ) ); - $revisions = wp_get_post_revisions( self::$post_id ); - $revision_1 = array_pop( $revisions ); + $revisions = wp_get_post_revisions( self::$post_id ); + $latest_revision = reset( $revisions ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = rest_get_server()->dispatch( $request ); @@ -2310,10 +2313,10 @@ public function test_get_item_links_predecessor() { $links = $response->get_links(); $this->assertSame( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] ); - $this->assertSame( 1, $links['version-history'][0]['attributes']['count'] ); + $this->assertSame( 2, $links['version-history'][0]['attributes']['count'] ); - $this->assertSame( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions/' . $revision_1->ID ), $links['predecessor-version'][0]['href'] ); - $this->assertSame( $revision_1->ID, $links['predecessor-version'][0]['attributes']['id'] ); + $this->assertSame( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions/' . $latest_revision->ID ), $links['predecessor-version'][0]['href'] ); + $this->assertSame( $latest_revision->ID, $links['predecessor-version'][0]['attributes']['id'] ); } public function test_get_item_links_no_author() { diff --git a/tests/phpunit/tests/rest-api/rest-revisions-controller.php b/tests/phpunit/tests/rest-api/rest-revisions-controller.php index fcf6b69393865..c2e7eca523d70 100644 --- a/tests/phpunit/tests/rest-api/rest-revisions-controller.php +++ b/tests/phpunit/tests/rest-api/rest-revisions-controller.php @@ -23,6 +23,8 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase private int $revision_id2; private WP_Post $revision_3; private int $revision_id3; + private WP_Post $revision_initial; + private int $revision_initial_id; private int $revision_2_1_id; public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { @@ -89,14 +91,16 @@ public function set_up() { parent::set_up(); // Set first post revision vars. - $revisions = wp_get_post_revisions( self::$post_id ); - $this->total_revisions = count( $revisions ); - $this->revision_1 = array_pop( $revisions ); - $this->revision_id1 = $this->revision_1->ID; - $this->revision_2 = array_pop( $revisions ); - $this->revision_id2 = $this->revision_2->ID; - $this->revision_3 = array_pop( $revisions ); - $this->revision_id3 = $this->revision_3->ID; + $revisions = array_values( wp_get_post_revisions( self::$post_id ) ); + $this->total_revisions = count( $revisions ); + $this->revision_3 = $revisions[0]; + $this->revision_id3 = $this->revision_3->ID; + $this->revision_2 = $revisions[1]; + $this->revision_id2 = $this->revision_2->ID; + $this->revision_1 = $revisions[2]; + $this->revision_id1 = $this->revision_1->ID; + $this->revision_initial = $revisions[3]; + $this->revision_initial_id = $this->revision_initial->ID; // Set second post revision vars. $revisions = wp_get_post_revisions( self::$post_id_2 ); @@ -159,6 +163,9 @@ public function test_get_items() { $this->assertSame( $this->revision_id1, $data[2]['id'] ); $this->check_get_revision_response( $data[2], $this->revision_1 ); + + $this->assertSame( $this->revision_initial_id, $data[3]['id'] ); + $this->check_get_revision_response( $data[3], $this->revision_initial ); } /** @@ -302,7 +309,7 @@ public function test_get_items_restores_global_post() { // The filter is passed each revision, not the global post restored afterwards. $this->check_rest_prepare_revision_filter_args( $mock, - array( $this->revision_id3, $this->revision_id2, $this->revision_id1 ), + array( $this->revision_id3, $this->revision_id2, $this->revision_id1, $this->revision_initial_id ), $request ); @@ -390,7 +397,7 @@ public function test_get_items_without_global_post_leaves_it_unset() { // The filter is passed each revision, even though there is no global post to restore. $this->check_rest_prepare_revision_filter_args( $mock, - array( $this->revision_id3, $this->revision_id2, $this->revision_id1 ), + array( $this->revision_id3, $this->revision_id2, $this->revision_id1, $this->revision_initial_id ), $request ); @@ -1394,7 +1401,7 @@ public function test_get_revisions_pagination() { $response = rest_get_server()->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertCount( 1, $data ); + $this->assertCount( 2, $data ); $this->assertSame( $this->total_revisions, $response->get_headers()['X-WP-Total'] ); $this->assertSame( (int) ceil( $this->total_revisions / 2 ), $response->get_headers()['X-WP-TotalPages'] ); @@ -1421,7 +1428,7 @@ public function test_get_item_meta_only_includes_revisioned_meta_keys() { $this->register_revisioned_and_non_revisioned_meta(); $post_id = $this->create_post_with_revisioned_meta( 'revisioned value' ); - $revision_id = $this->get_oldest_revision_id( $post_id ); + $revision_id = $this->get_first_update_revision_id( $post_id ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id . '/revisions/' . $revision_id ); @@ -1453,7 +1460,7 @@ public function test_get_items_meta_only_includes_revisioned_meta_keys() { $data = $response->get_data(); $this->assertSame( 200, $response->get_status() ); - $this->assertCount( 2, $data ); + $this->assertCount( 3, $data ); foreach ( $data as $revision ) { $this->assertArrayHasKey( 'revisioned_meta', $revision['meta'], 'Revisioned meta should be included.' ); @@ -1508,7 +1515,7 @@ static function ( $keys ) { 'post_content' => 'Revised content.', ) ); - $revision_id = $this->get_oldest_revision_id( $post_id ); + $revision_id = $this->get_first_update_revision_id( $post_id ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id . '/revisions/' . $revision_id ); @@ -1549,7 +1556,7 @@ public function test_get_item_meta_uses_rest_name_of_revisioned_meta_keys() { ); $post_id = $this->create_post_with_revisioned_meta( 'revisioned value' ); - $revision_id = $this->get_oldest_revision_id( $post_id ); + $revision_id = $this->get_first_update_revision_id( $post_id ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id . '/revisions/' . $revision_id ); @@ -1580,7 +1587,7 @@ public function test_restoring_revision_keeps_meta_that_is_not_revisioned() { $post_id = $this->create_post_with_revisioned_meta( 'old value' ); update_post_meta( $post_id, 'revisioned_meta', 'new value' ); update_post_meta( $post_id, 'non_revisioned_meta', 'kept value' ); - $revision_id = $this->get_oldest_revision_id( $post_id ); + $revision_id = $this->get_first_update_revision_id( $post_id ); wp_set_current_user( self::$editor_id ); $response = $this->restore_revision_like_the_block_editor( '/wp/v2/posts/' . $post_id, $revision_id ); @@ -1629,7 +1636,7 @@ public function test_restoring_pattern_revision_keeps_sync_status( $sync_status 'post_content' => 'Latest content.', ) ); - $revision_id = $this->get_oldest_revision_id( $pattern_id ); + $revision_id = $this->get_first_update_revision_id( $pattern_id ); wp_set_current_user( self::$editor_id ); $response = $this->restore_revision_like_the_block_editor( '/wp/v2/blocks/' . $pattern_id, $revision_id ); @@ -1663,7 +1670,7 @@ public function test_get_item_with_non_array_revision_meta_keys_filter() { $this->register_revisioned_and_non_revisioned_meta(); $post_id = $this->create_post_with_revisioned_meta( 'revisioned value' ); - $revision_id = $this->get_oldest_revision_id( $post_id ); + $revision_id = $this->get_first_update_revision_id( $post_id ); add_filter( 'wp_post_revision_meta_keys', '__return_false' ); @@ -1728,15 +1735,17 @@ private function create_post_with_revisioned_meta( $value ) { } /** - * Gets the ID of a post's oldest revision. + * Gets the ID of the revision saved by a post's first update. + * + * Skips the initial revision saved when the post was inserted. * * @param int $post_id Post ID. * @return int Revision ID. */ - private function get_oldest_revision_id( $post_id ) { - $revisions = wp_get_post_revisions( $post_id ); + private function get_first_update_revision_id( $post_id ) { + $revisions = array_values( wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ) ); - return end( $revisions )->ID; + return $revisions[1]->ID; } /** diff --git a/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php b/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php index 3475c87d68750..0332d617061a5 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php @@ -365,54 +365,31 @@ public function test_get_items_with_data_provider( $parent_post_property_name, $ $this->assertSame( WP_Http::OK, $response->get_status(), 'Response is expected to have a status code of 200.' ); $this->assertCount( - 4, + 5, $revisions, - 'Failed asserting that the response data contains exactly 4 items.' + 'Failed asserting that the response data contains exactly 5 items.' ); - $this->assertSame( - $parent_post->ID, - $revisions[0]['parent'], - 'Failed asserting that the parent ID of the revision matches the template post ID.' - ); - $this->assertSame( + $expected_contents = array( 'Content revision #5', - $revisions[0]['content']['raw'], - 'Failed asserting that the content of the revision is "Content revision #5".' - ); - - $this->assertSame( - $parent_post->ID, - $revisions[1]['parent'], - 'Failed asserting that the parent ID of the revision matches the template post ID.' - ); - $this->assertSame( 'Content revision #4', - $revisions[1]['content']['raw'], - 'Failed asserting that the content of the revision is "Content revision #4".' - ); - - $this->assertSame( - $parent_post->ID, - $revisions[2]['parent'], - 'Failed asserting that the parent ID of the revision matches the template post ID.' - ); - $this->assertSame( 'Content revision #3', - $revisions[2]['content']['raw'], - 'Failed asserting that the content of the revision is "Content revision #3".' - ); - - $this->assertSame( - $parent_post->ID, - $revisions[3]['parent'], - 'Failed asserting that the parent ID of the revision matches the template post ID.' - ); - $this->assertSame( 'Content revision #2', - $revisions[3]['content']['raw'], - 'Failed asserting that the content of the revision is "Content revision #2".' + 'Content', ); + + foreach ( $expected_contents as $index => $expected_content ) { + $this->assertSame( + $parent_post->ID, + $revisions[ $index ]['parent'], + 'Failed asserting that the parent ID of the revision matches the template post ID.' + ); + $this->assertSame( + $expected_content, + $revisions[ $index ]['content']['raw'], + 'Failed asserting that the revision content matches the expected content.' + ); + } } /** @@ -1223,8 +1200,8 @@ public function test_get_template_revisions_pagination() { $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 1, $data ); - $this->assertSame( 4, $response->get_headers()['X-WP-Total'] ); - $this->assertSame( 4, $response->get_headers()['X-WP-TotalPages'] ); + $this->assertSame( 5, $response->get_headers()['X-WP-Total'] ); + $this->assertSame( 5, $response->get_headers()['X-WP-TotalPages'] ); // Test paged. $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' ); @@ -1234,8 +1211,8 @@ public function test_get_template_revisions_pagination() { $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 2, $data ); - $this->assertSame( 4, $response->get_headers()['X-WP-Total'] ); - $this->assertSame( 2, $response->get_headers()['X-WP-TotalPages'] ); + $this->assertSame( 5, $response->get_headers()['X-WP-Total'] ); + $this->assertSame( 3, $response->get_headers()['X-WP-TotalPages'] ); // Test out of bounds. $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' ); diff --git a/tests/phpunit/tests/xmlrpc/wp/getRevisions.php b/tests/phpunit/tests/xmlrpc/wp/getRevisions.php index 01d8614460d8d..1272f60564027 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( @@ -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 ); } /** diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 807eda9d2cf92..c764dba73ced0 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -13418,7 +13418,7 @@ mockedApiResponse.PostsCollection = [ ], "version-history": [ { - "count": 2, + "count": 3, "href": "http://example.org/index.php?rest_route=/wp/v2/posts/4/revisions" } ], @@ -13565,6 +13565,36 @@ mockedApiResponse.postRevisions = [ } ] } + }, + { + "author": 674, + "date": "2017-02-14T00:00:00", + "date_gmt": "2017-02-14T00:00:00", + "id": 4202885, + "modified": "2017-02-14T00:00:00", + "modified_gmt": "2017-02-14T00:00:00", + "parent": 4202884, + "slug": "4202884-revision-v1", + "guid": { + "rendered": "http://example.org/?p=4202885" + }, + "title": { + "rendered": "REST API Client Fixture: Post" + }, + "content": { + "rendered": "
REST API Client Fixture: Post
\n" + }, + "excerpt": { + "rendered": "REST API Client Fixture: Post
\n" + }, + "meta": [], + "_links": { + "parent": [ + { + "href": "http://example.org/index.php?rest_route=/wp/v2/posts/4202884" + } + ] + } } ]; @@ -13584,7 +13614,7 @@ mockedApiResponse.revision = { "rendered": "REST API Client Fixture: Post" }, "content": { - "rendered": "Updated post content.
\n" + "rendered": "REST API Client Fixture: Post
\n" }, "excerpt": { "rendered": "REST API Client Fixture: Post
\n" @@ -13724,7 +13754,7 @@ mockedApiResponse.PagesCollection = [ ], "version-history": [ { - "count": 2, + "count": 3, "href": "http://example.org/index.php?rest_route=/wp/v2/pages/7/revisions" } ], @@ -13853,6 +13883,36 @@ mockedApiResponse.pageRevisions = [ } ] } + }, + { + "author": 674, + "date": "2017-02-14T00:00:00", + "date_gmt": "2017-02-14T00:00:00", + "id": 4202889, + "modified": "2017-02-14T00:00:00", + "modified_gmt": "2017-02-14T00:00:00", + "parent": 4202888, + "slug": "4202888-revision-v1", + "guid": { + "rendered": "http://example.org/?p=4202889" + }, + "title": { + "rendered": "REST API Client Fixture: Page" + }, + "content": { + "rendered": "REST API Client Fixture: Page
\n" + }, + "excerpt": { + "rendered": "REST API Client Fixture: Page
\n" + }, + "meta": [], + "_links": { + "parent": [ + { + "href": "http://example.org/index.php?rest_route=/wp/v2/pages/4202888" + } + ] + } } ]; @@ -13872,7 +13932,7 @@ mockedApiResponse.pageRevision = { "rendered": "REST API Client Fixture: Page" }, "content": { - "rendered": "Updated page content.
\n" + "rendered": "REST API Client Fixture: Page
\n" }, "excerpt": { "rendered": "REST API Client Fixture: Page
\n"