View source
<?php
namespace Drupal\Tests\simplenews_scheduler\Functional;
class SimplenewsSchedulerNextRunTimeTest extends SimplenewsSchedulerTestBase {
function testNextRunTimeOneMonth() {
$this->edition_day = '05';
$start_date = new \DateTime("2012-01-{$this->edition_day} 12:00:00");
$newsletter_parent_data = (object) array(
'nid' => 1,
'last_run' => 0,
'activated' => '1',
'send_interval' => 'month',
'interval_frequency' => '1',
'start_date' => $start_date
->getTimestamp(),
'next_run' => $start_date
->getTimestamp(),
'stop_type' => '0',
'stop_date' => '0',
'stop_edition' => '0',
'php_eval' => '',
'title' => '[node:title] for [current-date:long]',
);
$days = 370;
$added_days = 0;
$last_run_time = $start_date
->getTimestamp();
while ($added_days <= $days) {
$date = clone $start_date;
$date
->add(new \DateInterval("P{$added_days}D"));
$timestamp_noon = $date
->getTimestamp();
$next_run_time = simplenews_scheduler_calculate_next_run_time($newsletter_parent_data, $timestamp_noon);
if ($next_run_time != $last_run_time) {
$offset = _simplenews_scheduler_make_time_offset($newsletter_parent_data->send_interval, $newsletter_parent_data->interval_frequency);
$next_run_date = date_add(date_create(date('Y-m-d H:i:s', $last_run_time)), date_interval_create_from_date_string($offset));
$this
->assertEqual($next_run_date
->getTimestamp(), $next_run_time, t('New next run timestamp has advanced by the expected offset of 2offset.', array(
'@offset' => $offset,
)));
$last_run_time = $next_run_time;
}
$this
->assertTrue($timestamp_noon < $next_run_time, t('Next run time of @next-run is in the future relative to current time of @now.', array(
'@next-run' => date("Y-n-d H:i:s", $next_run_time),
'@now' => date("Y-n-d H:i:s", $timestamp_noon),
)));
$interval = $newsletter_parent_data->interval_frequency * 31 * 24 * 60 * 60;
$next_run_date = new \DateTime(date('Y-m-d H:i:s', $last_run_time));
$d = date_format($next_run_date, 'd');
$this
->assertEqual($next_run_date
->format('d'), $this->edition_day, t('Next run timestamp is on same day of the month as the start date.'));
$this
->assertEqual($next_run_date
->format('H:i:s'), '12:00:00', t('Next run timestamp is at the same time.'));
$added_days++;
}
}
function testNextRunTimeTwoMonths() {
$this->edition_day = '05';
$start_date = new \DateTime("2012-01-{$this->edition_day} 12:00:00");
$newsletter_parent_data = (object) array(
'nid' => 1,
'last_run' => 0,
'activated' => '1',
'send_interval' => 'month',
'interval_frequency' => '2',
'start_date' => $start_date
->getTimestamp(),
'next_run' => $start_date
->getTimestamp(),
'stop_type' => '0',
'stop_date' => '0',
'stop_edition' => '0',
'php_eval' => '',
'title' => '[node:title] for [current-date:long]',
);
$days = 370;
$added_days = 0;
while ($added_days <= $days) {
$date = clone $start_date;
$date
->add(new \DateInterval("P{$added_days}D"));
$timestamp_noon = $date
->getTimestamp();
$next_run_time = simplenews_scheduler_calculate_next_run_time($newsletter_parent_data, $timestamp_noon);
$this
->assertTrue($timestamp_noon < $next_run_time, t('Next run time of @next-run is in the future relative to current time of @now.', array(
'@next-run' => date("Y-n-d H:i:s", $next_run_time),
'@now' => date("Y-n-d H:i:s", $timestamp_noon),
)));
$interval = $newsletter_parent_data->interval_frequency * 31 * 24 * 60 * 60;
$this
->assertTrue($next_run_time - $timestamp_noon <= $interval, t('Next run timestamp is less than or exactly two months in the future.'));
$next_run_date = new \DateTime("@{$next_run_time}");
$d = date_format($next_run_date, 'd');
$this
->assertEqual($next_run_date
->format('d'), $this->edition_day, t('Next run timestamp is on same day of the month as the start date.'));
$added_days++;
}
}
}