From 4bfbe07a89232ed0a18c83a3bebd18e136e4a7d3 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 22 Aug 2022 19:50:15 +0000 Subject: [PATCH] Tests: Clean up test image before performing assertions in image tests. This makes sure there are no leftover images in case of a test failure. Applies to: `test_wp_calculate_image_srcset_no_date_uploads()`. Includes renaming the `$int` variable to `$int_size` for consistency with some other tests. Follow-up to [34855], [35412], [35751], [53463]. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53920 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/media.php | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index daf585d548..7a1723af12 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -1653,11 +1653,11 @@ EOF; $expected = trim( $expected, ' ,' ); - foreach ( $intermediates as $int ) { - $image_url = wp_get_attachment_image_url( self::$large_id, $int ); - $size_array = $this->get_image_size_array_from_meta( $image_meta, $int ); + foreach ( $intermediates as $int_size ) { + $image_url = wp_get_attachment_image_url( self::$large_id, $int_size ); + $size_array = $this->get_image_size_array_from_meta( $image_meta, $int_size ); - if ( 'full' === $int ) { + if ( 'full' === $int_size ) { // Add the full size image. Expected to be in the srcset when the full size image is used as src. $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected; } else { @@ -1706,11 +1706,19 @@ EOF; $expected = trim( $expected, ' ,' ); - foreach ( $intermediates as $int ) { - $size_array = $this->get_image_size_array_from_meta( $image_meta, $int ); - $image_url = wp_get_attachment_image_url( $id, $int ); + foreach ( $intermediates as $int_size ) { + $image_urls[ $int_size ] = wp_get_attachment_image_url( $id, $int_size ); + } - if ( 'full' === $int ) { + // Remove the attachment. + wp_delete_attachment( $id, true ); + remove_filter( 'upload_dir', '_upload_dir_no_subdir' ); + + foreach ( $intermediates as $int_size ) { + $size_array = $this->get_image_size_array_from_meta( $image_meta, $int_size ); + $image_url = $image_urls[ $int_size ]; + + if ( 'full' === $int_size ) { // Add the full size image. Expected to be in the srcset when the full size image is used as src. $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected; } else { @@ -1721,9 +1729,6 @@ EOF; $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); } - // Remove the attachment. - wp_delete_attachment( $id, true ); - remove_filter( 'upload_dir', '_upload_dir_no_subdir' ); } /** @@ -1796,11 +1801,11 @@ EOF; // Prepend an absolute path to simulate a pre-2.7 upload. $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file']; - foreach ( $intermediates as $int ) { - $image_url = wp_get_attachment_image_url( self::$large_id, $int ); - $size_array = $this->get_image_size_array_from_meta( $image_meta, $int ); + foreach ( $intermediates as $int_size ) { + $image_url = wp_get_attachment_image_url( self::$large_id, $int_size ); + $size_array = $this->get_image_size_array_from_meta( $image_meta, $int_size ); - if ( 'full' === $int ) { + if ( 'full' === $int_size ) { // Add the full size image. Expected to be in the srcset when the full size image is used as src. $_expected = $uploads_dir_url . $full_size_file . ' ' . $image_meta['width'] . 'w, ' . $expected; } else {