-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathtest-picture-element.php
More file actions
759 lines (660 loc) · 27.5 KB
/
Copy pathtest-picture-element.php
File metadata and controls
759 lines (660 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
<?php
/**
* Tests for webp-uploads plugin picture-element.php.
*
* @group picture-element
*
* @package webp-uploads
*/
use WebP_Uploads\Tests\TestCase;
class Test_WebP_Uploads_Picture_Element extends TestCase {
/**
* Mime type.
*
* @var string
*/
public static $mime_type = 'image/webp';
/**
* Attachment ID.
*
* @var int
*/
public static $image_id;
/**
* Temporary attachment IDs created during tests.
*
* @var array<int>
*/
private $temp_attachment_ids = array();
/**
* Temporary custom image sizes created during tests.
*
* @var array<string>
*/
private $temp_custom_image_sizes = array();
public function set_up(): void {
parent::set_up();
if ( ! wp_image_editor_supports( array( 'mime_type' => self::$mime_type ) ) ) {
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
}
// Run critical hooks to satisfy webp_uploads_in_frontend_body() conditions.
$this->mock_frontend_body_hooks();
// Many tests in this class rely on `wp_get_attachment_image()` returning
// unfiltered markup so they can exercise the `wp_content_img_tag` path
// manually. Remove the new auto-rewriter by default; tests that want to
// exercise it explicitly can re-register via `opt_in_to_picture_element()`
// (which re-runs `webp_uploads_init()`).
remove_filter( 'wp_get_attachment_image', 'webp_uploads_filter_wp_get_attachment_image', 10 );
}
public function tear_down(): void {
// Clean up any attachments created during tests.
foreach ( $this->temp_attachment_ids as $id ) {
wp_delete_attachment( $id, true );
}
$this->temp_attachment_ids = array();
// Clean up custom image sizes.
foreach ( $this->temp_custom_image_sizes as $size ) {
if ( has_image_size( $size ) ) {
remove_image_size( $size );
}
}
$this->temp_custom_image_sizes = array();
parent::tear_down();
}
/**
* Setup shared fixtures.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
// Default to webp output for tests.
update_option( 'perflab_modern_image_format', 'webp' );
// Fallback to JPEG IMG.
update_option( 'perflab_generate_webp_and_jpeg', '1' );
self::$image_id = $factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' );
}
public static function wpTearDownAfterClass(): void {
wp_delete_attachment( self::$image_id, true );
delete_option( 'perflab_generate_webp_and_jpeg' );
delete_option( 'perflab_modern_image_format' );
}
/**
* Test that images are wrapped in picture element when enabled.
*
* @dataProvider data_provider_it_should_maybe_wrap_images_in_picture_element
*
* @param bool $fallback_jpeg Whether to fallback JPEG output.
* @param bool $picture_element Whether to enable picture element output.
* @param string $expected_html The expected HTML output.
*/
public function test_maybe_wrap_images_in_picture_element( bool $fallback_jpeg, bool $picture_element, string $expected_html ): void {
update_option( 'perflab_generate_webp_and_jpeg', $fallback_jpeg );
// Apply picture element support.
if ( $picture_element ) {
$this->opt_in_to_picture_element();
// `opt_in_to_picture_element()` re-runs webp_uploads_init(), which
// re-registers the new wp_get_attachment_image rewriter. Remove it
// again so this test exercises the wp_content_img_tag path only.
remove_filter( 'wp_get_attachment_image', 'webp_uploads_filter_wp_get_attachment_image', 10 );
}
// Create some content with the image.
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
'loading' => false,
'fetchpriority' => false,
)
);
$processor = new WP_HTML_Tag_Processor( $image );
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'IMG' ) ) );
$width = (int) $processor->get_attribute( 'width' );
$height = (int) $processor->get_attribute( 'height' );
$alt = (string) $processor->get_attribute( 'alt' );
$size_to_use = ( $width > 0 && $height > 0 ) ? array( $width, $height ) : 'full';
$image_src = wp_get_attachment_image_src( self::$image_id, $size_to_use );
list( $src, $width, $height ) = $image_src;
$size_array = array( absint( $width ), absint( $height ) );
$image_meta = wp_get_attachment_metadata( self::$image_id );
$sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, self::$image_id );
$image_srcset = wp_get_attachment_image_srcset( self::$image_id, $size_to_use );
$img_src = '';
if ( is_array( $image_src ) ) {
$img_src = $image_src[0];
}
$webp_srcset = str_replace( '.jpg', '-jpg.webp', $image_srcset );
// Prepare the expected HTML by replacing placeholders with expected values.
$replacements = array(
'{{img-width}}' => $width,
'{{img-height}}' => $height,
'{{img-src}}' => $img_src,
'{{img-attachment-id}}' => self::$image_id,
'{{img-alt}}' => $alt,
'{{img-srcset}}' => $image_srcset,
'{{img-sizes}}' => $sizes,
'{{webp-srcset}}' => $webp_srcset,
);
$expected_html = str_replace( array_keys( $replacements ), array_values( $replacements ), $expected_html );
if ( webp_uploads_is_picture_element_enabled() ) {
// Apply the wp_content_img_tag filter.
$image = apply_filters( 'wp_content_img_tag', $image, 'the_content', self::$image_id );
}
// Check that the image has the expected HTML.
$this->assertEquals( $expected_html, $image );
}
/**
* Data provider for it_should_maybe_wrap_images_in_picture_element.
*
* @return array<string, array<string, bool|string>>
*/
public function data_provider_it_should_maybe_wrap_images_in_picture_element(): array {
return array(
'jpeg and picture enabled' => array(
'fallback_jpeg' => true,
'picture_element' => true,
'expected_html' => '<picture class="wp-picture-{{img-attachment-id}}" style="display: contents;"><source type="image/webp" srcset="{{webp-srcset}}" sizes="{{img-sizes}}"><img data-wp-picture-wrapped width="{{img-width}}" height="{{img-height}}" src="{{img-src}}" class="wp-image-{{img-attachment-id}}" alt="{{img-alt}}" decoding="async" srcset="{{img-srcset}}" sizes="{{img-sizes}}" /></picture>',
),
'only picture enabled' => array(
'fallback_jpeg' => false,
'picture_element' => true,
'expected_html' => '<img width="{{img-width}}" height="{{img-height}}" src="{{img-src}}" class="wp-image-{{img-attachment-id}}" alt="{{img-alt}}" decoding="async" srcset="{{img-srcset}}" sizes="{{img-sizes}}" />',
),
'only jpeg enabled' => array(
'fallback_jpeg' => true,
'picture_element' => false,
'expected_html' => '<img width="{{img-width}}" height="{{img-height}}" src="{{img-src}}" class="wp-image-{{img-attachment-id}}" alt="{{img-alt}}" decoding="async" srcset="{{img-srcset}}" sizes="{{img-sizes}}" />',
),
'neither enabled' => array(
'fallback_jpeg' => false,
'picture_element' => false,
'expected_html' => '<img width="{{img-width}}" height="{{img-height}}" src="{{img-src}}" class="wp-image-{{img-attachment-id}}" alt="{{img-alt}}" decoding="async" srcset="{{img-srcset}}" sizes="{{img-sizes}}" />',
),
);
}
public function test_picture_source_only_have_additional_mime_not_jpeg_and_return_jpeg_fallback(): void {
// Create some content with the image.
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
$img_markup = apply_filters( 'the_content', $image );
$img_processor = new WP_HTML_Tag_Processor( $img_markup );
$this->assertTrue( $img_processor->next_tag( array( 'tag_name' => 'IMG' ) ), 'There should be an IMG tag.' );
$img_src = $img_processor->get_attribute( 'src' );
$this->assertStringEndsWith( '.webp', $img_src, 'Make sure the IMG should return WEBP src.' );
$img_srcset = $img_processor->get_attribute( 'srcset' );
$this->assertStringContainsString( '.webp', $img_srcset, 'Make sure the IMG srcset should return WEBP images.' );
// Apply picture element support.
$this->opt_in_to_picture_element();
$picture_markup = apply_filters( 'the_content', $image );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
$picture_processor->next_tag( array( 'tag_name' => 'IMG' ) );
// The fallback image should be JPEG.
$this->assertStringEndsWith( '.jpg', $picture_processor->get_attribute( 'src' ), 'Make sure the fallback IMG should return JPEG src.' );
$this->assertStringContainsString( '.jpg', $picture_processor->get_attribute( 'srcset' ), 'Make sure the IMG srcset should return JPEG images.' );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
while ( $picture_processor->next_tag( array( 'tag_name' => 'source' ) ) ) {
$this->assertSame( self::$mime_type, $picture_processor->get_attribute( 'type' ), 'Make sure the Picture source should not return JPEG as source.' );
$this->assertStringContainsString( '.webp', $picture_processor->get_attribute( 'srcset' ), 'Make sure the Picture source srcset should return WEBP images.' );
}
}
/**
* @dataProvider data_provider_test_image_sizes_equality
*
* @param Closure|null $add_filter The filter.
*/
public function test_img_sizes_is_equal_to_picture_source_sizes_for_picture_element( ?Closure $add_filter ): void {
// Create some content with the image.
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
if ( $add_filter instanceof Closure ) {
$add_filter();
}
$img_markup = apply_filters( 'the_content', $image );
$img_processor = new WP_HTML_Tag_Processor( $img_markup );
$this->assertTrue( $img_processor->next_tag( array( 'tag_name' => 'IMG' ) ), 'There should be an IMG tag.' );
$img_sizes = $img_processor->get_attribute( 'sizes' );
// Apply picture element support.
$this->opt_in_to_picture_element();
$picture_markup = apply_filters( 'the_content', $image );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
$picture_processor->next_tag( array( 'tag_name' => 'IMG' ) );
$this->assertSame( $img_sizes, $picture_processor->get_attribute( 'sizes' ), 'The IMG and Picture IMG have same sizes attributes.' );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
while ( $picture_processor->next_tag( array( 'tag_name' => 'source' ) ) ) {
$this->assertSame( $img_sizes, $picture_processor->get_attribute( 'sizes' ), 'The IMG and Picture source have same sizes attributes.' );
}
}
/**
* Data provider for it_should_maybe_wrap_images_in_picture_element.
*
* @return array<string, array{ add_filter: Closure|null }>
*/
public function data_provider_test_image_sizes_equality(): array {
return array(
'no_filter' => array(
'add_filter' => null,
),
'with_filter' => array(
'add_filter' => function (): void {
add_filter(
'wp_content_img_tag',
function ( string $content ): string {
$processor = new WP_HTML_Tag_Processor( $content );
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'img' ) ) );
$processor->set_attribute( 'sizes', '(max-width: 333px) 100vw, 333px' );
return $processor->get_updated_html();
}
);
},
),
);
}
/**
* Test that the picture element source tag srcset has the same image sizes as the img tag srcset.
*
* @dataProvider data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset
* @covers ::webp_uploads_wrap_image_in_picture
*
* @param Closure|null $set_up Set up the test.
*/
public function test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset( ?Closure $set_up ): void {
if ( $set_up instanceof Closure ) {
$set_up();
}
update_option( 'perflab_generate_webp_and_jpeg', '1' );
update_option( 'perflab_generate_all_fallback_sizes', '1' );
// Create image with different sizes.
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' );
$this->assertNotWPError( $attachment_id );
$this->temp_attachment_ids[] = $attachment_id;
$image = wp_get_attachment_image(
$attachment_id,
'large',
false,
array(
'class' => 'wp-image-' . $attachment_id,
'alt' => 'Green Leaves',
)
);
// Apply picture element support.
$this->opt_in_to_picture_element();
$picture_markup = apply_filters( 'the_content', $image );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
$picture_processor->next_tag( array( 'tag_name' => 'SOURCE' ) );
$source_srcset = $picture_processor->get_attribute( 'srcset' );
$picture_processor->next_tag( array( 'tag_name' => 'IMG' ) );
$img_srcset = $picture_processor->get_attribute( 'srcset' );
// Extract file names from srcset.
$extract_file_names_from_srcset = static function ( string $srcset ): array {
$srcset_parts = explode( ', ', $srcset );
$files = array();
foreach ( $srcset_parts as $srcset_part ) {
$parts = explode( ' ', trim( $srcset_part ) );
$url = $parts[0];
$files[] = pathinfo( basename( $url ), PATHINFO_FILENAME );
}
return $files;
};
$source_files = $extract_file_names_from_srcset( $source_srcset );
$img_files = $extract_file_names_from_srcset( $img_srcset );
$this->assertCount( count( $source_files ), $img_files );
foreach ( $img_files as $img_index => $img_file ) {
// Check the file name starts with the same prefix.
// e.g. $img_file = 'leaves-350x200' and $source_files[ $img_index ] = 'leaves-350x350-jpg'.
$this->assertStringStartsWith( $img_file, $source_files[ $img_index ] );
}
}
/**
* Data provider for test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset.
*
* @return array<string, array{ set_up: Closure|null }>
*/
public function data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset(): array {
return array(
'default_sizes' => array(
'set_up' => null,
),
'when_two_different_image_sizes_have_same_width' => array(
'set_up' => function (): void {
add_image_size( 'square', 768, 768, true );
$this->temp_custom_image_sizes[] = 'square';
add_filter(
'pre_option_medium_large_size_w',
static function () {
return '768';
}
);
add_filter(
'pre_option_medium_large_size_h',
static function () {
return '512';
}
);
},
),
);
}
/**
* @dataProvider data_provider_test_disable_responsive_image_with_picture_element
*
* @param Closure $set_up Set up the filter.
*/
public function test_disable_responsive_image_with_picture_element( Closure $set_up ): void {
// Disable responsive images.
$set_up();
// Create some content with the image.
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
$img_markup = apply_filters( 'the_content', $image );
$img_processor = new WP_HTML_Tag_Processor( $img_markup );
$this->assertTrue( $img_processor->next_tag( array( 'tag_name' => 'IMG' ) ), 'There should be an IMG tag.' );
$img_src = $img_processor->get_attribute( 'src' );
$this->assertStringEndsWith( '.webp', $img_src, 'Make sure the IMG should return WEBP src.' );
$this->assertNull( $img_processor->get_attribute( 'sizes' ), 'Make sure that there is no sizes attribute in IMG tag.' );
$this->assertNull( $img_processor->get_attribute( 'srcset' ), 'Make sure that there is no srcset attribute in IMG tag.' );
// Apply picture element support.
$this->opt_in_to_picture_element();
$picture_markup = apply_filters( 'the_content', $image );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
$this->assertTrue( $picture_processor->next_tag( array( 'tag_name' => 'PICTURE' ) ), 'Make sure that there is a PICTURE tag.' );
$this->assertTrue( $picture_processor->next_tag( array( 'tag_name' => 'IMG' ) ), 'Make sure that there is a IMG tag.' );
$this->assertNull( $picture_processor->get_attribute( 'sizes' ), 'Make sure that there is no sizes attribute in IMG tag.' );
$this->assertNull( $picture_processor->get_attribute( 'srcset' ), 'Make sure that there is no srcset attribute in IMG tag.' );
// The fallback image should be JPEG.
$this->assertStringEndsWith( '.jpg', $picture_processor->get_attribute( 'src' ), 'Make sure the fallback IMG should return JPEG src.' );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
while ( $picture_processor->next_tag( array( 'tag_name' => 'source' ) ) ) {
$this->assertSame( self::$mime_type, $picture_processor->get_attribute( 'type' ), 'Make sure the Picture source should not return JPEG as source.' );
$this->assertStringContainsString( '.webp', $picture_processor->get_attribute( 'srcset' ), 'Make sure the Picture source srcset should return WEBP images.' );
}
}
/**
* Data provider for it_should_maybe_wrap_images_in_picture_element.
*
* @return array<string, array{ add_filter: Closure }>
*/
public function data_provider_test_disable_responsive_image_with_picture_element(): array {
return array(
'no_sizes' => array(
'add_filter' => static function (): void {
add_filter( 'wp_calculate_image_sizes', '__return_false' );
},
),
'no_srcset' => array(
'add_filter' => static function (): void {
add_filter( 'wp_calculate_image_srcset', '__return_false' );
},
),
);
}
public function test_picture_source_should_have_full_size_image_in_its_srcset(): void {
// Create some content with the image.
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
$image_meta = wp_get_attachment_metadata( self::$image_id );
$img_markup = apply_filters( 'the_content', $image );
$img_processor = new WP_HTML_Tag_Processor( $img_markup );
$this->assertTrue( $img_processor->next_tag( array( 'tag_name' => 'IMG' ) ), 'There should be an IMG tag.' );
$this->assertStringEndsWith( '.webp', $img_processor->get_attribute( 'src' ), 'Make sure the IMG should return WEBP src.' );
$this->assertStringContainsString( $image_meta['sources'][ self::$mime_type ]['file'], $img_processor->get_attribute( 'srcset' ), 'Make sure the IMG srcset should have full size image.' );
// Apply picture element support.
$this->opt_in_to_picture_element();
$picture_markup = apply_filters( 'the_content', $image );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
$picture_processor->next_tag( array( 'tag_name' => 'IMG' ) );
// The fallback image should be JPEG.
$this->assertStringEndsWith( '.jpg', $picture_processor->get_attribute( 'src' ), 'Make sure the fallback IMG should return JPEG src.' );
$this->assertStringContainsString( $image_meta['sources']['image/jpeg']['file'], $picture_processor->get_attribute( 'srcset' ), 'Make sure the IMG srcset should have full size image.' );
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
while ( $picture_processor->next_tag( array( 'tag_name' => 'source' ) ) ) {
$this->assertSame( self::$mime_type, $picture_processor->get_attribute( 'type' ), 'Make sure the Picture source should not return JPEG as source.' );
$this->assertStringContainsString( $image_meta['sources'][ self::$mime_type ]['file'], $picture_processor->get_attribute( 'srcset' ), 'Make sure the IMG srcset should have full size image.' );
}
}
/**
* Test handling of case when wp_get_attachment_image_src returns false.
*
* @covers ::webp_uploads_wrap_image_in_picture
*/
public function test_wrap_image_in_picture_with_false_image_src(): void {
$this->opt_in_to_picture_element();
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
add_filter( 'wp_get_attachment_image_src', '__return_false' );
$filtered_image = apply_filters( 'wp_content_img_tag', $image, 'the_content', self::$image_id );
remove_filter( 'wp_get_attachment_image_src', '__return_false' );
$this->assertSame( $image, $filtered_image );
}
/**
* Test that images are not wrapped in picture element for unsupported contexts.
*
* @dataProvider data_provider_webp_uploads_wrap_image_in_picture_with_different_context
*
* @param string $context The context to test.
* @param bool $expected Whether the image should be wrapped in a picture element.
*
* @covers ::webp_uploads_wrap_image_in_picture
*/
public function test_webp_uploads_wrap_image_in_picture_with_different_context( string $context, bool $expected ): void {
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
$this->opt_in_to_picture_element();
$filtered_image = apply_filters( 'wp_content_img_tag', $image, $context, self::$image_id );
if ( $expected ) {
$processor = new WP_HTML_Tag_Processor( $filtered_image );
$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'PICTURE', $processor->get_tag() );
$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'SOURCE', $processor->get_tag() );
$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'IMG', $processor->get_tag() );
} else {
$this->assertSame( $image, $filtered_image );
}
}
/**
* Data provider for test_webp_uploads_wrap_image_in_picture_with_different_context.
*
* @return array<string, array{ context: string, expected: bool }>
*/
public function data_provider_webp_uploads_wrap_image_in_picture_with_different_context(): array {
return array(
'the_content' =>
array(
'context' => 'the_content',
'expected' => true,
),
'post_thumbnail_html' =>
array(
'context' => 'post_thumbnail_html',
'expected' => true,
),
'widget_block_content' =>
array(
'context' => 'widget_block_content',
'expected' => true,
),
'wp_get_attachment_image' =>
array(
'context' => 'wp_get_attachment_image',
'expected' => true,
),
'invalid_context' =>
array(
'context' => 'invalid_context',
'expected' => false,
),
);
}
/**
* `wp_get_attachment_image()` should return a <picture>-wrapped image when
* picture-element output is enabled, because the new filter dispatches to
* `webp_uploads_wrap_image_in_picture()`.
*
* @covers ::webp_uploads_filter_wp_get_attachment_image
* @covers ::webp_uploads_wrap_image_in_picture
*/
public function test_wp_get_attachment_image_is_wrapped_in_picture_when_picture_element_enabled(): void {
$this->opt_in_to_picture_element();
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
$this->assertStringStartsWith( '<picture ', $image );
$this->assertStringContainsString( '<source type="image/webp"', $image );
}
/**
* `webp_uploads_wrap_image_in_picture()` must be idempotent: wrapping an
* already-wrapped string should be a no-op. Otherwise the same markup
* passing through both `wp_get_attachment_image` and `wp_content_img_tag`
* would end up double-wrapped.
*
* @covers ::webp_uploads_wrap_image_in_picture
*/
public function test_wrap_image_in_picture_is_idempotent(): void {
$this->opt_in_to_picture_element();
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
$twice = webp_uploads_wrap_image_in_picture( $image, 'wp_get_attachment_image', self::$image_id );
$this->assertSame( $image, $twice, 'Re-wrapping an already-wrapped picture should return the input unchanged.' );
$this->assertSame( 1, substr_count( $twice, '<picture ' ) );
}
/**
* A `<picture>` produced for a `wp_get_attachment_image()` call that is then
* embedded in post content must not be wrapped a second time when the content
* runs through `the_content` (`wp_filter_content_tags()` -> `wp_content_img_tag`).
*
* Regression test: `wp_filter_content_tags()` extracts only the inner `<img>`
* substring, so the `stripos( $image, '<picture' )` guard alone never sees the
* surrounding wrapper. The `data-wp-picture-wrapped` marker closes that gap.
*
* @covers ::webp_uploads_wrap_image_in_picture
* @covers ::webp_uploads_filter_wp_get_attachment_image
*/
public function test_wp_get_attachment_image_in_content_is_not_double_wrapped(): void {
$this->opt_in_to_picture_element();
// `wp_get_attachment_image()` now returns a `<picture>`; the inner `<img>`
// keeps the `wp-image-{ID}` class so `wp_filter_content_tags()` can resolve
// the attachment on the content pass.
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array(
'class' => 'wp-image-' . self::$image_id,
'alt' => 'Green Leaves',
)
);
$this->assertStringStartsWith( '<picture ', $image, 'Precondition: the image should be wrapped once.' );
// Simulate the markup being placed into post content and rendered.
$rendered = apply_filters( 'the_content', $image );
$this->assertSame(
1,
substr_count( $rendered, '<picture' ),
'An image embedded in content must be wrapped in exactly one <picture> element.'
);
$this->assertSame(
1,
substr_count( $rendered, '<img ' ),
'Exactly one <img> element should remain after the content pass.'
);
}
/**
* `webp_uploads_wrap_image_in_picture()` must not emit an empty `<picture>`
* wrapper (one with no `<source>` children) when no modern-format source can
* be resolved for the attachment.
*
* @covers ::webp_uploads_wrap_image_in_picture
*/
public function test_wrap_image_in_picture_skips_empty_wrapper_when_no_sources(): void {
$this->opt_in_to_picture_element();
// Let the first `wp_calculate_image_srcset()` call (core building the
// `<img>`) succeed so the tag keeps its `srcset`/`sizes`, then force every
// later call (the per-mime-type lookups inside the wrapper) to come back
// empty, leaving no `<source>` to emit.
$call = 0;
add_filter(
'wp_calculate_image_srcset',
static function ( $sources ) use ( &$call ) {
++$call;
return $call > 1 ? array() : $sources;
}
);
$image = wp_get_attachment_image(
self::$image_id,
'large',
false,
array( 'class' => 'wp-image-' . self::$image_id )
);
$this->assertStringNotContainsString( '<picture', $image, 'No <picture> wrapper should be emitted when there are no <source> elements.' );
$this->assertStringStartsWith( '<img ', $image );
}
/**
* The deprecated `webp_uploads_update_featured_image()` shim must still exist
* and return rewritten markup, so third-party callers do not fatal-error.
*
* @expectedDeprecated webp_uploads_update_featured_image
* @covers ::webp_uploads_update_featured_image
*/
public function test_webp_uploads_update_featured_image_is_deprecated_but_functional(): void {
$this->assertTrue( function_exists( 'webp_uploads_update_featured_image' ) );
$html = wp_get_attachment_image( self::$image_id, 'large', false, array( 'class' => 'wp-image-' . self::$image_id ) );
// @phpstan-ignore function.deprecated (Intentionally exercising the deprecated shim.)
$result = webp_uploads_update_featured_image( $html, 0, self::$image_id );
$this->assertStringContainsString( '<img ', $result );
}
}