Ticket #57957: 57957.2.diff
File 57957.2.diff, 3.3 KB (added by , 6 months ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 97ee830261..dbab4c933e 100644
class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 304 304 $attachment->post_mime_type = $type; 305 305 $attachment->guid = $url; 306 306 307 // If the title was not set, use the original filename. 308 if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) { 309 // Remove the file extension (after the last `.`) 310 $tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) ); 311 312 if ( ! empty( $tmp_title ) ) { 313 $attachment->post_title = $tmp_title; 314 } 315 } 316 317 // Fall back to the original approach. 307 318 if ( empty( $attachment->post_title ) ) { 308 319 $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); 309 320 } -
tests/phpunit/tests/rest-api/rest-attachments-controller.php
diff --git tests/phpunit/tests/rest-api/rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php index 956e8afd30..6d881a6412 100644
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 2015 2015 $this->assertSame( 1, self::$rest_after_insert_attachment_count ); 2016 2016 } 2017 2017 2018 /** 2019 * Tests that the naming behavior of REST media uploads matches core media uploads. 2020 * 2021 * In particular, filenames with spaces should maintain the spaces rather than 2022 * replacing them with hyphens. 2023 * 2024 * @ticket 57957 2025 * 2026 * @covers WP_REST_Attachments_Controller::insert_attachment 2027 * @dataProvider rest_upload_filename_spaces 2028 */ 2029 public function test_rest_upload_filename_spaces( $filename, $expected ) { 2030 wp_set_current_user( self::$editor_id ); 2031 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 2032 $request->set_header( 'Content-Type', 'image/jpeg' ); 2033 $request->set_body( file_get_contents( self::$test_file ) ); 2034 $request->set_file_params( 2035 array( 2036 'file' => array( 2037 'file' => file_get_contents( self::$test_file2 ), 2038 'name' => $filename, 2039 'size' => filesize( self::$test_file2 ), 2040 'tmp_name' => self::$test_file2, 2041 ), 2042 ) 2043 ); 2044 $response = rest_get_server()->dispatch( $request ); 2045 $data = $response->get_data(); 2046 $this->assertSame( 201, $response->get_status(), 'The file was not uploaded.' ); 2047 $this->assertSame( $expected, $data['title']['raw'], 'An incorrect filename was returned.' ); 2048 } 2049 2050 /** 2051 * Data provider for text_rest_upload_filename_spaces. 2052 * 2053 * @return array 2054 */ 2055 public function rest_upload_filename_spaces() { 2056 return array( 2057 'filename with spaces' => array( 2058 'Filename With Spaces.jpg', 2059 'Filename With Spaces', 2060 ), 2061 'filename.with.periods' => array( 2062 'Filename.With.Periods.jpg', 2063 'Filename.With.Periods', 2064 ), 2065 'filename-with-dashes' => array( 2066 'Filename-With-Dashes.jpg', 2067 'Filename-With-Dashes', 2068 ), 2069 ); 2070 } 2071 2018 2072 /** 2019 2073 * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire 2020 2074 * once when attachments are updated.