[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @group friends 5 * @group functions 6 */ 7 class BP_Tests_Friends_Functions extends BP_UnitTestCase { 8 static public $user_ids; 9 10 protected $filter_fired; 11 12 public function setUp() { 13 parent::setUp(); 14 $this->filter_fired = ''; 15 } 16 17 public static function wpSetUpBeforeClass( $factory ) { 18 $now = time(); 19 20 self::$user_ids[0] = $factory->user->create( array( 21 'last_activity' => date( 'Y-m-d H:i:s', $now ), 22 ) ); 23 self::$user_ids[1] = $factory->user->create( array( 24 'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ), 25 ) ); 26 self::$user_ids[2] = $factory->user->create( array( 27 'last_activity' => date( 'Y-m-d H:i:s', $now - 200 ), 28 ) ); 29 self::$user_ids[3] = $factory->user->create( array( 30 'last_activity' => date( 'Y-m-d H:i:s', $now - 300 ), 31 ) ); 32 self::$user_ids[4] = $factory->user->create( array( 33 'last_activity' => date( 'Y-m-d H:i:s', $now - 400 ), 34 ) ); 35 } 36 37 public static function wpTearDownAfterClass() { 38 foreach ( self::$user_ids as $user_id ) { 39 self::delete_user( $user_id ); 40 } 41 } 42 43 /** 44 * @group friends_get_friendship_request_user_ids 45 * @group friends_add_friend 46 * @group friends_accept_friendship 47 */ 48 public function test_requests_on_accept() { 49 $u1 = self::$user_ids[0]; 50 $u2 = self::$user_ids[1]; 51 $u3 = self::$user_ids[2]; 52 53 // request friendship 54 friends_add_friend( $u2, $u1 ); 55 friends_add_friend( $u3, $u1 ); 56 57 // Set the time of the earlier friendship for reliable ordering of the results. 58 $fid = friends_get_friendship_id( $u2, $u1 ); 59 $friendship = new BP_Friends_Friendship( $fid, false, false ); 60 $friendship->date_created = date( 'Y-m-d H:i:s', time() - 60 ); 61 $friendship->save(); 62 63 // get request count for user 1 and assert 64 $requests = friends_get_friendship_request_user_ids( $u1 ); 65 $this->assertEquals( array( $u3, $u2 ), $requests ); 66 67 // accept friendship 68 $old_user = get_current_user_id(); 69 $this->set_current_user( $u1 ); 70 friends_accept_friendship( friends_get_friendship_id( $u2, $u1 ) ); 71 72 // refetch request count for user 1 and assert 73 $requests = friends_get_friendship_request_user_ids( $u1 ); 74 $this->assertEquals( array( $u3 ), $requests ); 75 76 $this->set_current_user( $old_user ); 77 } 78 79 /** 80 * @group friends_get_friendship_request_user_ids 81 * @group friends_add_friend 82 */ 83 public function test_requests_on_request() { 84 $u1 = self::$user_ids[0]; 85 $u2 = self::$user_ids[1]; 86 $u3 = self::$user_ids[2]; 87 88 // request friendship 89 friends_add_friend( $u2, $u1 ); 90 // Set the time of the earlier friendship for reliable ordering of the results. 91 $fid = friends_get_friendship_id( $u2, $u1 ); 92 $friendship = new BP_Friends_Friendship( $fid, false, false ); 93 $friendship->date_created = date( 'Y-m-d H:i:s', time() - 60 ); 94 $friendship->save(); 95 96 // get request count for user 1 and assert 97 $requests = friends_get_friendship_request_user_ids( $u1 ); 98 $this->assertEquals( array( $u2 ), $requests ); 99 100 // request another friendship 101 friends_add_friend( $u3, $u1 ); 102 103 // refetch request count for user 1 and assert 104 $requests = friends_get_friendship_request_user_ids( $u1 ); 105 $this->assertEquals( array( $u3, $u2 ), $requests ); 106 } 107 108 /** 109 * @group friends_get_friendship_request_user_ids 110 * @group friends_add_friend 111 * @group friends_withdraw_friendship 112 */ 113 public function test_requests_on_withdraw() { 114 $u1 = self::$user_ids[0]; 115 $u2 = self::$user_ids[1]; 116 117 // request friendship 118 friends_add_friend( $u2, $u1 ); 119 120 // get request count for user 1 and assert 121 $requests = friends_get_friendship_request_user_ids( $u1 ); 122 $this->assertEquals( array( $u2 ), $requests ); 123 124 // user 2 withdraws friendship 125 $old_user = get_current_user_id(); 126 $this->set_current_user( $u2 ); 127 friends_withdraw_friendship( $u2, $u1 ); 128 129 // refetch request count for user 1 and assert 130 $requests = friends_get_friendship_request_user_ids( $u1 ); 131 $this->assertEquals( array(), $requests ); 132 133 $this->set_current_user( $old_user ); 134 } 135 136 /** 137 * @group friends_get_friendship_request_user_ids 138 * @group friends_add_friend 139 * @group friends_reject_friendship 140 */ 141 public function test_requests_on_reject() { 142 $u1 = self::$user_ids[0]; 143 $u2 = self::$user_ids[1]; 144 145 // request friendship 146 friends_add_friend( $u2, $u1 ); 147 148 // get request count for user 1 and assert 149 $requests = friends_get_friendship_request_user_ids( $u1 ); 150 $this->assertEquals( array( $u2 ), $requests ); 151 152 // user 1 rejects friendship 153 $old_user = get_current_user_id(); 154 $this->set_current_user( $u1 ); 155 friends_reject_friendship( friends_get_friendship_id( $u2, $u1 ) ); 156 157 // refetch request count for user 1 and assert 158 $requests = friends_get_friendship_request_user_ids( $u1 ); 159 $this->assertEquals( array(), $requests ); 160 161 $this->set_current_user( $old_user ); 162 } 163 164 /** 165 * @group friends_add_friend 166 */ 167 public function test_friends_add_friend_fail_on_self() { 168 $u1 = self::$user_ids[0]; 169 $this->assertFalse( friends_add_friend( $u1, $u1 ) ); 170 } 171 172 /** 173 * @group friends_add_friend 174 */ 175 public function test_friends_add_friend_already_friends() { 176 $u1 = self::$user_ids[0]; 177 $u2 = self::$user_ids[1]; 178 179 friends_add_friend( $u1, $u2, true ); 180 181 $this->assertTrue( friends_add_friend( $u1, $u2 ) ); 182 } 183 184 /** 185 * @group friends_check_friendship_status 186 */ 187 public function test_friends_check_friendship_status_in_members_loop() { 188 $now = time(); 189 $u1 = self::$user_ids[0]; 190 $u2 = self::$user_ids[1]; 191 $u3 = self::$user_ids[2]; 192 $u4 = self::$user_ids[3]; 193 $u5 = self::$user_ids[4]; 194 195 friends_add_friend( $u1, $u2, true ); 196 friends_add_friend( $u1, $u3, false ); 197 friends_add_friend( $u4, $u1, false ); 198 199 $old_user = get_current_user_id(); 200 $this->set_current_user( $u1 ); 201 202 $found = array(); 203 if ( bp_has_members() ) : while ( bp_members() ) : bp_the_member(); 204 $found[ bp_get_member_user_id() ] = friends_check_friendship_status( $u1, bp_get_member_user_id() ); 205 endwhile; endif; 206 207 $expected = array( 208 $u1 => 'not_friends', 209 $u2 => 'is_friend', 210 $u3 => 'pending', 211 $u4 => 'awaiting_response', 212 $u5 => 'not_friends', 213 ); 214 215 $this->assertSame( $expected, $found ); 216 217 // clean up 218 $GLOBALS['members_template'] = null; 219 $this->set_current_user( $old_user ); 220 } 221 222 /** 223 * @group friends_check_friendship_status 224 */ 225 public function test_friends_check_friendship_status_not_in_members_loop() { 226 $u1 = self::$user_ids[0]; 227 $u2 = self::$user_ids[1]; 228 $u3 = self::$user_ids[2]; 229 $u4 = self::$user_ids[3]; 230 $u5 = self::$user_ids[4]; 231 232 friends_add_friend( $u1, $u2, true ); 233 friends_add_friend( $u1, $u3, false ); 234 friends_add_friend( $u4, $u1, false ); 235 236 $found = array( 237 $u1 => friends_check_friendship_status( $u1, $u1 ), 238 $u2 => friends_check_friendship_status( $u1, $u2 ), 239 $u3 => friends_check_friendship_status( $u1, $u3 ), 240 $u4 => friends_check_friendship_status( $u1, $u4 ), 241 $u5 => friends_check_friendship_status( $u1, $u5 ), 242 ); 243 244 $expected = array( 245 $u1 => 'not_friends', 246 $u2 => 'is_friend', 247 $u3 => 'pending', 248 $u4 => 'awaiting_response', 249 $u5 => 'not_friends', 250 ); 251 252 $this->assertSame( $expected, $found ); 253 } 254 255 /** 256 * @group friends_add_friend 257 */ 258 public function test_friends_add_friend_friends_friendship_requested() { 259 $u1 = self::$user_ids[0]; 260 $u2 = self::$user_ids[1]; 261 262 add_filter( 'friends_friendship_requested', array( $this, 'friends_friendship_filter_callback' ) ); 263 $n = friends_add_friend( $u1, $u2, false ); 264 remove_filter( 'friends_friendship_requested', array( $this, 'friends_friendship_filter_callback' ) ); 265 266 $this->assertSame( 'friends_friendship_requested', $this->filter_fired ); 267 } 268 269 /** 270 * @group friends_add_friend 271 */ 272 public function test_friends_add_friend_friends_friendship_accepted() { 273 $u1 = self::$user_ids[0]; 274 $u2 = self::$user_ids[1]; 275 276 add_filter( 'friends_friendship_accepted', array( $this, 'friends_friendship_filter_callback' ) ); 277 $n = friends_add_friend( $u1, $u2, true ); 278 remove_filter( 'friends_friendship_accepted', array( $this, 'friends_friendship_filter_callback' ) ); 279 280 $this->assertSame( 'friends_friendship_accepted', $this->filter_fired ); 281 } 282 283 public function friends_friendship_filter_callback( $value ) { 284 $this->filter_fired = current_filter(); 285 return $value; 286 } 287 288 /** 289 * @group friends_remove_friend 290 */ 291 public function test_friends_check_is_friend_after_remove() { 292 $old_user = get_current_user_id(); 293 $u1 = self::$user_ids[0]; 294 $u2 = self::$user_ids[1]; 295 296 /* 297 * Pretend user 1 is on the Members Directory page. 298 * 299 * Also primes the friendship cache in the process. 300 * @see bp_friends_filter_user_query_populate_extras() 301 */ 302 $this->set_current_user( $u1 ); 303 $this->go_to( bp_get_members_directory_permalink() ); 304 ob_start(); 305 bp_get_template_part( 'members/members-loop' ); 306 ob_end_clean(); 307 308 // User 1 initiates friendship. 309 friends_add_friend( $u1, $u2 ); 310 311 /* 312 * Pretend user 2 is logged in and accepts friendship. 313 * 314 * User 2 needs to be logged in for friends_accept_friendship() to work 315 * properly. 316 */ 317 $this->set_current_user( $u2 ); 318 friends_accept_friendship( friends_get_friendship_id( $u1, $u2 ) ); 319 320 // Afterwards, user 1 decides to cancel friendship. 321 $this->set_current_user( $u1 ); 322 friends_remove_friend( $u1, $u2 ); 323 324 // Assert that users are no longer friends. 325 $this->assertEquals( 'not_friends', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) ); 326 327 $this->set_current_user( $old_user ); 328 } 329 330 /** 331 * @group friendship_caching 332 */ 333 public function test_friends_check_friendship_should_hit_friendship_object_cache() { 334 global $wpdb; 335 336 $u1 = self::$user_ids[0]; 337 $u2 = self::$user_ids[1]; 338 339 friends_add_friend( $u1, $u2, true ); 340 341 friends_check_friendship_status( $u1, $u2 ); 342 $first_query_count = $wpdb->num_queries; 343 344 /* 345 * This should access the previous friendship check's cached items. 346 */ 347 friends_check_friendship_status( $u2, $u1 ); 348 349 $this->assertEquals( $first_query_count, $wpdb->num_queries ); 350 } 351 352 public function test_friends_get_recently_active() { 353 $u1 = self::$user_ids[0]; 354 $u2 = self::$user_ids[1]; 355 $u3 = self::$user_ids[2]; 356 $u4 = self::$user_ids[3]; 357 358 // request friendship 359 friends_add_friend( $u1, $u2, true ); 360 friends_add_friend( $u1, $u3, true ); 361 friends_add_friend( $u1, $u4, true ); 362 363 bp_update_user_last_activity( $u2, date( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) ) ); 364 bp_update_user_last_activity( $u3, date( 'Y-m-d H:i:s', time() - ( 5 * DAY_IN_SECONDS ) ) ); 365 bp_update_user_last_activity( $u4, date( 'Y-m-d H:i:s', time() - ( 3 * DAY_IN_SECONDS ) ) ); 366 367 $recent = friends_get_recently_active( $u1 ); 368 369 $this->assertEquals( $recent['users'][0]->id, $u2 ); 370 $this->assertEquals( $recent['users'][1]->id, $u4 ); 371 $this->assertEquals( $recent['users'][2]->id, $u3 ); 372 } 373 374 public function test_friends_get_alphabetically() { 375 $u1 = self::$user_ids[0]; 376 $u2 = self::$user_ids[1]; 377 $u3 = self::$user_ids[2]; 378 379 // request friendship 380 friends_add_friend( $u1, $u2, true ); 381 friends_add_friend( $u1, $u3, true ); 382 383 $field_id = bp_xprofile_fullname_field_id(); 384 xprofile_set_field_data( $field_id, $u2, 'Dave Lister' ); 385 xprofile_set_field_data( $field_id, $u3, 'Arnold Rimmer' ); 386 xprofile_sync_wp_profile( $u2 ); 387 xprofile_sync_wp_profile( $u3 ); 388 389 $alpha = friends_get_alphabetically( $u1 ); 390 391 $this->assertEquals( $alpha['users'][0]->id, $u3 ); 392 $this->assertEquals( $alpha['users'][1]->id, $u2 ); 393 } 394 395 public function test_friends_get_newest() { 396 $u1 = self::$user_ids[0]; 397 $u2 = self::$user_ids[1]; 398 $u3 = self::$user_ids[2]; 399 $u4 = self::$user_ids[3]; 400 401 // request friendship 402 friends_add_friend( $u1, $u2, true ); 403 friends_add_friend( $u1, $u3, true ); 404 friends_add_friend( $u1, $u4, true ); 405 406 bp_update_user_last_activity( $u2, date( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) ) ); 407 bp_update_user_last_activity( $u3, date( 'Y-m-d H:i:s', time() - ( 5 * DAY_IN_SECONDS ) ) ); 408 bp_update_user_last_activity( $u4, date( 'Y-m-d H:i:s', time() - ( 3 * DAY_IN_SECONDS ) ) ); 409 410 $newest = friends_get_newest( $u1 ); 411 412 $this->assertEquals( $newest['users'][0]->id, $u4 ); 413 $this->assertEquals( $newest['users'][1]->id, $u3 ); 414 $this->assertEquals( $newest['users'][2]->id, $u2 ); 415 } 416 417 /** 418 * @ticket BP7821 419 * @ticket BP7698 420 */ 421 public function test_bp_friends_personal_data_exporter() { 422 friends_add_friend( self::$user_ids[0], self::$user_ids[1], true ); 423 friends_add_friend( self::$user_ids[0], self::$user_ids[2], false ); 424 friends_add_friend( self::$user_ids[3], self::$user_ids[0], true ); 425 friends_add_friend( self::$user_ids[4], self::$user_ids[0], false ); 426 427 $test_user = new WP_User( self::$user_ids[0] ); 428 429 $actual = bp_friends_personal_data_exporter( $test_user->user_email, 1 ); 430 431 $this->assertTrue( $actual['done'] ); 432 $this->assertCount( 2, $actual['data'] ); 433 434 $expected_ids = array( 435 'bp-friends-' . self::$user_ids[1], 436 'bp-friends-' . self::$user_ids[3], 437 ); 438 $this->assertEqualSets( $expected_ids, wp_list_pluck( $actual['data'], 'item_id' ) ); 439 } 440 441 /** 442 * @ticket BP7821 443 * @ticket BP7698 444 */ 445 public function test_bp_friends_pending_sent_requests_personal_data_exporter() { 446 friends_add_friend( self::$user_ids[0], self::$user_ids[1], true ); 447 friends_add_friend( self::$user_ids[0], self::$user_ids[2], false ); 448 friends_add_friend( self::$user_ids[3], self::$user_ids[0], true ); 449 friends_add_friend( self::$user_ids[4], self::$user_ids[0], false ); 450 451 $test_user = new WP_User( self::$user_ids[0] ); 452 453 $actual = bp_friends_pending_sent_requests_personal_data_exporter( $test_user->user_email, 1 ); 454 455 $this->assertTrue( $actual['done'] ); 456 $this->assertCount( 1, $actual['data'] ); 457 458 $expected_ids = array( 459 'bp-friends-pending-sent-request-' . self::$user_ids[2], 460 ); 461 $this->assertEqualSets( $expected_ids, wp_list_pluck( $actual['data'], 'item_id' ) ); 462 } 463 464 /** 465 * @ticket BP7821 466 * @ticket BP7698 467 */ 468 public function test_bp_friends_pending_received_requests_personal_data_exporter() { 469 friends_add_friend( self::$user_ids[0], self::$user_ids[1], true ); 470 friends_add_friend( self::$user_ids[0], self::$user_ids[2], false ); 471 friends_add_friend( self::$user_ids[3], self::$user_ids[0], true ); 472 friends_add_friend( self::$user_ids[4], self::$user_ids[0], false ); 473 474 $test_user = new WP_User( self::$user_ids[0] ); 475 476 $actual = bp_friends_pending_received_requests_personal_data_exporter( $test_user->user_email, 1 ); 477 478 $this->assertTrue( $actual['done'] ); 479 $this->assertCount( 1, $actual['data'] ); 480 481 $expected_ids = array( 482 'bp-friends-pending-received-request-' . self::$user_ids[4], 483 ); 484 $this->assertEqualSets( $expected_ids, wp_list_pluck( $actual['data'], 'item_id' ) ); 485 } 486 487 /** 488 * @ticket BP8175 489 */ 490 public function test_friends_data_should_be_deleted_on_user_delete_non_multisite() { 491 if ( is_multisite() ) { 492 $this->markTestSkipped( __METHOD__ . ' requires non-multisite.' ); 493 } 494 495 $u1 = self::factory()->user->create(); 496 $u2 = self::factory()->user->create(); 497 498 friends_add_friend( $u1, $u2, true ); 499 500 $this->assertEquals( 'is_friend', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) ); 501 502 wp_delete_user( $u1 ); 503 504 $this->assertEquals( 'not_friends', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) ); 505 } 506 507 /** 508 * @ticket BP8175 509 */ 510 public function test_xprofile_data_should_be_deleted_on_user_delete_multisite() { 511 if ( ! is_multisite() ) { 512 $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); 513 } 514 515 $u1 = self::factory()->user->create(); 516 $u2 = self::factory()->user->create(); 517 518 friends_add_friend( $u1, $u2, true ); 519 520 $this->assertEquals( 'is_friend', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) ); 521 522 wpmu_delete_user( $u1 ); 523 524 $this->assertEquals( 'not_friends', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) ); 525 } 526 527 /** 528 * @ticket BP8175 529 */ 530 public function test_xprofile_data_should_not_be_deleted_on_wp_delete_user_multisite() { 531 if ( ! is_multisite() ) { 532 $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); 533 } 534 535 $u1 = self::factory()->user->create(); 536 $u2 = self::factory()->user->create(); 537 538 friends_add_friend( $u1, $u2, true ); 539 540 $this->assertEquals( 'is_friend', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) ); 541 542 wp_delete_user( $u1 ); 543 544 $this->assertEquals( 'is_friend', BP_Friends_Friendship::check_is_friend( $u1, $u2 ) ); 545 } 546 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Oct 16 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |