| | 140 | |
| | 141 | /** |
| | 142 | * Test 'NOT EXISTS' user meta query compare with OR relations. |
| | 143 | * |
| | 144 | * @ticket 23849 |
| | 145 | */ |
| | 146 | public function test_meta_query_compare_not_exists() { |
| | 147 | |
| | 148 | // Set the 'test_meta_not_exists' key for just the first user. |
| | 149 | update_user_meta( $this->user_id, 'test_meta_not_exists', 1 ); |
| | 150 | |
| | 151 | // A meta query for the users with that key should return one user. |
| | 152 | $query_1 = new WP_User_Query( |
| | 153 | array( |
| | 154 | 'meta_query' => array( |
| | 155 | array( |
| | 156 | 'key' => 'test_meta_not_exists', |
| | 157 | 'compare' => 'EXISTS', |
| | 158 | ), |
| | 159 | ), |
| | 160 | ) |
| | 161 | ); |
| | 162 | |
| | 163 | $this->assertEquals( 1, $query_1->get_total() ); |
| | 164 | |
| | 165 | // Run the not exists query with AND relation. |
| | 166 | $query_2 = new WP_User_Query( |
| | 167 | array( |
| | 168 | 'meta_query' => array( |
| | 169 | 'relation' => 'AND', |
| | 170 | array( |
| | 171 | 'key' => 'test_meta_not_exists', |
| | 172 | 'compare' => 'NOT EXISTS', |
| | 173 | ), |
| | 174 | ), |
| | 175 | ) |
| | 176 | ); |
| | 177 | |
| | 178 | // We should find one user (the current user which won't have the meta key). |
| | 179 | $this->assertEquals( 1, $query_2->get_total() ); |
| | 180 | |
| | 181 | // Now run the not exists query with OR relation. |
| | 182 | $query_3 = new WP_User_Query( |
| | 183 | array( |
| | 184 | 'meta_query' => array( |
| | 185 | 'relation' => 'OR', |
| | 186 | array( |
| | 187 | 'key' => 'test_meta_not_exists', |
| | 188 | 'compare' => 'NOT EXISTS', |
| | 189 | ), |
| | 190 | ), |
| | 191 | ) |
| | 192 | ); |
| | 193 | |
| | 194 | // We should find one user. |
| | 195 | $this->assertEquals( 1, $query_3->get_total() ); |
| | 196 | } |
| | 197 | |
| | 198 | /** |
| | 199 | * Test using user meta query when specifying a role. |
| | 200 | * |
| | 201 | * @ticket 23849 |
| | 202 | */ |
| | 203 | public function test_meta_query_with_role() { |
| | 204 | |
| | 205 | $user_id = $this->factory->user->create( array( 'role' => 'author' ) ); |
| | 206 | |
| | 207 | update_user_meta( $user_id, 'test_meta_not_exists', 'off' ); |
| | 208 | |
| | 209 | $args = array( |
| | 210 | 'role' => 'Author', |
| | 211 | 'number' => 100, |
| | 212 | 'offset' => 0, |
| | 213 | 'meta_query' => array( |
| | 214 | 'relation' => 'OR', |
| | 215 | array( |
| | 216 | 'key' => 'test_meta_not_exists', |
| | 217 | 'compare' => 'NOT EXISTS', |
| | 218 | ), |
| | 219 | array( |
| | 220 | 'key' => 'test_meta_not_exists', |
| | 221 | 'value' => 'off', |
| | 222 | 'compare' => '!=', |
| | 223 | ), |
| | 224 | ), |
| | 225 | ); |
| | 226 | |
| | 227 | $this->assertEquals( 1, ( new WP_User_Query( $args ) )->get_total() ); |
| | 228 | |
| | 229 | update_user_meta( $user_id, 'test_meta_not_exists', 'on' ); |
| | 230 | |
| | 231 | $this->assertEquals( 2, ( new WP_User_Query( $args ) )->get_total() ); |
| | 232 | } |