You are here

function SimpleNewsSchedulerNodeCreationTest::testNewsletterGeneration in Simplenews Scheduler 7

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

Basic simplenews newsletter generation test create a simplenews node,

File

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

Class

SimpleNewsSchedulerNodeCreationTest
Test scheduled edition creation.

Code

function testNewsletterGeneration() {
  $edit = array();
  $title = "newsletter " . $this
    ->randomName(8);
  $edit = array();
  $edit['title'] = $title;
  $edit["body[und][0][value]"] = $this
    ->randomName(16);
  $this
    ->drupalPost('node/add/simplenews', $edit, t('Save'));
  $this
    ->assertText($title);
  preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches);
  $node = node_load($matches[1]);

  // Make sure that the editions tab is not visible as long as it's not a
  // scheduled newsletter.
  $this
    ->drupalGet("node/{$node->nid}/editions");
  $this
    ->assertResponse(403, t('Editions tab not accessible'));

  // 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]"] = '4';
  $edit["simplenews[scheduler][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();
  $date
    ->sub(new DateInterval('PT30M'));
  $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');
  $edit["simplenews[scheduler][title]"] = "Custom title [site:name]";
  $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 newsletter editions have been sent."));

  // Execute cron.
  drupal_cron_run();

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

  // original real node title
  // Go to node and verify creation time and token for custom title
  // @todo: make this real token integration
  $this
    ->clickLink("Custom title " . variable_get('site_name', 'Drupal'));
  $this
    ->assertText(t('This is a newsletter edititon. View the the master template of this newsletter here'));
  $this
    ->assertText(t('Submitted by @name on @date', array(
    '@name' => format_username($this->privileged_user),
    '@date' => format_date($date
      ->getTimestamp()),
  )));

  // Check sent mails.
  $mails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(1, count($mails), t('Newsletter mail has been sent.'));
  $this
    ->clickLink(t('Newsletter'));
  $this
    ->assertText(t('This node is part of a scheduled newsletter configuration.'));
  $this
    ->clickLink(t('here'));
  $this
    ->assertEqual(url('node/' . $node->nid, array(
    'absolute' => TRUE,
  )), $this
    ->getUrl());

  // Test the tab on a sent newsletter, schedule details should not be shown.
  $title = "newsletter " . $this
    ->randomName(8);
  $edit = array();
  $edit['title'] = $title;
  $edit["body[und][0][value]"] = $this
    ->randomName(16);
  $this
    ->drupalPost('node/add/simplenews', $edit, t('Save'));
  $this
    ->assertText($title);
  preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches);
  $node = node_load($matches[1]);
  $edit = array();
  $edit["simplenews[send]"] = SIMPLENEWS_COMMAND_SEND_NOW;
  $this
    ->drupalPost("node/{$node->nid}/simplenews", $edit, t('Submit'));
  $this
    ->assertNoText(t('Schedule details'));

  // Check sent mails.
  $mails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(1, count($mails), t('Newsletter mail has been sent.'));
}