Skip to content

Commit cd46a70

Browse files
committed
Tests: Use an unsupported image format for upload failures.
Use a TGA fixture so the unsupported-format assertions remain meaningful when AVIF support is available. Developed in: #10057 Props jigneshnakrani. See #64225, #63167. git-svn-id: https://develop.svn.wordpress.org/trunk@65116 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 518fcad commit cd46a70

1 file changed

Lines changed: 55 additions & 21 deletions

File tree

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

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
3232
*/
3333
private static string $test_avif_file;
3434

35+
/**
36+
* @var string The path to the TGA test image.
37+
*/
38+
private static string $test_tga_file;
39+
3540
/**
3641
* @var string The path to the SVG test image.
3742
*/
@@ -101,6 +106,9 @@ public static function wpTearDownAfterClass() {
101106
if ( file_exists( self::$test_avif_file ) ) {
102107
unlink( self::$test_avif_file );
103108
}
109+
if ( file_exists( self::$test_tga_file ) ) {
110+
unlink( self::$test_tga_file );
111+
}
104112
if ( file_exists( self::$test_video_file ) ) {
105113
unlink( self::$test_video_file );
106114
}
@@ -153,6 +161,12 @@ static function () {
153161
copy( $orig_avif_file, self::$test_avif_file );
154162
}
155163

164+
$orig_tga_file = DIR_TESTDATA . '/images/test-image.tga';
165+
self::$test_tga_file = get_temp_dir() . 'test-image.tga';
166+
if ( ! file_exists( self::$test_tga_file ) ) {
167+
copy( $orig_tga_file, self::$test_tga_file );
168+
}
169+
156170
$test_svg_file = DIR_TESTDATA . '/uploads/video-play.svg';
157171
self::$test_svg_file = get_temp_dir() . 'video-play.svg';
158172
if ( ! file_exists( self::$test_svg_file ) ) {
@@ -3502,46 +3516,66 @@ static function () {
35023516
/**
35033517
* Test that uploading unsupported image types throws a `rest_upload_image_type_not_supported` error.
35043518
*
3519+
* Uses TGA because it is reliably unsupported by the image editors, unlike AVIF which
3520+
* is commonly supported in CI. File params are required because the permission check
3521+
* reads `get_file_params()`.
3522+
*
35053523
* @ticket 61167
3524+
* @ticket 64225
35063525
*/
35073526
public function test_upload_unsupported_image_type() {
3508-
3509-
// Only run this test when the editor doesn't support AVIF.
3510-
if ( wp_image_editor_supports( array( 'AVIF' ) ) ) {
3511-
$this->markTestSkipped( 'The image editor suppports AVIF.' );
3527+
// Only run this test when the editor doesn't support TGA.
3528+
if ( wp_image_editor_supports( array( 'mime_type' => 'image/tga' ) ) ) {
3529+
$this->markTestSkipped( 'The image editor supports TGA.' );
35123530
}
35133531

3514-
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
3515-
35163532
wp_set_current_user( self::$author_id );
3517-
$request->set_header( 'Content-Type', 'image/avif' );
3518-
$request->set_header( 'Content-Disposition', 'attachment; filename=avif-lossy.avif' );
3519-
$request->set_body( file_get_contents( self::$test_avif_file ) );
3533+
3534+
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
3535+
$request->set_file_params(
3536+
array(
3537+
'file' => array(
3538+
'name' => 'test-image.tga',
3539+
'type' => 'image/tga',
3540+
'tmp_name' => self::$test_tga_file,
3541+
'error' => 0,
3542+
'size' => filesize( self::$test_tga_file ),
3543+
),
3544+
)
3545+
);
35203546
$response = rest_get_server()->dispatch( $request );
35213547

35223548
$this->assertErrorResponse( 'rest_upload_image_type_not_supported', $response, 400 );
35233549
}
35243550

35253551
/**
3526-
* Test that the `wp_prevent_unsupported_image_uploads` filter enables uploading of unsupported image types.
3552+
* Test that the `wp_prevent_unsupported_mime_type_uploads` filter enables uploading of unsupported image types.
3553+
*
3554+
* Forces the image editors list empty so the format is treated as unsupported regardless
3555+
* of host capabilities, then uses an allowed upload mime (AVIF) so the request can still
3556+
* complete with a successful 201 once the filter bypasses the editor-support check.
35273557
*
35283558
* @ticket 61167
3559+
* @ticket 64225
35293560
*/
35303561
public function test_upload_unsupported_image_type_with_filter() {
3562+
add_filter( 'wp_image_editors', '__return_empty_array' );
3563+
add_filter( 'wp_prevent_unsupported_mime_type_uploads', '__return_false' );
35313564

3532-
// Only run this test when the editor doesn't support AVIF.
3533-
if ( wp_image_editor_supports( array( 'AVIF' ) ) ) {
3534-
$this->markTestSkipped( 'The image editor suppports AVIF.' );
3535-
}
3536-
3537-
add_filter( 'wp_prevent_unsupported_image_uploads', '__return_false' );
3565+
wp_set_current_user( self::$author_id );
35383566

35393567
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
3540-
3541-
wp_set_current_user( self::$author_id );
3542-
$request->set_header( 'Content-Type', 'image/avif' );
3543-
$request->set_header( 'Content-Disposition', 'attachment; filename=avif-lossy.avif' );
3544-
$request->set_body( file_get_contents( self::$test_avif_file ) );
3568+
$request->set_file_params(
3569+
array(
3570+
'file' => array(
3571+
'name' => 'avif-lossy.avif',
3572+
'type' => 'image/avif',
3573+
'tmp_name' => self::$test_avif_file,
3574+
'error' => 0,
3575+
'size' => filesize( self::$test_avif_file ),
3576+
),
3577+
)
3578+
);
35453579
$response = rest_get_server()->dispatch( $request );
35463580

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

0 commit comments

Comments
 (0)