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