@@ -3097,4 +3097,58 @@ public function test_edit_image_vertical_flip_only() {
30973097 // The controller converts the integer values to booleans: 0 !== (int) 1 = true.
30983098 $ this ->assertSame ( array ( true , false ), WP_Image_Editor_Mock::$ spy ['flip ' ][0 ], 'Vertical flip of the image is not identical. ' );
30993099 }
3100+
3101+ /**
3102+ * Test that wp_slash() is properly applied when creating edited images.
3103+ *
3104+ * This test verifies that the object returned by prepare_item_for_database()
3105+ * is properly cast to an array before being passed to wp_slash(), ensuring
3106+ * that string values are properly escaped for database insertion.
3107+ *
3108+ * @ticket 64149
3109+ * @requires function imagejpeg
3110+ */
3111+ public function test_edit_image_wp_slash_with_object_cast () {
3112+ wp_set_current_user ( self ::$ superadmin_id );
3113+ $ attachment = self ::factory ()->attachment ->create_upload_object ( self ::$ test_file );
3114+
3115+ // Create a mock to capture the data passed to wp_insert_attachment.
3116+ $ captured_data = null ;
3117+
3118+ // Mock wp_insert_attachment to capture the data being passed.
3119+ add_filter (
3120+ 'wp_insert_attachment_data ' ,
3121+ function ( $ data ) use ( &$ captured_data ) {
3122+ $ captured_data = $ data ;
3123+ return $ data ;
3124+ },
3125+ 10 ,
3126+ 1
3127+ );
3128+
3129+ $ params = array (
3130+ 'rotation ' => 60 ,
3131+ 'src ' => wp_get_attachment_image_url ( $ attachment , 'full ' ),
3132+ 'title ' => 'Test Title with "quotes" and \'apostrophes \'' ,
3133+ 'caption ' => 'Test Caption with "quotes" and \'apostrophes \'' ,
3134+ 'description ' => 'Test Description with "quotes" and \'apostrophes \'' ,
3135+ );
3136+
3137+ $ request = new WP_REST_Request ( 'POST ' , "/wp/v2/media/ {$ attachment }/edit " );
3138+ $ request ->set_body_params ( $ params );
3139+ $ response = rest_do_request ( $ request );
3140+
3141+ $ this ->assertSame ( 201 , $ response ->get_status () );
3142+
3143+ // Verify that the data was properly slashed (escaped)
3144+ $ this ->assertNotNull ( $ captured_data , 'wp_insert_attachment was not called with data ' );
3145+
3146+ // Check that quotes are properly escaped in the captured data.
3147+ $ this ->assertStringContainsString ( 'Test Title with \"quotes\" ' , $ captured_data ['post_title ' ] ?? '' , 'Title quotes not properly escaped ' );
3148+ $ this ->assertStringContainsString ( 'Test Caption with \"quotes\" ' , $ captured_data ['post_excerpt ' ] ?? '' , 'Caption quotes not properly escaped ' );
3149+ $ this ->assertStringContainsString ( 'Test Description with \"quotes\" ' , $ captured_data ['post_content ' ] ?? '' , 'Description quotes not properly escaped ' );
3150+
3151+ // Verify that the data is an array (not an object).
3152+ $ this ->assertIsArray ( $ captured_data , 'Data passed to wp_insert_attachment should be an array ' );
3153+ }
31003154}
0 commit comments