You are here

public function MessageCron::testPurgeGlobalSettings in Message 8

Test global purge settings and overriding them.

File

tests/src/Functional/MessageCron.php, line 147

Class

MessageCron
Test message purging upon cron.

Namespace

Drupal\Tests\message\Functional

Code

public function testPurgeGlobalSettings() {

  // Set global purge settings.
  $quota = $this->purgeManager
    ->createInstance('quota', [
    'data' => [
      'quota' => 1,
    ],
  ]);
  $days = $this->purgeManager
    ->createInstance('days', [
    'data' => [
      'days' => 2,
    ],
  ]);
  $methods = [
    'quota' => $quota
      ->getConfiguration(),
    'days' => $days
      ->getConfiguration(),
  ];
  \Drupal::configFactory()
    ->getEditable('message.settings')
    ->set('purge_enable', TRUE)
    ->set('purge_methods', $methods)
    ->save();
  MessageTemplate::create([
    'template' => 'template1',
  ])
    ->save();

  // Create an overriding template with no purge methods.
  $data = [
    'purge_override' => TRUE,
    'purge_methods' => [],
  ];
  MessageTemplate::create([
    'template' => 'template2',
  ])
    ->setSettings($data)
    ->save();
  for ($i = 0; $i < 2; $i++) {
    Message::create([
      'template' => 'template1',
    ])
      ->setCreatedTime(time() - 3 * 86400)
      ->setOwnerId($this->account
      ->id())
      ->save();
    Message::create([
      'template' => 'template2',
    ])
      ->setCreatedTime(time() - 3 * 86400)
      ->setOwnerId($this->account
      ->id())
      ->save();
  }

  // Trigger message's hook_cron() as well as the queue processing.
  $this->cron
    ->run();
  $this
    ->assertEqual(count(Message::queryByTemplate('template1')), 0, t('All template1 messages deleted.'));
  $this
    ->assertEqual(count(Message::queryByTemplate('template2')), 2, t('Template2 messages were not deleted due to settings override.'));
}