<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][13243] trunk: Clear the notifications user cache when bulk updating/deleting items</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/13243">13243</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/13243","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-02-22 18:54:08 +0000 (Tue, 22 Feb 2022)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Clear the notifications user cache when bulk updating/deleting items
This commit also groups PHP Unit tests about notifications cache functions into a specific file.
Props niftythree, oztaser.
See <a href="http://buddypress.trac.wordpress.org/ticket/8637">#8637</a> (trunk)</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpnotificationsbpnotificationscachephp">trunk/src/bp-notifications/bp-notifications-cache.php</a></li>
<li><a href="#trunksrcbpnotificationsclassesclassbpnotificationsnotificationphp">trunk/src/bp-notifications/classes/class-bp-notifications-notification.php</a></li>
<li><a href="#trunktestsphpunittestcasesnotificationsfunctionsphp">trunk/tests/phpunit/testcases/notifications/functions.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestcasesnotificationscachephp">trunk/tests/phpunit/testcases/notifications/cache.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpnotificationsbpnotificationscachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-notifications/bp-notifications-cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-notifications/bp-notifications-cache.php 2022-02-21 20:52:31 UTC (rev 13242)
+++ trunk/src/bp-notifications/bp-notifications-cache.php 2022-02-22 18:54:08 UTC (rev 13243)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -101,6 +101,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( ! empty( $where_args['id'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $n = bp_notifications_get_notification( $where_args['id'] );
</span><span class="cx" style="display: block; padding: 0 10px"> bp_notifications_clear_all_for_user_cache( $n->user_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Get the list of user IDs from notification IDs.
+ } elseif ( isset( $where_args['ids'] ) && $where_args['ids'] ) {
+ $ids = (array) $where_args['ids'];
+
+ $ns = BP_Notifications_Notification::get(
+ array(
+ 'id' => $ids,
+ )
+ );
+
+ $user_ids = wp_list_pluck( $ns, 'user_id' );
+ $user_ids = array_unique( $user_ids );
+
+ foreach ( $user_ids as $user_id ) {
+ bp_notifications_clear_all_for_user_cache( $user_id );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'bp_notification_before_update', 'bp_notifications_clear_all_for_user_cache_before_update', 10, 2 );
</span></span></pre></div>
<a id="trunksrcbpnotificationsclassesclassbpnotificationsnotificationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-notifications/classes/class-bp-notifications-notification.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-notifications/classes/class-bp-notifications-notification.php 2022-02-21 20:52:31 UTC (rev 13242)
+++ trunk/src/bp-notifications/classes/class-bp-notifications-notification.php 2022-02-22 18:54:08 UTC (rev 13243)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -933,19 +933,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $_items = implode( ',', wp_parse_id_list( $items ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $conditions[] = "{$field} IN ({$_items})";
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $update_args['data'] as $field => $value ) {
- $index = array_search( $field, array_keys( $update_args['data'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $update_args['data'] as $update_field => $value ) {
+ $index = array_search( $update_field, array_keys( $update_args['data'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $format = $update_args['format'][ $index ];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $fields[] = "{$field} = {$format}";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $fields[] = "{$update_field} = {$format}";
</ins><span class="cx" style="display: block; padding: 0 10px"> $values[] = $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $where_args['data'] as $field => $value ) {
- $index = array_search( $field, array_keys( $where_args['data'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $where_args['data'] as $where_field => $value ) {
+ $index = array_search( $where_field, array_keys( $where_args['data'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $format = $where_args['format'][ $index ];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $conditions[] = "{$field} = {$format}";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $conditions[] = "{$where_field} = {$format}";
</ins><span class="cx" style="display: block; padding: 0 10px"> $values[] = $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -952,6 +952,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $fields = implode( ', ', $fields );
</span><span class="cx" style="display: block; padding: 0 10px"> $conditions = implode( ' AND ', $conditions );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'item_id' === $field && isset( $where_args['data']['user_id'] ) ) {
+ $where_args['item_ids'] = $items;
+ $where_args['user_id'] = $where_args['data']['user_id'];
+ } elseif ( 'id' === $field ) {
+ $where_args['ids'] = $items;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /** This action is documented in bp-notifications/classes/class-bp-notifications-notification.php */
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'bp_notification_before_update', $update_args, $where_args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1021,16 +1028,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $_items = implode( ',', wp_parse_id_list( $items ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $conditions[] = "{$field} IN ({$_items})";
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $where['data'] as $field => $value ) {
- $index = array_search( $field, array_keys( $where['data'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $where['data'] as $where_field => $value ) {
+ $index = array_search( $where_field, array_keys( $where['data'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $format = $where['format'][ $index ];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $conditions[] = "{$field} = {$format}";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $conditions[] = "{$where_field} = {$format}";
</ins><span class="cx" style="display: block; padding: 0 10px"> $values[] = $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $conditions = implode( ' AND ', $conditions );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'id' === $field ) {
+ $args['id'] = $items;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /** This action is documented in bp-notifications/classes/class-bp-notifications-notification.php */
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'bp_notification_before_delete', $args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestcasesnotificationscachephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/testcases/notifications/cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/notifications/cache.php (rev 0)
+++ trunk/tests/phpunit/testcases/notifications/cache.php 2022-02-22 18:54:08 UTC (rev 13243)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,357 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group notifications
+ * @group cache
+ */
+class BP_Tests_Notifications_Cache extends BP_UnitTestCase {
+
+ /**
+ * @group cache
+ */
+ public function test_cache_invalidation_all_for_user_on_save() {
+ $u = self::factory()->user->create();
+
+ self::factory()->notification->create( array(
+ 'component_name' => 'groups',
+ 'user_id' => $u
+ ) );
+ self::factory()->notification->create( array(
+ 'component_name' => 'messages',
+ 'user_id' => $u,
+ 'item_id' => 1
+ ) );
+
+ // prime cache
+ $count = bp_notifications_get_unread_notification_count( $u );
+
+ // just to be sure...
+ $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
+
+ // Trigger invalidation via save
+ self::factory()->notification->create( array(
+ 'component_name' => 'messages',
+ 'user_id' => $u,
+ 'item_id' => 2
+ ) );
+
+ $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
+ $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
+ }
+
+ /**
+ * @group cache
+ */
+ public function test_cache_invalidation_all_for_user_on_delete() {
+ $u = self::factory()->user->create();
+ $n1 = self::factory()->notification->create( array(
+ 'component_name' => 'groups',
+ 'user_id' => $u
+ ) );
+ self::factory()->notification->create( array(
+ 'component_name' => 'messages',
+ 'user_id' => $u
+ ) );
+
+ // prime cache
+ $count = bp_notifications_get_unread_notification_count( $u );
+
+ // just to be sure...
+ $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
+
+ // delete
+ BP_Notifications_Notification::delete( array( 'id' => $n1, ) );
+
+ $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
+ $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
+ }
+
+ /**
+ * @group cache
+ */
+ public function test_cache_invalidation_all_for_user_on_update_user_id() {
+ $u = self::factory()->user->create();
+
+ self::factory()->notification->create( array(
+ 'component_name' => 'groups',
+ 'user_id' => $u
+ ) );
+ self::factory()->notification->create( array(
+ 'component_name' => 'messages',
+ 'user_id' => $u
+ ) );
+
+ // prime cache
+ $count = bp_notifications_get_unread_notification_count( $u );
+
+ // just to be sure...
+ $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
+
+ // mark all notifications by user as read
+ BP_Notifications_Notification::update(
+ array( 'is_new' => false ),
+ array( 'user_id' => $u )
+ );
+
+ $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
+ $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
+ }
+
+ /**
+ * @group cache
+ */
+ public function test_cache_invalidation_all_for_user_on_update_id() {
+ $u = self::factory()->user->create();
+ $n1 = self::factory()->notification->create( array(
+ 'component_name' => 'groups',
+ 'user_id' => $u
+ ) );
+
+ self::factory()->notification->create( array(
+ 'component_name' => 'messages',
+ 'user_id' => $u
+ ) );
+
+ // prime cache
+ $count = bp_notifications_get_unread_notification_count( $u );
+
+ // just to be sure...
+ $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
+
+ // mark one notification as read
+ BP_Notifications_Notification::update(
+ array( 'is_new' => false ),
+ array( 'id' => $n1 )
+ );
+
+ $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
+ $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
+ }
+
+ /**
+ * @group bp_notifications_update_meta_cache
+ */
+ public function test_bp_notifications_update_meta_cache() {
+ $u = self::factory()->user->create();
+
+ $n1 = self::factory()->notification->create( array(
+ 'component_name' => 'messages',
+ 'user_id' => $u
+ ) );
+
+ $n2 = self::factory()->notification->create( array(
+ 'component_name' => 'groups',
+ 'user_id' => $u
+ ) );
+
+ // Add cache for each notification.
+ bp_notifications_update_meta( $n1, 'meta', 'data' );
+ bp_notifications_update_meta( $n1, 'data', 'meta' );
+ bp_notifications_update_meta( $n2, 'meta', 'human' );
+
+ // Prime cache.
+ bp_notifications_get_meta( $n1, 'meta' );
+
+ // Ensure an empty cache for second notification.
+ wp_cache_delete( $n2, 'notification_meta' );
+
+ // Update notification meta cache.
+ bp_notifications_update_meta_cache( array( $n1, $n2 ) );
+
+ $expected = array(
+ $n1 => array(
+ 'meta' => array(
+ 'data',
+ ),
+ 'data' => array(
+ 'meta',
+ ),
+ ),
+ $n2 => array(
+ 'meta' => array(
+ 'human',
+ ),
+ ),
+ );
+
+ $found = array(
+ $n1 => wp_cache_get( $n1, 'notification_meta' ),
+ $n2 => wp_cache_get( $n2, 'notification_meta' ),
+ );
+
+ $this->assertEquals( $expected, $found );
+ }
+
+ /**
+ * @group cache
+ * @ticket BP8637
+ */
+ public function test_bp_notifications_clear_all_for_user_cache_before_update() {
+ $u = self::factory()->user->create();
+ $a = self::factory()->activity->create();
+
+ $notification_ids = self::factory()->notification->create_many(
+ 4,
+ array(
+ 'component_name' => 'activity',
+ 'component_action' => 'at_mentions',
+ 'user_id' => $u,
+ 'item_id' => $a,
+ 'allow_duplicate' => true,
+ )
+ );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u );
+ $this->assertEquals( $notification_ids, wp_list_pluck( $all_for_user_notifications, 'id' ) );
+
+ // Mark as read.
+ $amount = bp_notifications_mark_notifications_by_ids( $notification_ids );
+ $this->assertTrue( $amount === count( $notification_ids ) );
+
+ // Add a new one.
+ $notification_id = self::factory()->notification->create(
+ array(
+ 'component_name' => 'activity',
+ 'component_action' => 'at_mentions',
+ 'user_id' => $u,
+ 'item_id' => $a,
+ 'allow_duplicate' => true,
+ )
+ );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u );
+ $all_ids = wp_list_pluck( $all_for_user_notifications, 'id' );
+
+ $this->assertEmpty( array_intersect( $notification_ids, $all_ids ) );
+ $this->assertContains( $notification_id, $all_ids );
+ }
+
+ /**
+ * @group cache
+ * @ticket BP8637
+ */
+ public function test_bp_notifications_clear_all_for_user_cache_before_delete() {
+ $u = self::factory()->user->create();
+ $a = self::factory()->activity->create();
+
+ $notification_ids = self::factory()->notification->create_many(
+ 4,
+ array(
+ 'component_name' => 'activity',
+ 'component_action' => 'at_mentions',
+ 'user_id' => $u,
+ 'item_id' => $a,
+ 'allow_duplicate' => true,
+ )
+ );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u );
+ $this->assertEquals( $notification_ids, wp_list_pluck( $all_for_user_notifications, 'id' ) );
+
+ $u2 = self::factory()->user->create();
+ $a2 = self::factory()->activity->create();
+
+ // Check this one is not deleted.
+ $notification_id = self::factory()->notification->create(
+ array(
+ 'component_name' => 'activity',
+ 'component_action' => 'at_mentions',
+ 'user_id' => $u2,
+ 'item_id' => $a2,
+ 'allow_duplicate' => true,
+ )
+ );
+
+ // Delete.
+ $amount = bp_notifications_delete_notifications_by_ids( $notification_ids );
+ $this->assertTrue( $amount === count( $notification_ids ) );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u );
+ $all_ids = wp_list_pluck( $all_for_user_notifications, 'id' );
+
+ $this->assertEmpty( array_intersect( $notification_ids, $all_ids ) );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $u2 );
+ $this->assertSame( $all_for_user_notifications[0]->id, $notification_id );
+ }
+
+ /**
+ * @group cache
+ * @ticket BP8637
+ */
+ public function test_bp_notifications_clear_all_for_user_cache_before_update_when_item_ids() {
+ $s = self::factory()->user->create();
+ $r = self::factory()->user->create();
+
+ $message_ids = self::factory()->message->create_many(
+ 4,
+ array(
+ 'sender_id' => $s,
+ 'recipients' => array( $r ),
+ 'content' => 'testing notification all for user cache',
+ )
+ );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r );
+ $this->assertEquals( $message_ids, wp_list_pluck( $all_for_user_notifications, 'item_id' ) );
+
+ // Mark read.
+ $amount = bp_notifications_mark_notifications_by_item_ids( $r, $message_ids, 'messages', 'new_message', false );
+ $this->assertTrue( $amount === count( $message_ids ) );
+
+ $message_id = self::factory()->message->create(
+ array(
+ 'sender_id' => $s,
+ 'recipients' => array( $r ),
+ 'content' => 'testing notification all for user cache',
+ )
+ );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r );
+ $all_ids = wp_list_pluck( $all_for_user_notifications, 'item_id' );
+
+ $this->assertEmpty( array_intersect( $message_ids, $all_ids ) );
+ $this->assertContains( $message_id, $all_ids );
+ }
+
+ /**
+ * @group cache
+ * @ticket BP8637
+ */
+ public function test_bp_notifications_clear_all_for_user_cache_before_delete_when_item_ids() {
+ $s = self::factory()->user->create();
+ $r = self::factory()->user->create();
+
+ $message_ids = self::factory()->message->create_many(
+ 4,
+ array(
+ 'sender_id' => $s,
+ 'recipients' => array( $r ),
+ 'content' => 'testing notification all for user cache',
+ )
+ );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r );
+ $this->assertEquals( $message_ids, wp_list_pluck( $all_for_user_notifications, 'item_id' ) );
+
+ $message_id = self::factory()->message->create(
+ array(
+ 'sender_id' => $r,
+ 'recipients' => array( $s ),
+ 'content' => 'testing notification all for user cache',
+ )
+ );
+
+ // Delete.
+ $amount = bp_notifications_delete_notifications_by_item_ids( $r, $message_ids, 'messages', 'new_message' );
+ $this->assertTrue( $amount === count( $message_ids ) );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $r );
+ $all_ids = wp_list_pluck( $all_for_user_notifications, 'item_id' );
+
+ $this->assertEmpty( array_intersect( $message_ids, $all_ids ) );
+
+ $all_for_user_notifications = bp_notifications_get_all_notifications_for_user( $s );
+ $this->assertSame( $all_for_user_notifications[0]->item_id, $message_id );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/testcases/notifications/cache.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestcasesnotificationsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/notifications/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/notifications/functions.php 2022-02-21 20:52:31 UTC (rev 13242)
+++ trunk/tests/phpunit/testcases/notifications/functions.php 2022-02-22 18:54:08 UTC (rev 13243)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,182 +6,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class BP_Tests_Notifications_Functions extends BP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @group cache
- */
- public function test_cache_invalidation_all_for_user_on_save() {
- $u = self::factory()->user->create();
-
- self::factory()->notification->create( array(
- 'component_name' => 'groups',
- 'user_id' => $u
- ) );
- self::factory()->notification->create( array(
- 'component_name' => 'messages',
- 'user_id' => $u,
- 'item_id' => 1
- ) );
-
- // prime cache
- $count = bp_notifications_get_unread_notification_count( $u );
-
- // just to be sure...
- $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
-
- // Trigger invalidation via save
- self::factory()->notification->create( array(
- 'component_name' => 'messages',
- 'user_id' => $u,
- 'item_id' => 2
- ) );
-
- $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
- $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
- }
-
- /**
- * @group cache
- */
- public function test_cache_invalidation_all_for_user_on_delete() {
- $u = self::factory()->user->create();
- $n1 = self::factory()->notification->create( array(
- 'component_name' => 'groups',
- 'user_id' => $u
- ) );
- self::factory()->notification->create( array(
- 'component_name' => 'messages',
- 'user_id' => $u
- ) );
-
- // prime cache
- $count = bp_notifications_get_unread_notification_count( $u );
-
- // just to be sure...
- $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
-
- // delete
- BP_Notifications_Notification::delete( array( 'id' => $n1, ) );
-
- $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
- $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
- }
-
- /**
- * @group cache
- */
- public function test_cache_invalidation_all_for_user_on_update_user_id() {
- $u = self::factory()->user->create();
-
- self::factory()->notification->create( array(
- 'component_name' => 'groups',
- 'user_id' => $u
- ) );
- self::factory()->notification->create( array(
- 'component_name' => 'messages',
- 'user_id' => $u
- ) );
-
- // prime cache
- $count = bp_notifications_get_unread_notification_count( $u );
-
- // just to be sure...
- $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
-
- // mark all notifications by user as read
- BP_Notifications_Notification::update(
- array( 'is_new' => false ),
- array( 'user_id' => $u )
- );
-
- $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
- $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
- }
-
- /**
- * @group cache
- */
- public function test_cache_invalidation_all_for_user_on_update_id() {
- $u = self::factory()->user->create();
- $n1 = self::factory()->notification->create( array(
- 'component_name' => 'groups',
- 'user_id' => $u
- ) );
-
- self::factory()->notification->create( array(
- 'component_name' => 'messages',
- 'user_id' => $u
- ) );
-
- // prime cache
- $count = bp_notifications_get_unread_notification_count( $u );
-
- // just to be sure...
- $this->assertEquals( 2, $count, 'Cache count should be 2 before invalidation.' );
-
- // mark one notification as read
- BP_Notifications_Notification::update(
- array( 'is_new' => false ),
- array( 'id' => $n1 )
- );
-
- $this->assertFalse( wp_cache_get( 'all_for_user_' . $u, 'bp_notifications' ) );
- $this->assertFalse( wp_cache_get( $u, 'bp_notifications_unread_count' ) );
- }
-
- /**
- * @group bp_notifications_update_meta_cache
- */
- public function test_bp_notifications_update_meta_cache() {
- $u = self::factory()->user->create();
-
- $n1 = self::factory()->notification->create( array(
- 'component_name' => 'messages',
- 'user_id' => $u
- ) );
-
- $n2 = self::factory()->notification->create( array(
- 'component_name' => 'groups',
- 'user_id' => $u
- ) );
-
- // Add cache for each notification.
- bp_notifications_update_meta( $n1, 'meta', 'data' );
- bp_notifications_update_meta( $n1, 'data', 'meta' );
- bp_notifications_update_meta( $n2, 'meta', 'human' );
-
- // Prime cache.
- bp_notifications_get_meta( $n1, 'meta' );
-
- // Ensure an empty cache for second notification.
- wp_cache_delete( $n2, 'notification_meta' );
-
- // Update notification meta cache.
- bp_notifications_update_meta_cache( array( $n1, $n2 ) );
-
- $expected = array(
- $n1 => array(
- 'meta' => array(
- 'data',
- ),
- 'data' => array(
- 'meta',
- ),
- ),
- $n2 => array(
- 'meta' => array(
- 'human',
- ),
- ),
- );
-
- $found = array(
- $n1 => wp_cache_get( $n1, 'notification_meta' ),
- $n2 => wp_cache_get( $n2, 'notification_meta' ),
- );
-
- $this->assertEquals( $expected, $found );
- }
-
- /**
</del><span class="cx" style="display: block; padding: 0 10px"> * @group bp_notifications_add_notification
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_bp_notifications_add_notification_no_dupes() {
</span></span></pre>
</div>
</div>
</body>
</html>