- Timestamp:
- 08/09/2024 01:13:58 PM (4 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php
r13358 r14000 11 11 */ 12 12 public function test_save_should_not_return_false_when_no_fields_have_been_altered() { 13 $g = self::factory()->xprofile_group->create();13 $g = self::factory()->xprofile_group->create(); 14 14 $group = new BP_XProfile_Group( $g ); 15 15 … … 22 22 * @ticket BP7916 23 23 */ 24 public function test_delete () {24 public function test_delete_xprofile_group() { 25 25 $g = self::factory()->xprofile_group->create(); 26 26 … … 43 43 $u = self::factory()->user->create(); 44 44 $g = self::factory()->xprofile_group->create(); 45 $f = self::factory()->xprofile_field->create( array( 46 'field_group_id' => $g, 47 ) ); 45 $f = self::factory()->xprofile_field->create( 46 array( 47 'field_group_id' => $g, 48 ) 49 ); 48 50 49 51 $f_obj = new BP_XProfile_Field( $f ); 50 52 51 53 $fields = array( 52 0 => new stdClass ,53 ); 54 55 $fields[0]->id = $f;56 $fields[0]->name = $f_obj->name;54 0 => new stdClass(), 55 ); 56 57 $fields[0]->id = $f; 58 $fields[0]->name = $f_obj->name; 57 59 $fields[0]->description = $f_obj->description; 58 $fields[0]->type = $f_obj->type;59 $fields[0]->group_id = $f_obj->group_id;60 $fields[0]->type = $f_obj->type; 61 $fields[0]->group_id = $f_obj->group_id; 60 62 $fields[0]->is_required = $f_obj->is_required; 61 $fields[0]->data = new stdClass;63 $fields[0]->data = new stdClass(); 62 64 $fields[0]->data->value = 'foo'; 63 $fields[0]->data->id = 123;65 $fields[0]->data->id = 123; 64 66 65 67 // custom visibility enabled, but no fallback … … 69 71 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 70 72 71 $expected = $fields;73 $expected = $fields; 72 74 $expected[0]->visibility_level = 'adminsonly'; 73 75 … … 81 83 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 82 84 83 $expected = $fields;85 $expected = $fields; 84 86 $expected[0]->visibility_level = 'public'; 85 87 … … 93 95 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 94 96 95 $expected = $fields;97 $expected = $fields; 96 98 $expected[0]->visibility_level = 'adminsonly'; 97 99 … … 107 109 $g3 = self::factory()->xprofile_group->create(); 108 110 109 $all = BP_XProfile_Group::get();111 $all = BP_XProfile_Group::get(); 110 112 $all_results = array_map( 'absint', wp_list_pluck( $all, 'id' ) ); 111 113 112 $e1 = array( $g1, $g2 ); 113 $groups1 = BP_XProfile_Group::get( array( 114 'exclude_groups' => implode( ',', $e1 ), 115 ) ); 114 $e1 = array( $g1, $g2 ); 115 $groups1 = BP_XProfile_Group::get( 116 array( 117 'exclude_groups' => implode( ',', $e1 ), 118 ) 119 ); 116 120 117 121 $r_groups1 = array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ); 118 $found1 = array_diff( $all_results, $r_groups1 );122 $found1 = array_diff( $all_results, $r_groups1 ); 119 123 120 124 $this->assertSame( $e1, array_merge( $found1, array() ) ); 121 125 122 $e2 = array( $g2, $g3 ); 123 $groups2 = BP_XProfile_Group::get( array( 124 'exclude_groups' => $e2, 125 ) ); 126 $e2 = array( $g2, $g3 ); 127 $groups2 = BP_XProfile_Group::get( 128 array( 129 'exclude_groups' => $e2, 130 ) 131 ); 126 132 127 133 $r_groups2 = array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ); 128 $found2 = array_diff( $all_results, $r_groups2 );134 $found2 = array_diff( $all_results, $r_groups2 ); 129 135 130 136 $this->assertSame( $e2, array_merge( $found2, array() ) ); … … 139 145 $g2 = self::factory()->xprofile_group->create(); 140 146 $g3 = self::factory()->xprofile_group->create(); 141 $e1 = [ $g1, $g2 ];147 $e1 = array( $g1, $g2 ); 142 148 143 149 // Comma-separated list of profile group ids. 144 $groups1 = BP_XProfile_Group::get( [ 'profile_group_id' => join( ',', $e1 ) ]);150 $groups1 = BP_XProfile_Group::get( array( 'profile_group_id' => join( ',', $e1 ) ) ); 145 151 146 152 $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ) ); 147 153 148 154 // Array of profile group ids. 149 $groups2 = BP_XProfile_Group::get( [ 'profile_group_id' => $e1 ]);155 $groups2 = BP_XProfile_Group::get( array( 'profile_group_id' => $e1 ) ); 150 156 151 157 $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ) ); … … 164 170 $field->set_member_types( array( 'foo' ) ); 165 171 166 $found_groups = BP_XProfile_Group::get( array( 167 'user_id' => false, 168 'fetch_fields' => true, 169 ) ); 172 $found_groups = BP_XProfile_Group::get( 173 array( 174 'user_id' => false, 175 'fetch_fields' => true, 176 ) 177 ); 170 178 171 179 // The groups aren't indexed, so we have to go looking for it. … … 191 199 $field->set_member_types( array( 'foo' ) ); 192 200 193 $found_groups = BP_XProfile_Group::get( array( 194 'user_id' => 0, 195 'member_type' => false, 196 'fetch_fields' => true, 197 ) ); 201 $found_groups = BP_XProfile_Group::get( 202 array( 203 'user_id' => 0, 204 'member_type' => false, 205 'fetch_fields' => true, 206 ) 207 ); 198 208 199 209 // The groups aren't indexed, so we have to go looking for it. … … 212 222 */ 213 223 public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id() { 214 $g = self::factory()->xprofile_group->create();224 $g = self::factory()->xprofile_group->create(); 215 225 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 216 226 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 238 248 bp_set_member_type( $u, 'foo' ); 239 249 240 $found_groups = BP_XProfile_Group::get( array( 241 'user_id' => $u, 242 'fetch_fields' => true, 243 ) ); 250 $found_groups = BP_XProfile_Group::get( 251 array( 252 'user_id' => $u, 253 'fetch_fields' => true, 254 ) 255 ); 244 256 245 257 // The groups aren't indexed, so we have to go looking for it. … … 261 273 */ 262 274 public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id_with_no_member_types() { 263 $g = self::factory()->xprofile_group->create();275 $g = self::factory()->xprofile_group->create(); 264 276 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 265 277 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 281 293 $u = self::factory()->user->create(); 282 294 283 $found_groups = BP_XProfile_Group::get( array( 284 'user_id' => $u, 285 'fetch_fields' => true, 286 ) ); 295 $found_groups = BP_XProfile_Group::get( 296 array( 297 'user_id' => $u, 298 'fetch_fields' => true, 299 ) 300 ); 287 301 288 302 // The groups aren't indexed, so we have to go looking for it. … … 303 317 */ 304 318 public function test_member_types_of_provided_user_id_should_take_precedence_over_provided_member_type() { 305 $g = self::factory()->xprofile_group->create();319 $g = self::factory()->xprofile_group->create(); 306 320 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 307 321 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 317 331 bp_set_member_type( $u, 'foo' ); 318 332 319 $found_groups = BP_XProfile_Group::get( array( 320 'user_id' => $u, 321 'member_type' => 'bar', 322 'fetch_fields' => true, 323 ) ); 333 $found_groups = BP_XProfile_Group::get( 334 array( 335 'user_id' => $u, 336 'member_type' => 'bar', 337 'fetch_fields' => true, 338 ) 339 ); 324 340 325 341 // The groups aren't indexed, so we have to go looking for it. … … 338 354 */ 339 355 public function test_member_type_single_value_should_be_respected() { 340 $g = self::factory()->xprofile_group->create();356 $g = self::factory()->xprofile_group->create(); 341 357 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 342 358 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 349 365 $field2->set_member_types( array( 'bar' ) ); 350 366 351 $found_groups = BP_XProfile_Group::get( array( 352 'member_type' => 'bar', 353 'fetch_fields' => true, 354 ) ); 367 $found_groups = BP_XProfile_Group::get( 368 array( 369 'member_type' => 'bar', 370 'fetch_fields' => true, 371 ) 372 ); 355 373 356 374 // The groups aren't indexed, so we have to go looking for it. … … 370 388 */ 371 389 public function test_member_type_array_value_should_be_respected() { 372 $g = self::factory()->xprofile_group->create();390 $g = self::factory()->xprofile_group->create(); 373 391 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 374 392 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 381 399 $field2->set_member_types( array( 'bar' ) ); 382 400 383 $found_groups = BP_XProfile_Group::get( array( 384 'member_type' => array( 'bar' ), 385 'fetch_fields' => true, 386 ) ); 401 $found_groups = BP_XProfile_Group::get( 402 array( 403 'member_type' => array( 'bar' ), 404 'fetch_fields' => true, 405 ) 406 ); 387 407 388 408 // The groups aren't indexed, so we have to go looking for it. … … 402 422 */ 403 423 public function test_member_type_null_should_be_respected() { 404 $g = self::factory()->xprofile_group->create();424 $g = self::factory()->xprofile_group->create(); 405 425 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 406 426 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 411 431 $field1->set_member_types( array( 'foo' ) ); 412 432 413 $found_groups = BP_XProfile_Group::get( array( 414 'member_type' => array( 'null' ), 415 'fetch_fields' => true, 416 ) ); 433 $found_groups = BP_XProfile_Group::get( 434 array( 435 'member_type' => array( 'null' ), 436 'fetch_fields' => true, 437 ) 438 ); 417 439 418 440 // The groups aren't indexed, so we have to go looking for it. … … 431 453 */ 432 454 public function test_save_xprofile_group_name() { 433 $g1 = self::factory()->xprofile_group->create( array( 434 'name' => "Test ' Name" 435 ) ); 455 $g1 = self::factory()->xprofile_group->create( 456 array( 457 'name' => "Test ' Name", 458 ) 459 ); 436 460 437 461 $e1 = new BP_XProfile_Group( $g1 ); … … 454 478 455 479 // Create a group 456 $g1 = self::factory()->xprofile_group->create( array( 457 'name' => $pristine_name 458 ) ); 480 $g1 = self::factory()->xprofile_group->create( 481 array( 482 'name' => $pristine_name, 483 ) 484 ); 459 485 460 486 // Get the field … … 471 497 $g2 = self::factory()->xprofile_group->create(); 472 498 473 $group_ids = [ 1 ]; // Default group.499 $group_ids = array( 1 ); // Default group. 474 500 $group_ids[] = self::factory()->xprofile_group->create(); 475 501 $group_ids[] = self::factory()->xprofile_group->create(); … … 481 507 $this->assertEqualSets( $group_ids, $found_1 ); 482 508 483 $found_2 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => $g1 ]);509 $found_2 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => $g1 ) ); 484 510 $this->assertCount( 1, $found_2 ); 485 $this->assertSame( [ $g1 ], $found_2 );486 487 $found_3 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1 ] ]);511 $this->assertSame( array( $g1 ), $found_2 ); 512 513 $found_3 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1 ) ) ); 488 514 $this->assertCount( 1, $found_3 ); 489 $this->assertSame( [ $g1 ], $found_3 );490 491 $found_4 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g2 ] ]);515 $this->assertSame( array( $g1 ), $found_3 ); 516 517 $found_4 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g2 ) ) ); 492 518 $this->assertCount( 1, $found_4 ); 493 $this->assertSame( [ $g2 ], $found_4 );494 495 $found_5 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1, $g2 ] ]);519 $this->assertSame( array( $g2 ), $found_4 ); 520 521 $found_5 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1, $g2 ) ) ); 496 522 $this->assertCount( 2, $found_5 ); 497 $this->assertSame( [ $g1, $g2 ], $found_5 );498 499 $found_6 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => join( ',', [ $g1, $g2 ] ) ]);523 $this->assertSame( array( $g1, $g2 ), $found_5 ); 524 525 $found_6 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => join( ',', array( $g1, $g2 ) ) ) ); 500 526 $this->assertCount( 2, $found_6 ); 501 $this->assertSame( [ $g1, $g2 ], $found_6 );502 503 $found_7 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => true ]);527 $this->assertSame( array( $g1, $g2 ), $found_6 ); 528 529 $found_7 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => true ) ); 504 530 $this->assertEqualSets( $group_ids, $found_7 ); 505 531 } … … 521 547 $group_ids[] = $g2; 522 548 523 $params_1 = [ 'exclude_groups' => false ];549 $params_1 = array( 'exclude_groups' => false ); 524 550 525 551 // Prime cache. … … 534 560 535 561 // Different parameters should trigger a cache miss. 536 $found_3 = BP_XProfile_Group::get_group_ids( [ 'exclude_groups' => [ 0 ] ]);562 $found_3 = BP_XProfile_Group::get_group_ids( array( 'exclude_groups' => array( 0 ) ) ); 537 563 $this->assertEqualSets( $group_ids, $found_3 ); 538 564 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 539 565 540 566 // Again, different parameters should trigger a cache miss. 541 $found_4 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1, $g2 ] ]);542 $this->assertEqualSets( [ $g1, $g2 ], $found_4 );567 $found_4 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1, $g2 ) ) ); 568 $this->assertEqualSets( array( $g1, $g2 ), $found_4 ); 543 569 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 544 570 } … … 551 577 $group_ids = array( 1 ); // Default group. 552 578 $group_ids[1] = self::factory()->xprofile_group->create(); 553 $group_ids[2] = self::factory()->xprofile_group->create(); 554 $group_ids[3] = self::factory()->xprofile_group->create(); 579 580 self::factory()->xprofile_group->create(); 581 self::factory()->xprofile_group->create(); 555 582 556 583 // Prime cache. … … 574 601 $group_ids = array( 1 ); // Default group. 575 602 $group_ids[1] = self::factory()->xprofile_group->create(); 576 $group_ids[2] = self::factory()->xprofile_group->create(); 577 $group_ids[3] = self::factory()->xprofile_group->create(); 603 604 self::factory()->xprofile_group->create(); 605 self::factory()->xprofile_group->create(); 578 606 579 607 // Prime cache. … … 586 614 $num_queries = $wpdb->num_queries; 587 615 588 $found = BP_XProfile_Group::get_group_ids(); 616 BP_XProfile_Group::get_group_ids(); 617 589 618 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 590 619 } … … 599 628 $group_ids = array( 1 ); // Default group. 600 629 $group_ids[1] = self::factory()->xprofile_group->create(); 601 $group_ids[2] = self::factory()->xprofile_group->create(); 602 $group_ids[3] = self::factory()->xprofile_group->create(); 603 604 $f = self::factory()->xprofile_field->create( array( 605 'field_group_id' => $group_ids[1], 606 ) ); 630 631 self::factory()->xprofile_group->create(); 632 self::factory()->xprofile_group->create(); 633 634 $f = self::factory()->xprofile_field->create( 635 array( 636 'field_group_id' => $group_ids[1], 637 ) 638 ); 607 639 $field = new BP_XProfile_Field( $f ); 608 640 … … 615 647 $num_queries = $wpdb->num_queries; 616 648 617 $found = BP_XProfile_Group::get_group_ids(); 649 BP_XProfile_Group::get_group_ids(); 650 618 651 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 619 652 } … … 626 659 $group_ids = array( 1 ); // Default group. 627 660 $group_ids[1] = self::factory()->xprofile_group->create(); 628 $group_ids[2] = self::factory()->xprofile_group->create(); 629 $group_ids[3] = self::factory()->xprofile_group->create(); 630 631 $f = self::factory()->xprofile_field->create( array( 632 'field_group_id' => $group_ids[1], 633 ) ); 661 662 self::factory()->xprofile_group->create(); 663 self::factory()->xprofile_group->create(); 664 665 $f = self::factory()->xprofile_field->create( 666 array( 667 'field_group_id' => $group_ids[1], 668 ) 669 ); 634 670 $field = new BP_XProfile_Field( $f ); 635 671 … … 657 693 $group_id = self::factory()->xprofile_group->create(); 658 694 659 $f = self::factory()->xprofile_field->create( array( 660 'field_group_id' => $group_id, 661 ) ); 662 $field = new BP_XProfile_Field( $f ); 695 $f = self::factory()->xprofile_field->create( 696 array( 697 'field_group_id' => $group_id, 698 ) 699 ); 700 701 new BP_XProfile_Field( $f ); 663 702 664 703 // Prime cache. … … 682 721 $group_id = self::factory()->xprofile_group->create(); 683 722 684 $f = self::factory()->xprofile_field->create( array( 685 'field_group_id' => $group_id, 686 ) ); 723 $f = self::factory()->xprofile_field->create( 724 array( 725 'field_group_id' => $group_id, 726 ) 727 ); 687 728 $field = new BP_XProfile_Field( $f ); 688 729 … … 706 747 $group_id = self::factory()->xprofile_group->create(); 707 748 708 $f = self::factory()->xprofile_field->create( array( 709 'field_group_id' => $group_id, 710 ) ); 749 $f = self::factory()->xprofile_field->create( 750 array( 751 'field_group_id' => $group_id, 752 ) 753 ); 711 754 $field = new BP_XProfile_Field( $f ); 712 755 … … 720 763 $this->assertNotContains( $f, $found ); 721 764 } 765 766 /** 767 * @ticket BP8552 768 * @group cache 769 */ 770 public function test_group_query_getter_with_cache_results() { 771 global $wpdb; 772 773 self::factory()->xprofile_group->create_many( 2 ); 774 775 $wpdb->num_queries = 0; 776 777 $first_query = BP_XProfile_Group::get( array( 'cache_results' => true ) ); 778 779 $queries_before = get_num_queries(); 780 781 $second_query = BP_XProfile_Group::get( array( 'cache_results' => false ) ); 782 783 $queries_after = get_num_queries(); 784 785 $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' ); 786 $this->assertSame( 3, $queries_after, 'Assert that the uncached query was run' ); 787 $this->assertEquals( $first_query, $second_query, 'Results of the query are expected to match.' ); 788 } 722 789 }
Note: See TracChangeset
for help on using the changeset viewer.