WordPress.org

Make WordPress Core

Ticket #44157: 44157.diff

File 44157.diff, 3.6 KB (added by tharsheblows, 3 years ago)

For illustrative purposes only

  • 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 d1fe7aa34a..478becaa98 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    541541                $this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
    542542        }
    543543
     544        // This is a test to show how it currently works, it's for an example only.
     545        public function test_get_items_type_arg() {
     546                // Authorized admin user.
     547                wp_set_current_user( self::$admin_id );
     548                $comment_type   = 'foo';
     549                $comment_type_2 = 'bar';
     550                $args           = array(
     551                        'comment_approved' => 1,
     552                        'comment_post_ID'  => self::$post_id,
     553                        'user_id'          => self::$author_id,
     554                        'comment_type'     => $comment_type,
     555                );
     556
     557                // Make comments with comment type 'foo'.
     558                $this->factory->comment->create_post_comments( self::$post_id, 5, $args );
     559                // Make comments with comment type 'bar'.
     560                $args['comment_type'] = $comment_type_2;
     561                $this->factory->comment->create_post_comments( self::$post_id, 9, $args );
     562
     563                // Make one more comment with comment type 'comment'.
     564                unset( $args['comment_type'] );
     565                $this->factory->comment->create( $args ); // This is the comment comment type and brings the total approved comment type comments to two.
     566
     567                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     568
     569                // Admin user and no type quereied gets the two comments of comment type 'comment' (the default).
     570                $response = rest_get_server()->dispatch( $request );
     571                $this->assertEquals( 200, $response->get_status() );
     572                $comments = $response->get_data();
     573                $headers  = $response->get_headers();
     574                $this->assertCount( 2, $comments ); // This is the one approved comment.
     575
     576                // Admin user comment type 'foo' gets those 5 comments.
     577                $request->set_param( 'type', $comment_type );
     578                $response = rest_get_server()->dispatch( $request );
     579                $this->assertEquals( 200, $response->get_status() );
     580                $comments = $response->get_data();
     581                $this->assertCount( 5, $comments );
     582                $foo_type_ids = wp_list_pluck( $comments, 'id' ); // So we can iterate through them later :) .
     583
     584                // Admin user comment type 'bar' gets the 9 comments.
     585                $request->set_param( 'type', $comment_type_2 );
     586                $response = rest_get_server()->dispatch( $request );
     587                $this->assertEquals( 200, $response->get_status() );
     588                $comments = $response->get_data();
     589                $this->assertCount( 9, $comments );
     590
     591                // Unset the current user.
     592                wp_set_current_user( 0 );
     593
     594                // This should get the comments of comment type 'comment'.
     595                $request->set_param( 'type', 'comment' );
     596                $response = rest_get_server()->dispatch( $request );
     597                $this->assertEquals( 200, $response->get_status() );
     598                $comments = $response->get_data();
     599                $this->assertCount( 2, $comments );
     600
     601                // This should throw an error when it tries to get comments of comment type 'foo'.
     602                $request->set_param( 'type', $comment_type );
     603                $response = rest_get_server()->dispatch( $request );
     604                $comments = $response->get_data();
     605                $this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
     606
     607                // But the unauthenticated user can see them at their individual endpoints.
     608                foreach ( $foo_type_ids as $foo_type_id ) {
     609                        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $foo_type_id ) );
     610                        $response = rest_get_server()->dispatch( $request );
     611                        $this->assertEquals( 200, $response->get_status() );
     612                }
     613
     614        }
    544615        public function test_get_items_author_arg() {
    545616                // Authorized
    546617                wp_set_current_user( self::$admin_id );