<!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][9753] trunk: Messages: Cache calls to `BP_Messages_Thread` class.</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 { 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/9753">9753</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/9753","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>r-a-y</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-04-15 22:44:55 +0000 (Wed, 15 Apr 2015)</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'>Messages: Cache calls to `BP_Messages_Thread` class.
This commit:
- Introduces static method - `BP_Messages_Thread::get_messages()` - caches fetching messages by thread ID and introduces the `'bp_messages_threads'` cache group.
- Converts `BP_Messages_Thread::check_access()` and `BP_Messages_Thread::is_valid()` methods from using direct DB queries to reference the cache.
- Includes unit tests.
- Consolidates cache clearing functions in bp-messages-cache.php.
This handles the majority of uncached message DB queries. For the inbox
page, if there are 10 message threads in the loop, we save 20 DB queries.
On a single message thread, we save 5 DB queries.
Fixes <a href="http://buddypress.trac.wordpress.org/ticket/6221">#6221</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpmessagesbpmessagescachephp">trunk/src/bp-messages/bp-messages-cache.php</a></li>
<li><a href="#trunksrcbpmessagesbpmessagesloaderphp">trunk/src/bp-messages/bp-messages-loader.php</a></li>
<li><a href="#trunksrcbpmessagesclassesclassbp_messagesthreadphp">trunk/src/bp-messages/classes/class-bp_messages-thread.php</a></li>
<li><a href="#trunktestsphpunittestcasesmessagesclassbpmessagesthreadphp">trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpmessagesbpmessagescachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/bp-messages-cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/bp-messages-cache.php 2015-04-15 22:38:41 UTC (rev 9752)
+++ trunk/src/bp-messages/bp-messages-cache.php 2015-04-15 22:44:55 UTC (rev 9753)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -46,31 +46,45 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'messages_screen_inbox', 'bp_core_clear_cache' );
</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">- * Clear unread count cache for each recipient after a message is sent.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Clear message cache after a message is saved.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since BuddyPress (2.0.0)
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param BP_Messages_Message $message
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function bp_messages_clear_unread_count_cache_on_message_save( BP_Messages_Message $message ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function bp_messages_clear_cache_on_message_save( BP_Messages_Message $message ) {
+ // Delete thread cache
+ wp_cache_delete( $message->thread_id, 'bp_messages_threads' );
+
+ // Delete unread count for each recipient
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( (array) $message->recipients as $recipient ) {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( $recipient->user_id, 'bp_messages_unread_count' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Delete thread recipient cache
+ wp_cache_delete( 'thread_recipients_' . $message->thread_id, 'bp_messages' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_action( 'messages_message_after_save', 'bp_messages_clear_unread_count_cache_on_message_save' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'messages_message_after_save', 'bp_messages_clear_cache_on_message_save' );
</ins><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">- * Clear unread count cache for the logged-in user after a message is deleted.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Clear message cache after a message thread is deleted.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since BuddyPress (2.0.0)
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int|array $thread_ids If single thread, the thread ID. Otherwise, an
</span><span class="cx" style="display: block; padding: 0 10px"> * array of thread IDs
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function bp_messages_clear_unread_count_cache_on_message_delete( $thread_ids ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function bp_messages_clear_cache_on_message_delete( $thread_ids ) {
+ // Delete thread and thread recipient cache
+ foreach( (array) $thread_ids as $thread_id ) {
+ wp_cache_delete( $thread_id, 'bp_messages_threads' );
+ wp_cache_delete( "thread_recipients_{$thread_id}", 'bp_messages' );
+ }
+
+ // Delete unread count for logged-in user
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_action( 'messages_before_delete_thread', 'bp_messages_clear_unread_count_cache_on_message_delete' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'messages_delete_thread', 'bp_messages_clear_cache_on_message_delete' );
</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"> * Invalidate cache for notices.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -84,29 +98,3 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'messages_notice_after_save', 'bp_notices_clear_cache' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'messages_notice_before_delete', 'bp_notices_clear_cache' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-/**
- * Invalidate thread recipient cache on message update.
- *
- * @since BuddyPress (2.3.0)
- *
- * @param BP_Messages_Message $message Message object.
- */
-function bp_messages_clear_message_thread_recipient_cache_on_message_sent( BP_Messages_Message $message ) {
- wp_cache_delete( 'thread_recipients_' . $message->thread_id, 'bp_messages' );
-}
-add_action( 'messages_message_sent', 'bp_messages_clear_message_thread_recipient_cache_on_message_sent' );
-
-/**
- * Invalidate thread recipient cache on thread deletion.
- *
- * @since BuddyPress (2.3.0)
- *
- * @param int|array $thread_ids IDs of deleted threads.
- */
-function bp_messages_clear_message_thread_recipient_cache_on_thread_delete( $thread_ids ) {
- foreach ( (array) $thread_ids as $thread_id ) {
- wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
- }
-}
-add_action( 'messages_delete_thread', 'bp_messages_clear_message_thread_recipient_cache_on_thread_delete' );
</del></span></pre></div>
<a id="trunksrcbpmessagesbpmessagesloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/bp-messages-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/bp-messages-loader.php 2015-04-15 22:38:41 UTC (rev 9752)
+++ trunk/src/bp-messages/bp-messages-loader.php 2015-04-15 22:44:55 UTC (rev 9753)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -303,6 +303,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Global groups
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_add_global_groups( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'bp_messages',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'bp_messages_threads',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'bp_messages_unread_count',
</span><span class="cx" style="display: block; padding: 0 10px"> 'message_meta'
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span></span></pre></div>
<a id="trunksrcbpmessagesclassesclassbp_messagesthreadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/classes/class-bp_messages-thread.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/classes/class-bp_messages-thread.php 2015-04-15 22:38:41 UTC (rev 9752)
+++ trunk/src/bp-messages/classes/class-bp_messages-thread.php 2015-04-15 22:44:55 UTC (rev 9753)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -147,12 +147,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->messages_order = $order;
</span><span class="cx" style="display: block; padding: 0 10px"> $this->thread_id = $thread_id;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $bp = buddypress();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // get messages for thread
+ $this->messages = self::get_messages( $thread_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( !$this->messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent " . $order, $this->thread_id ) ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // flip if order is DESC
+ if ( 'DESC' === $order ) {
+ $this->messages = array_reverse( $this->messages );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( (array) $this->messages as $key => $message ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->sender_ids[$message->sender_id] = $message->sender_id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -246,6 +252,31 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /** Static Functions ******************************************************/
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Get all messages associated with a thread.
+ *
+ * @since BuddyPress (2.3.0)
+ *
+ * @param int $thread_id The message thread ID
+ * @return array
+ */
+ public static function get_messages( $thread_id = 0 ) {
+ $messages = wp_cache_get( $thread_id, 'bp_messages_threads' );
+
+ if ( false === $messages ) {
+ global $wpdb;
+
+ $bp = buddypress();
+
+ // always sort by ASC by default
+ $messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", $thread_id ) );
+
+ wp_cache_set( $thread_id, (array) $messages, 'bp_messages_threads' );
+ }
+
+ return $messages;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Static method to get message recipients by thread ID.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since BuddyPress (2.3.0)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -663,17 +694,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $thread_id The message thread ID.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $user_id The user ID.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return int The message ID on success.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return int|null The recorded recipient ID on success, null on failure
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function check_access( $thread_id, $user_id = 0 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wpdb;
-
- if ( empty( $user_id ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $user_id ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $user_id = bp_loggedin_user_id();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $bp = buddypress();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $recipients = self::get_recipients_for_thread( $thread_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0 AND user_id = %d", $thread_id, $user_id ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $recipients[$user_id] ) && 0 == $recipients[$user_id]->is_deleted ) {
+ return $recipients[$user_id]->id;
+ } else {
+ return null;
+ }
</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"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -682,19 +716,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since BuddyPress (1.0.0)
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $thread_id The message thread ID.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return int The message thread ID on success.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return int|null The message thread ID on success, null on failure
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function is_valid( $thread_id = 0 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wpdb;
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Bail if no thread ID is passed
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $thread_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</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">- $bp = buddypress();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $thread = self::get_messages( $thread_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d LIMIT 1", $thread_id ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $thread ) ) {
+ return $thread_id;
+ } else {
+ return null;
+ }
</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"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestcasesmessagesclassbpmessagesthreadphp"></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/messages/class.bp-messages-thread.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php 2015-04-15 22:38:41 UTC (rev 9752)
+++ trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php 2015-04-15 22:44:55 UTC (rev 9753)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4,7 +4,72 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @group BP_Messages_Thread
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @group cache
+ */
+ public function test_construct_cache() {
+ $u1 = $this->factory->user->create();
+ $u2 = $this->factory->user->create();
+
+ $t1 = $this->factory->message->create( array(
+ 'sender_id' => $u1,
+ 'recipients' => array( $u2 ),
+ 'subject' => 'Foo',
+ ) );
+
+ // prime cache
+ new BP_Messages_Thread( $t1 );
+
+ // Cache should exist
+ $this->assertThat(
+ wp_cache_get( $t1, 'bp_messages_threads' ),
+ $this->logicalNot( $this->equalTo( false ) ),
+ 'Message thread cache should exist.'
+ );
+ }
+
+ /**
+ * @group order
+ */
+ public function test_construct_order_desc() {
+ $u1 = $this->factory->user->create();
+ $u2 = $this->factory->user->create();
+
+ // create thread
+ $t1 = $this->factory->message->create( array(
+ 'sender_id' => $u1,
+ 'recipients' => array( $u2 ),
+ 'subject' => 'Foo',
+ ) );
+ // save message ID
+ $thread = new BP_Messages_Thread( $t1 );
+ $m1 = wp_list_pluck( $thread->messages, 'id' );
+ $m1 = array_pop( $m1 );
+
+ // create reply
+ $t2 = $this->factory->message->create( array(
+ 'thread_id' => $t1,
+ 'sender_id' => $u1,
+ 'recipients' => array( $u2 ),
+ 'content' => 'Bar'
+ ) );
+ // save message ID
+ $thread = new BP_Messages_Thread( $t1 );
+ $m2 = wp_list_pluck( $thread->messages, 'id' );
+ $m2 = array_pop( $m2 );
+
+ // now get thread by DESC
+ $thread = new BP_Messages_Thread( $t1, 'DESC' );
+
+ // assert!
+ $this->assertEquals(
+ array( $m2, $m1 ),
+ wp_list_pluck( $thread->messages, 'id' )
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @group get_current_threads_for_user
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_current_threads_for_user_with_search_terms_inbox() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -286,4 +351,55 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->set_current_user( $current_user );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @group check_access
+ */
+ public function test_check_access_valid_thread() {
+ $u1 = $this->factory->user->create();
+ $u2 = $this->factory->user->create();
+
+ $t1 = $this->factory->message->create( array(
+ 'sender_id' => $u1,
+ 'recipients' => array( $u2 ),
+ 'subject' => 'Foo',
+ ) );
+
+ // save recipient ID
+ $thread = new BP_Messages_Thread( $t1 );
+ $r1 = wp_list_pluck( $thread->recipients, 'id' );
+ $r1 = array_pop( $r1 );
+
+ $this->assertEquals( $r1, BP_Messages_Thread::check_access( $t1, $u1 ) );
+ }
+
+ /**
+ * @group check_access
+ */
+ public function test_check_access_invalid_thread() {
+ $this->assertEquals( null, BP_Messages_Thread::check_access( 999, 1 ) );
+ }
+
+ /**
+ * @group is_valid
+ */
+ public function test_is_valid_valid_thread() {
+ $u1 = $this->factory->user->create();
+ $u2 = $this->factory->user->create();
+
+ $t1 = $this->factory->message->create( array(
+ 'sender_id' => $u1,
+ 'recipients' => array( $u2 ),
+ 'subject' => 'Foo',
+ ) );
+
+ $this->assertEquals( $t1, BP_Messages_Thread::is_valid( $t1 ) );
+ }
+
+ /**
+ * @group is_valid
+ */
+ public function test_is_valid_invalid_thread() {
+ $this->assertEquals( null, BP_Messages_Thread::is_valid( 999 ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>