You are here

function MessageCron::testPurgeRequestLimit in Message 7

Test compliance with MESSAGE_DELETE_PER_PURGE.

File

tests/message.test, line 545

Class

MessageCron
Test the Message cron functionallity.

Code

function testPurgeRequestLimit() {

  // Set maximal amount of messages to delete.
  variable_set('message_delete_cron_limit', 10);
  $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();
  $message_type = message_type_create('type2', $values);
  $message_type
    ->save();

  // Create more messages than may be deleted in one request.
  for ($i = 0; $i < 10; $i++) {
    $message = message_create('type1', array(), $web_user);
    $message
      ->save();
    $message = message_create('type2', array(), $web_user);
    $message
      ->save();
  }

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

  // There are 16 messages to be deleted and 10 deletions allowed, so 8
  // messages of type1 and 2 messages of type2 should be deleted, thus 2
  // messages of type1 and 8 messages of type2 remain.
  $messages = message_load_multiple(FALSE, array(
    'type' => 'type1',
  ));
  $this
    ->assertEqual(count($messages), 2, t('Two messages of type 1 left.'));
  $messages = message_load_multiple(FALSE, array(
    'type' => 'type2',
  ));
  $this
    ->assertEqual(count($messages), 8, t('Eight messages of type 2 left.'));
}