- Timestamp:
- 10/27/2021 06:42:13 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-users-controller.php
r51568 r51943 16 16 protected static $draft_editor; 17 17 protected static $subscriber; 18 protected static $author; 18 19 19 20 protected static $authors = array(); … … 54 55 'display_name' => 'subscriber', 55 56 'user_email' => 'subscriber@example.com', 57 ) 58 ); 59 self::$author = $factory->user->create( 60 array( 61 'display_name' => 'author', 62 'role' => 'author', 63 'user_email' => 'author@example.com', 56 64 ) 57 65 ); … … 108 116 109 117 // Set up users for pagination tests. 110 for ( $i = 0; $i < self::$total_users - 1 0; $i++ ) {118 for ( $i = 0; $i < self::$total_users - 11; $i++ ) { 111 119 self::$user_ids[] = $factory->user->create( 112 120 array( … … 122 130 self::delete_user( self::$editor ); 123 131 self::delete_user( self::$draft_editor ); 132 self::delete_user( self::$author ); 124 133 125 134 foreach ( self::$posts as $post ) { … … 184 193 $data = $response->get_data(); 185 194 $keys = array_keys( $data['endpoints'][0]['args'] ); 186 sort( $keys ); 187 $this->assertSame( 195 $this->assertEqualSets( 188 196 array( 189 197 'context', … … 196 204 'per_page', 197 205 'roles', 206 'capabilities', 198 207 'search', 199 208 'slug', … … 796 805 wp_set_current_user( self::$user ); 797 806 798 $tango = $this->factory->user->create(799 array(800 'display_name' => 'tango',801 'role' => 'subscriber',802 )803 );804 $yolo = $this->factory->user->create(805 array(806 'display_name' => 'yolo',807 'role' => 'author',808 )809 );810 811 807 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 812 808 $request->set_param( 'roles', 'author,subscriber' ); 813 809 $response = rest_get_server()->dispatch( $request ); 814 810 $data = $response->get_data(); 815 $this->assertCount( 3, $data );816 $this->assertSame( $tango, $data[1]['id'] );817 $this->assertSame( $yolo, $data[2]['id'] );811 $this->assertCount( 2, $data ); 812 $this->assertSame( self::$author, $data[0]['id'] ); 813 $this->assertSame( self::$subscriber, $data[1]['id'] ); 818 814 819 815 $request->set_param( 'roles', 'author' ); … … 821 817 $data = $response->get_data(); 822 818 $this->assertCount( 1, $data ); 823 $this->assertSame( $yolo, $data[0]['id'] );819 $this->assertSame( self::$author, $data[0]['id'] ); 824 820 825 821 wp_set_current_user( 0 ); … … 839 835 wp_set_current_user( self::$user ); 840 836 841 $lolz = $this->factory->user->create(842 array(843 'display_name' => 'lolz',844 'role' => 'author',845 )846 );847 848 837 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 849 838 $request->set_param( 'roles', 'ilovesteak,author' ); … … 851 840 $data = $response->get_data(); 852 841 $this->assertCount( 1, $data ); 853 $this->assertSame( $lolz, $data[0]['id'] );842 $this->assertSame( self::$author, $data[0]['id'] ); 854 843 855 844 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); … … 857 846 $response = rest_get_server()->dispatch( $request ); 858 847 $data = $response->get_data(); 859 $this->assertCount( 0, $data ); 860 $this->assertSame( array(), $data ); 861 } 862 848 $this->assertIsArray( $data ); 849 $this->assertEmpty( $data ); 850 } 851 852 /** 853 * @ticket 16841 854 */ 855 public function test_get_items_capabilities() { 856 wp_set_current_user( self::$user ); 857 858 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 859 $request->set_param( 'capabilities', 'edit_posts' ); 860 $response = rest_get_server()->dispatch( $request ); 861 $data = $response->get_data(); 862 863 $this->assertNotEmpty( $data ); 864 foreach ( $data as $user ) { 865 $this->assertTrue( user_can( $user['id'], 'edit_posts' ) ); 866 } 867 } 868 869 /** 870 * @ticket 16841 871 */ 872 public function test_get_items_capabilities_no_permission_no_user() { 873 wp_set_current_user( 0 ); 874 875 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 876 $request->set_param( 'capabilities', 'edit_posts' ); 877 $response = rest_get_server()->dispatch( $request ); 878 $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 ); 879 } 880 881 /** 882 * @ticket 16841 883 */ 884 public function test_get_items_capabilities_no_permission_editor() { 885 wp_set_current_user( self::$editor ); 886 887 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 888 $request->set_param( 'capabilities', 'edit_posts' ); 889 $response = rest_get_server()->dispatch( $request ); 890 $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 ); 891 } 892 893 /** 894 * @ticket 16841 895 */ 896 public function test_get_items_invalid_capabilities() { 897 wp_set_current_user( self::$user ); 898 899 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 900 $request->set_param( 'roles', 'ilovesteak,author' ); 901 $response = rest_get_server()->dispatch( $request ); 902 $data = $response->get_data(); 903 $this->assertCount( 1, $data ); 904 $this->assertSame( self::$author, $data[0]['id'] ); 905 906 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 907 $request->set_param( 'capabilities', 'steakisgood' ); 908 $response = rest_get_server()->dispatch( $request ); 909 $data = $response->get_data(); 910 $this->assertIsArray( $data ); 911 $this->assertEmpty( $data ); 912 } 913 914 /** 915 * @expectedDeprecated WP_User_Query 916 */ 863 917 public function test_get_items_who_author_query() { 864 918 wp_set_current_user( self::$superadmin );
Note: See TracChangeset
for help on using the changeset viewer.