WordPress.org

Make WordPress Core

Ticket #38700: 38700.7.diff

File 38700.7.diff, 3.1 KB (added by jnylen0, 3 years ago)

Use an existing string; tweak tests to more closely match r39371

  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
    index b07ced4..01298db 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    672672                        return $prepared_args;
    673673                }
    674674
     675                if ( ! empty( $prepared_args['comment_post_ID'] ) ) {
     676                        $post = get_post( $prepared_args['comment_post_ID'] );
     677                        if ( empty( $post ) ) {
     678                                return new WP_Error( 'rest_comment_invalid_post_id', __( 'Invalid post ID.' ), array( 'status' => 403 ) );
     679                        }
     680                }
     681
    675682                if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
    676683                        // Only the comment status is being changed.
    677684                        $change = $this->handle_status_param( $request['status'], $id );
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    698705
    699706                        $updated = wp_update_comment( wp_slash( (array) $prepared_args ) );
    700707
    701                         if ( 0 === $updated ) {
     708                        if ( false === $updated ) {
    702709                                return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) );
    703710                        }
    704711
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
    index 112a26c..e6ae0e7 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    19031903                $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
    19041904        }
    19051905
     1906        public function test_update_item_no_change() {
     1907                $comment = get_comment( self::$approved_id );
     1908
     1909                wp_set_current_user( self::$admin_id );
     1910                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     1911                $request->set_param( 'post', $comment->comment_post_ID );
     1912
     1913                // Run twice to make sure that the update still succeeds even if no DB
     1914                // rows are updated.
     1915                $response = $this->server->dispatch( $request );
     1916                $this->assertEquals( 200, $response->get_status() );
     1917
     1918                $response = $this->server->dispatch( $request );
     1919                $this->assertEquals( 200, $response->get_status() );
     1920        }
     1921
    19061922        public function test_update_comment_status() {
    19071923                wp_set_current_user( self::$admin_id );
    19081924
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    20542070                $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
    20552071        }
    20562072
     2073        public function test_update_comment_invalid_post_id() {
     2074                wp_set_current_user( self::$admin_id );
     2075
     2076                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2077                $request->set_param( 'post', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
     2078
     2079                $response = $this->server->dispatch( $request );
     2080                $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
     2081        }
     2082
    20572083        public function test_update_comment_invalid_permission() {
    20582084                add_filter( 'rest_allow_anonymous_comments', '__return_true' );
    20592085