Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisions controller: fix author and date fields #50117

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,24 @@ 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,
)
);
}

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 ) ) {
Expand All @@ -161,19 +161,19 @@ 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 ) ) {
$data['id'] = (int) $item->ID;
}

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 ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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',
)
Expand Down Expand Up @@ -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();
Expand All @@ -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'
Expand All @@ -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' );
}

/**
Expand Down