[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @group notifications 5 */ 6 class BP_Tests_Notifications_Functions extends BP_UnitTestCase { 7 8 /** 9 * @group cache 10 */ 11 public function test_cache_invalidation_all_for_user_on_save() { 12 $u = self::factory()->user->create(); 13 14 self::factory()->notification->create( array( 15 'component_name' => 'groups', 16 'user_id' => $u 17 ) ); 18 self::factory()->notification->create( array( 19 'component_name' => 'messages', 20 'user_id' => $u, 21 'item_id' => 1 22 ) ); 23 24 // prime cache 25 $count = bp_notifications_get_unread_notification_count( $u ); 26 27 // just to be sure... 28 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 29 30 // Trigger invalidation via save 31 self::factory()->notification->create( array( 32 'component_name' => 'messages', 33 'user_id' => $u, 34 'item_id' => 2 35 ) ); 36 37 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 38 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 39 } 40 41 /** 42 * @group cache 43 */ 44 public function test_cache_invalidation_all_for_user_on_delete() { 45 $u = self::factory()->user->create(); 46 $n1 = self::factory()->notification->create( array( 47 'component_name' => 'groups', 48 'user_id' => $u 49 ) ); 50 self::factory()->notification->create( array( 51 'component_name' => 'messages', 52 'user_id' => $u 53 ) ); 54 55 // prime cache 56 $count = bp_notifications_get_unread_notification_count( $u ); 57 58 // just to be sure... 59 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 60 61 // delete 62 BP_Notifications_Notification::delete( array( 'id' => $n1, ) ); 63 64 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 65 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 66 } 67 68 /** 69 * @group cache 70 */ 71 public function test_cache_invalidation_all_for_user_on_update_user_id() { 72 $u = self::factory()->user->create(); 73 74 self::factory()->notification->create( array( 75 'component_name' => 'groups', 76 'user_id' => $u 77 ) ); 78 self::factory()->notification->create( array( 79 'component_name' => 'messages', 80 'user_id' => $u 81 ) ); 82 83 // prime cache 84 $count = bp_notifications_get_unread_notification_count( $u ); 85 86 // just to be sure... 87 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 88 89 // mark all notifications by user as read 90 BP_Notifications_Notification::update( 91 array( 'is_new' => false ), 92 array( 'user_id' => $u ) 93 ); 94 95 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 96 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 97 } 98 99 /** 100 * @group cache 101 */ 102 public function test_cache_invalidation_all_for_user_on_update_id() { 103 $u = self::factory()->user->create(); 104 $n1 = self::factory()->notification->create( array( 105 'component_name' => 'groups', 106 'user_id' => $u 107 ) ); 108 109 self::factory()->notification->create( array( 110 'component_name' => 'messages', 111 'user_id' => $u 112 ) ); 113 114 // prime cache 115 $count = bp_notifications_get_unread_notification_count( $u ); 116 117 // just to be sure... 118 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 119 120 // mark one notification as read 121 BP_Notifications_Notification::update( 122 array( 'is_new' => false ), 123 array( 'id' => $n1 ) 124 ); 125 126 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 127 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 128 } 129 130 /** 131 * @group bp_notifications_update_meta_cache 132 */ 133 public function test_bp_notifications_update_meta_cache() { 134 $u = self::factory()->user->create(); 135 136 $n1 = self::factory()->notification->create( array( 137 'component_name' => 'messages', 138 'user_id' => $u 139 ) ); 140 141 $n2 = self::factory()->notification->create( array( 142 'component_name' => 'groups', 143 'user_id' => $u 144 ) ); 145 146 // Add cache for each notification. 147 bp_notifications_update_meta( $n1, 'meta', 'data' ); 148 bp_notifications_update_meta( $n1, 'data', 'meta' ); 149 bp_notifications_update_meta( $n2, 'meta', 'human' ); 150 151 // Prime cache. 152 bp_notifications_get_meta( $n1, 'meta' ); 153 154 // Ensure an empty cache for second notification. 155 wp_cache_delete( $n2, 'notification_meta' ); 156 157 // Update notification meta cache. 158 bp_notifications_update_meta_cache( array( $n1, $n2 ) ); 159 160 $expected = array( 161 $n1 => array( 162 'meta' => array( 163 'data', 164 ), 165 'data' => array( 166 'meta', 167 ), 168 ), 169 $n2 => array( 170 'meta' => array( 171 'human', 172 ), 173 ), 174 ); 175 176 $found = array( 177 $n1 => wp_cache_get( $n1, 'notification_meta' ), 178 $n2 => wp_cache_get( $n2, 'notification_meta' ), 179 ); 180 181 $this->assertEquals( $expected, $found ); 182 } 183 184 /** 185 * @group bp_notifications_add_notification 186 */ 187 public function test_bp_notifications_add_notification_no_dupes() { 188 $args = array( 189 'user_id' => 5, 190 'item_id' => 10, 191 'secondary_item_id' => 25, 192 'component_name' => 'messages', 193 'component_action' => 'new_message' 194 ); 195 196 self::factory()->notification->create( $args ); 197 198 $this->assertFalse( bp_notifications_add_notification( $args ) ); 199 } 200 201 /** 202 * @group bp_notifications_add_notification 203 */ 204 public function test_bp_notifications_add_notification_allow_duplicate() { 205 $args = array( 206 'user_id' => 5, 207 'item_id' => 10, 208 'secondary_item_id' => 25, 209 'component_name' => 'messages', 210 'component_action' => 'new_message' 211 ); 212 213 self::factory()->notification->create( $args ); 214 215 $args['allow_duplicate'] = true; 216 217 $this->assertNotEmpty( bp_notifications_add_notification( $args ) ); 218 } 219 220 /** 221 * @group bp_notifications_get_unread_notification_count 222 * @group cache 223 */ 224 public function test_bp_notifications_get_unread_notification_count_cache() { 225 $u1 = self::factory()->user->create(); 226 $u2 = self::factory()->user->create(); 227 228 self::factory()->notification->create( array( 229 'component_name' => 'messages', 230 'component_action' => 'new_message', 231 'item_id' => 99, 232 'user_id' => $u2, 233 'secondary_item_id' => $u1, 234 'is_new' => true 235 ) ); 236 237 // prime cache 238 bp_notifications_get_unread_notification_count( $u2 ); 239 240 // mark the created notification as read 241 bp_notifications_mark_notifications_by_item_id( $u2, 99, 'messages', 'new_message', $u1 ); 242 243 // now grab the updated notification count 244 $n = bp_notifications_get_unread_notification_count( $u2 ); 245 246 // assert 247 $this->assertEquals( 0, $n ); 248 } 249 250 /** 251 * @group bp_has_notifications 252 */ 253 public function test_bp_has_notifications_filtering() { 254 $u1 = self::factory()->user->create(); 255 $u2 = self::factory()->user->create(); 256 257 // create a mixture of different notifications 258 self::factory()->notification->create( array( 259 'component_name' => 'messages', 260 'component_action' => 'new_message', 261 'item_id' => 99, 262 'user_id' => $u2, 263 'secondary_item_id' => $u1, 264 'is_new' => true 265 ) ); 266 267 self::factory()->notification->create( array( 268 'component_name' => 'activity', 269 'component_action' => 'new_at_mention', 270 'item_id' => 99, 271 'user_id' => $u2, 272 'secondary_item_id' => $u1, 273 'is_new' => true 274 ) ); 275 276 self::factory()->notification->create( array( 277 'component_name' => 'activity', 278 'component_action' => 'new_at_mention', 279 'item_id' => 100, 280 'user_id' => $u2, 281 'secondary_item_id' => $u1, 282 'is_new' => true 283 ) ); 284 285 // now fetch only activity notifications 286 bp_has_notifications( array( 287 'component_name' => 'activity', 288 'user_id' => $u2 289 ) ); 290 291 // assert 292 $this->assertEquals( 2, buddypress()->notifications->query_loop->total_notification_count ); 293 } 294 295 /** 296 * @group bp_notifications_delete_notifications_on_user_delete 297 * @ticket BP6681 298 */ 299 public function test_bp_notifications_delete_notifications_on_user_delete_should_delete_all_notifications() { 300 $u = self::factory()->user->create(); 301 302 // Create notifications 303 $n1 = self::factory()->notification->create( array( 304 'component_name' => 'messages', 305 'component_action' => 'new_message', 306 'item_id' => 99, 307 'user_id' => $u, 308 ) ); 309 310 $n2 = self::factory()->notification->create( array( 311 'component_name' => 'activity', 312 'component_action' => 'new_at_mention', 313 'item_id' => 99, 314 'user_id' => $u, 315 ) ); 316 317 $n3 = self::factory()->notification->create( array( 318 'component_name' => 'groups', 319 'user_id' => $u, 320 ) ); 321 322 $n4 = self::factory()->notification->create( array( 323 'component_name' => 'friends', 324 'component_action' => 'friendship_request', 325 'user_id' => $u, 326 ) ); 327 328 // Create notification for non-core component 329 $n5 = self::factory()->notification->create( array( 330 'component_name' => 'foo', 331 'component_action' => 'bar', 332 'item_id' => 99, 333 'user_id' => $u, 334 ) ); 335 336 global $wpdb, $bp; 337 338 /** 339 * Can't use BP_Notifications_Notification::get(), because class::parse_args, 340 * checks against bp_notifications_get_registered_components() 341 * and if component is disabled it will be ignored. 342 */ 343 $query = $wpdb->prepare( "SELECT id FROM {$bp->notifications->table_name} WHERE user_id = %d and is_new = 1", $u ); 344 345 // Make sure notifications have been added. 346 $found1 = $wpdb->get_col( $query ); 347 $this->assertEqualSets( array( $n1, $n2, $n3, $n4, $n5 ), $found1 ); 348 349 $this->delete_user( $u ); 350 351 // Check if notifications are deleted. 352 $found2 = $wpdb->get_col( $query ); 353 $this->assertEmpty( $found2 ); 354 } 355 356 /** 357 * @group notification_callback 358 * @ticket BP7141 359 */ 360 public function test_notification_callback_parameter_integrity() { 361 $u = self::factory()->user->create(); 362 363 $n = self::factory()->notification->create( array( 364 'component_name' => 'activity', 365 'component_action' => 'new_at_mention', 366 'item_id' => 99, 367 'user_id' => $u, 368 ) ); 369 370 // Override activity notification callback so we can test integrity. 371 buddypress()->activity->notification_callback = array( $this, 'dummy_notification_callback' ); 372 373 // Fetch notifications with string format. 374 bp_notifications_get_notifications_for_user( $u, 'string' ); 375 376 // Assert! 377 // @todo When we cast all numeric strings as integers, this needs to be changed. 378 $expected = array( 379 'action' => 'new_at_mention', 380 'item_id' => '99', 381 'secondary_item_id' => '0', 382 'total_items' => 1, 383 'id' => (string) $n, 384 'format' => 'string' 385 ); 386 $this->assertEquals( $expected, $this->n_args ); 387 388 // Fetch notifications with object format this time. 389 bp_notifications_get_notifications_for_user( $u, 'object' ); 390 391 // Assert! 392 $expected['format'] = 'array'; 393 $this->assertEquals( $expected, $this->n_args ); 394 395 // Reset! 396 buddypress()->activity->notification_callback = 'bp_activity_format_notifications'; 397 unset( $this->n_args ); 398 } 399 400 /** 401 * Used in test_notification_callback_parameter_integrity() test. 402 */ 403 public function dummy_notification_callback( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $id = 0 ) { 404 $this->n_args = compact( 'action', 'item_id', 'secondary_item_id', 'total_items', 'id', 'format' ); 405 } 406 407 /** 408 * @group cache 409 * @ticket BP7130 410 */ 411 public function test_get_grouped_notifications_for_user_cache_invalidation() { 412 $u = self::factory()->user->create(); 413 414 $n1 = self::factory()->notification->create( array( 415 'component_name' => 'activity', 416 'component_action' => 'new_at_mention', 417 'item_id' => 99, 418 'user_id' => $u, 419 ) ); 420 421 // Prime cache. 422 $found = bp_notifications_get_grouped_notifications_for_user( $u ); 423 $this->assertEquals( 1, $found[0]->total_count ); 424 425 $n2 = self::factory()->notification->create( array( 426 'component_name' => 'activity', 427 'component_action' => 'new_at_mention', 428 'item_id' => 100, 429 'user_id' => $u, 430 ) ); 431 432 $found = bp_notifications_get_grouped_notifications_for_user( $u ); 433 $this->assertEquals( 2, $found[0]->total_count ); 434 } 435 436 /** 437 * @ticket BP7827 438 */ 439 public function test_bp_notifications_personal_data_exporter() { 440 $u = self::factory()->user->create(); 441 442 // Create notifications 443 $n1 = self::factory()->notification->create( array( 444 'component_name' => 'messages', 445 'component_action' => 'new_message', 446 'item_id' => 99, 447 'user_id' => $u, 448 ) ); 449 450 $n2 = self::factory()->notification->create( array( 451 'component_name' => 'activity', 452 'component_action' => 'new_at_mention', 453 'item_id' => 99, 454 'user_id' => $u, 455 ) ); 456 457 $test_user = new WP_User( $u ); 458 459 $actual = bp_notifications_personal_data_exporter( $test_user->user_email, 1 ); 460 461 $this->assertTrue( $actual['done'] ); 462 463 // Number of exported notification items. 464 $this->assertSame( 2, count( $actual['data'] ) ); 465 } 466 467 /** 468 * @ticket BP8175 469 */ 470 public function test_notifications_data_should_be_deleted_on_user_delete_non_multisite() { 471 if ( is_multisite() ) { 472 $this->markTestSkipped( __METHOD__ . ' requires non-multisite.' ); 473 } 474 475 $u = self::factory()->user->create(); 476 477 $n1 = self::factory()->notification->create( array( 478 'component_name' => 'messages', 479 'component_action' => 'new_message', 480 'item_id' => 99, 481 'user_id' => $u, 482 ) ); 483 484 $found = bp_notifications_get_notifications_for_user( $u, 'object' ); 485 $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) ); 486 487 wp_delete_user( $u ); 488 489 $found = bp_notifications_get_notifications_for_user( $u, 'object' ); 490 $this->assertEmpty( '', $found ); 491 } 492 493 /** 494 * @ticket BP8175 495 */ 496 public function test_notifications_data_should_be_deleted_on_user_delete_multisite() { 497 if ( ! is_multisite() ) { 498 $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); 499 } 500 501 $u = self::factory()->user->create(); 502 503 $n1 = self::factory()->notification->create( array( 504 'component_name' => 'messages', 505 'component_action' => 'new_message', 506 'item_id' => 99, 507 'user_id' => $u, 508 ) ); 509 510 $found = bp_notifications_get_notifications_for_user( $u, 'object' ); 511 $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) ); 512 513 wpmu_delete_user( $u ); 514 515 $found = bp_notifications_get_notifications_for_user( $u, 'object' ); 516 $this->assertEmpty( '', $found ); 517 } 518 519 /** 520 * @ticket BP8175 521 */ 522 public function test_notifications_data_should_not_be_deleted_on_wp_delete_user_multisite() { 523 if ( ! is_multisite() ) { 524 $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); 525 } 526 527 $u = self::factory()->user->create(); 528 529 $n1 = self::factory()->notification->create( array( 530 'component_name' => 'messages', 531 'component_action' => 'new_message', 532 'item_id' => 99, 533 'user_id' => $u, 534 ) ); 535 536 $found = bp_notifications_get_notifications_for_user( $u, 'object' ); 537 $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) ); 538 539 wp_delete_user( $u ); 540 541 $found = bp_notifications_get_notifications_for_user( $u, 'object' ); 542 $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) ); 543 } 544 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Mar 4 01:01:38 2021 | Cross-referenced by PHPXref 0.7.1 |