Skip to:
Content

BuddyPress.org

Ticket #6220: 6220.read.patch

File 6220.read.patch, 3.3 KB (added by r-a-y, 10 years ago)
  • src/bp-messages/classes/class-bp_messages-thread.php

     
    508508                $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
    509509                $wpdb->query($sql);
    510510
     511                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    511512                wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
    512513        }
    513514
     
    525526                $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
    526527                $wpdb->query($sql);
    527528
     529                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    528530                wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
    529531        }
    530532
  • tests/phpunit/testcases/messages/class.bp-messages-thread.php

     
    216216                // Cache should be empty.
    217217                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
    218218        }
     219
     220        /**
     221         * @group get_recipients
     222         * @group cache
     223         * @group raymsg
     224         */
     225        public function test_get_recipients_cache_should_be_busted_when_thread_is_read() {
     226                global $wpdb;
     227
     228                $u1 = $this->factory->user->create();
     229                $u2 = $this->factory->user->create();
     230
     231                $t1 = $this->factory->message->create( array(
     232                        'sender_id' => $u1,
     233                        'recipients' => array( $u2 ),
     234                        'subject' => 'Foo',
     235                ) );
     236
     237                $thread = new BP_Messages_Thread( $t1 );
     238                $recipients = $thread->get_recipients();
     239
     240                // Verify that the cache is populated.
     241                $num_queries = $wpdb->num_queries;
     242                $recipients_cached = $thread->get_recipients();
     243                $this->assertEquals( $num_queries, $wpdb->num_queries );
     244
     245                // Mark thread as read
     246                $current_user = get_current_user_id();
     247                $this->set_current_user( $u2 );
     248                messages_mark_thread_read( $t1 );
     249
     250                // Cache should be empty.
     251                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
     252
     253                $this->set_current_user( $current_user );
     254        }
     255
     256        /**
     257         * @group get_recipients
     258         * @group cache
     259         */
     260        public function test_get_recipients_cache_should_be_busted_when_thread_is_unread() {
     261                global $wpdb;
     262
     263                $u1 = $this->factory->user->create();
     264                $u2 = $this->factory->user->create();
     265
     266                $t1 = $this->factory->message->create( array(
     267                        'sender_id' => $u1,
     268                        'recipients' => array( $u2 ),
     269                        'subject' => 'Foo',
     270                ) );
     271
     272                $thread = new BP_Messages_Thread( $t1 );
     273                $recipients = $thread->get_recipients();
     274
     275                // Verify that the cache is populated.
     276                $num_queries = $wpdb->num_queries;
     277                $recipients_cached = $thread->get_recipients();
     278                $this->assertEquals( $num_queries, $wpdb->num_queries );
     279
     280                // Mark thread as unread
     281                $current_user = get_current_user_id();
     282                $this->set_current_user( $u2 );
     283                messages_mark_thread_unread( $t1 );
     284
     285                // Cache should be empty.
     286                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
     287
     288                $this->set_current_user( $current_user );
     289        }
    219290}