| 104 | |
| 105 | /** |
| 106 | * @ticket 22212 |
| 107 | */ |
| 108 | function test_get_single_role_by_user_query() { |
| 109 | $this->factory->user->create_many( 2, array( |
| 110 | 'role' => 'subscriber' |
| 111 | ) ); |
| 112 | |
| 113 | $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); |
| 114 | $users = $wp_user_search->get_results(); |
| 115 | $this->assertEquals( 2, count( $users ) ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @ticket 22212 |
| 120 | */ |
| 121 | function test_get_multiple_roles_by_user_query() { |
| 122 | $this->factory->user->create_many( 2, array( |
| 123 | 'role' => 'subscriber' |
| 124 | ) ); |
| 125 | |
| 126 | $this->factory->user->create_many( 3, array( |
| 127 | 'role' => 'editor' |
| 128 | ) ); |
| 129 | |
| 130 | $wp_user_search = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) ); |
| 131 | $users = $wp_user_search->get_results(); |
| 132 | $this->assertEquals( 5, count( $users ) ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @ticket 22212 |
| 137 | */ |
| 138 | function test_get_single_role_by_string() { |
| 139 | $this->factory->user->create_many( 2, array( |
| 140 | 'role' => 'subscriber' |
| 141 | ) ); |
| 142 | |
| 143 | $users = get_users( array( |
| 144 | 'role' => 'subscriber' |
| 145 | ) ); |
| 146 | |
| 147 | $this->assertEquals( 2, count( $users ) ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @ticket 22212 |
| 152 | */ |
| 153 | function test_get_single_role_by_array() { |
| 154 | $this->factory->user->create_many( 2, array( |
| 155 | 'role' => 'subscriber' |
| 156 | ) ); |
| 157 | |
| 158 | $users = get_users( array( |
| 159 | 'role' => array( 'subscriber' ) |
| 160 | ) ); |
| 161 | |
| 162 | $this->assertEquals( 2, count( $users ) ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @ticket 22212 |
| 167 | */ |
| 168 | function test_get_multiple_roles() { |
| 169 | $this->factory->user->create_many( 2, array( |
| 170 | 'role' => 'subscriber' |
| 171 | ) ); |
| 172 | |
| 173 | $this->factory->user->create_many( 3, array( |
| 174 | 'role' => 'editor' |
| 175 | ) ); |
| 176 | |
| 177 | $this->factory->user->create_many( 2, array( |
| 178 | 'role' => 'administrator' |
| 179 | ) ); |
| 180 | |
| 181 | $users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) ); |
| 182 | $users = $users->get_results(); |
| 183 | |
| 184 | $this->assertEquals( 5, count( $users ) ); |
| 185 | foreach ( $users as $user ) { |
| 186 | $this->assertInstanceOf( 'WP_User', $user ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @ticket 22212 |
| 192 | */ |
| 193 | function test_get_multiple_roles_with_meta() { |
| 194 | |
| 195 | // Create administrator user + meta |
| 196 | $administrator_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
| 197 | update_user_meta( $administrator_id, 'mk1', 1 ); |
| 198 | update_user_meta( $administrator_id, 'mk2', 1 ); |
| 199 | |
| 200 | // Create editor user + meta |
| 201 | $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); |
| 202 | update_user_meta( $editor_id, 'mk1', 1 ); |
| 203 | update_user_meta( $editor_id, 'mk2', 2 ); |
| 204 | |
| 205 | // Create subscriber user + meta |
| 206 | $subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); |
| 207 | update_user_meta( $subscriber_id, 'mk1', 1 ); |
| 208 | update_user_meta( $subscriber_id, 'mk2', 1 ); |
| 209 | |
| 210 | // Fetch users |
| 211 | $users = get_users( array( |
| 212 | 'role' => array( 'administrator', 'editor', 'subscriber' ), |
| 213 | 'meta_query' => array( |
| 214 | 'relation' => 'AND', |
| 215 | array( |
| 216 | 'key' => 'mk1', |
| 217 | 'value' => '1', |
| 218 | 'compare' => "=", |
| 219 | 'type' => 'numeric' |
| 220 | ), |
| 221 | array( |
| 222 | 'key' => 'mk2', |
| 223 | 'value' => '2', |
| 224 | 'compare' => "=", |
| 225 | 'type' => 'numeric' |
| 226 | ) |
| 227 | ) |
| 228 | ) ); |
| 229 | |
| 230 | // Check results |
| 231 | $this->assertEquals( 1, count( $users ) ); |
| 232 | |
| 233 | } |
| 234 | |