Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.73
diff -u -p -r1.70.2.30.2.91.2.73 privatemsg.module
--- privatemsg.module	10 Oct 2009 08:04:48 -0000	1.70.2.30.2.91.2.73
+++ privatemsg.module	12 Oct 2009 15:42:39 -0000
@@ -676,11 +676,6 @@ function privatemsg_unread_count($accoun
 function privatemsg_view($thread) {
   drupal_set_title(check_plain($thread['subject']));
 
-  if ($thread['read_all']) {
-    // User has permission to read all messages AND is not a participant of the current thread.
-    drupal_set_message(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), 'warning');
-  }
-
   // Render the participants.
   $content['participants']['#value'] = theme('privatemsg_recipients', $thread);
   $content['participants']['#weight'] = -5;
@@ -703,6 +698,13 @@ function privatemsg_view($thread) {
     $content['reply']['#weight'] = 5;
   }
 
+  // Check after calling the privatemsg_new form so that this message is only
+  // displayed when we are not sending a message.
+  if ($thread['read_all']) {
+    // User has permission to read all messages AND is not a participant of the current thread.
+    drupal_set_message(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), 'warning');
+  }
+
   //allow other modules to hook into the $content array and alter it
   drupal_alter('privatemsg_view_messages', $content, $thread);
   return drupal_render($content);
@@ -1618,6 +1620,19 @@ function _privatemsg_send($message) {
 
   drupal_alter('privatemsg_message_presave', $message);
 
+  $index_sql = "INSERT INTO {pm_index} (mid, thread_id, uid, is_new, deleted) VALUES (%d, %d, %d, %d, 0)";
+  if (isset($message['read_all']) && $message['read_all']) {
+    // The message was sent in read all mode, add the author as recipient to all
+    // existing messages.
+    $query_messages = _privatemsg_assemble_query('messages', array($message['thread_id']), NULL);
+    $conversation = db_query($query_messages['query']);
+    while ($result = db_fetch_array($conversation)) {
+      if (!db_query($index_sql, $result['mid'], $message['thread_id'], $message['author']->uid, 0)) {
+        return FALSE;
+      }
+    }
+  }
+
   // 1) Save the message body first.
   $args = array();
   $args[] = $message['subject'];
@@ -1625,8 +1640,8 @@ function _privatemsg_send($message) {
   $args[] = $message['body'];
   $args[] = $message['format'];
   $args[] = $message['timestamp'];
-  $query = "INSERT INTO {pm_message} (subject, author, body, format, timestamp) VALUES ('%s', %d, '%s', %d, %d)";
-  $resuld = db_query($query, $args);
+  $message_sql = "INSERT INTO {pm_message} (subject, author, body, format, timestamp) VALUES ('%s', %d, '%s', %d, %d)";
+  $resuld = db_query($message_sql, $args);
   $mid = db_last_insert_id('pm_message', 'mid');
   $message['mid'] = $mid;
 
@@ -1637,33 +1652,20 @@ function _privatemsg_send($message) {
 
   // 2) Save message to recipients.
   // Each recipient gets a record in the pm_index table.
-  $query = "INSERT INTO {pm_index} (mid, thread_id, uid, is_new, deleted) VALUES (%d, %d, %d, %d, 0)";
   foreach ($message['recipients'] as $recipient) {
-    if (!db_query($query, $mid, $message['thread_id'], $recipient->uid, 1) ) {
+    if (!db_query($index_sql, $mid, $message['thread_id'], $recipient->uid, 1) ) {
       // We assume if one insert failed then the rest may fail too against the
       // same table.
       return FALSE;
     }
   }
 
-  if (isset($message['read_all'])) {
-    // The message was sent in read all mode, add the author as recipient to all
-    // existing messages.
-    $query_messages = _privatemsg_assemble_query('messages', array($message['thread_id']), NULL);
-    $conversation = db_query($query_messages['query']);
-    while ($result = db_fetch_array($conversation)) {
-      if (!db_query($query, $result['mid'], $message['thread_id'], $message['author']->uid, 0)) {
-        return FALSE;
-      }
-    }
-  }
-
   // When author is also the recipient, we want to set message to UNREAD.
   // All other times the message is set to READ.
   $is_new = isset($message['recipients'][$message['author']->uid]) ? 1 : 0;
 
   // Also add a record for the author to the pm_index table.
-  if (!db_query($query, $mid, $message['thread_id'], $message['author']->uid, $is_new)) {
+  if (!db_query($index_sql, $mid, $message['thread_id'], $message['author']->uid, $is_new)) {
     return FALSE;
   }
 
Index: privatemsg.test
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.test,v
retrieving revision 1.2.2.2
diff -u -p -r1.2.2.2 privatemsg.test
--- privatemsg.test	10 Oct 2009 08:04:48 -0000	1.2.2.2
+++ privatemsg.test	12 Oct 2009 15:42:40 -0000
@@ -111,6 +111,64 @@ class PrivatemsgTestCase extends DrupalW
   }
 
   /**
+   * Test correct handling of read all permissions.
+   */
+  function testReadAllPermission() {
+    $author    = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
+    $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
+    $admin     = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'read all private messages'));
+
+    // Create new message.
+    $edit = array(
+      'recipient'   => $recipient->name,
+      'subject'     => $this->randomName(20),
+      'body'        => $this->randomName(100),
+    );
+    $this->drupalLogin($author);
+    $this->drupalPost('messages/new', $edit, t('Send message'));
+
+    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), t('Message sent confirmation displayed'));
+
+    $this->drupalLogin($admin);
+    $this->drupalGet('messages/view/1');
+
+    $this->assertText(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), t('Notice about read all mode displayed.'));
+
+    // Send a first response.
+    $admin_edit = array(
+      'body'        => $this->randomName(100),
+    );
+    $this->drupalPost('messages/view/1', $admin_edit, t('Send message'));
+    
+    // Make sure that the notice is not displayed anymore.
+    // @tod: Commented out because this does not work as expected.
+    $this->assertNoText(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), t('Notice about read all mode not displayed.'));
+
+    // Make sure that both the existing message body and the new one are displayed.
+    $this->assertText($edit['body'], t('First message body displayed.'));
+    $this->assertText($admin_edit['body'], t('New message body displayed.'));
+
+    $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
+    $this->assertEqual($admin_recipient_count, 2, t('Admin is listed as recipient for every message once.'));
+
+
+    // Send a second response.
+    $admin_edit2 = array(
+      'body'        => $this->randomName(100),
+    );
+    $this->drupalPost('messages/view/1', $admin_edit2, t('Send message'));
+
+    // Make sure that both the existing message body and the new one are displayed.
+    $this->assertText($edit['body'], t('First message body displayed.'));
+    $this->assertText($admin_edit['body'], t('Second response body displayed.'));
+    $this->assertText($admin_edit2['body'], t('Third message body displayed.'));
+
+    $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
+    $this->assertEqual($admin_recipient_count, 3, t('Admin is listed as recipient for every message once.'));
+
+  }
+
+  /**
    * Tests for the flush feature
    */
   function testPrivatemsgFlush()
