Make WordPress Core


Ignore:
Timestamp:
07/17/2018 07:21:50 AM (7 years ago)
Author:
pento
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.
Fixes #43751.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43437 r43462  
    14331433    }
    14341434
     1435    /**
     1436     * @ticket 43751
     1437     * @group multisite
     1438     * @group ms-required
     1439     */
     1440    public function test_create_item_with_file_exceeds_multisite_max_filesize() {
     1441        wp_set_current_user( self::$author_id );
     1442        update_site_option( 'fileupload_maxk', 1 );
     1443        update_site_option( 'upload_space_check_disabled', false );
     1444
     1445        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1446        $request->set_file_params(
     1447            array(
     1448                'file' => array(
     1449                    'error'    => '0',
     1450                    'file'     => file_get_contents( $this->test_file ),
     1451                    'name'     => 'canola.jpg',
     1452                    'size'     => filesize( $this->test_file ),
     1453                    'tmp_name' => $this->test_file,
     1454                ),
     1455            )
     1456        );
     1457        $request->set_param( 'title', 'My title is very cool' );
     1458        $request->set_param( 'caption', 'This is a better caption.' );
     1459        $request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
     1460
     1461        $response = rest_get_server()->dispatch( $request );
     1462        $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 );
     1463    }
     1464
     1465    /**
     1466     * @ticket 43751
     1467     * @group multisite
     1468     * @group ms-required
     1469     */
     1470    public function test_create_item_with_data_exceeds_multisite_max_filesize() {
     1471        wp_set_current_user( self::$author_id );
     1472        update_site_option( 'fileupload_maxk', 1 );
     1473        update_site_option( 'upload_space_check_disabled', false );
     1474
     1475        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1476        $request->set_header( 'Content-Type', 'image/jpeg' );
     1477        $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     1478        $request->set_body( file_get_contents( $this->test_file ) );
     1479        $request->set_param( 'title', 'My title is very cool' );
     1480        $request->set_param( 'caption', 'This is a better caption.' );
     1481
     1482        $response = rest_get_server()->dispatch( $request );
     1483        $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 );
     1484    }
     1485
     1486    /**
     1487     * @ticket 43751
     1488     * @group multisite
     1489     * @group ms-required
     1490     */
     1491    public function test_create_item_with_file_exceeds_multisite_site_upload_space() {
     1492        wp_set_current_user( self::$author_id );
     1493        add_filter( 'get_space_allowed', '__return_zero' );
     1494        update_site_option( 'upload_space_check_disabled', false );
     1495
     1496        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1497        $request->set_file_params(
     1498            array(
     1499                'file' => array(
     1500                    'error'    => '0',
     1501                    'file'     => file_get_contents( $this->test_file ),
     1502                    'name'     => 'canola.jpg',
     1503                    'size'     => filesize( $this->test_file ),
     1504                    'tmp_name' => $this->test_file,
     1505                ),
     1506            )
     1507        );
     1508        $request->set_param( 'title', 'My title is very cool' );
     1509        $request->set_param( 'caption', 'This is a better caption.' );
     1510        $request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
     1511
     1512        $response = rest_get_server()->dispatch( $request );
     1513        $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
     1514    }
     1515
     1516    /**
     1517     * @ticket 43751
     1518     * @group multisite
     1519     * @group ms-required
     1520     */
     1521    public function test_create_item_with_data_exceeds_multisite_site_upload_space() {
     1522        wp_set_current_user( self::$author_id );
     1523        add_filter( 'get_space_allowed', '__return_zero' );
     1524        update_site_option( 'upload_space_check_disabled', false );
     1525
     1526        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1527        $request->set_header( 'Content-Type', 'image/jpeg' );
     1528        $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     1529        $request->set_body( file_get_contents( $this->test_file ) );
     1530        $request->set_param( 'title', 'My title is very cool' );
     1531        $request->set_param( 'caption', 'This is a better caption.' );
     1532
     1533        $response = rest_get_server()->dispatch( $request );
     1534        $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
     1535    }
     1536
    14351537}
Note: See TracChangeset for help on using the changeset viewer.