Changeset 42343 for trunk/tests/phpunit/tests/image/functions.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/image/functions.php
r42228 r42343 30 30 /** 31 31 * Get the MIME type of a file 32 * 32 33 * @param string $filename 33 34 * @return string … … 36 37 $mime_type = ''; 37 38 if ( extension_loaded( 'fileinfo' ) ) { 38 $finfo = new finfo();39 $finfo = new finfo(); 39 40 $mime_type = $finfo->file( $filename, FILEINFO_MIME ); 40 41 } … … 59 60 'test-image-zip.tiff', 60 61 'test-image.jpg', 61 62 63 foreach ( $files as $file) {64 $this->assertTrue( file_is_valid_image( DIR_TESTDATA .'/images/'.$file ), "file_is_valid_image($file) should return true" );62 ); 63 64 foreach ( $files as $file ) { 65 $this->assertTrue( file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" ); 65 66 } 66 67 } … … 72 73 'test-image.tga', 73 74 'test-image.sgi', 74 75 76 foreach ( $files as $file) {77 $this->assertFalse( file_is_valid_image( DIR_TESTDATA .'/images/'.$file ), "file_is_valid_image($file) should return false" );75 ); 76 77 foreach ( $files as $file ) { 78 $this->assertFalse( file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return false" ); 78 79 } 79 80 } … … 85 86 'test-image.png', 86 87 'test-image.jpg', 87 88 89 foreach ( $files as $file) {90 $this->assertTrue( file_is_displayable_image( DIR_TESTDATA .'/images/'.$file ), "file_is_valid_image($file) should return true" );88 ); 89 90 foreach ( $files as $file ) { 91 $this->assertTrue( file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" ); 91 92 } 92 93 } … … 106 107 'test-image.psd', 107 108 'test-image-zip.tiff', 108 109 110 foreach ( $files as $file) {111 $this->assertFalse( file_is_displayable_image( DIR_TESTDATA .'/images/'.$file ), "file_is_valid_image($file) should return false" );109 ); 110 111 foreach ( $files as $file ) { 112 $this->assertFalse( file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return false" ); 112 113 } 113 114 } … … 115 116 /** 116 117 * Test save image file and mime_types 118 * 117 119 * @ticket 6821 118 120 */ … … 128 130 'image/jpeg', 129 131 'image/gif', 130 'image/png' 132 'image/png', 131 133 ); 132 134 133 135 // Test each image editor engine 134 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');136 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); 135 137 foreach ( $classes as $class ) { 136 138 … … 140 142 } 141 143 142 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );144 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' ); 143 145 $loaded = $img->load(); 144 146 … … 150 152 151 153 $file = wp_tempnam(); 152 $ret = wp_save_image_file( $file, $img, $mime_type, 1 );154 $ret = wp_save_image_file( $file, $img, $mime_type, 1 ); 153 155 $this->assertNotEmpty( $ret ); 154 156 $this->assertNotInstanceOf( 'WP_Error', $ret ); … … 167 169 /** 168 170 * Test that a passed mime type overrides the extension in the filename 171 * 169 172 * @ticket 6821 170 173 */ … … 175 178 176 179 // Test each image editor engine 177 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');180 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); 178 181 foreach ( $classes as $class ) { 179 182 … … 183 186 } 184 187 185 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );188 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' ); 186 189 $loaded = $img->load(); 187 190 188 191 // Save the file 189 192 $mime_type = 'image/gif'; 190 $file = wp_tempnam( 'tmp.jpg' );191 $ret = $img->save( $file, $mime_type );193 $file = wp_tempnam( 'tmp.jpg' ); 194 $ret = $img->save( $file, $mime_type ); 192 195 193 196 // Make assertions … … 205 208 /** 206 209 * Test that mime types are correctly inferred from file extensions 210 * 207 211 * @ticket 6821 208 212 */ … … 219 223 'gif' => 'image/gif', 220 224 'png' => 'image/png', 221 'unk' => 'image/jpeg' // Default, unknown225 'unk' => 'image/jpeg', // Default, unknown 222 226 ); 223 227 224 228 // Test each image editor engine 225 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');229 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); 226 230 foreach ( $classes as $class ) { 227 231 … … 231 235 } 232 236 233 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );237 $img = new $class( DIR_TESTDATA . '/images/canola.jpg' ); 234 238 $loaded = $img->load(); 235 239 … … 245 249 246 250 $file = wp_unique_filename( $temp, uniqid() . ".$ext" ); 247 $ret = $img->save( trailingslashit( $temp ) . $file );251 $ret = $img->save( trailingslashit( $temp ) . $file ); 248 252 $this->assertNotEmpty( $ret ); 249 253 $this->assertNotInstanceOf( 'WP_Error', $ret ); … … 273 277 274 278 // Then, test with editors. 275 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');279 $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); 276 280 foreach ( $classes as $class ) { 277 281 // If the image editor isn't available, skip it … … 289 293 290 294 public function test_wp_crop_image_file() { 291 if ( ! function_exists( 'imagejpeg' ) )295 if ( ! function_exists( 'imagejpeg' ) ) { 292 296 $this->fail( 'jpeg support unavailable' ); 293 294 $file = wp_crop_image( DIR_TESTDATA . '/images/canola.jpg', 295 0, 0, 100, 100, 100, 100 ); 297 } 298 299 $file = wp_crop_image( 300 DIR_TESTDATA . '/images/canola.jpg', 301 0, 0, 100, 100, 100, 100 302 ); 296 303 $this->assertNotInstanceOf( 'WP_Error', $file ); 297 304 $this->assertFileExists( $file ); 298 305 $image = wp_get_image_editor( $file ); 299 $size = $image->get_size();306 $size = $image->get_size(); 300 307 $this->assertEquals( 100, $size['height'] ); 301 308 $this->assertEquals( 100, $size['width'] ); … … 305 312 306 313 public function test_wp_crop_image_url() { 307 if ( ! function_exists( 'imagejpeg' ) )314 if ( ! function_exists( 'imagejpeg' ) ) { 308 315 $this->fail( 'jpeg support unavailable' ); 316 } 309 317 310 318 if ( ! extension_loaded( 'openssl' ) ) { … … 312 320 } 313 321 314 $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg', 322 $file = wp_crop_image( 323 'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg', 315 324 0, 0, 100, 100, 100, 100, false, 316 DIR_TESTDATA . '/images/' . __FUNCTION__ . '.jpg' ); 325 DIR_TESTDATA . '/images/' . __FUNCTION__ . '.jpg' 326 ); 317 327 $this->assertNotInstanceOf( 'WP_Error', $file ); 318 328 $this->assertFileExists( $file ); 319 329 $image = wp_get_image_editor( $file ); 320 $size = $image->get_size();330 $size = $image->get_size(); 321 331 $this->assertEquals( 100, $size['height'] ); 322 332 $this->assertEquals( 100, $size['width'] ); … … 326 336 327 337 public function test_wp_crop_image_file_not_exist() { 328 $file = wp_crop_image( DIR_TESTDATA . '/images/canoladoesnotexist.jpg', 329 0, 0, 100, 100, 100, 100 ); 338 $file = wp_crop_image( 339 DIR_TESTDATA . '/images/canoladoesnotexist.jpg', 340 0, 0, 100, 100, 100, 100 341 ); 330 342 $this->assertInstanceOf( 'WP_Error', $file ); 331 343 } … … 336 348 } 337 349 338 $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canoladoesnotexist.jpg', 339 0, 0, 100, 100, 100, 100 ); 350 $file = wp_crop_image( 351 'https://asdftestblog1.files.wordpress.com/2008/04/canoladoesnotexist.jpg', 352 0, 0, 100, 100, 100, 100 353 ); 340 354 $this->assertInstanceOf( 'WP_Error', $file ); 341 355 } … … 352 366 add_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) ); 353 367 354 $file = wp_crop_image( DIR_TESTDATA . '/images/canola.jpg', 355 0, 0, 100, 100, 100, 100 ); 368 $file = wp_crop_image( 369 DIR_TESTDATA . '/images/canola.jpg', 370 0, 0, 100, 100, 100, 100 371 ); 356 372 $this->assertInstanceOf( 'WP_Error', $file ); 357 373 … … 372 388 copy( $orig_file, $test_file ); 373 389 374 $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array( 375 'post_mime_type' => 'application/pdf', 376 ) ); 390 $attachment_id = $this->factory->attachment->create_object( 391 $test_file, 0, array( 392 'post_mime_type' => 'application/pdf', 393 ) 394 ); 377 395 378 396 $this->assertNotEmpty( $attachment_id ); … … 381 399 'sizes' => array( 382 400 'thumbnail' => array( 383 'file' => "wordpress-gsoc-flyer-pdf-116x150.jpg",401 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', 384 402 'width' => 116, 385 403 'height' => 150, 386 'mime-type' => "image/jpeg",404 'mime-type' => 'image/jpeg', 387 405 ), 388 406 'medium' => array( 389 'file' => "wordpress-gsoc-flyer-pdf-232x300.jpg",407 'file' => 'wordpress-gsoc-flyer-pdf-232x300.jpg', 390 408 'width' => 232, 391 409 'height' => 300, 392 'mime-type' => "image/jpeg",410 'mime-type' => 'image/jpeg', 393 411 ), 394 412 'large' => array( 395 'file' => "wordpress-gsoc-flyer-pdf-791x1024.jpg",413 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', 396 414 'width' => 791, 397 415 'height' => 1024, 398 'mime-type' => "image/jpeg",416 'mime-type' => 'image/jpeg', 399 417 ), 400 418 'full' => array( 401 'file' => "wordpress-gsoc-flyer-pdf.jpg",419 'file' => 'wordpress-gsoc-flyer-pdf.jpg', 402 420 'width' => 1088, 403 421 'height' => 1408, 404 'mime-type' => "image/jpeg",422 'mime-type' => 'image/jpeg', 405 423 ), 406 424 ), … … 412 430 unlink( $test_file ); 413 431 foreach ( $metadata['sizes'] as $size ) { 414 unlink 432 unlink( '/tmp/' . $size['file'] ); 415 433 } 416 434 } … … 428 446 copy( $orig_file, $test_file ); 429 447 430 $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array( 431 'post_mime_type' => 'application/pdf', 432 ) ); 448 $attachment_id = $this->factory->attachment->create_object( 449 $test_file, 0, array( 450 'post_mime_type' => 'application/pdf', 451 ) 452 ); 433 453 434 454 $this->assertNotEmpty( $attachment_id ); … … 453 473 unlink( $test_file ); 454 474 foreach ( $metadata['sizes'] as $size ) { 455 unlink 475 unlink( '/tmp/' . $size['file'] ); 456 476 } 457 477 } … … 466 486 /** 467 487 * Test PDF preview doesn't overwrite existing JPEG. 488 * 468 489 * @ticket 39875 469 490 */ … … 483 504 copy( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf', $pdf_path ); 484 505 485 $attachment_id = $this->factory->attachment->create_object( $pdf_path, 0, array( 486 'post_mime_type' => 'application/pdf', 487 ) ); 488 489 $metadata = wp_generate_attachment_metadata( $attachment_id, $pdf_path ); 506 $attachment_id = $this->factory->attachment->create_object( 507 $pdf_path, 0, array( 508 'post_mime_type' => 'application/pdf', 509 ) 510 ); 511 512 $metadata = wp_generate_attachment_metadata( $attachment_id, $pdf_path ); 490 513 $preview_path = '/tmp/' . $metadata['sizes']['full']['file']; 491 514
Note: See TracChangeset
for help on using the changeset viewer.