@@ -17,48 +17,99 @@ public function set_up() {
1717 }
1818
1919 /**
20- * Not create the original mime type for JPEG images.
20+ * Don't create the original mime type for JPEG images.
2121 *
2222 * @test
2323 */
2424 public function it_should_not_create_the_original_mime_type_for_jpeg_images () {
2525 $ attachment_id = $ this ->factory ->attachment ->create_upload_object ( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg ' );
2626
27+ // There should be a WebP source, but no JPEG source for the full image.
2728 $ this ->assertImageHasSource ( $ attachment_id , 'image/webp ' );
2829 $ this ->assertImageNotHasSource ( $ attachment_id , 'image/jpeg ' );
2930
3031 $ metadata = wp_get_attachment_metadata ( $ attachment_id );
32+
33+ // The full image should be a WebP.
3134 $ this ->assertArrayHasKey ( 'file ' , $ metadata );
35+ $ this ->assertStringEndsWith ( $ metadata ['sources ' ]['image/webp ' ]['file ' ], $ metadata ['file ' ] );
36+ $ this ->assertStringEndsWith ( $ metadata ['sources ' ]['image/webp ' ]['file ' ], get_attached_file ( $ attachment_id ) );
37+
38+ // The original JPEG should be backed up.
39+ $ this ->assertStringEndsWith ( '.jpg ' , wp_get_original_image_path ( $ attachment_id ) );
40+
41+ // For compatibility reasons, the post MIME type should remain JPEG.
42+ $ this ->assertSame ( 'image/jpeg ' , get_post_mime_type ( $ attachment_id ) );
3243
44+ // There should be a WebP source, but no JPEG source for all sizes.
3345 foreach ( array_keys ( $ metadata ['sizes ' ] ) as $ size_name ) {
3446 $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/webp ' );
3547 $ this ->assertImageNotHasSizeSource ( $ attachment_id , $ size_name , 'image/jpeg ' );
3648 }
3749 }
3850
3951 /**
40- * Create the original mime type for WEBP images.
52+ * Create the original mime type for WebP images.
4153 *
4254 * @test
4355 */
4456 public function it_should_create_the_original_mime_type_as_well_with_all_the_available_sources_for_the_specified_mime () {
4557 $ attachment_id = $ this ->factory ->attachment ->create_upload_object ( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/balloons.webp ' );
4658
59+ // There should be a WebP source, but no JPEG source for the full image.
4760 $ this ->assertImageNotHasSource ( $ attachment_id , 'image/jpeg ' );
4861 $ this ->assertImageHasSource ( $ attachment_id , 'image/webp ' );
4962
5063 $ metadata = wp_get_attachment_metadata ( $ attachment_id );
64+
65+ // The full image should be a WebP.
5166 $ this ->assertArrayHasKey ( 'file ' , $ metadata );
5267 $ this ->assertStringEndsWith ( $ metadata ['sources ' ]['image/webp ' ]['file ' ], $ metadata ['file ' ] );
68+ $ this ->assertStringEndsWith ( $ metadata ['sources ' ]['image/webp ' ]['file ' ], get_attached_file ( $ attachment_id ) );
69+
70+ // The post MIME type should be WebP.
71+ $ this ->assertSame ( 'image/webp ' , get_post_mime_type ( $ attachment_id ) );
5372
73+ // There should be a WebP source, but no JPEG source for all sizes.
5474 foreach ( array_keys ( $ metadata ['sizes ' ] ) as $ size_name ) {
5575 $ this ->assertImageNotHasSizeSource ( $ attachment_id , $ size_name , 'image/jpeg ' );
5676 $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/webp ' );
5777 }
5878 }
5979
6080 /**
61- * Not create the sources property if no transform is provided
81+ * Create JPEG and WebP for JPEG images, if opted in.
82+ *
83+ * @test
84+ */
85+ public function it_should_create_jpeg_and_webp_for_jpeg_images_if_opted_in () {
86+ $ this ->opt_in_to_jpeg_and_webp ();
87+
88+ $ attachment_id = $ this ->factory ->attachment ->create_upload_object ( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg ' );
89+
90+ // There should be JPEG and WebP sources for the full image.
91+ $ this ->assertImageHasSource ( $ attachment_id , 'image/jpeg ' );
92+ $ this ->assertImageHasSource ( $ attachment_id , 'image/webp ' );
93+
94+ $ metadata = wp_get_attachment_metadata ( $ attachment_id );
95+
96+ // The full image should be a JPEG.
97+ $ this ->assertArrayHasKey ( 'file ' , $ metadata );
98+ $ this ->assertStringEndsWith ( $ metadata ['sources ' ]['image/jpeg ' ]['file ' ], $ metadata ['file ' ] );
99+ $ this ->assertStringEndsWith ( $ metadata ['sources ' ]['image/jpeg ' ]['file ' ], get_attached_file ( $ attachment_id ) );
100+
101+ // The post MIME type should be JPEG.
102+ $ this ->assertSame ( 'image/jpeg ' , get_post_mime_type ( $ attachment_id ) );
103+
104+ // There should be JPEG and WebP sources for all sizes.
105+ foreach ( array_keys ( $ metadata ['sizes ' ] ) as $ size_name ) {
106+ $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/jpeg ' );
107+ $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/webp ' );
108+ }
109+ }
110+
111+ /**
112+ * Don't create the sources property if no transform is provided.
62113 *
63114 * @test
64115 */
0 commit comments