[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/xprofile/ -> class-bp-xprofile-query.php (source)

   1  <?php
   2  
   3  /**
   4   * @group xprofile
   5   * @group BP_XProfile_Query
   6   */
   7  class BP_Tests_BP_XProfile_Query extends BP_UnitTestCase {
   8      protected $group;
   9      protected $fields = array();
  10      protected $users = array();
  11  
  12  	public function tearDown() {
  13          parent::tearDown();
  14          $this->group = '';
  15          $this->fields = array();
  16          $this->users = array();
  17      }
  18  
  19  	public function test_no_field() {
  20          $this->create_fields( 2 );
  21          $this->create_users( 3 );
  22  
  23          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
  24          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
  25          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
  26  
  27          $q = new BP_User_Query( array(
  28              'xprofile_query' => array(
  29                  array(
  30                      'value' => 'foo',
  31                  ),
  32              ),
  33          ) );
  34  
  35          $expected = array( $this->users[0], $this->users[2] );
  36          $this->assertEqualSets( $expected, array_keys( $q->results ) );
  37      }
  38  
  39  	public function test_no_value() {
  40          $this->create_fields( 2 );
  41          $this->create_users( 3 );
  42  
  43          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
  44          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
  45          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
  46  
  47          $q = new BP_User_Query( array(
  48              'xprofile_query' => array(
  49                  array(
  50                      'field' => $this->fields[1],
  51                  ),
  52              ),
  53          ) );
  54  
  55          $expected = array( $this->users[2] );
  56          $this->assertEqualSets( $expected, array_keys( $q->results ) );
  57      }
  58  
  59  	public function test_translate_field_name_to_field_id() {
  60          $this->create_fields( 0 );
  61          $f = self::factory()->xprofile_field->create( array(
  62              'field_group_id' => $this->group,
  63              'type' => 'textbox',
  64              'name' => 'Foo Field',
  65          ) );
  66          $this->create_users( 2 );
  67  
  68          xprofile_set_field_data( $f, $this->users[0], 'foo' );
  69  
  70          $q = new BP_User_Query( array(
  71              'xprofile_query' => array(
  72                  array(
  73                      'field' => 'Foo Field',
  74                  ),
  75              ),
  76          ) );
  77  
  78          $expected = array( $this->users[0] );
  79          $this->assertEqualSets( $expected, array_keys( $q->results ) );
  80      }
  81  
  82  	public function test_single_clause_compare_default() {
  83          $this->create_fields( 2 );
  84          $this->create_users( 3 );
  85  
  86          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
  87          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
  88          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
  89  
  90          $q = new BP_User_Query( array(
  91              'xprofile_query' => array(
  92                  array(
  93                      'field' => $this->fields[0],
  94                      'value' => 'foo',
  95                  ),
  96              ),
  97          ) );
  98  
  99          $expected = array( $this->users[0] );
 100          $this->assertEquals( $expected, array_keys( $q->results ) );
 101      }
 102  
 103  	public function test_single_clause_compare_equals() {
 104          $this->create_fields( 2 );
 105          $this->create_users( 3 );
 106  
 107          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 108          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 109          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
 110  
 111          $q = new BP_User_Query( array(
 112              'xprofile_query' => array(
 113                  array(
 114                      'field' => $this->fields[0],
 115                      'value' => 'foo',
 116                      'compare' => '=',
 117                  ),
 118              ),
 119          ) );
 120  
 121          $expected = array( $this->users[0] );
 122          $this->assertEquals( $expected, array_keys( $q->results ) );
 123      }
 124  
 125  	public function test_single_clause_compare_not_equals() {
 126          $this->create_fields( 1 );
 127          $this->create_users( 2 );
 128  
 129          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 130          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 131  
 132          $q = new BP_User_Query( array(
 133              'xprofile_query' => array(
 134                  array(
 135                      'field' => $this->fields[0],
 136                      'value' => 'foo',
 137                      'compare' => '!=',
 138                  ),
 139              ),
 140          ) );
 141  
 142          $expected = array( $this->users[1] );
 143          $this->assertEquals( $expected, array_keys( $q->results ) );
 144      }
 145  
 146      public function test_single_clause_compare_arithmetic_comparisons() {
 147          $this->create_fields( 1 );
 148          $this->create_users( 3 );
 149  
 150          xprofile_set_field_data( $this->fields[0], $this->users[0], '1' );
 151          xprofile_set_field_data( $this->fields[0], $this->users[1], '2' );
 152          xprofile_set_field_data( $this->fields[0], $this->users[2], '3' );
 153  
 154          // <
 155          $q = new BP_User_Query( array(
 156              'xprofile_query' => array(
 157                  array(
 158                      'field' => $this->fields[0],
 159                      'value' => 2,
 160                      'compare' => '<',
 161                  ),
 162              ),
 163          ) );
 164  
 165          $expected = array( $this->users[0] );
 166          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 167  
 168          // <=
 169          $q = new BP_User_Query( array(
 170              'xprofile_query' => array(
 171                  array(
 172                      'field' => $this->fields[0],
 173                      'value' => 2,
 174                      'compare' => '<=',
 175                  ),
 176              ),
 177          ) );
 178  
 179          $expected = array( $this->users[0], $this->users[1] );
 180          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 181  
 182          // >=
 183          $q = new BP_User_Query( array(
 184              'xprofile_query' => array(
 185                  array(
 186                      'field' => $this->fields[0],
 187                      'value' => 2,
 188                      'compare' => '>=',
 189                  ),
 190              ),
 191          ) );
 192  
 193          $expected = array( $this->users[1], $this->users[2] );
 194          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 195  
 196          // >
 197          $q = new BP_User_Query( array(
 198              'xprofile_query' => array(
 199                  array(
 200                      'field' => $this->fields[0],
 201                      'value' => 2,
 202                      'compare' => '>',
 203                  ),
 204              ),
 205          ) );
 206  
 207          $expected = array( $this->users[2] );
 208          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 209      }
 210  
 211  	public function test_single_clause_compare_like() {
 212          $this->create_fields( 1 );
 213          $this->create_users( 2 );
 214  
 215          xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
 216  
 217          $q = new BP_User_Query( array(
 218              'xprofile_query' => array(
 219                  array(
 220                      'field' => $this->fields[0],
 221                      'value' => 'ba',
 222                      'compare' => 'LIKE',
 223                  ),
 224              ),
 225          ) );
 226  
 227          $expected = array( $this->users[0] );
 228          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 229      }
 230  
 231  	public function test_single_clause_compare_not_like() {
 232          $this->create_fields( 1 );
 233          $this->create_users( 3 );
 234  
 235          xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
 236          xprofile_set_field_data( $this->fields[0], $this->users[1], 'rab' );
 237  
 238          $q = new BP_User_Query( array(
 239              'xprofile_query' => array(
 240                  array(
 241                      'field' => $this->fields[0],
 242                      'value' => 'ba',
 243                      'compare' => 'NOT LIKE',
 244                  ),
 245              ),
 246          ) );
 247  
 248          $expected = array( $this->users[1] );
 249          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 250      }
 251  
 252      public function test_single_clause_compare_between_not_between() {
 253          $this->create_fields( 1 );
 254          $this->create_users( 3 );
 255  
 256          xprofile_set_field_data( $this->fields[0], $this->users[0], '1' );
 257          xprofile_set_field_data( $this->fields[0], $this->users[1], '10' );
 258          xprofile_set_field_data( $this->fields[0], $this->users[2], '100' );
 259  
 260          $q = new BP_User_Query( array(
 261              'xprofile_query' => array(
 262                  array(
 263                      'field' => $this->fields[0],
 264                      'value' => array( 9, 12 ),
 265                      'compare' => 'BETWEEN',
 266                      'type' => 'NUMERIC',
 267                  ),
 268              ),
 269          ) );
 270  
 271          $expected = array( $this->users[1] );
 272          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 273  
 274          $q = new BP_User_Query( array(
 275              'xprofile_query' => array(
 276                  array(
 277                      'field' => $this->fields[0],
 278                      'value' => array( 9, 12 ),
 279                      'compare' => 'NOT BETWEEN',
 280                      'type' => 'NUMERIC',
 281                  ),
 282              ),
 283          ) );
 284  
 285          $expected = array( $this->users[0], $this->users[2] );
 286          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 287      }
 288  
 289  	public function test_single_clause_compare_regexp_rlike() {
 290          $this->create_fields( 1 );
 291          $this->create_users( 3 );
 292  
 293          xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
 294          xprofile_set_field_data( $this->fields[0], $this->users[1], 'baz' );
 295  
 296          $q = new BP_User_Query( array(
 297              'xprofile_query' => array(
 298                  array(
 299                      'field' => $this->fields[0],
 300                      'value' => 'z$',
 301                      'compare' => 'REGEXP',
 302                  ),
 303              ),
 304          ) );
 305  
 306          $expected = array( $this->users[1] );
 307          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 308  
 309          // RLIKE is a synonym for REGEXP.
 310          $q = new BP_User_Query( array(
 311              'xprofile_query' => array(
 312                  array(
 313                      'field' => $this->fields[0],
 314                      'value' => 'z$',
 315                      'compare' => 'RLIKE',
 316                  ),
 317              ),
 318          ) );
 319  
 320          $expected = array( $this->users[1] );
 321          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 322      }
 323  
 324  	public function test_single_clause_compare_not_regexp() {
 325          $this->create_fields( 1 );
 326          $this->create_users( 3 );
 327  
 328          xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
 329          xprofile_set_field_data( $this->fields[0], $this->users[1], 'baz' );
 330  
 331          $q = new BP_User_Query( array(
 332              'xprofile_query' => array(
 333                  array(
 334                      'field' => $this->fields[0],
 335                      'value' => 'z$',
 336                      'compare' => 'NOT REGEXP',
 337                  ),
 338              ),
 339          ) );
 340  
 341          $expected = array( $this->users[0] );
 342          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 343      }
 344  
 345  	public function test_single_clause_compare_not_exists() {
 346          $this->create_fields( 2 );
 347          $this->create_users( 2 );
 348  
 349          xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
 350          xprofile_set_field_data( $this->fields[1], $this->users[1], 'bar' );
 351  
 352          $q = new BP_User_Query( array(
 353              'xprofile_query' => array(
 354                  array(
 355                      'field' => $this->fields[0],
 356                      'compare' => 'NOT EXISTS',
 357                  ),
 358              ),
 359          ) );
 360  
 361          $expected = array( $this->users[1] );
 362          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 363      }
 364  
 365  	public function test_relation_default_to_and() {
 366          $this->create_fields( 2 );
 367          $this->create_users( 4 );
 368  
 369          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 370          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 371          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
 372          xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
 373          xprofile_set_field_data( $this->fields[0], $this->users[3], 'foo' );
 374  
 375          $q = new BP_User_Query( array(
 376              'xprofile_query' => array(
 377                  array(
 378                      'field' => $this->fields[0],
 379                      'value' => 'foo',
 380                  ),
 381                  array(
 382                      'field' => $this->fields[1],
 383                      'value' => 'bar',
 384                  ),
 385              ),
 386          ) );
 387  
 388          $expected = array( $this->users[3] );
 389          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 390      }
 391  
 392  	public function test_relation_and() {
 393          $this->create_fields( 2 );
 394          $this->create_users( 4 );
 395  
 396          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 397          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 398          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
 399          xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
 400          xprofile_set_field_data( $this->fields[0], $this->users[3], 'foo' );
 401  
 402          $q = new BP_User_Query( array(
 403              'xprofile_query' => array(
 404                  'relation' => 'AND',
 405                  array(
 406                      'field' => $this->fields[0],
 407                      'value' => 'foo',
 408                  ),
 409                  array(
 410                      'field' => $this->fields[1],
 411                      'value' => 'bar',
 412                  ),
 413              ),
 414          ) );
 415  
 416          $expected = array( $this->users[3] );
 417          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 418      }
 419  
 420  	public function test_relation_or() {
 421          $this->create_fields( 2 );
 422          $this->create_users( 4 );
 423  
 424          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 425          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 426          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
 427          xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
 428          xprofile_set_field_data( $this->fields[0], $this->users[3], 'foo' );
 429  
 430          $q = new BP_User_Query( array(
 431              'xprofile_query' => array(
 432                  'relation' => 'OR',
 433                  array(
 434                      'field' => $this->fields[0],
 435                      'value' => 'foo',
 436                  ),
 437                  array(
 438                      'field' => $this->fields[1],
 439                      'value' => 'bar',
 440                  ),
 441              ),
 442          ) );
 443  
 444          $expected = array( $this->users[0], $this->users[3] );
 445          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 446      }
 447  
 448  	public function test_relation_and_with_compare_not_exists() {
 449          $this->create_fields( 2 );
 450          $this->create_users( 4 );
 451  
 452          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 453          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 454          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
 455          xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
 456  
 457          $q = new BP_User_Query( array(
 458              'xprofile_query' => array(
 459                  'relation' => 'AND',
 460                  array(
 461                      'field' => $this->fields[0],
 462                      'compare' => 'NOT EXISTS',
 463                  ),
 464                  array(
 465                      'field' => $this->fields[1],
 466                      'value' => 'bar',
 467                  ),
 468              ),
 469          ) );
 470  
 471          $expected = array( $this->users[3] );
 472          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 473      }
 474  
 475      /**
 476       * @ticket BP7202
 477       */
 478  	public function test_relation_and_with_compare_not_in() {
 479          $this->create_fields( 1 );
 480          $this->create_users( 4 );
 481  
 482          xprofile_set_field_data( $this->fields[0], $this->users[0], 'boo' );
 483          xprofile_set_field_data( $this->fields[0], $this->users[3], 'far' );
 484          xprofile_set_field_data( $this->fields[0], $this->users[1], 'foo' );
 485          xprofile_set_field_data( $this->fields[0], $this->users[2], 'bar' );
 486  
 487          $q = new BP_User_Query( array(
 488              'xprofile_query' => array(
 489                  'relation' => 'AND',
 490                  array(
 491                      'field' => $this->fields[0],
 492                      'compare' => 'NOT IN',
 493                      'value' => array( 'foo', 'bar' )
 494                  ),
 495                  array(
 496                      'field' => $this->fields[0],
 497                      'compare' => '!=',
 498                      'value' => 'far',
 499                  ),
 500              ),
 501          ) );
 502  
 503          $expected = array( $this->users[0] );
 504          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 505      }
 506  
 507  	public function test_relation_or_with_compare_not_exists() {
 508          $this->create_fields( 2 );
 509          $this->create_users( 4 );
 510  
 511          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 512          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 513          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
 514          xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
 515          xprofile_set_field_data( $this->fields[0], $this->users[3], 'bar' );
 516  
 517          $q = new BP_User_Query( array(
 518              'xprofile_query' => array(
 519                  'relation' => 'OR',
 520                  array(
 521                      'field' => $this->fields[0],
 522                      'compare' => 'NOT EXISTS',
 523                  ),
 524                  array(
 525                      'field' => $this->fields[1],
 526                      'value' => 'bar',
 527                  ),
 528              ),
 529          ) );
 530  
 531          $expected = array( $this->users[2], $this->users[3] );
 532          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 533      }
 534  
 535      /**
 536       * Tests for table join logic.
 537       */
 538  	public function test_relation_or_compare_equals_and_like() {
 539          $this->create_fields( 2 );
 540          $this->create_users( 4 );
 541  
 542          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 543          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 544          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
 545          xprofile_set_field_data( $this->fields[1], $this->users[3], 'barry' );
 546  
 547          $q = new BP_User_Query( array(
 548              'xprofile_query' => array(
 549                  'relation' => 'OR',
 550                  array(
 551                      'field' => $this->fields[0],
 552                      'compare' => '=',
 553                      'value' => 'foo',
 554                  ),
 555                  array(
 556                      'field' => $this->fields[1],
 557                      'value' => 'bar',
 558                      'compare' => 'LIKE',
 559                  ),
 560              ),
 561          ) );
 562  
 563          $expected = array( $this->users[0], $this->users[3] );
 564          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 565      }
 566  
 567  	public function test_nested() {
 568          $this->create_fields( 2 );
 569          $this->create_users( 3 );
 570  
 571          xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
 572          xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
 573          xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
 574          xprofile_set_field_data( $this->fields[1], $this->users[1], 'foo' );
 575  
 576          $q = new BP_User_Query( array(
 577              'xprofile_query' => array(
 578                  'relation' => 'OR',
 579                  array(
 580                      'field' => $this->fields[0],
 581                      'compare' => '=',
 582                      'value' => 'foo',
 583                  ),
 584                  array(
 585                      'relation' => 'AND',
 586                      array(
 587                          'field' => $this->fields[0],
 588                          'value' => 'bar',
 589                      ),
 590                      array(
 591                          'field' => $this->fields[1],
 592                          'value' => 'foo',
 593                      ),
 594                  ),
 595              ),
 596          ) );
 597  
 598          $expected = array( $this->users[0], $this->users[1] );
 599          $this->assertEqualSets( $expected, array_keys( $q->results ) );
 600      }
 601  
 602      /**
 603       * @group BP7202
 604       */
 605      public function test_find_compatible_table_alias_should_match_negative_siblings_joined_with_relation_and() {
 606          $this->create_fields( 1 );
 607  
 608          $q = new BP_XProfile_Query( array(
 609              'relation' => 'AND',
 610              array(
 611                  'field' => $this->fields[0],
 612                  'compare' => '!=',
 613                  'value' => 'foo',
 614              ),
 615              array(
 616                  'field' => $this->fields[0],
 617                  'compare' => 'NOT IN',
 618                  'value' => array( 'bar', 'baz' ),
 619              )
 620          ) );
 621  
 622          $sql = $q->get_sql( buddypress()->profile->table_name_data, 'user_id' );
 623  
 624          $this->assertSame( 1, substr_count( $sql['join'], 'INNER JOIN' ) );
 625      }
 626  
 627      /** Helpers **********************************************************/
 628  
 629  	protected function create_fields( $count ) {
 630          $this->group = self::factory()->xprofile_group->create();
 631          for ( $i = 0; $i < $count; $i++ ) {
 632              $this->fields[] = self::factory()->xprofile_field->create( array(
 633                  'field_group_id' => $this->group,
 634                  'type' => 'textbox',
 635              ) );
 636          }
 637      }
 638  
 639  	protected function create_users( $count ) {
 640          for ( $i = 0; $i < $count; $i++ ) {
 641              $this->users[] = self::factory()->user->create();
 642          }
 643      }
 644  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1