diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index f270d5acb9..0f43bf3145 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -10,29 +10,42 @@ * @group restapi */ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Controller_Testcase { + protected static $editor_id; + protected static $author_id; + protected static $contributor_id; + protected static $uploader_id; + + public static function wpSetUpBeforeClass( $factory ) { + self::$editor_id = $factory->user->create( array( + 'role' => 'editor', + ) ); + self::$author_id = $factory->user->create( array( + 'role' => 'author', + ) ); + self::$contributor_id = $factory->user->create( array( + 'role' => 'contributor', + ) ); + self::$uploader_id = $factory->user->create( array( + 'role' => 'uploader', + ) ); + } + + public static function wpTearDownAfterClass() { + self::delete_user( self::$editor_id ); + self::delete_user( self::$author_id ); + self::delete_user( self::$contributor_id ); + self::delete_user( self::$uploader_id ); + } public function setUp() { parent::setUp(); - $this->editor_id = $this->factory->user->create( array( - 'role' => 'editor', - ) ); - $this->author_id = $this->factory->user->create( array( - 'role' => 'author', - ) ); - $this->contributor_id = $this->factory->user->create( array( - 'role' => 'contributor', - ) ); - // Add an uploader role to test upload capabilities. add_role( 'uploader', 'File upload role' ); $role = get_role( 'uploader' ); $role->add_cap( 'upload_files' ); $role->add_cap( 'read' ); $role->add_cap( 'level_0' ); - $this->uploader_id = $this->factory->user->create( array( - 'role' => 'uploader', - ) ); $orig_file = DIR_TESTDATA . '/images/canola.jpg'; $this->test_file = '/tmp/canola.jpg'; @@ -179,7 +192,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_get_items_logged_in_editor() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', @@ -280,7 +293,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_get_items_invalid_status_param_is_error_response() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', @@ -307,7 +320,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); // Properly authorized users can make the request - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); @@ -415,7 +428,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_header( 'Content-Type', 'image/jpeg' ); $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); @@ -427,7 +440,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_default_filename_title() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_file_params( array( 'file' => array( @@ -445,7 +458,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_with_files() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_file_params( array( 'file' => array( @@ -461,7 +474,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_with_upload_files_role() { - wp_set_current_user( $this->uploader_id ); + wp_set_current_user( self::$uploader_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_file_params( array( 'file' => array( @@ -477,14 +490,14 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_empty_body() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_upload_no_data', $response, 400 ); } public function test_create_item_missing_content_type() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_body( file_get_contents( $this->test_file ) ); $response = $this->server->dispatch( $request ); @@ -492,7 +505,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_missing_content_disposition() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_header( 'Content-Type', 'image/jpeg' ); $request->set_body( file_get_contents( $this->test_file ) ); @@ -501,7 +514,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_bad_md5_header() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_header( 'Content-Type', 'image/jpeg' ); $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); @@ -512,7 +525,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_with_files_bad_md5_header() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_file_params( array( 'file' => array( @@ -528,15 +541,15 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_invalid_upload_files_capability() { - wp_set_current_user( $this->contributor_id ); + wp_set_current_user( self::$contributor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_create', $response, 403 ); } public function test_create_item_invalid_edit_permissions() { - $post_id = $this->factory->post->create( array( 'post_author' => $this->editor_id ) ); - wp_set_current_user( $this->author_id ); + $post_id = $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_param( 'post', $post_id ); $response = $this->server->dispatch( $request ); @@ -544,8 +557,8 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_invalid_upload_permissions() { - $post_id = $this->factory->post->create( array( 'post_author' => $this->editor_id ) ); - wp_set_current_user( $this->uploader_id ); + $post_id = $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); + wp_set_current_user( self::$uploader_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_param( 'post', $post_id ); $response = $this->server->dispatch( $request ); @@ -554,7 +567,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control public function test_create_item_invalid_post_type() { $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_header( 'Content-Type', 'image/jpeg' ); $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); @@ -565,7 +578,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_alt_text() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_header( 'Content-Type', 'image/jpeg' ); $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); @@ -578,7 +591,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_create_item_unsafe_alt_text() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); $request->set_header( 'Content-Type', 'image/jpeg' ); $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); @@ -590,11 +603,11 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_update_item() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', - 'post_author' => $this->editor_id, + 'post_author' => self::$editor_id, ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); $request->set_param( 'title', 'My title is very cool' ); @@ -615,7 +628,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_update_item_parent() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $original_parent = $this->factory->post->create( array() ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, $original_parent, array( 'post_mime_type' => 'image/jpeg', @@ -636,11 +649,11 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_update_item_invalid_permissions() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', - 'post_author' => $this->editor_id, + 'post_author' => self::$editor_id, ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); $request->set_param( 'caption', 'This is a better caption.' ); @@ -650,11 +663,11 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control public function test_update_item_invalid_post_type() { $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', - 'post_author' => $this->editor_id, + 'post_author' => self::$editor_id, ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); $request->set_param( 'post', $attachment_id ); @@ -663,7 +676,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_delete_item() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', @@ -675,7 +688,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_delete_item_no_trash() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', @@ -692,11 +705,11 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } public function test_delete_item_invalid_delete_permissions() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', - 'post_author' => $this->editor_id, + 'post_author' => self::$editor_id, ) ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); $response = $this->server->dispatch( $request ); @@ -707,7 +720,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', - 'post_author' => $this->editor_id, + 'post_author' => self::$editor_id, ) ); $attachment = get_post( $attachment_id ); @@ -798,11 +811,11 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 'update_callback' => array( $this, 'additional_field_update_callback' ), ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', - 'post_author' => $this->editor_id, + 'post_author' => self::$editor_id, ) ); // Check for error on update. $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/media/%d', $attachment_id ) ); diff --git a/tests/phpunit/tests/rest-api/rest-categories-controller.php b/tests/phpunit/tests/rest-api/rest-categories-controller.php index ac2d0f1aa3..440cb45967 100644 --- a/tests/phpunit/tests/rest-api/rest-categories-controller.php +++ b/tests/phpunit/tests/rest-api/rest-categories-controller.php @@ -11,17 +11,23 @@ * @group restapi */ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcase { + protected static $administrator; + protected static $subscriber; - public function setUp() { - parent::setUp(); - $this->administrator = $this->factory->user->create( array( + public static function wpSetUpBeforeClass( $factory ) { + self::$administrator = $factory->user->create( array( 'role' => 'administrator', ) ); - $this->subscriber = $this->factory->user->create( array( + self::$subscriber = $factory->user->create( array( 'role' => 'subscriber', ) ); } + public static function wpTearDownAfterClass() { + self::delete_user( self::$administrator ); + self::delete_user( self::$subscriber ); + } + public function test_register_routes() { $routes = $this->server->get_routes(); $this->assertArrayHasKey( '/wp/v2/categories', $routes ); @@ -572,7 +578,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_create_item() { - wp_set_current_user( $this->administrator ); + 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.' ); @@ -588,7 +594,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_create_item_invalid_taxonomy() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy' ); $request->set_param( 'name', 'Invalid Taxonomy' ); $response = $this->server->dispatch( $request ); @@ -596,7 +602,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_create_item_incorrect_permissions() { - wp_set_current_user( $this->subscriber ); + wp_set_current_user( self::$subscriber ); $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request->set_param( 'name', 'Incorrect permissions' ); $response = $this->server->dispatch( $request ); @@ -604,14 +610,14 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_create_item_missing_arguments() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 ); } public function test_create_item_with_parent() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $parent = wp_insert_term( 'test-category', 'category' ); $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request->set_param( 'name', 'My Awesome Term' ); @@ -623,7 +629,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_create_item_invalid_parent() { - wp_set_current_user( $this->administrator ); + 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 ); @@ -634,7 +640,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_update_item() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $orig_args = array( 'name' => 'Original Name', 'description' => 'Original Description', @@ -654,7 +660,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_update_item_invalid_taxonomy() { - wp_set_current_user( $this->administrator ); + 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 = $this->server->dispatch( $request ); @@ -662,7 +668,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_update_item_invalid_term() { - wp_set_current_user( $this->administrator ); + 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 = $this->server->dispatch( $request ); @@ -670,7 +676,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_update_item_incorrect_permissions() { - wp_set_current_user( $this->subscriber ); + wp_set_current_user( self::$subscriber ); $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' ); @@ -679,7 +685,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_update_item_parent() { - wp_set_current_user( $this->administrator ); + 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' ); @@ -693,7 +699,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_update_item_invalid_parent() { - wp_set_current_user( $this->administrator ); + 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 ); @@ -703,7 +709,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_delete_item() { - wp_set_current_user( $this->administrator ); + 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 ); $request->set_param( 'force', true ); @@ -714,7 +720,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_delete_item_force_false() { - wp_set_current_user( $this->administrator ); + 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 ); // force defaults to false @@ -723,21 +729,21 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas } public function test_delete_item_invalid_taxonomy() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_no_route', $response, 404 ); } public function test_delete_item_invalid_term() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_term_invalid', $response, 404 ); } public function test_delete_item_incorrect_permissions() { - wp_set_current_user( $this->subscriber ); + 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 = $this->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 df7f3fa583..8dc8c4c7de 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -11,59 +11,73 @@ */ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase { - protected $admin_id; - protected $subscriber_id; + protected static $admin_id; + protected static $subscriber_id; + protected static $author_id; - protected $post_id; - protected $private_id; - protected $draft_id; - protected $trash_id; - - protected $approved_id; - protected $hold_id; + protected static $post_id; + protected static $private_id; + protected static $draft_id; + protected static $trash_id; + protected static $approved_id; + protected static $hold_id; protected $endpoint; - public function setUp() { - parent::setUp(); - - $this->admin_id = $this->factory->user->create( array( + public static function wpSetUpBeforeClass( $factory ) { + self::$admin_id = $factory->user->create( array( 'role' => 'administrator', - )); - $this->subscriber_id = $this->factory->user->create( array( + ) ); + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber', - )); - $this->author_id = $this->factory->user->create( array( + ) ); + self::$author_id = $factory->user->create( array( 'role' => 'author', 'display_name' => 'Sea Captain', 'first_name' => 'Horatio', 'last_name' => 'McCallister', 'user_email' => 'captain@thefryingdutchman.com', 'user_url' => 'http://thefryingdutchman.com', - )); + ) ); - $this->post_id = $this->factory->post->create(); - $this->private_id = $this->factory->post->create( array( + self::$post_id = $factory->post->create(); + self::$private_id = $factory->post->create( array( 'post_status' => 'private', - )); - $this->draft_id = $this->factory->post->create( array( + ) ); + self::$draft_id = $factory->post->create( array( 'post_status' => 'draft', - )); - $this->trash_id = $this->factory->post->create( array( + ) ); + self::$trash_id = $factory->post->create( array( 'post_status' => 'trash', - )); + ) ); - $this->approved_id = $this->factory->comment->create( array( + self::$approved_id = $factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, 'user_id' => 0, - )); - $this->hold_id = $this->factory->comment->create( array( + ) ); + self::$hold_id = $factory->comment->create( array( 'comment_approved' => 0, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, - )); + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, + ) ); + } + public static function wpTearDownAfterClass() { + self::delete_user( self::$admin_id ); + self::delete_user( self::$subscriber_id ); + self::delete_user( self::$author_id ); + + wp_delete_post( self::$post_id, true ); + wp_delete_post( self::$private_id, true ); + wp_delete_post( self::$draft_id, true ); + wp_delete_post( self::$trash_id, true ); + wp_delete_post( self::$approved_id, true ); + wp_delete_post( self::$hold_id, true ); + } + + public function setUp() { + parent::setUp(); $this->endpoint = new WP_REST_Comments_Controller; } @@ -88,7 +102,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); // Single - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments/' . $this->approved_id ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments/' . self::$approved_id ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); @@ -126,7 +140,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items() { - $this->factory->comment->create_post_comments( $this->post_id, 6 ); + $this->factory->comment->create_post_comments( self::$post_id, 6 ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -134,7 +148,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); - // We created 6 comments in this method, plus $this->approved_id. + // We created 6 comments in this method, plus self::$approved_id. $this->assertCount( 7, $comments ); } @@ -143,7 +157,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->private_id, + 'comment_post_ID' => self::$private_id, ); $private_comment = $this->factory->comment->create( $args ); @@ -157,11 +171,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( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->private_id, + 'comment_post_ID' => self::$private_id, ); $private_comment = $this->factory->comment->create( $args ); @@ -194,7 +208,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_with_invalid_post_permission() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, @@ -222,7 +236,7 @@ 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( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'post', 0 ); $response = $this->server->dispatch( $request ); @@ -240,7 +254,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_edit_context() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); @@ -264,10 +278,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_include_query() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ); $id1 = $this->factory->comment->create( $args ); $this->factory->comment->create( $args ); @@ -289,10 +303,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_exclude_query() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ); $id1 = $this->factory->comment->create( $args ); $id2 = $this->factory->comment->create( $args ); @@ -309,10 +323,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_offset_query() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ); $this->factory->comment->create( $args ); $this->factory->comment->create( $args ); @@ -332,10 +346,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_order_query() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ); $this->factory->comment->create( $args ); $this->factory->comment->create( $args ); @@ -349,7 +363,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $request->set_param( 'order', 'asc' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( $this->approved_id, $data[0]['id'] ); + $this->assertEquals( self::$approved_id, $data[0]['id'] ); } public function test_get_items_private_post_no_permissions() { @@ -363,27 +377,27 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_author_arg() { // Authorized - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->author_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$author_id, ); $this->factory->comment->create( $args ); - $args['user_id'] = $this->subscriber_id; + $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 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - $request->set_param( 'author', $this->author_id ); + $request->set_param( 'author', self::$author_id ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); $this->assertCount( 1, $comments ); // Multiple authors are supported - $request->set_param( 'author', array( $this->author_id, $this->subscriber_id ) ); + $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); @@ -396,14 +410,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_author_exclude_arg() { // Authorized - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->author_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$author_id, ); $this->factory->comment->create( $args ); - $args['user_id'] = $this->subscriber_id; + $args['user_id'] = self::$subscriber_id; $this->factory->comment->create( $args ); unset( $args['user_id'] ); $this->factory->comment->create( $args ); @@ -415,14 +429,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase // 'author_exclude' limits result to 3 of 4 $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - $request->set_param( 'author_exclude', $this->author_id ); + $request->set_param( 'author_exclude', self::$author_id ); $response = $this->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) $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); - $request->set_param( 'author_exclude', array( $this->author_id, $this->subscriber_id ) ); + $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $comments = $response->get_data(); @@ -436,7 +450,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_parent_arg() { $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ); $parent_id = $this->factory->comment->create( $args ); $parent_id2 = $this->factory->comment->create( $args ); @@ -461,7 +475,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_parent_exclude_arg() { $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ); $parent_id = $this->factory->comment->create( $args ); $parent_id2 = $this->factory->comment->create( $args ); @@ -484,10 +498,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items_search_query() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $args = array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, 'comment_content' => 'foo', 'comment_author' => 'Homer J Simpson', ); @@ -509,12 +523,12 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_comments_pagination_headers() { - wp_set_current_user( $this->admin_id ); + 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' => $this->post_id, + 'comment_post_ID' => self::$post_id, ) ); } $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -530,7 +544,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase // 3rd page $this->factory->comment->create( array( 'comment_content' => 'Comment 51', - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'page', 3 ); @@ -583,15 +597,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comments_valid_date() { $comment1 = $this->factory->comment->create( array( 'comment_date' => '2016-01-15T00:00:00Z', - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ) ); $comment2 = $this->factory->comment->create( array( 'comment_date' => '2016-01-16T00:00:00Z', - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ) ); $comment3 = $this->factory->comment->create( array( 'comment_date' => '2016-01-17T00:00:00Z', - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); @@ -604,7 +618,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_item() { - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -614,8 +628,8 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_prepare_item() { - wp_set_current_user( $this->admin_id ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) ); + 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( 'context' => 'edit', ) ); @@ -628,7 +642,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_comment_author_avatar_urls() { - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $response = $this->server->dispatch( $request ); @@ -637,7 +651,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertArrayHasKey( 48, $data['author_avatar_urls'] ); $this->assertArrayHasKey( 96, $data['author_avatar_urls'] ); - $comment = get_comment( $this->approved_id ); + $comment = get_comment( self::$approved_id ); /** * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar * server when building the url string. @@ -654,7 +668,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', $this->approved_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 ); @@ -673,7 +687,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_comment_invalid_post_id_as_admin() { - wp_set_current_user( $this->admin_id ); + 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, @@ -687,16 +701,16 @@ 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', $this->hold_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); } public function test_get_comment_not_approved_same_user() { - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->hold_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -705,15 +719,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_with_children_link() { $comment_id_1 = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, ) ); $child_comment = $this->factory->comment->create( array( 'comment_approved' => 1, 'comment_parent' => $comment_id_1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, ) ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) ); @@ -725,8 +739,8 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_get_comment_without_children_link() { $comment_id_1 = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, ) ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) ); @@ -739,7 +753,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 0 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', @@ -758,14 +772,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->check_comment_data( $data, 'view', $response->get_links() ); $this->assertEquals( 'hold', $data['status'] ); $this->assertEquals( '2014-11-07T10:14:25', $data['date'] ); - $this->assertEquals( $this->post_id, $data['post'] ); + $this->assertEquals( self::$post_id, $data['post'] ); } public function test_create_item_using_accepted_content_raw_value() { wp_set_current_user( 0 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Reverend Lovejoy', 'author_email' => 'lovejoy@example.com', 'author_url' => 'http://timothylovejoy.jr', @@ -790,7 +804,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase update_option( 'require_name_email', 1 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.', ); @@ -808,7 +822,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase update_option( 'require_name_email', 1 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_email' => 'ekrabappel@springfield-elementary.edu', 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.', ); @@ -827,7 +841,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase update_option( 'require_name_email', 1 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Edna Krabappel', 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.', ); @@ -846,7 +860,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 0 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Reverend Lovejoy', 'author_email' => 'lovejoy@example.com', 'author_url' => 'http://timothylovejoy.jr', @@ -865,7 +879,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 0 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Reverend Lovejoy', 'author_email' => 'lovejoy@example.com', 'author_url' => 'http://timothylovejoy.jr', @@ -888,9 +902,9 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'user_email' => 'cbg@androidsdungeon.com', )); - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', @@ -911,11 +925,11 @@ 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( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'post' => $post_id, - 'author' => $this->admin_id, + 'author' => self::$admin_id, 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', @@ -956,7 +970,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( $user_id ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!", ); @@ -978,15 +992,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_comment_other_user() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, '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' => $this->subscriber_id, + 'author' => self::$subscriber_id, ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); @@ -996,22 +1010,22 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( 201, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( $this->subscriber_id, $data['author'] ); + $this->assertEquals( self::$subscriber_id, $data['author'] ); $this->assertEquals( 'Homer Jay Simpson', $data['author_name'] ); $this->assertEquals( 'chunkylover53@aol.com', $data['author_email'] ); $this->assertEquals( 'http://compuglobalhypermeganet.com', $data['author_url'] ); } public function test_create_comment_other_user_without_permission() { - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, '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' => $this->admin_id, + 'author' => self::$admin_id, ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); @@ -1023,15 +1037,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_comment_karma_without_permission() { - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, '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' => $this->subscriber_id, + 'author' => self::$subscriber_id, 'karma' => 100, ); @@ -1044,15 +1058,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_comment_status_without_permission() { - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, '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' => $this->subscriber_id, + 'author' => self::$subscriber_id, 'status' => 'approved', ); @@ -1066,7 +1080,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( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'post' => $post_id, @@ -1093,7 +1107,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_comment_invalid_author_IP() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'author_name' => 'Comic Book Guy', @@ -1113,7 +1127,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_comment_no_post_id() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'author_name' => 'Comic Book Guy', @@ -1131,14 +1145,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( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $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' => $this->subscriber_id, + 'author' => self::$subscriber_id, ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -1150,15 +1164,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_comment_draft_post() { - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $params = array( - 'post' => $this->draft_id, + 'post' => self::$draft_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville', 'content' => 'Call me Ishmael.', - 'author' => $this->subscriber_id, + 'author' => self::$subscriber_id, ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -1170,15 +1184,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_comment_trash_post() { - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $params = array( - 'post' => $this->trash_id, + 'post' => self::$trash_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville', 'content' => 'Call me Ishmael.', - 'author' => $this->subscriber_id, + 'author' => self::$subscriber_id, ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -1190,15 +1204,15 @@ 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( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $params = array( - 'post' => $this->private_id, + 'post' => self::$private_id, 'author_name' => 'Homer Jay Simpson', 'author_email' => 'chunkylover53@aol.com', 'author_url' => 'http://compuglobalhypermeganet.com', 'content' => 'I\’d be a vegetarian if bacon grew on trees.', - 'author' => $this->subscriber_id, + 'author' => self::$subscriber_id, ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'content-type', 'application/json' ); @@ -1212,7 +1226,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_create_item_duplicate() { $this->factory->comment->create( array( - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, 'comment_author' => 'Guy N. Cognito', 'comment_author_email' => 'chunkylover53@aol.co.uk', 'comment_content' => 'Homer? Who is Homer? My name is Guy N. Cognito.', @@ -1221,7 +1235,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 0 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Guy N. Cognito', 'author_email' => 'chunkylover53@aol.co.uk', 'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.', @@ -1257,7 +1271,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 0 ); update_option( 'comment_registration', 1 ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); - $request->set_param( 'post', $this->post_id ); + $request->set_param( 'post', self::$post_id ); $response = $this->server->dispatch( $request ); $this->assertEquals( 401, $response->get_status() ); $data = $response->get_data(); @@ -1265,10 +1279,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_item_invalid_author() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, 'content' => 'It\'s all over\, people! We don\'t have a prayer!', ); @@ -1282,12 +1296,12 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_item_pull_author_info() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); - $author = new WP_User( $this->author_id ); + $author = new WP_User( self::$author_id ); $params = array( - 'post' => $this->post_id, - 'author' => $this->author_id, + 'post' => self::$post_id, + 'author' => self::$author_id, 'content' => 'It\'s all over\, people! We don\'t have a prayer!', ); @@ -1298,7 +1312,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $response = $this->server->dispatch( $request ); $result = $response->get_data(); - $this->assertSame( $this->author_id, $result['author'] ); + $this->assertSame( self::$author_id, $result['author'] ); $this->assertSame( 'Sea Captain', $result['author_name'] ); $this->assertSame( 'captain@thefryingdutchman.com', $result['author_email'] ); $this->assertSame( 'http://thefryingdutchman.com', $result['author_url'] ); @@ -1308,7 +1322,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 0 ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', @@ -1323,7 +1337,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( 201, $response->get_status() ); $params = array( - 'post' => $this->post_id, + 'post' => self::$post_id, 'author_name' => 'Comic Book Guy', 'author_email' => 'cbg@androidsdungeon.com', 'author_url' => 'http://androidsdungeon.com', @@ -1341,10 +1355,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_item() { $post_id = $this->factory->post->create(); - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( - 'author' => $this->subscriber_id, + 'author' => self::$subscriber_id, 'author_name' => 'Disco Stu', 'author_url' => 'http://stusdisco.com', 'author_email' => 'stu@stusdisco.com', @@ -1354,7 +1368,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'karma' => 100, 'post' => $post_id, ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_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 ) ); @@ -1362,7 +1376,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( 200, $response->get_status() ); $comment = $response->get_data(); - $updated = get_comment( $this->approved_id ); + $updated = get_comment( self::$approved_id ); $this->assertEquals( $params['content'], $comment['content']['raw'] ); $this->assertEquals( $params['author'], $comment['author'] ); $this->assertEquals( $params['author_name'], $comment['author_name'] ); @@ -1377,11 +1391,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_comment_status() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id = $this->factory->comment->create( array( 'comment_approved' => 0, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, )); $params = array( @@ -1401,11 +1415,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_comment_field_does_not_use_default_values() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id = $this->factory->comment->create( array( 'comment_approved' => 0, - 'comment_post_ID' => $this->post_id, + 'comment_post_ID' => self::$post_id, 'comment_content' => 'some content', )); @@ -1427,13 +1441,13 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_comment_date_gmt() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $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', $this->approved_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 ) ); @@ -1441,18 +1455,18 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( 200, $response->get_status() ); $comment = $response->get_data(); - $updated = get_comment( $this->approved_id ); + $updated = get_comment( self::$approved_id ); $this->assertEquals( $params['date_gmt'], $comment['date_gmt'] ); $this->assertEquals( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) ); } public function test_update_comment_invalid_type() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'type' => 'trackback', ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_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 ) ); @@ -1461,14 +1475,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_comment_with_raw_property() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'content' => array( 'raw' => 'What the heck kind of name is Persephone?', ), ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_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 ) ); @@ -1477,19 +1491,19 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( 200, $response->get_status() ); $comment = $response->get_data(); - $updated = get_comment( $this->approved_id ); + $updated = get_comment( self::$approved_id ); $this->assertEquals( $params['content']['raw'], $updated->comment_content ); } public function test_update_item_invalid_date() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'content' => rand_str(), 'date' => rand_str(), ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_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 ) ); @@ -1498,14 +1512,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_item_invalid_date_gmt() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $params = array( 'content' => rand_str(), 'date_gmt' => rand_str(), ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_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 ) ); @@ -1533,7 +1547,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $params = array( 'content' => 'Disco Stu likes disco music.', ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->hold_id ) ); + $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 ) ); @@ -1544,11 +1558,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase public function test_update_comment_private_post_invalid_permission() { $private_comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->private_id, + 'comment_post_ID' => self::$private_id, 'user_id' => 0, )); - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); $params = array( 'content' => 'Disco Stu likes disco music.', @@ -1562,17 +1576,17 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_comment_with_children_link() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id_1 = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, ) ); $child_comment = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, ) ); // Check if comment 1 does not have the child link. @@ -1596,28 +1610,28 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_delete_item() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, )); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( $this->post_id, $data['post'] ); + $this->assertEquals( self::$post_id, $data['post'] ); } public function test_delete_item_skip_trash() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + '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; @@ -1625,16 +1639,16 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( $this->post_id, $data['post'] ); + $this->assertEquals( self::$post_id, $data['post'] ); } public function test_delete_item_already_trashed() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, )); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); $response = $this->server->dispatch( $request ); @@ -1645,7 +1659,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_delete_comment_invalid_id() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); @@ -1654,27 +1668,27 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase } public function test_delete_comment_without_permission() { - wp_set_current_user( $this->subscriber_id ); + wp_set_current_user( self::$subscriber_id ); - $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $this->approved_id ) ); + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); } public function test_delete_child_comment_link() { - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); $comment_id_1 = $this->factory->comment->create( array( 'comment_approved' => 1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, ) ); $child_comment = $this->factory->comment->create( array( 'comment_approved' => 1, 'comment_parent' => $comment_id_1, - 'comment_post_ID' => $this->post_id, - 'user_id' => $this->subscriber_id, + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$subscriber_id, ) ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%s', $child_comment ) ); @@ -1747,12 +1761,12 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $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/' . $this->approved_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id ); $response = $this->server->dispatch( $request ); $this->assertArrayHasKey( 'my_custom_int', $response->data ); - $request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . $this->approved_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . self::$approved_id ); $request->set_body_params(array( 'my_custom_int' => 123, 'content' => 'abc', @@ -1760,14 +1774,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 1 ); $this->server->dispatch( $request ); - $this->assertEquals( 123, get_comment_meta( $this->approved_id, 'my_custom_int', true ) ); + $this->assertEquals( 123, get_comment_meta( self::$approved_id, 'my_custom_int', true ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->set_body_params(array( 'my_custom_int' => 123, 'title' => 'hello', 'content' => 'goodbye', - 'post' => $this->post_id, + 'post' => self::$post_id, )); $response = $this->server->dispatch( $request ); @@ -1792,10 +1806,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'update_callback' => array( $this, 'additional_field_update_callback' ), ) ); - wp_set_current_user( $this->admin_id ); + wp_set_current_user( self::$admin_id ); // Check for error on update. - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', $this->approved_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); $request->set_body_params(array( 'my_custom_int' => 'returnError', 'content' => 'abc', diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index f94bbf6466..29f050faf3 100644 --- a/tests/phpunit/tests/rest-api/rest-pages-controller.php +++ b/tests/phpunit/tests/rest-api/rest-pages-controller.php @@ -11,17 +11,20 @@ * @group restapi */ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Testcase { + protected static $editor_id; + + public static function wpSetUpBeforeClass( $factory ) { + self::$editor_id = $factory->user->create( array( + 'role' => 'editor', + ) ); + } + + public static function wpTearDownAfterClass() { + self::delete_user( self::$editor_id ); + } public function setUp() { parent::setUp(); - - $this->editor_id = $this->factory->user->create( array( - 'role' => 'editor', - ) ); - $this->author_id = $this->factory->user->create( array( - 'role' => 'author', - ) ); - $this->has_setup_template = false; add_filter( 'theme_page_templates', array( $this, 'filter_theme_page_templates' ) ); } @@ -183,7 +186,7 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); // But they are accessible to authorized users - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertCount( 1, $data ); @@ -227,7 +230,7 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_item_with_template() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/pages' ); $params = $this->set_post_data( array( @@ -246,7 +249,7 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te $page_id = $this->factory->post->create( array( 'type' => 'page', ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/pages' ); $params = $this->set_post_data( array( @@ -267,7 +270,7 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_page_with_invalid_parent() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/pages' ); $params = $this->set_post_data( array( @@ -322,7 +325,7 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 'post_type' => 'page', ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $page_id ) ); @@ -342,7 +345,7 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 'menu_order' => 1, ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $page_id ) ); diff --git a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php index 7f0cd66f1a..79f907c7ab 100644 --- a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php +++ b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php @@ -10,6 +10,16 @@ * @group restapi */ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { + protected static $post_id; + + public static function wpSetUpBeforeClass( $factory ) { + self::$post_id = $factory->post->create(); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post_id, true ); + } + public function setUp() { parent::setUp(); @@ -54,8 +64,6 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { global $wp_rest_server; $this->server = $wp_rest_server = new Spy_REST_Server; do_action( 'rest_api_init' ); - - $this->post_id = $this->factory->post->create(); } protected function grant_write_permission() { @@ -67,9 +75,9 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { } public function test_get_value() { - add_post_meta( $this->post_id, 'test_single', 'testvalue' ); + add_post_meta( self::$post_id, 'test_single', 'testvalue' ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -86,8 +94,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { * @depends test_get_value */ public function test_get_multi_value() { - add_post_meta( $this->post_id, 'test_multi', 'value1' ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + add_post_meta( self::$post_id, 'test_multi', 'value1' ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -99,7 +107,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { $this->assertContains( 'value1', $meta['test_multi'] ); // Check after an update. - add_post_meta( $this->post_id, 'test_multi', 'value2' ); + add_post_meta( self::$post_id, 'test_multi', 'value2' ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -113,8 +121,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { * @depends test_get_value */ public function test_get_unregistered() { - add_post_meta( $this->post_id, 'test_unregistered', 'value1' ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + add_post_meta( self::$post_id, 'test_unregistered', 'value1' ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -128,8 +136,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { * @depends test_get_value */ public function test_get_registered_no_api_access() { - add_post_meta( $this->post_id, 'test_no_rest', 'for_the_wicked' ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + add_post_meta( self::$post_id, 'test_no_rest', 'for_the_wicked' ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -143,8 +151,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { * @depends test_get_value */ public function test_get_registered_api_disabled() { - add_post_meta( $this->post_id, 'test_rest_disabled', 'sleepless_nights' ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + add_post_meta( self::$post_id, 'test_rest_disabled', 'sleepless_nights' ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -176,11 +184,11 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { $this->server = $wp_rest_server = new Spy_REST_Server; do_action( 'rest_api_init' ); - add_post_meta( $this->post_id, 'test_string', 42 ); - add_post_meta( $this->post_id, 'test_number', '42' ); - add_post_meta( $this->post_id, 'test_bool', 1 ); + add_post_meta( self::$post_id, 'test_string', 42 ); + add_post_meta( self::$post_id, 'test_number', '42' ); + add_post_meta( self::$post_id, 'test_bool', 1 ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -205,7 +213,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { */ public function test_set_value() { // Ensure no data exists currently. - $values = get_post_meta( $this->post_id, 'test_single', false ); + $values = get_post_meta( self::$post_id, 'test_single', false ); $this->assertEmpty( $values ); $this->grant_write_permission(); @@ -215,13 +223,13 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_single' => 'test_value', ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $meta = get_post_meta( $this->post_id, 'test_single', false ); + $meta = get_post_meta( self::$post_id, 'test_single', false ); $this->assertNotEmpty( $meta ); $this->assertCount( 1, $meta ); $this->assertEquals( 'test_value', $meta[0] ); @@ -237,8 +245,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { */ public function test_set_duplicate_single_value() { // Start with an existing metakey and value. - $values = update_post_meta( $this->post_id, 'test_single', 'test_value' ); - $this->assertEquals( 'test_value', get_post_meta( $this->post_id, 'test_single', true ) ); + $values = update_post_meta( self::$post_id, 'test_single', 'test_value' ); + $this->assertEquals( 'test_value', get_post_meta( self::$post_id, 'test_single', true ) ); $this->grant_write_permission(); @@ -247,13 +255,13 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_single' => 'test_value', ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $meta = get_post_meta( $this->post_id, 'test_single', true ); + $meta = get_post_meta( self::$post_id, 'test_single', true ); $this->assertNotEmpty( $meta ); $this->assertEquals( 'test_value', $meta ); @@ -275,14 +283,14 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 ); // Check that the value wasn't actually updated. - $this->assertEmpty( get_post_meta( $this->post_id, 'test_single', false ) ); + $this->assertEmpty( get_post_meta( self::$post_id, 'test_single', false ) ); } /** @@ -297,12 +305,12 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { $this->grant_write_permission(); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_update', $response, 403 ); - $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth', false ) ); + $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth', false ) ); } /** @@ -317,7 +325,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { $this->grant_write_permission(); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); /** @@ -335,7 +343,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { public function test_set_value_multiple() { // Ensure no data exists currently. - $values = get_post_meta( $this->post_id, 'test_multi', false ); + $values = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertEmpty( $values ); $this->grant_write_permission(); @@ -345,13 +353,13 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_multi' => array( 'val1' ), ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $meta = get_post_meta( $this->post_id, 'test_multi', false ); + $meta = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertNotEmpty( $meta ); $this->assertCount( 1, $meta ); $this->assertEquals( 'val1', $meta[0] ); @@ -367,7 +375,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $meta = get_post_meta( $this->post_id, 'test_multi', false ); + $meta = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertNotEmpty( $meta ); $this->assertCount( 2, $meta ); $this->assertContains( 'val1', $meta ); @@ -378,9 +386,9 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { * Test removing only one item with duplicate items. */ public function test_set_value_remove_one() { - add_post_meta( $this->post_id, 'test_multi', 'c' ); - add_post_meta( $this->post_id, 'test_multi', 'n' ); - add_post_meta( $this->post_id, 'test_multi', 'n' ); + add_post_meta( self::$post_id, 'test_multi', 'c' ); + add_post_meta( self::$post_id, 'test_multi', 'n' ); + add_post_meta( self::$post_id, 'test_multi', 'n' ); $this->grant_write_permission(); @@ -389,13 +397,13 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_multi' => array( 'c', 'n' ), ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $meta = get_post_meta( $this->post_id, 'test_multi', false ); + $meta = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertNotEmpty( $meta ); $this->assertCount( 2, $meta ); $this->assertContains( 'c', $meta ); @@ -407,7 +415,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { */ public function test_set_value_multiple_unauthenticated() { // Ensure no data exists currently. - $values = get_post_meta( $this->post_id, 'test_multi', false ); + $values = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertEmpty( $values ); wp_set_current_user( 0 ); @@ -417,13 +425,13 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_multi' => array( 'val1' ), ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 ); - $meta = get_post_meta( $this->post_id, 'test_multi', false ); + $meta = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertEmpty( $meta ); } @@ -439,17 +447,17 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { $this->grant_write_permission(); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_update', $response, 403 ); - $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth_multi', false ) ); + $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth_multi', false ) ); } public function test_add_multi_value_db_error() { // Ensure no data exists currently. - $values = get_post_meta( $this->post_id, 'test_multi', false ); + $values = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertEmpty( $values ); $this->grant_write_permission(); @@ -459,7 +467,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_multi' => array( 'val1' ), ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); /** @@ -478,8 +486,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { } public function test_remove_multi_value_db_error() { - add_post_meta( $this->post_id, 'test_multi', 'val1' ); - $values = get_post_meta( $this->post_id, 'test_multi', false ); + add_post_meta( self::$post_id, 'test_multi', 'val1' ); + $values = get_post_meta( self::$post_id, 'test_multi', false ); $this->assertEquals( array( 'val1' ), $values ); $this->grant_write_permission(); @@ -489,7 +497,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_multi' => array(), ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); /** @@ -508,8 +516,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { } public function test_delete_value() { - add_post_meta( $this->post_id, 'test_single', 'val1' ); - $current = get_post_meta( $this->post_id, 'test_single', true ); + add_post_meta( self::$post_id, 'test_single', 'val1' ); + $current = get_post_meta( self::$post_id, 'test_single', true ); $this->assertEquals( 'val1', $current ); $this->grant_write_permission(); @@ -519,13 +527,13 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_single' => null, ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); - $meta = get_post_meta( $this->post_id, 'test_single', false ); + $meta = get_post_meta( self::$post_id, 'test_single', false ); $this->assertEmpty( $meta ); } @@ -533,8 +541,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { * @depends test_delete_value */ public function test_delete_value_blocked() { - add_post_meta( $this->post_id, 'test_bad_auth', 'val1' ); - $current = get_post_meta( $this->post_id, 'test_bad_auth', true ); + add_post_meta( self::$post_id, 'test_bad_auth', 'val1' ); + $current = get_post_meta( self::$post_id, 'test_bad_auth', true ); $this->assertEquals( 'val1', $current ); $this->grant_write_permission(); @@ -544,13 +552,13 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_bad_auth' => null, ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); - $meta = get_post_meta( $this->post_id, 'test_bad_auth', true ); + $meta = get_post_meta( self::$post_id, 'test_bad_auth', true ); $this->assertEquals( 'val1', $meta ); } @@ -558,8 +566,8 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { * @depends test_delete_value */ public function test_delete_value_db_error() { - add_post_meta( $this->post_id, 'test_single', 'val1' ); - $current = get_post_meta( $this->post_id, 'test_single', true ); + add_post_meta( self::$post_id, 'test_single', 'val1' ); + $current = get_post_meta( self::$post_id, 'test_single', true ); $this->assertEquals( 'val1', $current ); $this->grant_write_permission(); @@ -569,7 +577,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 'test_single' => null, ), ); - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( $data ); /** * Disable showing error as the below is going to intentionally @@ -587,7 +595,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { } public function test_get_schema() { - $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index dfc832ceec..3365f7767c 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -10,22 +10,36 @@ * @group restapi */ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Testcase { + protected static $post_id; + + protected static $editor_id; + protected static $author_id; + protected static $contributor_id; + + public static function wpSetUpBeforeClass( $factory ) { + self::$post_id = $factory->post->create(); + + self::$editor_id = $factory->user->create( array( + 'role' => 'editor', + ) ); + self::$author_id = $factory->user->create( array( + 'role' => 'author', + ) ); + self::$contributor_id = $factory->user->create( array( + 'role' => 'contributor', + ) ); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post_id, true ); + + self::delete_user( self::$editor_id ); + self::delete_user( self::$author_id ); + self::delete_user( self::$contributor_id ); + } public function setUp() { parent::setUp(); - - $this->post_id = $this->factory->post->create(); - - $this->editor_id = $this->factory->user->create( array( - 'role' => 'editor', - ) ); - $this->author_id = $this->factory->user->create( array( - 'role' => 'author', - ) ); - $this->contributor_id = $this->factory->user->create( array( - 'role' => 'contributor', - ) ); - register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) ); } @@ -46,7 +60,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); // Single - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); @@ -105,8 +119,8 @@ 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' => $this->editor_id ) ); - $this->factory->post->create( array( 'post_author' => $this->author_id ) ); + $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' ); $response = $this->server->dispatch( $request ); @@ -114,25 +128,25 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( 3, count( $response->get_data() ) ); // 2 of 3 posts $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); - $request->set_param( 'author', array( $this->editor_id, $this->author_id ) ); + $request->set_param( 'author', array( self::$editor_id, self::$author_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - $this->assertEqualSets( array( $this->editor_id, $this->author_id ), wp_list_pluck( $data, 'author' ) ); + $this->assertEqualSets( array( self::$editor_id, self::$author_id ), wp_list_pluck( $data, 'author' ) ); // 1 of 3 posts $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); - $request->set_param( 'author', $this->editor_id ); + $request->set_param( 'author', self::$editor_id ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); - $this->assertEquals( $this->editor_id, $data[0]['author'] ); + $this->assertEquals( self::$editor_id, $data[0]['author'] ); } public function test_get_items_author_exclude_query() { - $this->factory->post->create( array( 'post_author' => $this->editor_id ) ); - $this->factory->post->create( array( 'post_author' => $this->author_id ) ); + $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' ); $response = $this->server->dispatch( $request ); @@ -140,22 +154,22 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( 3, count( $response->get_data() ) ); // 1 of 3 posts $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); - $request->set_param( 'author_exclude', array( $this->editor_id, $this->author_id ) ); + $request->set_param( 'author_exclude', array( self::$editor_id, self::$author_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); $this->assertEquals( 1, count( $data ) ); - $this->assertNotEquals( $this->editor_id, $data[0]['author'] ); - $this->assertNotEquals( $this->author_id, $data[0]['author'] ); + $this->assertNotEquals( self::$editor_id, $data[0]['author'] ); + $this->assertNotEquals( self::$author_id, $data[0]['author'] ); // 2 of 3 posts $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); - $request->set_param( 'author_exclude', $this->editor_id ); + $request->set_param( 'author_exclude', self::$editor_id ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); - $this->assertNotEquals( $this->editor_id, $data[0]['author'] ); - $this->assertNotEquals( $this->editor_id, $data[1]['author'] ); + $this->assertNotEquals( self::$editor_id, $data[0]['author'] ); + $this->assertNotEquals( self::$editor_id, $data[1]['author'] ); } public function test_get_items_include_query() { @@ -232,7 +246,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $request->set_param( 'status', 'draft' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'status', 'draft' ); $response = $this->server->dispatch( $request ); @@ -305,18 +319,18 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( array( $this->post_id, $post_id3, $post_id2, $post_id1 ), wp_list_pluck( $data, 'id' ) ); + $this->assertEquals( array( self::$post_id, $post_id3, $post_id2, $post_id1 ), wp_list_pluck( $data, 'id' ) ); // Permit stickies $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'ignore_sticky_posts', false ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( array( $post_id2, $this->post_id, $post_id3, $post_id1 ), wp_list_pluck( $data, 'id' ) ); + $this->assertEquals( array( $post_id2, self::$post_id, $post_id3, $post_id1 ), wp_list_pluck( $data, 'id' ) ); } public function test_get_items_offset_query() { - $id1 = $this->post_id; + $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' ) ); @@ -335,7 +349,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_tags_query() { - $id1 = $this->post_id; + $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' ) ); @@ -352,7 +366,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_tags_exclude_query() { - $id1 = $this->post_id; + $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' ) ); @@ -371,7 +385,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_tags_and_categories_query() { - $id1 = $this->post_id; + $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' ) ); @@ -391,7 +405,7 @@ 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 = $this->post_id; + $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' ) ); @@ -413,7 +427,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_sticky_query() { - $id1 = $this->post_id; + $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); update_option( 'sticky_posts', array( $id2 ) ); @@ -430,7 +444,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_sticky_with_post__in_query() { - $id1 = $this->post_id; + $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); @@ -459,7 +473,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_not_sticky_query() { - $id1 = $this->post_id; + $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); update_option( 'sticky_posts', array( $id2 ) ); @@ -476,7 +490,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_items_sticky_with_post__not_in_query() { - $id1 = $this->post_id; + $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); @@ -586,7 +600,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); // But they are accessible to authorized users - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertCount( 1, $data ); @@ -630,31 +644,31 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_get_item() { - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->check_get_post_response( $response, 'view' ); } public function test_get_item_links() { - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $links = $response->get_links(); - $this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id ), $links['self'][0]['href'] ); + $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id ), $links['self'][0]['href'] ); $this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] ); - $this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( $this->post_id ) ), $links['about'][0]['href'] ); + $this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( self::$post_id ) ), $links['about'][0]['href'] ); $replies_url = rest_url( '/wp/v2/comments' ); - $replies_url = add_query_arg( 'post', $this->post_id, $replies_url ); + $replies_url = add_query_arg( 'post', self::$post_id, $replies_url ); $this->assertEquals( $replies_url, $links['replies'][0]['href'] ); - $this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id . '/revisions' ), $links['version-history'][0]['href'] ); + $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] ); $attachments_url = rest_url( '/wp/v2/media' ); - $attachments_url = add_query_arg( 'parent', $this->post_id, $attachments_url ); + $attachments_url = add_query_arg( 'parent', self::$post_id, $attachments_url ); $this->assertEquals( $attachments_url, $links['https://api.w.org/attachment'][0]['href'] ); $term_links = $links['https://api.w.org/term']; @@ -672,23 +686,23 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertNotEmpty( $cat_link ); $this->assertNull( $format_link ); - $tags_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/tags' ) ); + $tags_url = add_query_arg( 'post', self::$post_id, rest_url( '/wp/v2/tags' ) ); $this->assertEquals( $tags_url, $tag_link['href'] ); - $category_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/categories' ) ); + $category_url = add_query_arg( 'post', self::$post_id, rest_url( '/wp/v2/categories' ) ); $this->assertEquals( $category_url, $cat_link['href'] ); } public function test_get_item_links_no_author() { - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $links = $response->get_links(); $this->assertFalse( isset( $links['author'] ) ); - wp_update_post( array( 'ID' => $this->post_id, 'post_author' => $this->author_id ) ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + wp_update_post( array( 'ID' => self::$post_id, 'post_author' => self::$author_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $links = $response->get_links(); - $this->assertEquals( rest_url( '/wp/v2/users/' . $this->author_id ), $links['author'][0]['href'] ); + $this->assertEquals( rest_url( '/wp/v2/users/' . self::$author_id ), $links['author'][0]['href'] ); } public function test_get_post_without_permission() { @@ -716,7 +730,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'context' => 'edit', ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $response = $this->server->dispatch( $request ); @@ -736,7 +750,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', $this->post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_query_params( array( 'context' => 'edit', ) ); @@ -811,21 +825,21 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te register_post_status( 'testpubstatus', array( 'public' => true ) ); register_post_status( 'testprivtatus', array( 'public' => false ) ); // Public status - wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testpubstatus' ) ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + wp_update_post( array( 'ID' => self::$post_id, 'post_status' => 'testpubstatus' ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); // Private status - wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testprivtatus' ) ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + wp_update_post( array( 'ID' => self::$post_id, 'post_status' => 'testprivtatus' ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 403, $response->get_status() ); } public function test_prepare_item() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_query_params( array( 'context' => 'edit' ) ); $response = $this->server->dispatch( $request ); @@ -833,7 +847,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_item() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -845,7 +859,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_rest_create_item() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $request->add_header( 'content-type', 'application/json' ); @@ -857,7 +871,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_invalid_id() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -870,7 +884,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_as_contributor() { - wp_set_current_user( $this->contributor_id ); + wp_set_current_user( self::$contributor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data(array( @@ -883,7 +897,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_sticky() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -899,7 +913,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_sticky_as_contributor() { - wp_set_current_user( $this->contributor_id ); + wp_set_current_user( self::$contributor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -913,11 +927,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_other_author_without_permission() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data(array( - 'author' => $this->editor_id, + 'author' => self::$editor_id, )); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); @@ -939,7 +953,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_draft() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -958,7 +972,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_private() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -974,7 +988,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_private_without_permission() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $user = wp_get_current_user(); $user->add_cap( 'publish_posts', false ); // Flush capabilities, https://core.trac.wordpress.org/ticket/28374 @@ -984,7 +998,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( 'status' => 'private', - 'author' => $this->author_id, + 'author' => self::$author_id, ) ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); @@ -993,7 +1007,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_publish_without_permission() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $user = wp_get_current_user(); $user->add_cap( 'publish_posts', false ); // Flush capabilities, https://core.trac.wordpress.org/ticket/28374 @@ -1011,7 +1025,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_invalid_status() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1024,7 +1038,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_format() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1040,7 +1054,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_invalid_format() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1060,7 +1074,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'menu_order' => rand( 1, 100 ), ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1085,7 +1099,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_invalid_author() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1098,11 +1112,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_invalid_author_without_permission() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( - 'author' => $this->editor_id, + 'author' => self::$editor_id, ) ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); @@ -1111,7 +1125,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_password() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1125,7 +1139,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_falsy_password() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1140,7 +1154,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_empty_string_password_and_sticky() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1156,7 +1170,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_password_and_sticky_fails() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1170,7 +1184,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_custom_date() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1187,7 +1201,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_custom_date_with_timezone() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1208,7 +1222,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_db_error() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array() ); @@ -1230,7 +1244,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_invalid_date() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1243,7 +1257,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_invalid_date_gmt() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1256,7 +1270,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_quotes_in_title() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1269,7 +1283,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_categories() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $category = wp_insert_term( 'Test Category', 'category' ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( @@ -1286,7 +1300,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_create_post_with_invalid_categories() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); $params = $this->set_post_data( array( 'password' => 'testing', @@ -1302,9 +1316,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_item() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $params = $this->set_post_data(); $request->set_body_params( $params ); @@ -1312,20 +1326,20 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->check_update_post_response( $response ); $new_data = $response->get_data(); - $this->assertEquals( $this->post_id, $new_data['id'] ); + $this->assertEquals( self::$post_id, $new_data['id'] ); $this->assertEquals( $params['title'], $new_data['title']['raw'] ); $this->assertEquals( $params['content'], $new_data['content']['raw'] ); $this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] ); - $post = get_post( $this->post_id ); + $post = get_post( self::$post_id ); $this->assertEquals( $params['title'], $post->post_title ); $this->assertEquals( $params['content'], $post->post_content ); $this->assertEquals( $params['excerpt'], $post->post_excerpt ); } public function test_rest_update_post() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->add_header( 'content-type', 'application/json' ); $params = $this->set_post_data(); $request->set_body( wp_json_encode( $params ) ); @@ -1333,20 +1347,20 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->check_update_post_response( $response ); $new_data = $response->get_data(); - $this->assertEquals( $this->post_id, $new_data['id'] ); + $this->assertEquals( self::$post_id, $new_data['id'] ); $this->assertEquals( $params['title'], $new_data['title']['raw'] ); $this->assertEquals( $params['content'], $new_data['content']['raw'] ); $this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] ); - $post = get_post( $this->post_id ); + $post = get_post( self::$post_id ); $this->assertEquals( $params['title'], $post->post_title ); $this->assertEquals( $params['content'], $post->post_content ); $this->assertEquals( $params['excerpt'], $post->post_excerpt ); } public function test_rest_update_post_raw() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->add_header( 'content-type', 'application/json' ); $params = $this->set_raw_post_data(); $request->set_body( wp_json_encode( $params ) ); @@ -1354,20 +1368,20 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->check_update_post_response( $response ); $new_data = $response->get_data(); - $this->assertEquals( $this->post_id, $new_data['id'] ); + $this->assertEquals( self::$post_id, $new_data['id'] ); $this->assertEquals( $params['title']['raw'], $new_data['title']['raw'] ); $this->assertEquals( $params['content']['raw'], $new_data['content']['raw'] ); $this->assertEquals( $params['excerpt']['raw'], $new_data['excerpt']['raw'] ); - $post = get_post( $this->post_id ); + $post = get_post( self::$post_id ); $this->assertEquals( $params['title']['raw'], $post->post_title ); $this->assertEquals( $params['content']['raw'], $post->post_content ); $this->assertEquals( $params['excerpt']['raw'], $post->post_excerpt ); } public function test_update_post_without_extra_params() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data(); unset( $params['type'] ); unset( $params['name'] ); @@ -1380,14 +1394,14 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_without_permission() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $user = wp_get_current_user(); $user->add_cap( 'edit_published_posts', false ); // Flush capabilities, https://core.trac.wordpress.org/ticket/28374 $user->get_role_caps(); $user->update_user_level_from_caps(); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data(); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); @@ -1396,9 +1410,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_sticky_as_contributor() { - wp_set_current_user( $this->contributor_id ); + wp_set_current_user( self::$contributor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'sticky' => true, 'status' => 'pending', @@ -1410,7 +1424,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_invalid_id() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -1419,18 +1433,18 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_invalid_route() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); } public function test_update_post_with_format() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'format' => 'gallery', ) ); @@ -1444,9 +1458,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_with_invalid_format() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'format' => 'testformat', ) ); @@ -1457,12 +1471,12 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_ignore_readonly() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $new_content = rand_str(); $expected_modified = current_time( 'mysql' ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'modified' => '2010-06-01T02:00:00Z', 'content' => $new_content, @@ -1483,9 +1497,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_with_invalid_date() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'date' => rand_str(), ) ); @@ -1496,9 +1510,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_with_invalid_date_gmt() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'date_gmt' => rand_str(), ) ); @@ -1509,9 +1523,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_slug() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'slug' => 'sample-slug', ) ); @@ -1525,9 +1539,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_slug_accented_chars() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'slug' => 'tęst-acceńted-chäræcters', ) ); @@ -1541,9 +1555,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_sticky() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'sticky' => true, ) ); @@ -1556,7 +1570,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( true, is_sticky( $post->ID ) ); // Updating another field shouldn't change sticky status - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'title' => 'This should not reset sticky', ) ); @@ -1570,9 +1584,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_excerpt() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( array( 'excerpt' => 'An Excerpt', ) ); @@ -1583,9 +1597,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_empty_excerpt() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( array( 'excerpt' => '', ) ); @@ -1596,9 +1610,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_content() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( array( 'content' => 'Some Content', ) ); @@ -1609,9 +1623,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_empty_content() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( array( 'content' => '', ) ); @@ -1622,9 +1636,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_with_password_and_sticky_fails() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'password' => '123', 'sticky' => true, @@ -1636,11 +1650,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_stick_post_with_password_fails() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - stick_post( $this->post_id ); + stick_post( self::$post_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'password' => '123', ) ); @@ -1651,11 +1665,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_password_protected_post_with_sticky_fails() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - wp_update_post( array( 'ID' => $this->post_id, 'post_password' => '123' ) ); + wp_update_post( array( 'ID' => self::$post_id, 'post_password' => '123' ) ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'sticky' => true, ) ); @@ -1666,9 +1680,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_update_post_with_quotes_in_title() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'title' => "Rob O'Rourke's Diary", ) ); @@ -1680,10 +1694,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_update_post_with_categories() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $category = wp_insert_term( 'Test Category', 'category' ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'title' => 'Tester', 'categories' => array( @@ -1714,11 +1728,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_update_post_with_empty_categories() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $category = wp_insert_term( 'Test Category', 'category' ); - wp_set_object_terms( $this->post_id, $category['term_id'], 'category' ); + wp_set_object_terms( self::$post_id, $category['term_id'], 'category' ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $params = $this->set_post_data( array( 'title' => 'Tester', 'categories' => array(), @@ -1731,7 +1745,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_item() { $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); $response = $this->server->dispatch( $request ); @@ -1744,7 +1758,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_item_skip_trash() { $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); $request['force'] = true; @@ -1758,7 +1772,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_item_already_trashed() { $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -1767,7 +1781,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_delete_post_invalid_id() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = $this->server->dispatch( $request ); @@ -1777,7 +1791,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function test_delete_post_invalid_post_type() { $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $page_id ); $response = $this->server->dispatch( $request ); @@ -1786,9 +1800,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te } public function test_delete_post_without_permission() { - wp_set_current_user( $this->author_id ); + wp_set_current_user( self::$author_id ); - $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); @@ -1905,9 +1919,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'update_callback' => array( $this, 'additional_field_update_callback' ), ) ); - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); // Check for error on update. - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $request->set_body_params( array( 'my_custom_int' => 'returnError', ) ); diff --git a/tests/phpunit/tests/rest-api/rest-revisions-controller.php b/tests/phpunit/tests/rest-api/rest-revisions-controller.php index 79bdd2064c..5d0836660c 100644 --- a/tests/phpunit/tests/rest-api/rest-revisions-controller.php +++ b/tests/phpunit/tests/rest-api/rest-revisions-controller.php @@ -10,22 +10,40 @@ * @group restapi */ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase { + protected static $post_id; + protected static $page_id; - public function setUp() { - parent::setUp(); - $this->post_id = $this->factory->post->create(); - $this->page_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + protected static $editor_id; + protected static $contributor_id; - $this->editor_id = $this->factory->user->create( array( + public static function wpSetUpBeforeClass( $factory ) { + self::$post_id = $factory->post->create(); + self::$page_id = $factory->post->create( array( 'post_type' => 'page' ) ); + + self::$editor_id = $factory->user->create( array( 'role' => 'editor', ) ); - $this->contributor_id = $this->factory->user->create( array( + self::$contributor_id = $factory->user->create( array( 'role' => 'contributor', ) ); - wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => $this->post_id ) ); - wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => $this->post_id ) ); - $revisions = wp_get_post_revisions( $this->post_id ); + wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => self::$post_id ) ); + wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => self::$post_id ) ); + } + + public static function wpTearDownAfterClass() { + // Also deletes revisions. + wp_delete_post( self::$post_id, true ); + wp_delete_post( self::$page_id, true ); + + self::delete_user( self::$editor_id ); + self::delete_user( self::$contributor_id ); + } + + public function setUp() { + parent::setUp(); + + $revisions = wp_get_post_revisions( self::$post_id ); $this->revision_1 = array_pop( $revisions ); $this->revision_id1 = $this->revision_1->ID; $this->revision_2 = array_pop( $revisions ); @@ -42,13 +60,13 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase public function test_context_param() { // Collection - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] ); // Single - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_1->ID ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_1->ID ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); @@ -56,8 +74,8 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_items() { - wp_set_current_user( $this->editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions' ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 200, $response->get_status() ); @@ -73,32 +91,32 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase public function test_get_items_no_permission() { wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); - wp_set_current_user( $this->contributor_id ); + wp_set_current_user( self::$contributor_id ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); } public function test_get_items_missing_parent() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); } public function test_get_items_invalid_parent_post_type() { - wp_set_current_user( $this->editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->page_id . '/revisions' ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); } public function test_get_item() { - wp_set_current_user( $this->editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $this->check_get_revision_response( $response, $this->revision_1 ); @@ -121,8 +139,8 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_item_embed_context() { - wp_set_current_user( $this->editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $request->set_param( 'context', 'embed' ); $response = $this->server->dispatch( $request ); $fields = array( @@ -140,54 +158,54 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase public function test_get_item_no_permission() { wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); - wp_set_current_user( $this->contributor_id ); + wp_set_current_user( self::$contributor_id ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); } public function test_get_item_missing_parent() { - wp_set_current_user( $this->editor_id ); + wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); } public function test_get_item_invalid_parent_post_type() { - wp_set_current_user( $this->editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->page_id . '/revisions/' . $this->revision_id1 ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); } public function test_delete_item() { - wp_set_current_user( $this->editor_id ); - $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $this->assertNull( get_post( $this->revision_id1 ) ); } public function test_delete_item_no_permission() { - wp_set_current_user( $this->contributor_id ); - $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + wp_set_current_user( self::$contributor_id ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); } public function test_prepare_item() { - wp_set_current_user( $this->editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + wp_set_current_user( self::$editor_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); $this->check_get_revision_response( $response, $this->revision_1 ); } public function test_get_item_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; @@ -207,13 +225,13 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase } public function test_create_item() { - $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $this->post_id . '/revisions' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_no_route', $response, 404 ); } public function test_update_item() { - $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_no_route', $response, 404 ); } @@ -233,7 +251,7 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase 'update_callback' => array( $this, 'additional_field_update_callback' ), ) ); - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); @@ -243,7 +261,7 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase wp_set_current_user( 1 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); $response = $this->server->dispatch( $request ); $this->assertArrayHasKey( 'my_custom_int', $response->data ); diff --git a/tests/phpunit/tests/rest-api/rest-settings-controller.php b/tests/phpunit/tests/rest-api/rest-settings-controller.php index f3d7b0a12c..65661e933f 100644 --- a/tests/phpunit/tests/rest-api/rest-settings-controller.php +++ b/tests/phpunit/tests/rest-api/rest-settings-controller.php @@ -10,12 +10,20 @@ * @group restapi */ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase { + protected static $administrator; + + public static function wpSetUpBeforeClass( $factory ) { + self::$administrator = $factory->user->create( array( + 'role' => 'administrator', + ) ); + } + + public static function wpTearDownAfterClass() { + self::delete_user( self::$administrator ); + } public function setUp() { parent::setUp(); - $this->administrator = $this->factory->user->create( array( - 'role' => 'administrator', - ) ); $this->endpoint = new WP_REST_Settings_Controller(); } @@ -37,7 +45,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_item() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); @@ -63,7 +71,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_item_value_is_cast_to_type() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); update_option( 'posts_per_page', 'invalid_number' ); // this is cast to (int) 1 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' ); $response = $this->server->dispatch( $request ); @@ -74,7 +82,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_item_with_custom_setting() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); register_setting( 'somegroup', 'mycustomsetting', array( 'show_in_rest' => array( @@ -114,7 +122,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } public function test_get_item_with_filter() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); add_filter( 'rest_pre_get_setting', array( $this, 'get_setting_custom_callback' ), 10, 3 ); @@ -155,7 +163,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_item() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); $request->set_param( 'title', 'The new title!' ); $response = $this->server->dispatch( $request ); @@ -176,7 +184,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_item_with_filter() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); $request->set_param( 'title', 'The old title!' ); @@ -207,7 +215,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } public function test_update_item_with_invalid_type() { - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); $request->set_param( 'title', array( 'rendered' => 'This should fail.' ) ); $response = $this->server->dispatch( $request ); @@ -220,7 +228,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase public function test_update_item_with_null() { update_option( 'posts_per_page', 9 ); - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); $request->set_param( 'posts_per_page', null ); $response = $this->server->dispatch( $request ); @@ -233,7 +241,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase public function test_update_item_with_invalid_enum() { update_option( 'posts_per_page', 9 ); - wp_set_current_user( $this->administrator ); + wp_set_current_user( self::$administrator ); $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); $request->set_param( 'default_ping_status', 'open&closed' ); $response = $this->server->dispatch( $request ); diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index b200fc455c..f2131d4806 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -10,21 +10,29 @@ * @group restapi */ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { + protected static $user; + protected static $editor; + + public static function wpSetUpBeforeClass( $factory ) { + self::$user = $factory->user->create( array( + 'role' => 'administrator', + ) ); + self::$editor = $factory->user->create( array( + 'role' => 'editor', + 'user_email' => 'editor@example.com', + ) ); + } + + public static function wpTearDownAfterClass() { + self::delete_user( self::$user ); + self::delete_user( self::$editor ); + } + /** * This function is run before each method */ public function setUp() { parent::setUp(); - - $this->user = $this->factory->user->create( array( - 'role' => 'administrator', - ) ); - - $this->editor = $this->factory->user->create( array( - 'role' => 'editor', - 'user_email' => 'editor@example.com', - ) ); - $this->endpoint = new WP_REST_Users_Controller(); } @@ -46,7 +54,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); // Single - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users/' . $this->user ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users/' . self::$user ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); @@ -75,7 +83,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'context', 'view' ); @@ -90,7 +98,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_with_edit_context() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'context', 'edit' ); @@ -113,7 +121,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 401, $response->get_status() ); //test with a user logged in but without sufficient capabilities; capability in question: 'list_users' - wp_set_current_user( $this->editor ); + wp_set_current_user( self::$editor ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); @@ -127,8 +135,8 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( array(), $response->get_data() ); - $this->factory->post->create( array( 'post_author' => $this->editor ) ); - $this->factory->post->create( array( 'post_author' => $this->user, 'post_status' => 'draft' ) ); + $this->factory->post->create( array( 'post_author' => self::$editor ) ); + $this->factory->post->create( array( 'post_author' => self::$user, 'post_status' => 'draft' ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $response = $this->server->dispatch( $request ); @@ -148,7 +156,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { * @group test */ public function test_get_items_pagination_headers() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); // Start of the index, including the three existing users for ( $i = 0; $i < 47; $i++ ) { $this->factory->user->create( array( @@ -210,7 +218,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_per_page() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); for ( $i = 0; $i < 20; $i++ ) { $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); } @@ -224,7 +232,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_page() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); for ( $i = 0; $i < 20; $i++ ) { $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); } @@ -242,7 +250,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_orderby_name() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $low_id = $this->factory->user->create( array( 'display_name' => 'AAAAA' ) ); $mid_id = $this->factory->user->create( array( 'display_name' => 'NNNNN' ) ); $high_id = $this->factory->user->create( array( 'display_name' => 'ZZZZ' ) ); @@ -263,7 +271,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_orderby_url() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $low_id = $this->factory->user->create( array( 'user_url' => 'http://a.com' ) ); $high_id = $this->factory->user->create( array( 'user_url' => 'http://b.com' ) ); @@ -289,7 +297,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_orderby_slug() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $high_id = $this->factory->user->create( array( 'user_nicename' => 'blogin' ) ); $low_id = $this->factory->user->create( array( 'user_nicename' => 'alogin' ) ); @@ -315,7 +323,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_orderby_email() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $high_id = $this->factory->user->create( array( 'user_email' => 'bemail@gmail.com' ) ); $low_id = $this->factory->user->create( array( 'user_email' => 'aemail@gmail.com' ) ); @@ -356,7 +364,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_offset() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); // 2 users created in __construct(), plus default user $this->factory->user->create(); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); @@ -374,7 +382,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_include_query() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $id1 = $this->factory->user->create(); $id2 = $this->factory->user->create(); $id3 = $this->factory->user->create(); @@ -400,7 +408,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_exclude_query() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $id1 = $this->factory->user->create(); $id2 = $this->factory->user->create(); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); @@ -416,7 +424,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_search() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'search', 'yololololo' ); $response = $this->server->dispatch( $request ); @@ -440,7 +448,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_items_slug_query() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $this->factory->user->create( array( 'display_name' => 'foo', 'user_login' => 'bar' ) ); $id2 = $this->factory->user->create( array( 'display_name' => 'Moo', 'user_login' => 'foo' ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); @@ -453,7 +461,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { // Note: Do not test using editor role as there is an editor role created in testing and it makes it hard to test this functionality. public function test_get_items_roles() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $tango = $this->factory->user->create( array( 'display_name' => 'tango', 'role' => 'subscriber' ) ); $yolo = $this->factory->user->create( array( 'display_name' => 'yolo', 'role' => 'author' ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); @@ -472,14 +480,14 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $request->set_param( 'roles', 'author' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 ); - wp_set_current_user( $this->editor ); + wp_set_current_user( self::$editor ); $request->set_param( 'roles', 'author' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 ); } public function test_get_items_invalid_roles() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $lolz = $this->factory->user->create( array( 'display_name' => 'lolz', 'role' => 'author' ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); $request->set_param( 'roles', 'ilovesteak,author' ); @@ -497,7 +505,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_get_item() { $user_id = $this->factory->user->create(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -506,7 +514,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_prepare_item() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request; $request->set_param( 'context', 'edit' ); $user = get_user_by( 'id', get_current_user_id() ); @@ -515,9 +523,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_user_avatar_urls() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->editor ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) ); $response = $this->server->dispatch( $request ); @@ -526,7 +534,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertArrayHasKey( 48, $data['avatar_urls'] ); $this->assertArrayHasKey( 96, $data['avatar_urls'] ); - $user = get_user_by( 'id', $this->editor ); + $user = get_user_by( 'id', self::$editor ); /** * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar * server when building the url string. @@ -535,7 +543,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_user_invalid_id() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'GET', '/wp/v2/users/100' ); $response = $this->server->dispatch( $request ); @@ -543,7 +551,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_user_empty_capabilities() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $this->allow_user_to_manage_multisite(); $lolz = $this->factory->user->create( array( 'display_name' => 'lolz', 'roles' => '' ) ); @@ -559,9 +567,9 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_item_without_permission() { - wp_set_current_user( $this->editor ); + wp_set_current_user( self::$editor ); - $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->user ) ); + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 ); @@ -622,7 +630,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_get_current_user() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' ); @@ -631,7 +639,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $headers = $response->get_headers(); $this->assertArrayHasKey( 'Location', $headers ); - $this->assertEquals( rest_url( 'wp/v2/users/' . $this->user ), $headers['Location'] ); + $this->assertEquals( rest_url( 'wp/v2/users/' . self::$user ), $headers['Location'] ); } public function test_get_current_user_without_permission() { @@ -644,7 +652,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_item() { $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $params = array( 'username' => 'testuser', @@ -670,7 +678,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_json_create_user() { $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $params = array( 'username' => 'testjsonuser', @@ -687,7 +695,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_create_user_without_permission() { - wp_set_current_user( $this->editor ); + wp_set_current_user( self::$editor ); $params = array( 'username' => 'homersimpson', @@ -705,7 +713,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_user_invalid_id() { $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $params = array( 'id' => '156', @@ -724,7 +732,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_user_invalid_email() { $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $params = array( 'username' => 'lisasimpson', @@ -742,7 +750,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_create_user_invalid_role() { $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $params = array( 'username' => 'maggiesimpson', @@ -768,7 +776,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'user_url' => 'http://apple.com', )); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $userdata = get_userdata( $user_id ); $pw_before = $userdata->user_pass; @@ -803,7 +811,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user1 = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com' ) ); $user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) ); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 ); $request->set_param( 'email', 'testjson@example.com' ); @@ -816,7 +824,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user1 = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com' ) ); $user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) ); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 ); $request->set_param( 'username', 'test_json_user' ); @@ -829,7 +837,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user1 = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com' ) ); $user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) ); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 ); $request->set_param( 'slug', 'test_json_user' ); @@ -847,7 +855,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'last_name' => 'Original Last', )); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $params = array( 'username' => 'test_json_update', @@ -882,7 +890,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_user_role() { $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $this->allow_user_to_manage_multisite(); $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -900,14 +908,14 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } public function test_update_user_role_invalid_privilege_escalation() { - wp_set_current_user( $this->editor ); + wp_set_current_user( self::$editor ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->editor ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) ); $request->set_param( 'roles', array( 'administrator' ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_edit_roles', $response, 403 ); - $user = get_userdata( $this->editor ); + $user = get_userdata( self::$editor ); $this->assertArrayHasKey( 'editor', $user->caps ); $this->assertArrayNotHasKey( 'administrator', $user->caps ); } @@ -954,22 +962,22 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_user_role_invalid_role() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $this->allow_user_to_manage_multisite(); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->editor ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) ); $request->set_param( 'roles', array( 'BeSharp' ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_user_invalid_role', $response, 400 ); - $user = get_userdata( $this->editor ); + $user = get_userdata( self::$editor ); $this->assertArrayHasKey( 'editor', $user->caps ); $this->assertArrayNotHasKey( 'BeSharp', $user->caps ); } public function test_update_user_without_permission() { - wp_set_current_user( $this->editor ); + wp_set_current_user( self::$editor ); $params = array( 'username' => 'homersimpson', @@ -977,7 +985,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'email' => 'chunkylover53@aol.com', ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->user ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$user ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); @@ -987,7 +995,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_update_user_invalid_id() { $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $params = array( 'id' => '156', @@ -996,7 +1004,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 'email' => 'smartgirl63_@yahoo.com', ); - $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->editor ) ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); @@ -1008,7 +1016,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) ); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $userdata = get_userdata( $user_id ); // cache for later $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -1024,7 +1032,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) ); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $userdata = get_userdata( $user_id ); // cache for later $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); @@ -1040,7 +1048,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->editor ); + wp_set_current_user( self::$editor ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request['force'] = true; @@ -1051,7 +1059,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { public function test_delete_user_invalid_id() { $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/100' ); $request['force'] = true; @@ -1075,7 +1083,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( $user_id, $post->post_author ); // Delete our test user, and reassign to the new author - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request['force'] = true; $request->set_param( 'reassign', $reassign_id ); @@ -1092,7 +1100,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $user_id = $this->factory->user->create(); $this->allow_user_to_manage_multisite(); - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); $request['force'] = true; @@ -1219,7 +1227,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } // Check for error on update. - $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/users/%d', $this->user ) ); + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/users/%d', self::$user ) ); $request->set_body_params( array( 'my_custom_int' => 'returnError', ) ); @@ -1308,7 +1316,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { } protected function allow_user_to_manage_multisite() { - wp_set_current_user( $this->user ); + wp_set_current_user( self::$user ); $user = wp_get_current_user(); if ( is_multisite() ) {