WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/27/2016 02:56:28 AM (5 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.