Make WordPress Core

Ticket #64921: 64921-tests-create-item.diff

File 64921-tests-create-item.diff, 2.4 KB (added by softglaze, 4 months ago)

PHPUnit coverage for create_item() with a slug on draft or pending status. Red on trunk, green with PR 11327.

  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 212ddde..3f9f821 100644
    a b Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket baco  
    37253725                $this->assertErrorResponse( 'rest_cannot_assign_term', $response, 403 );
    37263726        }
    37273727
     3728        /**
     3729         * Tests that creating an unpublished post with a slug does not access
     3730         * undefined properties on the prepared post object.
     3731         *
     3732         * `prepare_item_for_database()` sets an `ID` property only when the request
     3733         * carries an ID, which is never the case when creating, and sets
     3734         * `post_parent` only for hierarchical post types, which `post` is not.
     3735         * Creating a draft or pending post with a slug therefore reaches
     3736         * `wp_unique_post_slug()` through two undefined property accesses.
     3737         *
     3738         * @ticket 64921
     3739         *
     3740         * @dataProvider data_create_item_with_slug_and_unpublished_status
     3741         *
     3742         * @covers WP_REST_Posts_Controller::create_item
     3743         *
     3744         * @param string $status The post status to create the post with.
     3745         */
     3746        public function test_create_item_with_slug_and_unpublished_status( $status ) {
     3747                wp_set_current_user( self::$editor_id );
     3748
     3749                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     3750                $request->set_body_params(
     3751                        array(
     3752                                'title'  => 'Unpublished post with a slug',
     3753                                'status' => $status,
     3754                                'slug'   => 'unpublished-post-with-a-slug',
     3755                        )
     3756                );
     3757
     3758                $response = rest_get_server()->dispatch( $request );
     3759
     3760                $this->assertSame( 201, $response->get_status(), 'The post should have been created.' );
     3761
     3762                $data = $response->get_data();
     3763                $this->assertSame( 'unpublished-post-with-a-slug', $data['slug'], 'The requested slug should have been used.' );
     3764                $this->assertSame( $status, get_post( $data['id'] )->post_status, 'The requested status should have been saved.' );
     3765        }
     3766
     3767        /**
     3768         * Data provider.
     3769         *
     3770         * @return array[] Test parameters.
     3771         */
     3772        public function data_create_item_with_slug_and_unpublished_status() {
     3773                return array(
     3774                        'draft status'   => array( 'draft' ),
     3775                        'pending status' => array( 'pending' ),
     3776                );
     3777        }
     3778
    37283779        public function revoke_assign_term( $caps, $cap, $user_id, $args ) {
    37293780                if ( 'assign_term' === $cap && isset( $args[0] ) && $this->forbidden_cat === $args[0] ) {
    37303781                        $caps = array( 'do_not_allow' );