Skip to content

Commit 2118cf0

Browse files
committed
Enhance test case by adding temporary attachment and custom image size management
1 parent f645402 commit 2118cf0

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

plugins/webp-uploads/tests/test-picture-element.php

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ class Test_WebP_Uploads_Picture_Element extends TestCase {
2525
*/
2626
public static $image_id;
2727

28+
/**
29+
* Temporary attachment IDs created during tests.
30+
*
31+
* @var array<int>
32+
*/
33+
private $temp_attachment_ids = array();
34+
35+
/**
36+
* Temporary custom image sizes created during tests.
37+
*
38+
* @var array<string>
39+
*/
40+
private $temp_custom_image_sizes = array();
41+
2842
public function set_up(): void {
2943
parent::set_up();
3044

@@ -36,6 +50,24 @@ public function set_up(): void {
3650
$this->mock_frontend_body_hooks();
3751
}
3852

53+
public function tear_down(): void {
54+
// Clean up any attachments created during tests.
55+
foreach ( $this->temp_attachment_ids as $id ) {
56+
wp_delete_attachment( $id, true );
57+
}
58+
$this->temp_attachment_ids = array();
59+
60+
// Clean up custom image sizes.
61+
foreach ( $this->temp_custom_image_sizes as $size ) {
62+
if ( has_image_size( $size ) ) {
63+
remove_image_size( $size );
64+
}
65+
}
66+
$this->temp_custom_image_sizes = array();
67+
68+
parent::tear_down();
69+
}
70+
3971
/**
4072
* Setup shared fixtures.
4173
*/
@@ -270,20 +302,27 @@ function ( string $content ): string {
270302
* @dataProvider data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset
271303
* @covers ::webp_uploads_wrap_image_in_picture
272304
*
273-
* @param Closure|null $set_up Set up the test.
274-
* @param Closure|null $tear_down Tear down the test.
305+
* @param Closure|null $set_up Set up the test.
306+
* @param array<string>|null $custom_sizes Custom image sizes.
275307
*/
276-
public function test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset( ?Closure $set_up, ?Closure $tear_down ): void {
308+
public function test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset( ?Closure $set_up, ?array $custom_sizes ): void {
277309
if ( $set_up instanceof Closure ) {
278310
$set_up();
279311
}
280312

313+
if ( is_array( $custom_sizes ) ) {
314+
foreach ( $custom_sizes as $size ) {
315+
$this->temp_custom_image_sizes[] = $size;
316+
}
317+
}
318+
281319
update_option( 'perflab_generate_webp_and_jpeg', '1' );
282320
update_option( 'perflab_generate_all_fallback_sizes', '1' );
283321

284322
// Create image with different sizes.
285323
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' );
286324
$this->assertNotWPError( $attachment_id );
325+
$this->temp_attachment_ids[] = $attachment_id;
287326

288327
$image = wp_get_attachment_image(
289328
$attachment_id,
@@ -327,48 +366,36 @@ public function test_picture_element_source_tag_srcset_has_same_image_sizes_as_i
327366
// e.g. $img_file = 'leaves-350x200' and $source_files[ $img_index ] = 'leaves-350x350-jpg'.
328367
$this->assertStringStartsWith( $img_file, $source_files[ $img_index ] );
329368
}
330-
331-
wp_delete_attachment( $attachment_id, true );
332-
333-
if ( $tear_down instanceof Closure ) {
334-
$tear_down();
335-
}
336369
}
337370

338371
/**
339372
* Data provider for test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset.
340373
*
341-
* @return array<string, array{ set_up: Closure|null, tear_down: Closure|null }>
374+
* @return array<string, array{ set_up: Closure|null, custom_sizes: array<string>|null }>
342375
*/
343376
public function data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset(): array {
344377
return array(
345378
'default_sizes' => array(
346-
'set_up' => null,
347-
'tear_down' => null,
379+
'set_up' => null,
380+
'custom_sizes' => null,
348381
),
349382
'when_two_different_image_sizes_have_same_width' => array(
350-
'set_up' => static function (): void {
383+
'set_up' => static function (): void {
351384
add_image_size( 'square', 768, 768, true );
352-
remove_all_filters( 'pre_option_medium_large_size_w' );
353385
add_filter(
354386
'pre_option_medium_large_size_w',
355387
static function () {
356388
return '768';
357389
}
358390
);
359-
remove_all_filters( 'pre_option_medium_large_size_h' );
360391
add_filter(
361392
'pre_option_medium_large_size_h',
362393
static function () {
363394
return '512';
364395
}
365396
);
366397
},
367-
'tear_down' => static function (): void {
368-
remove_image_size( 'square' );
369-
remove_all_filters( 'pre_option_medium_large_size_w' );
370-
remove_all_filters( 'pre_option_medium_large_size_h' );
371-
},
398+
'custom_sizes' => array( 'square' ),
372399
),
373400
);
374401
}

0 commit comments

Comments
 (0)