You are here

function SimpleNewsSchedulerNodeCreationTest::testNewsletterGeneration in Simplenews Scheduler 6.2

Same name and namespace in other branches
  1. 7 tests/simplenews_scheduler.test \SimpleNewsSchedulerNodeCreationTest::testNewsletterGeneration()

Basic simplenews newsletter generation test create a simplenews node,

File

tests/simplenews_scheduler.test, line 104
Tests for Simplenews Scheduler.

Class

SimpleNewsSchedulerNodeCreationTest
Test scheduled edition creation.

Code

function testNewsletterGeneration() {
  $edit = array();
  $title = "newsletter " . $this
    ->randomName(8);

  // Create a template newsletter node.
  $edit = array();
  $edit['title'] = $title;
  $edit['body'] = $this
    ->randomName(16);

  // Tests run on a clean enviroment; we can assume the newsletter taxonomy
  // term ID is 1.
  $edit['taxonomy[1]'] = 1;
  $this
    ->drupalPost('node/add/simplenews', $edit, t('Save'));
  $this
    ->assertText($title);
  preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches);
  $node = node_load($matches[1]);

  // The 'Editions' node tab should not yet be accessible.
  $this
    ->drupalGet("node/{$node->nid}/editions");
  $this
    ->assertResponse(403, 'Access is denied on the editions tab for a non-scheduled node.');

  // Now create the simplenews schedule configuration.
  $this
    ->drupalGet("node/{$node->nid}/simplenews");
  $this
    ->assertText(t("Send newsletter according to schedule"));
  $edit = array();
  $edit["simplenews[send]"] = '3';
  $edit["simplenews[scheduler][send_interval]"] = "hour";

  // Specify a start time 30 minutes in the past to be able to have a known
  // edition creation time that can be checked.
  $date = new DateTime('now', $this->site_timezone_object);
  $date
    ->modify('-30 minutes');
  $edit["simplenews[scheduler][start_date][year]"] = $date
    ->format('Y');
  $edit["simplenews[scheduler][start_date][month]"] = $date
    ->format('n');
  $edit["simplenews[scheduler][start_date][day]"] = $date
    ->format('j');
  $edit["simplenews[scheduler][start_date][hour]"] = $date
    ->format('G');
  $edit["simplenews[scheduler][start_date][minute]"] = $date
    ->format('i');
  $this
    ->drupalPost("node/{$node->nid}/simplenews", $edit, t('Submit'));

  // Make sure it knows no editions created yet.
  $this
    ->drupalGet("node/{$node->nid}/editions");
  $this
    ->assertText(t("No scheduled newsletters have been sent."));

  // Force a clear of node_load()'s static cache. This ensures that the
  // call to _simplenews_scheduler_new_edition() during the simulated cron
  // gets scheduler data loaded into the node when it calls node_load().
  node_load(NULL, NULL, TRUE);

  // Call our hook to generate it.
  simplenews_scheduler_cron();

  // See if it was created.
  $this
    ->drupalGet("node/{$node->nid}/editions");
  $this
    ->assertText($title);
  $this
    ->assertNoText(t("No scheduled newsletter editions have been sent."));

  // Go to node and verify creation time. Due to the breadcrumb, this is the
  // second link with the label on the page.
  $this
    ->clickLink($title, 1);
  $this
    ->assertText(t('@date', array(
    '@date' => $this
      ->format_date_helper($date),
  )));
  $this
    ->drupalGet("node/2/editions");
  $this
    ->assertText(t('This node is part of a scheduled newsletter configuration. View the original newsletter here.'));
}