You are here

function MessageCron::testPurge in Message 7

Test purging of messages upon message_cron according to message type purge settings.

File

tests/message.test, line 472

Class

MessageCron
Test the Message cron functionallity.

Code

function testPurge() {
  $web_user = $this
    ->drupalCreateUser();

  // Create a purgeable message type with max quota 2 and max days 0.
  $values = array(
    'data' => array(
      'purge' => array(
        'override' => TRUE,
        'enabled' => TRUE,
        'quota' => 2,
        'days' => 0,
      ),
    ),
  );
  $message_type = message_type_create('type1', $values);
  $message_type
    ->save();

  // Make sure the purging data is actually saved.
  $this
    ->assertEqual($message_type->data['purge'], $values['data']['purge'], t('Purge settings are stored in message type.'));

  // Create a purgeable message type with max quota 1 and max days 2.
  $values['data']['purge']['quota'] = 1;
  $values['data']['purge']['days'] = 2;
  $message_type = message_type_create('type2', $values);
  $message_type
    ->save();

  // Create a non purgeable message type with max quota 1 and max days 10.
  $values['data']['purge']['enabled'] = FALSE;
  $values['data']['purge']['quota'] = 1;
  $values['data']['purge']['days'] = 1;
  $message_type = message_type_create('type3', $values);
  $message_type
    ->save();
  $values = array(
    // Set messages creation time to three days ago.
    'timestamp' => time() - 3 * 86400,
  );

  // Create messages.
  for ($i = 0; $i < 4; $i++) {
    $message = message_create('type1', $values, $web_user);
    $message
      ->save();
  }
  for ($i = 0; $i < 3; $i++) {
    $message = message_create('type2', $values, $web_user);
    $message
      ->save();
  }
  for ($i = 0; $i < 3; $i++) {
    $message = message_create('type3', $values, $web_user);
    $message
      ->save();
  }

  // Trigger message's hook_cron().
  message_cron();

  // Four type1 messages were created. The first two should have been
  // deleted.
  $messages = message_load_multiple(FALSE, array(
    'type' => 'type1',
  ));
  $this
    ->assertEqual(array_keys($messages), array(
    3,
    4,
  ), t('Two messages deleted due to quota definition.'));

  // All type2 messages should have been deleted.
  $messages = message_load_multiple(FALSE, array(
    'type' => 'type2',
  ));
  $this
    ->assertEqual(array_keys($messages), array(), t('Three messages deleted due to age definition.'));

  // type3 messages should not have been deleted
  $messages = message_load_multiple(FALSE, array(
    'type' => 'type3',
  ));
  $this
    ->assertEqual(array_keys($messages), array(
    8,
    9,
    10,
  ), t('Messages with disabled purging settings were not deleted.'));
}