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