You are here

public function SimplenewsSubscribeTest::testAdminCreate in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/SimplenewsSubscribeTest.php \Drupal\Tests\simplenews\Functional\SimplenewsSubscribeTest::testAdminCreate()

Tests admin creating a single subscriber.

File

tests/src/Functional/SimplenewsSubscribeTest.php, line 647

Class

SimplenewsSubscribeTest
Un/subscription of anonymous and authenticated users.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testAdminCreate() {
  $admin_user = $this
    ->drupalCreateUser([
    'administer simplenews subscriptions',
  ]);
  $this
    ->drupalLogin($admin_user);
  $newsletter_id = $this
    ->getRandomNewsletter();
  $mail = $this
    ->randomEmail();
  $this
    ->drupalGet('admin/people/simplenews/create');
  $this
    ->assertText('Add subscriber');
  $edit = [
    "subscriptions[{$newsletter_id}]" => TRUE,
    'mail[0][value]' => $mail,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertText(t('Subscriber @mail has been added.', [
    '@mail' => $mail,
  ]));
  $subscriber = Subscriber::loadByMail($mail);
  $subscription = $subscriber
    ->getSubscription($newsletter_id);
  $this
    ->assertEquals(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $subscription->status);

  // Check that an unsubscribe link works without any permissions.
  $this
    ->drupalLogout();
  user_role_revoke_permissions(AccountInterface::ANONYMOUS_ROLE, [
    'subscribe to newsletters',
  ]);
  $node = $this
    ->drupalCreateNode([
    'type' => 'simplenews_issue',
    'simplenews_issue[target_id]' => [
      'target_id' => $newsletter_id,
    ],
  ]);
  \Drupal::service('simplenews.spool_storage')
    ->addIssue($node);
  \Drupal::service('simplenews.mailer')
    ->sendSpool();
  $unsubscribe_url = $this
    ->extractConfirmationLink($this
    ->getMail(0));
  $this
    ->drupalGet($unsubscribe_url);
  $this
    ->assertText('Confirm remove subscription');
  $this
    ->submitForm([], 'Unsubscribe');
  $this
    ->assertText('was unsubscribed from the Default newsletter mailing list.');
}