Skip to content

Commit 1fd3a4d

Browse files
committed
Add tests to ensure bugs are fixed
1 parent 3539959 commit 1fd3a4d

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

plugins/webp-uploads/tests/test-load.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,4 +1268,44 @@ static function () {
12681268
);
12691269
$this->assertSameSets( $file, webp_uploads_convert_palette_png_to_truecolor( $file ) );
12701270
}
1271+
1272+
/**
1273+
* Test that the webp_uploads_update_featured_image function is hooked to the post_thumbnail_html filter.
1274+
*
1275+
* @covers ::webp_uploads_update_featured_image
1276+
*/
1277+
public function test_webp_uploads_update_featured_image_hooked_into_post_thumbnail_html(): void {
1278+
$this->assertSame( 10, has_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image' ) );
1279+
}
1280+
1281+
/**
1282+
* Test that the featured image is not wrapped in a picture element.
1283+
*
1284+
* @covers ::webp_uploads_update_featured_image
1285+
*/
1286+
public function test_webp_uploads_update_featured_image_picture_element_disabled(): void {
1287+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
1288+
$post_id = self::factory()->post->create();
1289+
set_post_thumbnail( $post_id, $attachment_id );
1290+
1291+
$featured_image = get_the_post_thumbnail( $post_id );
1292+
$this->assertStringStartsWith( '<img ', $featured_image );
1293+
}
1294+
1295+
/**
1296+
* Test that the featured image is wrapped in a picture element.
1297+
*
1298+
* @covers ::webp_uploads_update_featured_image
1299+
*/
1300+
public function test_webp_uploads_update_featured_image_picture_element_enabled(): void {
1301+
update_option( 'perflab_generate_webp_and_jpeg', '1' );
1302+
$this->opt_in_to_picture_element();
1303+
1304+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
1305+
$post_id = self::factory()->post->create();
1306+
set_post_thumbnail( $post_id, $attachment_id );
1307+
1308+
$featured_image = get_the_post_thumbnail( $post_id );
1309+
$this->assertStringStartsWith( '<picture ', $featured_image );
1310+
}
12711311
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,4 +526,62 @@ public function test_wrap_image_in_picture_with_false_image_src(): void {
526526

527527
$this->assertSame( $image, $filtered_image );
528528
}
529+
530+
/**
531+
* Test that images are not wrapped in picture element for unsupported contexts.
532+
*
533+
* @dataProvider data_provider_webp_uploads_wrap_image_in_picture_with_different_context
534+
*
535+
* @param string $context The context to test.
536+
* @param bool $expected Whether the image should be wrapped in a picture element.
537+
*/
538+
public function test_webp_uploads_wrap_image_in_picture_with_different_context( string $context, bool $expected ): void {
539+
$image = wp_get_attachment_image(
540+
self::$image_id,
541+
'large',
542+
false,
543+
array(
544+
'class' => 'wp-image-' . self::$image_id,
545+
'alt' => 'Green Leaves',
546+
)
547+
);
548+
549+
$this->opt_in_to_picture_element();
550+
$filtered_image = apply_filters( 'wp_content_img_tag', $image, $context, self::$image_id );
551+
if ( $expected ) {
552+
$this->assertStringStartsWith( '<picture', $filtered_image );
553+
} else {
554+
$this->assertSame( $image, $filtered_image );
555+
}
556+
}
557+
558+
/**
559+
* Data provider for test_webp_uploads_wrap_image_in_picture_with_different_context.
560+
*
561+
* @return array<string, array{ context: string, expected: bool }>
562+
*/
563+
public function data_provider_webp_uploads_wrap_image_in_picture_with_different_context(): array {
564+
return array(
565+
'the_content' =>
566+
array(
567+
'context' => 'the_content',
568+
'expected' => true,
569+
),
570+
'post_thumbnail_html' =>
571+
array(
572+
'context' => 'post_thumbnail_html',
573+
'expected' => true,
574+
),
575+
'widget_block_content' =>
576+
array(
577+
'context' => 'widget_block_content',
578+
'expected' => true,
579+
),
580+
'invalid_context' =>
581+
array(
582+
'context' => 'invalid_context',
583+
'expected' => false,
584+
),
585+
);
586+
}
529587
}

0 commit comments

Comments
 (0)