@@ -25,6 +25,20 @@ class Test_WebP_Uploads_Picture_Element extends TestCase {
25
25
*/
26
26
public static $ image_id ;
27
27
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
+
28
42
public function set_up (): void {
29
43
parent ::set_up ();
30
44
@@ -36,6 +50,24 @@ public function set_up(): void {
36
50
$ this ->mock_frontend_body_hooks ();
37
51
}
38
52
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
+
39
71
/**
40
72
* Setup shared fixtures.
41
73
*/
@@ -270,20 +302,27 @@ function ( string $content ): string {
270
302
* @dataProvider data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset
271
303
* @covers ::webp_uploads_wrap_image_in_picture
272
304
*
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 .
275
307
*/
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 {
277
309
if ( $ set_up instanceof Closure ) {
278
310
$ set_up ();
279
311
}
280
312
313
+ if ( is_array ( $ custom_sizes ) ) {
314
+ foreach ( $ custom_sizes as $ size ) {
315
+ $ this ->temp_custom_image_sizes [] = $ size ;
316
+ }
317
+ }
318
+
281
319
update_option ( 'perflab_generate_webp_and_jpeg ' , '1 ' );
282
320
update_option ( 'perflab_generate_all_fallback_sizes ' , '1 ' );
283
321
284
322
// Create image with different sizes.
285
323
$ attachment_id = self ::factory ()->attachment ->create_upload_object ( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg ' );
286
324
$ this ->assertNotWPError ( $ attachment_id );
325
+ $ this ->temp_attachment_ids [] = $ attachment_id ;
287
326
288
327
$ image = wp_get_attachment_image (
289
328
$ attachment_id ,
@@ -327,48 +366,36 @@ public function test_picture_element_source_tag_srcset_has_same_image_sizes_as_i
327
366
// e.g. $img_file = 'leaves-350x200' and $source_files[ $img_index ] = 'leaves-350x350-jpg'.
328
367
$ this ->assertStringStartsWith ( $ img_file , $ source_files [ $ img_index ] );
329
368
}
330
-
331
- wp_delete_attachment ( $ attachment_id , true );
332
-
333
- if ( $ tear_down instanceof Closure ) {
334
- $ tear_down ();
335
- }
336
369
}
337
370
338
371
/**
339
372
* Data provider for test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset.
340
373
*
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 }>
342
375
*/
343
376
public function data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset (): array {
344
377
return array (
345
378
'default_sizes ' => array (
346
- 'set_up ' => null ,
347
- 'tear_down ' => null ,
379
+ 'set_up ' => null ,
380
+ 'custom_sizes ' => null ,
348
381
),
349
382
'when_two_different_image_sizes_have_same_width ' => array (
350
- 'set_up ' => static function (): void {
383
+ 'set_up ' => static function (): void {
351
384
add_image_size ( 'square ' , 768 , 768 , true );
352
- remove_all_filters ( 'pre_option_medium_large_size_w ' );
353
385
add_filter (
354
386
'pre_option_medium_large_size_w ' ,
355
387
static function () {
356
388
return '768 ' ;
357
389
}
358
390
);
359
- remove_all_filters ( 'pre_option_medium_large_size_h ' );
360
391
add_filter (
361
392
'pre_option_medium_large_size_h ' ,
362
393
static function () {
363
394
return '512 ' ;
364
395
}
365
396
);
366
397
},
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 ' ),
372
399
),
373
400
);
374
401
}
0 commit comments