Skip to content

Commit 928a872

Browse files
committed
Add test to ensure picture element source tag srcset image sizes matches img tag srcset image sizes
1 parent bfd7d2b commit 928a872

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,115 @@ function ( string $content ): string {
264264
);
265265
}
266266

267+
/**
268+
* Test that the picture element source tag srcset has the same image sizes as the img tag srcset.
269+
*
270+
* @dataProvider data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset
271+
* @covers ::webp_uploads_wrap_image_in_picture
272+
*
273+
* @param Closure|null $set_up Set up the test.
274+
* @param Closure|null $tear_down Tear down the test.
275+
*/
276+
public function test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset( ?Closure $set_up, ?Closure $tear_down ): void {
277+
if ( $set_up instanceof Closure ) {
278+
$set_up();
279+
}
280+
281+
update_option( 'perflab_generate_webp_and_jpeg', '1' );
282+
update_option( 'perflab_generate_all_fallback_sizes', '1' );
283+
284+
// Create image with different sizes.
285+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' );
286+
$this->assertNotWPError( $attachment_id );
287+
288+
$image = wp_get_attachment_image(
289+
$attachment_id,
290+
'large',
291+
false,
292+
array(
293+
'class' => 'wp-image-' . $attachment_id,
294+
'alt' => 'Green Leaves',
295+
)
296+
);
297+
298+
// Apply picture element support.
299+
$this->opt_in_to_picture_element();
300+
301+
$picture_markup = apply_filters( 'the_content', $image );
302+
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
303+
$picture_processor->next_tag( array( 'tag_name' => 'SOURCE' ) );
304+
$source_srcset = $picture_processor->get_attribute( 'srcset' );
305+
$picture_processor->next_tag( array( 'tag_name' => 'IMG' ) );
306+
$img_srcset = $picture_processor->get_attribute( 'srcset' );
307+
308+
// Extract file names from srcset.
309+
$extract_file_names_from_srcset = static function ( string $srcset ): array {
310+
$srcset_parts = explode( ', ', $srcset );
311+
$files = array();
312+
foreach ( $srcset_parts as $srcset_part ) {
313+
$parts = explode( ' ', trim( $srcset_part ) );
314+
$url = $parts[0];
315+
$files[] = pathinfo( basename( $url ), PATHINFO_FILENAME );
316+
}
317+
return $files;
318+
};
319+
320+
$source_files = $extract_file_names_from_srcset( $source_srcset );
321+
$img_files = $extract_file_names_from_srcset( $img_srcset );
322+
323+
$this->assertCount( count( $source_files ), $img_files );
324+
325+
foreach ( $img_files as $img_index => $img_file ) {
326+
// Check the file name starts with the same prefix.
327+
// e.g. $img_file = 'leaves-350x200' and $source_files[ $img_index ] = 'leaves-350x350-jpg'.
328+
$this->assertStringStartsWith( $img_file, $source_files[ $img_index ] );
329+
}
330+
331+
wp_delete_attachment( $attachment_id, true );
332+
333+
if ( $tear_down instanceof Closure ) {
334+
$tear_down();
335+
}
336+
}
337+
338+
/**
339+
* Data provider for test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset.
340+
*
341+
* @return array<string, array{ set_up: Closure|null, tear_down: Closure|null }>
342+
*/
343+
public function data_provider_test_picture_element_source_tag_srcset_has_same_image_sizes_as_img_tag_srcset(): array {
344+
return array(
345+
'default_sizes' => array(
346+
'set_up' => null,
347+
'tear_down' => null,
348+
),
349+
'when_two_different_image_sizes_have_same_width' => array(
350+
'set_up' => static function (): void {
351+
add_image_size( 'square', 768, 768, true );
352+
remove_all_filters( 'pre_option_medium_large_size_w' );
353+
add_filter(
354+
'pre_option_medium_large_size_w',
355+
static function () {
356+
return '768';
357+
}
358+
);
359+
remove_all_filters( 'pre_option_medium_large_size_h' );
360+
add_filter(
361+
'pre_option_medium_large_size_h',
362+
static function () {
363+
return '512';
364+
}
365+
);
366+
},
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+
},
372+
),
373+
);
374+
}
375+
267376
/**
268377
* @dataProvider data_provider_test_disable_responsive_image_with_picture_element
269378
*
@@ -376,6 +485,8 @@ public function test_picture_source_should_have_full_size_image_in_its_srcset():
376485

377486
/**
378487
* Test handling of case when wp_get_attachment_image_src returns false.
488+
*
489+
* @covers ::webp_uploads_wrap_image_in_picture
379490
*/
380491
public function test_wrap_image_in_picture_with_false_image_src(): void {
381492
$this->opt_in_to_picture_element();

0 commit comments

Comments
 (0)