View source
<?php
namespace Drupal\Tests\simplenews_scheduler\Functional;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
class SimplenewsSchedulerEditionDueTest extends SimplenewsSchedulerTestBase {
function setUp() {
parent::setUp();
$admin_user = $this
->drupalCreateUser(array(
'access content',
'administer nodes',
'create simplenews_issue content',
'edit own simplenews_issue content',
'send newsletter',
'send scheduled newsletters',
'overview scheduled newsletters',
));
$this
->drupalLogin($admin_user);
$this->edition_day = '05';
$start_date = new \DateTime("2012-01-{$this->edition_day} 12:00:00");
$node = Node::create(array(
'type' => 'simplenews_issue',
'title' => 'Parent',
'uid' => 1,
'status' => 1,
));
$node->simplenews_issue->target_id = 'default';
$node->simplenews_issue->handler = 'simplenews_all';
$node
->save();
$node->simplenews_scheduler = (object) array(
'nid' => $node
->id(),
'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',
'title' => '[node:title] for [current-date:long]',
);
$record = (array) $node->simplenews_scheduler;
$query = \Drupal::database()
->merge('simplenews_scheduler');
$query
->key(array(
'nid' => $record['nid'],
))
->fields($record)
->execute();
$this->parent_nid = $node
->id();
}
function testEditionsDue() {
$parent_nid = $this->parent_nid;
$this
->drupalGet("node/{$parent_nid}");
$start_date = new \DateTime("2012-01-01 12:00:00");
$time_offsets = array(
'before' => "-1 hour",
'after' => "+1 hour",
);
$days = 150;
$added_days = 0;
while ($added_days <= $days) {
$date = clone $start_date;
$date
->add(new \DateInterval("P{$added_days}D"));
$timestamp_noon = $date
->getTimestamp();
foreach ($time_offsets as $offset_key => $offset) {
$timestamp = strtotime($offset, $timestamp_noon);
$due = simplenews_scheduler_get_newsletters_due($timestamp);
$formatted = date(DATE_RFC850, $timestamp);
if ($offset_key == 'after' && date('d', $timestamp) == $this->edition_day) {
$this
->assertTrue(isset($due[$parent_nid]), "Edition due at day {$added_days}, {$formatted}, {$timestamp}");
}
else {
$this
->assertFalse(isset($due[$parent_nid]), "Edition not due at day {$added_days}, {$formatted}, {$timestamp}");
}
$intervals['hour'] = 3600;
$intervals['day'] = 86400;
$intervals['week'] = $intervals['day'] * 7;
$intervals['month'] = $intervals['day'] * date('t', $timestamp);
if (isset($due[$parent_nid])) {
$newsletter_parent_data = $due[$parent_nid];
$edition_time = simplenews_scheduler_calculate_edition_time($newsletter_parent_data, $timestamp);
$eid = _simplenews_scheduler_new_edition($newsletter_parent_data->nid, $timestamp);
$result = \Drupal::database()
->query("SELECT last_run FROM {simplenews_scheduler} WHERE nid = :nid", array(
':nid' => $parent_nid,
));
$last_run = $result
->fetchField();
$formatted = date(DATE_RFC850, $last_run);
$formatted = date(DATE_RFC850, $edition_time);
$this
->assertEqual(date('H:i', $edition_time), '12:00', t('Edition time is at 12:00.'));
\Drupal::database()
->update('simplenews_scheduler')
->fields(array(
'last_run' => $timestamp,
))
->condition('nid', $parent_nid)
->execute();
simplenews_scheduler_scheduler_update($newsletter_parent_data, $timestamp);
$this
->drupalGet("node/{$eid}");
}
}
$added_days++;
}
}
}