Make WordPress Core


Ignore:
Timestamp:
11/08/2016 05:54:22 AM (9 years ago)
Author:
rmccue
Message:

REST API: Respect unfiltered_html for HTML post fields.

This necessitates a change to our slashing code as well. Ah slashing, the cause of, and solution to, all of life's problems.

Props jnylen0.
Fixes #38609.

File:
1 edited

Legend:

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

    r39154 r39155  
    1111 */
    1212class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
     13
     14    protected static $superadmin_id;
    1315    protected static $editor_id;
    1416    protected static $author_id;
     
    1719
    1820    public static function wpSetUpBeforeClass( $factory ) {
     21        self::$superadmin_id = $factory->user->create( array(
     22            'role'       => 'administrator',
     23            'user_login' => 'superadmin',
     24        ) );
    1925        self::$editor_id = $factory->user->create( array(
    2026            'role' => 'editor',
     
    2935            'role' => 'uploader',
    3036        ) );
     37
     38        if ( is_multisite() ) {
     39            update_site_option( 'site_admins', array( 'superadmin' ) );
     40        }
    3141    }
    3242
     
    5464        $this->test_file2 = '/tmp/codeispoetry.png';
    5565        copy( $orig_file2, $this->test_file2 );
    56 
    5766    }
    5867
     
    724733    }
    725734
     735    public function verify_attachment_roundtrip( $input = array(), $expected_output = array() ) {
     736        // Create the post
     737        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     738        $request->set_header( 'Content-Type', 'image/jpeg' );
     739        $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     740        $request->set_body( file_get_contents( $this->test_file ) );
     741
     742        foreach ( $input as $name => $value ) {
     743            $request->set_param( $name, $value );
     744        }
     745        $response = $this->server->dispatch( $request );
     746        $this->assertEquals( 201, $response->get_status() );
     747        $actual_output = $response->get_data();
     748
     749        // Remove <p class="attachment"> from rendered description
     750        // see https://core.trac.wordpress.org/ticket/38679
     751        $content = $actual_output['description']['rendered'];
     752        $content = explode( "\n", trim( $content ) );
     753        if ( preg_match( '/^<p class="attachment">/', $content[0] ) ) {
     754            $content = implode( "\n", array_slice( $content, 1 ) );
     755            $actual_output['description']['rendered'] = $content;
     756        }
     757
     758        // Compare expected API output to actual API output
     759        $this->assertEquals( $expected_output['title']['raw']           , $actual_output['title']['raw'] );
     760        $this->assertEquals( $expected_output['title']['rendered']      , trim( $actual_output['title']['rendered'] ) );
     761        $this->assertEquals( $expected_output['description']['raw']     , $actual_output['description']['raw'] );
     762        $this->assertEquals( $expected_output['description']['rendered'], trim( $actual_output['description']['rendered'] ) );
     763        $this->assertEquals( $expected_output['caption']['raw']         , $actual_output['caption']['raw'] );
     764        $this->assertEquals( $expected_output['caption']['rendered']    , trim( $actual_output['caption']['rendered'] ) );
     765
     766        // Compare expected API output to WP internal values
     767        $post = get_post( $actual_output['id'] );
     768        $this->assertEquals( $expected_output['title']['raw'], $post->post_title );
     769        $this->assertEquals( $expected_output['description']['raw'], $post->post_content );
     770        $this->assertEquals( $expected_output['caption']['raw'], $post->post_excerpt );
     771
     772        // Update the post
     773        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/media/%d', $actual_output['id'] ) );
     774        foreach ( $input as $name => $value ) {
     775            $request->set_param( $name, $value );
     776        }
     777        $response = $this->server->dispatch( $request );
     778        $this->assertEquals( 200, $response->get_status() );
     779        $actual_output = $response->get_data();
     780
     781        // Remove <p class="attachment"> from rendered description
     782        // see https://core.trac.wordpress.org/ticket/38679
     783        $content = $actual_output['description']['rendered'];
     784        $content = explode( "\n", trim( $content ) );
     785        if ( preg_match( '/^<p class="attachment">/', $content[0] ) ) {
     786            $content = implode( "\n", array_slice( $content, 1 ) );
     787            $actual_output['description']['rendered'] = $content;
     788        }
     789
     790        // Compare expected API output to actual API output
     791        $this->assertEquals( $expected_output['title']['raw']           , $actual_output['title']['raw'] );
     792        $this->assertEquals( $expected_output['title']['rendered']      , trim( $actual_output['title']['rendered'] ) );
     793        $this->assertEquals( $expected_output['description']['raw']     , $actual_output['description']['raw'] );
     794        $this->assertEquals( $expected_output['description']['rendered'], trim( $actual_output['description']['rendered'] ) );
     795        $this->assertEquals( $expected_output['caption']['raw']         , $actual_output['caption']['raw'] );
     796        $this->assertEquals( $expected_output['caption']['rendered']    , trim( $actual_output['caption']['rendered'] ) );
     797
     798        // Compare expected API output to WP internal values
     799        $post = get_post( $actual_output['id'] );
     800        $this->assertEquals( $expected_output['title']['raw']  , $post->post_title );
     801        $this->assertEquals( $expected_output['description']['raw'], $post->post_content );
     802        $this->assertEquals( $expected_output['caption']['raw'], $post->post_excerpt );
     803    }
     804
     805    public static function attachment_roundtrip_provider() {
     806        return array(
     807            array(
     808                // Raw values.
     809                array(
     810                    'title'   => '\o/ ¯\_(ツ)_/¯ 🚢',
     811                    'description' => '\o/ ¯\_(ツ)_/¯ 🚢',
     812                    'caption' => '\o/ ¯\_(ツ)_/¯ 🚢',
     813                ),
     814                // Expected returned values.
     815                array(
     816                    'title' => array(
     817                        'raw'      => '\o/ ¯\_(ツ)_/¯ 🚢',
     818                        'rendered' => '\o/ ¯\_(ツ)_/¯ 🚢',
     819                    ),
     820                    'description' => array(
     821                        'raw'      => '\o/ ¯\_(ツ)_/¯ 🚢',
     822                        'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
     823                    ),
     824                    'caption' => array(
     825                        'raw'      => '\o/ ¯\_(ツ)_/¯ 🚢',
     826                        'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
     827                    ),
     828                )
     829            ),
     830            array(
     831                // Raw values.
     832                array(
     833                    'title'   => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     834                    'description' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     835                    'caption' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     836                ),
     837                // Expected returned values.
     838                array(
     839                    'title' => array(
     840                        'raw'      => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     841                        'rendered' => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     842                    ),
     843                    'description' => array(
     844                        'raw'      => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     845                        'rendered' => '<p>\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;</p>',
     846                    ),
     847                    'caption' => array(
     848                        'raw'      => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     849                        'rendered' => '<p>\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;</p>',
     850                    ),
     851                ),
     852            ),
     853            array(
     854                // Raw values.
     855                array(
     856                    'title'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     857                    'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     858                    'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     859                ),
     860                // Expected returned values.
     861                array(
     862                    'title' => array(
     863                        'raw'      => 'div <strong>strong</strong> oh noes',
     864                        'rendered' => 'div <strong>strong</strong> oh noes',
     865                    ),
     866                    'description' => array(
     867                        'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
     868                        'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     869                    ),
     870                    'caption' => array(
     871                        'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
     872                        'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     873                    ),
     874                )
     875            ),
     876            array(
     877                // Raw values.
     878                array(
     879                    'title'   => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     880                    'description' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     881                    'caption' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     882                ),
     883                // Expected returned values.
     884                array(
     885                    'title' => array(
     886                        'raw'      => '<a href="#">link</a>',
     887                        'rendered' => '<a href="#">link</a>',
     888                    ),
     889                    'description' => array(
     890                        'raw'      => '<a href="#" target="_blank">link</a>',
     891                        'rendered' => '<p><a href="#" target="_blank">link</a></p>',
     892                    ),
     893                    'caption' => array(
     894                        'raw'      => '<a href="#" target="_blank">link</a>',
     895                        'rendered' => '<p><a href="#" target="_blank">link</a></p>',
     896                    ),
     897                )
     898            ),
     899        );
     900    }
     901
     902    /**
     903     * @dataProvider attachment_roundtrip_provider
     904     */
     905    public function test_post_roundtrip_as_author( $raw, $expected ) {
     906        wp_set_current_user( self::$author_id );
     907        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     908        $this->verify_attachment_roundtrip( $raw, $expected );
     909    }
     910
     911    public function test_attachment_roundtrip_as_editor_unfiltered_html() {
     912        wp_set_current_user( self::$editor_id );
     913        if ( is_multisite() ) {
     914            $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     915            $this->verify_attachment_roundtrip( array(
     916                'title'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     917                'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     918                'caption'     => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     919            ), array(
     920                'title' => array(
     921                    'raw'      => 'div <strong>strong</strong> oh noes',
     922                    'rendered' => 'div <strong>strong</strong> oh noes',
     923                ),
     924                'description' => array(
     925                    'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
     926                    'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     927                ),
     928                'caption' => array(
     929                    'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
     930                    'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     931                ),
     932            ) );
     933        } else {
     934            $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     935            $this->verify_attachment_roundtrip( array(
     936                'title'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     937                'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     938                'caption'     => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     939            ), array(
     940                'title' => array(
     941                    'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     942                    'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     943                ),
     944                'description' => array(
     945                    'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     946                    'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
     947                ),
     948                'caption' => array(
     949                    'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     950                    'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
     951                ),
     952            ) );
     953        }
     954    }
     955
     956    public function test_attachment_roundtrip_as_superadmin_unfiltered_html() {
     957        wp_set_current_user( self::$superadmin_id );
     958        $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     959        $this->verify_attachment_roundtrip( array(
     960            'title'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     961            'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     962            'caption'     => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     963        ), array(
     964            'title' => array(
     965                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     966                'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     967            ),
     968            'description' => array(
     969                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     970                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
     971            ),
     972            'caption' => array(
     973                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     974                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
     975            ),
     976        ) );
     977    }
     978
    726979    public function test_delete_item() {
    727980        wp_set_current_user( self::$editor_id );
Note: See TracChangeset for help on using the changeset viewer.