WordPress.org

Make WordPress Core

Changeset 39177


Ignore:
Timestamp:
11/09/2016 01:34:56 AM (3 years ago)
Author:
rmccue
Message:

REST API: Fire correct hooks when creating users on multiste.

add_user_to_blog() is now called, ensuring the correct hooks are called, along with setting the primary blog and clearing relevant caches.

Props jeremyfelt.
Fixes #38526.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r39169 r39177  
    435435                return $user_id;
    436436            }
     437
     438            add_user_to_blog( get_site()->id, $user_id, '' );
    437439        } else {
    438440            $user_id = wp_insert_user( $user );
     
    558560
    559561        $user = get_user_by( 'id', $id );
     562
     563        if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
     564            add_user_to_blog( get_current_blog_id(), $id, '' );
     565        }
    560566
    561567        if ( ! empty( $request['roles'] ) ) {
  • trunk/tests/phpunit/includes/testcase-rest-controller.php

    r38832 r39177  
    4141
    4242    public function filter_rest_url_for_leading_slash( $url, $path ) {
     43        if ( is_multisite() ) {
     44            return $url;
     45        }
     46
    4347        // Make sure path for rest_url has a leading slash for proper resolution.
    44         $this->assertTrue( 0 === strpos( $path, '/' ) );
     48        $this->assertTrue( 0 === strpos( $path, '/' ), 'REST API URL should have a leading slash.' );
    4549
    4650        return $url;
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r39169 r39177  
    1313    protected static $user;
    1414    protected static $editor;
     15    protected static $site;
    1516
    1617    public static function wpSetUpBeforeClass( $factory ) {
     
    2223            'user_email' => 'editor@example.com',
    2324        ) );
     25
     26        if ( is_multisite() ) {
     27            self::$site = $factory->blog->create( array( 'domain' => 'rest.wordpress.org', 'path' => '/' ) );
     28        }
    2429    }
    2530
     
    2732        self::delete_user( self::$user );
    2833        self::delete_user( self::$editor );
     34
     35        if ( is_multisite() ) {
     36            wpmu_delete_blog( self::$site, true );
     37        }
    2938    }
    3039
     
    705714        $this->assertEquals( array( 'editor' ), $data['roles'] );
    706715        $this->check_add_edit_user_response( $response );
     716    }
     717
     718    public function test_create_new_network_user_on_site_does_not_add_user_to_sub_site() {
     719        if ( ! is_multisite() ) {
     720            $this->markTestSkipped( 'Test requires multisite.' );
     721        }
     722
     723        $this->allow_user_to_manage_multisite();
     724
     725        $params = array(
     726            'username' => 'testuser123',
     727            'password' => 'testpassword',
     728            'email'    => 'test@example.com',
     729            'name'     => 'Test User 123',
     730            'roles'    => array( 'editor' ),
     731        );
     732
     733        $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
     734        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     735        $request->set_body_params( $params );
     736        $response = $this->server->dispatch( $request );
     737        $data = $response->get_data();
     738        $user_id = $data['id'];
     739
     740        $user_is_member = is_user_member_of_blog( $user_id, self::$site );
     741
     742        wpmu_delete_user( $user_id );
     743
     744        $this->assertFalse( $user_is_member );
     745    }
     746
     747    public function test_create_new_network_user_on_sub_site_adds_user_to_site() {
     748        if ( ! is_multisite() ) {
     749            $this->markTestSkipped( 'Test requires multisite.' );
     750        }
     751
     752        $this->allow_user_to_manage_multisite();
     753
     754        $params = array(
     755            'username' => 'testuser123',
     756            'password' => 'testpassword',
     757            'email'    => 'test@example.com',
     758            'name'     => 'Test User 123',
     759            'roles'    => array( 'editor' ),
     760        );
     761
     762        switch_to_blog( self::$site );
     763
     764        $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
     765        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     766        $request->set_body_params( $params );
     767        $response = $this->server->dispatch( $request );
     768        $data = $response->get_data();
     769        $user_id = $data['id'];
     770
     771        restore_current_blog();
     772
     773        $user_is_member = is_user_member_of_blog( $user_id, self::$site );
     774
     775        wpmu_delete_user( $user_id );
     776
     777        $this->assertTrue( $user_is_member );
     778    }
     779
     780    public function test_create_existing_network_user_on_sub_site_has_error() {
     781        if ( ! is_multisite() ) {
     782            $this->markTestSkipped( 'Test requires multisite.' );
     783        }
     784
     785        $this->allow_user_to_manage_multisite();
     786
     787        $params = array(
     788            'username' => 'testuser123',
     789            'password' => 'testpassword',
     790            'email'    => 'test@example.com',
     791            'name'     => 'Test User 123',
     792            'roles'    => array( 'editor' ),
     793        );
     794
     795        $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
     796        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     797        $request->set_body_params( $params );
     798        $response = $this->server->dispatch( $request );
     799        $data = $response->get_data();
     800        $user_id = $data['id'];
     801
     802        switch_to_blog( self::$site );
     803
     804        $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
     805        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     806        $request->set_body_params( $params );
     807        $switched_response = $this->server->dispatch( $request );
     808
     809        restore_current_blog();
     810
     811        wpmu_delete_user( $user_id );
     812
     813        $this->assertErrorResponse( 'user_name', $switched_response );
     814    }
     815
     816    public function test_update_existing_network_user_on_sub_site_adds_user_to_site() {
     817        if ( ! is_multisite() ) {
     818            $this->markTestSkipped( 'Test requires multisite.' );
     819        }
     820
     821        $this->allow_user_to_manage_multisite();
     822
     823        $params = array(
     824            'username' => 'testuser123',
     825            'password' => 'testpassword',
     826            'email'    => 'test@example.com',
     827            'name'     => 'Test User 123',
     828            'roles'    => array( 'editor' ),
     829        );
     830
     831        $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
     832        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     833        $request->set_body_params( $params );
     834        $response = $this->server->dispatch( $request );
     835        $data = $response->get_data();
     836        $user_id = $data['id'];
     837
     838        switch_to_blog( self::$site );
     839
     840        $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user_id );
     841        $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     842        $request->set_body_params( $params );
     843        $this->server->dispatch( $request );
     844
     845        restore_current_blog();
     846
     847        $user_is_member = is_user_member_of_blog( $user_id, self::$site );
     848
     849        wpmu_delete_user( $user_id );
     850
     851        $this->assertTrue( $user_is_member );
    707852    }
    708853
Note: See TracChangeset for help on using the changeset viewer.