Make WordPress Core

Changeset 64827


Ignore:
Timestamp:
10/27/2026 07:40:03 PM (4 days ago)
Author:
lancewillett
Message:

Tests: Cover sidebar REST response preparation.

Replace the assertion-free sidebar preparation test with checks for response data and links. Verify that links are omitted when _fields excludes them.

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

Props devmuhib, shailu25, huzaifaalmesbah.
See #40538.

File:
1 edited

Legend:

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

    r64283 r64827  
    11381138
    11391139        /**
    1140          * The prepare_item() method does not exist for sidebar.
     1140         * @ticket 40538
    11411141         *
    1142          * @doesNotPerformAssertions
     1142         * @covers WP_REST_Sidebars_Controller::prepare_item_for_response
    11431143         */
    11441144        public function test_prepare_item() {
    1145                 // Controller does not implement prepare_item().
     1145                $this->setup_sidebar(
     1146                        'sidebar-1',
     1147                        array(
     1148                                'name'        => 'Test sidebar',
     1149                                'description' => 'Test description',
     1150                        )
     1151                );
     1152
     1153                $raw_sidebar = array(
     1154                        'id'   => 'sidebar-1',
     1155                        'name' => 'Test sidebar',
     1156                );
     1157
     1158                $controller = new WP_REST_Sidebars_Controller();
     1159                $request    = new WP_REST_Request( 'GET', '/wp/v2/sidebars/sidebar-1' );
     1160                $response   = $controller->prepare_item_for_response( $raw_sidebar, $request );
     1161
     1162                $this->assertInstanceOf( WP_REST_Response::class, $response );
     1163                $this->assertSame( 200, $response->get_status() );
     1164
     1165                $data = $response->get_data();
     1166                $this->assertSame( 'sidebar-1', $data['id'] );
     1167                $this->assertSame( 'Test sidebar', $data['name'] );
     1168                $this->assertSame( 'Test description', $data['description'] );
     1169                $this->assertSame( 'active', $data['status'] );
     1170                $this->assertSame( array(), $data['widgets'] );
     1171
     1172                $links = $response->get_links();
     1173                $this->assertArrayHasKey( 'self', $links );
     1174                $this->assertSame( rest_url( 'wp/v2/sidebars/sidebar-1' ), $links['self'][0]['href'] );
     1175                $this->assertArrayHasKey( 'collection', $links );
     1176                $this->assertSame( rest_url( 'wp/v2/sidebars' ), $links['collection'][0]['href'] );
     1177        }
     1178
     1179        /**
     1180         * @ticket 40538
     1181         *
     1182         * @covers WP_REST_Sidebars_Controller::prepare_item_for_response
     1183         */
     1184        public function test_prepare_item_limit_fields() {
     1185                $this->setup_sidebar(
     1186                        'sidebar-1',
     1187                        array(
     1188                                'name' => 'Test sidebar',
     1189                        )
     1190                );
     1191
     1192                $raw_sidebar = array(
     1193                        'id'   => 'sidebar-1',
     1194                        'name' => 'Test sidebar',
     1195                );
     1196
     1197                $controller = new WP_REST_Sidebars_Controller();
     1198                $request    = new WP_REST_Request( 'GET', '/wp/v2/sidebars/sidebar-1' );
     1199                $request->set_param( '_fields', 'id,name' );
     1200
     1201                $response = $controller->prepare_item_for_response( $raw_sidebar, $request );
     1202
     1203                $this->assertEmpty( $response->get_links(), 'Links should be omitted when _links is not in _fields.' );
    11461204        }
    11471205
Note: See TracChangeset for help on using the changeset viewer.