WordPress.org

Make WordPress Core

Ticket #39010: 39010.diff

File 39010.diff, 2.3 KB (added by jnylen0, 4 years ago)
  • 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..45e00c0 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    392392                         *                                 response.
    393393                         */
    394394                        $allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request );
    395                         if ( false === $allow_anonymous ) {
     395                        if ( ! $allow_anonymous ) {
    396396                                return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) );
    397397                        }
    398398                }
  • 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 1daa35e..6705970 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    17371737                $this->assertEquals( 400, $response->get_status() );
    17381738        }
    17391739
     1740        public function anonymous_comments_callback_null() {
     1741                // I'm a plugin developer who forgot to include a return value for some
     1742                // code path in my 'rest_allow_anonymous_comments' filter.
     1743        }
     1744
     1745        public function test_allow_anonymous_comments_null() {
     1746                add_filter( 'rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2 );
     1747
     1748                $params = array(
     1749                        'post'         => self::$post_id,
     1750                        'author_name'  => 'Comic Book Guy',
     1751                        'author_email' => 'cbg@androidsdungeon.com',
     1752                        'author_url'   => 'http://androidsdungeon.com',
     1753                        'content'      => 'Worst Comment Ever!',
     1754                );
     1755
     1756                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1757                $request->add_header( 'content-type', 'application/json' );
     1758                $request->set_body( wp_json_encode( $params ) );
     1759
     1760                $response = $this->server->dispatch( $request );
     1761
     1762                remove_filter( 'rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2 );
     1763
     1764                $this->assertErrorResponse( 'rest_comment_login_required', $response, 401 );
     1765        }
     1766
    17401767        /**
    17411768         * @ticket 38477
    17421769         */