From 3fc895725ddd53c93233713872fe34fc948c4b78 Mon Sep 17 00:00:00 2001 From: Ramon Date: Thu, 27 Apr 2023 16:07:50 +1000 Subject: [PATCH] Revisions controller: fix author and date fields (#50117) * Correcting a mistake I made in https://github.com/WordPress/gutenberg/pull/49974: we need the author, date and other details from the revision item not the parent Adding tests * ICH LIEBE DICH, LINTER --- ...est-global-styles-revisions-controller.php | 14 +++---- ...lobal-styles-revisions-controller-test.php | 37 ++++++++++++++++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php b/lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php index 19e5262a1d3517..dfd57effe16ca9 100644 --- a/lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php +++ b/lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php @@ -135,12 +135,12 @@ public function prepare_item_for_response( $item, $request ) { $fields = $this->get_fields_for_response( $request ); if ( rest_is_field_included( 'author', $fields ) ) { - $data['author'] = (int) $parent->post_author; + $data['author'] = (int) $item->post_author; } if ( rest_is_field_included( 'author_avatar_url', $fields ) ) { $data['author_avatar_url'] = get_avatar_url( - $parent->post_author, + $item->post_author, array( 'size' => 24, ) @@ -148,11 +148,11 @@ public function prepare_item_for_response( $item, $request ) { } if ( rest_is_field_included( 'author_display_name', $fields ) ) { - $data['author_display_name'] = get_the_author_meta( 'display_name', $parent->post_author ); + $data['author_display_name'] = get_the_author_meta( 'display_name', $item->post_author ); } if ( rest_is_field_included( 'date', $fields ) ) { - $data['date'] = $parent->post_date; + $data['date'] = $item->post_date; } if ( rest_is_field_included( 'date_display', $fields ) ) { @@ -161,7 +161,7 @@ public function prepare_item_for_response( $item, $request ) { } if ( rest_is_field_included( 'date_gmt', $fields ) ) { - $data['date_gmt'] = $parent->post_date_gmt; + $data['date_gmt'] = $item->post_date_gmt; } if ( rest_is_field_included( 'id', $fields ) ) { @@ -169,11 +169,11 @@ public function prepare_item_for_response( $item, $request ) { } if ( rest_is_field_included( 'modified', $fields ) ) { - $data['modified'] = $parent->post_modified; + $data['modified'] = $item->post_modified; } if ( rest_is_field_included( 'modified_gmt', $fields ) ) { - $data['modified_gmt'] = $parent->post_modified_gmt; + $data['modified_gmt'] = $item->post_modified_gmt; } if ( rest_is_field_included( 'parent', $fields ) ) { diff --git a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php index e74b1c2dbe7dec..656e21c0d1060d 100644 --- a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php +++ b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php @@ -6,6 +6,11 @@ class Gutenberg_REST_Global_Styles_Revisions_Controller_Test extends WP_Test_RES */ protected static $admin_id; + /** + * @var int + */ + protected static $second_admin_id; + /** * @var int */ @@ -22,7 +27,12 @@ public function set_up() { * @param WP_UnitTest_Factory $factory Helper that lets us create fake data. */ public static function wpSetupBeforeClass( $factory ) { - self::$admin_id = $factory->user->create( + self::$admin_id = $factory->user->create( + array( + 'role' => 'administrator', + ) + ); + self::$second_admin_id = $factory->user->create( array( 'role' => 'administrator', ) @@ -75,8 +85,8 @@ public function test_get_items() { 'post_content' => wp_json_encode( $config ), ); - $post_id = wp_update_post( $new_styles_post, true, false ); - $post = get_post( $post_id ); + wp_update_post( $new_styles_post, true, false ); + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -94,8 +104,8 @@ public function test_get_items() { $this->assertArrayHasKey( 'modified_gmt', $data[0], 'Check that an modified_gmt key exists' ); // Author information. - $this->assertEquals( $post->post_author, $data[0]['author'], 'Check that author id returns expected value' ); - $this->assertEquals( get_the_author_meta( 'display_name', $post->post_author ), $data[0]['author_display_name'], 'Check that author display_name returns expected value' ); + $this->assertEquals( self::$admin_id, $data[0]['author'], 'Check that author id returns expected value' ); + $this->assertEquals( get_the_author_meta( 'display_name', self::$admin_id ), $data[0]['author_display_name'], 'Check that author display_name returns expected value' ); $this->assertIsString( $data[0]['author_avatar_url'], 'Check that author avatar_url returns expected value type' @@ -116,6 +126,23 @@ public function test_get_items() { ), 'Check that the revision styles match the last updated styles.' ); + + // Checks that the revisions are returned for all eligible users. + wp_set_current_user( self::$second_admin_id ); + $config['styles']['color']['background'] = 'blue'; + $new_styles_post = array( + 'ID' => self::$global_styles_id, + 'post_content' => wp_json_encode( $config ), + ); + + wp_update_post( $new_styles_post, true, false ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertCount( 2, $data, 'Check that two revisions exists' ); + $this->assertEquals( self::$second_admin_id, $data[0]['author'], 'Check that second author id returns expected value' ); } /**