Share fixtures in REST API endpoint tests.

As sparrows' tears shed steadily
Make widest rivers filled,

`setUp()` routines run prodig'ly
Add minutes to a build.

So cull ye fixtures profligate!
Direct thine frugal gaze!

Our savings here - a half-minute -
When multiplied: Amaze!

See #30017.

git-svn-id: https://develop.svn.wordpress.org/trunk@38975 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-10-27 02:56:28 +00:00
parent 5da7a1dccf
commit eb9e2b9207
9 changed files with 713 additions and 621 deletions

View File

@ -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 ) );

View File

@ -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 );

File diff suppressed because it is too large Load Diff

View File

@ -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 ) );

View File

@ -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();

View File

@ -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',
) );

View File

@ -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 );

View File

@ -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 );

View File

@ -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() ) {