Skip to:
Content

BuddyPress.org

Changeset 13243


Ignore:
Timestamp:
02/22/2022 06:54:08 PM (5 years ago)
Author:
imath
Message:

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 #8637 (trunk)

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/bp-notifications-cache.php

    r13091 r13243  
    102102                $n = bp_notifications_get_notification( $where_args['id'] );
    103103                bp_notifications_clear_all_for_user_cache( $n->user_id );
     104
     105                // Get the list of user IDs from notification IDs.
     106        } elseif ( isset( $where_args['ids'] ) && $where_args['ids'] ) {
     107                $ids = (array) $where_args['ids'];
     108
     109                $ns = BP_Notifications_Notification::get(
     110                        array(
     111                                'id' => $ids,
     112                        )
     113                );
     114
     115                $user_ids = wp_list_pluck( $ns, 'user_id' );
     116                $user_ids = array_unique( $user_ids );
     117
     118                foreach ( $user_ids as $user_id ) {
     119                        bp_notifications_clear_all_for_user_cache( $user_id );
     120                }
    104121        }
    105122}
  • trunk/src/bp-notifications/classes/class-bp-notifications-notification.php

    r13184 r13243  
    934934                $conditions[] = "{$field} IN ({$_items})";
    935935
    936                 foreach ( $update_args['data'] as $field => $value ) {
    937                         $index  = array_search( $field, array_keys( $update_args['data'] ) );
     936                foreach ( $update_args['data'] as $update_field => $value ) {
     937                        $index  = array_search( $update_field, array_keys( $update_args['data'] ) );
    938938                        $format = $update_args['format'][ $index ];
    939939
    940                         $fields[] = "{$field} = {$format}";
     940                        $fields[] = "{$update_field} = {$format}";
    941941                        $values[] = $value;
    942942                }
    943943
    944                 foreach ( $where_args['data'] as $field => $value ) {
    945                         $index  = array_search( $field, array_keys( $where_args['data'] ) );
     944                foreach ( $where_args['data'] as $where_field => $value ) {
     945                        $index  = array_search( $where_field, array_keys( $where_args['data'] ) );
    946946                        $format = $where_args['format'][ $index ];
    947947
    948                         $conditions[] = "{$field} = {$format}";
     948                        $conditions[] = "{$where_field} = {$format}";
    949949                        $values[]     = $value;
    950950                }
     
    952952                $fields     = implode( ', ', $fields );
    953953                $conditions = implode( ' AND ', $conditions );
     954
     955                if ( 'item_id' === $field && isset( $where_args['data']['user_id'] ) ) {
     956                        $where_args['item_ids'] = $items;
     957                        $where_args['user_id']  = $where_args['data']['user_id'];
     958                } elseif ( 'id' === $field ) {
     959                        $where_args['ids'] = $items;
     960                }
    954961
    955962                /** This action is documented in bp-notifications/classes/class-bp-notifications-notification.php */
     
    10221029                $conditions[] = "{$field} IN ({$_items})";
    10231030
    1024                 foreach ( $where['data'] as $field => $value ) {
    1025                         $index  = array_search( $field, array_keys( $where['data'] ) );
     1031                foreach ( $where['data'] as $where_field => $value ) {
     1032                        $index  = array_search( $where_field, array_keys( $where['data'] ) );
    10261033                        $format = $where['format'][ $index ];
    10271034
    1028                         $conditions[] = "{$field} = {$format}";
     1035                        $conditions[] = "{$where_field} = {$format}";
    10291036                        $values[]     = $value;
    10301037                }
    10311038
    10321039                $conditions = implode( ' AND ', $conditions );
     1040
     1041                if ( 'id' === $field ) {
     1042                        $args['id'] = $items;
     1043                }
    10331044
    10341045                /** This action is documented in bp-notifications/classes/class-bp-notifications-notification.php */
  • trunk/tests/phpunit/testcases/notifications/functions.php

    r13112 r13243  
    55 */
    66class 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         }
    1837
    1848        /**
Note: See TracChangeset for help on using the changeset viewer.