You are here

function PrivatemsgTestCase::testPrivatemsgFlush in Privatemsg 6

Same name and namespace in other branches
  1. 6.2 privatemsg.test \PrivatemsgTestCase::testPrivatemsgFlush()
  2. 7.2 privatemsg.test \PrivatemsgTestCase::testPrivatemsgFlush()
  3. 7 privatemsg.test \PrivatemsgTestCase::testPrivatemsgFlush()

Tests for the flush feature

File

./privatemsg.test, line 418
Test file for privatemsg.module

Class

PrivatemsgTestCase
@file Test file for privatemsg.module

Code

function testPrivatemsgFlush() {
  $author = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
  ));
  $recipient = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
  ));

  // Send 10 messages.
  for ($i = 0; $i < 10; $i++) {
    privatemsg_new_thread(array(
      $recipient,
    ), 'Message #' . $i, 'This is the body', array(
      'author' => $author,
    ));
  }

  // Delete message 1, 3, 4, 6, 9 for author.
  foreach (array(
    1,
    3,
    4,
    6,
    9,
  ) as $pmid) {
    privatemsg_message_change_delete($pmid, TRUE, $author);
  }

  // Delete message 1, 2, 4, 6, 8 for recipient.
  foreach (array(
    1,
    3,
    4,
    6,
    9,
  ) as $pmid) {
    privatemsg_message_change_delete($pmid, TRUE, $recipient);
  }

  // Now, mid 1, 4 and 6 have been deleted by both.
  // Flush configuration, enable, delay is default, 30 days
  variable_set('privatemsg_flush_enabled', TRUE);

  // Set back the deleted timestamp 35 days back of mid 4.
  db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 4', time() - 35 * 86400);

  // Set back the deleted timestamp of mid 6, but only 20 back.
  db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 6', time() - 20 * 86400);

  // Run flush.
  privatemsg_cron();

  // Check if the undeleted messages are still there.
  foreach (array(
    2,
    3,
    5,
    7,
    8,
    9,
    10,
  ) as $pmid) {
    $message = privatemsg_message_load($pmid, $author);
    $this
      ->assertTrue(!empty($message), t('Undeleted message #%id is still in the system', array(
      '%id' => $pmid,
    )));
  }

  // Check if the "recently" deleted  messages are still there.
  foreach (array(
    1,
    6,
  ) as $pmid) {
    $message = privatemsg_message_load($pmid, $author);
    $this
      ->assertTrue(!empty($message), t('Deleted message #%id is still in the system', array(
      '%id' => $pmid,
    )));
  }

  // Mid 4 should have been flushed.
  $message = privatemsg_message_load(4, $author);
  $this
    ->assertTrue(empty($message), t('Message #4 has been flushed'));
}