You are here

function SimplenewsSubscribeTest::testSubscribeAnonymousSingle in Simplenews 8

Test anonymous subscription with single opt in.

Steps performed: 0. Preparation 1. Subscribe anonymous via block

File

src/Tests/SimplenewsSubscribeTest.php, line 638
Simplenews subscribe test functions.

Class

SimplenewsSubscribeTest
(un)subscription of anonymous and authenticated users.

Namespace

Drupal\simplenews\Tests

Code

function testSubscribeAnonymousSingle() {

  // 0. Preparation
  // Login admin
  // Create single opt in newsletter.
  // Set permission for anonymous to subscribe
  // Enable newsletter block
  // Logout admin
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer blocks',
    'administer content types',
    'administer nodes',
    'access administration pages',
    'administer permissions',
    'administer newsletters',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('admin/config/services/simplenews');
  $this
    ->clickLink(t('Add newsletter'));
  $name = $this
    ->randomMachineName();
  $edit = array(
    'name' => $name,
    'id' => strtolower($name),
    'description' => $this
      ->randomString(20),
    'opt_inout' => 'single',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->drupalLogout();
  $newsletter_id = $edit['id'];

  // Setup subscription block with subscription form.
  $block_settings = array(
    'newsletters' => array(
      $newsletter_id,
    ),
    'message' => $this
      ->randomMachineName(4),
  );
  $block = $this
    ->setupSubscriptionBlock($block_settings);

  // 1. Subscribe anonymous via block
  // Subscribe + submit
  // Assert confirmation message
  // Verify subscription state.
  $mail = $this
    ->randomEmail(8);
  $edit = array(
    'mail[0][value]' => $mail,
  );
  $this
    ->drupalPostForm('', $edit, t('Subscribe'));
  $this
    ->assertText(t('You have been subscribed.'), t('Anonymous subscriber added to newsletter'));
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $subscription = $subscriber
    ->getSubscription($newsletter_id);
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $subscription->status);

  // Unsubscribe again.
  $edit = array(
    'mail[0][value]' => $mail,
    'subscriptions[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPostForm('newsletter/subscriptions', $edit, t('Unsubscribe'));
  \Drupal::entityTypeManager()
    ->getStorage('simplenews_subscriber')
    ->resetCache();
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $subscription = $subscriber
    ->getSubscription($newsletter_id);
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, $subscription->status);
}