Skip to content

Commit 81627bb

Browse files
committed
Tests: Cover global styles REST response preparation.
Replace the empty `test_prepare_item()` placeholder with assertions for the global styles response data and links. Verify that `_fields=id` omits unrequested fields and links. Developed in: #14694 Props shailu25. See #40538. git-svn-id: https://develop.svn.wordpress.org/trunk@64787 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 044ceb8 commit 81627bb

1 file changed

Lines changed: 49 additions & 2 deletions

File tree

‎tests/phpunit/tests/rest-api/rest-global-styles-controller.php‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,57 @@ public function test_delete_item() {
793793
}
794794

795795
/**
796-
* @doesNotPerformAssertions
796+
* @ticket 40538
797+
*
798+
* @covers WP_REST_Global_Styles_Controller::prepare_item_for_response
797799
*/
798800
public function test_prepare_item() {
799-
// Controller does not implement prepare_item().
801+
wp_set_current_user( self::$admin_id );
802+
803+
$post = get_post( self::$global_styles_id );
804+
$controller = new WP_REST_Global_Styles_Controller();
805+
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
806+
807+
$response = $controller->prepare_item_for_response( $post, $request );
808+
809+
$this->assertInstanceOf( WP_REST_Response::class, $response );
810+
$this->assertSame( 200, $response->get_status() );
811+
812+
$this->assertEqualSetsWithIndex(
813+
array(
814+
'id' => self::$global_styles_id,
815+
'title' => array(
816+
'raw' => 'Custom Styles',
817+
'rendered' => 'Custom Styles',
818+
),
819+
'settings' => new stdClass(),
820+
'styles' => new stdClass(),
821+
),
822+
$response->get_data()
823+
);
824+
825+
$links = test_rest_expand_compact_links( $response->get_links() );
826+
$this->assertSame( rest_url( 'wp/v2/global-styles/' . self::$global_styles_id ), $links['self'][0]['href'] );
827+
$this->assertSame( rest_url( 'wp/v2/types/wp_global_styles' ), $links['about'][0]['href'] );
828+
}
829+
830+
/**
831+
* @ticket 40538
832+
*
833+
* @covers WP_REST_Global_Styles_Controller::prepare_item_for_response
834+
*/
835+
public function test_prepare_item_limit_fields() {
836+
wp_set_current_user( self::$admin_id );
837+
838+
$post = get_post( self::$global_styles_id );
839+
$controller = new WP_REST_Global_Styles_Controller();
840+
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
841+
$request->set_param( '_fields', 'id' );
842+
843+
$response = $controller->prepare_item_for_response( $post, $request );
844+
845+
$this->assertSame( array( 'id' => self::$global_styles_id ), $response->get_data() );
846+
$this->assertEmpty( $response->get_links() );
800847
}
801848

802849
/**

0 commit comments

Comments
 (0)