You are here

public function SimplenewsSubscribeTest::testSubscribeAnonymousSingle in Simplenews 8.2

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

Test anonymous subscription with single opt in.

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

File

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

Class

SimplenewsSubscribeTest
Un/subscription of anonymous and authenticated users.

Namespace

Drupal\Tests\simplenews\Functional

Code

public 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([
    '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 = [
    '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 = [
    'newsletters' => [
      $newsletter_id,
    ],
    'message' => $this
      ->randomMachineName(4),
  ];
  $this
    ->setupSubscriptionBlock($block_settings);

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

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