Ticket #5419: 5419.01.patch
File 5419.01.patch, 3.8 KB (added by , 11 years ago) |
---|
-
bp-messages/bp-messages-cache.php
add_action( 'messages_message_sent', 'bp_core_clear_cache' ); 22 22 add_action( 'messages_screen_compose', 'bp_core_clear_cache' ); 23 23 add_action( 'messages_screen_sentbox', 'bp_core_clear_cache' ); 24 24 add_action( 'messages_screen_inbox', 'bp_core_clear_cache' ); 25 26 /** 27 * Invalidates cache for notices. 28 * 29 * Currently, invalidates active notice cache. 30 * 31 * @since BuddyPress (2.0.0) 32 */ 33 function bp_notices_clear_cache( $notice ) { 34 wp_cache_delete( 'active_notice', 'bp_messages' ); 35 } 36 add_action( 'messages_notice_after_save', 'bp_notices_clear_cache' ); 37 add_action( 'messages_notice_before_delete', 'bp_notices_clear_cache' ); 38 No newline at end of file -
bp-messages/bp-messages-classes.php
class BP_Messages_Notice { 577 577 public function delete() { 578 578 global $wpdb, $bp; 579 579 580 do_action_ref_array( 'messages_notice_before_delete', array( &$this ) ); 581 580 582 $sql = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ); 581 583 582 584 if ( ! $wpdb->query( $sql ) ) { … … class BP_Messages_Notice { 643 645 * @return object The BP_Messages_Notice object 644 646 */ 645 647 public static function get_active() { 646 global $wpdb, $bp; 648 $notice = wp_cache_get( 'active_notice', 'bp_messages' ); 649 650 if ( false === $notice ) { 651 global $wpdb, $bp; 647 652 648 $notice_id = $wpdb->get_var( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1" ); 653 $notice_id = $wpdb->get_var( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1" ); 654 $notice = new BP_Messages_Notice( $notice_id ); 655 656 wp_cache_set( 'active_notice', $notice, 'bp_messages' ); 657 } 649 658 650 return new BP_Messages_Notice( $notice_id );659 return $notice; 651 660 } 652 661 } -
tests/testcases/messages/class.bp-messages-notice.php
new file mode 100644
1 <?php 2 3 /** 4 * @group notices 5 */ 6 class BP_Tests_BP_Messages_Notice_TestCases extends BP_UnitTestCase { 7 8 protected $old_current_user = 0; 9 10 public function setUp() { 11 parent::setUp(); 12 $this->old_current_user = get_current_user_id(); 13 $this->set_current_user( $this->create_user( array( 'role' => 'administrator' ) ) ); 14 } 15 16 public function tearDown() { 17 parent::tearDown(); 18 $this->set_current_user( $this->old_current_user ); 19 } 20 21 /** 22 * @group cache 23 */ 24 public function test_get_active_notices() { 25 // send notice 26 $subject = 'Test notice'; 27 $message = 'This is a notice'; 28 messages_send_notice( $subject, $message ); 29 30 // now get the active notice and assert 31 $notice = BP_Messages_Notice::get_active(); 32 $this->assertEquals( $subject, $notice->subject ); 33 $this->assertEquals( $message, $notice->message ); 34 35 // deactivate notice and make sure cache is invalidated 36 $notice->deactivate(); 37 $this->assertFalse( wp_cache_get( 'active_notice', 'bp_messages' ) ); 38 39 // create a new notice 40 $subject2 = 'Another notice'; 41 $message2 = 'Say what?'; 42 messages_send_notice( $subject2, $message2 ); 43 44 // now get the new active notice 45 BP_Messages_Notice::get_active(); 46 47 // grab the cache and make sure it equals our new notice 48 $cache = wp_cache_get( 'active_notice', 'bp_messages' ); 49 $this->assertEquals( $subject2, $cache->subject ); 50 $this->assertEquals( $message2, $cache->message ); 51 } 52 53 }