| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group BP_XProfile_Query |
| | 5 | */ |
| | 6 | class BP_XProfile_Query_Tests extends BP_UnitTestCase { |
| | 7 | protected $users = array(); |
| | 8 | protected $fields = array(); |
| | 9 | protected $userdata = array(); |
| | 10 | |
| | 11 | protected $bpbd_query; |
| | 12 | |
| | 13 | public function setUp() { |
| | 14 | parent::setUp(); |
| | 15 | |
| | 16 | // fields |
| | 17 | $g = $this->factory->xprofile_group->create(); |
| | 18 | for ( $i = 1; $i <= 3; $i++ ) { |
| | 19 | $this->fields[ $i ] = $this->factory->xprofile_field->create( array( |
| | 20 | 'field_group_id' => $g, |
| | 21 | 'type' => 'textarea', |
| | 22 | ) ); |
| | 23 | } |
| | 24 | |
| | 25 | // users |
| | 26 | $now = time(); |
| | 27 | for ( $i = 1; $i <= 5; $i++ ) { |
| | 28 | $this->users[ $i ] = $this->create_user( array( |
| | 29 | 'last_activity' => date( 'Y-m-d H:i:s', $now - 60 * 60 * $i ), // $i hours ago |
| | 30 | ) ); |
| | 31 | } |
| | 32 | } |
| | 33 | |
| | 34 | public function tearDown() { |
| | 35 | parent::tearDown(); |
| | 36 | } |
| | 37 | |
| | 38 | /** |
| | 39 | * @group BP_User_Query |
| | 40 | */ |
| | 41 | public function test_query_single_clause_default_compare() { |
| | 42 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 43 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 44 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 45 | |
| | 46 | $q = new BP_User_Query( array( |
| | 47 | 'xprofile_query' => array( |
| | 48 | array( |
| | 49 | 'field_id' => $this->fields[1], |
| | 50 | 'value' => 'Bar', |
| | 51 | ), |
| | 52 | ), |
| | 53 | ) ); |
| | 54 | |
| | 55 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 56 | |
| | 57 | $expected = array( |
| | 58 | $this->users[2] => $this->users[2], |
| | 59 | ); |
| | 60 | |
| | 61 | $this->assertEquals( $expected, $found ); |
| | 62 | } |
| | 63 | |
| | 64 | /** |
| | 65 | * @group BP_User_Query |
| | 66 | */ |
| | 67 | public function test_query_single_clause_compare_equals() { |
| | 68 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 69 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 70 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 71 | |
| | 72 | $q = new BP_User_Query( array( |
| | 73 | 'xprofile_query' => array( |
| | 74 | array( |
| | 75 | 'field_id' => $this->fields[1], |
| | 76 | 'value' => 'Bar', |
| | 77 | 'compare' => '=', |
| | 78 | ), |
| | 79 | ), |
| | 80 | ) ); |
| | 81 | |
| | 82 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 83 | |
| | 84 | $expected = array( |
| | 85 | $this->users[2] => $this->users[2], |
| | 86 | ); |
| | 87 | |
| | 88 | $this->assertEquals( $expected, $found ); |
| | 89 | } |
| | 90 | |
| | 91 | /** |
| | 92 | * @group BP_User_Query |
| | 93 | */ |
| | 94 | public function test_query_single_clause_compare_notequals() { |
| | 95 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 96 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 97 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 98 | |
| | 99 | $q = new BP_User_Query( array( |
| | 100 | 'xprofile_query' => array( |
| | 101 | array( |
| | 102 | 'field_id' => $this->fields[1], |
| | 103 | 'value' => 'Bar', |
| | 104 | 'compare' => '!=', |
| | 105 | ), |
| | 106 | ), |
| | 107 | ) ); |
| | 108 | |
| | 109 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 110 | |
| | 111 | $expected = array( |
| | 112 | $this->users[1] => $this->users[1], |
| | 113 | $this->users[3] => $this->users[3], |
| | 114 | ); |
| | 115 | |
| | 116 | $this->assertEquals( $expected, $found ); |
| | 117 | } |
| | 118 | |
| | 119 | /** |
| | 120 | * @group BP_User_Query |
| | 121 | */ |
| | 122 | public function test_query_single_clause_compare_greaterthan_type_numeric() { |
| | 123 | xprofile_set_field_data( $this->fields[1], $this->users[1], '10' ); |
| | 124 | xprofile_set_field_data( $this->fields[1], $this->users[2], '100' ); |
| | 125 | xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' ); |
| | 126 | |
| | 127 | $q = new BP_User_Query( array( |
| | 128 | 'xprofile_query' => array( |
| | 129 | array( |
| | 130 | 'field_id' => $this->fields[1], |
| | 131 | 'value' => '101', |
| | 132 | 'compare' => '>', |
| | 133 | 'type' => 'NUMERIC', |
| | 134 | ), |
| | 135 | ), |
| | 136 | ) ); |
| | 137 | |
| | 138 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 139 | |
| | 140 | $expected = array( |
| | 141 | $this->users[3] => $this->users[3], |
| | 142 | ); |
| | 143 | |
| | 144 | $this->assertEquals( $expected, $found ); |
| | 145 | } |
| | 146 | |
| | 147 | /** |
| | 148 | * @group BP_User_Query |
| | 149 | */ |
| | 150 | public function test_query_single_clause_compare_greaterthanequalto_type_numeric() { |
| | 151 | xprofile_set_field_data( $this->fields[1], $this->users[1], '10' ); |
| | 152 | xprofile_set_field_data( $this->fields[1], $this->users[2], '100' ); |
| | 153 | xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' ); |
| | 154 | |
| | 155 | $q = new BP_User_Query( array( |
| | 156 | 'xprofile_query' => array( |
| | 157 | array( |
| | 158 | 'field_id' => $this->fields[1], |
| | 159 | 'value' => '100', |
| | 160 | 'compare' => '>=', |
| | 161 | 'type' => 'NUMERIC', |
| | 162 | ), |
| | 163 | ), |
| | 164 | ) ); |
| | 165 | |
| | 166 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 167 | |
| | 168 | $expected = array( |
| | 169 | $this->users[2] => $this->users[2], |
| | 170 | $this->users[3] => $this->users[3], |
| | 171 | ); |
| | 172 | |
| | 173 | $this->assertEquals( $expected, $found ); |
| | 174 | } |
| | 175 | |
| | 176 | /** |
| | 177 | * @group BP_User_Query |
| | 178 | */ |
| | 179 | public function test_query_single_clause_compare_lessthan_type_numeric() { |
| | 180 | xprofile_set_field_data( $this->fields[1], $this->users[1], '10' ); |
| | 181 | xprofile_set_field_data( $this->fields[1], $this->users[2], '100' ); |
| | 182 | xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' ); |
| | 183 | |
| | 184 | $q = new BP_User_Query( array( |
| | 185 | 'xprofile_query' => array( |
| | 186 | array( |
| | 187 | 'field_id' => $this->fields[1], |
| | 188 | 'value' => '100', |
| | 189 | 'compare' => '<', |
| | 190 | 'type' => 'NUMERIC', |
| | 191 | ), |
| | 192 | ), |
| | 193 | ) ); |
| | 194 | |
| | 195 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 196 | |
| | 197 | $expected = array( |
| | 198 | $this->users[1] => $this->users[1], |
| | 199 | ); |
| | 200 | |
| | 201 | $this->assertEquals( $expected, $found ); |
| | 202 | } |
| | 203 | |
| | 204 | /** |
| | 205 | * @group BP_User_Query |
| | 206 | */ |
| | 207 | public function test_query_single_clause_compare_lessthanequalto_than_type_numeric() { |
| | 208 | xprofile_set_field_data( $this->fields[1], $this->users[1], '10' ); |
| | 209 | xprofile_set_field_data( $this->fields[1], $this->users[2], '100' ); |
| | 210 | xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' ); |
| | 211 | |
| | 212 | $q = new BP_User_Query( array( |
| | 213 | 'xprofile_query' => array( |
| | 214 | array( |
| | 215 | 'field_id' => $this->fields[1], |
| | 216 | 'value' => '100', |
| | 217 | 'compare' => '<=', |
| | 218 | 'type' => 'NUMERIC', |
| | 219 | ), |
| | 220 | ), |
| | 221 | ) ); |
| | 222 | |
| | 223 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 224 | |
| | 225 | $expected = array( |
| | 226 | $this->users[1] => $this->users[1], |
| | 227 | $this->users[2] => $this->users[2], |
| | 228 | ); |
| | 229 | |
| | 230 | $this->assertEquals( $expected, $found ); |
| | 231 | } |
| | 232 | |
| | 233 | /** |
| | 234 | * @group BP_User_Query |
| | 235 | */ |
| | 236 | public function test_query_single_clause_compare_like() { |
| | 237 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 238 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 239 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 240 | |
| | 241 | $q = new BP_User_Query( array( |
| | 242 | 'xprofile_query' => array( |
| | 243 | array( |
| | 244 | 'field_id' => $this->fields[1], |
| | 245 | 'value' => 'Bar', |
| | 246 | 'compare' => 'LIKE', |
| | 247 | ), |
| | 248 | ), |
| | 249 | ) ); |
| | 250 | |
| | 251 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 252 | |
| | 253 | $expected = array( |
| | 254 | $this->users[2] => $this->users[2], |
| | 255 | $this->users[3] => $this->users[3], |
| | 256 | ); |
| | 257 | |
| | 258 | $this->assertEquals( $expected, $found ); |
| | 259 | } |
| | 260 | |
| | 261 | /** |
| | 262 | * @group BP_User_Query |
| | 263 | */ |
| | 264 | public function test_query_single_clause_compare_notlike() { |
| | 265 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 266 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 267 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 268 | |
| | 269 | $q = new BP_User_Query( array( |
| | 270 | 'xprofile_query' => array( |
| | 271 | array( |
| | 272 | 'field_id' => $this->fields[1], |
| | 273 | 'value' => 'Bar', |
| | 274 | 'compare' => 'NOT LIKE', |
| | 275 | ), |
| | 276 | ), |
| | 277 | ) ); |
| | 278 | |
| | 279 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 280 | |
| | 281 | $expected = array( |
| | 282 | $this->users[1] => $this->users[1], |
| | 283 | ); |
| | 284 | |
| | 285 | $this->assertEquals( $expected, $found ); |
| | 286 | } |
| | 287 | |
| | 288 | /** |
| | 289 | * @group BP_User_Query |
| | 290 | */ |
| | 291 | public function test_query_single_clause_compare_in_single_value() { |
| | 292 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 293 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 294 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 295 | |
| | 296 | $q = new BP_User_Query( array( |
| | 297 | 'xprofile_query' => array( |
| | 298 | array( |
| | 299 | 'field_id' => $this->fields[1], |
| | 300 | 'value' => 'Bar', |
| | 301 | 'compare' => 'IN', |
| | 302 | ), |
| | 303 | ), |
| | 304 | ) ); |
| | 305 | |
| | 306 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 307 | |
| | 308 | $expected = array( |
| | 309 | $this->users[2] => $this->users[2], |
| | 310 | ); |
| | 311 | |
| | 312 | $this->assertEquals( $expected, $found ); |
| | 313 | } |
| | 314 | |
| | 315 | /** |
| | 316 | * @group BP_User_Query |
| | 317 | */ |
| | 318 | public function test_query_single_clause_compare_in() { |
| | 319 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 320 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 321 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 322 | |
| | 323 | $q = new BP_User_Query( array( |
| | 324 | 'xprofile_query' => array( |
| | 325 | array( |
| | 326 | 'field_id' => $this->fields[1], |
| | 327 | 'value' => array( 'Foo', 'Bar' ), |
| | 328 | 'compare' => 'IN', |
| | 329 | ), |
| | 330 | ), |
| | 331 | ) ); |
| | 332 | |
| | 333 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 334 | |
| | 335 | $expected = array( |
| | 336 | $this->users[1] => $this->users[1], |
| | 337 | $this->users[2] => $this->users[2], |
| | 338 | ); |
| | 339 | |
| | 340 | $this->assertEquals( $expected, $found ); |
| | 341 | } |
| | 342 | |
| | 343 | /** |
| | 344 | * @group BP_User_Query |
| | 345 | */ |
| | 346 | public function test_query_single_clause_compare_in_comma_sep_value() { |
| | 347 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 348 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 349 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 350 | |
| | 351 | $q = new BP_User_Query( array( |
| | 352 | 'xprofile_query' => array( |
| | 353 | array( |
| | 354 | 'field_id' => $this->fields[1], |
| | 355 | 'value' => 'Foo, Bar', |
| | 356 | 'compare' => 'IN', |
| | 357 | ), |
| | 358 | ), |
| | 359 | ) ); |
| | 360 | |
| | 361 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 362 | |
| | 363 | $expected = array( |
| | 364 | $this->users[1] => $this->users[1], |
| | 365 | $this->users[2] => $this->users[2], |
| | 366 | ); |
| | 367 | |
| | 368 | $this->assertEquals( $expected, $found ); |
| | 369 | } |
| | 370 | /** |
| | 371 | * @group BP_User_Query |
| | 372 | */ |
| | 373 | public function test_query_single_clause_compare_notin_single_value() { |
| | 374 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 375 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 376 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 377 | |
| | 378 | $q = new BP_User_Query( array( |
| | 379 | 'xprofile_query' => array( |
| | 380 | array( |
| | 381 | 'field_id' => $this->fields[1], |
| | 382 | 'value' => 'Bar', |
| | 383 | 'compare' => 'NOT IN', |
| | 384 | ), |
| | 385 | ), |
| | 386 | ) ); |
| | 387 | |
| | 388 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 389 | |
| | 390 | $expected = array( |
| | 391 | $this->users[1] => $this->users[1], |
| | 392 | $this->users[3] => $this->users[3], |
| | 393 | ); |
| | 394 | |
| | 395 | $this->assertEquals( $expected, $found ); |
| | 396 | } |
| | 397 | |
| | 398 | /** |
| | 399 | * @group BP_User_Query |
| | 400 | */ |
| | 401 | public function test_query_single_clause_compare_notin() { |
| | 402 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 403 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 404 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 405 | |
| | 406 | $q = new BP_User_Query( array( |
| | 407 | 'xprofile_query' => array( |
| | 408 | array( |
| | 409 | 'field_id' => $this->fields[1], |
| | 410 | 'value' => array( 'Foo', 'Bar' ), |
| | 411 | 'compare' => 'NOT IN', |
| | 412 | ), |
| | 413 | ), |
| | 414 | ) ); |
| | 415 | |
| | 416 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 417 | |
| | 418 | $expected = array( |
| | 419 | $this->users[3] => $this->users[3], |
| | 420 | ); |
| | 421 | |
| | 422 | $this->assertEquals( $expected, $found ); |
| | 423 | } |
| | 424 | |
| | 425 | /** |
| | 426 | * @group BP_User_Query |
| | 427 | */ |
| | 428 | public function test_query_single_clause_compare_between() { |
| | 429 | xprofile_set_field_data( $this->fields[1], $this->users[1], '10' ); |
| | 430 | xprofile_set_field_data( $this->fields[1], $this->users[2], '100' ); |
| | 431 | xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' ); |
| | 432 | |
| | 433 | $q = new BP_User_Query( array( |
| | 434 | 'xprofile_query' => array( |
| | 435 | array( |
| | 436 | 'field_id' => $this->fields[1], |
| | 437 | 'value' => array( '99', '101' ), |
| | 438 | 'compare' => 'BETWEEN', |
| | 439 | 'type' => 'NUMERIC', |
| | 440 | ), |
| | 441 | ), |
| | 442 | ) ); |
| | 443 | |
| | 444 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 445 | |
| | 446 | $expected = array( |
| | 447 | $this->users[2] => $this->users[2], |
| | 448 | ); |
| | 449 | |
| | 450 | $this->assertEquals( $expected, $found ); |
| | 451 | } |
| | 452 | |
| | 453 | /** |
| | 454 | * @group BP_User_Query |
| | 455 | */ |
| | 456 | public function test_query_single_clause_compare_notbetween() { |
| | 457 | xprofile_set_field_data( $this->fields[1], $this->users[1], '10' ); |
| | 458 | xprofile_set_field_data( $this->fields[1], $this->users[2], '100' ); |
| | 459 | xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' ); |
| | 460 | |
| | 461 | $q = new BP_User_Query( array( |
| | 462 | 'xprofile_query' => array( |
| | 463 | array( |
| | 464 | 'field_id' => $this->fields[1], |
| | 465 | 'value' => array( '99', '101' ), |
| | 466 | 'compare' => 'NOT BETWEEN', |
| | 467 | 'type' => 'NUMERIC', |
| | 468 | ), |
| | 469 | ), |
| | 470 | ) ); |
| | 471 | |
| | 472 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 473 | |
| | 474 | $expected = array( |
| | 475 | $this->users[1] => $this->users[1], |
| | 476 | $this->users[3] => $this->users[3], |
| | 477 | ); |
| | 478 | |
| | 479 | $this->assertEquals( $expected, $found ); |
| | 480 | } |
| | 481 | |
| | 482 | /** |
| | 483 | * @group BP_User_Query |
| | 484 | */ |
| | 485 | public function test_query_single_clause_compare_regexp() { |
| | 486 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 487 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Foo Bar' ); |
| | 488 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Fooooo Bar' ); |
| | 489 | |
| | 490 | $q = new BP_User_Query( array( |
| | 491 | 'xprofile_query' => array( |
| | 492 | array( |
| | 493 | 'field_id' => $this->fields[1], |
| | 494 | 'value' => '^Fo+ Bar', |
| | 495 | 'compare' => 'REGEXP', |
| | 496 | ), |
| | 497 | ), |
| | 498 | ) ); |
| | 499 | |
| | 500 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 501 | |
| | 502 | $expected = array( |
| | 503 | $this->users[2] => $this->users[2], |
| | 504 | $this->users[3] => $this->users[3], |
| | 505 | ); |
| | 506 | |
| | 507 | $this->assertEquals( $expected, $found ); |
| | 508 | } |
| | 509 | |
| | 510 | /** |
| | 511 | * @group BP_User_Query |
| | 512 | */ |
| | 513 | public function test_query_single_clause_compare_notregexp() { |
| | 514 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 515 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Foo Bar' ); |
| | 516 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Fooooo Bar' ); |
| | 517 | |
| | 518 | $q = new BP_User_Query( array( |
| | 519 | 'xprofile_query' => array( |
| | 520 | array( |
| | 521 | 'field_id' => $this->fields[1], |
| | 522 | 'value' => '^Fo+ Bar', |
| | 523 | 'compare' => 'NOT REGEXP', |
| | 524 | ), |
| | 525 | ), |
| | 526 | ) ); |
| | 527 | |
| | 528 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 529 | |
| | 530 | $expected = array( |
| | 531 | $this->users[1] => $this->users[1], |
| | 532 | ); |
| | 533 | |
| | 534 | $this->assertEquals( $expected, $found ); |
| | 535 | } |
| | 536 | |
| | 537 | /** |
| | 538 | * @group BP_User_Query |
| | 539 | */ |
| | 540 | public function test_query_multiple_clauses_default_compare_default_relation() { |
| | 541 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 542 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 543 | xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' ); |
| | 544 | xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' ); |
| | 545 | |
| | 546 | $q = new BP_User_Query( array( |
| | 547 | 'xprofile_query' => array( |
| | 548 | array( |
| | 549 | 'field_id' => $this->fields[1], |
| | 550 | 'value' => 'Bar', |
| | 551 | ), |
| | 552 | array( |
| | 553 | 'field_id' => $this->fields[2], |
| | 554 | 'value' => 'Foo', |
| | 555 | ), |
| | 556 | ), |
| | 557 | ) ); |
| | 558 | |
| | 559 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 560 | |
| | 561 | $expected = array( |
| | 562 | $this->users[2] => $this->users[2], |
| | 563 | ); |
| | 564 | |
| | 565 | $this->assertEquals( $expected, $found ); |
| | 566 | } |
| | 567 | |
| | 568 | /** |
| | 569 | * @group BP_User_Query |
| | 570 | */ |
| | 571 | public function test_query_multiple_clauses_default_compare_relation_and() { |
| | 572 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 573 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 574 | xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' ); |
| | 575 | xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' ); |
| | 576 | |
| | 577 | $q = new BP_User_Query( array( |
| | 578 | 'xprofile_query' => array( |
| | 579 | 'relation' => 'AND', |
| | 580 | array( |
| | 581 | 'field_id' => $this->fields[1], |
| | 582 | 'value' => 'Bar', |
| | 583 | ), |
| | 584 | array( |
| | 585 | 'field_id' => $this->fields[2], |
| | 586 | 'value' => 'Foo', |
| | 587 | ), |
| | 588 | ), |
| | 589 | ) ); |
| | 590 | |
| | 591 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 592 | |
| | 593 | $expected = array( |
| | 594 | $this->users[2] => $this->users[2], |
| | 595 | ); |
| | 596 | |
| | 597 | $this->assertEquals( $expected, $found ); |
| | 598 | } |
| | 599 | |
| | 600 | /** |
| | 601 | * @group BP_User_Query |
| | 602 | */ |
| | 603 | public function test_query_multiple_clauses_default_compare_relation_or() { |
| | 604 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 605 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 606 | xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' ); |
| | 607 | xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' ); |
| | 608 | xprofile_set_field_data( $this->fields[2], $this->users[3], 'Barry' ); |
| | 609 | |
| | 610 | $q = new BP_User_Query( array( |
| | 611 | 'xprofile_query' => array( |
| | 612 | 'relation' => 'OR', |
| | 613 | array( |
| | 614 | 'field_id' => $this->fields[1], |
| | 615 | 'value' => 'Foo', |
| | 616 | ), |
| | 617 | array( |
| | 618 | 'field_id' => $this->fields[2], |
| | 619 | 'value' => 'Barry', |
| | 620 | ), |
| | 621 | ), |
| | 622 | ) ); |
| | 623 | |
| | 624 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 625 | |
| | 626 | $expected = array( |
| | 627 | $this->users[1] => $this->users[1], |
| | 628 | $this->users[3] => $this->users[3], |
| | 629 | ); |
| | 630 | |
| | 631 | $this->assertEquals( $expected, $found ); |
| | 632 | } |
| | 633 | |
| | 634 | /** |
| | 635 | * @group BP_User_Query |
| | 636 | */ |
| | 637 | public function test_query_multiple_clauses_compare_equals() { |
| | 638 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 639 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 640 | xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' ); |
| | 641 | xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' ); |
| | 642 | |
| | 643 | $q = new BP_User_Query( array( |
| | 644 | 'xprofile_query' => array( |
| | 645 | 'relation' => 'AND', |
| | 646 | array( |
| | 647 | 'field_id' => $this->fields[1], |
| | 648 | 'value' => 'Bar', |
| | 649 | 'compare' => '=', |
| | 650 | ), |
| | 651 | array( |
| | 652 | 'field_id' => $this->fields[2], |
| | 653 | 'value' => 'Foo', |
| | 654 | 'compare' => '=', |
| | 655 | ), |
| | 656 | ), |
| | 657 | ) ); |
| | 658 | |
| | 659 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 660 | |
| | 661 | $expected = array( |
| | 662 | $this->users[2] => $this->users[2], |
| | 663 | ); |
| | 664 | |
| | 665 | $this->assertEquals( $expected, $found ); |
| | 666 | } |
| | 667 | |
| | 668 | /** |
| | 669 | * @group BP_User_Query |
| | 670 | */ |
| | 671 | public function test_query_multiple_clauses_compare_mixed() { |
| | 672 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 673 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 674 | xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' ); |
| | 675 | xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' ); |
| | 676 | |
| | 677 | $q = new BP_User_Query( array( |
| | 678 | 'xprofile_query' => array( |
| | 679 | 'relation' => 'AND', |
| | 680 | array( |
| | 681 | 'field_id' => $this->fields[1], |
| | 682 | 'value' => 'B', |
| | 683 | 'compare' => 'LIKE', |
| | 684 | ), |
| | 685 | array( |
| | 686 | 'field_id' => $this->fields[2], |
| | 687 | 'value' => '^F', |
| | 688 | 'compare' => 'REGEXP', |
| | 689 | ), |
| | 690 | ), |
| | 691 | ) ); |
| | 692 | |
| | 693 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 694 | |
| | 695 | $expected = array( |
| | 696 | $this->users[2] => $this->users[2], |
| | 697 | ); |
| | 698 | |
| | 699 | $this->assertEquals( $expected, $found ); |
| | 700 | } |
| | 701 | |
| | 702 | /** |
| | 703 | * @group BP_User_Query |
| | 704 | */ |
| | 705 | public function test_query_single_clause_compare_exists() { |
| | 706 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 707 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 708 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 709 | |
| | 710 | $q = new BP_User_Query( array( |
| | 711 | 'xprofile_query' => array( |
| | 712 | array( |
| | 713 | 'field_id' => $this->fields[1], |
| | 714 | 'compare' => 'EXISTS', |
| | 715 | ), |
| | 716 | ), |
| | 717 | ) ); |
| | 718 | |
| | 719 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 720 | |
| | 721 | $expected = array( |
| | 722 | $this->users[1] => $this->users[1], |
| | 723 | $this->users[2] => $this->users[2], |
| | 724 | $this->users[3] => $this->users[3], |
| | 725 | ); |
| | 726 | |
| | 727 | $this->assertEquals( $expected, $found ); |
| | 728 | } |
| | 729 | |
| | 730 | /** |
| | 731 | * @group BP_User_Query |
| | 732 | */ |
| | 733 | public function test_query_single_clause_compare_exists_with_value() { |
| | 734 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 735 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 736 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 737 | |
| | 738 | $q = new BP_User_Query( array( |
| | 739 | 'xprofile_query' => array( |
| | 740 | array( |
| | 741 | 'field_id' => $this->fields[1], |
| | 742 | 'compare' => 'EXISTS', |
| | 743 | 'value' => 'Foo', |
| | 744 | ), |
| | 745 | ), |
| | 746 | ) ); |
| | 747 | |
| | 748 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 749 | |
| | 750 | $expected = array( |
| | 751 | $this->users[1] => $this->users[1], |
| | 752 | ); |
| | 753 | |
| | 754 | $this->assertEquals( $expected, $found ); |
| | 755 | } |
| | 756 | |
| | 757 | /** |
| | 758 | * @group BP_User_Query |
| | 759 | */ |
| | 760 | public function test_query_single_clause_compare_notexists() { |
| | 761 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 762 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 763 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 764 | |
| | 765 | $q = new BP_User_Query( array( |
| | 766 | 'xprofile_query' => array( |
| | 767 | array( |
| | 768 | 'field_id' => $this->fields[1], |
| | 769 | 'compare' => 'NOT EXISTS', |
| | 770 | ), |
| | 771 | ), |
| | 772 | ) ); |
| | 773 | |
| | 774 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 775 | |
| | 776 | $expected = array( |
| | 777 | $this->users[4] => $this->users[4], |
| | 778 | $this->users[5] => $this->users[5], |
| | 779 | ); |
| | 780 | |
| | 781 | $this->assertEquals( $expected, $found ); |
| | 782 | } |
| | 783 | |
| | 784 | /** |
| | 785 | * @group BP_User_Query |
| | 786 | */ |
| | 787 | public function test_query_single_clause_compare_notexists_with_value() { |
| | 788 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 789 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 790 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 791 | |
| | 792 | $q = new BP_User_Query( array( |
| | 793 | 'xprofile_query' => array( |
| | 794 | array( |
| | 795 | 'field_id' => $this->fields[1], |
| | 796 | 'compare' => 'NOT EXISTS', |
| | 797 | 'value' => 'Foo', |
| | 798 | ), |
| | 799 | ), |
| | 800 | ) ); |
| | 801 | |
| | 802 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 803 | |
| | 804 | $expected = array( |
| | 805 | $this->users[4] => $this->users[4], |
| | 806 | $this->users[5] => $this->users[5], |
| | 807 | ); |
| | 808 | |
| | 809 | $this->assertEquals( $expected, $found ); |
| | 810 | } |
| | 811 | |
| | 812 | /** |
| | 813 | * @group BP_User_Query |
| | 814 | */ |
| | 815 | public function test_query_single_clause_no_compare_no_value() { |
| | 816 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 817 | xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' ); |
| | 818 | xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' ); |
| | 819 | |
| | 820 | $q = new BP_User_Query( array( |
| | 821 | 'xprofile_query' => array( |
| | 822 | array( |
| | 823 | 'field_id' => $this->fields[1], |
| | 824 | ), |
| | 825 | ), |
| | 826 | ) ); |
| | 827 | |
| | 828 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 829 | |
| | 830 | // Same as EXISTS |
| | 831 | $expected = array( |
| | 832 | $this->users[1] => $this->users[1], |
| | 833 | $this->users[2] => $this->users[2], |
| | 834 | $this->users[3] => $this->users[3], |
| | 835 | ); |
| | 836 | |
| | 837 | $this->assertEquals( $expected, $found ); |
| | 838 | } |
| | 839 | |
| | 840 | /** |
| | 841 | * @group BP_User_Query |
| | 842 | */ |
| | 843 | public function test_query_multiple_no_key_clauses() { |
| | 844 | xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' ); |
| | 845 | xprofile_set_field_data( $this->fields[2], $this->users[2], 'Bar' ); |
| | 846 | xprofile_set_field_data( $this->fields[3], $this->users[3], 'Barry' ); |
| | 847 | |
| | 848 | $q = new BP_User_Query( array( |
| | 849 | 'xprofile_query' => array( |
| | 850 | 'relation' => 'OR', |
| | 851 | array( |
| | 852 | 'field_id' => $this->fields[1], |
| | 853 | ), |
| | 854 | array( |
| | 855 | 'field_id' => $this->fields[2], |
| | 856 | ), |
| | 857 | array( |
| | 858 | 'field_id' => $this->fields[3], |
| | 859 | 'value' => 'Barry', |
| | 860 | ), |
| | 861 | ), |
| | 862 | ) ); |
| | 863 | |
| | 864 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 865 | |
| | 866 | // Same as EXISTS |
| | 867 | $expected = array( |
| | 868 | $this->users[1] => $this->users[1], |
| | 869 | $this->users[2] => $this->users[2], |
| | 870 | $this->users[3] => $this->users[3], |
| | 871 | ); |
| | 872 | |
| | 873 | $this->assertEquals( $expected, $found ); |
| | 874 | } |
| | 875 | |
| | 876 | /** |
| | 877 | * @group BP_User_Query |
| | 878 | */ |
| | 879 | public function test_query_translate_field_name() { |
| | 880 | $g = $this->factory->xprofile_group->create(); |
| | 881 | $f = $this->factory->xprofile_field->create( array( |
| | 882 | 'field_group_id' => $g, |
| | 883 | 'name' => 'Foo Field', |
| | 884 | 'type' => 'textarea', |
| | 885 | ) ); |
| | 886 | |
| | 887 | xprofile_set_field_data( $f, $this->users[1], 'Foo' ); |
| | 888 | xprofile_set_field_data( $f, $this->users[2], 'Bar' ); |
| | 889 | xprofile_set_field_data( $f, $this->users[3], 'Barry' ); |
| | 890 | |
| | 891 | $q = new BP_User_Query( array( |
| | 892 | 'xprofile_query' => array( |
| | 893 | array( |
| | 894 | 'field_name' => 'Foo Field', |
| | 895 | 'value' => 'Bar', |
| | 896 | ), |
| | 897 | ), |
| | 898 | ) ); |
| | 899 | |
| | 900 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 901 | |
| | 902 | $expected = array( |
| | 903 | $this->users[2] => $this->users[2], |
| | 904 | ); |
| | 905 | |
| | 906 | $this->assertEquals( $expected, $found ); |
| | 907 | } |
| | 908 | |
| | 909 | /** |
| | 910 | * @group BP_User_Query |
| | 911 | */ |
| | 912 | public function test_query_translate_field_name_with_invalid_field_name() { |
| | 913 | $g = $this->factory->xprofile_group->create(); |
| | 914 | $f = $this->factory->xprofile_field->create( array( |
| | 915 | 'field_group_id' => $g, |
| | 916 | 'name' => 'Foo Field', |
| | 917 | 'type' => 'textarea', |
| | 918 | ) ); |
| | 919 | |
| | 920 | xprofile_set_field_data( $f, $this->users[1], 'Foo' ); |
| | 921 | xprofile_set_field_data( $f, $this->users[2], 'Bar' ); |
| | 922 | xprofile_set_field_data( $f, $this->users[3], 'Barry' ); |
| | 923 | |
| | 924 | // AND should return nothing |
| | 925 | $q = new BP_User_Query( array( |
| | 926 | 'xprofile_query' => array( |
| | 927 | 'relation' => 'AND', |
| | 928 | array( |
| | 929 | 'field_name' => 'Foo Field', |
| | 930 | 'value' => 'Bar', |
| | 931 | ), |
| | 932 | array( |
| | 933 | 'field_name' => 'Foo Field 2', |
| | 934 | 'value' => 'Bar', |
| | 935 | ), |
| | 936 | ), |
| | 937 | ) ); |
| | 938 | |
| | 939 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 940 | $this->assertEquals( array(), $found ); |
| | 941 | |
| | 942 | // OR should return hit for the legit field |
| | 943 | $q = new BP_User_Query( array( |
| | 944 | 'xprofile_query' => array( |
| | 945 | 'relation' => 'OR', |
| | 946 | array( |
| | 947 | 'field_name' => 'Foo Field', |
| | 948 | 'value' => 'Bar', |
| | 949 | ), |
| | 950 | array( |
| | 951 | 'field_name' => 'Foo Field 2', |
| | 952 | 'value' => 'Bar', |
| | 953 | ), |
| | 954 | ), |
| | 955 | ) ); |
| | 956 | |
| | 957 | $found = wp_list_pluck( $q->results, 'ID' ); |
| | 958 | |
| | 959 | $expected = array( |
| | 960 | $this->users[2] => $this->users[2], |
| | 961 | ); |
| | 962 | |
| | 963 | $this->assertEquals( $expected, $found ); |
| | 964 | } |
| | 965 | } |