Changeset 13243
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-notifications/bp-notifications-cache.php
r13091 r13243 102 102 $n = bp_notifications_get_notification( $where_args['id'] ); 103 103 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 } 104 121 } 105 122 } -
trunk/src/bp-notifications/classes/class-bp-notifications-notification.php
r13184 r13243 934 934 $conditions[] = "{$field} IN ({$_items})"; 935 935 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'] ) ); 938 938 $format = $update_args['format'][ $index ]; 939 939 940 $fields[] = "{$ field} = {$format}";940 $fields[] = "{$update_field} = {$format}"; 941 941 $values[] = $value; 942 942 } 943 943 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'] ) ); 946 946 $format = $where_args['format'][ $index ]; 947 947 948 $conditions[] = "{$ field} = {$format}";948 $conditions[] = "{$where_field} = {$format}"; 949 949 $values[] = $value; 950 950 } … … 952 952 $fields = implode( ', ', $fields ); 953 953 $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 } 954 961 955 962 /** This action is documented in bp-notifications/classes/class-bp-notifications-notification.php */ … … 1022 1029 $conditions[] = "{$field} IN ({$_items})"; 1023 1030 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'] ) ); 1026 1033 $format = $where['format'][ $index ]; 1027 1034 1028 $conditions[] = "{$ field} = {$format}";1035 $conditions[] = "{$where_field} = {$format}"; 1029 1036 $values[] = $value; 1030 1037 } 1031 1038 1032 1039 $conditions = implode( ' AND ', $conditions ); 1040 1041 if ( 'id' === $field ) { 1042 $args['id'] = $items; 1043 } 1033 1044 1034 1045 /** This action is documented in bp-notifications/classes/class-bp-notifications-notification.php */ -
trunk/tests/phpunit/testcases/notifications/functions.php
r13112 r13243 5 5 */ 6 6 class BP_Tests_Notifications_Functions extends BP_UnitTestCase { 7 8 /**9 * @group cache10 */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' => $u17 ) );18 self::factory()->notification->create( array(19 'component_name' => 'messages',20 'user_id' => $u,21 'item_id' => 122 ) );23 24 // prime cache25 $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 save31 self::factory()->notification->create( array(32 'component_name' => 'messages',33 'user_id' => $u,34 'item_id' => 235 ) );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 cache43 */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' => $u49 ) );50 self::factory()->notification->create( array(51 'component_name' => 'messages',52 'user_id' => $u53 ) );54 55 // prime cache56 $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 // delete62 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 cache70 */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' => $u77 ) );78 self::factory()->notification->create( array(79 'component_name' => 'messages',80 'user_id' => $u81 ) );82 83 // prime cache84 $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 read90 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 cache101 */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' => $u107 ) );108 109 self::factory()->notification->create( array(110 'component_name' => 'messages',111 'user_id' => $u112 ) );113 114 // prime cache115 $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 read121 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_cache132 */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' => $u139 ) );140 141 $n2 = self::factory()->notification->create( array(142 'component_name' => 'groups',143 'user_id' => $u144 ) );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 7 184 8 /**
Note: See TracChangeset
for help on using the changeset viewer.