Make WordPress Core

Ticket #64921: 64921-tests-update-item-filtered-object.diff

File 64921-tests-update-item-filtered-object.diff, 2.9 KB (added by softglaze, 4 months ago)

PHPUnit coverage for update_item() when rest_pre_insert_post returns an object without ID (repro from comment:13). Red on trunk.

  • 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..d5f8372 100644
    a b Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket baco  
    45494549                $this->assertSame( 'post-my-invalid-template.php', $data['template'] );
    45504550        }
    45514551
     4552        /**
     4553         * Tests that a `rest_pre_insert_post` filter returning an object without an
     4554         * `ID` property does not cause the update to fail.
     4555         *
     4556         * The filter is documented as receiving and returning a `stdClass`, so a
     4557         * filter may build and return its own object rather than modifying the one it
     4558         * was given. When that object has no `ID`, the cast to an array in
     4559         * `update_item()` produces an array with no `ID` key, `wp_update_post()` then
     4560         * calls `get_post( null )`, and the request fails with a 400 `invalid_post`
     4561         * error instead of updating the post.
     4562         *
     4563         * @ticket 64921
     4564         *
     4565         * @covers WP_REST_Posts_Controller::update_item
     4566         */
     4567        public function test_update_item_with_filtered_post_object_missing_id() {
     4568                wp_set_current_user( self::$editor_id );
     4569
     4570                add_filter( 'rest_pre_insert_post', array( $this, 'filter_rest_pre_insert_post_strip_identifiers' ) );
     4571
     4572                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     4573                $request->set_body_params( array( 'title' => 'Updated through a filtered object' ) );
     4574
     4575                $response = rest_get_server()->dispatch( $request );
     4576
     4577                $this->assertSame( 200, $response->get_status(), 'The update should not fail when the filtered object omits ID.' );
     4578
     4579                $data = $response->get_data();
     4580                $this->assertSame( self::$post_id, $data['id'], 'The response should refer to the post from the request.' );
     4581                $this->assertSame(
     4582                        'Updated through a filtered object',
     4583                        get_post( self::$post_id )->post_title,
     4584                        'The filtered title should have been saved to the post from the request.'
     4585                );
     4586        }
     4587
     4588        /**
     4589         * Filter callback that returns a new object without the `ID` and
     4590         * `post_parent` properties.
     4591         *
     4592         * This mirrors a filter that rebuilds the prepared post rather than
     4593         * modifying the object it was given.
     4594         *
     4595         * @ticket 64921
     4596         *
     4597         * @param stdClass $prepared_post The prepared post object.
     4598         * @return stdClass A new object with the identifier properties removed.
     4599         */
     4600        public function filter_rest_pre_insert_post_strip_identifiers( $prepared_post ) {
     4601                $filtered = new stdClass();
     4602
     4603                foreach ( get_object_vars( $prepared_post ) as $property => $value ) {
     4604                        if ( 'ID' === $property || 'post_parent' === $property ) {
     4605                                continue;
     4606                        }
     4607
     4608                        $filtered->$property = $value;
     4609                }
     4610
     4611                return $filtered;
     4612        }
     4613
    45524614        public function verify_post_roundtrip( $input = array(), $expected_output = array() ) {
    45534615                // Create the post.
    45544616                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );