[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @group BP_XProfile_Group 5 * @group xprofile 6 */ 7 class BP_Tests_BP_XProfile_Group extends BP_UnitTestCase { 8 9 /** 10 * @ticket BP6552 11 */ 12 public function test_save_should_not_return_false_when_no_fields_have_been_altered() { 13 $g = self::factory()->xprofile_group->create(); 14 $group = new BP_XProfile_Group( $g ); 15 16 $saved = $group->save(); 17 18 $this->assertEquals( $g, $saved ); 19 } 20 21 /** 22 * @ticket BP7916 23 */ 24 public function test_delete() { 25 $g = self::factory()->xprofile_group->create(); 26 27 $groups = bp_xprofile_get_groups(); 28 $group_ids = wp_list_pluck( $groups, 'id' ); 29 $this->assertContains( $g, $group_ids ); 30 31 $group = new BP_XProfile_Group( $g ); 32 $this->assertTrue( $group->delete() ); 33 34 $groups = bp_xprofile_get_groups(); 35 $group_ids = wp_list_pluck( $groups, 'id' ); 36 $this->assertNotContains( $g, $group_ids ); 37 } 38 39 /** 40 * @group fetch_visibility_level 41 */ 42 public function test_fetch_visibility_level() { 43 $u = self::factory()->user->create(); 44 $g = self::factory()->xprofile_group->create(); 45 $f = self::factory()->xprofile_field->create( array( 46 'field_group_id' => $g, 47 ) ); 48 49 $f_obj = new BP_XProfile_Field( $f ); 50 51 $fields = array( 52 0 => new stdClass, 53 ); 54 55 $fields[0]->id = $f; 56 $fields[0]->name = $f_obj->name; 57 $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]->is_required = $f_obj->is_required; 61 $fields[0]->data = new stdClass; 62 $fields[0]->data->value = 'foo'; 63 $fields[0]->data->id = 123; 64 65 // custom visibility enabled, but no fallback 66 bp_xprofile_update_meta( $f, 'field', 'default_visibility', 'adminsonly' ); 67 bp_xprofile_update_meta( $f, 'field', 'allow_custom_visibility', 'enabled' ); 68 69 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 70 71 $expected = $fields; 72 $expected[0]->visibility_level = 'adminsonly'; 73 74 $this->assertSame( $expected, $found ); 75 76 // custom visibility enabled, with user-provided value 77 bp_xprofile_update_meta( $f, 'field', 'default_visibility', 'adminsonly' ); 78 bp_xprofile_update_meta( $f, 'field', 'allow_custom_visibility', 'enabled' ); 79 xprofile_set_field_visibility_level( $f, $u, 'public' ); 80 81 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 82 83 $expected = $fields; 84 $expected[0]->visibility_level = 'public'; 85 86 $this->assertSame( $expected, $found ); 87 88 // custom visibility disabled 89 bp_xprofile_update_meta( $f, 'field', 'default_visibility', 'adminsonly' ); 90 bp_xprofile_update_meta( $f, 'field', 'allow_custom_visibility', 'disabled' ); 91 xprofile_set_field_visibility_level( $f, $u, 'public' ); 92 93 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 94 95 $expected = $fields; 96 $expected[0]->visibility_level = 'adminsonly'; 97 98 $this->assertSame( $expected, $found ); 99 } 100 101 /** 102 * @group get_xprofile_groups 103 */ 104 public function test_get_xprofile_groups() { 105 $g1 = self::factory()->xprofile_group->create(); 106 $g2 = self::factory()->xprofile_group->create(); 107 $g3 = self::factory()->xprofile_group->create(); 108 109 $all = BP_XProfile_Group::get(); 110 $all_results = array_map( 'absint', wp_list_pluck( $all, 'id' ) ); 111 112 $e1 = array( $g1, $g2 ); 113 $groups1 = BP_XProfile_Group::get( array( 114 'exclude_groups' => implode( ',', $e1 ), 115 ) ); 116 117 $r_groups1 = array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ); 118 $found1 = array_diff( $all_results, $r_groups1 ); 119 120 $this->assertSame( $e1, array_merge( $found1, array() ) ); 121 122 $e2 = array( $g2, $g3 ); 123 $groups2 = BP_XProfile_Group::get( array( 124 'exclude_groups' => $e2, 125 ) ); 126 127 $r_groups2 = array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ); 128 $found2 = array_diff( $all_results, $r_groups2 ); 129 130 $this->assertSame( $e2, array_merge( $found2, array() ) ); 131 } 132 133 /** 134 * @group member_types 135 * @ticket BP5192 136 */ 137 public function test_member_type_restrictions_should_be_ignored_when_user_id_is_null_and_member_type_is_not_explicitly_provided() { 138 $g = self::factory()->xprofile_group->create(); 139 $f = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 140 bp_register_member_type( 'foo' ); 141 142 $field = new BP_XProfile_Field( $f ); 143 $field->set_member_types( array( 'foo' ) ); 144 145 $found_groups = BP_XProfile_Group::get( array( 146 'user_id' => false, 147 'fetch_fields' => true, 148 ) ); 149 150 // The groups aren't indexed, so we have to go looking for it. 151 foreach ( $found_groups as $fg ) { 152 if ( $g == $fg->id ) { 153 $the_group = $fg; 154 } 155 } 156 157 $this->assertContains( $f, wp_list_pluck( $the_group->fields, 'id' ) ); 158 } 159 160 /** 161 * @group member_types 162 * @ticket BP5192 163 */ 164 public function test_member_type_restrictions_should_be_ignored_when_user_id_is_0_and_member_type_is_false() { 165 $g = self::factory()->xprofile_group->create(); 166 $f = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 167 bp_register_member_type( 'foo' ); 168 169 $field = new BP_XProfile_Field( $f ); 170 $field->set_member_types( array( 'foo' ) ); 171 172 $found_groups = BP_XProfile_Group::get( array( 173 'user_id' => 0, 174 'member_type' => false, 175 'fetch_fields' => true, 176 ) ); 177 178 // The groups aren't indexed, so we have to go looking for it. 179 foreach ( $found_groups as $fg ) { 180 if ( $g == $fg->id ) { 181 $the_group = $fg; 182 } 183 } 184 185 $this->assertContains( $f, wp_list_pluck( $the_group->fields, 'id' ) ); 186 } 187 188 /** 189 * @group member_types 190 * @ticket BP5192 191 */ 192 public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id() { 193 $g = self::factory()->xprofile_group->create(); 194 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 195 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 196 $f3 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 197 $f4 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 198 bp_register_member_type( 'foo' ); 199 bp_register_member_type( 'bar' ); 200 201 // Field 1 is visible only to 'foo' users. 202 $field1 = new BP_XProfile_Field( $f1 ); 203 $field1->set_member_types( array( 'foo' ) ); 204 205 // Field 2 is visible only to 'bar' users. 206 $field2 = new BP_XProfile_Field( $f2 ); 207 $field2->set_member_types( array( 'bar' ) ); 208 209 // Field 3 is visible to all users (no member type set). 210 211 // Field 4 is visible to no one. 212 $field4 = new BP_XProfile_Field( $f4 ); 213 $field4->set_member_types( array() ); 214 215 // User is in 'foo', so should have f1 and f3 only. 216 $u = self::factory()->user->create(); 217 bp_set_member_type( $u, 'foo' ); 218 219 $found_groups = BP_XProfile_Group::get( array( 220 'user_id' => $u, 221 'fetch_fields' => true, 222 ) ); 223 224 // The groups aren't indexed, so we have to go looking for it. 225 foreach ( $found_groups as $fg ) { 226 if ( $g == $fg->id ) { 227 $the_group = $fg; 228 } 229 } 230 231 $this->assertContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) ); 232 $this->assertContains( $f3, wp_list_pluck( $the_group->fields, 'id' ) ); 233 $this->assertNotContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) ); 234 $this->assertNotContains( $f4, wp_list_pluck( $the_group->fields, 'id' ) ); 235 } 236 237 /** 238 * @group member_types 239 * @ticket BP5192 240 */ 241 public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id_with_no_member_types() { 242 $g = self::factory()->xprofile_group->create(); 243 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 244 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 245 $f3 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 246 bp_register_member_type( 'foo' ); 247 bp_register_member_type( 'bar' ); 248 249 // Field 1 is visible only to 'foo' users. 250 $field1 = new BP_XProfile_Field( $f1 ); 251 $field1->set_member_types( array( 'foo' ) ); 252 253 // Field 2 is visible only to 'null' users. 254 $field2 = new BP_XProfile_Field( $f2 ); 255 $field2->set_member_types( array( 'null' ) ); 256 257 // Field 3 is visible to all users (no member type set). 258 259 // User has no member types, so should see f2 and f3 . 260 $u = self::factory()->user->create(); 261 262 $found_groups = BP_XProfile_Group::get( array( 263 'user_id' => $u, 264 'fetch_fields' => true, 265 ) ); 266 267 // The groups aren't indexed, so we have to go looking for it. 268 foreach ( $found_groups as $fg ) { 269 if ( $g == $fg->id ) { 270 $the_group = $fg; 271 } 272 } 273 274 $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) ); 275 $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) ); 276 $this->assertContains( $f3, wp_list_pluck( $the_group->fields, 'id' ) ); 277 } 278 279 /** 280 * @group member_types 281 * @ticket BP5192 282 */ 283 public function test_member_types_of_provided_user_id_should_take_precedence_over_provided_member_type() { 284 $g = self::factory()->xprofile_group->create(); 285 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 286 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 287 bp_register_member_type( 'foo' ); 288 bp_register_member_type( 'bar' ); 289 290 $field1 = new BP_XProfile_Field( $f1 ); 291 $field1->set_member_types( array( 'foo' ) ); 292 $field2 = new BP_XProfile_Field( $f2 ); 293 $field2->set_member_types( array( 'bar' ) ); 294 295 $u = self::factory()->user->create(); 296 bp_set_member_type( $u, 'foo' ); 297 298 $found_groups = BP_XProfile_Group::get( array( 299 'user_id' => $u, 300 'member_type' => 'bar', 301 'fetch_fields' => true, 302 ) ); 303 304 // The groups aren't indexed, so we have to go looking for it. 305 foreach ( $found_groups as $fg ) { 306 if ( $g == $fg->id ) { 307 $the_group = $fg; 308 } 309 } 310 311 $this->assertContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) ); 312 } 313 314 /** 315 * @group member_types 316 * @ticket BP5192 317 */ 318 public function test_member_type_single_value_should_be_respected() { 319 $g = self::factory()->xprofile_group->create(); 320 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 321 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 322 bp_register_member_type( 'foo' ); 323 bp_register_member_type( 'bar' ); 324 325 $field1 = new BP_XProfile_Field( $f1 ); 326 $field1->set_member_types( array( 'foo' ) ); 327 $field2 = new BP_XProfile_Field( $f2 ); 328 $field2->set_member_types( array( 'bar' ) ); 329 330 $found_groups = BP_XProfile_Group::get( array( 331 'member_type' => 'bar', 332 'fetch_fields' => true, 333 ) ); 334 335 // The groups aren't indexed, so we have to go looking for it. 336 foreach ( $found_groups as $fg ) { 337 if ( $g == $fg->id ) { 338 $the_group = $fg; 339 } 340 } 341 342 $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) ); 343 $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) ); 344 } 345 346 /** 347 * @group member_types 348 * @ticket BP5192 349 */ 350 public function test_member_type_array_value_should_be_respected() { 351 $g = self::factory()->xprofile_group->create(); 352 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 353 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 354 bp_register_member_type( 'foo' ); 355 bp_register_member_type( 'bar' ); 356 357 $field1 = new BP_XProfile_Field( $f1 ); 358 $field1->set_member_types( array( 'foo' ) ); 359 $field2 = new BP_XProfile_Field( $f2 ); 360 $field2->set_member_types( array( 'bar' ) ); 361 362 $found_groups = BP_XProfile_Group::get( array( 363 'member_type' => array( 'bar' ), 364 'fetch_fields' => true, 365 ) ); 366 367 // The groups aren't indexed, so we have to go looking for it. 368 foreach ( $found_groups as $fg ) { 369 if ( $g == $fg->id ) { 370 $the_group = $fg; 371 } 372 } 373 374 $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) ); 375 $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) ); 376 } 377 378 /** 379 * @group member_types 380 * @ticket BP5192 381 */ 382 public function test_member_type_null_should_be_respected() { 383 $g = self::factory()->xprofile_group->create(); 384 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 385 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 386 bp_register_member_type( 'foo' ); 387 bp_register_member_type( 'bar' ); 388 389 $field1 = new BP_XProfile_Field( $f1 ); 390 $field1->set_member_types( array( 'foo' ) ); 391 392 $found_groups = BP_XProfile_Group::get( array( 393 'member_type' => array( 'null' ), 394 'fetch_fields' => true, 395 ) ); 396 397 // The groups aren't indexed, so we have to go looking for it. 398 foreach ( $found_groups as $fg ) { 399 if ( $g == $fg->id ) { 400 $the_group = $fg; 401 } 402 } 403 404 $this->assertNotContains( $f1, wp_list_pluck( $the_group->fields, 'id' ) ); 405 $this->assertContains( $f2, wp_list_pluck( $the_group->fields, 'id' ) ); 406 } 407 408 /** 409 * @group save_xprofile_group_name 410 */ 411 public function test_save_xprofile_group_name() { 412 $g1 = self::factory()->xprofile_group->create( array( 413 'name' => "Test ' Name" 414 ) ); 415 416 $e1 = new BP_XProfile_Group( $g1 ); 417 $e1->save(); 418 419 wp_cache_delete( $g1, 'bp_xprofile_groups' ); 420 421 $e2 = new BP_XProfile_Group( $g1 ); 422 423 $this->assertSame( $e1->name, $e2->name ); 424 } 425 426 /** 427 * @group save_xprofile_group_name 428 */ 429 public function test_save_xprofile_group_name_with_single_quote() { 430 431 // Set the original group name with no slashes 432 $pristine_name = "Test \' Name"; 433 434 // Create a group 435 $g1 = self::factory()->xprofile_group->create( array( 436 'name' => $pristine_name 437 ) ); 438 439 // Get the field 440 $e1 = new BP_XProfile_Group( $g1 ); 441 442 $this->assertSame( "Test ' Name", $e1->name ); 443 } 444 445 /** 446 * @group BP7435 447 * @group cache 448 */ 449 public function test_group_ids_query_should_be_cached() { 450 global $wpdb; 451 452 $group_ids = array( 1 ); // Default group. 453 $group_ids[] = self::factory()->xprofile_group->create(); 454 $group_ids[] = self::factory()->xprofile_group->create(); 455 $group_ids[] = self::factory()->xprofile_group->create(); 456 457 $params_1 = array( 458 'exclude_groups' => false, 459 ); 460 461 $params_2 = array( 462 'exclude_groups' => array( 0 ), 463 ); 464 465 // Prime cache. 466 $found_1 = BP_XProfile_Group::get_group_ids( $params_1 ); 467 $this->assertEqualSets( $group_ids, $found_1 ); 468 469 $num_queries = $wpdb->num_queries; 470 471 $found_1 = BP_XProfile_Group::get_group_ids( $params_1 ); 472 $this->assertEqualSets( $group_ids, $found_1 ); 473 $this->assertSame( $num_queries, $wpdb->num_queries ); 474 475 // Different parameters should trigger a cache miss. 476 $found_2 = BP_XProfile_Group::get_group_ids( $params_2 ); 477 $this->assertEqualSets( $group_ids, $found_2 ); 478 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 479 } 480 481 /** 482 * @group BP7435 483 * @group cache 484 */ 485 public function test_group_ids_query_cache_should_be_busted_on_group_delete() { 486 $group_ids = array( 1 ); // Default group. 487 $group_ids[1] = self::factory()->xprofile_group->create(); 488 $group_ids[2] = self::factory()->xprofile_group->create(); 489 $group_ids[3] = self::factory()->xprofile_group->create(); 490 491 // Prime cache. 492 $found = BP_XProfile_Group::get_group_ids(); 493 $this->assertContains( $group_ids[1], $found ); 494 495 $g1 = new BP_XProfile_Group( $group_ids[1] ); 496 $g1->delete(); 497 498 $found = BP_XProfile_Group::get_group_ids(); 499 $this->assertNotContains( $group_ids[1], $found ); 500 } 501 502 /** 503 * @group BP7435 504 * @group cache 505 */ 506 public function test_group_ids_query_cache_should_be_busted_on_group_save() { 507 global $wpdb; 508 509 $group_ids = array( 1 ); // Default group. 510 $group_ids[1] = self::factory()->xprofile_group->create(); 511 $group_ids[2] = self::factory()->xprofile_group->create(); 512 $group_ids[3] = self::factory()->xprofile_group->create(); 513 514 // Prime cache. 515 $found = BP_XProfile_Group::get_group_ids(); 516 $this->assertContains( $group_ids[1], $found ); 517 518 $g1 = new BP_XProfile_Group( $group_ids[1] ); 519 $g1->save(); 520 521 $num_queries = $wpdb->num_queries; 522 523 $found = BP_XProfile_Group::get_group_ids(); 524 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 525 } 526 527 /** 528 * @group BP7435 529 * @group cache 530 */ 531 public function test_group_ids_query_cache_should_be_busted_on_field_save() { 532 global $wpdb; 533 534 $group_ids = array( 1 ); // Default group. 535 $group_ids[1] = self::factory()->xprofile_group->create(); 536 $group_ids[2] = self::factory()->xprofile_group->create(); 537 $group_ids[3] = self::factory()->xprofile_group->create(); 538 539 $f = self::factory()->xprofile_field->create( array( 540 'field_group_id' => $group_ids[1], 541 ) ); 542 $field = new BP_XProfile_Field( $f ); 543 544 // Prime cache. 545 $found = BP_XProfile_Group::get_group_ids(); 546 $this->assertContains( $group_ids[1], $found ); 547 548 $field->save(); 549 550 $num_queries = $wpdb->num_queries; 551 552 $found = BP_XProfile_Group::get_group_ids(); 553 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 554 } 555 556 /** 557 * @group BP7435 558 * @group cache 559 */ 560 public function test_group_ids_query_cache_should_be_busted_on_field_delete() { 561 $group_ids = array( 1 ); // Default group. 562 $group_ids[1] = self::factory()->xprofile_group->create(); 563 $group_ids[2] = self::factory()->xprofile_group->create(); 564 $group_ids[3] = self::factory()->xprofile_group->create(); 565 566 $f = self::factory()->xprofile_field->create( array( 567 'field_group_id' => $group_ids[1], 568 ) ); 569 $field = new BP_XProfile_Field( $f ); 570 571 $args = array( 572 'hide_empty_groups' => true, 573 ); 574 575 // Prime cache. 576 $found = BP_XProfile_Group::get_group_ids( $args ); 577 $this->assertContains( $group_ids[1], $found ); 578 579 $field->delete(); 580 581 $found = BP_XProfile_Group::get_group_ids( $args ); 582 $this->assertNotContains( $group_ids[1], $found ); 583 } 584 585 /** 586 * @group BP7435 587 * @group cache 588 */ 589 public function test_group_field_ids_query_cache() { 590 global $wpdb; 591 592 $group_id = self::factory()->xprofile_group->create(); 593 594 $f = self::factory()->xprofile_field->create( array( 595 'field_group_id' => $group_id, 596 ) ); 597 $field = new BP_XProfile_Field( $f ); 598 599 // Prime cache. 600 $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) ); 601 $this->assertContains( $f, $found ); 602 603 $num_queries = $wpdb->num_queries; 604 605 $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) ); 606 $this->assertContains( $f, $found ); 607 $this->assertSame( $num_queries, $wpdb->num_queries ); 608 } 609 610 /** 611 * @group BP7435 612 * @group cache 613 */ 614 public function test_group_field_ids_query_cache_should_be_busted_on_field_save() { 615 global $wpdb; 616 617 $group_id = self::factory()->xprofile_group->create(); 618 619 $f = self::factory()->xprofile_field->create( array( 620 'field_group_id' => $group_id, 621 ) ); 622 $field = new BP_XProfile_Field( $f ); 623 624 // Prime cache. 625 $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) ); 626 $this->assertContains( $f, $found ); 627 628 $field->save(); 629 $num_queries = $wpdb->num_queries; 630 631 $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) ); 632 $this->assertContains( $f, $found ); 633 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 634 } 635 636 /** 637 * @group BP7435 638 * @group cache 639 */ 640 public function test_group_field_ids_query_cache_should_be_busted_on_field_delete() { 641 $group_id = self::factory()->xprofile_group->create(); 642 643 $f = self::factory()->xprofile_field->create( array( 644 'field_group_id' => $group_id, 645 ) ); 646 $field = new BP_XProfile_Field( $f ); 647 648 // Prime cache. 649 $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) ); 650 $this->assertContains( $f, $found ); 651 652 $field->delete(); 653 654 $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) ); 655 $this->assertNotContains( $f, $found ); 656 } 657 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 22 01:00:56 2024 | Cross-referenced by PHPXref 0.7.1 |