You are here

public function TestSendingHour::testRespectSendingHour in Notify 7

File

./notify.test, line 294
Automated tests for notify.

Class

TestSendingHour

Code

public function testRespectSendingHour() {
  debug('DEBUG START: testRespectSendingHour');

  // Set up notify environment.
  variable_set('notify_attempts', 5);
  variable_set('notify_include_updates', 0);
  variable_set('notify_reg_default', 1);
  variable_set('notify_period', 172800);

  // every 2 days

  //variable_set('mail_system', array('default-system' => 'TestingMailSystem'));
  $hour = date('G', REQUEST_TIME);
  if ($hour < 3) {
    return $this
      ->assert('exception', 'Test "testRespectSendingHour" can only be run after 0300 hours.', 'Notify');
  }
  $hour = date('G', REQUEST_TIME - 7200);

  // Two hours ago
  variable_set('notify_send_hour', (int) $hour);
  variable_set('notify_batchsize', 100);

  // Set up some times.
  $now = REQUEST_TIME;
  $yesterday = $now - 87000;

  // yesterday - 10 min
  $hoursago1 = format_date($now - 3600, 'custom', 'Y-m-d H:i:s O');
  $hoursago5 = format_date($now - 18000, 'custom', 'Y-m-d H:i:s O');

  // Content is: 1 x old, 1 x 5 hour old, 1 x 1hour old
  $this
    ->createContent(1, NULL, TRUE, '2014-01-01 14:00:00 +0100');
  $this
    ->createContent(1, NULL, TRUE, $hoursago5);
  $this
    ->createContent(1, NULL, TRUE, $hoursago1);
  $send_hour = variable_get('notify_send_hour', 8);

  //debug($send_hour, 'notify_send_hour (about two hours ago)');
  variable_set('notify_send_start', 0);

  // reset
  variable_set('notify_send_last', $yesterday);

  //$cron_next = $now + 6000; // +1:40 (future)

  //$_debug = format_date($cron_next, 'custom', 'Y-m-d H:i:s O');

  //debug($_debug, 'notify_cron_next (string) #1');
  debug('DEBUG: Not setting $cron_next.');
  $this
    ->cronRun();
  $mails = $this
    ->drupalGetMails();
  $howmany = count($mails);
  $this
    ->assertEqual($howmany, 0, 'No emails are sent.');
  $cron_next = $now - 6000;

  // -1:40 (past)

  //$_debug = format_date($cron_next, 'custom', 'Y-m-d H:i:s O');

  //debug($_debug, 'notify_cron_next (string) #2');
  variable_set('notify_send_start', 0);

  // reset
  variable_set('notify_send_last', $yesterday);
  variable_set('notify_cron_next', $cron_next);
  $this
    ->cronRun();
  $mails = $this
    ->drupalGetMails();
  $firstbatch = count($mails);
  $this
    ->assertEqual($firstbatch, 3, 'Three emails are sent.');
  $cron_next = variable_get('notify_cron_next', 0);

  //$_debug = format_date($cron_next, 'custom', 'Y-m-d H:i:s O');

  //debug($_debug, 'notify_cron_next (string) #3');
}