| | 247 | * Test delete with edited image (and backup sizes). |
| | 248 | * @ticket 39476 |
| | 249 | */ |
| | 250 | function test_insert_image_delete_with_edited_image() { |
| | 251 | if ( ! function_exists( 'imagejpeg' ) ) { |
| | 252 | $this->markTestSkipped( 'jpeg support unavailable' ); |
| | 253 | } |
| | 254 | |
| | 255 | $prev_opt_medium_size_w = get_option( 'medium_size_w' ); |
| | 256 | $prev_opt_medium_size_h = get_option( 'medium_size_h' ); |
| | 257 | |
| | 258 | // Generate thumbnail only. |
| | 259 | update_option( 'medium_size_w', 0 ); |
| | 260 | update_option( 'medium_size_h', 0 ); |
| | 261 | |
| | 262 | $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' ); |
| | 263 | $basename_noext = 'blah'; |
| | 264 | |
| | 265 | $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents ); |
| | 266 | $this->assertEmpty( $upload['error'] ); |
| | 267 | $basename_noext = sanitize_file_name( $basename_noext ); |
| | 268 | |
| | 269 | $id = $this->_make_attachment( $upload ); |
| | 270 | $uploads = wp_upload_dir(); |
| | 271 | $dirname = trailingslashit( dirname( get_attached_file( $id ) ) ); |
| | 272 | $expected_files = array(); |
| | 273 | |
| | 274 | // Check that the intermediate thumbnail exists. |
| | 275 | $thumb = image_get_intermediate_size( $id, 'thumbnail' ); |
| | 276 | $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] ); |
| | 277 | $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) ); |
| | 278 | $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']; |
| | 279 | |
| | 280 | // Check that the intermediate medium doesn't exist. |
| | 281 | $medium = image_get_intermediate_size( $id, 'medium' ); |
| | 282 | $this->assertEmpty( $medium ); |
| | 283 | |
| | 284 | $meta = wp_get_attachment_metadata( $id ); |
| | 285 | $original = $meta['file']; |
| | 286 | $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) ); |
| | 287 | $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $original; |
| | 288 | |
| | 289 | // Create edited version. |
| | 290 | require_once ABSPATH . 'wp-admin/includes/image-edit.php'; |
| | 291 | // a2-small.jpg is 400x300, so halve. |
| | 292 | $_REQUEST = array( 'fwidth' => 200, 'fheight' => 150, 'target' => 'all', 'do' => 'scale' ); |
| | 293 | $edited = wp_save_image( $id ); |
| | 294 | $this->assertTrue( is_object( $edited ) ); |
| | 295 | $this->assertTrue( isset( $edited->thumbnail ) ); |
| | 296 | $this->assertTrue( is_file( $dirname . wp_basename( $edited->thumbnail ) ) ); |
| | 297 | $expected_files[] = $dirname . wp_basename( $edited->thumbnail ); |
| | 298 | |
| | 299 | // Backup sizes should exist. |
| | 300 | $backup_sizes = get_post_meta( $id, '_wp_attachment_backup_sizes', true ); |
| | 301 | $this->assertTrue( is_array( $backup_sizes ) ); |
| | 302 | $this->assertNotEmpty( $backup_sizes['thumbnail-orig'] ); |
| | 303 | |
| | 304 | // And be same as prior meta. |
| | 305 | $this->assertSame( $meta['sizes']['thumbnail'], $backup_sizes['thumbnail-orig'] ); |
| | 306 | $this->assertSame( wp_basename( $original ), $backup_sizes['full-orig']['file'] ); |
| | 307 | |
| | 308 | // But new meta should point to edited. |
| | 309 | $new_meta = wp_get_attachment_metadata( $id ); |
| | 310 | $this->assertSame( $new_meta['sizes']['thumbnail']['file'], wp_basename( $edited->thumbnail ) ); |
| | 311 | $this->assertNotSame( $new_meta['file'], $original ); |
| | 312 | $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file'] ) ); |
| | 313 | $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file']; |
| | 314 | |
| | 315 | // Passthru filter. |
| | 316 | add_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) ); |
| | 317 | |
| | 318 | // Now delete the attachment and make sure all files are gone. |
| | 319 | wp_delete_attachment( $id ); |
| | 320 | |
| | 321 | foreach ( $expected_files as $expected_file ) { |
| | 322 | $this->assertFalse( is_file( $expected_file ) ); |
| | 323 | } |
| | 324 | |
| | 325 | $this->assertSame( sort( $expected_files ), sort( self::$wp_delete_files ) ); |
| | 326 | |
| | 327 | // Cleanup. |
| | 328 | remove_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) ); |
| | 329 | update_option( 'medium_size_w', $prev_opt_medium_size_w ); |
| | 330 | update_option( 'medium_size_h', $prev_opt_medium_size_h ); |
| | 331 | } |
| | 332 | |
| | 333 | static $wp_delete_files = array(); |
| | 334 | |
| | 335 | function wp_delete_file_passthru_filter( $file ) { |
| | 336 | $this->assertTrue( path_is_absolute( $file ) ); |
| | 337 | self::$wp_delete_files[] = $file; |
| | 338 | return $file; |
| | 339 | } |
| | 340 | |
| | 341 | /** |
| | 342 | * Test delete of UTF-8 filename. |
| | 343 | * @ticket 33227 |
| | 344 | * @ticket 39476 |
| | 345 | */ |
| | 346 | function test_insert_image_delete_utf8() { |
| | 347 | if ( ! function_exists( 'imagejpeg' ) ) { |
| | 348 | $this->markTestSkipped( 'jpeg support unavailable' ); |
| | 349 | } |
| | 350 | |
| | 351 | $prev_opt_medium_size_w = get_option( 'medium_size_w' ); |
| | 352 | $prev_opt_medium_size_h = get_option( 'medium_size_h' ); |
| | 353 | |
| | 354 | // Generate thumbnail only. |
| | 355 | update_option( 'medium_size_w', 0 ); |
| | 356 | update_option( 'medium_size_h', 0 ); |
| | 357 | |
| | 358 | $prev_ctype_locale = setlocale( LC_CTYPE, 'C' ); // In case shell env has set LC_CTYPE. |
| | 359 | $this->assertTrue( false !== $prev_ctype_locale ); |
| | 360 | |
| | 361 | $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' ); |
| | 362 | $basename_noext = 'هم اندیشی'; |
| | 363 | |
| | 364 | $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents ); |
| | 365 | $this->assertEmpty( $upload['error'] ); |
| | 366 | $basename_noext = sanitize_file_name( $basename_noext ); |
| | 367 | |
| | 368 | $id = $this->_make_attachment( $upload ); |
| | 369 | $uploads = wp_upload_dir(); |
| | 370 | |
| | 371 | // Check that the intermediate thumbnail exists. |
| | 372 | $thumb = image_get_intermediate_size( $id, 'thumbnail' ); |
| | 373 | $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] ); |
| | 374 | $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) ); |
| | 375 | |
| | 376 | // Check that the intermediate medium doesn't exist. |
| | 377 | $medium = image_get_intermediate_size( $id, 'medium' ); |
| | 378 | $this->assertEmpty( $medium ); |
| | 379 | |
| | 380 | $meta = wp_get_attachment_metadata( $id ); |
| | 381 | $original = $meta['file']; |
| | 382 | $this->assertSame( $basename_noext . '.jpg', wp_basename( $original ) ); |
| | 383 | $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) ); |
| | 384 | |
| | 385 | // now delete the attachment and make sure all files are gone |
| | 386 | wp_delete_attachment( $id ); |
| | 387 | |
| | 388 | $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) ); |
| | 389 | $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) ); |
| | 390 | |
| | 391 | // Cleanup. |
| | 392 | setlocale( LC_CTYPE, $prev_ctype_locale ); |
| | 393 | update_option( 'medium_size_w', $prev_opt_medium_size_w ); |
| | 394 | update_option( 'medium_size_h', $prev_opt_medium_size_h ); |
| | 395 | } |
| | 396 | |
| | 397 | /** |