Make WordPress Core

Changeset 64784


Ignore:
Timestamp:
10/27/2026 12:15:09 AM (31 hours ago)
Author:
lancewillett
Message:

Tests: Cover menu location REST response preparation.

Replace the empty test_prepare_item() placeholder with checks for menu location data, assigned-menu links, and _fields filtering.

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

Props devmuhib, shailu25, huzaifaalmesbah.
See #40538.

File:
1 edited

Legend:

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

    r64004 r64784  
    227227
    228228        /**
    229          * The prepare_item() method does not exist for menu locations.
    230          *
    231          * @doesNotPerformAssertions
     229         * @ticket 40538
     230         *
     231         * @covers ::prepare_item_for_response
    232232         */
    233233        public function test_prepare_item() {
    234                 // Controller does not implement prepare_item().
     234                $this->register_nav_menu_locations( array( 'primary' ) );
     235
     236                $menu = wp_create_nav_menu( 'Primary Menu' );
     237                set_theme_mod( 'nav_menu_locations', array( 'primary' => $menu ) );
     238
     239                $location = (object) array(
     240                        'name'        => 'primary',
     241                        'description' => 'Primary',
     242                );
     243
     244                $endpoint = new WP_REST_Menu_Locations_Controller();
     245                $request  = new WP_REST_Request( 'GET', '/wp/v2/menu-locations/primary' );
     246                $response = $endpoint->prepare_item_for_response( $location, $request );
     247
     248                $this->assertInstanceOf( WP_REST_Response::class, $response );
     249                $this->assertSame( 200, $response->get_status() );
     250
     251                $data = $response->get_data();
     252                $this->assertSame( 'primary', $data['name'] );
     253                $this->assertSame( 'Primary', $data['description'] );
     254                $this->assertSame( $menu, $data['menu'] );
     255
     256                $links = $response->get_links();
     257                $this->assertArrayHasKey( 'self', $links );
     258                $this->assertSame( rest_url( 'wp/v2/menu-locations/primary' ), $links['self'][0]['href'] );
     259                $this->assertArrayHasKey( 'collection', $links );
     260                $this->assertSame( rest_url( 'wp/v2/menu-locations' ), $links['collection'][0]['href'] );
     261                $this->assertArrayHasKey( 'https://api.w.org/menu', $links );
     262                $this->assertSame( rest_url( rest_get_route_for_term( $menu ) ), $links['https://api.w.org/menu'][0]['href'] );
     263        }
     264
     265        /**
     266         * @ticket 40538
     267         *
     268         * @covers ::prepare_item_for_response
     269         */
     270        public function test_prepare_item_limit_fields() {
     271                $location = (object) array(
     272                        'name'        => 'primary',
     273                        'description' => 'Primary',
     274                );
     275
     276                $endpoint = new WP_REST_Menu_Locations_Controller();
     277                $request  = new WP_REST_Request( 'GET', '/wp/v2/menu-locations/primary' );
     278                $request->set_param( '_fields', 'name' );
     279
     280                $response = $endpoint->prepare_item_for_response( $location, $request );
     281                $this->assertSame( array( 'name' => 'primary' ), $response->get_data() );
     282                $this->assertEmpty( $response->get_links() );
    235283        }
    236284
Note: See TracChangeset for help on using the changeset viewer.