[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @group notifications 5 * @group cache 6 */ 7 class BP_Tests_Notifications_Cache extends BP_UnitTestCase { 8 9 /** 10 * @group cache 11 */ 12 public function test_cache_invalidation_all_for_user_on_save() { 13 $u = self::factory()->user->create(); 14 15 self::factory()->notification->create( array( 16 'component_name' => 'groups', 17 'user_id' => $u 18 ) ); 19 self::factory()->notification->create( array( 20 'component_name' => 'messages', 21 'user_id' => $u, 22 'item_id' => 1 23 ) ); 24 25 // prime cache 26 $count = bp_notifications_get_unread_notification_count( $u ); 27 28 // just to be sure... 29 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 30 31 // Trigger invalidation via save 32 self::factory()->notification->create( array( 33 'component_name' => 'messages', 34 'user_id' => $u, 35 'item_id' => 2 36 ) ); 37 38 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 39 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 40 } 41 42 /** 43 * @group cache 44 */ 45 public function test_cache_invalidation_all_for_user_on_delete() { 46 $u = self::factory()->user->create(); 47 $n1 = self::factory()->notification->create( array( 48 'component_name' => 'groups', 49 'user_id' => $u 50 ) ); 51 self::factory()->notification->create( array( 52 'component_name' => 'messages', 53 'user_id' => $u 54 ) ); 55 56 // prime cache 57 $count = bp_notifications_get_unread_notification_count( $u ); 58 59 // just to be sure... 60 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 61 62 // delete 63 BP_Notifications_Notification::delete( array( 'id' => $n1, ) ); 64 65 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 66 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 67 } 68 69 /** 70 * @group cache 71 */ 72 public function test_cache_invalidation_all_for_user_on_update_user_id() { 73 $u = self::factory()->user->create(); 74 75 self::factory()->notification->create( array( 76 'component_name' => 'groups', 77 'user_id' => $u 78 ) ); 79 self::factory()->notification->create( array( 80 'component_name' => 'messages', 81 'user_id' => $u 82 ) ); 83 84 // prime cache 85 $count = bp_notifications_get_unread_notification_count( $u ); 86 87 // just to be sure... 88 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 89 90 // mark all notifications by user as read 91 BP_Notifications_Notification::update( 92 array( 'is_new' => false ), 93 array( 'user_id' => $u ) 94 ); 95 96 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 97 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 98 } 99 100 /** 101 * @group cache 102 */ 103 public function test_cache_invalidation_all_for_user_on_update_id() { 104 $u = self::factory()->user->create(); 105 $n1 = self::factory()->notification->create( array( 106 'component_name' => 'groups', 107 'user_id' => $u 108 ) ); 109 110 self::factory()->notification->create( array( 111 'component_name' => 'messages', 112 'user_id' => $u 113 ) ); 114 115 // prime cache 116 $count = bp_notifications_get_unread_notification_count( $u ); 117 118 // just to be sure... 119 $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' ); 120 121 // mark one notification as read 122 BP_Notifications_Notification::update( 123 array( 'is_new' => false ), 124 array( 'id' => $n1 ) 125 ); 126 127 $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) ); 128 $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) ); 129 } 130 131 /** 132 * @group bp_notifications_update_meta_cache 133 */ 134 public function test_bp_notifications_update_meta_cache() { 135 $u = self::factory()->user->create(); 136 137 $n1 = self::factory()->notification->create( array( 138 'component_name' => 'messages', 139 'user_id' => $u 140 ) ); 141 142 $n2 = self::factory()->notification->create( array( 143 'component_name' => 'groups', 144 'user_id' => $u 145 ) ); 146 147 // Add cache for each notification. 148 bp_notifications_update_meta( $n1, 'meta', 'data' ); 149 bp_notifications_update_meta( $n1, 'data', 'meta' ); 150 bp_notifications_update_meta( $n2, 'meta', 'human' ); 151 152 // Prime cache. 153 bp_notifications_get_meta( $n1, 'meta' ); 154 155 // Ensure an empty cache for second notification. 156 wp_cache_delete( $n2, 'notification_meta' ); 157 158 // Update notification meta cache. 159 bp_notifications_update_meta_cache( array( $n1, $n2 ) ); 160 161 $expected = array( 162 $n1 => array( 163 'meta' => array( 164 'data', 165 ), 166 'data' => array( 167 'meta', 168 ), 169 ), 170 $n2 => array( 171 'meta' => array( 172 'human', 173 ), 174 ), 175 ); 176 177 $found = array( 178 $n1 => wp_cache_get( $n1, 'notification_meta' ), 179 $n2 => wp_cache_get( $n2, 'notification_meta' ), 180 ); 181 182 $this->assertEquals( $expected, $found ); 183 } 184 185 /** 186 * @group cache 187 * @ticket BP8637 188 */ 189 public function test_bp_notifications_clear_all_for_user_cache_before_update() { 190 $u = self::factory()->user->create(); 191 $a = self::factory()->activity->create(); 192 193 $notification_ids = self::factory()->notification->create_many( 194 4, 195 array( 196 'component_name' => 'activity', 197 'component_action' => 'at_mentions', 198 'user_id' => $u, 199 'item_id' => $a, 200 'allow_duplicate' => true, 201 ) 202 ); 203 204 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); 205 $this->assertEquals( $notification_ids, wp_list_pluck( $all_for_user_notifications, 'id' ) ); 206 207 // Mark as read. 208 $amount = bp_notifications_mark_notifications_by_ids( $notification_ids ); 209 $this->assertTrue( $amount === count( $notification_ids ) ); 210 211 // Add a new one. 212 $notification_id = self::factory()->notification->create( 213 array( 214 'component_name' => 'activity', 215 'component_action' => 'at_mentions', 216 'user_id' => $u, 217 'item_id' => $a, 218 'allow_duplicate' => true, 219 ) 220 ); 221 222 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); 223 $all_ids = wp_list_pluck( $all_for_user_notifications, 'id' ); 224 225 $this->assertEmpty( array_intersect( $notification_ids, $all_ids ) ); 226 $this->assertContains( $notification_id, $all_ids ); 227 } 228 229 /** 230 * @group cache 231 * @ticket BP8642 232 */ 233 public function test_bp_notifications_clear_all_for_user_cache_before_update_when_marked_unread() { 234 $u = self::factory()->user->create(); 235 $a = self::factory()->activity->create(); 236 237 $notification_ids = self::factory()->notification->create_many( 238 4, 239 array( 240 'component_name' => 'activity', 241 'component_action' => 'at_mentions', 242 'user_id' => $u, 243 'item_id' => $a, 244 'is_new' => 0, 245 'allow_duplicate' => true, 246 ) 247 ); 248 249 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); 250 $this->assertEmpty( $all_for_user_notifications ); 251 252 // Mark as unread. 253 $amount = bp_notifications_mark_notifications_by_ids( $notification_ids, 1 ); 254 $this->assertTrue( $amount === count( $notification_ids ) ); 255 256 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); 257 $this->assertEquals( $notification_ids, wp_list_pluck( $all_for_user_notifications, 'id' ) ); 258 } 259 260 /** 261 * @group cache 262 * @ticket BP8637 263 */ 264 public function test_bp_notifications_clear_all_for_user_cache_before_delete() { 265 $u = self::factory()->user->create(); 266 $a = self::factory()->activity->create(); 267 268 $notification_ids = self::factory()->notification->create_many( 269 4, 270 array( 271 'component_name' => 'activity', 272 'component_action' => 'at_mentions', 273 'user_id' => $u, 274 'item_id' => $a, 275 'allow_duplicate' => true, 276 ) 277 ); 278 279 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); 280 $this->assertEquals( $notification_ids, wp_list_pluck( $all_for_user_notifications, 'id' ) ); 281 282 $u2 = self::factory()->user->create(); 283 $a2 = self::factory()->activity->create(); 284 285 // Check this one is not deleted. 286 $notification_id = self::factory()->notification->create( 287 array( 288 'component_name' => 'activity', 289 'component_action' => 'at_mentions', 290 'user_id' => $u2, 291 'item_id' => $a2, 292 'allow_duplicate' => true, 293 ) 294 ); 295 296 // Delete. 297 $amount = bp_notifications_delete_notifications_by_ids( $notification_ids ); 298 $this->assertTrue( $amount === count( $notification_ids ) ); 299 300 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); 301 $all_ids = wp_list_pluck( $all_for_user_notifications, 'id' ); 302 303 $this->assertEmpty( array_intersect( $notification_ids, $all_ids ) ); 304 305 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u2 ); 306 $this->assertSame( $all_for_user_notifications[0]->id, $notification_id ); 307 } 308 309 /** 310 * @group cache 311 * @ticket BP8637 312 */ 313 public function test_bp_notifications_clear_all_for_user_cache_before_update_when_item_ids() { 314 $s = self::factory()->user->create(); 315 $r = self::factory()->user->create(); 316 317 $message_ids = self::factory()->message->create_many( 318 4, 319 array( 320 'sender_id' => $s, 321 'recipients' => array( $r ), 322 'content' => 'testing notification all for user cache', 323 ) 324 ); 325 326 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); 327 $this->assertEquals( $message_ids, wp_list_pluck( $all_for_user_notifications, 'item_id' ) ); 328 329 // Mark read. 330 $amount = bp_notifications_mark_notifications_by_item_ids( $r, $message_ids, 'messages', 'new_message', false ); 331 $this->assertTrue( $amount === count( $message_ids ) ); 332 333 $message_id = self::factory()->message->create( 334 array( 335 'sender_id' => $s, 336 'recipients' => array( $r ), 337 'content' => 'testing notification all for user cache', 338 ) 339 ); 340 341 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); 342 $all_ids = wp_list_pluck( $all_for_user_notifications, 'item_id' ); 343 344 $this->assertEmpty( array_intersect( $message_ids, $all_ids ) ); 345 $this->assertContains( $message_id, $all_ids ); 346 } 347 348 /** 349 * @group cache 350 * @ticket BP8642 351 */ 352 public function test_bp_notifications_clear_all_for_user_cache_before_update_when_item_ids_and_marked_unread() { 353 $s = self::factory()->user->create(); 354 $r = self::factory()->user->create(); 355 $notification_ids = array(); 356 357 remove_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 ); 358 359 $message_ids = self::factory()->message->create_many( 360 4, 361 array( 362 'sender_id' => $s, 363 'recipients' => array( $r ), 364 'content' => 'testing notification all for user cache', 365 ) 366 ); 367 368 foreach ( $message_ids as $message_id ) { 369 $notification_ids[] = self::factory()->notification->create( 370 array( 371 'component_name' => 'messages', 372 'component_action' => 'new_message', 373 'user_id' => $r, 374 'item_id' => $message_id, 375 'is_new' => 0, 376 ) 377 ); 378 } 379 380 add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 ); 381 382 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); 383 $this->assertEmpty( $all_for_user_notifications ); 384 385 // Mark unread. 386 $amount = bp_notifications_mark_notifications_by_item_ids( $r, $message_ids, 'messages', 'new_message', 1 ); 387 $this->assertTrue( $amount === count( $message_ids ) ); 388 389 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); 390 $this->assertEquals( $message_ids, wp_list_pluck( $all_for_user_notifications, 'item_id' ) ); 391 } 392 393 /** 394 * @group cache 395 * @ticket BP8637 396 */ 397 public function test_bp_notifications_clear_all_for_user_cache_before_delete_when_item_ids() { 398 $s = self::factory()->user->create(); 399 $r = self::factory()->user->create(); 400 401 $message_ids = self::factory()->message->create_many( 402 4, 403 array( 404 'sender_id' => $s, 405 'recipients' => array( $r ), 406 'content' => 'testing notification all for user cache', 407 ) 408 ); 409 410 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); 411 $this->assertEquals( $message_ids, wp_list_pluck( $all_for_user_notifications, 'item_id' ) ); 412 413 $message_id = self::factory()->message->create( 414 array( 415 'sender_id' => $r, 416 'recipients' => array( $s ), 417 'content' => 'testing notification all for user cache', 418 ) 419 ); 420 421 // Delete. 422 $amount = bp_notifications_delete_notifications_by_item_ids( $r, $message_ids, 'messages', 'new_message' ); 423 $this->assertTrue( $amount === count( $message_ids ) ); 424 425 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); 426 $all_ids = wp_list_pluck( $all_for_user_notifications, 'item_id' ); 427 428 $this->assertEmpty( array_intersect( $message_ids, $all_ids ) ); 429 430 $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $s ); 431 $this->assertSame( $all_for_user_notifications[0]->item_id, $message_id ); 432 } 433 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |