Make WordPress Core


Ignore:
Timestamp:
07/17/2018 04:11:48 PM (6 years ago)
Author:
SergeyBiryukov
Message:

REST API: Attachments controller should respect upload limits.

When the REST API is in use on WordPress multisite, the WP_REST_Attachments_Controller should respect the "Max upload file size" and "Site upload space" site options.

Props flixos90, danielbachhuber.
Merges [43462] to the 4.9 branch.
Fixes #43751.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43445 r43489  
    13151315    }
    13161316
     1317    /**
     1318     * @ticket 43751
     1319     * @group multisite
     1320     * @group ms-required
     1321     */
     1322    public function test_create_item_with_file_exceeds_multisite_max_filesize() {
     1323        wp_set_current_user( self::$author_id );
     1324        update_site_option( 'fileupload_maxk', 1 );
     1325        update_site_option( 'upload_space_check_disabled', false );
     1326
     1327        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1328        $request->set_file_params(
     1329            array(
     1330                'file' => array(
     1331                    'error'    => '0',
     1332                    'file'     => file_get_contents( $this->test_file ),
     1333                    'name'     => 'canola.jpg',
     1334                    'size'     => filesize( $this->test_file ),
     1335                    'tmp_name' => $this->test_file,
     1336                ),
     1337            )
     1338        );
     1339        $request->set_param( 'title', 'My title is very cool' );
     1340        $request->set_param( 'caption', 'This is a better caption.' );
     1341        $request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
     1342
     1343        $response = rest_get_server()->dispatch( $request );
     1344        $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 );
     1345    }
     1346
     1347    /**
     1348     * @ticket 43751
     1349     * @group multisite
     1350     * @group ms-required
     1351     */
     1352    public function test_create_item_with_data_exceeds_multisite_max_filesize() {
     1353        wp_set_current_user( self::$author_id );
     1354        update_site_option( 'fileupload_maxk', 1 );
     1355        update_site_option( 'upload_space_check_disabled', false );
     1356
     1357        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1358        $request->set_header( 'Content-Type', 'image/jpeg' );
     1359        $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     1360        $request->set_body( file_get_contents( $this->test_file ) );
     1361        $request->set_param( 'title', 'My title is very cool' );
     1362        $request->set_param( 'caption', 'This is a better caption.' );
     1363
     1364        $response = rest_get_server()->dispatch( $request );
     1365        $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 );
     1366    }
     1367
     1368    /**
     1369     * @ticket 43751
     1370     * @group multisite
     1371     * @group ms-required
     1372     */
     1373    public function test_create_item_with_file_exceeds_multisite_site_upload_space() {
     1374        wp_set_current_user( self::$author_id );
     1375        add_filter( 'get_space_allowed', '__return_zero' );
     1376        update_site_option( 'upload_space_check_disabled', false );
     1377
     1378        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1379        $request->set_file_params(
     1380            array(
     1381                'file' => array(
     1382                    'error'    => '0',
     1383                    'file'     => file_get_contents( $this->test_file ),
     1384                    'name'     => 'canola.jpg',
     1385                    'size'     => filesize( $this->test_file ),
     1386                    'tmp_name' => $this->test_file,
     1387                ),
     1388            )
     1389        );
     1390        $request->set_param( 'title', 'My title is very cool' );
     1391        $request->set_param( 'caption', 'This is a better caption.' );
     1392        $request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
     1393
     1394        $response = rest_get_server()->dispatch( $request );
     1395        $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
     1396    }
     1397
     1398    /**
     1399     * @ticket 43751
     1400     * @group multisite
     1401     * @group ms-required
     1402     */
     1403    public function test_create_item_with_data_exceeds_multisite_site_upload_space() {
     1404        wp_set_current_user( self::$author_id );
     1405        add_filter( 'get_space_allowed', '__return_zero' );
     1406        update_site_option( 'upload_space_check_disabled', false );
     1407
     1408        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1409        $request->set_header( 'Content-Type', 'image/jpeg' );
     1410        $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     1411        $request->set_body( file_get_contents( $this->test_file ) );
     1412        $request->set_param( 'title', 'My title is very cool' );
     1413        $request->set_param( 'caption', 'This is a better caption.' );
     1414
     1415        $response = rest_get_server()->dispatch( $request );
     1416        $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
     1417    }
     1418
    13171419}
Note: See TracChangeset for help on using the changeset viewer.