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' => '