| | 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 | |