You are here

function SimplenewsSchedulerNodeCreationTest::testNewsletterGeneration in Simplenews Scheduler 2.0.x

Basic simplenews newsletter generation test create a simplenews node,

File

tests/src/Functional/SimplenewsSchedulerNodeCreationTest.php, line 43

Class

SimplenewsSchedulerNodeCreationTest
Testing generation of newsletters.

Namespace

Drupal\Tests\simplenews_scheduler\Functional

Code

function testNewsletterGeneration() {
  $title = "newsletter " . $this
    ->randomMachineName(8);
  $edit = array(
    'title[0][value]' => $title,
    'body[0][value]' => $this
      ->randomMachineName(16),
    'simplenews_issue[target_id]' => 'default',
    'status[value]' => TRUE,
  );
  $this
    ->drupalPostForm('node/add/simplenews_issue', $edit, t('Save'));
  $this
    ->assertText($title);
  $node = $this
    ->drupalGetNodeByTitle($title);

  // Make sure that the editions tab is not visible as long as it's not a
  // scheduled newsletter.
  $this
    ->drupalGet("node/{$node->id()}/editions");
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Now create the simplenews schedule configuration.
  $this
    ->drupalGet("node/" . $node
    ->id() . "/simplenews");
  $this
    ->assertText(t('Enable scheduled newsletter'));
  $edit = array();
  $edit['enable_scheduler'] = TRUE;
  $edit["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["start_date[date]"] = $date
    ->format('Y-m-d');
  $edit["start_date[time]"] = $date
    ->format('H:i:s');
  $edit["title"] = "Custom title [node:nid]";
  $this
    ->drupalPostForm("node/{$node->id()}/simplenews", $edit, t('Save scheduler settings'));

  // Check if the saving was successful.
  $this
    ->assertText('Newsletter schedule preferences have been saved.');
  $this
    ->assertFieldByName('start_date[date]', $date
    ->format('Y-m-d'));
  $this
    ->assertFieldByName('start_date[time]', $date
    ->format('H:i:s'));
  $this
    ->assertFieldByName('title', "Custom title [node:nid]");

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

  // Execute cron.
  \Drupal::service('cron')
    ->run();
  $edition_nids = \Drupal::entityQuery('node')
    ->sort('nid', 'DESC')
    ->execute();
  $edition_node = Node::load(reset($edition_nids));

  // Check that the newsletter has been saved and sent.
  $this
    ->assertTrue($edition_node->simplenews_issue->subscribers > 0);

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

  // Go to node and verify creation time and token for custom title
  $this
    ->clickLink("Custom title " . $edition_node
    ->id());
  $this
    ->assertEqual($edition_node
    ->getCreatedTime(), $date
    ->getTimestamp());

  // Check sent mails.
  $mails = $this
    ->getMails();
  $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'));
  $url = $node
    ->toUrl('canonical', [
    'absolute' => TRUE,
  ])
    ->toString();
  $this
    ->assertEqual($url, $this
    ->getUrl());

  // Test the tab on a sent newsletter, schedule details should not be shown.
  $title = "newsletter " . $this
    ->randomMachineName(8);
  $edit = array(
    'title[0][value]' => $title,
    'body[0][value]' => $this
      ->randomMachineName(16),
    'simplenews_issue[target_id]' => 'default',
    'status[value]' => TRUE,
  );
  $this
    ->drupalPostForm('node/add/simplenews_issue', $edit, t('Save'));
  $this
    ->assertText($title);
  $node = $this
    ->drupalGetNodeByTitle($title);
  $edit = array();
  $this
    ->drupalPostForm("node/{$node->id()}/simplenews", $edit, t('Send now'));
  $this
    ->assertNoText(t('Scheduled Newsletter'));

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