You are here

public function TestSendingHour::testSendingHourNoneToSend in Notify 7

File

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

Class

TestSendingHour

Code

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

  // 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
  $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
  $hour = '1';
  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: 3 x 1hour old
  $this
    ->createContent(1, NULL, TRUE, $hoursago1);
  $this
    ->createContent(1, NULL, TRUE, $hoursago1);
  $this
    ->createContent(1, NULL, TRUE, $hoursago1);
  $send_hour = variable_get('notify_send_hour', 8);
  debug($send_hour, 'notify_send_hour (about two hours ago)');
  $cron_next = $now + 173000;

  // More than two days in the future
  $_debug = format_date($cron_next, 'custom', 'Y-m-d H:i:s O');
  debug($_debug, 'notify_cron_next (string) #1');
  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();
  $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');
}