View source
<?php
namespace Drupal\Tests\simplenews_scheduler\Functional;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\node\Entity\Node;
class SimplenewsSchedulerNodeCreationTest extends SimplenewsSchedulerTestBase {
use AssertMailTrait;
protected static $modules = [
'block',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$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->mail = 'test@example.org';
\Drupal::service('simplenews.subscription_manager')
->subscribe($this->mail, 'default', FALSE, 'test');
}
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);
$this
->drupalGet("node/{$node->id()}/editions");
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet("node/" . $node
->id() . "/simplenews");
$this
->assertText(t('Enable scheduled newsletter'));
$edit = array();
$edit['enable_scheduler'] = TRUE;
$edit["interval"] = "hour";
$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'));
$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]");
$this
->drupalGet("node/{$node->id()}/editions");
$this
->assertText(t("No scheduled newsletter editions have been sent."));
\Drupal::service('cron')
->run();
$edition_nids = \Drupal::entityQuery('node')
->sort('nid', 'DESC')
->execute();
$edition_node = Node::load(reset($edition_nids));
$this
->assertTrue($edition_node->simplenews_issue->subscribers > 0);
$this
->drupalGet("node/{$node->id()}/editions");
$this
->assertText("Custom title " . $edition_node
->id());
$this
->assertNoText(t("No scheduled newsletter editions have been sent."));
$this
->clickLink("Custom title " . $edition_node
->id());
$this
->assertEqual($edition_node
->getCreatedTime(), $date
->getTimestamp());
$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());
$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'));
$mails = $this
->getMails();
$this
->assertEqual(1, count($mails), t('Newsletter mail has been sent.'));
}
}