Make WordPress Core

Changeset 65106


Ignore:
Timestamp:
10/30/2026 10:12:24 PM (8 hours ago)
Author:
lancewillett
Message:

Tests: Cover empty objects in REST media responses.

Verify empty media_details.sizes and missing attachment metadata are returned as objects that encode to {}, preserving the REST response schema.

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

Props therssoftware, huzaifaalmesbah.
See #65251.

File:
1 edited

Legend:

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

    r65089 r65106  
    11931193                $this->assertIsArray( $data['media_details']['sizes'], 'Could not retrieve the sizes data.' );
    11941194                $this->assertArrayNotHasKey( 'source_url', $data['media_details']['sizes']['rest-api-test'] );
     1195        }
     1196
     1197        /**
     1198         * @ticket 65251
     1199         */
     1200        public function test_get_item_sizes_empty_sizes_is_object() {
     1201                $attachment_id = self::factory()->attachment->create_object(
     1202                        self::$test_file,
     1203                        0,
     1204                        array(
     1205                                'post_mime_type' => 'image/jpeg',
     1206                                'post_excerpt'   => 'A sample caption',
     1207                        ),
     1208                        self::$test_file
     1209                );
     1210
     1211                wp_update_attachment_metadata(
     1212                        $attachment_id,
     1213                        array(
     1214                                'width'      => 50,
     1215                                'height'     => 50,
     1216                                'file'       => 'canola.jpg',
     1217                                'sizes'      => array(),
     1218                                'image_meta' => array(),
     1219                        )
     1220                );
     1221
     1222                $request  = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );
     1223                $response = rest_get_server()->dispatch( $request );
     1224                $data     = $response->get_data();
     1225
     1226                $this->assertInstanceOf( 'stdClass', $data['media_details']['sizes'], 'Sizes should be an empty object (stdClass) when empty.' );
     1227                $this->assertSame( '{}', wp_json_encode( $data['media_details']['sizes'] ), 'Sizes must JSON-encode as an empty object.' );
     1228        }
     1229
     1230        /**
     1231         * @ticket 65251
     1232         */
     1233        public function test_get_item_empty_media_details_is_object() {
     1234                $attachment_id = self::factory()->attachment->create_object(
     1235                        self::$test_file,
     1236                        0,
     1237                        array(
     1238                                'post_mime_type' => 'image/jpeg',
     1239                                'post_excerpt'   => 'A sample caption',
     1240                        ),
     1241                        self::$test_file
     1242                );
     1243
     1244                delete_post_meta( $attachment_id, '_wp_attachment_metadata' );
     1245
     1246                $request  = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );
     1247                $response = rest_get_server()->dispatch( $request );
     1248                $data     = $response->get_data();
     1249
     1250                $this->assertInstanceOf( 'stdClass', $data['media_details'], 'media_details should be an empty object (stdClass) when metadata is missing.' );
     1251                $this->assertSame( '{}', wp_json_encode( $data['media_details'] ), 'media_details must JSON-encode as an empty object.' );
    11951252        }
    11961253
Note: See TracChangeset for help on using the changeset viewer.