You are here

function SimplenewsSchedulerNextRunTimeTest::testNextRunTimeTwoMonths in Simplenews Scheduler 2.0.x

Test a frequency of 2 months.

File

tests/src/Functional/SimplenewsSchedulerNextRunTimeTest.php, line 85

Class

SimplenewsSchedulerNextRunTimeTest
Testing next run times for newsletters monthly.

Namespace

Drupal\Tests\simplenews_scheduler\Functional

Code

function testNextRunTimeTwoMonths() {

  // The start date of the edition.
  $this->edition_day = '05';
  $start_date = new \DateTime("2012-01-{$this->edition_day} 12:00:00");

  // Fake newsletter parent data: sets the interval, start date, and frequency.
  $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(),
    // Needs to be set manually when creating new records programmatically.
    'stop_type' => '0',
    'stop_date' => '0',
    'stop_edition' => '0',
    'php_eval' => '',
    'title' => '[node:title] for [current-date:long]',
  );

  // Number of days to run for.
  $days = 370;

  // Index of the days we've done so far.
  $added_days = 0;

  // Iterate over days.
  while ($added_days <= $days) {

    // Create today's date at noon and get the timestamp.
    $date = clone $start_date;
    $date
      ->add(new \DateInterval("P{$added_days}D"));
    $timestamp_noon = $date
      ->getTimestamp();

    // Get the next run time from the API function we're testing.
    $next_run_time = simplenews_scheduler_calculate_next_run_time($newsletter_parent_data, $timestamp_noon);

    //debug($edition_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;
    $this
      ->assertTrue($next_run_time - $timestamp_noon <= $interval, t('Next run timestamp is less than or exactly two months in the future.'));

    // Create a date object from the timestamp. The '@' makes the constructor
    // consider the string as a timestamp.
    $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++;
  }

  // while days
}