diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index 11f2bba9d97ff..8c49c6e98dc52 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -100,20 +100,19 @@ 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; } - wp_save_post_revision( $post_id ); + wp_save_post_revision( + $post_id, + ! $update && 0 === get_current_user_id() ? $post->post_author : null + ); } /** @@ -123,11 +122,13 @@ function wp_save_post_revision_on_insert( $post_id, $post, $update ) { * and the most recent revision always matches the current post. * * @since 2.6.0 + * @since 7.1.0 The `$revision_author` parameter was added. * - * @param int $post_id The ID of the post to save as a revision. + * @param int $post_id The ID of the post to save as a revision. + * @param int|null $revision_author Optional. The revision author ID. Default null. * @return int|WP_Error|null Null or 0 if error, new revision ID, if success. */ -function wp_save_post_revision( $post_id ) { +function wp_save_post_revision( $post_id, $revision_author = null ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return null; } @@ -214,7 +215,7 @@ function wp_save_post_revision( $post_id ) { } } - $return = _wp_put_post_revision( $post ); + $return = _wp_put_post_revision( $post, false, $revision_author ); /* * If a limit for the number of revisions to keep has been set, @@ -344,14 +345,16 @@ function wp_is_post_autosave( $post ) { * Inserts post data into the posts table as a post revision. * * @since 2.6.0 + * @since 7.1.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 ) ) { @@ -367,6 +370,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 07869ae61d0ff..222af49a68161 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -2141,16 +2141,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 a95cc3a260a26..e6a881cd9ff6a 100644 --- a/tests/phpunit/tests/post/metaRevisions.php +++ b/tests/phpunit/tests/post/metaRevisions.php @@ -39,7 +39,7 @@ public function test_revisions_stores_meta_values_with_slashes( $passed, $expect // Set up a new post. $post_id = $this->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', @@ -121,7 +121,7 @@ public function test_revisions_stores_meta_values() { $post_id = $this->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', @@ -129,9 +129,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. @@ -149,7 +149,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' ); @@ -163,7 +163,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. @@ -181,7 +181,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. @@ -208,7 +208,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. @@ -228,7 +228,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 5b6e70b4605bf..86ec7520b4242 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' ) ) ); } @@ -655,6 +659,149 @@ public function test_wp_save_post_revision_error() { $this->assertWPError( $revision ); } + /** + * 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. * @@ -724,25 +871,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, @@ -752,7 +894,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' ); } @@ -772,25 +914,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, @@ -800,7 +937,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' ); } @@ -839,7 +976,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 ) ); } @@ -925,7 +1067,7 @@ static function ( $revisions ) { ); $this->assertSame( - 'Test 57320 Update 1', + 'Test 57320', $second->post_title, 'The title of the second revision was incorrect.' ); @@ -939,7 +1081,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 5382e496b7049..1b8e29f003d3d 100644 --- a/tests/phpunit/tests/post/wpDeletePost.php +++ b/tests/phpunit/tests/post/wpDeletePost.php @@ -159,7 +159,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 a715899979ad1..f84ee2c4c642c 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 212ddde70dd83..7b298ae1de657 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -2250,9 +2250,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 ); @@ -2289,8 +2292,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 ); @@ -2298,10 +2301,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 52011afcb9318..128e9e2a41954 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 $revision_id2; private $revision_3; private $revision_id3; + private $revision_initial; + private $revision_initial_id; private $revision_2_1_id; public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { @@ -89,15 +91,17 @@ 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->revisions = $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->revisions = $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 ); @@ -160,6 +164,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 ); } /** @@ -1070,7 +1077,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'] ); diff --git a/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php b/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php index e8a18b275e7cd..957ec7dae83be 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.' + ); + } } /** @@ -1199,8 +1176,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' ); @@ -1210,8 +1187,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 893a7a06dae93..43cf1459f1fa5 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 ); } /**