From 4a729f04985b158674f7c2e44bb696a6ea6ed2fe Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 5 Nov 2019 20:41:12 +0000 Subject: [PATCH] REST API: Speed up pagination unit tests by creating less fixtures and reusing them where possible. Includes minor documentation and code layout fixes for better readability. See #30017, #48145. git-svn-id: https://develop.svn.wordpress.org/trunk@46657 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/rest-attachments-controller.php | 2 +- .../rest-api/rest-categories-controller.php | 207 +++++--- .../rest-api/rest-comments-controller.php | 468 ++++++++++++------ .../tests/rest-api/rest-pages-controller.php | 36 +- .../tests/rest-api/rest-posts-controller.php | 393 +++++++++------ .../tests/rest-api/rest-tags-controller.php | 265 ++++++---- .../tests/rest-api/rest-users-controller.php | 329 ++++++++---- 7 files changed, 1098 insertions(+), 602 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index dc0b61835d..60686523fd 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -1451,7 +1451,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_search_item_by_filename() { - $id = $this->factory->attachment->create_object( + $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( diff --git a/tests/phpunit/tests/rest-api/rest-categories-controller.php b/tests/phpunit/tests/rest-api/rest-categories-controller.php index 8d44e282db..2035bdbf80 100644 --- a/tests/phpunit/tests/rest-api/rest-categories-controller.php +++ b/tests/phpunit/tests/rest-api/rest-categories-controller.php @@ -15,6 +15,10 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas protected static $contributor; protected static $subscriber; + protected static $category_ids = array(); + protected static $total_categories = 30; + protected static $per_page = 50; + public static function wpSetUpBeforeClass( $factory ) { self::$administrator = $factory->user->create( array( @@ -31,11 +35,25 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 'role' => 'subscriber', ) ); + + // Set up categories for pagination tests. + for ( $i = 0; $i < self::$total_categories - 1; $i++ ) { + $category_ids[] = $factory->category->create( + array( + 'name' => "Category {$i}", + ) + ); + } } public static function wpTearDownAfterClass() { self::delete_user( self::$administrator ); self::delete_user( self::$subscriber ); + + // Remove categories for pagination tests. + foreach ( self::$category_ids as $category_id ) { + wp_delete_term( $category_id, 'category' ); + } } public function setUp() { @@ -136,13 +154,15 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_get_items() { - $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $this->check_get_taxonomy_terms_response( $response ); } public function test_get_items_invalid_permission_for_context() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -153,8 +173,13 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas $post_id = $this->factory->post->create(); $category1 = $this->factory->category->create( array( 'name' => 'Season 5' ) ); $category2 = $this->factory->category->create( array( 'name' => 'The Be Sharps' ) ); + + $total_categories = self::$total_categories + 2; + wp_set_object_terms( $post_id, array( $category1, $category2 ), 'category' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'hide_empty', true ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -166,7 +191,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas $request->set_param( 'hide_empty', 'false' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( 3, count( $data ) ); + $this->assertEquals( $total_categories, count( $data ) ); } public function test_get_items_parent_zero_arg() { @@ -184,7 +209,9 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 'parent' => $parent2, ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'parent', 0 ); $response = rest_get_server()->dispatch( $request ); @@ -214,7 +241,9 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 'parent' => $parent2, ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'parent', '0' ); $response = rest_get_server()->dispatch( $request ); @@ -254,41 +283,49 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_get_items_include_query() { $id1 = $this->factory->category->create(); - $this->factory->category->create(); - $id3 = $this->factory->category->create(); + $id2 = $this->factory->category->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); - // Orderby=>asc - $request->set_param( 'include', array( $id3, $id1 ) ); + + // 'orderby' => 'asc'. + $request->set_param( 'include', array( $id2, $id1 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( $id1, $data[0]['id'] ); - // Orderby=>include + + // 'orderby' => 'include'. $request->set_param( 'orderby', 'include' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - $this->assertEquals( $id3, $data[0]['id'] ); + $this->assertEquals( $id2, $data[0]['id'] ); } public function test_get_items_exclude_query() { - $id1 = $this->factory->category->create(); - $id2 = $this->factory->category->create(); - $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); + $id1 = $this->factory->category->create(); + $id2 = $this->factory->category->create(); + + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertTrue( in_array( $id2, $ids, true ) ); + $request->set_param( 'exclude', array( $id2 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertFalse( in_array( $id2, $ids, true ) ); } public function test_get_items_orderby_args() { $this->factory->category->create( array( 'name' => 'Apple' ) ); $this->factory->category->create( array( 'name' => 'Banana' ) ); + /* * Tests: * - orderby @@ -304,6 +341,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); $this->assertEquals( 'Uncategorized', $data[0]['name'] ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'orderby', 'name' ); $request->set_param( 'order', 'asc' ); @@ -319,7 +357,8 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas $this->factory->category->create( array( 'name' => 'Cantaloupe' ) ); $this->factory->category->create( array( 'name' => 'Apple' ) ); $this->factory->category->create( array( 'name' => 'Banana' ) ); - // defaults to orderby=name, order=asc + + // Defaults to 'orderby' => 'name', 'order' => 'asc'. $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -327,18 +366,18 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas $this->assertEquals( 'Apple', $data[0]['name'] ); $this->assertEquals( 'Banana', $data[1]['name'] ); $this->assertEquals( 'Cantaloupe', $data[2]['name'] ); - $this->assertEquals( 'Uncategorized', $data[3]['name'] ); - // orderby=id, with default order=asc + + // 'orderby' => 'id', with default 'order' => 'asc'. $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'orderby', 'id' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( 'Uncategorized', $data[0]['name'] ); - $this->assertEquals( 'Cantaloupe', $data[1]['name'] ); - $this->assertEquals( 'Apple', $data[2]['name'] ); - $this->assertEquals( 'Banana', $data[3]['name'] ); - // orderby=id, order=desc + $this->assertEquals( 'Category 0', $data[1]['name'] ); + $this->assertEquals( 'Category 1', $data[2]['name'] ); + $this->assertEquals( 'Category 2', $data[3]['name'] ); + + // 'orderby' => 'id', 'order' => 'desc'. $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'orderby', 'id' ); $request->set_param( 'order', 'desc' ); @@ -486,6 +525,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_get_items_search_args() { $this->factory->category->create( array( 'name' => 'Apple' ) ); $this->factory->category->create( array( 'name' => 'Banana' ) ); + /* * Tests: * - search @@ -497,6 +537,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); $this->assertEquals( 'Apple', $data[0]['name'] ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'search', 'Garbage' ); $response = rest_get_server()->dispatch( $request ); @@ -508,6 +549,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_get_items_slug_arg() { $this->factory->category->create( array( 'name' => 'Apple' ) ); $this->factory->category->create( array( 'name' => 'Banana' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'slug', 'apple' ); $response = rest_get_server()->dispatch( $request ); @@ -525,6 +567,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 'parent' => $category1, ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'parent', $category1 ); $response = rest_get_server()->dispatch( $request ); @@ -534,13 +577,6 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_get_terms_invalid_parent_arg() { - $category1 = $this->factory->category->create( array( 'name' => 'Parent' ) ); - $this->factory->category->create( - array( - 'name' => 'Child', - 'parent' => $category1, - ) - ); $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'parent', 'invalid-parent' ); $response = rest_get_server()->dispatch( $request ); @@ -574,19 +610,15 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_get_terms_pagination_headers() { - // Start of the index + Uncategorized default term - for ( $i = 0; $i < 49; $i++ ) { - $this->factory->category->create( - array( - 'name' => "Category {$i}", - ) - ); - } + $total_categories = self::$total_categories; + $total_pages = (int) ceil( $total_categories / 10 ); + + // Start of the index + Uncategorized default term. $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 50, $headers['X-WP-Total'] ); - $this->assertEquals( 5, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $this->assertCount( 10, $response->get_data() ); $next_link = add_query_arg( array( @@ -596,18 +628,17 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas ); $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // 3rd page - $this->factory->category->create( - array( - 'name' => 'Category 51', - ) - ); + + // 3rd page. + $this->factory->category->create(); + $total_categories++; + $total_pages++; $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $this->assertCount( 10, $response->get_data() ); $prev_link = add_query_arg( array( @@ -623,33 +654,35 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas rest_url( 'wp/v2/categories' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // Last page + + // Last page. $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); - $request->set_param( 'page', 6 ); + $request->set_param( 'page', $total_pages ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $this->assertCount( 1, $response->get_data() ); $prev_link = add_query_arg( array( - 'page' => 5, + 'page' => $total_pages - 1, ), rest_url( 'wp/v2/categories' ) ); $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); - // Out of bounds + + // Out of bounds. $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); - $request->set_param( 'page', 8 ); + $request->set_param( 'page', 100 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_categories, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $this->assertCount( 0, $response->get_data() ); $prev_link = add_query_arg( array( - 'page' => 6, + 'page' => $total_pages, ), rest_url( 'wp/v2/categories' ) ); @@ -658,28 +691,22 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_get_items_per_page_exceeds_number_of_items() { - // Start of the index + Uncategorized default term - for ( $i = 0; $i < 17; $i++ ) { - $this->factory->category->create( - array( - 'name' => "Category {$i}", - ) - ); - } + // Start of the index + Uncategorized default term. $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'page', 1 ); $request->set_param( 'per_page', 100 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 18, $headers['X-WP-Total'] ); + $this->assertEquals( self::$total_categories, $headers['X-WP-Total'] ); $this->assertEquals( 1, $headers['X-WP-TotalPages'] ); - $this->assertCount( 18, $response->get_data() ); + $this->assertCount( self::$total_categories, $response->get_data() ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories' ); $request->set_param( 'page', 2 ); $request->set_param( 'per_page', 100 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 18, $headers['X-WP-Total'] ); + $this->assertEquals( self::$total_categories, $headers['X-WP-Total'] ); $this->assertEquals( 1, $headers['X-WP-TotalPages'] ); $this->assertCount( 0, $response->get_data() ); } @@ -737,6 +764,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_get_item_invalid_permission_for_context() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -759,12 +787,13 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_get_item_incorrect_taxonomy() { register_taxonomy( 'robin', 'post' ); - $term1 = $this->factory->term->create( + $term1 = $this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'robin', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/categories/' . $term1 ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_term_invalid', $response, 404 ); @@ -772,6 +801,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request->set_param( 'name', 'My Awesome Term' ); $request->set_param( 'description', 'This term is so awesome.' ); @@ -791,6 +821,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas */ public function test_create_item_term_already_exists() { wp_set_current_user( self::$administrator ); + $existing_id = $this->factory->category->create( array( 'name' => 'Existing' ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); @@ -807,6 +838,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item_invalid_taxonomy() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy' ); $request->set_param( 'name', 'Invalid Taxonomy' ); $response = rest_get_server()->dispatch( $request ); @@ -815,6 +847,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item_incorrect_permissions() { wp_set_current_user( self::$subscriber ); + $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request->set_param( 'name', 'Incorrect permissions' ); $response = rest_get_server()->dispatch( $request ); @@ -823,6 +856,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item_incorrect_permissions_contributor() { wp_set_current_user( self::$contributor ); + $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request->set_param( 'name', 'Incorrect permissions' ); $response = rest_get_server()->dispatch( $request ); @@ -831,6 +865,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item_missing_arguments() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 ); @@ -838,7 +873,9 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item_with_parent() { wp_set_current_user( self::$administrator ); - $parent = wp_insert_term( 'test-category', 'category' ); + + $parent = wp_insert_term( 'test-category', 'category' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request->set_param( 'name', 'My Awesome Term' ); $request->set_param( 'parent', $parent['term_id'] ); @@ -850,6 +887,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item_invalid_parent() { wp_set_current_user( self::$administrator ); + $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); @@ -861,7 +899,9 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_create_item_with_no_parent() { wp_set_current_user( self::$administrator ); - $parent = 0; + + $parent = 0; + $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request->set_param( 'name', 'My Awesome Term' ); $request->set_param( 'parent', $parent ); @@ -873,13 +913,16 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_update_item() { wp_set_current_user( self::$administrator ); + $orig_args = array( 'name' => 'Original Name', 'description' => 'Original Description', 'slug' => 'original-slug', ); - $term = get_term_by( 'id', $this->factory->category->create( $orig_args ), 'category' ); - $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); + + $term = get_term_by( 'id', $this->factory->category->create( $orig_args ), 'category' ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); $request->set_param( 'name', 'New Name' ); $request->set_param( 'description', 'New Description' ); $request->set_param( 'slug', 'new-slug' ); @@ -904,6 +947,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_update_item_invalid_taxonomy() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'name', 'Invalid Taxonomy' ); $response = rest_get_server()->dispatch( $request ); @@ -912,6 +956,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_update_item_invalid_term() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'name', 'Invalid Term' ); $response = rest_get_server()->dispatch( $request ); @@ -920,7 +965,9 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_update_item_incorrect_permissions() { wp_set_current_user( self::$subscriber ); - $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); + + $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); $request->set_param( 'name', 'Incorrect permissions' ); $response = rest_get_server()->dispatch( $request ); @@ -929,6 +976,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_update_item_parent() { wp_set_current_user( self::$administrator ); + $parent = get_term_by( 'id', $this->factory->category->create(), 'category' ); $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); @@ -970,6 +1018,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_update_item_invalid_parent() { wp_set_current_user( self::$administrator ); + $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); @@ -980,7 +1029,9 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_delete_item() { wp_set_current_user( self::$administrator ); - $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' ); + + $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id ); $request->set_param( 'force', true ); $response = rest_get_server()->dispatch( $request ); @@ -992,6 +1043,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_delete_item_no_trash() { wp_set_current_user( self::$administrator ); + $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id ); @@ -1005,6 +1057,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_delete_item_invalid_taxonomy() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_no_route', $response, 404 ); @@ -1012,6 +1065,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_delete_item_invalid_term() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_term_invalid', $response, 404 ); @@ -1019,6 +1073,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas public function test_delete_item_incorrect_permissions() { wp_set_current_user( self::$subscriber ); + $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id ); $response = rest_get_server()->dispatch( $request ); diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index aa77f481ab..da2d372a54 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -25,6 +25,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase protected static $approved_id; protected static $hold_id; + protected static $comment_ids = array(); + protected static $total_comments = 30; + protected static $per_page = 50; + protected $endpoint; public static function wpSetUpBeforeClass( $factory ) { @@ -110,6 +114,16 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'user_id' => self::$subscriber_id, ) ); + + // Set up comments for pagination tests. + for ( $i = 0; $i < self::$total_comments - 1; $i++ ) { + $comment_ids[] = $factory->comment->create( + array( + 'comment_content' => "Comment {$i}", + 'comment_post_ID' => self::$post_id, + ) + ); + } } public static function wpTearDownAfterClass() { @@ -129,6 +143,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_delete_post( self::$trash_id, true ); wp_delete_post( self::$approved_id, true ); wp_delete_post( self::$hold_id, true ); + + // Remove comments for pagination tests. + foreach ( self::$comment_ids as $comment_id ) { + wp_delete_comment( $comment_id, true ); + } } public function setUp() { @@ -197,16 +216,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items() { - $this->factory->comment->create_post_comments( self::$post_id, 6 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); - // We created 6 comments in this method, plus self::$approved_id. - $this->assertCount( 7, $comments ); + $this->assertCount( self::$total_comments, $comments ); } /** @@ -215,10 +232,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_with_password() { wp_set_current_user( 0 ); - $args = array( + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$password_id, ); + $password_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -237,10 +255,12 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase */ public function test_get_items_with_password_without_post() { wp_set_current_user( 0 ); - $args = array( + + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$password_id, ); + $password_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -258,10 +278,12 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase */ public function test_get_items_with_password_with_multiple_post() { wp_set_current_user( 0 ); - $args = array( + + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$password_id, ); + $password_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -275,10 +297,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_password_items_without_edit_post_permission() { wp_set_current_user( 0 ); - $args = array( + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$password_id, ); + $password_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -293,10 +316,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_password_items_with_edit_post_permission() { wp_set_current_user( self::$admin_id ); - $args = array( + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$password_id, ); + $password_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -311,10 +335,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_without_private_post_permission() { wp_set_current_user( 0 ); - $args = array( + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$private_id, ); + $private_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -329,10 +354,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_with_private_post_permission() { wp_set_current_user( self::$admin_id ); - $args = array( + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$private_id, ); + $private_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -388,6 +414,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_no_permission_for_context() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -395,8 +422,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_no_post() { - $this->factory->comment->create_post_comments( 0, 2 ); wp_set_current_user( self::$admin_id ); + + $this->factory->comment->create_post_comments( 0, 2 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'post', 0 ); $response = rest_get_server()->dispatch( $request ); @@ -407,6 +436,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_no_permission_for_no_post() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'post', 0 ); $response = rest_get_server()->dispatch( $request ); @@ -415,6 +445,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_edit_context() { wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -441,32 +472,38 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_include_query() { wp_set_current_user( self::$admin_id ); + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$post_id, ); - $id1 = $this->factory->comment->create( $args ); - $this->factory->comment->create( $args ); - $id3 = $this->factory->comment->create( $args ); + + $id1 = $this->factory->comment->create( $args ); + $id2 = $this->factory->comment->create( $args ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - // Order=>asc + + // 'order' => 'asc'. $request->set_param( 'order', 'asc' ); - $request->set_param( 'include', array( $id3, $id1 ) ); + $request->set_param( 'include', array( $id2, $id1 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( $id1, $data[0]['id'] ); - // Orderby=>include + + // 'orderby' => 'include'. $request->set_param( 'orderby', 'include' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - $this->assertEquals( $id3, $data[0]['id'] ); - // Orderby=>invalid should fail. + $this->assertEquals( $id2, $data[0]['id'] ); + + // Invalid 'orderby' should error. $request->set_param( 'orderby', 'invalid' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); - // fails on invalid id. + + // Invalid 'include' should error. $request->set_param( 'orderby', array( 'include' ) ); $request->set_param( 'include', array( 'invalid' ) ); $response = rest_get_server()->dispatch( $request ); @@ -475,24 +512,30 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_exclude_query() { wp_set_current_user( self::$admin_id ); - $args = array( + + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$post_id, ); - $id1 = $this->factory->comment->create( $args ); - $id2 = $this->factory->comment->create( $args ); + + $id1 = $this->factory->comment->create( $args ); + $id2 = $this->factory->comment->create( $args ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertTrue( in_array( $id2, $ids, true ) ); + $request->set_param( 'exclude', array( $id2 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertFalse( in_array( $id2, $ids, true ) ); - // fails on invalid id. + // Invalid 'exclude' should error. $request->set_param( 'exclude', array( 'invalid' ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -500,26 +543,24 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_offset_query() { wp_set_current_user( self::$admin_id ); - $args = array( - 'comment_approved' => 1, - 'comment_post_ID' => self::$post_id, - ); - $this->factory->comment->create( $args ); - $this->factory->comment->create( $args ); - $this->factory->comment->create( $args ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'offset', 1 ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 3, $response->get_data() ); - // 'offset' works with 'per_page' + $this->assertCount( self::$total_comments - 1, $response->get_data() ); + + // 'offset' works with 'per_page'. $request->set_param( 'per_page', 2 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // 'offset' takes priority over 'page' + + // 'offset' takes priority over 'page'. $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // 'offset' with invalid value errors. + + // Invalid 'offset' should error. $request->set_param( 'offset', 'moreplease' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -527,24 +568,28 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_order_query() { wp_set_current_user( self::$admin_id ); + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$post_id, ); - $this->factory->comment->create( $args ); - $this->factory->comment->create( $args ); - $id3 = $this->factory->comment->create( $args ); + + $id = $this->factory->comment->create( $args ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - // order defaults to 'desc' + + // Order defaults to 'desc'. $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( $id3, $data[0]['id'] ); - // order=>asc + $this->assertEquals( $id, $data[0]['id'] ); + + // 'order' => 'asc'. $request->set_param( 'order', 'asc' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( self::$approved_id, $data[0]['id'] ); - // order=>asc,id should fail + + // 'order' => 'asc,id' should error. $request->set_param( 'order', 'asc,id' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -552,7 +597,9 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_private_post_no_permissions() { wp_set_current_user( 0 ); + $post_id = $this->factory->post->create( array( 'post_status' => 'private' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'post', $post_id ); $response = rest_get_server()->dispatch( $request ); @@ -560,37 +607,42 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_author_arg() { - // Authorized + // Authorized. wp_set_current_user( self::$admin_id ); + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$post_id, 'user_id' => self::$author_id, ); + $this->factory->comment->create( $args ); $args['user_id'] = self::$subscriber_id; $this->factory->comment->create( $args ); unset( $args['user_id'] ); $this->factory->comment->create( $args ); - // 'author' limits result to 1 of 3 + // Limit to comment author. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'author', self::$author_id ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); $this->assertCount( 1, $comments ); - // Multiple authors are supported + + // Multiple authors are supported. $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); $this->assertCount( 2, $comments ); - // Invalid author param errors + + // Invalid 'author' should error. $request->set_param( 'author', 'skippy' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); - // Unavailable to unauthenticated; defaults to error + + // Unavailable to unauthenticated; defaults to error. wp_set_current_user( 0 ); $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) ); $response = rest_get_server()->dispatch( $request ); @@ -598,44 +650,54 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_author_exclude_arg() { - // Authorized + // Authorized. wp_set_current_user( self::$admin_id ); + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$post_id, 'user_id' => self::$author_id, ); + $this->factory->comment->create( $args ); $args['user_id'] = self::$subscriber_id; $this->factory->comment->create( $args ); unset( $args['user_id'] ); $this->factory->comment->create( $args ); - $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $total_comments = self::$total_comments + 3; + + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $comments = $response->get_data(); - $this->assertCount( 4, $comments ); + $this->assertCount( $total_comments, $comments ); - // 'author_exclude' limits result to 3 of 4 + // Exclude comment author. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'author_exclude', self::$author_id ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); - $this->assertCount( 3, $comments ); - // 'author_exclude' for both comment authors (2 of 4) + $this->assertCount( $total_comments - 1, $comments ); + + // Exclude both comment authors. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); - $this->assertCount( 2, $comments ); - // 'author_exclude' for both invalid author + $this->assertCount( $total_comments - 2, $comments ); + + // 'author_exclude' for invalid author. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'author_exclude', 'skippy' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); - // Unavailable to unauthenticated; defaults to error + + // Unavailable to unauthenticated; defaults to error. wp_set_current_user( 0 ); $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) ); $response = rest_get_server()->dispatch( $request ); @@ -653,19 +715,26 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->factory->comment->create( $args ); $args['comment_parent'] = $parent_id2; $this->factory->comment->create( $args ); - // All comments in the database - $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + + $total_comments = self::$total_comments + 4; + + // All comments in the database. + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 5, $response->get_data() ); - // Limit to the parent + $this->assertCount( $total_comments, $response->get_data() ); + + // Limit to the parent. $request->set_param( 'parent', $parent_id ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 1, $response->get_data() ); - // Limit to two parents + + // Limit to two parents. $request->set_param( 'parent', array( $parent_id, $parent_id2 ) ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // Invalid parent should error + + // Invalid 'parent' should error. $request->set_param( 'parent', 'invalid' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -682,19 +751,26 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->factory->comment->create( $args ); $args['comment_parent'] = $parent_id2; $this->factory->comment->create( $args ); - // All comments in the database - $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + + $total_comments = self::$total_comments + 4; + + // All comments in the database. + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 5, $response->get_data() ); - // Exclude this particular parent + $this->assertCount( $total_comments, $response->get_data() ); + + // Exclude this particular parent. $request->set_param( 'parent_exclude', $parent_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 4, $response->get_data() ); - // Exclude both comment parents + $this->assertCount( $total_comments - 1, $response->get_data() ); + + // Exclude both comment parents. $request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 3, $response->get_data() ); - // Invalid parent id should error + $this->assertCount( $total_comments - 2, $response->get_data() ); + + // Invalid 'parent_exclude' should error. $request->set_param( 'parent_exclude', 'invalid' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -702,45 +778,43 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_search_query() { wp_set_current_user( self::$admin_id ); - $args = array( + + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$post_id, 'comment_content' => 'foo', 'comment_author' => 'Homer J Simpson', ); - $id1 = $this->factory->comment->create( $args ); - $args['comment_content'] = 'bar'; - $this->factory->comment->create( $args ); - $args['comment_content'] = 'burrito'; - $this->factory->comment->create( $args ); - // 3 comments, plus 1 created in construct - $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + + $id = $this->factory->comment->create( $args ); + + $total_comments = self::$total_comments + 1; + + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 4, $response->get_data() ); - // One matching comments + $this->assertCount( $total_comments, $response->get_data() ); + + // One matching comment. $request->set_param( 'search', 'foo' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertCount( 1, $data ); - $this->assertEquals( $id1, $data[0]['id'] ); + $this->assertEquals( $id, $data[0]['id'] ); } public function test_get_comments_pagination_headers() { + $total_comments = self::$total_comments; + $total_pages = (int) ceil( $total_comments / 10 ); + wp_set_current_user( self::$admin_id ); - // Start of the index - for ( $i = 0; $i < 49; $i++ ) { - $this->factory->comment->create( - array( - 'comment_content' => "Comment {$i}", - 'comment_post_ID' => self::$post_id, - ) - ); - } + + // Start of the index. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 50, $headers['X-WP-Total'] ); - $this->assertEquals( 5, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $next_link = add_query_arg( array( 'page' => 2, @@ -749,19 +823,21 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase ); $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // 3rd page + + // 3rd page. $this->factory->comment->create( array( - 'comment_content' => 'Comment 51', 'comment_post_ID' => self::$post_id, ) ); + $total_comments++; + $total_pages++; $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( 'page' => 2, @@ -776,31 +852,33 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase rest_url( '/wp/v2/comments' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // Last page + + // Last page. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - $request->set_param( 'page', 6 ); + $request->set_param( 'page', $total_pages ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( - 'page' => 5, + 'page' => $total_pages - 1, ), rest_url( '/wp/v2/comments' ) ); $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); - // Out of bounds + + // Out of bounds. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - $request->set_param( 'page', 8 ); + $request->set_param( 'page', 100 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_comments, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( - 'page' => 6, + 'page' => $total_pages, ), rest_url( '/wp/v2/comments' ) ); @@ -857,6 +935,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_prepare_item() { wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->set_query_params( array( @@ -873,6 +952,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_prepare_item_limit_fields() { wp_set_current_user( self::$admin_id ); + $endpoint = new WP_REST_Comments_Controller; $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->set_param( 'context', 'edit' ); @@ -915,6 +995,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_invalid_context() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -923,28 +1004,30 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_invalid_post_id() { wp_set_current_user( 0 ); + $comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, 'comment_post_ID' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ) ); - $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); } public function test_get_comment_invalid_post_id_as_admin() { wp_set_current_user( self::$admin_id ); + $comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, 'comment_post_ID' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ) ); - $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); } @@ -952,8 +1035,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_not_approved() { wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); - + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); } @@ -961,8 +1043,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_not_approved_same_user() { wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); - + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); } @@ -1008,13 +1089,16 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_with_password_without_edit_post_permission() { wp_set_current_user( self::$subscriber_id ); - $args = array( + + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$password_id, ); + $password_comment = $this->factory->comment->create( $args ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) ); - $response = rest_get_server()->dispatch( $request ); + + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) ); + $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); } @@ -1024,10 +1108,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_with_password_with_valid_password() { wp_set_current_user( self::$subscriber_id ); - $args = array( + $args = array( 'comment_approved' => 1, 'comment_post_ID' => self::$password_id, ); + $password_comment = $this->factory->comment->create( $args ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) ); @@ -1113,6 +1198,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase */ public function test_create_comment_date( $params, $results ) { wp_set_current_user( self::$admin_id ); + update_option( 'timezone_string', $params['timezone_string'] ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); @@ -1233,6 +1319,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_missing_required_author_email() { wp_set_current_user( self::$admin_id ); + update_option( 'require_name_email', 1 ); $params = array( @@ -1251,6 +1338,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_empty_required_author_email() { wp_set_current_user( self::$admin_id ); + update_option( 'require_name_email', 1 ); $params = array( @@ -1342,7 +1430,8 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase ); wp_set_current_user( self::$admin_id ); - $params = array( + + $params = array( 'post' => self::$post_id, 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', @@ -1351,6 +1440,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'Worst Comment Ever!', 'date' => '2014-11-07T10:14:25', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -1364,6 +1454,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_without_type() { $post_id = $this->factory->post->create(); + wp_set_current_user( self::$admin_id ); $params = array( @@ -1401,6 +1492,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase */ public function test_create_comment_with_invalid_type() { $post_id = $this->factory->post->create(); + wp_set_current_user( self::$admin_id ); $params = array( @@ -1424,6 +1516,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_invalid_email() { $post_id = $this->factory->post->create(); + wp_set_current_user( self::$admin_id ); $params = array( @@ -1568,6 +1661,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_with_status_IP_and_user_agent() { $post_id = $this->factory->post->create(); + wp_set_current_user( self::$admin_id ); $params = array( @@ -1662,7 +1756,8 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_author_ip_no_permission() { wp_set_current_user( self::$subscriber_id ); - $params = array( + + $params = array( 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', @@ -1670,6 +1765,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'Worst Comment Ever!', 'status' => 'approved', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -1679,15 +1775,18 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_author_ip_defaults_to_remote_addr() { wp_set_current_user( self::$admin_id ); + $_SERVER['REMOTE_ADDR'] = '127.0.0.2'; - $params = array( + + $params = array( 'post' => self::$post_id, 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', 'content' => 'Worst Comment Ever!', ); - $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); @@ -1699,13 +1798,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_no_post_id() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', 'content' => 'Worst Comment Ever!', 'status' => 'approved', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -1718,13 +1818,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_no_post_id_no_permission() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'author_name' => 'Homer Jay Simpson', 'author_email' => 'chunkylover53@aol.com', 'author_url' => 'http://compuglobalhypermeganet.com', 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.', 'author' => self::$subscriber_id, ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -1756,7 +1857,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_draft_post() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$draft_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', @@ -1764,19 +1865,19 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'Call me Ishmael.', 'author' => self::$subscriber_id, ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_comment_draft_post', $response, 403 ); } public function test_create_comment_trash_post() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$trash_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', @@ -1784,6 +1885,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'Call me Ishmael.', 'author' => self::$subscriber_id, ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -1796,7 +1898,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_private_post_invalid_permission() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$private_id, 'author_name' => 'Homer Jay Simpson', 'author_email' => 'chunkylover53@aol.com', @@ -1804,19 +1906,19 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'I\’d be a vegetarian if bacon grew on trees.', 'author' => self::$subscriber_id, ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 ); } public function test_create_comment_password_post_invalid_permission() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$password_id, 'author_name' => 'Homer Jay Simpson', 'author_email' => 'chunkylover53@aol.com', @@ -1824,6 +1926,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'I\’d be a vegetarian if bacon grew on trees.', 'author' => self::$subscriber_id, ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -1834,6 +1937,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_item_duplicate() { wp_set_current_user( self::$subscriber_id ); + $this->factory->comment->create( array( 'comment_post_ID' => self::$post_id, @@ -1864,6 +1968,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'comment_status' => 'closed', ) ); + wp_set_current_user( self::$subscriber_id ); $params = array( @@ -1880,8 +1985,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_require_login() { wp_set_current_user( 0 ); + update_option( 'comment_registration', 1 ); add_filter( 'rest_allow_anonymous_comments', '__return_true' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->set_param( 'post', self::$post_id ); $response = rest_get_server()->dispatch( $request ); @@ -1997,7 +2104,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_author_name_too_long() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$post_id, 'author_name' => rand_long_str( 246 ), 'author_email' => 'murphy@gingivitis.com', @@ -2005,6 +2112,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 'date' => '1995-04-30T10:22:00', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -2020,7 +2128,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_author_email_too_long() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$post_id, 'author_name' => 'Bleeding Gums Murphy', 'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com', @@ -2028,6 +2136,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 'date' => '1995-04-30T10:22:00', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -2043,7 +2152,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_author_url_too_long() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$post_id, 'author_name' => 'Bleeding Gums Murphy', 'author_email' => 'murphy@gingivitis.com', @@ -2051,6 +2160,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 'date' => '1995-04-30T10:22:00', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -2066,7 +2176,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_content_too_long() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$post_id, 'author_name' => 'Bleeding Gums Murphy', 'author_email' => 'murphy@gingivitis.com', @@ -2074,6 +2184,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => rand_long_str( 66525 ), 'date' => '1995-04-30T10:22:00', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -2086,13 +2197,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_without_password() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'post' => self::$password_id, 'author_name' => 'Bleeding Gums Murphy', 'author_email' => 'murphy@gingivitis.com', 'author_url' => 'http://jazz.gingivitis.com', 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -2105,7 +2217,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_comment_with_password() { add_filter( 'rest_allow_anonymous_comments', '__return_true' ); - $params = array( + $params = array( 'post' => self::$password_id, 'author_name' => 'Bleeding Gums Murphy', 'author_email' => 'murphy@gingivitis.com', @@ -2113,6 +2225,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', 'password' => 'toomanysecrets', ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -2126,7 +2239,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'author' => self::$subscriber_id, 'author_name' => 'Disco Stu', 'author_url' => 'http://stusdisco.com', @@ -2136,6 +2249,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'date' => '2014-11-07T10:14:25', 'post' => $post_id, ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2162,6 +2276,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase */ public function test_update_comment_date( $params, $results ) { wp_set_current_user( self::$editor_id ); + update_option( 'timezone_string', $params['timezone_string'] ); $comment_id = $this->factory->comment->create(); @@ -2213,6 +2328,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $comment = get_comment( self::$approved_id ); wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->set_param( 'post', $comment->comment_post_ID ); @@ -2235,9 +2351,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase ) ); - $params = array( + $params = array( 'status' => 'approve', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2262,9 +2379,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase ) ); - $params = array( + $params = array( 'status' => 'approve', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2282,10 +2400,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_date_gmt() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'date_gmt' => '2015-05-07T10:14:25', 'content' => 'I\'ll be deep in the cold, cold ground before I recognize Missouri.', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2301,6 +2420,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_author_email_only() { wp_set_current_user( self::$editor_id ); + update_option( 'require_name_email', 1 ); $params = array( @@ -2319,6 +2439,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_empty_author_name() { wp_set_current_user( self::$editor_id ); + update_option( 'require_name_email', 1 ); $params = array( @@ -2338,6 +2459,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_author_name_only() { wp_set_current_user( self::$admin_id ); + update_option( 'require_name_email', 1 ); $params = array( @@ -2356,6 +2478,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_empty_author_email() { wp_set_current_user( self::$admin_id ); + update_option( 'require_name_email', 1 ); $params = array( @@ -2396,9 +2519,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_invalid_type() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'type' => 'trackback', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2410,11 +2534,12 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_with_raw_property() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'content' => array( 'raw' => 'What the heck kind of name is Persephone?', ), ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2463,9 +2588,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_invalid_id() { wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'content' => 'Oh, they have the internet on computers now!', ); + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2487,9 +2613,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_invalid_permission() { add_filter( 'rest_allow_anonymous_comments', '__return_true' ); - $params = array( + $params = array( 'content' => 'Disco Stu likes disco music.', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2504,11 +2631,12 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_when_can_moderate_comments() { wp_set_current_user( self::$moderator_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); - $params = array( + $params = array( 'content' => 'Updated comment.', 'date' => '2019-10-07T23:14:25', ); + + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2534,9 +2662,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( self::$subscriber_id ); - $params = array( + $params = array( 'content' => 'Disco Stu likes disco music.', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $private_comment_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); @@ -2547,6 +2676,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_with_children_link() { wp_set_current_user( self::$admin_id ); + $comment_id_1 = $this->factory->comment->create( array( 'comment_approved' => 1, @@ -2589,10 +2719,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_author_name_too_long() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'author_name' => rand_long_str( 246 ), 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -2608,10 +2739,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_author_email_too_long() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com', 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -2627,10 +2759,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_author_url_too_long() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'author_url' => 'http://jazz.' . rand_long_str( 185 ) . '.com', 'content' => 'This isn\'t a saxophone. It\'s an umbrella.', ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -2646,9 +2779,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_content_too_long() { wp_set_current_user( self::$admin_id ); - $params = array( + $params = array( 'content' => rand_long_str( 66525 ), ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -2711,6 +2845,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_comment_roundtrip_as_editor() { wp_set_current_user( self::$editor_id ); + $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); $this->verify_comment_roundtrip( array( @@ -2731,6 +2866,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_comment_roundtrip_as_editor_unfiltered_html() { wp_set_current_user( self::$editor_id ); + if ( is_multisite() ) { $this->assertFalse( current_user_can( 'unfiltered_html' ) ); $this->verify_comment_roundtrip( @@ -2770,6 +2906,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_comment_roundtrip_as_superadmin() { wp_set_current_user( self::$superadmin_id ); + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); $this->verify_comment_roundtrip( array( @@ -2790,6 +2927,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_comment_roundtrip_as_superadmin_unfiltered_html() { wp_set_current_user( self::$superadmin_id ); + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); $this->verify_comment_roundtrip( array( @@ -2831,13 +2969,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_delete_item_skip_trash() { wp_set_current_user( self::$admin_id ); - $comment_id = $this->factory->comment->create( + $comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, 'comment_post_ID' => self::$post_id, 'user_id' => self::$subscriber_id, ) ); + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); $request['force'] = true; @@ -2858,8 +2997,9 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'user_id' => self::$subscriber_id, ) ); - $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); - $response = rest_get_server()->dispatch( $request ); + + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); + $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); $response = rest_get_server()->dispatch( $request ); @@ -2869,8 +3009,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_delete_comment_invalid_id() { wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 ); } @@ -2878,14 +3017,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_delete_comment_without_permission() { wp_set_current_user( self::$subscriber_id ); - $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); - + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); } public function test_delete_child_comment_link() { wp_set_current_user( self::$admin_id ); + $comment_id_1 = $this->factory->comment->create( array( 'comment_approved' => 1, @@ -2947,6 +3086,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_item_schema_show_avatar() { update_option( 'show_avatars', false ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -2974,16 +3114,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase ) ); - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' ); - + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] ); $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] ); - $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id ); - + $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id ); $response = rest_get_server()->dispatch( $request ); $this->assertArrayHasKey( 'my_custom_int', $response->data ); diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index 79da6b05e8..dc2d41f567 100644 --- a/tests/phpunit/tests/rest-api/rest-pages-controller.php +++ b/tests/phpunit/tests/rest-api/rest-pages-controller.php @@ -125,18 +125,21 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_parent' => $id1, ) ); - // No parent + + // No parent. $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - // Filter to parent + + // Filter to parent. $request->set_param( 'parent', $id1 ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); $this->assertEquals( $id2, $data[0]['id'] ); - // Invalid parent should fail + + // Invalid 'parent' should error. $request->set_param( 'parent', 'some-slug' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -169,12 +172,14 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_parent' => $id3, ) ); - // No parent + + // No parent. $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 4, count( $data ) ); - // Filter to parents + + // Filter to parents. $request->set_param( 'parent', array( $id1, $id3 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -196,18 +201,21 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_parent' => $id1, ) ); - // No parent + + // No parent. $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - // Filter to parent + + // Filter to parent. $request->set_param( 'parent_exclude', $id1 ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); $this->assertEquals( $id1, $data[0]['id'] ); - // Invalid parent_exclude should error + + // Invalid 'parent_exclude' should error. $request->set_param( 'parent_exclude', 'some-slug' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -241,17 +249,20 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 'menu_order' => 1, ) ); - // No parent + + // No parent. $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEqualSets( array( $id1, $id2, $id3, $id4 ), wp_list_pluck( $data, 'id' ) ); - // Filter to menu_order + + // Filter to 'menu_order'. $request->set_param( 'menu_order', 1 ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEqualSets( array( $id4 ), wp_list_pluck( $data, 'id' ) ); - // Order by menu order + + // Order by 'menu order'. $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); $request->set_param( 'order', 'asc' ); $request->set_param( 'orderby', 'menu_order' ); @@ -261,7 +272,8 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $id4, $data[1]['id'] ); $this->assertEquals( $id2, $data[2]['id'] ); $this->assertEquals( $id3, $data[3]['id'] ); - // Invalid menu_order should fail + + // Invalid 'menu_order' should error. $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); $request->set_param( 'menu_order', 'top-first' ); $response = rest_get_server()->dispatch( $request ); diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 105e1565f2..172e8b648f 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -19,6 +19,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te protected static $private_reader_id; protected static $supported_formats; + protected static $post_ids = array(); + protected static $total_posts = 30; + protected static $per_page = 50; protected $forbidden_cat; protected $posts_clauses; @@ -61,6 +64,15 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te // Only support 'post' and 'gallery' self::$supported_formats = get_theme_support( 'post-formats' ); add_theme_support( 'post-formats', array( 'post', 'gallery' ) ); + + // Set up posts for pagination tests. + for ( $i = 0; $i < self::$total_posts - 1; $i++ ) { + self::$post_ids[] = $factory->post->create( + array( + 'post_title' => "Post {$i}", + ) + ); + } } public static function wpTearDownAfterClass() { @@ -71,6 +83,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te remove_theme_support( 'post-formats' ); } + // Remove posts for pagination tests. + foreach ( self::$post_ids as $post_id ) { + wp_delete_post( $post_id, true ); + } + wp_delete_post( self::$post_id, true ); self::delete_user( self::$superadmin_id ); @@ -203,6 +220,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $headers['Allow'], 'GET' ); wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = rest_get_server()->dispatch( $request ); $response = apply_filters( 'rest_post_dispatch', $response, rest_get_server(), $request ); @@ -240,12 +258,17 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_author_query() { $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); $this->factory->post->create( array( 'post_author' => self::$author_id ) ); - // All 3 posts - $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + + $total_posts = self::$total_posts + 2; + + // All posts in the database. + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 3, count( $response->get_data() ) ); - // 2 of 3 posts + $this->assertEquals( $total_posts, count( $response->get_data() ) ); + + // Limit to editor and author. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'author', array( self::$editor_id, self::$author_id ) ); $response = rest_get_server()->dispatch( $request ); @@ -253,7 +276,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEqualSets( array( self::$editor_id, self::$author_id ), wp_list_pluck( $data, 'author' ) ); - // 1 of 3 posts + + // Limit to editor. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'author', self::$editor_id ); $response = rest_get_server()->dispatch( $request ); @@ -266,30 +290,39 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_author_exclude_query() { $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); $this->factory->post->create( array( 'post_author' => self::$author_id ) ); - // All 3 posts - $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + + $total_posts = self::$total_posts + 2; + + // All posts in the database. + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 3, count( $response->get_data() ) ); - // 1 of 3 posts + $this->assertEquals( $total_posts, count( $response->get_data() ) ); + + // Exclude editor and author. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'author_exclude', array( self::$editor_id, self::$author_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( 1, count( $data ) ); + $this->assertEquals( $total_posts - 2, count( $data ) ); $this->assertNotEquals( self::$editor_id, $data[0]['author'] ); $this->assertNotEquals( self::$author_id, $data[0]['author'] ); - // 2 of 3 posts + + // Exclude editor. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'author_exclude', self::$editor_id ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( 2, count( $data ) ); + $this->assertEquals( $total_posts - 1, count( $data ) ); $this->assertNotEquals( self::$editor_id, $data[0]['author'] ); $this->assertNotEquals( self::$editor_id, $data[1]['author'] ); - // invalid author_exclude errors + + // Invalid 'author_exclude' should error. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'author_exclude', 'invalid' ); $response = rest_get_server()->dispatch( $request ); @@ -298,24 +331,27 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_include_query() { $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); - // Orderby=>desc - $request->set_param( 'include', array( $id1, $id3 ) ); + + // Order defaults to 'desc'. + $request->set_param( 'include', array( $id1, $id2 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - $this->assertEquals( $id3, $data[0]['id'] ); + $this->assertEquals( $id2, $data[0]['id'] ); $this->assertPostsOrderedBy( '{posts}.post_date DESC' ); - // Orderby=>include + + // 'orderby' => 'include' $request->set_param( 'orderby', 'include' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( $id1, $data[0]['id'] ); - $this->assertPostsOrderedBy( "FIELD({posts}.ID,$id1,$id3)" ); - // Invalid include should error + $this->assertPostsOrderedBy( "FIELD({posts}.ID,$id1,$id2)" ); + + // Invalid 'include' should error. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'include', 'invalid' ); $response = rest_get_server()->dispatch( $request ); @@ -420,25 +456,29 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_exclude_query() { - $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertTrue( in_array( $id2, $ids, true ) ); $request->set_param( 'exclude', array( $id2 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertFalse( in_array( $id2, $ids, true ) ); $request->set_param( 'exclude', "$id2" ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertFalse( in_array( $id2, $ids, true ) ); $request->set_param( 'exclude', 'invalid' ); $response = rest_get_server()->dispatch( $request ); @@ -446,18 +486,19 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_search_query() { - for ( $i = 0; $i < 5; $i++ ) { - $this->factory->post->create( array( 'post_status' => 'publish' ) ); - } $this->factory->post->create( array( 'post_title' => 'Search Result', 'post_status' => 'publish', ) ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $total_posts = self::$total_posts + 1; + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); - $this->assertEquals( 7, count( $response->get_data() ) ); + $this->assertEquals( $total_posts, count( $response->get_data() ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'search', 'Search Result' ); $response = rest_get_server()->dispatch( $request ); @@ -479,6 +520,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_status' => 'publish', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'slug', 'apple' ); $response = rest_get_server()->dispatch( $request ); @@ -507,6 +549,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_status' => 'publish', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'slug', array( 'banana', 'peach' ) ); $response = rest_get_server()->dispatch( $request ); @@ -540,6 +583,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_status' => 'publish', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'slug', 'apple,banana' ); $response = rest_get_server()->dispatch( $request ); @@ -556,17 +600,23 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_status_query() { wp_set_current_user( 0 ); + $this->factory->post->create( array( 'post_status' => 'draft' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'status', 'publish' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $response->get_data() ) ); + $this->assertEquals( self::$total_posts, count( $response->get_data() ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'draft' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'draft' ); $response = rest_get_server()->dispatch( $request ); @@ -635,6 +685,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $private_post_id = $this->factory->post->create( array( 'post_status' => 'private' ) ); wp_set_current_user( self::$private_reader_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', array( 'private', 'future' ) ); @@ -644,6 +695,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_invalid_status_query() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'invalid' ); $response = rest_get_server()->dispatch( $request ); @@ -656,6 +708,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_status' => 'draft', ) ); + wp_set_current_user( 0 ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); @@ -694,25 +747,30 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_status' => 'publish', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'search', 'Apple' ); - // order defaults to 'desc' + + // Order defaults to 'desc'. $request->set_param( 'orderby', 'title' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] ); $this->assertPostsOrderedBy( '{posts}.post_title DESC' ); - // order=>asc + + // 'order' => 'asc'. $request->set_param( 'order', 'asc' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] ); $this->assertPostsOrderedBy( '{posts}.post_title ASC' ); - // order=>asc,id should fail + + // 'order' => 'asc,id' should error. $request->set_param( 'order', 'asc,id' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); - // orderby=>content should fail (invalid param test) + + // 'orderby' => 'content' should error (invalid param test). $request->set_param( 'order', 'asc' ); $request->set_param( 'orderby', 'content' ); $response = rest_get_server()->dispatch( $request ); @@ -721,6 +779,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_with_orderby_include_without_include_param() { $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'orderby', 'include' ); @@ -817,20 +876,21 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_with_orderby_relevance() { - $id1 = $this->factory->post->create( + $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish', ) ); - $id2 = $this->factory->post->create( + $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'orderby', 'relevance' ); $request->set_param( 'search', 'relevant' ); @@ -844,20 +904,21 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_with_orderby_relevance_two_terms() { - $id1 = $this->factory->post->create( + $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish', ) ); - $id2 = $this->factory->post->create( + $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'orderby', 'relevance' ); $request->set_param( 'search', 'relevant content' ); @@ -878,23 +939,23 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_offset_query() { - $id1 = self::$post_id; - $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'offset', 1 ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 3, $response->get_data() ); - // 'offset' works with 'per_page' + $this->assertCount( self::$total_posts - 1, $response->get_data() ); + + // 'offset' works with 'per_page'. $request->set_param( 'per_page', 2 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // 'offset' takes priority over 'page' + + // 'offset' takes priority over 'page'. $request->set_param( 'page', 2 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // Invalid 'offset' should error + + // Invalid 'offset' should error. $request->set_param( 'offset', 'moreplease' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -902,12 +963,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_tags_query() { $id1 = self::$post_id; - $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $tag = wp_insert_term( 'My Tag', 'post_tag' ); wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'tags', array( $tag['term_id'] ) ); @@ -924,13 +983,17 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $tag = wp_insert_term( 'My Tag', 'post_tag' ); + $total_posts = self::$total_posts + 3; + wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'tags_exclude', array( $tag['term_id'] ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertCount( 3, $data ); + $this->assertCount( $total_posts - 1, $data ); $this->assertEquals( $id4, $data[0]['id'] ); $this->assertEquals( $id3, $data[1]['id'] ); $this->assertEquals( $id2, $data[2]['id'] ); @@ -939,8 +1002,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_tags_and_categories_query() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $tag = wp_insert_term( 'My Tag', 'post_tag' ); $category = wp_insert_term( 'My Category', 'category' ); @@ -966,8 +1027,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_tags_or_categories_query() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $tag = wp_insert_term( 'My Tag', 'post_tag' ); $category = wp_insert_term( 'My Category', 'category' ); @@ -990,8 +1049,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_tags_and_categories_exclude_query() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $tag = wp_insert_term( 'My Tag', 'post_tag' ); $category = wp_insert_term( 'My Category', 'category' ); @@ -1017,19 +1074,22 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te * @ticket 44326 */ public function test_get_items_tags_or_categories_exclude_query() { - $id1 = self::$post_id; + $id1 = end( self::$post_ids ); $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $tag = wp_insert_term( 'My Tag', 'post_tag' ); $category = wp_insert_term( 'My Category', 'category' ); + $total_posts = self::$total_posts + 3; + wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' ); wp_set_object_terms( $id2, array( $category['term_id'] ), 'category' ); wp_set_object_terms( $id3, array( $category['term_id'] ), 'category' ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'tags', array( $tag['term_id'] ) ); $request->set_param( 'categories_exclude', array( $category['term_id'] ) ); $request->set_param( 'tax_relation', 'OR' ); @@ -1037,7 +1097,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertCount( 3, $data ); + $this->assertCount( $total_posts - 1, $data ); $this->assertEquals( $id4, $data[0]['id'] ); $this->assertEquals( $id2, $data[1]['id'] ); $this->assertEquals( $id1, $data[2]['id'] ); @@ -1081,7 +1141,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_items_sticky_with_include() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); - $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); update_option( 'sticky_posts', array( $id2 ) ); @@ -1116,8 +1175,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_sticky_no_sticky_posts() { - $id1 = self::$post_id; - update_option( 'sticky_posts', array() ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); @@ -1153,16 +1210,19 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_not_sticky() { - $id1 = self::$post_id; + $id1 = end( self::$post_ids ); $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $total_posts = self::$total_posts + 1; + update_option( 'sticky_posts', array( $id2 ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'sticky', false ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 1, $response->get_data() ); + $this->assertCount( $total_posts - 1, $response->get_data() ); $posts = $response->get_data(); $post = $posts[0]; @@ -1172,22 +1232,27 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_not_sticky_with_exclude() { - $id1 = self::$post_id; + $id1 = end( self::$post_ids ); $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $total_posts = self::$total_posts + 2; + update_option( 'sticky_posts', array( $id2 ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'sticky', false ); $request->set_param( 'exclude', array( $id3 ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 1, $response->get_data() ); + $this->assertCount( $total_posts - 2, $response->get_data() ); $posts = $response->get_data(); - $post = $posts[0]; - $this->assertEquals( $id1, $post['id'] ); + $ids = wp_list_pluck( $posts, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertFalse( in_array( $id2, $ids, true ) ); + $this->assertFalse( in_array( $id3, $ids, true ) ); $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); } @@ -1197,37 +1262,37 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $total_posts = self::$total_posts + 2; + update_option( 'sticky_posts', array() ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'sticky', false ); $request->set_param( 'exclude', array( $id3 ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 2, $response->get_data() ); + $this->assertCount( $total_posts - 1, $response->get_data() ); $posts = $response->get_data(); $ids = wp_list_pluck( $posts, 'id' ); - sort( $ids ); - $this->assertEquals( array( $id1, $id2 ), $ids ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertTrue( in_array( $id2, $ids, true ) ); + $this->assertFalse( in_array( $id3, $ids, true ) ); $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); } public function test_get_items_pagination_headers() { - // Start of the index - for ( $i = 0; $i < 49; $i++ ) { - $this->factory->post->create( - array( - 'post_title' => "Post {$i}", - ) - ); - } + $total_posts = self::$total_posts; + $total_pages = (int) ceil( $total_posts / 10 ); + + // Start of the index. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 50, $headers['X-WP-Total'] ); - $this->assertEquals( 5, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $next_link = add_query_arg( array( 'page' => 2, @@ -1236,18 +1301,17 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ); $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // 3rd page - $this->factory->post->create( - array( - 'post_title' => 'Post 51', - ) - ); + + // 3rd page. + $this->factory->post->create(); + $total_posts++; + $total_pages++; $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( 'page' => 2, @@ -1262,31 +1326,33 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te rest_url( '/wp/v2/posts' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // Last page + + // Last page. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); - $request->set_param( 'page', 6 ); + $request->set_param( 'page', $total_pages ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( - 'page' => 5, + 'page' => $total_pages - 1, ), rest_url( '/wp/v2/posts' ) ); $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); - // Out of bounds + // Out of bounds. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); - $request->set_param( 'page', 8 ); + $request->set_param( 'page', 100 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); $this->assertErrorResponse( 'rest_post_invalid_page_number', $response, 400 ); // With query params. - $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $total_pages = (int) ceil( $total_posts / 5 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_query_params( array( 'per_page' => 5, @@ -1295,8 +1361,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 11, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_posts, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( 'per_page' => 5, @@ -1320,6 +1386,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te // Drafts status query var inaccessible to unauthorized users. wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'draft' ); $response = rest_get_server()->dispatch( $request ); @@ -1327,6 +1394,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te // Users with 'read_private_posts' cap shouldn't also be able to view drafts. wp_set_current_user( self::$private_reader_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'draft' ); $response = rest_get_server()->dispatch( $request ); @@ -1334,9 +1402,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te // But drafts are accessible to authorized users. wp_set_current_user( self::$editor_id ); + $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( $draft_id, $data[0]['id'] ); } @@ -1347,12 +1415,14 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $private_post_id = $this->factory->post->create( array( 'post_status' => 'private' ) ); wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'private' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); wp_set_current_user( self::$private_reader_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'private' ); @@ -1520,6 +1590,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_status' => 'draft', ) ); + wp_set_current_user( 0 ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $draft_id ) ); @@ -1552,6 +1623,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_post_list_context_without_permission() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_query_params( array( @@ -1565,6 +1637,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_post_context_without_permission() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_query_params( array( @@ -1604,7 +1677,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ) ); - $post = get_post( $post_id ); + $post = get_post( $post_id ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); $request->set_param( 'password', '$inthebananastand' ); $response = rest_get_server()->dispatch( $request ); @@ -1625,7 +1699,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ) ); - $post = get_post( $post_id ); + $post = get_post( $post_id ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); $request->set_param( 'password', 'wrongpassword' ); $response = rest_get_server()->dispatch( $request ); @@ -1634,13 +1709,14 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_post_with_password_without_permission() { - $post_id = $this->factory->post->create( + $post_id = $this->factory->post->create( array( 'post_password' => '$inthebananastand', 'post_content' => 'Some secret content.', 'post_excerpt' => 'Some secret excerpt.', ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -1703,6 +1779,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_content' => '
', ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -1713,23 +1790,27 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_get_item_read_permission_custom_post_status_not_authenticated() { register_post_status( 'testpubstatus', array( 'public' => true ) ); register_post_status( 'testprivtatus', array( 'public' => false ) ); - // Public status + + // Public status. wp_update_post( array( 'ID' => self::$post_id, 'post_status' => 'testpubstatus', ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - // Private status + + // Private status. wp_update_post( array( 'ID' => self::$post_id, 'post_status' => 'testprivtatus', ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 401, $response->get_status() ); @@ -1747,6 +1828,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_prepare_item_limit_fields() { wp_set_current_user( self::$editor_id ); + $endpoint = new WP_REST_Posts_Controller( 'post' ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_param( 'context', 'edit' ); @@ -1774,8 +1856,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te add_filter( 'the_content', $filter_content ); wp_set_current_user( self::$editor_id ); + $endpoint = new WP_REST_Posts_Controller( 'post' ); - $request = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_param( 'context', 'edit' ); $request->set_param( '_fields', 'content.rendered' ); @@ -1809,8 +1892,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te add_filter( 'the_content', $filter_content ); wp_set_current_user( self::$editor_id ); + $endpoint = new WP_REST_Posts_Controller( 'post' ); - $request = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_param( 'context', 'edit' ); $request->set_param( '_fields', 'content.raw' ); @@ -1919,6 +2003,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te */ public function test_create_post_date( $status, $params, $results ) { wp_set_current_user( self::$editor_id ); + update_option( 'timezone_string', $params['timezone_string'] ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); @@ -1952,6 +2037,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te */ public function test_create_item_with_template() { wp_set_current_user( self::$editor_id ); + add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); // reregister the route as we now have a template available. @@ -2001,6 +2087,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te */ public function test_create_item_with_template_none() { wp_set_current_user( self::$editor_id ); + add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' ); @@ -2049,6 +2136,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_create_post_as_contributor() { wp_set_current_user( self::$contributor_id ); + update_option( 'timezone_string', 'America/Chicago' ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); @@ -2182,6 +2270,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_create_post_private_without_permission() { wp_set_current_user( self::$author_id ); + $user = wp_get_current_user(); $user->add_cap( 'publish_posts', false ); // Flush capabilities, https://core.trac.wordpress.org/ticket/28374 @@ -2203,6 +2292,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_create_post_publish_without_permission() { wp_set_current_user( self::$author_id ); + $user = wp_get_current_user(); $user->add_cap( 'publish_posts', false ); // Flush capabilities, https://core.trac.wordpress.org/ticket/28374 @@ -2408,7 +2498,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( '0', $data['password'] ); } @@ -2551,15 +2640,18 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ); $request->set_body_params( $params ); $response = rest_get_server()->dispatch( $request ); - $new_data = $response->get_data(); - $this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] ); + + $data = $response->get_data(); + $this->assertEquals( "Rob O'Rourke's Diary", $data['title']['raw'] ); } public function test_create_post_with_categories() { wp_set_current_user( self::$editor_id ); + $category = wp_insert_term( 'Test Category', 'category' ); - $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); - $params = $this->set_post_data( + + $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); + $params = $this->set_post_data( array( 'password' => 'testing', 'categories' => array( @@ -2576,10 +2668,12 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_create_post_with_categories_as_csv() { wp_set_current_user( self::$editor_id ); + $category = wp_insert_term( 'Chicken', 'category' ); $category2 = wp_insert_term( 'Ribs', 'category' ); - $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); - $params = $this->set_post_data( + + $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); + $params = $this->set_post_data( array( 'categories' => $category['term_id'] . ',' . $category2['term_id'], ) @@ -2593,6 +2687,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_create_post_with_invalid_categories() { wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -2617,6 +2712,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->forbidden_cat = $cats[1]; wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -2663,6 +2759,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_update_item_no_change() { wp_set_current_user( self::$editor_id ); + $post = get_post( self::$post_id ); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); @@ -2708,11 +2805,13 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_rest_update_post_with_empty_date() { // Create a new test post. $post_id = $this->factory->post->create(); + wp_set_current_user( self::$editor_id ); // Set the post date to the future. $future_date = '2919-07-29T18:00:00'; - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); + + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); $request->add_header( 'content-type', 'application/json' ); $params = $this->set_post_data( array( @@ -2796,6 +2895,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_update_post_without_permission() { wp_set_current_user( self::$editor_id ); + $user = wp_get_current_user(); $user->add_cap( 'edit_published_posts', false ); // Flush capabilities, https://core.trac.wordpress.org/ticket/28374 @@ -2950,6 +3050,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te */ public function test_update_post_date( $status, $params, $results ) { wp_set_current_user( self::$editor_id ); + update_option( 'timezone_string', $params['timezone_string'] ); $post_id = $this->factory->post->create( array( 'post_status' => $status ) ); @@ -3012,6 +3113,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te global $wpdb; wp_set_current_user( self::$editor_id ); + update_option( 'timezone_string', 'America/Chicago' ); // Need to set dates using wpdb directly because `wp_update_post` and @@ -3190,6 +3292,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_update_post_with_empty_password() { wp_set_current_user( self::$editor_id ); + wp_update_post( array( 'ID' => self::$post_id, @@ -3280,8 +3383,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_with_categories() { - wp_set_current_user( self::$editor_id ); + $category = wp_insert_term( 'Test Category', 'category' ); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); @@ -3306,6 +3409,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } $query = parse_url( $categories_path, PHP_URL_QUERY ); parse_str( $query, $args ); + $request = new WP_REST_Request( 'GET', $args['rest_route'] ); unset( $args['rest_route'] ); $request->set_query_params( $args ); @@ -3316,8 +3420,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_with_empty_categories() { - wp_set_current_user( self::$editor_id ); + $category = wp_insert_term( 'Test Category', 'category' ); wp_set_object_terms( self::$post_id, $category['term_id'], 'category' ); @@ -3342,6 +3446,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->forbidden_cat = $cats[1]; wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( @@ -3363,6 +3468,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te */ public function test_update_item_with_template() { wp_set_current_user( self::$editor_id ); + add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); // reregister the route as we now have a template available. @@ -3392,6 +3498,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te */ public function test_update_item_with_template_none() { wp_set_current_user( self::$editor_id ); + add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' ); @@ -3424,7 +3531,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te * @ticket 39996 */ public function test_update_item_with_same_template_that_no_longer_exists() { - wp_set_current_user( self::$editor_id ); update_post_meta( self::$post_id, '_wp_page_template', 'post-my-invalid-template.php' ); @@ -3597,12 +3703,14 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te */ public function test_post_roundtrip_as_author( $raw, $expected ) { wp_set_current_user( self::$author_id ); + $this->assertFalse( current_user_can( 'unfiltered_html' ) ); $this->verify_post_roundtrip( $raw, $expected ); } public function test_post_roundtrip_as_editor_unfiltered_html() { wp_set_current_user( self::$editor_id ); + if ( is_multisite() ) { $this->assertFalse( current_user_can( 'unfiltered_html' ) ); $this->verify_post_roundtrip( @@ -3654,6 +3762,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_post_roundtrip_as_superadmin_unfiltered_html() { wp_set_current_user( self::$superadmin_id ); + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); $this->verify_post_roundtrip( array( @@ -3680,6 +3789,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_item() { $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); @@ -3694,6 +3804,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_item_skip_trash() { $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); @@ -3708,7 +3819,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_item_already_trashed() { $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -3727,6 +3840,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_post_invalid_post_type() { $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $page_id ); @@ -3945,8 +4059,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ) ); - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); - + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -3957,8 +4070,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $post_id = $this->factory->post->create(); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); - + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); $response = rest_get_server()->dispatch( $request ); $this->assertArrayHasKey( 'my_custom_int', $response->data ); @@ -4043,6 +4155,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ); wp_set_current_user( self::$editor_id ); + // Check for error on update. $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( @@ -4071,7 +4184,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_publish_action_ldo_registered() { - $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); $data = $response->get_data(); $schema = $data['schema']; @@ -4083,7 +4195,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_sticky_action_ldo_registered_for_posts() { - $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); $data = $response->get_data(); $schema = $data['schema']; @@ -4095,7 +4206,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_sticky_action_ldo_not_registered_for_non_posts() { - $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' ) ); $data = $response->get_data(); $schema = $data['schema']; @@ -4107,7 +4217,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_author_action_ldo_registered_for_post_types_with_author_support() { - $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); $data = $response->get_data(); $schema = $data['schema']; @@ -4119,7 +4228,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_author_action_ldo_not_registered_for_post_types_without_author_support() { - remove_post_type_support( 'post', 'author' ); // Re-initialize the controller to cache-bust schemas from prior test runs. @@ -4139,7 +4247,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_term_action_ldos_registered() { - $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); $data = $response->get_data(); $schema = $data['schema']; @@ -4159,7 +4266,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_action_links_only_available_in_edit_context() { - wp_set_current_user( self::$author_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4175,7 +4281,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_publish_action_link_exists_for_author() { - wp_set_current_user( self::$author_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4191,7 +4296,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_publish_action_link_does_not_exist_for_contributor() { - wp_set_current_user( self::$contributor_id ); $post = self::factory()->post->create( array( 'post_author' => self::$contributor_id ) ); @@ -4207,7 +4311,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_sticky_action_exists_for_editor() { - wp_set_current_user( self::$editor_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4223,7 +4326,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_sticky_action_does_not_exist_for_author() { - wp_set_current_user( self::$author_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4239,7 +4341,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_sticky_action_does_not_exist_for_non_post_posts() { - wp_set_current_user( self::$editor_id ); $post = self::factory()->post->create( @@ -4261,7 +4362,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_assign_author_action_exists_for_editor() { - wp_set_current_user( self::$editor_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4277,7 +4377,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_assign_author_action_does_not_exist_for_author() { - wp_set_current_user( self::$author_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4293,7 +4392,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_assign_author_action_does_not_exist_for_post_types_without_author_support() { - remove_post_type_support( 'post', 'author' ); wp_set_current_user( self::$editor_id ); @@ -4311,7 +4409,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_term_action_exists_for_editor() { - wp_set_current_user( self::$editor_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4329,7 +4426,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_term_action_non_hierarchical_exists_for_author() { - wp_set_current_user( self::$author_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4345,7 +4441,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_term_action_hierarchical_does_not_exists_for_author() { - wp_set_current_user( self::$author_id ); $post = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); @@ -4361,7 +4456,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_assign_term_action_exists_for_contributor() { - wp_set_current_user( self::$contributor_id ); $post = self::factory()->post->create( @@ -4384,7 +4478,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_assign_unfiltered_html_action_superadmin() { $post_id = self::factory()->post->create(); + wp_set_current_user( self::$superadmin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); $request->set_param( 'context', 'edit' ); $response = rest_do_request( $request ); @@ -4394,7 +4490,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_assign_unfiltered_html_action_editor() { $post_id = self::factory()->post->create(); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); $request->set_param( 'context', 'edit' ); $response = rest_do_request( $request ); @@ -4409,7 +4507,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_assign_unfiltered_html_action_author() { $post_id = self::factory()->post->create(); + wp_set_current_user( self::$author_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); $request->set_param( 'context', 'edit' ); $response = rest_do_request( $request ); @@ -4490,7 +4590,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te * @ticket 39953 */ public function test_putting_same_publish_date_does_not_remove_floating_date() { - wp_set_current_user( self::$superadmin_id ); $time = date( 'Y-m-d H:i:s' ); @@ -4526,7 +4625,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te * @ticket 39953 */ public function test_putting_different_publish_date_removes_floating_date() { - wp_set_current_user( self::$superadmin_id ); $time = date( 'Y-m-d H:i:s' ); @@ -4569,7 +4667,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te * @ticket 39953 */ public function test_publishing_post_with_same_date_removes_floating_date() { - wp_set_current_user( self::$superadmin_id ); $time = date( 'Y-m-d H:i:s' ); diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php index ec17bc71e9..b842f811bc 100644 --- a/tests/phpunit/tests/rest-api/rest-tags-controller.php +++ b/tests/phpunit/tests/rest-api/rest-tags-controller.php @@ -16,6 +16,10 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { protected static $contributor; protected static $subscriber; + protected static $tag_ids = array(); + protected static $total_tags = 30; + protected static $per_page = 50; + public static function wpSetUpBeforeClass( $factory ) { self::$superadmin = $factory->user->create( array( @@ -43,9 +47,19 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { 'role' => 'subscriber', ) ); + if ( is_multisite() ) { update_site_option( 'site_admins', array( 'superadmin' ) ); } + + // Set up tags for pagination tests. + for ( $i = 0; $i < self::$total_tags; $i++ ) { + $tag_ids[] = $factory->tag->create( + array( + 'name' => "Tag {$i}", + ) + ); + } } public static function wpTearDownAfterClass() { @@ -53,6 +67,11 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { self::delete_user( self::$administrator ); self::delete_user( self::$editor ); self::delete_user( self::$subscriber ); + + // Remove tags for pagination tests. + foreach ( self::$tag_ids as $tag_id ) { + wp_delete_term( $tag_id, 'post_tag' ); + } } public function setUp() { @@ -154,13 +173,16 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items() { $this->factory->tag->create(); - $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $this->check_get_taxonomy_terms_response( $response ); } public function test_get_items_invalid_permission_for_context() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -171,7 +193,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $post_id = $this->factory->post->create(); $tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) ); $tag2 = $this->factory->tag->create( array( 'name' => 'The Be Sharps' ) ); + wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'hide_empty', true ); $response = rest_get_server()->dispatch( $request ); @@ -179,72 +203,82 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 2, count( $data ) ); $this->assertEquals( 'Season 5', $data[0]['name'] ); $this->assertEquals( 'The Be Sharps', $data[1]['name'] ); - // invalid value should fail + + // Invalid 'hide_empty' should error. $request->set_param( 'hide_empty', 'nothanks' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } public function test_get_items_include_query() { - $id1 = $this->factory->tag->create(); - $id2 = $this->factory->tag->create(); - $id3 = $this->factory->tag->create(); + $id1 = $this->factory->tag->create(); + $id2 = $this->factory->tag->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); - // Orderby=>asc - $request->set_param( 'include', array( $id3, $id1 ) ); + + // 'orderby' => 'asc'. + $request->set_param( 'include', array( $id2, $id1 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( $id1, $data[0]['id'] ); - // Orderby=>include + + // 'orderby' => 'include'. $request->set_param( 'orderby', 'include' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - $this->assertEquals( $id3, $data[0]['id'] ); - // Include invalid value shoud fail + $this->assertEquals( $id2, $data[0]['id'] ); + + // Invalid 'include' should error. $request->set_param( 'include', array( 'myterm' ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } public function test_get_items_exclude_query() { - $id1 = $this->factory->tag->create(); - $id2 = $this->factory->tag->create(); - $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); + $id1 = $this->factory->tag->create(); + $id2 = $this->factory->tag->create(); + + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); + $request->set_param( 'per_page', self::$per_page ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertTrue( in_array( $id2, $ids, true ) ); + $request->set_param( 'exclude', array( $id2 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); - // Invalid exclude value should fail + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertFalse( in_array( $id2, $ids, true ) ); + + // Invalid 'exclude' should error. $request->set_param( 'exclude', array( 'invalid' ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } public function test_get_items_offset_query() { - $id1 = $this->factory->tag->create(); - $id2 = $this->factory->tag->create(); - $id3 = $this->factory->tag->create(); - $id4 = $this->factory->tag->create(); $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'offset', 1 ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 3, $response->get_data() ); - // 'offset' works with 'per_page' + $this->assertCount( self::$total_tags - 1, $response->get_data() ); + + // 'offset' works with 'per_page'. $request->set_param( 'per_page', 2 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // 'offset' takes priority over 'page' + + // 'offset' takes priority over 'page'. $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // 'offset' invalid value shoudl fail + + // Invalid 'offset' should error. $request->set_param( 'offset', 'moreplease' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -253,7 +287,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_orderby_args() { $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); - $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); + $tag2 = $this->factory->tag->create( array( 'name' => 'Zucchini' ) ); + /* * Tests: * - orderby @@ -268,7 +303,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); - $this->assertEquals( 'Banana', $data[0]['name'] ); + $this->assertEquals( 'Zucchini', $data[0]['name'] ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'orderby', 'name' ); $request->set_param( 'order', 'asc' ); @@ -278,7 +314,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( 'Apple', $data[0]['name'] ); - // Invalid orderby should fail. + + // Invalid 'orderby' should error. $request->set_param( 'orderby', 'invalid' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -288,7 +325,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $tag0 = $this->factory->tag->create( array( 'name' => 'Cantaloupe' ) ); $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); - // defaults to orderby=name, order=asc + + // Defaults to 'orderby' => 'name', 'order' => 'asc'. $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -296,16 +334,18 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 'Apple', $data[0]['name'] ); $this->assertEquals( 'Banana', $data[1]['name'] ); $this->assertEquals( 'Cantaloupe', $data[2]['name'] ); - // orderby=id, with default order=asc + + // 'orderby' => 'id', with default 'order' => 'asc'. $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'orderby', 'id' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( 'Cantaloupe', $data[0]['name'] ); - $this->assertEquals( 'Apple', $data[1]['name'] ); - $this->assertEquals( 'Banana', $data[2]['name'] ); - // orderby=id, order=desc + $this->assertEquals( 'Tag 0', $data[0]['name'] ); + $this->assertEquals( 'Tag 1', $data[1]['name'] ); + $this->assertEquals( 'Tag 2', $data[2]['name'] ); + + // 'orderby' => 'id', 'order' => 'desc'. $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'orderby', 'id' ); $request->set_param( 'order', 'desc' ); @@ -338,6 +378,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $tag1 = $this->factory->tag->create( array( 'name' => 'DC' ) ); $tag2 = $this->factory->tag->create( array( 'name' => 'Marvel' ) ); $this->factory->tag->create( array( 'name' => 'Dark Horse' ) ); + wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' ); $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); @@ -349,7 +390,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 2, count( $data ) ); $this->assertEquals( 'DC', $data[0]['name'] ); - // Invalid post should error. + // Invalid 'post' should error. $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'post', 'invalid-post' ); $response = rest_get_server()->dispatch( $request ); @@ -358,16 +399,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_terms_post_args_paging() { $post_id = $this->factory->post->create(); - $tag_ids = array(); - for ( $i = 0; $i < 30; $i++ ) { - $tag_ids[] = $this->factory->tag->create( - array( - 'name' => "Tag {$i}", - ) - ); - } - wp_set_object_terms( $post_id, $tag_ids, 'post_tag' ); + wp_set_object_terms( $post_id, self::$tag_ids, 'post_tag' ); $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'post', $post_id ); @@ -432,6 +465,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { ) ); $post_id = $this->factory->post->create(); + wp_set_object_terms( $post_id, array( $term1, $term2 ), 'batman' ); $request = new WP_REST_Request( 'GET', '/wp/v2/batman' ); @@ -447,6 +481,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_search_args() { $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); + /* * Tests: * - search @@ -458,6 +493,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); $this->assertEquals( 'Apple', $data[0]['name'] ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'search', 'Garbage' ); $response = rest_get_server()->dispatch( $request ); @@ -467,8 +503,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_slug_arg() { - $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); - $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); + $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) ); + $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'slug', 'apple' ); $response = rest_get_server()->dispatch( $request ); @@ -483,6 +520,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) ); $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) ); $this->factory->tag->create( array( 'name' => 'Pizza' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'slug', @@ -505,6 +543,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) ); $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) ); $this->factory->tag->create( array( 'name' => 'Pizza' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'slug', 'taco,burrito, enchilada' ); $response = rest_get_server()->dispatch( $request ); @@ -536,19 +575,15 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_terms_pagination_headers() { - // Start of the index - for ( $i = 0; $i < 50; $i++ ) { - $this->factory->tag->create( - array( - 'name' => "Tag {$i}", - ) - ); - } + $total_tags = self::$total_tags; + $total_pages = (int) ceil( $total_tags / 10 ); + + // Start of the index. $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 50, $headers['X-WP-Total'] ); - $this->assertEquals( 5, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $next_link = add_query_arg( array( 'page' => 2, @@ -557,18 +592,17 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { ); $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // 3rd page - $this->factory->tag->create( - array( - 'name' => 'Tag 51', - ) - ); + + // 3rd page. + $this->factory->tag->create(); + $total_tags++; + $total_pages++; $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( 'page' => 2, @@ -583,31 +617,33 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { rest_url( 'wp/v2/tags' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // Last page + + // Last page. $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); - $request->set_param( 'page', 6 ); + $request->set_param( 'page', $total_pages ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( - 'page' => 5, + 'page' => $total_pages - 1, ), rest_url( 'wp/v2/tags' ) ); $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); - // Out of bounds + + // Out of bounds. $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); - $request->set_param( 'page', 8 ); + $request->set_param( 'page', 100 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 51, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_tags, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( - 'page' => 6, + 'page' => $total_pages, ), rest_url( 'wp/v2/tags' ) ); @@ -623,7 +659,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_item() { - $id = $this->factory->tag->create(); + $id = $this->factory->tag->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); $response = rest_get_server()->dispatch( $request ); $this->check_get_taxonomy_term_response( $response, $id ); @@ -633,7 +670,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { * @ticket 39122 */ public function test_get_item_meta() { - $id = $this->factory->tag->create(); + $id = $this->factory->tag->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -654,7 +692,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { * @ticket 39122 */ public function test_get_item_meta_registered_for_different_taxonomy() { - $id = $this->factory->tag->create(); + $id = $this->factory->tag->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -672,7 +711,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_item_invalid_permission_for_context() { $id = $this->factory->tag->create(); + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -695,12 +736,13 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_item_incorrect_taxonomy() { register_taxonomy( 'robin', 'post' ); - $term1 = $this->factory->term->create( + $term1 = $this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'robin', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term1 ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_term_invalid', $response, 404 ); @@ -708,6 +750,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); $request->set_param( 'name', 'My Awesome Term' ); $request->set_param( 'description', 'This term is so awesome.' ); @@ -724,6 +767,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item_contributor() { wp_set_current_user( self::$contributor ); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); $request->set_param( 'name', 'My Awesome Term' ); $request->set_param( 'description', 'This term is so awesome.' ); @@ -740,6 +784,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item_incorrect_permissions() { wp_set_current_user( self::$subscriber ); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); $request->set_param( 'name', 'Incorrect permissions' ); $response = rest_get_server()->dispatch( $request ); @@ -748,6 +793,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item_missing_arguments() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 ); @@ -765,6 +811,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item_with_meta() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); $request->set_param( 'name', 'My Awesome Term' ); $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) ); @@ -779,8 +826,10 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item_with_meta_wrong_id() { wp_set_current_user( self::$administrator ); + $existing_tag_id = $this->factory->tag->create( array( 'name' => 'My Not So Awesome Term' ) ); - $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); $request->set_param( 'name', 'My Awesome Term' ); $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) ); $request->set_param( 'id', $existing_tag_id ); @@ -796,13 +845,16 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_item() { wp_set_current_user( self::$administrator ); + $orig_args = array( 'name' => 'Original Name', 'description' => 'Original Description', 'slug' => 'original-slug', ); - $term = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' ); - $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); + + $term = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); $request->set_param( 'name', 'New Name' ); $request->set_param( 'description', 'New Description' ); $request->set_param( 'slug', 'new-slug' ); @@ -827,10 +879,10 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_item_no_change() { wp_set_current_user( self::$administrator ); + $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); - $request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id ); - + $request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $request->set_param( 'slug', $term->slug ); @@ -846,6 +898,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_item_invalid_term() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'name', 'Invalid Term' ); $response = rest_get_server()->dispatch( $request ); @@ -854,7 +907,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_item_incorrect_permissions() { wp_set_current_user( self::$subscriber ); - $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); + + $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); $request->set_param( 'name', 'Incorrect permissions' ); $response = rest_get_server()->dispatch( $request ); @@ -866,7 +921,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { */ public function test_update_item_with_edit_term_cap_granted() { wp_set_current_user( self::$subscriber ); - $term = $this->factory->tag->create_and_get(); + + $term = $this->factory->tag->create_and_get(); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); $request->set_param( 'name', 'New Name' ); @@ -891,7 +948,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { */ public function test_update_item_with_edit_term_cap_revoked() { wp_set_current_user( self::$administrator ); - $term = $this->factory->tag->create_and_get(); + + $term = $this->factory->tag->create_and_get(); + $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); $request->set_param( 'name', 'New Name' ); @@ -911,6 +970,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_item_parent_non_hierarchical_taxonomy() { wp_set_current_user( self::$administrator ); + $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id ); @@ -959,6 +1019,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_tag_roundtrip_as_editor() { wp_set_current_user( self::$editor ); + $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); $this->verify_tag_roundtrip( array( @@ -974,6 +1035,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_tag_roundtrip_as_editor_html() { wp_set_current_user( self::$editor ); + if ( is_multisite() ) { $this->assertFalse( current_user_can( 'unfiltered_html' ) ); $this->verify_tag_roundtrip( @@ -1003,6 +1065,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_tag_roundtrip_as_superadmin() { wp_set_current_user( self::$superadmin ); + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); $this->verify_tag_roundtrip( array( @@ -1018,6 +1081,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_tag_roundtrip_as_superadmin_html() { wp_set_current_user( self::$superadmin ); + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); $this->verify_tag_roundtrip( array( @@ -1033,7 +1097,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_delete_item() { wp_set_current_user( self::$administrator ); - $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); + + $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); $request->set_param( 'force', true ); $response = rest_get_server()->dispatch( $request ); @@ -1045,6 +1111,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_delete_item_no_trash() { wp_set_current_user( self::$administrator ); + $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); @@ -1058,6 +1125,7 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_delete_item_invalid_term() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_term_invalid', $response, 404 ); @@ -1065,7 +1133,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { public function test_delete_item_incorrect_permissions() { wp_set_current_user( self::$subscriber ); - $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); + + $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); @@ -1076,7 +1146,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { */ public function test_delete_item_with_delete_term_cap_granted() { wp_set_current_user( self::$subscriber ); - $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); + + $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); $request->set_param( 'force', true ); @@ -1102,7 +1174,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { */ public function test_delete_item_with_delete_term_cap_revoked() { wp_set_current_user( self::$administrator ); - $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); + + $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); $request->set_param( 'force', true ); @@ -1121,7 +1195,8 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { } public function test_prepare_item() { - $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); + $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term->term_id ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -1195,9 +1270,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] ); $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] ); - $tag_id = $this->factory->tag->create(); - $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id ); + $tag_id = $this->factory->tag->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id ); $response = rest_get_server()->dispatch( $request ); $this->assertArrayHasKey( 'my_custom_int', $response->data ); @@ -1224,7 +1299,9 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { ); wp_set_current_user( self::$administrator ); + $tag_id = $this->factory->tag->create(); + // Check for error on update. $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/tags/%d', $tag_id ) ); $request->set_body_params( diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index 28755a97d1..c678ce63bd 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -15,8 +15,13 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { protected static $editor; protected static $draft_editor; protected static $subscriber; - protected static $authors = array(); - protected static $posts = array(); + + protected static $authors = array(); + protected static $posts = array(); + protected static $user_ids = array(); + protected static $total_users = 30; + protected static $per_page = 50; + protected static $site; public static function wpSetUpBeforeClass( $factory ) { @@ -100,6 +105,16 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ); update_site_option( 'site_admins', array( 'superadmin' ) ); } + + // Set up users for pagination tests. + for ( $i = 0; $i < self::$total_users - 10; $i++ ) { + self::$user_ids[] = $factory->user->create( + array( + 'role' => 'contributor', + 'display_name' => "User {$i}", + ) + ); + } } public static function wpTearDownAfterClass() { @@ -110,9 +125,11 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { foreach ( self::$posts as $post ) { wp_delete_post( $post, true ); } + foreach ( self::$authors as $author ) { self::delete_user( $author ); } + _unregister_post_type( 'r_true_p_true' ); _unregister_post_type( 'r_true_p_false' ); _unregister_post_type( 'r_false_p_true' ); @@ -121,6 +138,11 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { if ( is_multisite() ) { wpmu_delete_blog( self::$site, true ); } + + // Remove users for pagination tests. + foreach ( self::$user_ids as $user_id ) { + self::delete_user( $user_id ); + } } /** @@ -212,15 +234,17 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_with_edit_context_without_permission() { - //test with a user not logged in + // Test with a user not logged in. $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 401, $response->get_status() ); - //test with a user logged in but without sufficient capabilities; capability in question: 'list_users' + // Test with a user logged in but without sufficient capabilities; + // capability in question: 'list_users'. wp_set_current_user( self::$editor ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -280,19 +304,17 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_pagination_headers() { + $total_users = self::$total_users; + $total_pages = (int) ceil( $total_users / 10 ); + wp_set_current_user( self::$user ); - for ( $i = 0; $i < 44; $i++ ) { - $this->factory->user->create( - array( - 'name' => "User {$i}", - ) - ); - } + + // Start of the index. $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 54, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_users, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $next_link = add_query_arg( array( 'page' => 2, @@ -301,18 +323,17 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ); $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // 3rd page - $this->factory->user->create( - array( - 'name' => 'User 51', - ) - ); + + // 3rd page. + $this->factory->user->create(); + $total_users++; + $total_pages++; $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 55, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_users, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( 'page' => 2, @@ -327,31 +348,33 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { rest_url( 'wp/v2/users' ) ); $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); - // Last page + + // Last page. $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); - $request->set_param( 'page', 6 ); + $request->set_param( 'page', $total_pages ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 55, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_users, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( - 'page' => 5, + 'page' => $total_pages - 1, ), rest_url( 'wp/v2/users' ) ); $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); - // Out of bounds + + // Out of bounds. $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); - $request->set_param( 'page', 8 ); + $request->set_param( 'page', 100 ); $response = rest_get_server()->dispatch( $request ); $headers = $response->get_headers(); - $this->assertEquals( 55, $headers['X-WP-Total'] ); - $this->assertEquals( 6, $headers['X-WP-TotalPages'] ); + $this->assertEquals( $total_users, $headers['X-WP-Total'] ); + $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); $prev_link = add_query_arg( array( - 'page' => 6, + 'page' => $total_pages, ), rest_url( 'wp/v2/users' ) ); @@ -361,12 +384,11 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_per_page() { wp_set_current_user( self::$user ); - for ( $i = 0; $i < 20; $i++ ) { - $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); - } + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 10, count( $response->get_data() ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'per_page', 5 ); $response = rest_get_server()->dispatch( $request ); @@ -375,9 +397,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_page() { wp_set_current_user( self::$user ); - for ( $i = 0; $i < 20; $i++ ) { - $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); - } + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'per_page', 5 ); $request->set_param( 'page', 2 ); @@ -396,9 +416,11 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_orderby_name() { wp_set_current_user( self::$user ); + $low_id = $this->factory->user->create( array( 'display_name' => 'AAAAA' ) ); $mid_id = $this->factory->user->create( array( 'display_name' => 'NNNNN' ) ); $high_id = $this->factory->user->create( array( 'display_name' => 'ZZZZ' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'orderby', 'name' ); $request->set_param( 'order', 'desc' ); @@ -406,6 +428,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( $high_id, $data[0]['id'] ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'orderby', 'name' ); $request->set_param( 'order', 'asc' ); @@ -428,7 +451,6 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request->set_param( 'include', array( $low_id, $high_id ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( $high_id, $data[0]['id'] ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); @@ -454,7 +476,6 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request->set_param( 'include', array( $low_id, $high_id ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( $high_id, $data[0]['id'] ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); @@ -542,21 +563,24 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_offset() { wp_set_current_user( self::$user ); - // 9 users created in wpSetUpBeforeClass(), plus default user. - $this->factory->user->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); + $request->set_param( 'per_page', self::$per_page ); $request->set_param( 'offset', 1 ); $response = rest_get_server()->dispatch( $request ); - $this->assertCount( 10, $response->get_data() ); - // 'offset' works with 'per_page' + $this->assertCount( self::$total_users - 1, $response->get_data() ); + + // 'offset' works with 'per_page'. $request->set_param( 'per_page', 2 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // 'offset' takes priority over 'page' + + // 'offset' takes priority over 'page'. $request->set_param( 'page', 3 ); $response = rest_get_server()->dispatch( $request ); $this->assertCount( 2, $response->get_data() ); - // 'offset' invalid value should error + + // Invalid 'offset' should error. $request->set_param( 'offset', 'moreplease' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -564,28 +588,33 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_include_query() { wp_set_current_user( self::$user ); - $id1 = $this->factory->user->create(); - $id2 = $this->factory->user->create(); - $id3 = $this->factory->user->create(); + + $id1 = $this->factory->user->create(); + $id2 = $this->factory->user->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); - // Orderby=>asc - $request->set_param( 'include', array( $id3, $id1 ) ); + + // 'orderby' => 'asc'. + $request->set_param( 'include', array( $id2, $id1 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( $id1, $data[0]['id'] ); - // Orderby=>include + + // 'orderby' => 'include'. $request->set_param( 'orderby', 'include' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - $this->assertEquals( $id3, $data[0]['id'] ); - // Invalid include should fail + $this->assertEquals( $id2, $data[0]['id'] ); + + // Invalid 'include' should error. $request->set_param( 'include', 'invalid' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); - // No privileges - $request->set_param( 'include', array( $id3, $id1 ) ); + + // No privileges. + $request->set_param( 'include', array( $id2, $id1 ) ); wp_set_current_user( 0 ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -595,20 +624,26 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_exclude_query() { wp_set_current_user( self::$user ); - $id1 = $this->factory->user->create(); - $id2 = $this->factory->user->create(); + + $id1 = $this->factory->user->create(); + $id2 = $this->factory->user->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); - $request->set_param( 'per_page', 20 ); // there are >10 users at this point + $request->set_param( 'per_page', self::$per_page ); // There are >10 users at this point. $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertTrue( in_array( $id2, $ids, true ) ); + $request->set_param( 'exclude', array( $id2 ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); - $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); - // Invalid exlude value should error. + $ids = wp_list_pluck( $data, 'id' ); + $this->assertTrue( in_array( $id1, $ids, true ) ); + $this->assertFalse( in_array( $id2, $ids, true ) ); + + // Invalid 'exclude' should error. $request->set_param( 'exclude', 'none-of-those-please' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -616,11 +651,14 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_search() { wp_set_current_user( self::$user ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'search', 'yololololo' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 0, count( $response->get_data() ) ); + $yolo_id = $this->factory->user->create( array( 'display_name' => 'yololololo' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'search', 'yololololo' ); $response = rest_get_server()->dispatch( $request ); @@ -632,6 +670,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_nicename' => 'adam', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'search', 'ada' ); $response = rest_get_server()->dispatch( $request ); @@ -642,18 +681,20 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_slug_query() { wp_set_current_user( self::$user ); + $this->factory->user->create( array( 'display_name' => 'foo', 'user_login' => 'bar', ) ); - $id2 = $this->factory->user->create( + $id2 = $this->factory->user->create( array( 'display_name' => 'Moo', 'user_login' => 'foo', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'slug', 'foo' ); $response = rest_get_server()->dispatch( $request ); @@ -664,6 +705,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_slug_array_query() { wp_set_current_user( self::$user ); + $id1 = $this->factory->user->create( array( 'display_name' => 'Taco', @@ -688,6 +730,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_login' => 'pizza', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'slug', @@ -708,6 +751,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_slug_csv_query() { wp_set_current_user( self::$user ); + $id1 = $this->factory->user->create( array( 'display_name' => 'Taco', @@ -732,6 +776,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_login' => 'pizza', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'slug', 'taco,burrito , enchilada' ); $request->set_param( 'orderby', 'slug' ); @@ -746,18 +791,20 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { // Note: Do not test using editor role as there is an editor role created in testing and it makes it hard to test this functionality. public function test_get_items_roles() { wp_set_current_user( self::$user ); - $tango = $this->factory->user->create( + + $tango = $this->factory->user->create( array( 'display_name' => 'tango', 'role' => 'subscriber', ) ); - $yolo = $this->factory->user->create( + $yolo = $this->factory->user->create( array( 'display_name' => 'yolo', 'role' => 'author', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'roles', 'author,subscriber' ); $response = rest_get_server()->dispatch( $request ); @@ -765,16 +812,21 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 3, count( $data ) ); $this->assertEquals( $tango, $data[1]['id'] ); $this->assertEquals( $yolo, $data[2]['id'] ); + $request->set_param( 'roles', 'author' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); $this->assertEquals( $yolo, $data[0]['id'] ); + wp_set_current_user( 0 ); + $request->set_param( 'roles', 'author' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 ); + wp_set_current_user( self::$editor ); + $request->set_param( 'roles', 'author' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 ); @@ -782,18 +834,21 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_invalid_roles() { wp_set_current_user( self::$user ); - $lolz = $this->factory->user->create( + + $lolz = $this->factory->user->create( array( 'display_name' => 'lolz', 'role' => 'author', ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'roles', 'ilovesteak,author' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); $this->assertEquals( $lolz, $data[0]['id'] ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'roles', 'steakisgood' ); $response = rest_get_server()->dispatch( $request ); @@ -804,12 +859,14 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_who_author_query() { wp_set_current_user( self::$superadmin ); + // First request should include subscriber in the set. $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'search', 'subscriber' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $this->assertCount( 1, $response->get_data() ); + // Second request should exclude subscriber. $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'who', 'authors' ); @@ -821,6 +878,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_items_who_invalid_query() { wp_set_current_user( self::$user ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'who', 'editor' ); $response = rest_get_server()->dispatch( $request ); @@ -833,6 +891,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { */ public function test_get_items_who_unauthorized_query() { wp_set_current_user( self::$subscriber ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'who', 'authors' ); $response = rest_get_server()->dispatch( $request ); @@ -841,16 +900,17 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_item() { $user_id = $this->factory->user->create(); + wp_set_current_user( self::$user ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); - + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); $response = rest_get_server()->dispatch( $request ); $this->check_get_user_response( $response, 'embed' ); } public function test_prepare_item() { wp_set_current_user( self::$user ); + $request = new WP_REST_Request; $request->set_param( 'context', 'edit' ); $user = get_user_by( 'id', get_current_user_id() ); @@ -860,6 +920,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_prepare_item_limit_fields() { wp_set_current_user( self::$user ); + $request = new WP_REST_Request; $request->set_param( 'context', 'edit' ); $request->set_param( '_fields', 'id,name' ); @@ -877,8 +938,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_user_avatar_urls() { wp_set_current_user( self::$user ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) ); - + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -905,6 +965,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_user_empty_capabilities() { wp_set_current_user( self::$user ); + $this->allow_user_to_manage_multisite(); $lolz = $this->factory->user->create( @@ -913,8 +974,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'roles' => '', ) ); + delete_user_option( $lolz, 'capabilities' ); delete_user_option( $lolz, 'user_level' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users/' . $lolz ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -931,6 +994,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_cannot_get_item_without_permission() { wp_set_current_user( self::$editor ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 ); @@ -944,6 +1008,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_can_get_item_author_of_rest_true_public_true_authenticated() { wp_set_current_user( self::$editor ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$authors['r_true_p_true'] ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -963,6 +1028,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_cannot_get_item_author_of_rest_false_public_true_without_permission() { wp_set_current_user( self::$editor ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$authors['r_false_p_true'] ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 ); @@ -992,12 +1058,15 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'role' => 'author', ) ); - $this->post_id = $this->factory->post->create( + + $this->post_id = $this->factory->post->create( array( 'post_author' => $this->author_id, ) ); + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) ); $response = rest_get_server()->dispatch( $request ); $this->check_get_user_response( $response, 'embed' ); @@ -1009,27 +1078,31 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'role' => 'author', ) ); + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 401, $response->get_status() ); + $this->post_id = $this->factory->post->create( array( 'post_author' => $this->author_id, 'post_type' => 'page', ) ); - $response = rest_get_server()->dispatch( $request ); + + $response = rest_get_server()->dispatch( $request ); $this->check_get_user_response( $response, 'embed' ); } public function test_get_user_with_edit_context() { $user_id = $this->factory->user->create(); + $this->allow_user_to_manage_multisite(); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'context', 'edit' ); - $response = rest_get_server()->dispatch( $request ); $this->check_get_user_response( $response, 'edit' ); } @@ -1040,12 +1113,15 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'role' => 'author', ) ); - $this->post_id = $this->factory->post->create( + + $this->post_id = $this->factory->post->create( array( 'post_author' => $this->author_id, ) ); + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); @@ -1055,8 +1131,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_current_user() { wp_set_current_user( self::$user ); - $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' ); - + $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $this->check_get_user_response( $response, 'view' ); @@ -1070,14 +1145,15 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_current_user_without_permission() { wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_not_logged_in', $response, 401 ); } public function test_create_item() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( @@ -1095,9 +1171,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( $params ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); + + $data = $response->get_data(); $this->assertEquals( 'http://example.com', $data['url'] ); $this->assertEquals( array( 'editor' ), $data['roles'] ); $this->check_add_edit_user_response( $response ); @@ -1105,6 +1181,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item_invalid_username() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( @@ -1127,11 +1204,11 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( $params ); - $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); $data = $response->get_data(); + if ( is_multisite() ) { $this->assertInternalType( 'array', $data['additional_errors'] ); $this->assertCount( 1, $data['additional_errors'] ); @@ -1152,6 +1229,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item_illegal_username() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); add_filter( 'illegal_user_logins', array( $this, 'get_illegal_user_logins' ) ); @@ -1171,7 +1249,6 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( $params ); - $response = rest_get_server()->dispatch( $request ); remove_filter( 'illegal_user_logins', array( $this, 'get_illegal_user_logins' ) ); @@ -1319,6 +1396,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_json_create_user() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( @@ -1330,8 +1408,8 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); - $response = rest_get_server()->dispatch( $request ); + $this->check_add_edit_user_response( $response ); } @@ -1354,6 +1432,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_user_invalid_id() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( @@ -1373,6 +1452,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_user_invalid_email() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( @@ -1391,6 +1471,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_user_invalid_role() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( @@ -1419,7 +1500,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'locale' => 'en_US', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $userdata = get_userdata( $user_id ); @@ -1434,8 +1517,8 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( $_POST ); - $response = rest_get_server()->dispatch( $request ); + $this->check_add_edit_user_response( $response, true ); // Check that the name has been updated correctly @@ -1455,7 +1538,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_item_no_change() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); + $user = get_userdata( self::$editor ); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) ); @@ -1483,7 +1568,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_email' => 'testjson2@example.com', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 ); @@ -1539,7 +1626,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_email' => 'testjson@example.com', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user1 ); @@ -1556,7 +1645,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_email' => 'testjson@example.com', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user_id ); @@ -1579,7 +1670,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'locale' => 'de_DE', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user_id ); @@ -1606,7 +1699,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_email' => 'testjson2@example.com', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 ); @@ -1629,7 +1724,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_email' => 'testjson2@example.com', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 ); @@ -1649,7 +1746,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'last_name' => 'Original Last', ) ); + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( @@ -1686,6 +1785,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( self::$user ); + $this->allow_user_to_manage_multisite(); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -1706,6 +1806,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( self::$user ); + $this->allow_user_to_manage_multisite(); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -1809,6 +1910,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_user_role_invalid_role() { wp_set_current_user( self::$user ); + $this->allow_user_to_manage_multisite(); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) ); @@ -1858,10 +1960,11 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_user_invalid_id() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $params = array( - 'id' => '156', + 'id' => '0', 'username' => 'lisasimpson', 'password' => 'DavidHasselhoff', 'email' => 'smartgirl63_@yahoo.com', @@ -1886,6 +1989,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ); wp_set_current_user( self::$editor ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'roles', array( 'editor' ) ); $response = rest_get_server()->dispatch( $request ); @@ -1903,6 +2007,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ); wp_set_current_user( self::$user ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'roles', array( 'editor' ) ); $response = rest_get_server()->dispatch( $request ); @@ -1923,6 +2028,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ); wp_set_current_user( self::$user ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'roles', array( 'editor' ) ); $request->set_param( 'name', 'Short-Lived User' ); @@ -1945,10 +2051,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_item_invalid_password() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) ); - $request->set_param( 'password', 'no\\backslashes\\allowed' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -2034,6 +2140,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_user_roundtrip_as_editor() { wp_set_current_user( self::$editor ); + $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); $this->verify_user_roundtrip( array( @@ -2060,6 +2167,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_user_roundtrip_as_editor_html() { wp_set_current_user( self::$editor ); + if ( is_multisite() ) { $this->assertFalse( current_user_can( 'unfiltered_html' ) ); $this->verify_user_roundtrip( @@ -2111,6 +2219,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_user_roundtrip_as_superadmin() { wp_set_current_user( self::$superadmin ); + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here'; $this->verify_user_roundtrip( @@ -2139,6 +2248,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_user_roundtrip_as_superadmin_html() { wp_set_current_user( self::$superadmin ); + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here'; $this->verify_user_roundtrip( @@ -2169,9 +2279,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) ); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); - $userdata = get_userdata( $user_id ); // cache for later + $userdata = get_userdata( $user_id ); // Cache for later. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'force', true ); $request->set_param( 'reassign', false ); @@ -2193,9 +2304,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) ); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); - $userdata = get_userdata( $user_id ); // cache for later + $userdata = get_userdata( $user_id ); // Cache for later. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'reassign', false ); @@ -2213,7 +2325,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); - // Ensure the user still exists + // Ensure the user still exists. $user = get_user_by( 'id', $user_id ); $this->assertNotEmpty( $user ); } @@ -2284,6 +2396,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$editor ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -2303,6 +2416,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_delete_user_invalid_id() { $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); @@ -2316,7 +2430,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_delete_user_reassign() { $this->allow_user_to_manage_multisite(); - // Test with a new user, to avoid any complications + // Test with a new user, to avoid any complications. $user_id = $this->factory->user->create(); $reassign_id = $this->factory->user->create(); $test_post = $this->factory->post->create( @@ -2325,12 +2439,13 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ) ); - // Sanity check to ensure the factory created the post correctly + // Sanity check to ensure the factory created the post correctly. $post = get_post( $test_post ); $this->assertEquals( $user_id, $post->post_author ); - // Delete our test user, and reassign to the new author wp_set_current_user( self::$user ); + + // Delete our test user, and reassign to the new author. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request['force'] = true; $request->set_param( 'reassign', $reassign_id ); @@ -2353,6 +2468,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -2373,6 +2489,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -2387,6 +2504,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $test_post = $this->factory->post->create( @@ -2414,6 +2532,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $test_post = $this->factory->post->create( @@ -2441,6 +2560,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $test_post = $this->factory->post->create( @@ -2468,6 +2588,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); $test_post = $this->factory->post->create( @@ -2556,8 +2677,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ) ); - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); - + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -2565,13 +2685,13 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] ); wp_set_current_user( 1 ); + if ( is_multisite() ) { $current_user = wp_get_current_user( 1 ); update_site_option( 'site_admins', array( $current_user->user_login ) ); } - $request = new WP_REST_Request( 'GET', '/wp/v2/users/1' ); - + $request = new WP_REST_Request( 'GET', '/wp/v2/users/1' ); $response = rest_get_server()->dispatch( $request ); $this->assertArrayHasKey( 'my_custom_int', $response->data ); @@ -2581,7 +2701,6 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'my_custom_int' => 123, ) ); - $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 123, get_user_meta( 1, 'my_custom_int', true ) ); @@ -2594,9 +2713,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'password' => 'hello', ) ); - $response = rest_get_server()->dispatch( $request ); - $this->assertEquals( 123, $response->data['my_custom_int'] ); global $wp_rest_additional_fields; @@ -2622,6 +2739,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { ); wp_set_current_user( 1 ); + if ( is_multisite() ) { $current_user = wp_get_current_user( 1 ); update_site_option( 'site_admins', array( $current_user->user_login ) ); @@ -2634,7 +2752,6 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'my_custom_int' => 'returnError', ) ); - $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); @@ -2657,8 +2774,8 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { restore_current_blog(); wp_set_current_user( self::$user ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); } @@ -2677,8 +2794,8 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { restore_current_blog(); wp_set_current_user( self::$superadmin ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); } @@ -2697,10 +2814,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { restore_current_blog(); wp_set_current_user( self::$user ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( array( 'first_name' => 'New Name' ) ); - $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); } @@ -2719,10 +2836,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { restore_current_blog(); wp_set_current_user( self::$superadmin ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( array( 'first_name' => 'New Name' ) ); - $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); } @@ -2741,10 +2858,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { restore_current_blog(); wp_set_current_user( self::$user ); + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'force', true ); $request->set_param( 'reassign', false ); - $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); } @@ -2763,10 +2880,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { restore_current_blog(); wp_set_current_user( self::$superadmin ); + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request->set_param( 'force', true ); $request->set_param( 'reassign', false ); - $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); }