Skip to content

Commit a136469

Browse files
committed
Expand assertions and harden types in tests
1 parent 4de3c1d commit a136469

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,27 +3350,29 @@ public function test_sideload_scaled_unique_filename_conflict() {
33503350
* @ticket 62243
33513351
* @requires function imagejpeg
33523352
*/
3353-
public function test_finalize_item() {
3353+
public function test_finalize_item(): void {
33543354
wp_set_current_user( self::$author_id );
33553355

33563356
// Create an attachment.
33573357
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
33583358
$request->set_header( 'Content-Type', 'image/jpeg' );
33593359
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
3360-
$request->set_body( file_get_contents( self::$test_file ) );
3360+
$request->set_body( (string) file_get_contents( self::$test_file ) );
33613361
$response = rest_get_server()->dispatch( $request );
33623362
$attachment_id = $response->get_data()['id'];
33633363

33643364
$this->assertSame( 201, $response->get_status() );
33653365

33663366
// Track whether wp_generate_attachment_metadata filter fires.
3367-
$filter_called = false;
3368-
$filter_context = null;
3367+
$filter_metadata = null;
3368+
$filter_id = null;
3369+
$filter_context = null;
33693370
add_filter(
33703371
'wp_generate_attachment_metadata',
3371-
function ( $metadata, $id, $context ) use ( &$filter_called, &$filter_context ) {
3372-
$filter_called = true;
3373-
$filter_context = $context;
3372+
function ( array $metadata, int $id, string $context ) use ( &$filter_metadata, &$filter_id, &$filter_context ) {
3373+
$filter_metadata = $metadata;
3374+
$filter_id = $id;
3375+
$filter_context = $context;
33743376
return $metadata;
33753377
},
33763378
10,
@@ -3382,7 +3384,9 @@ function ( $metadata, $id, $context ) use ( &$filter_called, &$filter_context )
33823384
$response = rest_get_server()->dispatch( $request );
33833385

33843386
$this->assertSame( 200, $response->get_status(), 'Finalize endpoint should return 200.' );
3385-
$this->assertTrue( $filter_called, 'wp_generate_attachment_metadata filter should have been called.' );
3387+
$this->assertIsArray( $filter_metadata );
3388+
$this->assertStringContainsString( 'canola', $filter_metadata['file'], 'Expected the canola image to have been had its metadata updated.' );
3389+
$this->assertSame( $attachment_id, $filter_id, 'Expected the post ID to be passed to the filter.' );
33863390
$this->assertSame( 'update', $filter_context, 'Filter context should be "update".' );
33873391
}
33883392

@@ -3392,14 +3396,14 @@ function ( $metadata, $id, $context ) use ( &$filter_called, &$filter_context )
33923396
* @ticket 62243
33933397
* @requires function imagejpeg
33943398
*/
3395-
public function test_finalize_item_requires_auth() {
3399+
public function test_finalize_item_requires_auth(): void {
33963400
wp_set_current_user( self::$author_id );
33973401

33983402
// Create an attachment.
33993403
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
34003404
$request->set_header( 'Content-Type', 'image/jpeg' );
34013405
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
3402-
$request->set_body( file_get_contents( self::$test_file ) );
3406+
$request->set_body( (string) file_get_contents( self::$test_file ) );
34033407
$response = rest_get_server()->dispatch( $request );
34043408
$attachment_id = $response->get_data()['id'];
34053409

@@ -3417,10 +3421,12 @@ public function test_finalize_item_requires_auth() {
34173421
*
34183422
* @ticket 62243
34193423
*/
3420-
public function test_finalize_item_invalid_id() {
3424+
public function test_finalize_item_invalid_id(): void {
34213425
wp_set_current_user( self::$author_id );
34223426

3423-
$request = new WP_REST_Request( 'POST', '/wp/v2/media/999999/finalize' );
3427+
$invalid_id = PHP_INT_MAX;
3428+
$this->assertNull( get_post( $invalid_id ), 'Expected invalid ID to not exist for an existing post.' );
3429+
$request = new WP_REST_Request( 'POST', "/wp/v2/media/$invalid_id/finalize" );
34243430
$response = rest_get_server()->dispatch( $request );
34253431

34263432
$this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );

0 commit comments

Comments
 (0)