| | 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 BP8637 |
| | 232 | */ |
| | 233 | public function test_bp_notifications_clear_all_for_user_cache_before_delete() { |
| | 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 | 'allow_duplicate' => true, |
| | 245 | ) |
| | 246 | ); |
| | 247 | |
| | 248 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); |
| | 249 | $this->assertEquals( $notification_ids, wp_list_pluck( $all_for_user_notifications, 'id' ) ); |
| | 250 | |
| | 251 | $u2 = self::factory()->user->create(); |
| | 252 | $a2 = self::factory()->activity->create(); |
| | 253 | |
| | 254 | // Check this one is not deleted. |
| | 255 | $notification_id = self::factory()->notification->create( |
| | 256 | array( |
| | 257 | 'component_name' => 'activity', |
| | 258 | 'component_action' => 'at_mentions', |
| | 259 | 'user_id' => $u2, |
| | 260 | 'item_id' => $a2, |
| | 261 | 'allow_duplicate' => true, |
| | 262 | ) |
| | 263 | ); |
| | 264 | |
| | 265 | // Delete. |
| | 266 | $amount = bp_notifications_delete_notifications_by_ids( $notification_ids ); |
| | 267 | $this->assertTrue( $amount === count( $notification_ids ) ); |
| | 268 | |
| | 269 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u ); |
| | 270 | $all_ids = wp_list_pluck( $all_for_user_notifications, 'id' ); |
| | 271 | |
| | 272 | $this->assertEmpty( array_intersect( $notification_ids, $all_ids ) ); |
| | 273 | |
| | 274 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u2 ); |
| | 275 | $this->assertSame( $all_for_user_notifications[0]->id, $notification_id ); |
| | 276 | } |
| | 277 | |
| | 278 | /** |
| | 279 | * @group cache |
| | 280 | * @ticket BP8637 |
| | 281 | */ |
| | 282 | public function test_bp_notifications_clear_all_for_user_cache_before_update_when_item_ids() { |
| | 283 | $s = self::factory()->user->create(); |
| | 284 | $r = self::factory()->user->create(); |
| | 285 | |
| | 286 | $message_ids = self::factory()->message->create_many( |
| | 287 | 4, |
| | 288 | array( |
| | 289 | 'sender_id' => $s, |
| | 290 | 'recipients' => array( $r ), |
| | 291 | 'content' => 'testing notification all for user cache', |
| | 292 | ) |
| | 293 | ); |
| | 294 | |
| | 295 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); |
| | 296 | $this->assertEquals( $message_ids, wp_list_pluck( $all_for_user_notifications, 'item_id' ) ); |
| | 297 | |
| | 298 | // Mark read. |
| | 299 | $amount = bp_notifications_mark_notifications_by_item_ids( $r, $message_ids, 'messages', 'new_message', false ); |
| | 300 | $this->assertTrue( $amount === count( $message_ids ) ); |
| | 301 | |
| | 302 | $message_id = self::factory()->message->create( |
| | 303 | array( |
| | 304 | 'sender_id' => $s, |
| | 305 | 'recipients' => array( $r ), |
| | 306 | 'content' => 'testing notification all for user cache', |
| | 307 | ) |
| | 308 | ); |
| | 309 | |
| | 310 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); |
| | 311 | $all_ids = wp_list_pluck( $all_for_user_notifications, 'item_id' ); |
| | 312 | |
| | 313 | $this->assertEmpty( array_intersect( $message_ids, $all_ids ) ); |
| | 314 | $this->assertContains( $message_id, $all_ids ); |
| | 315 | } |
| | 316 | |
| | 317 | /** |
| | 318 | * @group cache |
| | 319 | * @ticket BP8637 |
| | 320 | */ |
| | 321 | public function test_bp_notifications_clear_all_for_user_cache_before_delete_when_item_ids() { |
| | 322 | $s = self::factory()->user->create(); |
| | 323 | $r = self::factory()->user->create(); |
| | 324 | |
| | 325 | $message_ids = self::factory()->message->create_many( |
| | 326 | 4, |
| | 327 | array( |
| | 328 | 'sender_id' => $s, |
| | 329 | 'recipients' => array( $r ), |
| | 330 | 'content' => 'testing notification all for user cache', |
| | 331 | ) |
| | 332 | ); |
| | 333 | |
| | 334 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); |
| | 335 | $this->assertEquals( $message_ids, wp_list_pluck( $all_for_user_notifications, 'item_id' ) ); |
| | 336 | |
| | 337 | $message_id = self::factory()->message->create( |
| | 338 | array( |
| | 339 | 'sender_id' => $r, |
| | 340 | 'recipients' => array( $s ), |
| | 341 | 'content' => 'testing notification all for user cache', |
| | 342 | ) |
| | 343 | ); |
| | 344 | |
| | 345 | // Delete. |
| | 346 | $amount = bp_notifications_delete_notifications_by_item_ids( $r, $message_ids, 'messages', 'new_message' ); |
| | 347 | $this->assertTrue( $amount === count( $message_ids ) ); |
| | 348 | |
| | 349 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r ); |
| | 350 | $all_ids = wp_list_pluck( $all_for_user_notifications, 'item_id' ); |
| | 351 | |
| | 352 | $this->assertEmpty( array_intersect( $message_ids, $all_ids ) ); |
| | 353 | |
| | 354 | $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $s ); |
| | 355 | $this->assertSame( $all_for_user_notifications[0]->item_id, $message_id ); |
| | 356 | } |
| | 357 | } |