Skip to content

Commit

Permalink
Correcting a mistake I made in #49974: we need the author, date and o…
Browse files Browse the repository at this point in the history
…ther details from the revision item not the parent

Adding tests
  • Loading branch information
ramonjd committed Apr 27, 2023
1 parent 61bbcd0 commit 931affd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
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 @@ -76,7 +86,6 @@ public function test_get_items() {
);

$post_id = wp_update_post( $new_styles_post, true, false );
$post = get_post( $post_id );
$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 +103,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 +125,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

0 comments on commit 931affd

Please sign in to comment.