WordPress.org

Make WordPress Core

Changeset 38975


Ignore:
Timestamp:
10/27/2016 02:56:28 AM (4 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk/tests/phpunit/tests/rest-api
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r38968 r38975  
    1111 */
    1212class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
     13    protected static $editor_id;
     14    protected static $author_id;
     15    protected static $contributor_id;
     16    protected static $uploader_id;
     17
     18    public static function wpSetUpBeforeClass( $factory ) {
     19        self::$editor_id = $factory->user->create( array(
     20            'role' => 'editor',
     21        ) );
     22        self::$author_id = $factory->user->create( array(
     23            'role' => 'author',
     24        ) );
     25        self::$contributor_id = $factory->user->create( array(
     26            'role' => 'contributor',
     27        ) );
     28        self::$uploader_id = $factory->user->create( array(
     29            'role' => 'uploader',
     30        ) );
     31    }
     32
     33    public static function wpTearDownAfterClass() {
     34        self::delete_user( self::$editor_id );
     35        self::delete_user( self::$author_id );
     36        self::delete_user( self::$contributor_id );
     37        self::delete_user( self::$uploader_id );
     38    }
    1339
    1440    public function setUp() {
    1541        parent::setUp();
    16 
    17         $this->editor_id = $this->factory->user->create( array(
    18             'role' => 'editor',
    19         ) );
    20         $this->author_id = $this->factory->user->create( array(
    21             'role' => 'author',
    22         ) );
    23         $this->contributor_id = $this->factory->user->create( array(
    24             'role' => 'contributor',
    25         ) );
    2642
    2743        // Add an uploader role to test upload capabilities.
     
    3147        $role->add_cap( 'read' );
    3248        $role->add_cap( 'level_0' );
    33         $this->uploader_id = $this->factory->user->create( array(
    34             'role' => 'uploader',
    35         ) );
    3649
    3750        $orig_file = DIR_TESTDATA . '/images/canola.jpg';
     
    180193
    181194    public function test_get_items_logged_in_editor() {
    182         wp_set_current_user( $this->editor_id );
     195        wp_set_current_user( self::$editor_id );
    183196        $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array(
    184197            'post_mime_type' => 'image/jpeg',
     
    281294
    282295    public function test_get_items_invalid_status_param_is_error_response() {
    283         wp_set_current_user( $this->editor_id );
     296        wp_set_current_user( self::$editor_id );
    284297        $this->factory->attachment->create_object( $this->test_file, 0, array(
    285298            'post_mime_type' => 'image/jpeg',
     
    308321        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    309322        // Properly authorized users can make the request
    310         wp_set_current_user( $this->editor_id );
     323        wp_set_current_user( self::$editor_id );
    311324        $response = $this->server->dispatch( $request );
    312325        $this->assertEquals( 200, $response->get_status() );
     
    416429
    417430    public function test_create_item() {
    418         wp_set_current_user( $this->author_id );
     431        wp_set_current_user( self::$author_id );
    419432        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    420433        $request->set_header( 'Content-Type', 'image/jpeg' );
     
    428441
    429442    public function test_create_item_default_filename_title() {
    430         wp_set_current_user( $this->author_id );
     443        wp_set_current_user( self::$author_id );
    431444        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    432445        $request->set_file_params( array(
     
    446459
    447460    public function test_create_item_with_files() {
    448         wp_set_current_user( $this->author_id );
     461        wp_set_current_user( self::$author_id );
    449462        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    450463        $request->set_file_params( array(
     
    462475
    463476    public function test_create_item_with_upload_files_role() {
    464         wp_set_current_user( $this->uploader_id );
     477        wp_set_current_user( self::$uploader_id );
    465478        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    466479        $request->set_file_params( array(
     
    478491
    479492    public function test_create_item_empty_body() {
    480         wp_set_current_user( $this->author_id );
     493        wp_set_current_user( self::$author_id );
    481494        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    482495        $response = $this->server->dispatch( $request );
     
    485498
    486499    public function test_create_item_missing_content_type() {
    487         wp_set_current_user( $this->author_id );
     500        wp_set_current_user( self::$author_id );
    488501        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    489502        $request->set_body( file_get_contents( $this->test_file ) );
     
    493506
    494507    public function test_create_item_missing_content_disposition() {
    495         wp_set_current_user( $this->author_id );
     508        wp_set_current_user( self::$author_id );
    496509        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    497510        $request->set_header( 'Content-Type', 'image/jpeg' );
     
    502515
    503516    public function test_create_item_bad_md5_header() {
    504         wp_set_current_user( $this->author_id );
     517        wp_set_current_user( self::$author_id );
    505518        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    506519        $request->set_header( 'Content-Type', 'image/jpeg' );
     
    513526
    514527    public function test_create_item_with_files_bad_md5_header() {
    515         wp_set_current_user( $this->author_id );
     528        wp_set_current_user( self::$author_id );
    516529        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    517530        $request->set_file_params( array(
     
    529542
    530543    public function test_create_item_invalid_upload_files_capability() {
    531         wp_set_current_user( $this->contributor_id );
     544        wp_set_current_user( self::$contributor_id );
    532545        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    533546        $response = $this->server->dispatch( $request );
     
    536549
    537550    public function test_create_item_invalid_edit_permissions() {
    538         $post_id = $this->factory->post->create( array( 'post_author' => $this->editor_id ) );
    539         wp_set_current_user( $this->author_id );
     551        $post_id = $this->factory->post->create( array( 'post_author' => self::$editor_id ) );
     552        wp_set_current_user( self::$author_id );
    540553        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    541554        $request->set_param( 'post', $post_id );
     
    545558
    546559    public function test_create_item_invalid_upload_permissions() {
    547         $post_id = $this->factory->post->create( array( 'post_author' => $this->editor_id ) );
    548         wp_set_current_user( $this->uploader_id );
     560        $post_id = $this->factory->post->create( array( 'post_author' => self::$editor_id ) );
     561        wp_set_current_user( self::$uploader_id );
    549562        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    550563        $request->set_param( 'post', $post_id );
     
    555568    public function test_create_item_invalid_post_type() {
    556569        $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) );
    557         wp_set_current_user( $this->editor_id );
     570        wp_set_current_user( self::$editor_id );
    558571        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    559572        $request->set_header( 'Content-Type', 'image/jpeg' );
     
    566579
    567580    public function test_create_item_alt_text() {
    568         wp_set_current_user( $this->author_id );
     581        wp_set_current_user( self::$author_id );
    569582        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    570583        $request->set_header( 'Content-Type', 'image/jpeg' );
     
    579592
    580593    public function test_create_item_unsafe_alt_text() {
    581         wp_set_current_user( $this->author_id );
     594        wp_set_current_user( self::$author_id );
    582595        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    583596        $request->set_header( 'Content-Type', 'image/jpeg' );
     
    591604
    592605    public function test_update_item() {
    593         wp_set_current_user( $this->editor_id );
    594         $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    595             'post_mime_type' => 'image/jpeg',
    596             'post_excerpt'   => 'A sample caption',
    597             'post_author'    => $this->editor_id,
     606        wp_set_current_user( self::$editor_id );
     607        $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
     608            'post_mime_type' => 'image/jpeg',
     609            'post_excerpt'   => 'A sample caption',
     610            'post_author'    => self::$editor_id,
    598611        ) );
    599612        $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     
    616629
    617630    public function test_update_item_parent() {
    618         wp_set_current_user( $this->editor_id );
     631        wp_set_current_user( self::$editor_id );
    619632        $original_parent = $this->factory->post->create( array() );
    620633        $attachment_id = $this->factory->attachment->create_object( $this->test_file, $original_parent, array(
     
    637650
    638651    public function test_update_item_invalid_permissions() {
    639         wp_set_current_user( $this->author_id );
    640         $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    641             'post_mime_type' => 'image/jpeg',
    642             'post_excerpt'   => 'A sample caption',
    643             'post_author'    => $this->editor_id,
     652        wp_set_current_user( self::$author_id );
     653        $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
     654            'post_mime_type' => 'image/jpeg',
     655            'post_excerpt'   => 'A sample caption',
     656            'post_author'    => self::$editor_id,
    644657        ) );
    645658        $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     
    651664    public function test_update_item_invalid_post_type() {
    652665        $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) );
    653         wp_set_current_user( $this->editor_id );
    654         $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    655             'post_mime_type' => 'image/jpeg',
    656             'post_excerpt'   => 'A sample caption',
    657             'post_author'    => $this->editor_id,
     666        wp_set_current_user( self::$editor_id );
     667        $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
     668            'post_mime_type' => 'image/jpeg',
     669            'post_excerpt'   => 'A sample caption',
     670            'post_author'    => self::$editor_id,
    658671        ) );
    659672        $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     
    664677
    665678    public function test_delete_item() {
    666         wp_set_current_user( $this->editor_id );
     679        wp_set_current_user( self::$editor_id );
    667680        $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    668681            'post_mime_type' => 'image/jpeg',
     
    676689
    677690    public function test_delete_item_no_trash() {
    678         wp_set_current_user( $this->editor_id );
     691        wp_set_current_user( self::$editor_id );
    679692        $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    680693            'post_mime_type' => 'image/jpeg',
     
    693706
    694707    public function test_delete_item_invalid_delete_permissions() {
    695         wp_set_current_user( $this->author_id );
    696         $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    697             'post_mime_type' => 'image/jpeg',
    698             'post_excerpt'   => 'A sample caption',
    699             'post_author'    => $this->editor_id,
     708        wp_set_current_user( self::$author_id );
     709        $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
     710            'post_mime_type' => 'image/jpeg',
     711            'post_excerpt'   => 'A sample caption',
     712            'post_author'    => self::$editor_id,
    700713        ) );
    701714        $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id );
     
    708721            'post_mime_type' => 'image/jpeg',
    709722            'post_excerpt'   => 'A sample caption',
    710             'post_author'    => $this->editor_id,
     723            'post_author'    => self::$editor_id,
    711724        ) );
    712725
     
    799812        ) );
    800813
    801         wp_set_current_user( $this->editor_id );
    802         $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    803             'post_mime_type' => 'image/jpeg',
    804             'post_excerpt'   => 'A sample caption',
    805             'post_author'    => $this->editor_id,
     814        wp_set_current_user( self::$editor_id );
     815        $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
     816            'post_mime_type' => 'image/jpeg',
     817            'post_excerpt'   => 'A sample caption',
     818            'post_author'    => self::$editor_id,
    806819        ) );
    807820        // Check for error on update.
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r38942 r38975  
    1212 */
    1313class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcase {
    14 
    15     public function setUp() {
    16         parent::setUp();
    17         $this->administrator = $this->factory->user->create( array(
     14    protected static $administrator;
     15    protected static $subscriber;
     16
     17    public static function wpSetUpBeforeClass( $factory ) {
     18        self::$administrator = $factory->user->create( array(
    1819            'role' => 'administrator',
    1920        ) );
    20         $this->subscriber = $this->factory->user->create( array(
     21        self::$subscriber = $factory->user->create( array(
    2122            'role' => 'subscriber',
    2223        ) );
     24    }
     25
     26    public static function wpTearDownAfterClass() {
     27        self::delete_user( self::$administrator );
     28        self::delete_user( self::$subscriber );
    2329    }
    2430
     
    573579
    574580    public function test_create_item() {
    575         wp_set_current_user( $this->administrator );
     581        wp_set_current_user( self::$administrator );
    576582        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    577583        $request->set_param( 'name', 'My Awesome Term' );
     
    589595
    590596    public function test_create_item_invalid_taxonomy() {
    591         wp_set_current_user( $this->administrator );
     597        wp_set_current_user( self::$administrator );
    592598        $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy' );
    593599        $request->set_param( 'name', 'Invalid Taxonomy' );
     
    597603
    598604    public function test_create_item_incorrect_permissions() {
    599         wp_set_current_user( $this->subscriber );
     605        wp_set_current_user( self::$subscriber );
    600606        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    601607        $request->set_param( 'name', 'Incorrect permissions' );
     
    605611
    606612    public function test_create_item_missing_arguments() {
    607         wp_set_current_user( $this->administrator );
     613        wp_set_current_user( self::$administrator );
    608614        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    609615        $response = $this->server->dispatch( $request );
     
    612618
    613619    public function test_create_item_with_parent() {
    614         wp_set_current_user( $this->administrator );
     620        wp_set_current_user( self::$administrator );
    615621        $parent = wp_insert_term( 'test-category', 'category' );
    616622        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
     
    624630
    625631    public function test_create_item_invalid_parent() {
    626         wp_set_current_user( $this->administrator );
     632        wp_set_current_user( self::$administrator );
    627633        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    628634
     
    635641
    636642    public function test_update_item() {
    637         wp_set_current_user( $this->administrator );
     643        wp_set_current_user( self::$administrator );
    638644        $orig_args = array(
    639645            'name'        => 'Original Name',
     
    655661
    656662    public function test_update_item_invalid_taxonomy() {
    657         wp_set_current_user( $this->administrator );
     663        wp_set_current_user( self::$administrator );
    658664        $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    659665        $request->set_param( 'name', 'Invalid Taxonomy' );
     
    663669
    664670    public function test_update_item_invalid_term() {
    665         wp_set_current_user( $this->administrator );
     671        wp_set_current_user( self::$administrator );
    666672        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    667673        $request->set_param( 'name', 'Invalid Term' );
     
    671677
    672678    public function test_update_item_incorrect_permissions() {
    673         wp_set_current_user( $this->subscriber );
     679        wp_set_current_user( self::$subscriber );
    674680        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    675681        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
     
    680686
    681687    public function test_update_item_parent() {
    682         wp_set_current_user( $this->administrator );
     688        wp_set_current_user( self::$administrator );
    683689        $parent = get_term_by( 'id', $this->factory->category->create(), 'category' );
    684690        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
     
    694700
    695701    public function test_update_item_invalid_parent() {
    696         wp_set_current_user( $this->administrator );
     702        wp_set_current_user( self::$administrator );
    697703        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    698704
     
    704710
    705711    public function test_delete_item() {
    706         wp_set_current_user( $this->administrator );
     712        wp_set_current_user( self::$administrator );
    707713        $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
    708714        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
     
    715721
    716722    public function test_delete_item_force_false() {
    717         wp_set_current_user( $this->administrator );
     723        wp_set_current_user( self::$administrator );
    718724        $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
    719725        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
     
    724730
    725731    public function test_delete_item_invalid_taxonomy() {
    726         wp_set_current_user( $this->administrator );
     732        wp_set_current_user( self::$administrator );
    727733        $request = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    728734        $response = $this->server->dispatch( $request );
     
    731737
    732738    public function test_delete_item_invalid_term() {
    733         wp_set_current_user( $this->administrator );
     739        wp_set_current_user( self::$administrator );
    734740        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    735741        $response = $this->server->dispatch( $request );
     
    738744
    739745    public function test_delete_item_incorrect_permissions() {
    740         wp_set_current_user( $this->subscriber );
     746        wp_set_current_user( self::$subscriber );
    741747        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    742748        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r38864 r38975  
    1212class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase {
    1313
    14     protected $admin_id;
    15     protected $subscriber_id;
    16 
    17     protected $post_id;
    18     protected $private_id;
    19     protected $draft_id;
    20     protected $trash_id;
    21 
    22     protected $approved_id;
    23     protected $hold_id;
     14    protected static $admin_id;
     15    protected static $subscriber_id;
     16    protected static $author_id;
     17
     18    protected static $post_id;
     19    protected static $private_id;
     20    protected static $draft_id;
     21    protected static $trash_id;
     22    protected static $approved_id;
     23    protected static $hold_id;
    2424
    2525    protected $endpoint;
    2626
    27     public function setUp() {
    28         parent::setUp();
    29 
    30         $this->admin_id = $this->factory->user->create( array(
     27    public static function wpSetUpBeforeClass( $factory ) {
     28        self::$admin_id = $factory->user->create( array(
    3129            'role' => 'administrator',
    32         ));
    33         $this->subscriber_id = $this->factory->user->create( array(
     30        ) );
     31        self::$subscriber_id = $factory->user->create( array(
    3432            'role' => 'subscriber',
    35         ));
    36         $this->author_id = $this->factory->user->create( array(
     33        ) );
     34        self::$author_id = $factory->user->create( array(
    3735            'role'         => 'author',
    3836            'display_name' => 'Sea Captain',
     
    4139            'user_email'   => 'captain@thefryingdutchman.com',
    4240            'user_url'     => 'http://thefryingdutchman.com',
    43         ));
    44 
    45         $this->post_id = $this->factory->post->create();
    46         $this->private_id = $this->factory->post->create( array(
     41        ) );
     42
     43        self::$post_id = $factory->post->create();
     44        self::$private_id = $factory->post->create( array(
    4745            'post_status' => 'private',
    48         ));
    49         $this->draft_id = $this->factory->post->create( array(
     46        ) );
     47        self::$draft_id = $factory->post->create( array(
    5048            'post_status' => 'draft',
    51         ));
    52         $this->trash_id = $this->factory->post->create( array(
     49        ) );
     50        self::$trash_id = $factory->post->create( array(
    5351            'post_status' => 'trash',
    54         ));
    55 
    56         $this->approved_id = $this->factory->comment->create( array(
    57             'comment_approved' => 1,
    58             'comment_post_ID'  => $this->post_id,
     52        ) );
     53
     54        self::$approved_id = $factory->comment->create( array(
     55            'comment_approved' => 1,
     56            'comment_post_ID'  => self::$post_id,
    5957            'user_id'          => 0,
    60         ));
    61         $this->hold_id = $this->factory->comment->create( array(
     58        ) );
     59        self::$hold_id = $factory->comment->create( array(
    6260            'comment_approved' => 0,
    63             'comment_post_ID'  => $this->post_id,
    64             'user_id'          => $this->subscriber_id,
    65         ));
    66 
     61            'comment_post_ID'  => self::$post_id,
     62            'user_id'          => self::$subscriber_id,
     63        ) );
     64    }
     65
     66    public static function wpTearDownAfterClass() {
     67        self::delete_user( self::$admin_id );
     68        self::delete_user( self::$subscriber_id );
     69        self::delete_user( self::$author_id );
     70
     71        wp_delete_post( self::$post_id, true );
     72        wp_delete_post( self::$private_id, true );
     73        wp_delete_post( self::$draft_id, true );
     74        wp_delete_post( self::$trash_id, true );
     75        wp_delete_post( self::$approved_id, true );
     76        wp_delete_post( self::$hold_id, true );
     77    }
     78
     79    public function setUp() {
     80        parent::setUp();
    6781        $this->endpoint = new WP_REST_Comments_Controller;
    6882    }
     
    89103        $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    90104        // Single
    91         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments/' . $this->approved_id );
     105        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments/' . self::$approved_id );
    92106        $response = $this->server->dispatch( $request );
    93107        $data = $response->get_data();
     
    127141
    128142    public function test_get_items() {
    129         $this->factory->comment->create_post_comments( $this->post_id, 6 );
     143        $this->factory->comment->create_post_comments( self::$post_id, 6 );
    130144
    131145        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     
    135149
    136150        $comments = $response->get_data();
    137         // We created 6 comments in this method, plus $this->approved_id.
     151        // We created 6 comments in this method, plus self::$approved_id.
    138152        $this->assertCount( 7, $comments );
    139153    }
     
    144158        $args = array(
    145159            'comment_approved' => 1,
    146             'comment_post_ID'  => $this->private_id,
     160            'comment_post_ID'  => self::$private_id,
    147161        );
    148162        $private_comment = $this->factory->comment->create( $args );
     
    158172
    159173    public function test_get_items_with_private_post_permission() {
    160         wp_set_current_user( $this->admin_id );
     174        wp_set_current_user( self::$admin_id );
    161175
    162176        $args = array(
    163177            'comment_approved' => 1,
    164             'comment_post_ID'  => $this->private_id,
     178            'comment_post_ID'  => self::$private_id,
    165179        );
    166180        $private_comment = $this->factory->comment->create( $args );
     
    195209
    196210    public function test_get_items_with_invalid_post_permission() {
    197         wp_set_current_user( $this->admin_id );
     211        wp_set_current_user( self::$admin_id );
    198212
    199213        $comment_id = $this->factory->comment->create( array(
     
    223237    public function test_get_items_no_post() {
    224238        $this->factory->comment->create_post_comments( 0, 2 );
    225         wp_set_current_user( $this->admin_id );
     239        wp_set_current_user( self::$admin_id );
    226240        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    227241        $request->set_param( 'post', 0 );
     
    241255
    242256    public function test_get_items_edit_context() {
    243         wp_set_current_user( $this->admin_id );
     257        wp_set_current_user( self::$admin_id );
    244258        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    245259        $request->set_param( 'context', 'edit' );
     
    265279
    266280    public function test_get_items_include_query() {
    267         wp_set_current_user( $this->admin_id );
     281        wp_set_current_user( self::$admin_id );
    268282        $args = array(
    269283            'comment_approved' => 1,
    270             'comment_post_ID'  => $this->post_id,
     284            'comment_post_ID'  => self::$post_id,
    271285        );
    272286        $id1 = $this->factory->comment->create( $args );
     
    290304
    291305    public function test_get_items_exclude_query() {
    292         wp_set_current_user( $this->admin_id );
     306        wp_set_current_user( self::$admin_id );
    293307        $args = array(
    294308            'comment_approved' => 1,
    295             'comment_post_ID'  => $this->post_id,
     309            'comment_post_ID'  => self::$post_id,
    296310        );
    297311        $id1 = $this->factory->comment->create( $args );
     
    310324
    311325    public function test_get_items_offset_query() {
    312         wp_set_current_user( $this->admin_id );
     326        wp_set_current_user( self::$admin_id );
    313327        $args = array(
    314328            'comment_approved' => 1,
    315             'comment_post_ID'  => $this->post_id,
     329            'comment_post_ID'  => self::$post_id,
    316330        );
    317331        $this->factory->comment->create( $args );
     
    333347
    334348    public function test_get_items_order_query() {
    335         wp_set_current_user( $this->admin_id );
     349        wp_set_current_user( self::$admin_id );
    336350        $args = array(
    337351            'comment_approved' => 1,
    338             'comment_post_ID'  => $this->post_id,
     352            'comment_post_ID'  => self::$post_id,
    339353        );
    340354        $this->factory->comment->create( $args );
     
    350364        $response = $this->server->dispatch( $request );
    351365        $data = $response->get_data();
    352         $this->assertEquals( $this->approved_id, $data[0]['id'] );
     366        $this->assertEquals( self::$approved_id, $data[0]['id'] );
    353367    }
    354368
     
    364378    public function test_get_items_author_arg() {
    365379        // Authorized
    366         wp_set_current_user( $this->admin_id );
     380        wp_set_current_user( self::$admin_id );
    367381        $args = array(
    368382            'comment_approved' => 1,
    369             'comment_post_ID'  => $this->post_id,
    370             'user_id'          => $this->author_id,
     383            'comment_post_ID'  => self::$post_id,
     384            'user_id'          => self::$author_id,
    371385        );
    372386        $this->factory->comment->create( $args );
    373         $args['user_id'] = $this->subscriber_id;
     387        $args['user_id'] = self::$subscriber_id;
    374388        $this->factory->comment->create( $args );
    375389        unset( $args['user_id'] );
     
    378392        // 'author' limits result to 1 of 3
    379393        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    380         $request->set_param( 'author', $this->author_id );
     394        $request->set_param( 'author', self::$author_id );
    381395        $response = $this->server->dispatch( $request );
    382396        $this->assertEquals( 200, $response->get_status() );
     
    384398        $this->assertCount( 1, $comments );
    385399        // Multiple authors are supported
    386         $request->set_param( 'author', array( $this->author_id, $this->subscriber_id ) );
     400        $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
    387401        $response = $this->server->dispatch( $request );
    388402        $this->assertEquals( 200, $response->get_status() );
     
    397411    public function test_get_items_author_exclude_arg() {
    398412        // Authorized
    399         wp_set_current_user( $this->admin_id );
     413        wp_set_current_user( self::$admin_id );
    400414        $args = array(
    401415            'comment_approved' => 1,
    402             'comment_post_ID'  => $this->post_id,
    403             'user_id'          => $this->author_id,
     416            'comment_post_ID'  => self::$post_id,
     417            'user_id'          => self::$author_id,
    404418        );
    405419        $this->factory->comment->create( $args );
    406         $args['user_id'] = $this->subscriber_id;
     420        $args['user_id'] = self::$subscriber_id;
    407421        $this->factory->comment->create( $args );
    408422        unset( $args['user_id'] );
     
    416430        // 'author_exclude' limits result to 3 of 4
    417431        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    418         $request->set_param( 'author_exclude', $this->author_id );
     432        $request->set_param( 'author_exclude', self::$author_id );
    419433        $response = $this->server->dispatch( $request );
    420434        $this->assertEquals( 200, $response->get_status() );
     
    423437        // 'author_exclude' for both comment authors (2 of 4)
    424438        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    425         $request->set_param( 'author_exclude', array( $this->author_id, $this->subscriber_id ) );
     439        $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
    426440        $response = $this->server->dispatch( $request );
    427441        $this->assertEquals( 200, $response->get_status() );
     
    437451        $args = array(
    438452            'comment_approved'  => 1,
    439             'comment_post_ID'   => $this->post_id,
     453            'comment_post_ID'   => self::$post_id,
    440454        );
    441455        $parent_id = $this->factory->comment->create( $args );
     
    462476        $args = array(
    463477            'comment_approved'  => 1,
    464             'comment_post_ID'   => $this->post_id,
     478            'comment_post_ID'   => self::$post_id,
    465479        );
    466480        $parent_id = $this->factory->comment->create( $args );
     
    485499
    486500    public function test_get_items_search_query() {
    487         wp_set_current_user( $this->admin_id );
     501        wp_set_current_user( self::$admin_id );
    488502        $args = array(
    489503            'comment_approved' => 1,
    490             'comment_post_ID'  => $this->post_id,
     504            'comment_post_ID'  => self::$post_id,
    491505            'comment_content'  => 'foo',
    492506            'comment_author'   => 'Homer J Simpson',
     
    510524
    511525    public function test_get_comments_pagination_headers() {
    512         wp_set_current_user( $this->admin_id );
     526        wp_set_current_user( self::$admin_id );
    513527        // Start of the index
    514528        for ( $i = 0; $i < 49; $i++ ) {
    515529            $this->factory->comment->create( array(
    516530                'comment_content'   => "Comment {$i}",
    517                 'comment_post_ID'   => $this->post_id,
     531                'comment_post_ID'   => self::$post_id,
    518532                ) );
    519533        }
     
    531545        $this->factory->comment->create( array(
    532546                'comment_content'   => 'Comment 51',
    533                 'comment_post_ID'   => $this->post_id,
     547                'comment_post_ID'   => self::$post_id,
    534548                ) );
    535549        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     
    584598        $comment1 = $this->factory->comment->create( array(
    585599            'comment_date'    => '2016-01-15T00:00:00Z',
    586             'comment_post_ID' => $this->post_id,
     600            'comment_post_ID' => self::$post_id,
    587601        ) );
    588602        $comment2 = $this->factory->comment->create( array(
    589603            'comment_date'    => '2016-01-16T00:00:00Z',
    590             'comment_post_ID' => $this->post_id,
     604            'comment_post_ID' => self::$post_id,
    591605        ) );
    592606        $comment3 = $this->factory->comment->create( array(
    593607            'comment_date'    => '2016-01-17T00:00:00Z',
    594             'comment_post_ID' => $this->post_id,
     608            'comment_post_ID' => self::$post_id,
    595609        ) );
    596610
     
    605619
    606620    public function test_get_item() {
    607         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     621        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    608622
    609623        $response = $this->server->dispatch( $request );
     
    615629
    616630    public function test_prepare_item() {
    617         wp_set_current_user( $this->admin_id );
    618         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     631        wp_set_current_user( self::$admin_id );
     632        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    619633        $request->set_query_params( array(
    620634            'context' => 'edit',
     
    629643
    630644    public function test_get_comment_author_avatar_urls() {
    631         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     645        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    632646
    633647        $response = $this->server->dispatch( $request );
     
    638652        $this->assertArrayHasKey( 96,  $data['author_avatar_urls'] );
    639653
    640         $comment = get_comment( $this->approved_id );
     654        $comment = get_comment( self::$approved_id );
    641655        /**
    642656         * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar
     
    655669    public function test_get_comment_invalid_context() {
    656670        wp_set_current_user( 0 );
    657         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $this->approved_id ) );
     671        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) );
    658672        $request->set_param( 'context', 'edit' );
    659673        $response = $this->server->dispatch( $request );
     
    674688
    675689    public function test_get_comment_invalid_post_id_as_admin() {
    676         wp_set_current_user( $this->admin_id );
     690        wp_set_current_user( self::$admin_id );
    677691        $comment_id = $this->factory->comment->create( array(
    678692            'comment_approved' => 1,
     
    688702        wp_set_current_user( 0 );
    689703
    690         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
     704        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    691705
    692706        $response = $this->server->dispatch( $request );
     
    695709
    696710    public function test_get_comment_not_approved_same_user() {
    697         wp_set_current_user( $this->subscriber_id );
    698 
    699         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
     711        wp_set_current_user( self::$subscriber_id );
     712
     713        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    700714
    701715        $response = $this->server->dispatch( $request );
     
    706720        $comment_id_1 = $this->factory->comment->create( array(
    707721            'comment_approved' => 1,
    708             'comment_post_ID'  => $this->post_id,
    709             'user_id'          => $this->subscriber_id,
     722            'comment_post_ID'  => self::$post_id,
     723            'user_id'          => self::$subscriber_id,
    710724        ) );
    711725
     
    713727            'comment_approved' => 1,
    714728            'comment_parent'   => $comment_id_1,
    715             'comment_post_ID'  => $this->post_id,
    716             'user_id'          => $this->subscriber_id,
     729            'comment_post_ID'  => self::$post_id,
     730            'user_id'          => self::$subscriber_id,
    717731        ) );
    718732
     
    726740        $comment_id_1 = $this->factory->comment->create( array(
    727741            'comment_approved' => 1,
    728             'comment_post_ID'  => $this->post_id,
    729             'user_id'          => $this->subscriber_id,
     742            'comment_post_ID'  => self::$post_id,
     743            'user_id'          => self::$subscriber_id,
    730744        ) );
    731745
     
    740754
    741755        $params = array(
    742             'post'    => $this->post_id,
     756            'post'    => self::$post_id,
    743757            'author_name'  => 'Comic Book Guy',
    744758            'author_email' => 'cbg@androidsdungeon.com',
     
    759773        $this->assertEquals( 'hold', $data['status'] );
    760774        $this->assertEquals( '2014-11-07T10:14:25', $data['date'] );
    761         $this->assertEquals( $this->post_id, $data['post'] );
     775        $this->assertEquals( self::$post_id, $data['post'] );
    762776    }
    763777
     
    766780
    767781        $params = array(
    768             'post'         => $this->post_id,
     782            'post'         => self::$post_id,
    769783            'author_name'  => 'Reverend Lovejoy',
    770784            'author_email' => 'lovejoy@example.com',
     
    791805
    792806        $params = array(
    793             'post'    => $this->post_id,
     807            'post'    => self::$post_id,
    794808            'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
    795809        );
     
    809823
    810824        $params = array(
    811             'post'         => $this->post_id,
     825            'post'         => self::$post_id,
    812826            'author_email' => 'ekrabappel@springfield-elementary.edu',
    813827            'content'      => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     
    828842
    829843        $params = array(
    830             'post'        => $this->post_id,
     844            'post'        => self::$post_id,
    831845            'author_name' => 'Edna Krabappel',
    832846            'content'     => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
     
    847861
    848862        $params = array(
    849             'post'         => $this->post_id,
     863            'post'         => self::$post_id,
    850864            'author_name'  => 'Reverend Lovejoy',
    851865            'author_email' => 'lovejoy@example.com',
     
    866880
    867881        $params = array(
    868             'post'         => $this->post_id,
     882            'post'         => self::$post_id,
    869883            'author_name'  => 'Reverend Lovejoy',
    870884            'author_email' => 'lovejoy@example.com',
     
    889903        ));
    890904
    891         wp_set_current_user( $this->admin_id );
    892         $params = array(
    893             'post'    => $this->post_id,
     905        wp_set_current_user( self::$admin_id );
     906        $params = array(
     907            'post'    => self::$post_id,
    894908            'author_name'  => 'Comic Book Guy',
    895909            'author_email' => 'cbg@androidsdungeon.com',
     
    912926    public function test_create_comment_without_type() {
    913927        $post_id = $this->factory->post->create();
    914         wp_set_current_user( $this->admin_id );
     928        wp_set_current_user( self::$admin_id );
    915929
    916930        $params = array(
    917931            'post'    => $post_id,
    918             'author'       => $this->admin_id,
     932            'author'       => self::$admin_id,
    919933            'author_name'  => 'Comic Book Guy',
    920934            'author_email' => 'cbg@androidsdungeon.com',
     
    957971
    958972        $params = array(
    959             'post' => $this->post_id,
     973            'post' => self::$post_id,
    960974            'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!",
    961975        );
     
    979993
    980994    public function test_create_comment_other_user() {
    981         wp_set_current_user( $this->admin_id );
    982 
    983         $params = array(
    984             'post'    => $this->post_id,
     995        wp_set_current_user( self::$admin_id );
     996
     997        $params = array(
     998            'post'    => self::$post_id,
    985999            'author_name'  => 'Homer Jay Simpson',
    9861000            'author_email' => 'chunkylover53@aol.com',
    9871001            'author_url'   => 'http://compuglobalhypermeganet.com',
    9881002            'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
    989             'author'    => $this->subscriber_id,
     1003            'author'    => self::$subscriber_id,
    9901004        );
    9911005
     
    9971011        $this->assertEquals( 201, $response->get_status() );
    9981012        $data = $response->get_data();
    999         $this->assertEquals( $this->subscriber_id, $data['author'] );
     1013        $this->assertEquals( self::$subscriber_id, $data['author'] );
    10001014        $this->assertEquals( 'Homer Jay Simpson', $data['author_name'] );
    10011015        $this->assertEquals( 'chunkylover53@aol.com', $data['author_email'] );
     
    10041018
    10051019    public function test_create_comment_other_user_without_permission() {
    1006         wp_set_current_user( $this->subscriber_id );
    1007 
    1008         $params = array(
    1009             'post'         => $this->post_id,
     1020        wp_set_current_user( self::$subscriber_id );
     1021
     1022        $params = array(
     1023            'post'         => self::$post_id,
    10101024            'author_name'  => 'Homer Jay Simpson',
    10111025            'author_email' => 'chunkylover53@aol.com',
    10121026            'author_url'   => 'http://compuglobalhypermeganet.com',
    10131027            'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
    1014             'author'       => $this->admin_id,
     1028            'author'       => self::$admin_id,
    10151029        );
    10161030
     
    10241038
    10251039    public function test_create_comment_karma_without_permission() {
    1026         wp_set_current_user( $this->subscriber_id );
    1027 
    1028         $params = array(
    1029             'post'         => $this->post_id,
     1040        wp_set_current_user( self::$subscriber_id );
     1041
     1042        $params = array(
     1043            'post'         => self::$post_id,
    10301044            'author_name'  => 'Homer Jay Simpson',
    10311045            'author_email' => 'chunkylover53@aol.com',
    10321046            'author_url'   => 'http://compuglobalhypermeganet.com',
    10331047            'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
    1034             'author'       => $this->subscriber_id,
     1048            'author'       => self::$subscriber_id,
    10351049            'karma'        => 100,
    10361050        );
     
    10451059
    10461060    public function test_create_comment_status_without_permission() {
    1047         wp_set_current_user( $this->subscriber_id );
    1048 
    1049         $params = array(
    1050             'post'         => $this->post_id,
     1061        wp_set_current_user( self::$subscriber_id );
     1062
     1063        $params = array(
     1064            'post'         => self::$post_id,
    10511065            'author_name'  => 'Homer Jay Simpson',
    10521066            'author_email' => 'chunkylover53@aol.com',
    10531067            'author_url'   => 'http://compuglobalhypermeganet.com',
    10541068            'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
    1055             'author'       => $this->subscriber_id,
     1069            'author'       => self::$subscriber_id,
    10561070            'status'        => 'approved',
    10571071        );
     
    10671081    public function test_create_comment_with_status_IP_and_user_agent() {
    10681082        $post_id = $this->factory->post->create();
    1069         wp_set_current_user( $this->admin_id );
     1083        wp_set_current_user( self::$admin_id );
    10701084
    10711085        $params = array(
     
    10941108
    10951109    public function test_create_comment_invalid_author_IP() {
    1096         wp_set_current_user( $this->admin_id );
     1110        wp_set_current_user( self::$admin_id );
    10971111
    10981112        $params = array(
     
    11141128
    11151129    public function test_create_comment_no_post_id() {
    1116         wp_set_current_user( $this->admin_id );
     1130        wp_set_current_user( self::$admin_id );
    11171131
    11181132        $params = array(
     
    11321146
    11331147    public function test_create_comment_no_post_id_no_permission() {
    1134         wp_set_current_user( $this->subscriber_id );
     1148        wp_set_current_user( self::$subscriber_id );
    11351149
    11361150        $params = array(
     
    11391153            'author_url'   => 'http://compuglobalhypermeganet.com',
    11401154            'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
    1141             'author'       => $this->subscriber_id,
     1155            'author'       => self::$subscriber_id,
    11421156        );
    11431157        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     
    11511165
    11521166    public function test_create_comment_draft_post() {
    1153         wp_set_current_user( $this->subscriber_id );
    1154 
    1155         $params = array(
    1156             'post'         => $this->draft_id,
     1167        wp_set_current_user( self::$subscriber_id );
     1168
     1169        $params = array(
     1170            'post'         => self::$draft_id,
    11571171            'author_name'  => 'Ishmael',
    11581172            'author_email' => 'herman-melville@earthlink.net',
    11591173            'author_url'   => 'https://en.wikipedia.org/wiki/Herman_Melville',
    11601174            'content'      => 'Call me Ishmael.',
    1161             'author'       => $this->subscriber_id,
     1175            'author'       => self::$subscriber_id,
    11621176        );
    11631177        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     
    11711185
    11721186    public function test_create_comment_trash_post() {
    1173         wp_set_current_user( $this->subscriber_id );
    1174 
    1175         $params = array(
    1176             'post'         => $this->trash_id,
     1187        wp_set_current_user( self::$subscriber_id );
     1188
     1189        $params = array(
     1190            'post'         => self::$trash_id,
    11771191            'author_name'  => 'Ishmael',
    11781192            'author_email' => 'herman-melville@earthlink.net',
    11791193            'author_url'   => 'https://en.wikipedia.org/wiki/Herman_Melville',
    11801194            'content'      => 'Call me Ishmael.',
    1181             'author'       => $this->subscriber_id,
     1195            'author'       => self::$subscriber_id,
    11821196        );
    11831197        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     
    11911205
    11921206    public function test_create_comment_private_post_invalid_permission() {
    1193         wp_set_current_user( $this->subscriber_id );
    1194 
    1195         $params = array(
    1196             'post'         => $this->private_id,
     1207        wp_set_current_user( self::$subscriber_id );
     1208
     1209        $params = array(
     1210            'post'         => self::$private_id,
    11971211            'author_name'  => 'Homer Jay Simpson',
    11981212            'author_email' => 'chunkylover53@aol.com',
    11991213            'author_url'   => 'http://compuglobalhypermeganet.com',
    12001214            'content'      => 'I\’d be a vegetarian if bacon grew on trees.',
    1201             'author'       => $this->subscriber_id,
     1215            'author'       => self::$subscriber_id,
    12021216        );
    12031217        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     
    12131227        $this->factory->comment->create(
    12141228            array(
    1215                 'comment_post_ID'      => $this->post_id,
     1229                'comment_post_ID'      => self::$post_id,
    12161230                'comment_author'       => 'Guy N. Cognito',
    12171231                'comment_author_email' => 'chunkylover53@aol.co.uk',
     
    12221236
    12231237        $params = array(
    1224             'post'    => $this->post_id,
     1238            'post'    => self::$post_id,
    12251239            'author_name'  => 'Guy N. Cognito',
    12261240            'author_email' => 'chunkylover53@aol.co.uk',
     
    12581272        update_option( 'comment_registration', 1 );
    12591273        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    1260         $request->set_param( 'post', $this->post_id );
     1274        $request->set_param( 'post', self::$post_id );
    12611275        $response = $this->server->dispatch( $request );
    12621276        $this->assertEquals( 401, $response->get_status() );
     
    12661280
    12671281    public function test_create_item_invalid_author() {
    1268         wp_set_current_user( $this->admin_id );
    1269 
    1270         $params = array(
    1271             'post'         => $this->post_id,
     1282        wp_set_current_user( self::$admin_id );
     1283
     1284        $params = array(
     1285            'post'         => self::$post_id,
    12721286            'author'       => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
    12731287            'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
     
    12831297
    12841298    public function test_create_item_pull_author_info() {
    1285         wp_set_current_user( $this->admin_id );
    1286 
    1287         $author = new WP_User( $this->author_id );
    1288         $params = array(
    1289             'post'         => $this->post_id,
    1290             'author'       => $this->author_id,
     1299        wp_set_current_user( self::$admin_id );
     1300
     1301        $author = new WP_User( self::$author_id );
     1302        $params = array(
     1303            'post'         => self::$post_id,
     1304            'author'       => self::$author_id,
    12911305            'content'      => 'It\'s all over\, people! We don\'t have a prayer!',
    12921306        );
     
    12991313
    13001314        $result = $response->get_data();
    1301         $this->assertSame( $this->author_id, $result['author'] );
     1315        $this->assertSame( self::$author_id, $result['author'] );
    13021316        $this->assertSame( 'Sea Captain', $result['author_name'] );
    13031317        $this->assertSame( 'captain@thefryingdutchman.com', $result['author_email'] );
     
    13091323
    13101324        $params = array(
    1311             'post'    => $this->post_id,
     1325            'post'    => self::$post_id,
    13121326            'author_name'  => 'Comic Book Guy',
    13131327            'author_email' => 'cbg@androidsdungeon.com',
     
    13241338
    13251339        $params = array(
    1326             'post'    => $this->post_id,
     1340            'post'    => self::$post_id,
    13271341            'author_name'  => 'Comic Book Guy',
    13281342            'author_email' => 'cbg@androidsdungeon.com',
     
    13421356        $post_id = $this->factory->post->create();
    13431357
    1344         wp_set_current_user( $this->admin_id );
    1345 
    1346         $params = array(
    1347             'author'       => $this->subscriber_id,
     1358        wp_set_current_user( self::$admin_id );
     1359
     1360        $params = array(
     1361            'author'       => self::$subscriber_id,
    13481362            'author_name'  => 'Disco Stu',
    13491363            'author_url'   => 'http://stusdisco.com',
     
    13551369            'post'         => $post_id,
    13561370        );
    1357         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1371        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    13581372        $request->add_header( 'content-type', 'application/json' );
    13591373        $request->set_body( wp_json_encode( $params ) );
     
    13631377
    13641378        $comment = $response->get_data();
    1365         $updated = get_comment( $this->approved_id );
     1379        $updated = get_comment( self::$approved_id );
    13661380        $this->assertEquals( $params['content'], $comment['content']['raw'] );
    13671381        $this->assertEquals( $params['author'], $comment['author'] );
     
    13781392
    13791393    public function test_update_comment_status() {
    1380         wp_set_current_user( $this->admin_id );
     1394        wp_set_current_user( self::$admin_id );
    13811395
    13821396        $comment_id = $this->factory->comment->create( array(
    13831397            'comment_approved' => 0,
    1384             'comment_post_ID'  => $this->post_id,
     1398            'comment_post_ID'  => self::$post_id,
    13851399        ));
    13861400
     
    14021416
    14031417    public function test_update_comment_field_does_not_use_default_values() {
    1404         wp_set_current_user( $this->admin_id );
     1418        wp_set_current_user( self::$admin_id );
    14051419
    14061420        $comment_id = $this->factory->comment->create( array(
    14071421            'comment_approved' => 0,
    1408             'comment_post_ID'  => $this->post_id,
     1422            'comment_post_ID'  => self::$post_id,
    14091423            'comment_content'  => 'some content',
    14101424        ));
     
    14281442
    14291443    public function test_update_comment_date_gmt() {
    1430         wp_set_current_user( $this->admin_id );
     1444        wp_set_current_user( self::$admin_id );
    14311445
    14321446        $params = array(
     
    14341448            'content'  => 'I\'ll be deep in the cold, cold ground before I recognize Missouri.',
    14351449        );
    1436         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1450        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    14371451        $request->add_header( 'content-type', 'application/json' );
    14381452        $request->set_body( wp_json_encode( $params ) );
     
    14421456
    14431457        $comment = $response->get_data();
    1444         $updated = get_comment( $this->approved_id );
     1458        $updated = get_comment( self::$approved_id );
    14451459        $this->assertEquals( $params['date_gmt'], $comment['date_gmt'] );
    14461460        $this->assertEquals( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) );
     
    14481462
    14491463    public function test_update_comment_invalid_type() {
    1450         wp_set_current_user( $this->admin_id );
     1464        wp_set_current_user( self::$admin_id );
    14511465
    14521466        $params = array(
    14531467            'type' => 'trackback',
    14541468        );
    1455         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1469        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    14561470        $request->add_header( 'content-type', 'application/json' );
    14571471        $request->set_body( wp_json_encode( $params ) );
     
    14621476
    14631477    public function test_update_comment_with_raw_property() {
    1464         wp_set_current_user( $this->admin_id );
     1478        wp_set_current_user( self::$admin_id );
    14651479
    14661480        $params = array(
     
    14691483            ),
    14701484        );
    1471         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1485        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    14721486        $request->add_header( 'content-type', 'application/json' );
    14731487        $request->set_body( wp_json_encode( $params ) );
     
    14781492
    14791493        $comment = $response->get_data();
    1480         $updated = get_comment( $this->approved_id );
     1494        $updated = get_comment( self::$approved_id );
    14811495        $this->assertEquals( $params['content']['raw'], $updated->comment_content );
    14821496    }
    14831497
    14841498    public function test_update_item_invalid_date() {
    1485         wp_set_current_user( $this->admin_id );
     1499        wp_set_current_user( self::$admin_id );
    14861500
    14871501        $params = array(
     
    14901504        );
    14911505
    1492         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1506        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    14931507        $request->add_header( 'content-type', 'application/json' );
    14941508        $request->set_body( wp_json_encode( $params ) );
     
    14991513
    15001514    public function test_update_item_invalid_date_gmt() {
    1501         wp_set_current_user( $this->admin_id );
     1515        wp_set_current_user( self::$admin_id );
    15021516
    15031517        $params = array(
     
    15061520        );
    15071521
    1508         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1522        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    15091523        $request->add_header( 'content-type', 'application/json' );
    15101524        $request->set_body( wp_json_encode( $params ) );
     
    15341548            'content' => 'Disco Stu likes disco music.',
    15351549        );
    1536         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $this->hold_id ) );
     1550        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    15371551        $request->add_header( 'content-type', 'application/json' );
    15381552        $request->set_body( wp_json_encode( $params ) );
     
    15451559        $private_comment_id = $this->factory->comment->create( array(
    15461560            'comment_approved' => 1,
    1547             'comment_post_ID'  => $this->private_id,
     1561            'comment_post_ID'  => self::$private_id,
    15481562            'user_id'          => 0,
    15491563        ));
    15501564
    1551         wp_set_current_user( $this->subscriber_id );
     1565        wp_set_current_user( self::$subscriber_id );
    15521566
    15531567        $params = array(
     
    15631577
    15641578    public function test_update_comment_with_children_link() {
    1565         wp_set_current_user( $this->admin_id );
     1579        wp_set_current_user( self::$admin_id );
    15661580        $comment_id_1 = $this->factory->comment->create( array(
    15671581            'comment_approved' => 1,
    1568             'comment_post_ID'  => $this->post_id,
    1569             'user_id'          => $this->subscriber_id,
     1582            'comment_post_ID'  => self::$post_id,
     1583            'user_id'          => self::$subscriber_id,
    15701584        ) );
    15711585
    15721586        $child_comment = $this->factory->comment->create( array(
    15731587            'comment_approved' => 1,
    1574             'comment_post_ID'  => $this->post_id,
    1575             'user_id'          => $this->subscriber_id,
     1588            'comment_post_ID'  => self::$post_id,
     1589            'user_id'          => self::$subscriber_id,
    15761590        ) );
    15771591
     
    15971611
    15981612    public function test_delete_item() {
    1599         wp_set_current_user( $this->admin_id );
     1613        wp_set_current_user( self::$admin_id );
    16001614
    16011615        $comment_id = $this->factory->comment->create( array(
    16021616            'comment_approved' => 1,
    1603             'comment_post_ID'  => $this->post_id,
    1604             'user_id'          => $this->subscriber_id,
     1617            'comment_post_ID'  => self::$post_id,
     1618            'user_id'          => self::$subscriber_id,
    16051619        ));
    16061620        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
     
    16091623        $this->assertEquals( 200, $response->get_status() );
    16101624        $data = $response->get_data();
    1611         $this->assertEquals( $this->post_id, $data['post'] );
     1625        $this->assertEquals( self::$post_id, $data['post'] );
    16121626    }
    16131627
    16141628    public function test_delete_item_skip_trash() {
    1615         wp_set_current_user( $this->admin_id );
     1629        wp_set_current_user( self::$admin_id );
    16161630
    16171631        $comment_id = $this->factory->comment->create( array(
    16181632            'comment_approved' => 1,
    1619             'comment_post_ID'  => $this->post_id,
    1620             'user_id'          => $this->subscriber_id,
     1633            'comment_post_ID'  => self::$post_id,
     1634            'user_id'          => self::$subscriber_id,
    16211635        ));
    16221636        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
     
    16261640        $this->assertEquals( 200, $response->get_status() );
    16271641        $data = $response->get_data();
    1628         $this->assertEquals( $this->post_id, $data['post'] );
     1642        $this->assertEquals( self::$post_id, $data['post'] );
    16291643    }
    16301644
    16311645    public function test_delete_item_already_trashed() {
    1632         wp_set_current_user( $this->admin_id );
     1646        wp_set_current_user( self::$admin_id );
    16331647
    16341648        $comment_id = $this->factory->comment->create( array(
    16351649            'comment_approved' => 1,
    1636             'comment_post_ID'  => $this->post_id,
    1637             'user_id'          => $this->subscriber_id,
     1650            'comment_post_ID'  => self::$post_id,
     1651            'user_id'          => self::$subscriber_id,
    16381652        ));
    16391653        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
     
    16461660
    16471661    public function test_delete_comment_invalid_id() {
    1648         wp_set_current_user( $this->admin_id );
     1662        wp_set_current_user( self::$admin_id );
    16491663
    16501664        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
     
    16551669
    16561670    public function test_delete_comment_without_permission() {
    1657         wp_set_current_user( $this->subscriber_id );
    1658 
    1659         $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1671        wp_set_current_user( self::$subscriber_id );
     1672
     1673        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    16601674
    16611675        $response = $this->server->dispatch( $request );
     
    16641678
    16651679    public function test_delete_child_comment_link() {
    1666         wp_set_current_user( $this->admin_id );
     1680        wp_set_current_user( self::$admin_id );
    16671681        $comment_id_1 = $this->factory->comment->create( array(
    16681682            'comment_approved' => 1,
    1669             'comment_post_ID'  => $this->post_id,
    1670             'user_id'          => $this->subscriber_id,
     1683            'comment_post_ID'  => self::$post_id,
     1684            'user_id'          => self::$subscriber_id,
    16711685        ) );
    16721686
     
    16741688            'comment_approved' => 1,
    16751689            'comment_parent'   => $comment_id_1,
    1676             'comment_post_ID'  => $this->post_id,
    1677             'user_id'          => $this->subscriber_id,
     1690            'comment_post_ID'  => self::$post_id,
     1691            'user_id'          => self::$subscriber_id,
    16781692        ) );
    16791693
     
    17481762        $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
    17491763
    1750         $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $this->approved_id );
     1764        $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id );
    17511765
    17521766        $response = $this->server->dispatch( $request );
    17531767        $this->assertArrayHasKey( 'my_custom_int', $response->data );
    17541768
    1755         $request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . $this->approved_id );
     1769        $request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . self::$approved_id );
    17561770        $request->set_body_params(array(
    17571771            'my_custom_int' => 123,
     
    17611775        wp_set_current_user( 1 );
    17621776        $this->server->dispatch( $request );
    1763         $this->assertEquals( 123, get_comment_meta( $this->approved_id, 'my_custom_int', true ) );
     1777        $this->assertEquals( 123, get_comment_meta( self::$approved_id, 'my_custom_int', true ) );
    17641778
    17651779        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     
    17681782            'title'         => 'hello',
    17691783            'content'       => 'goodbye',
    1770             'post'          => $this->post_id,
     1784            'post'          => self::$post_id,
    17711785        ));
    17721786
     
    17931807        ) );
    17941808
    1795         wp_set_current_user( $this->admin_id );
     1809        wp_set_current_user( self::$admin_id );
    17961810
    17971811        // Check for error on update.
    1798         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
     1812        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    17991813        $request->set_body_params(array(
    18001814            'my_custom_int' => 'returnError',
  • trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r38968 r38975  
    1212  */
    1313class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
     14    protected static $editor_id;
     15
     16    public static function wpSetUpBeforeClass( $factory ) {
     17        self::$editor_id = $factory->user->create( array(
     18            'role' => 'editor',
     19        ) );
     20    }
     21
     22    public static function wpTearDownAfterClass() {
     23        self::delete_user( self::$editor_id );
     24    }
    1425
    1526    public function setUp() {
    1627        parent::setUp();
    17 
    18         $this->editor_id = $this->factory->user->create( array(
    19             'role' => 'editor',
    20         ) );
    21         $this->author_id = $this->factory->user->create( array(
    22             'role' => 'author',
    23         ) );
    24 
    2528        $this->has_setup_template = false;
    2629        add_filter( 'theme_page_templates', array( $this, 'filter_theme_page_templates' ) );
     
    184187
    185188        // But they are accessible to authorized users
    186         wp_set_current_user( $this->editor_id );
     189        wp_set_current_user( self::$editor_id );
    187190        $response = $this->server->dispatch( $request );
    188191        $data = $response->get_data();
     
    228231
    229232    public function test_create_item_with_template() {
    230         wp_set_current_user( $this->editor_id );
     233        wp_set_current_user( self::$editor_id );
    231234
    232235        $request = new WP_REST_Request( 'POST', '/wp/v2/pages' );
     
    247250            'type' => 'page',
    248251        ) );
    249         wp_set_current_user( $this->editor_id );
     252        wp_set_current_user( self::$editor_id );
    250253
    251254        $request = new WP_REST_Request( 'POST', '/wp/v2/pages' );
     
    268271
    269272    public function test_create_page_with_invalid_parent() {
    270         wp_set_current_user( $this->editor_id );
     273        wp_set_current_user( self::$editor_id );
    271274
    272275        $request = new WP_REST_Request( 'POST', '/wp/v2/pages' );
     
    323326        ) );
    324327
    325         wp_set_current_user( $this->editor_id );
     328        wp_set_current_user( self::$editor_id );
    326329
    327330        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $page_id ) );
     
    343346        ) );
    344347
    345         wp_set_current_user( $this->editor_id );
     348        wp_set_current_user( self::$editor_id );
    346349
    347350        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $page_id ) );
  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r38832 r38975  
    1111  */
    1212class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
     13    protected static $post_id;
     14
     15    public static function wpSetUpBeforeClass( $factory ) {
     16        self::$post_id = $factory->post->create();
     17    }
     18
     19    public static function wpTearDownAfterClass() {
     20        wp_delete_post( self::$post_id, true );
     21    }
     22
    1323    public function setUp() {
    1424        parent::setUp();
     
    5565        $this->server = $wp_rest_server = new Spy_REST_Server;
    5666        do_action( 'rest_api_init' );
    57 
    58         $this->post_id = $this->factory->post->create();
    5967    }
    6068
     
    6876
    6977    public function test_get_value() {
    70         add_post_meta( $this->post_id, 'test_single', 'testvalue' );
    71 
    72         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     78        add_post_meta( self::$post_id, 'test_single', 'testvalue' );
     79
     80        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    7381        $response = $this->server->dispatch( $request );
    7482
     
    8795     */
    8896    public function test_get_multi_value() {
    89         add_post_meta( $this->post_id, 'test_multi', 'value1' );
    90         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     97        add_post_meta( self::$post_id, 'test_multi', 'value1' );
     98        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    9199
    92100        $response = $this->server->dispatch( $request );
     
    100108
    101109        // Check after an update.
    102         add_post_meta( $this->post_id, 'test_multi', 'value2' );
     110        add_post_meta( self::$post_id, 'test_multi', 'value2' );
    103111
    104112        $response = $this->server->dispatch( $request );
     
    114122     */
    115123    public function test_get_unregistered() {
    116         add_post_meta( $this->post_id, 'test_unregistered', 'value1' );
    117         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     124        add_post_meta( self::$post_id, 'test_unregistered', 'value1' );
     125        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    118126
    119127        $response = $this->server->dispatch( $request );
     
    129137     */
    130138    public function test_get_registered_no_api_access() {
    131         add_post_meta( $this->post_id, 'test_no_rest', 'for_the_wicked' );
    132         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     139        add_post_meta( self::$post_id, 'test_no_rest', 'for_the_wicked' );
     140        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    133141
    134142        $response = $this->server->dispatch( $request );
     
    144152     */
    145153    public function test_get_registered_api_disabled() {
    146         add_post_meta( $this->post_id, 'test_rest_disabled', 'sleepless_nights' );
    147         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     154        add_post_meta( self::$post_id, 'test_rest_disabled', 'sleepless_nights' );
     155        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    148156
    149157        $response = $this->server->dispatch( $request );
     
    177185        do_action( 'rest_api_init' );
    178186
    179         add_post_meta( $this->post_id, 'test_string', 42 );
    180         add_post_meta( $this->post_id, 'test_number', '42' );
    181         add_post_meta( $this->post_id, 'test_bool', 1 );
    182 
    183         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     187        add_post_meta( self::$post_id, 'test_string', 42 );
     188        add_post_meta( self::$post_id, 'test_number', '42' );
     189        add_post_meta( self::$post_id, 'test_bool', 1 );
     190
     191        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    184192        $response = $this->server->dispatch( $request );
    185193        $this->assertEquals( 200, $response->get_status() );
     
    206214    public function test_set_value() {
    207215        // Ensure no data exists currently.
    208         $values = get_post_meta( $this->post_id, 'test_single', false );
     216        $values = get_post_meta( self::$post_id, 'test_single', false );
    209217        $this->assertEmpty( $values );
    210218
     
    216224            ),
    217225        );
    218         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    219         $request->set_body_params( $data );
    220 
    221         $response = $this->server->dispatch( $request );
    222         $this->assertEquals( 200, $response->get_status() );
    223 
    224         $meta = get_post_meta( $this->post_id, 'test_single', false );
     226        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     227        $request->set_body_params( $data );
     228
     229        $response = $this->server->dispatch( $request );
     230        $this->assertEquals( 200, $response->get_status() );
     231
     232        $meta = get_post_meta( self::$post_id, 'test_single', false );
    225233        $this->assertNotEmpty( $meta );
    226234        $this->assertCount( 1, $meta );
     
    238246    public function test_set_duplicate_single_value() {
    239247        // Start with an existing metakey and value.
    240         $values = update_post_meta( $this->post_id, 'test_single', 'test_value' );
    241         $this->assertEquals( 'test_value', get_post_meta( $this->post_id, 'test_single', true ) );
     248        $values = update_post_meta( self::$post_id, 'test_single', 'test_value' );
     249        $this->assertEquals( 'test_value', get_post_meta( self::$post_id, 'test_single', true ) );
    242250
    243251        $this->grant_write_permission();
     
    248256            ),
    249257        );
    250         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    251         $request->set_body_params( $data );
    252 
    253         $response = $this->server->dispatch( $request );
    254         $this->assertEquals( 200, $response->get_status() );
    255 
    256         $meta = get_post_meta( $this->post_id, 'test_single', true );
     258        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     259        $request->set_body_params( $data );
     260
     261        $response = $this->server->dispatch( $request );
     262        $this->assertEquals( 200, $response->get_status() );
     263
     264        $meta = get_post_meta( self::$post_id, 'test_single', true );
    257265        $this->assertNotEmpty( $meta );
    258266        $this->assertEquals( 'test_value', $meta );
     
    276284        wp_set_current_user( 0 );
    277285
    278         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     286        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    279287        $request->set_body_params( $data );
    280288
     
    283291
    284292        // Check that the value wasn't actually updated.
    285         $this->assertEmpty( get_post_meta( $this->post_id, 'test_single', false ) );
     293        $this->assertEmpty( get_post_meta( self::$post_id, 'test_single', false ) );
    286294    }
    287295
     
    298306        $this->grant_write_permission();
    299307
    300         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     308        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    301309        $request->set_body_params( $data );
    302310
    303311        $response = $this->server->dispatch( $request );
    304312        $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
    305         $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth', false ) );
     313        $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth', false ) );
    306314    }
    307315
     
    318326        $this->grant_write_permission();
    319327
    320         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     328        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    321329        $request->set_body_params( $data );
    322330
     
    336344    public function test_set_value_multiple() {
    337345        // Ensure no data exists currently.
    338         $values = get_post_meta( $this->post_id, 'test_multi', false );
     346        $values = get_post_meta( self::$post_id, 'test_multi', false );
    339347        $this->assertEmpty( $values );
    340348
     
    346354            ),
    347355        );
    348         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    349         $request->set_body_params( $data );
    350 
    351         $response = $this->server->dispatch( $request );
    352         $this->assertEquals( 200, $response->get_status() );
    353 
    354         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     356        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     357        $request->set_body_params( $data );
     358
     359        $response = $this->server->dispatch( $request );
     360        $this->assertEquals( 200, $response->get_status() );
     361
     362        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    355363        $this->assertNotEmpty( $meta );
    356364        $this->assertCount( 1, $meta );
     
    368376        $this->assertEquals( 200, $response->get_status() );
    369377
    370         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     378        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    371379        $this->assertNotEmpty( $meta );
    372380        $this->assertCount( 2, $meta );
     
    379387     */
    380388    public function test_set_value_remove_one() {
    381         add_post_meta( $this->post_id, 'test_multi', 'c' );
    382         add_post_meta( $this->post_id, 'test_multi', 'n' );
    383         add_post_meta( $this->post_id, 'test_multi', 'n' );
     389        add_post_meta( self::$post_id, 'test_multi', 'c' );
     390        add_post_meta( self::$post_id, 'test_multi', 'n' );
     391        add_post_meta( self::$post_id, 'test_multi', 'n' );
    384392
    385393        $this->grant_write_permission();
     
    390398            ),
    391399        );
    392         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    393         $request->set_body_params( $data );
    394 
    395         $response = $this->server->dispatch( $request );
    396         $this->assertEquals( 200, $response->get_status() );
    397 
    398         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     400        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     401        $request->set_body_params( $data );
     402
     403        $response = $this->server->dispatch( $request );
     404        $this->assertEquals( 200, $response->get_status() );
     405
     406        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    399407        $this->assertNotEmpty( $meta );
    400408        $this->assertCount( 2, $meta );
     
    408416    public function test_set_value_multiple_unauthenticated() {
    409417        // Ensure no data exists currently.
    410         $values = get_post_meta( $this->post_id, 'test_multi', false );
     418        $values = get_post_meta( self::$post_id, 'test_multi', false );
    411419        $this->assertEmpty( $values );
    412420
     
    418426            ),
    419427        );
    420         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     428        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    421429        $request->set_body_params( $data );
    422430
     
    424432        $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
    425433
    426         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     434        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    427435        $this->assertEmpty( $meta );
    428436    }
     
    440448        $this->grant_write_permission();
    441449
    442         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     450        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    443451        $request->set_body_params( $data );
    444452
    445453        $response = $this->server->dispatch( $request );
    446454        $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
    447         $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth_multi', false ) );
     455        $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth_multi', false ) );
    448456    }
    449457
    450458    public function test_add_multi_value_db_error() {
    451459        // Ensure no data exists currently.
    452         $values = get_post_meta( $this->post_id, 'test_multi', false );
     460        $values = get_post_meta( self::$post_id, 'test_multi', false );
    453461        $this->assertEmpty( $values );
    454462
     
    460468            ),
    461469        );
    462         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     470        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    463471        $request->set_body_params( $data );
    464472
     
    479487
    480488    public function test_remove_multi_value_db_error() {
    481         add_post_meta( $this->post_id, 'test_multi', 'val1' );
    482         $values = get_post_meta( $this->post_id, 'test_multi', false );
     489        add_post_meta( self::$post_id, 'test_multi', 'val1' );
     490        $values = get_post_meta( self::$post_id, 'test_multi', false );
    483491        $this->assertEquals( array( 'val1' ), $values );
    484492
     
    490498            ),
    491499        );
    492         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     500        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    493501        $request->set_body_params( $data );
    494502
     
    509517
    510518    public function test_delete_value() {
    511         add_post_meta( $this->post_id, 'test_single', 'val1' );
    512         $current = get_post_meta( $this->post_id, 'test_single', true );
     519        add_post_meta( self::$post_id, 'test_single', 'val1' );
     520        $current = get_post_meta( self::$post_id, 'test_single', true );
    513521        $this->assertEquals( 'val1', $current );
    514522
     
    520528            ),
    521529        );
    522         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    523         $request->set_body_params( $data );
    524 
    525         $response = $this->server->dispatch( $request );
    526         $this->assertEquals( 200, $response->get_status() );
    527 
    528         $meta = get_post_meta( $this->post_id, 'test_single', false );
     530        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     531        $request->set_body_params( $data );
     532
     533        $response = $this->server->dispatch( $request );
     534        $this->assertEquals( 200, $response->get_status() );
     535
     536        $meta = get_post_meta( self::$post_id, 'test_single', false );
    529537        $this->assertEmpty( $meta );
    530538    }
     
    534542     */
    535543    public function test_delete_value_blocked() {
    536         add_post_meta( $this->post_id, 'test_bad_auth', 'val1' );
    537         $current = get_post_meta( $this->post_id, 'test_bad_auth', true );
     544        add_post_meta( self::$post_id, 'test_bad_auth', 'val1' );
     545        $current = get_post_meta( self::$post_id, 'test_bad_auth', true );
    538546        $this->assertEquals( 'val1', $current );
    539547
     
    545553            ),
    546554        );
    547         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     555        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    548556        $request->set_body_params( $data );
    549557
     
    551559        $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
    552560
    553         $meta = get_post_meta( $this->post_id, 'test_bad_auth', true );
     561        $meta = get_post_meta( self::$post_id, 'test_bad_auth', true );
    554562        $this->assertEquals( 'val1', $meta );
    555563    }
     
    559567     */
    560568    public function test_delete_value_db_error() {
    561         add_post_meta( $this->post_id, 'test_single', 'val1' );
    562         $current = get_post_meta( $this->post_id, 'test_single', true );
     569        add_post_meta( self::$post_id, 'test_single', 'val1' );
     570        $current = get_post_meta( self::$post_id, 'test_single', true );
    563571        $this->assertEquals( 'val1', $current );
    564572
     
    570578            ),
    571579        );
    572         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     580        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    573581        $request->set_body_params( $data );
    574582        /**
     
    588596
    589597    public function test_get_schema() {
    590         $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     598        $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    591599        $response = $this->server->dispatch( $request );
    592600
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r38968 r38975  
    1111 */
    1212class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
     13    protected static $post_id;
     14
     15    protected static $editor_id;
     16    protected static $author_id;
     17    protected static $contributor_id;
     18
     19    public static function wpSetUpBeforeClass( $factory ) {
     20        self::$post_id = $factory->post->create();
     21
     22        self::$editor_id = $factory->user->create( array(
     23            'role' => 'editor',
     24        ) );
     25        self::$author_id = $factory->user->create( array(
     26            'role' => 'author',
     27        ) );
     28        self::$contributor_id = $factory->user->create( array(
     29            'role' => 'contributor',
     30        ) );
     31    }
     32
     33    public static function wpTearDownAfterClass() {
     34        wp_delete_post( self::$post_id, true );
     35
     36        self::delete_user( self::$editor_id );
     37        self::delete_user( self::$author_id );
     38        self::delete_user( self::$contributor_id );
     39    }
    1340
    1441    public function setUp() {
    1542        parent::setUp();
    16 
    17         $this->post_id = $this->factory->post->create();
    18 
    19         $this->editor_id = $this->factory->user->create( array(
    20             'role' => 'editor',
    21         ) );
    22         $this->author_id = $this->factory->user->create( array(
    23             'role' => 'author',
    24         ) );
    25         $this->contributor_id = $this->factory->user->create( array(
    26             'role' => 'contributor',
    27         ) );
    28 
    2943        register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) );
    3044    }
     
    4761        $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    4862        // Single
    49         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id );
     63        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id );
    5064        $response = $this->server->dispatch( $request );
    5165        $data = $response->get_data();
     
    106120
    107121    public function test_get_items_author_query() {
    108         $this->factory->post->create( array( 'post_author' => $this->editor_id ) );
    109         $this->factory->post->create( array( 'post_author' => $this->author_id ) );
     122        $this->factory->post->create( array( 'post_author' => self::$editor_id ) );
     123        $this->factory->post->create( array( 'post_author' => self::$author_id ) );
    110124        // All 3 posts
    111125        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     
    115129        // 2 of 3 posts
    116130        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    117         $request->set_param( 'author', array( $this->editor_id, $this->author_id ) );
     131        $request->set_param( 'author', array( self::$editor_id, self::$author_id ) );
    118132        $response = $this->server->dispatch( $request );
    119133        $this->assertEquals( 200, $response->get_status() );
    120134        $data = $response->get_data();
    121135        $this->assertEquals( 2, count( $data ) );
    122         $this->assertEqualSets( array( $this->editor_id, $this->author_id ), wp_list_pluck( $data, 'author' ) );
     136        $this->assertEqualSets( array( self::$editor_id, self::$author_id ), wp_list_pluck( $data, 'author' ) );
    123137        // 1 of 3 posts
    124138        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    125         $request->set_param( 'author', $this->editor_id );
     139        $request->set_param( 'author', self::$editor_id );
    126140        $response = $this->server->dispatch( $request );
    127141        $this->assertEquals( 200, $response->get_status() );
    128142        $data = $response->get_data();
    129143        $this->assertEquals( 1, count( $data ) );
    130         $this->assertEquals( $this->editor_id, $data[0]['author'] );
     144        $this->assertEquals( self::$editor_id, $data[0]['author'] );
    131145    }
    132146
    133147    public function test_get_items_author_exclude_query() {
    134         $this->factory->post->create( array( 'post_author' => $this->editor_id ) );
    135         $this->factory->post->create( array( 'post_author' => $this->author_id ) );
     148        $this->factory->post->create( array( 'post_author' => self::$editor_id ) );
     149        $this->factory->post->create( array( 'post_author' => self::$author_id ) );
    136150        // All 3 posts
    137151        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     
    141155        // 1 of 3 posts
    142156        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    143         $request->set_param( 'author_exclude', array( $this->editor_id, $this->author_id ) );
     157        $request->set_param( 'author_exclude', array( self::$editor_id, self::$author_id ) );
    144158        $response = $this->server->dispatch( $request );
    145159        $this->assertEquals( 200, $response->get_status() );
    146160        $data = $response->get_data();
    147161        $this->assertEquals( 1, count( $data ) );
    148         $this->assertNotEquals( $this->editor_id, $data[0]['author'] );
    149         $this->assertNotEquals( $this->author_id, $data[0]['author'] );
     162        $this->assertNotEquals( self::$editor_id, $data[0]['author'] );
     163        $this->assertNotEquals( self::$author_id, $data[0]['author'] );
    150164        // 2 of 3 posts
    151165        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    152         $request->set_param( 'author_exclude', $this->editor_id );
     166        $request->set_param( 'author_exclude', self::$editor_id );
    153167        $response = $this->server->dispatch( $request );
    154168        $this->assertEquals( 200, $response->get_status() );
    155169        $data = $response->get_data();
    156170        $this->assertEquals( 2, count( $data ) );
    157         $this->assertNotEquals( $this->editor_id, $data[0]['author'] );
    158         $this->assertNotEquals( $this->editor_id, $data[1]['author'] );
     171        $this->assertNotEquals( self::$editor_id, $data[0]['author'] );
     172        $this->assertNotEquals( self::$editor_id, $data[1]['author'] );
    159173    }
    160174
     
    233247        $response = $this->server->dispatch( $request );
    234248        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    235         wp_set_current_user( $this->editor_id );
     249        wp_set_current_user( self::$editor_id );
    236250        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    237251        $request->set_param( 'status', 'draft' );
     
    306320        $response = $this->server->dispatch( $request );
    307321        $data = $response->get_data();
    308         $this->assertEquals( array( $this->post_id, $post_id3, $post_id2, $post_id1 ), wp_list_pluck( $data, 'id' ) );
     322        $this->assertEquals( array( self::$post_id, $post_id3, $post_id2, $post_id1 ), wp_list_pluck( $data, 'id' ) );
    309323
    310324        // Permit stickies
     
    313327        $response = $this->server->dispatch( $request );
    314328        $data = $response->get_data();
    315         $this->assertEquals( array( $post_id2, $this->post_id, $post_id3, $post_id1 ), wp_list_pluck( $data, 'id' ) );
     329        $this->assertEquals( array( $post_id2, self::$post_id, $post_id3, $post_id1 ), wp_list_pluck( $data, 'id' ) );
    316330    }
    317331
    318332    public function test_get_items_offset_query() {
    319         $id1 = $this->post_id;
     333        $id1 = self::$post_id;
    320334        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    321335        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    336350
    337351    public function test_get_items_tags_query() {
    338         $id1 = $this->post_id;
     352        $id1 = self::$post_id;
    339353        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    340354        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    353367
    354368    public function test_get_items_tags_exclude_query() {
    355         $id1 = $this->post_id;
     369        $id1 = self::$post_id;
    356370        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    357371        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    372386
    373387    public function test_get_items_tags_and_categories_query() {
    374         $id1 = $this->post_id;
     388        $id1 = self::$post_id;
    375389        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    376390        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    392406
    393407    public function test_get_items_tags_and_categories_exclude_query() {
    394         $id1 = $this->post_id;
     408        $id1 = self::$post_id;
    395409        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    396410        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    414428
    415429    public function test_get_items_sticky_query() {
    416         $id1 = $this->post_id;
     430        $id1 = self::$post_id;
    417431        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    418432
     
    431445
    432446    public function test_get_items_sticky_with_post__in_query() {
    433         $id1 = $this->post_id;
     447        $id1 = self::$post_id;
    434448        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    435449        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    460474
    461475    public function test_get_items_not_sticky_query() {
    462         $id1 = $this->post_id;
     476        $id1 = self::$post_id;
    463477        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    464478
     
    477491
    478492    public function test_get_items_sticky_with_post__not_in_query() {
    479         $id1 = $this->post_id;
     493        $id1 = self::$post_id;
    480494        $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    481495        $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    587601
    588602        // But they are accessible to authorized users
    589         wp_set_current_user( $this->editor_id );
     603        wp_set_current_user( self::$editor_id );
    590604        $response = $this->server->dispatch( $request );
    591605        $data = $response->get_data();
     
    631645
    632646    public function test_get_item() {
    633         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     647        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    634648        $response = $this->server->dispatch( $request );
    635649
     
    638652
    639653    public function test_get_item_links() {
    640         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     654        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    641655        $response = $this->server->dispatch( $request );
    642656
    643657        $links = $response->get_links();
    644658
    645         $this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id ), $links['self'][0]['href'] );
     659        $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id ), $links['self'][0]['href'] );
    646660        $this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] );
    647661
    648         $this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( $this->post_id ) ), $links['about'][0]['href'] );
     662        $this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( self::$post_id ) ), $links['about'][0]['href'] );
    649663
    650664        $replies_url = rest_url( '/wp/v2/comments' );
    651         $replies_url = add_query_arg( 'post', $this->post_id, $replies_url );
     665        $replies_url = add_query_arg( 'post', self::$post_id, $replies_url );
    652666        $this->assertEquals( $replies_url, $links['replies'][0]['href'] );
    653667
    654         $this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id . '/revisions' ), $links['version-history'][0]['href'] );
     668        $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] );
    655669
    656670        $attachments_url = rest_url( '/wp/v2/media' );
    657         $attachments_url = add_query_arg( 'parent', $this->post_id, $attachments_url );
     671        $attachments_url = add_query_arg( 'parent', self::$post_id, $attachments_url );
    658672        $this->assertEquals( $attachments_url, $links['https://api.w.org/attachment'][0]['href'] );
    659673
     
    673687        $this->assertNull( $format_link );
    674688
    675         $tags_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/tags' ) );
     689        $tags_url = add_query_arg( 'post', self::$post_id, rest_url( '/wp/v2/tags' ) );
    676690        $this->assertEquals( $tags_url, $tag_link['href'] );
    677691
    678         $category_url = add_query_arg( 'post', $this->post_id, rest_url( '/wp/v2/categories' ) );
     692        $category_url = add_query_arg( 'post', self::$post_id, rest_url( '/wp/v2/categories' ) );
    679693        $this->assertEquals( $category_url, $cat_link['href'] );
    680694    }
    681695
    682696    public function test_get_item_links_no_author() {
    683         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     697        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    684698        $response = $this->server->dispatch( $request );
    685699        $links = $response->get_links();
    686700        $this->assertFalse( isset( $links['author'] ) );
    687         wp_update_post( array( 'ID' => $this->post_id, 'post_author' => $this->author_id ) );
    688         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     701        wp_update_post( array( 'ID' => self::$post_id, 'post_author' => self::$author_id ) );
     702        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    689703        $response = $this->server->dispatch( $request );
    690704        $links = $response->get_links();
    691         $this->assertEquals( rest_url( '/wp/v2/users/' . $this->author_id ), $links['author'][0]['href'] );
     705        $this->assertEquals( rest_url( '/wp/v2/users/' . self::$author_id ), $links['author'][0]['href'] );
    692706    }
    693707
     
    717731        ) );
    718732
    719         wp_set_current_user( $this->editor_id );
     733        wp_set_current_user( self::$editor_id );
    720734
    721735        $response = $this->server->dispatch( $request );
     
    737751    public function test_get_post_context_without_permission() {
    738752        wp_set_current_user( 0 );
    739         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     753        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    740754        $request->set_query_params( array(
    741755            'context' => 'edit',
     
    812826        register_post_status( 'testprivtatus', array( 'public' => false ) );
    813827        // Public status
    814         wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testpubstatus' ) );
    815         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     828        wp_update_post( array( 'ID' => self::$post_id, 'post_status' => 'testpubstatus' ) );
     829        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    816830        $response = $this->server->dispatch( $request );
    817831        $this->assertEquals( 200, $response->get_status() );
    818832        // Private status
    819         wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'testprivtatus' ) );
    820         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     833        wp_update_post( array( 'ID' => self::$post_id, 'post_status' => 'testprivtatus' ) );
     834        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    821835        $response = $this->server->dispatch( $request );
    822836        $this->assertEquals( 403, $response->get_status() );
     
    824838
    825839    public function test_prepare_item() {
    826         wp_set_current_user( $this->editor_id );
    827 
    828         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     840        wp_set_current_user( self::$editor_id );
     841
     842        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    829843        $request->set_query_params( array( 'context' => 'edit' ) );
    830844        $response = $this->server->dispatch( $request );
     
    834848
    835849    public function test_create_item() {
    836         wp_set_current_user( $this->editor_id );
     850        wp_set_current_user( self::$editor_id );
    837851
    838852        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    846860
    847861    public function test_rest_create_item() {
    848         wp_set_current_user( $this->editor_id );
     862        wp_set_current_user( self::$editor_id );
    849863
    850864        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    858872
    859873    public function test_create_post_invalid_id() {
    860         wp_set_current_user( $this->editor_id );
     874        wp_set_current_user( self::$editor_id );
    861875
    862876        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    871885
    872886    public function test_create_post_as_contributor() {
    873         wp_set_current_user( $this->contributor_id );
     887        wp_set_current_user( self::$contributor_id );
    874888
    875889        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    884898
    885899    public function test_create_post_sticky() {
    886         wp_set_current_user( $this->editor_id );
     900        wp_set_current_user( self::$editor_id );
    887901
    888902        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    900914
    901915    public function test_create_post_sticky_as_contributor() {
    902         wp_set_current_user( $this->contributor_id );
     916        wp_set_current_user( self::$contributor_id );
    903917
    904918        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    914928
    915929    public function test_create_post_other_author_without_permission() {
    916         wp_set_current_user( $this->author_id );
     930        wp_set_current_user( self::$author_id );
    917931
    918932        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    919933        $params = $this->set_post_data(array(
    920             'author' => $this->editor_id,
     934            'author' => self::$editor_id,
    921935        ));
    922936        $request->set_body_params( $params );
     
    940954
    941955    public function test_create_post_draft() {
    942         wp_set_current_user( $this->editor_id );
     956        wp_set_current_user( self::$editor_id );
    943957
    944958        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    959973
    960974    public function test_create_post_private() {
    961         wp_set_current_user( $this->editor_id );
     975        wp_set_current_user( self::$editor_id );
    962976
    963977        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    975989
    976990    public function test_create_post_private_without_permission() {
    977         wp_set_current_user( $this->author_id );
     991        wp_set_current_user( self::$author_id );
    978992        $user = wp_get_current_user();
    979993        $user->add_cap( 'publish_posts', false );
     
    985999        $params = $this->set_post_data( array(
    9861000            'status' => 'private',
    987             'author' => $this->author_id,
     1001            'author' => self::$author_id,
    9881002        ) );
    9891003        $request->set_body_params( $params );
     
    9941008
    9951009    public function test_create_post_publish_without_permission() {
    996         wp_set_current_user( $this->author_id );
     1010        wp_set_current_user( self::$author_id );
    9971011        $user = wp_get_current_user();
    9981012        $user->add_cap( 'publish_posts', false );
     
    10121026
    10131027    public function test_create_post_invalid_status() {
    1014         wp_set_current_user( $this->editor_id );
     1028        wp_set_current_user( self::$editor_id );
    10151029
    10161030        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    10251039
    10261040    public function test_create_post_with_format() {
    1027         wp_set_current_user( $this->editor_id );
     1041        wp_set_current_user( self::$editor_id );
    10281042
    10291043        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    10411055
    10421056    public function test_create_post_with_invalid_format() {
    1043         wp_set_current_user( $this->editor_id );
     1057        wp_set_current_user( self::$editor_id );
    10441058
    10451059        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    10611075        ) );
    10621076
    1063         wp_set_current_user( $this->editor_id );
     1077        wp_set_current_user( self::$editor_id );
    10641078
    10651079        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    10861100
    10871101    public function test_create_post_invalid_author() {
    1088         wp_set_current_user( $this->editor_id );
     1102        wp_set_current_user( self::$editor_id );
    10891103
    10901104        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    10991113
    11001114    public function test_create_post_invalid_author_without_permission() {
    1101         wp_set_current_user( $this->author_id );
    1102 
    1103         $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    1104         $params = $this->set_post_data( array(
    1105             'author' => $this->editor_id,
     1115        wp_set_current_user( self::$author_id );
     1116
     1117        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1118        $params = $this->set_post_data( array(
     1119            'author' => self::$editor_id,
    11061120        ) );
    11071121        $request->set_body_params( $params );
     
    11121126
    11131127    public function test_create_post_with_password() {
    1114         wp_set_current_user( $this->editor_id );
     1128        wp_set_current_user( self::$editor_id );
    11151129
    11161130        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    11261140
    11271141    public function test_create_post_with_falsy_password() {
    1128         wp_set_current_user( $this->editor_id );
     1142        wp_set_current_user( self::$editor_id );
    11291143
    11301144        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    11411155
    11421156    public function test_create_post_with_empty_string_password_and_sticky() {
    1143         wp_set_current_user( $this->editor_id );
     1157        wp_set_current_user( self::$editor_id );
    11441158
    11451159        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    11571171
    11581172    public function test_create_post_with_password_and_sticky_fails() {
    1159         wp_set_current_user( $this->editor_id );
     1173        wp_set_current_user( self::$editor_id );
    11601174
    11611175        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    11711185
    11721186    public function test_create_post_custom_date() {
    1173         wp_set_current_user( $this->editor_id );
     1187        wp_set_current_user( self::$editor_id );
    11741188
    11751189        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    11881202
    11891203    public function test_create_post_custom_date_with_timezone() {
    1190         wp_set_current_user( $this->editor_id );
     1204        wp_set_current_user( self::$editor_id );
    11911205
    11921206        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    12091223
    12101224    public function test_create_post_with_db_error() {
    1211         wp_set_current_user( $this->editor_id );
     1225        wp_set_current_user( self::$editor_id );
    12121226
    12131227        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    12311245
    12321246    public function test_create_post_with_invalid_date() {
    1233         wp_set_current_user( $this->editor_id );
     1247        wp_set_current_user( self::$editor_id );
    12341248
    12351249        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    12441258
    12451259    public function test_create_post_with_invalid_date_gmt() {
    1246         wp_set_current_user( $this->editor_id );
     1260        wp_set_current_user( self::$editor_id );
    12471261
    12481262        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    12571271
    12581272    public function test_create_post_with_quotes_in_title() {
    1259         wp_set_current_user( $this->editor_id );
     1273        wp_set_current_user( self::$editor_id );
    12601274
    12611275        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    12701284
    12711285    public function test_create_post_with_categories() {
    1272         wp_set_current_user( $this->editor_id );
     1286        wp_set_current_user( self::$editor_id );
    12731287        $category = wp_insert_term( 'Test Category', 'category' );
    12741288        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     
    12871301
    12881302    public function test_create_post_with_invalid_categories() {
    1289         wp_set_current_user( $this->editor_id );
     1303        wp_set_current_user( self::$editor_id );
    12901304        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    12911305        $params = $this->set_post_data( array(
     
    13031317
    13041318    public function test_update_item() {
    1305         wp_set_current_user( $this->editor_id );
    1306 
    1307         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1319        wp_set_current_user( self::$editor_id );
     1320
     1321        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    13081322        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    13091323        $params = $this->set_post_data();
     
    13131327        $this->check_update_post_response( $response );
    13141328        $new_data = $response->get_data();
    1315         $this->assertEquals( $this->post_id, $new_data['id'] );
     1329        $this->assertEquals( self::$post_id, $new_data['id'] );
    13161330        $this->assertEquals( $params['title'], $new_data['title']['raw'] );
    13171331        $this->assertEquals( $params['content'], $new_data['content']['raw'] );
    13181332        $this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
    1319         $post = get_post( $this->post_id );
     1333        $post = get_post( self::$post_id );
    13201334        $this->assertEquals( $params['title'], $post->post_title );
    13211335        $this->assertEquals( $params['content'], $post->post_content );
     
    13241338
    13251339    public function test_rest_update_post() {
    1326         wp_set_current_user( $this->editor_id );
    1327 
    1328         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1340        wp_set_current_user( self::$editor_id );
     1341
     1342        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    13291343        $request->add_header( 'content-type', 'application/json' );
    13301344        $params = $this->set_post_data();
     
    13341348        $this->check_update_post_response( $response );
    13351349        $new_data = $response->get_data();
    1336         $this->assertEquals( $this->post_id, $new_data['id'] );
     1350        $this->assertEquals( self::$post_id, $new_data['id'] );
    13371351        $this->assertEquals( $params['title'], $new_data['title']['raw'] );
    13381352        $this->assertEquals( $params['content'], $new_data['content']['raw'] );
    13391353        $this->assertEquals( $params['excerpt'], $new_data['excerpt']['raw'] );
    1340         $post = get_post( $this->post_id );
     1354        $post = get_post( self::$post_id );
    13411355        $this->assertEquals( $params['title'], $post->post_title );
    13421356        $this->assertEquals( $params['content'], $post->post_content );
     
    13451359
    13461360    public function test_rest_update_post_raw() {
    1347         wp_set_current_user( $this->editor_id );
    1348 
    1349         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1361        wp_set_current_user( self::$editor_id );
     1362
     1363        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    13501364        $request->add_header( 'content-type', 'application/json' );
    13511365        $params = $this->set_raw_post_data();
     
    13551369        $this->check_update_post_response( $response );
    13561370        $new_data = $response->get_data();
    1357         $this->assertEquals( $this->post_id, $new_data['id'] );
     1371        $this->assertEquals( self::$post_id, $new_data['id'] );
    13581372        $this->assertEquals( $params['title']['raw'], $new_data['title']['raw'] );
    13591373        $this->assertEquals( $params['content']['raw'], $new_data['content']['raw'] );
    13601374        $this->assertEquals( $params['excerpt']['raw'], $new_data['excerpt']['raw'] );
    1361         $post = get_post( $this->post_id );
     1375        $post = get_post( self::$post_id );
    13621376        $this->assertEquals( $params['title']['raw'], $post->post_title );
    13631377        $this->assertEquals( $params['content']['raw'], $post->post_content );
     
    13661380
    13671381    public function test_update_post_without_extra_params() {
    1368         wp_set_current_user( $this->editor_id );
    1369 
    1370         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1382        wp_set_current_user( self::$editor_id );
     1383
     1384        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    13711385        $params = $this->set_post_data();
    13721386        unset( $params['type'] );
     
    13811395
    13821396    public function test_update_post_without_permission() {
    1383         wp_set_current_user( $this->editor_id );
     1397        wp_set_current_user( self::$editor_id );
    13841398        $user = wp_get_current_user();
    13851399        $user->add_cap( 'edit_published_posts', false );
     
    13881402        $user->update_user_level_from_caps();
    13891403
    1390         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1404        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    13911405        $params = $this->set_post_data();
    13921406        $request->set_body_params( $params );
     
    13971411
    13981412    public function test_update_post_sticky_as_contributor() {
    1399         wp_set_current_user( $this->contributor_id );
    1400 
    1401         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1413        wp_set_current_user( self::$contributor_id );
     1414
     1415        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    14021416        $params = $this->set_post_data( array(
    14031417            'sticky' => true,
     
    14111425
    14121426    public function test_update_post_invalid_id() {
    1413         wp_set_current_user( $this->editor_id );
     1427        wp_set_current_user( self::$editor_id );
    14141428
    14151429        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
     
    14201434
    14211435    public function test_update_post_invalid_route() {
    1422         wp_set_current_user( $this->editor_id );
    1423 
    1424         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $this->post_id ) );
     1436        wp_set_current_user( self::$editor_id );
     1437
     1438        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', self::$post_id ) );
    14251439        $response = $this->server->dispatch( $request );
    14261440
     
    14291443
    14301444    public function test_update_post_with_format() {
    1431         wp_set_current_user( $this->editor_id );
    1432 
    1433         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1445        wp_set_current_user( self::$editor_id );
     1446
     1447        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    14341448        $params = $this->set_post_data( array(
    14351449            'format' => 'gallery',
     
    14451459
    14461460    public function test_update_post_with_invalid_format() {
    1447         wp_set_current_user( $this->editor_id );
    1448 
    1449         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1461        wp_set_current_user( self::$editor_id );
     1462
     1463        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    14501464        $params = $this->set_post_data( array(
    14511465            'format' => 'testformat',
     
    14581472
    14591473    public function test_update_post_ignore_readonly() {
    1460         wp_set_current_user( $this->editor_id );
     1474        wp_set_current_user( self::$editor_id );
    14611475
    14621476        $new_content = rand_str();
    14631477        $expected_modified = current_time( 'mysql' );
    14641478
    1465         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1479        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    14661480        $params = $this->set_post_data( array(
    14671481            'modified' => '2010-06-01T02:00:00Z',
     
    14841498
    14851499    public function test_update_post_with_invalid_date() {
    1486         wp_set_current_user( $this->editor_id );
    1487 
    1488         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1500        wp_set_current_user( self::$editor_id );
     1501
     1502        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    14891503        $params = $this->set_post_data( array(
    14901504            'date' => rand_str(),
     
    14971511
    14981512    public function test_update_post_with_invalid_date_gmt() {
    1499         wp_set_current_user( $this->editor_id );
    1500 
    1501         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1513        wp_set_current_user( self::$editor_id );
     1514
     1515        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15021516        $params = $this->set_post_data( array(
    15031517            'date_gmt' => rand_str(),
     
    15101524
    15111525    public function test_update_post_slug() {
    1512         wp_set_current_user( $this->editor_id );
    1513 
    1514         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1526        wp_set_current_user( self::$editor_id );
     1527
     1528        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15151529        $params = $this->set_post_data( array(
    15161530            'slug' => 'sample-slug',
     
    15261540
    15271541    public function test_update_post_slug_accented_chars() {
    1528         wp_set_current_user( $this->editor_id );
    1529 
    1530         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1542        wp_set_current_user( self::$editor_id );
     1543
     1544        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15311545        $params = $this->set_post_data( array(
    15321546            'slug' => 'tęst-acceńted-chäræcters',
     
    15421556
    15431557    public function test_update_post_sticky() {
    1544         wp_set_current_user( $this->editor_id );
    1545 
    1546         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1558        wp_set_current_user( self::$editor_id );
     1559
     1560        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15471561        $params = $this->set_post_data( array(
    15481562            'sticky' => true,
     
    15571571
    15581572        // Updating another field shouldn't change sticky status
    1559         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1573        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15601574        $params = $this->set_post_data( array(
    15611575            'title'       => 'This should not reset sticky',
     
    15711585
    15721586    public function test_update_post_excerpt() {
    1573         wp_set_current_user( $this->editor_id );
    1574 
    1575         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1587        wp_set_current_user( self::$editor_id );
     1588
     1589        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15761590        $request->set_body_params( array(
    15771591            'excerpt' => 'An Excerpt',
     
    15841598
    15851599    public function test_update_post_empty_excerpt() {
    1586         wp_set_current_user( $this->editor_id );
    1587 
    1588         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1600        wp_set_current_user( self::$editor_id );
     1601
     1602        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15891603        $request->set_body_params( array(
    15901604            'excerpt' => '',
     
    15971611
    15981612    public function test_update_post_content() {
    1599         wp_set_current_user( $this->editor_id );
    1600 
    1601         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1613        wp_set_current_user( self::$editor_id );
     1614
     1615        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    16021616        $request->set_body_params( array(
    16031617            'content' => 'Some Content',
     
    16101624
    16111625    public function test_update_post_empty_content() {
    1612         wp_set_current_user( $this->editor_id );
    1613 
    1614         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1626        wp_set_current_user( self::$editor_id );
     1627
     1628        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    16151629        $request->set_body_params( array(
    16161630            'content' => '',
     
    16231637
    16241638    public function test_update_post_with_password_and_sticky_fails() {
    1625         wp_set_current_user( $this->editor_id );
    1626 
    1627         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1639        wp_set_current_user( self::$editor_id );
     1640
     1641        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    16281642        $params = $this->set_post_data( array(
    16291643            'password' => '123',
     
    16371651
    16381652    public function test_update_stick_post_with_password_fails() {
    1639         wp_set_current_user( $this->editor_id );
    1640 
    1641         stick_post( $this->post_id );
    1642 
    1643         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1653        wp_set_current_user( self::$editor_id );
     1654
     1655        stick_post( self::$post_id );
     1656
     1657        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    16441658        $params = $this->set_post_data( array(
    16451659            'password' => '123',
     
    16521666
    16531667    public function test_update_password_protected_post_with_sticky_fails() {
    1654         wp_set_current_user( $this->editor_id );
    1655 
    1656         wp_update_post( array( 'ID' => $this->post_id, 'post_password' => '123' ) );
    1657 
    1658         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1668        wp_set_current_user( self::$editor_id );
     1669
     1670        wp_update_post( array( 'ID' => self::$post_id, 'post_password' => '123' ) );
     1671
     1672        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    16591673        $params = $this->set_post_data( array(
    16601674            'sticky' => true,
     
    16671681
    16681682    public function test_update_post_with_quotes_in_title() {
    1669         wp_set_current_user( $this->editor_id );
    1670 
    1671         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1683        wp_set_current_user( self::$editor_id );
     1684
     1685        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    16721686        $params = $this->set_post_data( array(
    16731687            'title' => "Rob O'Rourke's Diary",
     
    16811695    public function test_update_post_with_categories() {
    16821696
    1683         wp_set_current_user( $this->editor_id );
     1697        wp_set_current_user( self::$editor_id );
    16841698        $category = wp_insert_term( 'Test Category', 'category' );
    16851699
    1686         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1700        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    16871701        $params = $this->set_post_data( array(
    16881702            'title' => 'Tester',
     
    17151729    public function test_update_post_with_empty_categories() {
    17161730
    1717         wp_set_current_user( $this->editor_id );
     1731        wp_set_current_user( self::$editor_id );
    17181732        $category = wp_insert_term( 'Test Category', 'category' );
    1719         wp_set_object_terms( $this->post_id, $category['term_id'], 'category' );
    1720 
    1721         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1733        wp_set_object_terms( self::$post_id, $category['term_id'], 'category' );
     1734
     1735        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    17221736        $params = $this->set_post_data( array(
    17231737            'title' => 'Tester',
     
    17321746    public function test_delete_item() {
    17331747        $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
    1734         wp_set_current_user( $this->editor_id );
     1748        wp_set_current_user( self::$editor_id );
    17351749
    17361750        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
     
    17451759    public function test_delete_item_skip_trash() {
    17461760        $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
    1747         wp_set_current_user( $this->editor_id );
     1761        wp_set_current_user( self::$editor_id );
    17481762
    17491763        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
     
    17591773    public function test_delete_item_already_trashed() {
    17601774        $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
    1761         wp_set_current_user( $this->editor_id );
     1775        wp_set_current_user( self::$editor_id );
    17621776        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
    17631777        $response = $this->server->dispatch( $request );
     
    17681782
    17691783    public function test_delete_post_invalid_id() {
    1770         wp_set_current_user( $this->editor_id );
     1784        wp_set_current_user( self::$editor_id );
    17711785
    17721786        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
     
    17781792    public function test_delete_post_invalid_post_type() {
    17791793        $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    1780         wp_set_current_user( $this->editor_id );
     1794        wp_set_current_user( self::$editor_id );
    17811795
    17821796        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $page_id );
     
    17871801
    17881802    public function test_delete_post_without_permission() {
    1789         wp_set_current_user( $this->author_id );
    1790 
    1791         $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1803        wp_set_current_user( self::$author_id );
     1804
     1805        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    17921806        $response = $this->server->dispatch( $request );
    17931807
     
    19061920        ) );
    19071921
    1908         wp_set_current_user( $this->editor_id );
     1922        wp_set_current_user( self::$editor_id );
    19091923        // Check for error on update.
    1910         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     1924        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    19111925        $request->set_body_params( array(
    19121926            'my_custom_int' => 'returnError',
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r38832 r38975  
    1111  */
    1212class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase {
     13    protected static $post_id;
     14    protected static $page_id;
     15
     16    protected static $editor_id;
     17    protected static $contributor_id;
     18
     19    public static function wpSetUpBeforeClass( $factory ) {
     20        self::$post_id = $factory->post->create();
     21        self::$page_id = $factory->post->create( array( 'post_type' => 'page' ) );
     22
     23        self::$editor_id = $factory->user->create( array(
     24            'role' => 'editor',
     25        ) );
     26        self::$contributor_id = $factory->user->create( array(
     27            'role' => 'contributor',
     28        ) );
     29
     30        wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => self::$post_id ) );
     31        wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => self::$post_id ) );
     32    }
     33
     34    public static function wpTearDownAfterClass() {
     35        // Also deletes revisions.
     36        wp_delete_post( self::$post_id, true );
     37        wp_delete_post( self::$page_id, true );
     38
     39        self::delete_user( self::$editor_id );
     40        self::delete_user( self::$contributor_id );
     41    }
    1342
    1443    public function setUp() {
    1544        parent::setUp();
    16         $this->post_id = $this->factory->post->create();
    17         $this->page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    18 
    19         $this->editor_id = $this->factory->user->create( array(
    20             'role' => 'editor',
    21         ) );
    22         $this->contributor_id = $this->factory->user->create( array(
    23             'role' => 'contributor',
    24         ) );
    25 
    26         wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => $this->post_id ) );
    27         wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => $this->post_id ) );
    28         $revisions = wp_get_post_revisions( $this->post_id );
     45
     46        $revisions = wp_get_post_revisions( self::$post_id );
    2947        $this->revision_1 = array_pop( $revisions );
    3048        $this->revision_id1 = $this->revision_1->ID;
     
    4361    public function test_context_param() {
    4462        // Collection
    45         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     63        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    4664        $response = $this->server->dispatch( $request );
    4765        $data = $response->get_data();
     
    4967        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    5068        // Single
    51         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_1->ID );
     69        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_1->ID );
    5270        $response = $this->server->dispatch( $request );
    5371        $data = $response->get_data();
     
    5775
    5876    public function test_get_items() {
    59         wp_set_current_user( $this->editor_id );
    60         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     77        wp_set_current_user( self::$editor_id );
     78        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    6179        $response = $this->server->dispatch( $request );
    6280        $data = $response->get_data();
     
    7492    public function test_get_items_no_permission() {
    7593        wp_set_current_user( 0 );
    76         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     94        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    7795        $response = $this->server->dispatch( $request );
    7896
    7997        $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
    80         wp_set_current_user( $this->contributor_id );
     98        wp_set_current_user( self::$contributor_id );
    8199        $response = $this->server->dispatch( $request );
    82100        $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
     
    84102
    85103    public function test_get_items_missing_parent() {
    86         wp_set_current_user( $this->editor_id );
     104        wp_set_current_user( self::$editor_id );
    87105        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
    88106        $response = $this->server->dispatch( $request );
     
    91109
    92110    public function test_get_items_invalid_parent_post_type() {
    93         wp_set_current_user( $this->editor_id );
    94         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->page_id . '/revisions' );
     111        wp_set_current_user( self::$editor_id );
     112        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions' );
    95113        $response = $this->server->dispatch( $request );
    96114        $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
     
    98116
    99117    public function test_get_item() {
    100         wp_set_current_user( $this->editor_id );
    101         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     118        wp_set_current_user( self::$editor_id );
     119        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    102120        $response = $this->server->dispatch( $request );
    103121        $this->assertEquals( 200, $response->get_status() );
     
    122140
    123141    public function test_get_item_embed_context() {
    124         wp_set_current_user( $this->editor_id );
    125         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     142        wp_set_current_user( self::$editor_id );
     143        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    126144        $request->set_param( 'context', 'embed' );
    127145        $response = $this->server->dispatch( $request );
     
    141159    public function test_get_item_no_permission() {
    142160        wp_set_current_user( 0 );
    143         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     161        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    144162
    145163        $response = $this->server->dispatch( $request );
    146164        $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
    147         wp_set_current_user( $this->contributor_id );
     165        wp_set_current_user( self::$contributor_id );
    148166        $response = $this->server->dispatch( $request );
    149167        $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
     
    151169
    152170    public function test_get_item_missing_parent() {
    153         wp_set_current_user( $this->editor_id );
     171        wp_set_current_user( self::$editor_id );
    154172        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 );
    155173        $response = $this->server->dispatch( $request );
     
    158176
    159177    public function test_get_item_invalid_parent_post_type() {
    160         wp_set_current_user( $this->editor_id );
    161         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->page_id . '/revisions/' . $this->revision_id1 );
     178        wp_set_current_user( self::$editor_id );
     179        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 );
    162180        $response = $this->server->dispatch( $request );
    163181        $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
     
    165183
    166184    public function test_delete_item() {
    167         wp_set_current_user( $this->editor_id );
    168         $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     185        wp_set_current_user( self::$editor_id );
     186        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    169187        $response = $this->server->dispatch( $request );
    170188        $this->assertEquals( 200, $response->get_status() );
     
    173191
    174192    public function test_delete_item_no_permission() {
    175         wp_set_current_user( $this->contributor_id );
    176         $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     193        wp_set_current_user( self::$contributor_id );
     194        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    177195        $response = $this->server->dispatch( $request );
    178196        $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
     
    180198
    181199    public function test_prepare_item() {
    182         wp_set_current_user( $this->editor_id );
    183         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     200        wp_set_current_user( self::$editor_id );
     201        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    184202        $response = $this->server->dispatch( $request );
    185203        $this->assertEquals( 200, $response->get_status() );
     
    188206
    189207    public function test_get_item_schema() {
    190         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     208        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    191209        $response = $this->server->dispatch( $request );
    192210        $data = $response->get_data();
     
    208226
    209227    public function test_create_item() {
    210         $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     228        $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    211229        $response = $this->server->dispatch( $request );
    212230        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
     
    214232
    215233    public function test_update_item() {
    216         $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     234        $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    217235        $response = $this->server->dispatch( $request );
    218236        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
     
    234252        ) );
    235253
    236         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     254        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    237255
    238256        $response = $this->server->dispatch( $request );
     
    244262        wp_set_current_user( 1 );
    245263
    246         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     264        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    247265
    248266        $response = $this->server->dispatch( $request );
  • trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php

    r38971 r38975  
    1111 */
    1212class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase {
     13    protected static $administrator;
     14
     15    public static function wpSetUpBeforeClass( $factory ) {
     16        self::$administrator = $factory->user->create( array(
     17            'role' => 'administrator',
     18        ) );
     19    }
     20
     21    public static function wpTearDownAfterClass() {
     22        self::delete_user( self::$administrator );
     23    }
    1324
    1425    public function setUp() {
    1526        parent::setUp();
    16         $this->administrator = $this->factory->user->create( array(
    17             'role' => 'administrator',
    18         ) );
    1927        $this->endpoint = new WP_REST_Settings_Controller();
    2028    }
     
    3846
    3947    public function test_get_item() {
    40         wp_set_current_user( $this->administrator );
     48        wp_set_current_user( self::$administrator );
    4149        $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    4250        $response = $this->server->dispatch( $request );
     
    6472
    6573    public function test_get_item_value_is_cast_to_type() {
    66         wp_set_current_user( $this->administrator );
     74        wp_set_current_user( self::$administrator );
    6775        update_option( 'posts_per_page', 'invalid_number' ); // this is cast to (int) 1
    6876        $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     
    7583
    7684    public function test_get_item_with_custom_setting() {
    77         wp_set_current_user( $this->administrator );
     85        wp_set_current_user( self::$administrator );
    7886
    7987        register_setting( 'somegroup', 'mycustomsetting', array(
     
    115123
    116124    public function test_get_item_with_filter() {
    117         wp_set_current_user( $this->administrator );
     125        wp_set_current_user( self::$administrator );
    118126
    119127        add_filter( 'rest_pre_get_setting', array( $this, 'get_setting_custom_callback' ), 10, 3 );
     
    156164
    157165    public function test_update_item() {
    158         wp_set_current_user( $this->administrator );
     166        wp_set_current_user( self::$administrator );
    159167        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    160168        $request->set_param( 'title', 'The new title!' );
     
    177185
    178186    public function test_update_item_with_filter() {
    179         wp_set_current_user( $this->administrator );
     187        wp_set_current_user( self::$administrator );
    180188
    181189        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
     
    208216
    209217    public function test_update_item_with_invalid_type() {
    210         wp_set_current_user( $this->administrator );
     218        wp_set_current_user( self::$administrator );
    211219        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    212220        $request->set_param( 'title', array( 'rendered' => 'This should fail.' ) );
     
    221229        update_option( 'posts_per_page', 9 );
    222230
    223         wp_set_current_user( $this->administrator );
     231        wp_set_current_user( self::$administrator );
    224232        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    225233        $request->set_param( 'posts_per_page', null );
     
    234242        update_option( 'posts_per_page', 9 );
    235243
    236         wp_set_current_user( $this->administrator );
     244        wp_set_current_user( self::$administrator );
    237245        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    238246        $request->set_param( 'default_ping_status', 'open&closed' );
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r38832 r38975  
    1111 */
    1212class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
     13    protected static $user;
     14    protected static $editor;
     15
     16    public static function wpSetUpBeforeClass( $factory ) {
     17        self::$user = $factory->user->create( array(
     18            'role' => 'administrator',
     19        ) );
     20        self::$editor = $factory->user->create( array(
     21            'role'       => 'editor',
     22            'user_email' => 'editor@example.com',
     23        ) );
     24    }
     25
     26    public static function wpTearDownAfterClass() {
     27        self::delete_user( self::$user );
     28        self::delete_user( self::$editor );
     29    }
     30
    1331    /**
    1432     * This function is run before each method
     
    1634    public function setUp() {
    1735        parent::setUp();
    18 
    19         $this->user = $this->factory->user->create( array(
    20             'role' => 'administrator',
    21         ) );
    22 
    23         $this->editor = $this->factory->user->create( array(
    24             'role'       => 'editor',
    25             'user_email' => 'editor@example.com',
    26         ) );
    27 
    2836        $this->endpoint = new WP_REST_Users_Controller();
    2937    }
     
    4755        $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    4856        // Single
    49         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users/' . $this->user );
     57        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users/' . self::$user );
    5058        $response = $this->server->dispatch( $request );
    5159        $data = $response->get_data();
     
    7684
    7785    public function test_get_items() {
    78         wp_set_current_user( $this->user );
     86        wp_set_current_user( self::$user );
    7987
    8088        $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     
    9199
    92100    public function test_get_items_with_edit_context() {
    93         wp_set_current_user( $this->user );
     101        wp_set_current_user( self::$user );
    94102
    95103        $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     
    114122
    115123        //test with a user logged in but without sufficient capabilities; capability in question: 'list_users'
    116         wp_set_current_user( $this->editor );
     124        wp_set_current_user( self::$editor );
    117125        $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    118126        $request->set_param( 'context', 'edit' );
     
    128136        $this->assertEquals( array(), $response->get_data() );
    129137
    130         $this->factory->post->create( array( 'post_author' => $this->editor ) );
    131         $this->factory->post->create( array( 'post_author' => $this->user, 'post_status' => 'draft' ) );
     138        $this->factory->post->create( array( 'post_author' => self::$editor ) );
     139        $this->factory->post->create( array( 'post_author' => self::$user, 'post_status' => 'draft' ) );
    132140
    133141        $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     
    149157     */
    150158    public function test_get_items_pagination_headers() {
    151         wp_set_current_user( $this->user );
     159        wp_set_current_user( self::$user );
    152160        // Start of the index, including the three existing users
    153161        for ( $i = 0; $i < 47; $i++ ) {
     
    211219
    212220    public function test_get_items_per_page() {
    213         wp_set_current_user( $this->user );
     221        wp_set_current_user( self::$user );
    214222        for ( $i = 0; $i < 20; $i++ ) {
    215223            $this->factory->user->create( array( 'display_name' => "User {$i}" ) );
     
    225233
    226234    public function test_get_items_page() {
    227         wp_set_current_user( $this->user );
     235        wp_set_current_user( self::$user );
    228236        for ( $i = 0; $i < 20; $i++ ) {
    229237            $this->factory->user->create( array( 'display_name' => "User {$i}" ) );
     
    243251
    244252    public function test_get_items_orderby_name() {
    245         wp_set_current_user( $this->user );
     253        wp_set_current_user( self::$user );
    246254        $low_id = $this->factory->user->create( array( 'display_name' => 'AAAAA' ) );
    247255        $mid_id = $this->factory->user->create( array( 'display_name' => 'NNNNN' ) );
     
    264272
    265273    public function test_get_items_orderby_url() {
    266         wp_set_current_user( $this->user );
     274        wp_set_current_user( self::$user );
    267275
    268276        $low_id = $this->factory->user->create( array( 'user_url' => 'http://a.com' ) );
     
    290298
    291299    public function test_get_items_orderby_slug() {
    292         wp_set_current_user( $this->user );
     300        wp_set_current_user( self::$user );
    293301
    294302        $high_id = $this->factory->user->create( array( 'user_nicename' => 'blogin' ) );
     
    316324
    317325    public function test_get_items_orderby_email() {
    318         wp_set_current_user( $this->user );
     326        wp_set_current_user( self::$user );
    319327
    320328        $high_id = $this->factory->user->create( array( 'user_email' => 'bemail@gmail.com' ) );
     
    357365
    358366    public function test_get_items_offset() {
    359         wp_set_current_user( $this->user );
     367        wp_set_current_user( self::$user );
    360368        // 2 users created in __construct(), plus default user
    361369        $this->factory->user->create();
     
    375383
    376384    public function test_get_items_include_query() {
    377         wp_set_current_user( $this->user );
     385        wp_set_current_user( self::$user );
    378386        $id1 = $this->factory->user->create();
    379387        $id2 = $this->factory->user->create();
     
    401409
    402410    public function test_get_items_exclude_query() {
    403         wp_set_current_user( $this->user );
     411        wp_set_current_user( self::$user );
    404412        $id1 = $this->factory->user->create();
    405413        $id2 = $this->factory->user->create();
     
    417425
    418426    public function test_get_items_search() {
    419         wp_set_current_user( $this->user );
     427        wp_set_current_user( self::$user );
    420428        $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    421429        $request->set_param( 'search', 'yololololo' );
     
    441449
    442450    public function test_get_items_slug_query() {
    443         wp_set_current_user( $this->user );
     451        wp_set_current_user( self::$user );
    444452        $this->factory->user->create( array( 'display_name' => 'foo', 'user_login' => 'bar' ) );
    445453        $id2 = $this->factory->user->create( array( 'display_name' => 'Moo', 'user_login' => 'foo' ) );
     
    454462    // 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.
    455463    public function test_get_items_roles() {
    456         wp_set_current_user( $this->user );
     464        wp_set_current_user( self::$user );
    457465        $tango = $this->factory->user->create( array( 'display_name' => 'tango', 'role' => 'subscriber' ) );
    458466        $yolo  = $this->factory->user->create( array( 'display_name' => 'yolo', 'role' => 'author' ) );
     
    473481        $response = $this->server->dispatch( $request );
    474482        $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 );
    475         wp_set_current_user( $this->editor );
     483        wp_set_current_user( self::$editor );
    476484        $request->set_param( 'roles', 'author' );
    477485        $response = $this->server->dispatch( $request );
     
    480488
    481489    public function test_get_items_invalid_roles() {
    482         wp_set_current_user( $this->user );
     490        wp_set_current_user( self::$user );
    483491        $lolz = $this->factory->user->create( array( 'display_name' => 'lolz', 'role' => 'author' ) );
    484492        $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     
    498506    public function test_get_item() {
    499507        $user_id = $this->factory->user->create();
    500         wp_set_current_user( $this->user );
     508        wp_set_current_user( self::$user );
    501509
    502510        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
     
    507515
    508516    public function test_prepare_item() {
    509         wp_set_current_user( $this->user );
     517        wp_set_current_user( self::$user );
    510518        $request = new WP_REST_Request;
    511519        $request->set_param( 'context', 'edit' );
     
    516524
    517525    public function test_get_user_avatar_urls() {
    518         wp_set_current_user( $this->user );
    519 
    520         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->editor ) );
     526        wp_set_current_user( self::$user );
     527
     528        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) );
    521529
    522530        $response = $this->server->dispatch( $request );
     
    527535        $this->assertArrayHasKey( 96,  $data['avatar_urls'] );
    528536
    529         $user = get_user_by( 'id', $this->editor );
     537        $user = get_user_by( 'id', self::$editor );
    530538        /**
    531539         * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar
     
    536544
    537545    public function test_get_user_invalid_id() {
    538         wp_set_current_user( $this->user );
     546        wp_set_current_user( self::$user );
    539547        $request = new WP_REST_Request( 'GET', '/wp/v2/users/100' );
    540548        $response = $this->server->dispatch( $request );
     
    544552
    545553    public function test_get_user_empty_capabilities() {
    546         wp_set_current_user( $this->user );
     554        wp_set_current_user( self::$user );
    547555        $this->allow_user_to_manage_multisite();
    548556
     
    560568
    561569    public function test_get_item_without_permission() {
    562         wp_set_current_user( $this->editor );
    563 
    564         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->user ) );
     570        wp_set_current_user( self::$editor );
     571
     572        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) );
    565573        $response = $this->server->dispatch( $request );
    566574
     
    623631
    624632    public function test_get_current_user() {
    625         wp_set_current_user( $this->user );
     633        wp_set_current_user( self::$user );
    626634
    627635        $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' );
     
    632640        $headers = $response->get_headers();
    633641        $this->assertArrayHasKey( 'Location', $headers );
    634         $this->assertEquals( rest_url( 'wp/v2/users/' . $this->user ), $headers['Location'] );
     642        $this->assertEquals( rest_url( 'wp/v2/users/' . self::$user ), $headers['Location'] );
    635643    }
    636644
     
    645653    public function test_create_item() {
    646654        $this->allow_user_to_manage_multisite();
    647         wp_set_current_user( $this->user );
     655        wp_set_current_user( self::$user );
    648656
    649657        $params = array(
     
    671679    public function test_json_create_user() {
    672680        $this->allow_user_to_manage_multisite();
    673         wp_set_current_user( $this->user );
     681        wp_set_current_user( self::$user );
    674682
    675683        $params = array(
     
    688696
    689697    public function test_create_user_without_permission() {
    690         wp_set_current_user( $this->editor );
     698        wp_set_current_user( self::$editor );
    691699
    692700        $params = array(
     
    706714    public function test_create_user_invalid_id() {
    707715        $this->allow_user_to_manage_multisite();
    708         wp_set_current_user( $this->user );
     716        wp_set_current_user( self::$user );
    709717
    710718        $params = array(
     
    725733    public function test_create_user_invalid_email() {
    726734        $this->allow_user_to_manage_multisite();
    727         wp_set_current_user( $this->user );
     735        wp_set_current_user( self::$user );
    728736
    729737        $params = array(
     
    743751    public function test_create_user_invalid_role() {
    744752        $this->allow_user_to_manage_multisite();
    745         wp_set_current_user( $this->user );
     753        wp_set_current_user( self::$user );
    746754
    747755        $params = array(
     
    769777        ));
    770778        $this->allow_user_to_manage_multisite();
    771         wp_set_current_user( $this->user );
     779        wp_set_current_user( self::$user );
    772780
    773781        $userdata = get_userdata( $user_id );
     
    804812        $user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) );
    805813        $this->allow_user_to_manage_multisite();
    806         wp_set_current_user( $this->user );
     814        wp_set_current_user( self::$user );
    807815
    808816        $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 );
     
    817825        $user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) );
    818826        $this->allow_user_to_manage_multisite();
    819         wp_set_current_user( $this->user );
     827        wp_set_current_user( self::$user );
    820828
    821829        $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 );
     
    830838        $user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) );
    831839        $this->allow_user_to_manage_multisite();
    832         wp_set_current_user( $this->user );
     840        wp_set_current_user( self::$user );
    833841
    834842        $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user2 );
     
    848856        ));
    849857        $this->allow_user_to_manage_multisite();
    850         wp_set_current_user( $this->user );
     858        wp_set_current_user( self::$user );
    851859
    852860        $params = array(
     
    883891        $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    884892
    885         wp_set_current_user( $this->user );
     893        wp_set_current_user( self::$user );
    886894        $this->allow_user_to_manage_multisite();
    887895
     
    901909
    902910    public function test_update_user_role_invalid_privilege_escalation() {
    903         wp_set_current_user( $this->editor );
    904 
    905         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->editor ) );
     911        wp_set_current_user( self::$editor );
     912
     913        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) );
    906914        $request->set_param( 'roles', array( 'administrator' ) );
    907915        $response = $this->server->dispatch( $request );
    908916
    909917        $this->assertErrorResponse( 'rest_cannot_edit_roles', $response, 403 );
    910         $user = get_userdata( $this->editor );
     918        $user = get_userdata( self::$editor );
    911919        $this->assertArrayHasKey( 'editor', $user->caps );
    912920        $this->assertArrayNotHasKey( 'administrator', $user->caps );
     
    955963
    956964    public function test_update_user_role_invalid_role() {
    957         wp_set_current_user( $this->user );
    958         $this->allow_user_to_manage_multisite();
    959 
    960         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->editor ) );
     965        wp_set_current_user( self::$user );
     966        $this->allow_user_to_manage_multisite();
     967
     968        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) );
    961969        $request->set_param( 'roles', array( 'BeSharp' ) );
    962970        $response = $this->server->dispatch( $request );
     
    964972        $this->assertErrorResponse( 'rest_user_invalid_role', $response, 400 );
    965973
    966         $user = get_userdata( $this->editor );
     974        $user = get_userdata( self::$editor );
    967975        $this->assertArrayHasKey( 'editor', $user->caps );
    968976        $this->assertArrayNotHasKey( 'BeSharp', $user->caps );
     
    970978
    971979    public function test_update_user_without_permission() {
    972         wp_set_current_user( $this->editor );
     980        wp_set_current_user( self::$editor );
    973981
    974982        $params = array(
     
    978986        );
    979987
    980         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->user ) );
     988        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$user ) );
    981989        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    982990        $request->set_body_params( $params );
     
    988996    public function test_update_user_invalid_id() {
    989997        $this->allow_user_to_manage_multisite();
    990         wp_set_current_user( $this->user );
     998        wp_set_current_user( self::$user );
    991999
    9921000        $params = array(
     
    9971005        );
    9981006
    999         $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $this->editor ) );
     1007        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) );
    10001008        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    10011009        $request->set_body_params( $params );
     
    10091017
    10101018        $this->allow_user_to_manage_multisite();
    1011         wp_set_current_user( $this->user );
     1019        wp_set_current_user( self::$user );
    10121020
    10131021        $userdata = get_userdata( $user_id ); // cache for later
     
    10251033
    10261034        $this->allow_user_to_manage_multisite();
    1027         wp_set_current_user( $this->user );
     1035        wp_set_current_user( self::$user );
    10281036
    10291037        $userdata = get_userdata( $user_id ); // cache for later
     
    10411049
    10421050        $this->allow_user_to_manage_multisite();
    1043         wp_set_current_user( $this->editor );
     1051        wp_set_current_user( self::$editor );
    10441052
    10451053        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
     
    10521060    public function test_delete_user_invalid_id() {
    10531061        $this->allow_user_to_manage_multisite();
    1054         wp_set_current_user( $this->user );
     1062        wp_set_current_user( self::$user );
    10551063
    10561064        $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/100' );
     
    10761084
    10771085        // Delete our test user, and reassign to the new author
    1078         wp_set_current_user( $this->user );
     1086        wp_set_current_user( self::$user );
    10791087        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    10801088        $request['force'] = true;
     
    10931101
    10941102        $this->allow_user_to_manage_multisite();
    1095         wp_set_current_user( $this->user );
     1103        wp_set_current_user( self::$user );
    10961104
    10971105        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
     
    12201228
    12211229        // Check for error on update.
    1222         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/users/%d', $this->user ) );
     1230        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/users/%d', self::$user ) );
    12231231        $request->set_body_params( array(
    12241232            'my_custom_int' => 'returnError',
     
    13091317
    13101318    protected function allow_user_to_manage_multisite() {
    1311         wp_set_current_user( $this->user );
     1319        wp_set_current_user( self::$user );
    13121320        $user = wp_get_current_user();
    13131321
Note: See TracChangeset for help on using the changeset viewer.