Make WordPress Core

Changeset 64451


Ignore:
Timestamp:
10/17/2026 07:20:38 PM (7 days ago)
Author:
lancewillett
Message:

Tests: Cover REST page response preparation.

Replace the empty test_prepare_item() stub with assertions for page fields in the edit context and the self, collection, and about links returned by WP_REST_Posts_Controller::prepare_item_for_response().

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14384

Props rejaulalomkhan, huzaifaalmesbah.
See #40538.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r64433 r64451  
    615615
    616616        /**
    617          * @doesNotPerformAssertions
     617         * @ticket 40538
    618618         */
    619619        public function test_prepare_item() {
    620                 // Controller does not implement prepare_item().
     620                wp_set_current_user( self::$editor_id );
     621
     622                $page_id = self::factory()->post->create(
     623                        array(
     624                                'post_type'    => 'page',
     625                                'post_title'   => 'Sample Page',
     626                                'post_content' => 'Sample content',
     627                                'post_status'  => 'publish',
     628                                'post_author'  => self::$editor_id,
     629                        )
     630                );
     631
     632                $endpoint = new WP_REST_Posts_Controller( 'page' );
     633                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) );
     634                $request->set_param( 'context', 'edit' );
     635                $page     = get_post( $page_id );
     636                $response = $endpoint->prepare_item_for_response( $page, $request );
     637
     638                $this->assertInstanceOf( WP_REST_Response::class, $response );
     639                $this->assertSame( 200, $response->get_status() );
     640
     641                $data = $response->get_data();
     642                $this->assertSame( $page_id, $data['id'] );
     643                $this->assertSame( 'page', $data['type'] );
     644                $this->assertSame( 'Sample Page', $data['title']['raw'] );
     645                $this->assertSame( 'Sample content', $data['content']['raw'] );
     646                $this->assertSame( 'publish', $data['status'] );
     647                $this->assertSame( self::$editor_id, $data['author'] );
     648                $this->assertSame( $page->post_excerpt, $data['excerpt']['raw'] );
     649                $this->assertSame( 0, $data['parent'] );
     650                $this->assertSame( 0, $data['menu_order'] );
     651
     652                $links = test_rest_expand_compact_links( $response->get_links() );
     653                $this->assertSame( rest_url( sprintf( 'wp/v2/pages/%d', $page_id ) ), $links['self'][0]['href'] );
     654                $this->assertSame( rest_url( 'wp/v2/pages' ), $links['collection'][0]['href'] );
     655                $this->assertSame( rest_url( 'wp/v2/types/page' ), $links['about'][0]['href'] );
    621656        }
    622657
Note: See TracChangeset for help on using the changeset viewer.