@@ -873,15 +873,31 @@ public function it_should_restore_the_sources_array_from_the_backup_when_an_imag
873873
874874 wp_restore_image ( $ attachment_id );
875875
876+ $ this ->assertImageHasSource ( $ attachment_id , 'image/jpeg ' );
877+ $ this ->assertImageHasSource ( $ attachment_id , 'image/webp ' );
878+
876879 $ metadata = wp_get_attachment_metadata ( $ attachment_id );
877- $ this -> assertArrayHasKey ( ' sources ' , $ metadata );
880+
878881 $ this ->assertSame ( $ backup_sources ['full-orig ' ], $ metadata ['sources ' ] );
879882 $ this ->assertSame ( $ backup_sources , get_post_meta ( $ attachment_id , '_wp_attachment_backup_sources ' , true ) );
880883
881884 $ backup_sizes = get_post_meta ( $ attachment_id , '_wp_attachment_backup_sizes ' , true );
882- foreach ( $ metadata ['sizes ' ] as $ size_name => $ properties ) {
883- $ this ->assertArrayHasKey ( 'sources ' , $ backup_sizes [ $ size_name . '-orig ' ] );
884- $ this ->assertSame ( $ backup_sizes [ $ size_name . '-orig ' ]['sources ' ], $ properties ['sources ' ] );
885+ foreach ( $ backup_sizes as $ size_name => $ properties ) {
886+ // We are only interested in the original filenames to be compared against the backup and restored values.
887+ if ( false === strpos ( $ size_name , '-orig ' ) ) {
888+ $ this ->assertSizeNameIsHashed ( '' , $ size_name , "{$ size_name } is not a valid edited name " );
889+ continue ;
890+ }
891+
892+ $ size_name = str_replace ( '-orig ' , '' , $ size_name );
893+ // Full name is verified above.
894+ if ( 'full ' === $ size_name ) {
895+ continue ;
896+ }
897+
898+ $ this ->assertArrayHasKey ( $ size_name , $ metadata ['sizes ' ] );
899+ $ this ->assertArrayHasKey ( 'sources ' , $ metadata ['sizes ' ][ $ size_name ] );
900+ $ this ->assertSame ( $ properties ['sources ' ], $ metadata ['sizes ' ][ $ size_name ]['sources ' ] );
885901 }
886902 }
887903
@@ -938,7 +954,17 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum
938954 $ this ->assertArrayHasKey ( 'sources ' , $ backup_sizes ['thumbnail-orig ' ] );
939955
940956 $ metadata = wp_get_attachment_metadata ( $ attachment_id );
941- $ this ->assertArrayHasKey ( 'sources ' , $ metadata );
957+
958+ $ this ->assertImageHasSource ( $ attachment_id , 'image/jpeg ' );
959+ $ this ->assertImageHasSource ( $ attachment_id , 'image/webp ' );
960+
961+ $ this ->assertImageHasSizeSource ( $ attachment_id , 'thumbnail ' , 'image/jpeg ' );
962+ $ this ->assertImageHasSizeSource ( $ attachment_id , 'thumbnail ' , 'image/webp ' );
963+
964+ foreach ( $ metadata ['sizes ' ] as $ size_name => $ properties ) {
965+ $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/jpeg ' );
966+ $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/webp ' );
967+ }
942968 }
943969
944970 /**
@@ -959,7 +985,12 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_
959985 $ this ->assertArrayHasKey ( 'full-orig ' , $ backup_sources );
960986 $ this ->assertSame ( $ metadata ['sources ' ], $ backup_sources ['full-orig ' ] );
961987
962- $ this ->assertArrayNotHasKey ( 'sources ' , wp_get_attachment_metadata ( $ attachment_id ), 'The sources attributes was not removed from the metadata. ' );
988+ $ updated_metadata = wp_get_attachment_metadata ( $ attachment_id );
989+
990+ $ this ->assertArrayHasKey ( 'sources ' , $ updated_metadata );
991+ $ this ->assertNotSame ( $ metadata ['sources ' ], $ updated_metadata ['sources ' ] );
992+ $ this ->assertImageHasSource ( $ attachment_id , 'image/jpeg ' );
993+ $ this ->assertImageHasSource ( $ attachment_id , 'image/webp ' );
963994
964995 $ backup_sizes = get_post_meta ( $ attachment_id , '_wp_attachment_backup_sizes ' , true );
965996 $ this ->assertIsArray ( $ backup_sizes );
@@ -973,6 +1004,39 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_
9731004 }
9741005 }
9751006
1007+ /**
1008+ * Update source attributes when webp is edited.
1009+ *
1010+ * @test
1011+ */
1012+ public function it_should_validate_source_attribute_update_when_webp_edited () {
1013+ $ attachment_id = $ this ->factory ->attachment ->create_upload_object ( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg ' );
1014+
1015+ $ editor = new WP_Image_Edit ( $ attachment_id );
1016+ $ editor ->crop ( 1000 , 200 , 0 , 0 )->save ();
1017+ $ this ->assertTrue ( $ editor ->success () );
1018+
1019+ $ this ->assertImageHasSource ( $ attachment_id , 'image/webp ' );
1020+ $ this ->assertImageHasSource ( $ attachment_id , 'image/jpeg ' );
1021+
1022+ $ metadata = wp_get_attachment_metadata ( $ attachment_id );
1023+
1024+ $ this ->assertFileNameIsEdited ( $ metadata ['sources ' ]['image/webp ' ]['file ' ] );
1025+ $ this ->assertFileNameIsEdited ( $ metadata ['sources ' ]['image/jpeg ' ]['file ' ] );
1026+
1027+ $ this ->assertArrayHasKey ( 'sources ' , $ metadata );
1028+ $ this ->assertArrayHasKey ( 'sizes ' , $ metadata );
1029+
1030+ foreach ( $ metadata ['sizes ' ] as $ size_name => $ properties ) {
1031+ $ this ->assertArrayHasKey ( 'sources ' , $ properties );
1032+ $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/webp ' );
1033+ $ this ->assertImageHasSizeSource ( $ attachment_id , $ size_name , 'image/jpeg ' );
1034+
1035+ $ this ->assertFileNameIsEdited ( $ properties ['sources ' ]['image/webp ' ]['file ' ] );
1036+ $ this ->assertFileNameIsEdited ( $ properties ['sources ' ]['image/jpeg ' ]['file ' ] );
1037+ }
1038+ }
1039+
9761040 /**
9771041 * Allow the upload of a WebP image if at least one editor supports the format
9781042 *
@@ -1058,7 +1122,7 @@ public function it_should_u_se_the_next_available_hash_for_the_full_size_image_o
10581122 remove_filter ( 'wp_update_attachment_metadata ' , 'webp_uploads_update_attachment_metadata ' );
10591123
10601124 $ editor ->rotate_right ()->save ();
1061- $ this ->assertRegExp ( '/ full-\d{13}/ ' , webp_uploads_get_next_full_size_key_from_backup ( $ attachment_id ) );
1125+ $ this ->assertSizeNameIsHashed ( 'full ' , webp_uploads_get_next_full_size_key_from_backup ( $ attachment_id ) );
10621126 }
10631127
10641128 /**
@@ -1106,7 +1170,7 @@ public function it_should_store_the_metadata_on_the_next_available_hash() {
11061170
11071171 $ backup_sources_keys = array_keys ( $ backup_sources );
11081172 $ this ->assertSame ( 'full-orig ' , reset ( $ backup_sources_keys ) );
1109- $ this ->assertRegExp ( '/ full-\d{13}/ ' , end ( $ backup_sources_keys ) );
1173+ $ this ->assertSizeNameIsHashed ( 'full ' , end ( $ backup_sources_keys ) );
11101174 $ this ->assertSame ( $ sources , end ( $ backup_sources ) );
11111175 }
11121176
0 commit comments