Index: privatemsg_filter/privatemsg_filter.test
===================================================================
RCS file: privatemsg_filter/privatemsg_filter.test
diff -N privatemsg_filter/privatemsg_filter.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ privatemsg_filter/privatemsg_filter.test	12 Oct 2009 15:42:40 -0000
@@ -0,0 +1,72 @@
+<?php
+class PrivatemsgFilterTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Privatemsg Filter functionality.'),
+      'description' => t('Test filters, tags and inbox/sent handling'),
+      'group' => t('Privatemsg'),
+    );
+  }
+
+  /**
+   * Implementation of setUp().
+   */
+  function setUp() {
+    parent::setUp('privatemsg', 'privatemsg_filter');
+  }
+
+  /**
+   * Test correct handling of read all permissions.
+   */
+  function testInboxSentHandling() {
+    $author    = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
+    $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
+
+    // Create new message.
+    $edit = array(
+      'recipient'   => $recipient->name,
+      'subject'     => $this->randomName(20),
+      'body'        => $this->randomName(100),
+    );
+    $this->drupalLogin($author);
+    $this->drupalPost('messages/new', $edit, t('Send message'));
+    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), t('Message sent confirmation displayed'));
+
+    // Validate that the message is not displayed in the inbox of the author
+    // but in the sent list.
+    $this->drupalGet('messages');
+    $this->assertNoText($edit['subject'], t('Thread not displayed in inbox for author.'));
+    $this->drupalGet('messages/sent');
+    $this->assertText($edit['subject'], t('Thread displayed in "Sent messages" for author.'));
+    $this->drupalGet('messages/list');
+    $this->assertText($edit['subject'], t('Thread displayed in "All messages" for author.'));
+
+    // Write a reply as recipient.
+    $this->drupalLogin($recipient);
+    $this->drupalGet('messages');
+    $this->assertText($edit['subject'], t('Thread displayed in inbox for recipient.'));
+    $this->drupalGet('messages/sent');
+    $this->assertNoText($edit['subject'], t('Thread not displayed in "Sent messages" for recipient.'));
+    $this->drupalGet('messages/list');
+    $this->assertText($edit['subject'], t('Thread displayed in "All messages." for recipient.'));
+
+    // Navigate to the new message.
+    $this->clickLink($edit['subject']);
+    $response = array(
+      'body' => $this->randomName(100),
+    );
+    $this->drupalPost(NULL, $response, t('Send message'));
+    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $author->name)), t('Message sent confirmation displayed'));
+
+    $this->drupalGet('messages/sent');
+    $this->assertText($edit['subject'], t('Thread displayed in "Sent messages" for recipient.'));
+
+    $this->drupalLogin($author);
+    $this->drupalGet('messages');
+    $this->assertText($edit['subject'], t('Thread displayed in inbox for author.'));
+  }
+}
+?>
