You are here

public function SimplenewsSendTest::testDelete in Simplenews 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Functional/SimplenewsSendTest.php \Drupal\Tests\simplenews\Functional\SimplenewsSendTest::testDelete()

Create a newsletter, send mails and then delete.

File

tests/src/Functional/SimplenewsSendTest.php, line 493

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testDelete() {

  // Verify that the newsletter settings are shown.
  $this
    ->drupalGet('node/add/simplenews_issue');
  $this
    ->assertText(t('Create Newsletter Issue'));

  // Prevent deleting the mail spool entries automatically.
  $config = $this
    ->config('simplenews.settings');
  $config
    ->set('mail.spool_expire', 1);
  $config
    ->save();
  $edit = [
    'title[0][value]' => $this
      ->randomString(10),
    'simplenews_issue[target_id]' => 'default',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertEqual(1, preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = Node::load($matches[1]);
  $this
    ->clickLink(t('Newsletter'));
  $this
    ->assertText(t('Send'));
  $this
    ->assertText(t('Test'));

  // Verify state.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, $node->simplenews_issue->status, t('Newsletter not sent yet.'));

  // Send now.
  $this
    ->drupalPostForm(NULL, [], t('Send now'));

  // Verify state.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_PENDING, $node->simplenews_issue->status, t('Newsletter sending pending.'));
  $spooled = \Drupal::database()
    ->query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', [
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ])
    ->fetchField();
  $this
    ->assertEqual(5, $spooled, t('5 mails remaining in spool.'));

  // Verify that deleting isn't possible right now.
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertNoText(t('Delete'));

  // Send mails.
  simplenews_cron();

  // Verify state.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_READY, $node->simplenews_issue->status, t('Newsletter sending finished'));
  $spooled = \Drupal::database()
    ->query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', [
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ])
    ->fetchField();
  $this
    ->assertEqual(5, $spooled, t('Mails are kept in simplenews_mail_spool after being sent.'));

  // Verify mails.
  $mails = $this
    ->getMails();
  $this
    ->assertEqual(5, count($mails), t('All mails were sent.'));
  foreach ($mails as $mail) {
    $this
      ->assertEqual($mail['subject'], '[Default newsletter] ' . $edit['title[0][value]'], t('Mail has correct subject'));
    $this
      ->assertTrue(isset($this->subscribers[$mail['to']]), t('Found valid recipient'));
    unset($this->subscribers[$mail['to']]);
  }
  $this
    ->assertEqual(0, count($this->subscribers), t('all subscribers have received a mail'));

  // Update timestamp to simulate pending lock expiration.
  \Drupal::database()
    ->update('simplenews_mail_spool')
    ->fields([
    'timestamp' => REQUEST_TIME - $this
      ->config('simplenews.settings')
      ->get('mail.spool_progress_expiration') - 1,
  ])
    ->execute();

  // Verify that kept mail spool rows are not re-sent.
  simplenews_cron();
  \Drupal::service('simplenews.spool_storage')
    ->getMails();
  $mails = $this
    ->getMails();
  $this
    ->assertEqual(5, count($mails), t('No additional mails have been sent.'));

  // Now delete.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->clickLink(t('Delete'));
  $this
    ->drupalPostForm(NULL, [], t('Delete'));

  // Verify.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $this
    ->assertEmpty(Node::load($node
    ->id()));
  $spooled = \Drupal::database()
    ->query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', [
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ])
    ->fetchField();
  $this
    ->assertEqual(0, $spooled, t('No mails remaining in spool.'));
}