You are here

public function SimplenewsSendTest::testUpdateNewsletter in Simplenews 8.2

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

Test newsletter update.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

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

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testUpdateNewsletter() {

  // Create a second newsletter.
  $this
    ->drupalGet('admin/config/services/simplenews');
  $this
    ->clickLink(t('Add newsletter'));
  $edit = [
    'name' => $this
      ->randomString(10),
    'id' => strtolower($this
      ->randomMachineName(10)),
    'description' => $this
      ->randomString(20),
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText(t('Newsletter @name has been added', [
    '@name' => $edit['name'],
  ]));
  $this
    ->drupalGet('node/add/simplenews_issue');
  $this
    ->assertText(t('Create Newsletter Issue'));
  $first_newsletter_id = $this
    ->getRandomNewsletter();
  $edit = [
    'title[0][value]' => $this
      ->randomString(10),
    'simplenews_issue[target_id]' => $first_newsletter_id,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertEqual(1, preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created.');

  // Verify newsletter.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($matches[1]);
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, $node->simplenews_issue->status, t('Newsletter sending not started.'));
  $this
    ->assertEqual($first_newsletter_id, $node->simplenews_issue->target_id);
  do {
    $second_newsletter_id = $this
      ->getRandomNewsletter();
  } while ($first_newsletter_id == $second_newsletter_id);
  $this
    ->clickLink(t('Edit'));
  $update = [
    'simplenews_issue[target_id]' => $second_newsletter_id,
  ];
  $this
    ->drupalPostForm(NULL, $update, t('Save'));

  // Verify newsletter.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, $node->simplenews_issue->status, t('Newsletter sending not started.'));
  $this
    ->assertEqual($second_newsletter_id, $node->simplenews_issue->target_id, t('Newsletter has newsletter_id @id.', [
    '@id' => $second_newsletter_id,
  ]));
}