Changeset 53531
- Timestamp:
- 06/19/2022 05:10:48 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/image/functions.php
r53530 r53531 268 268 } 269 269 270 // Save the file. 270 271 $file = wp_tempnam(); 271 272 $ret = wp_save_image_file( $file, $img, $mime_type, 1 ); 272 273 274 // Make assertions. 273 275 $this->assertNotEmpty( $ret, 'Image failed to save - "empty" response returned' ); 274 276 $this->assertNotWPError( $ret, 'Image failed to save - WP_Error returned' ); … … 318 320 * Test that a passed mime type overrides the extension in the filename when saving an image. 319 321 * 320 * @dataProvider data_mime_overrides_filename 322 * @dataProvider data_mime_overrides_filename_when_saving_an_image 321 323 * 322 324 * @ticket 6821 … … 352 354 * @return array 353 355 */ 354 public function data_mime_overrides_filename () {356 public function data_mime_overrides_filename_when_saving_an_image() { 355 357 return $this->text_array_to_dataprovider( $this->get_image_editor_engine_classes() ); 356 358 } 357 359 358 360 /** 359 * Test that mime types are correctly inferred from file extensions 361 * Test that mime types are correctly inferred from file extensions when saving an image. 362 * 363 * @dataProvider data_inferred_mime_types_when_saving_an_image 360 364 * 361 365 * @ticket 6821 366 * @covers WP_Image_Editor::get_mime_type 367 * @covers WP_Image_Editor::get_output_format 362 368 * @requires extension fileinfo 363 */ 364 public function test_inferred_mime_types() { 369 * 370 * @param string $class_name Name of the image editor engine class to be tested. 371 * @param string $extension File extension. 372 * @param string $mime_type The mime type to test. 373 */ 374 public function test_inferred_mime_types_when_saving_an_image( $class_name, $extension, $mime_type ) { 375 $img = new $class_name( DIR_TESTDATA . '/images/canola.jpg' ); 376 $loaded = $img->load(); 377 378 $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); 379 $this->assertNotWPError( $img ); 380 381 if ( ! $img->supports_mime_type( $mime_type ) ) { 382 $this->markTestSkipped( 383 sprintf( 384 'The %s mime type is not supported by the %s engine', 385 $mime_type, 386 str_replace( 'WP_Image_Editor_', '', $class_name ) 387 ) 388 ); 389 } 390 391 // Save the file. 392 $temp = get_temp_dir(); 393 $file = wp_unique_filename( $temp, uniqid() . ".$extension" ); 394 $ret = $img->save( trailingslashit( $temp ) . $file ); 395 396 // Make assertions. 397 $this->assertNotEmpty( $ret, 'Image failed to save - "empty" response returned' ); 398 $this->assertNotWPError( $ret, 'Image failed to save - WP Error returned' ); 399 $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image was not inferred correctly' ); 400 401 // Clean up. 402 unlink( $ret['path'] ); 403 unset( $img ); 404 } 405 406 /** 407 * Data provider. 408 * 409 * @return array 410 */ 411 public function data_inferred_mime_types_when_saving_an_image() { 365 412 $classes = $this->get_image_editor_engine_classes(); 366 413 … … 373 420 'png' => 'image/png', 374 421 'webp' => 'image/webp', 375 'unk' => 'image/jpeg', // Default, unknown. 376 ); 377 378 // Test each image editor engine. 422 'unk' => 'image/jpeg', // Default, unknown. 423 ); 424 425 $data = array(); 426 379 427 foreach ( $classes as $class ) { 380 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );381 $loaded = $img->load();382 383 // Save the image as each file extension, check the mime type.384 $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );385 $this->assertNotWPError( $img );386 387 $temp = get_temp_dir();388 428 foreach ( $mime_types as $ext => $mime_type ) { 389 if ( ! $img->supports_mime_type( $mime_type ) ) { 390 continue; 391 } 392 393 $file = wp_unique_filename( $temp, uniqid() . ".$ext" ); 394 $ret = $img->save( trailingslashit( $temp ) . $file ); 395 $this->assertNotEmpty( $ret ); 396 $this->assertNotWPError( $ret ); 397 $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ) ); 398 unlink( $ret['path'] ); 429 $data[ $class . '; Extension: ' . $ext . '; Mime type: ' . $mime_type ] = array( 430 'class_name' => $class, 431 'extension' => $ext, 432 'mime_type' => $mime_type, 433 ); 399 434 } 400 401 // Clean up. 402 unset( $img ); 403 } 435 } 436 437 return $data; 404 438 } 405 439
Note: See TracChangeset
for help on using the changeset viewer.