You are here

function SimplenewsSubscribeTestCase::testSubscribeAnonymous in Simplenews 6.2

Same name and namespace in other branches
  1. 7.2 tests/simplenews.test \SimplenewsSubscribeTestCase::testSubscribeAnonymous()
  2. 7 tests/simplenews.test \SimplenewsSubscribeTestCase::testSubscribeAnonymous()

testSubscribeAnonymous

Steps performed: 0. Preparation 1. Subscribe anonymous via block 2. Subscribe anonymous via subscription page 3. Subscribe anonymous via multi block

File

tests/simplenews.test, line 187
Simplenews test functions.

Class

SimplenewsSubscribeTestCase

Code

function testSubscribeAnonymous() {

  // Include simplenews.subscription.inc for simplenews_mask_mail().
  module_load_include('inc', 'simplenews', 'includes/simplenews.subscription');

  // 0. Preparation
  // Login admin
  // 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',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->setAnonymousUserSubscription(TRUE);

  // Setup subscription block with subscription form.
  $block_settings = array(
    'message' => $this
      ->randomName(4),
    'form' => '1',
    'link to previous' => FALSE,
    'previous issues' => FALSE,
    'rss feed' => TRUE,
  );
  $tid = $this
    ->getRandomNewsletter();
  $this
    ->setupSubscriptionBlock($tid, $block_settings);
  $this
    ->drupalLogout();

  //file_put_contents('output.html', $this->drupalGetContent());

  // 1. Subscribe anonymous via block
  // Subscribe + submit
  // Assert confirmation message
  // Assert outgoing email
  //
  // Confirm using mail link
  // Confirm using mail link
  // Assert confirmation message
  $mail = $this
    ->randomEmail(8, 'testmail');
  $edit = array(
    'mail' => $mail,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'), t('Subscription confirmation e-mail sent.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[0]['body'];
  $pattern = '@newsletter/confirm/add/[0-9,a-f]+t[0-9]+@';
  preg_match($pattern, $body, $match);
  $found = preg_match($pattern, $body, $match);
  $confirm_url = $match[0];
  $this
    ->assertTrue($found, t('Confirmation URL found: @url', array(
    '@url' => $confirm_url,
  )));
  $this
    ->drupalGet($confirm_url);
  $newsletter = taxonomy_get_term($tid);
  $this
    ->assertRaw(t('Are you sure you want to add %user to the %newsletter mailing list?', array(
    '%user' => simplenews_mask_mail($mail),
    '%newsletter' => $newsletter->name,
  )), t('Subscription confirmation found.'));
  $this
    ->drupalPost(NULL, array(), t('Subscribe'));
  $this
    ->assertRaw(t('%user was added to the %newsletter mailing list.', array(
    '%user' => $mail,
    '%newsletter' => $newsletter->name,
  )), t('Anonymous subscriber added to newsletter'));

  // 2. Subscribe anonymous via subscription page
  // Subscribe + submit
  // Assert confirmation message
  // Assert outgoing email
  //
  // Confirm using mail link
  // Confirm using mail link
  // Assert confirmation message
  $mail = $this
    ->randomEmail(8, 'testmail');
  $edit = array(
    "newsletters[{$tid}]" => '1',
    'mail' => $mail,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'), t('Subscription confirmation e-mail sent.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[1]['body'];
  $pattern = '@newsletter/confirm/add/[0-9,a-f]+t[0-9]+@';
  preg_match($pattern, $body, $match);
  $found = preg_match($pattern, $body, $match);
  $confirm_url = $match[0];
  $this
    ->assertTrue($found, t('Confirmation URL found: @url', array(
    '@url' => $confirm_url,
  )));
  $this
    ->drupalGet($confirm_url);
  $newsletter = taxonomy_get_term($tid);
  $this
    ->assertRaw(t('Are you sure you want to add %user to the %newsletter mailing list?', array(
    '%user' => simplenews_mask_mail($mail),
    '%newsletter' => $newsletter->name,
  )), t('Subscription confirmation found.'));
  $this
    ->drupalPost($confirm_url, NULL, t('Subscribe'));
  $this
    ->assertRaw(t('%user was added to the %newsletter mailing list.', array(
    '%user' => $mail,
    '%newsletter' => $newsletter->name,
  )), t('Anonymous subscriber added to newsletter'));

  // 3. Subscribe anonymous via multi block
  $this
    ->drupalLogin($admin_user);

  // Enable the multi-sign up block.
  $this
    ->setupSubscriptionBlock(0);

  // Disable the category block.
  $edit = array(
    'simplenews_' . $tid . '[region]' => -1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save blocks'));
  $this
    ->drupalLogout();

  // Try to submit multi-signup form without selecting a category.
  $mail = $this
    ->randomEmail(8, 'testmail');
  $edit = array(
    'mail' => $mail,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You must select at least one newsletter.'));

  // Now fill out the form and try again. The e-mail should still be listed.
  $edit = array(
    'newsletters[' . $tid . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[2]['body'];
  $pattern = '@newsletter/confirm/add/[0-9,a-f]+t[0-9]+@';
  preg_match($pattern, $body, $match);
  $found = preg_match($pattern, $body, $match);
  $confirm_url = $match[0];
  $this
    ->assertTrue($found, t('Confirmation URL found: @url', array(
    '@url' => $confirm_url,
  )));
  $this
    ->drupalGet($confirm_url);
  $newsletter = taxonomy_get_term($tid);
  $this
    ->assertRaw(t('Are you sure you want to add %user to the %newsletter mailing list?', array(
    '%user' => simplenews_mask_mail($mail),
    '%newsletter' => $newsletter->name,
  )), t('Subscription confirmation found.'));
  $this
    ->drupalPost($confirm_url, NULL, t('Subscribe'));
  $this
    ->assertRaw(t('%user was added to the %newsletter mailing list.', array(
    '%user' => $mail,
    '%newsletter' => $newsletter->name,
  )), t('Anonymous subscriber added to newsletter'));

  // Now the same with the newsletter/subscriptions page.
  $mail = $this
    ->randomEmail(8, 'testmail');
  $edit = array(
    'mail' => $mail,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Subscribe'));
  $this
    ->assertText(t('You must select at least one newsletter.'));

  // Now fill out the form and try again.
  $edit = array(
    'newsletters[' . $tid . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[3]['body'];
  $pattern = '@newsletter/confirm/add/[0-9,a-f]+t[0-9]+@';
  preg_match($pattern, $body, $match);
  $found = preg_match($pattern, $body, $match);
  $confirm_url = $match[0];
  $this
    ->assertTrue($found, t('Confirmation URL found: @url', array(
    '@url' => $confirm_url,
  )));
  $this
    ->drupalGet($confirm_url);
  $newsletter = taxonomy_get_term($tid);
  $this
    ->assertRaw(t('Are you sure you want to add %user to the %newsletter mailing list?', array(
    '%user' => simplenews_mask_mail($mail),
    '%newsletter' => $newsletter->name,
  )), t('Subscription confirmation found.'));
  $this
    ->drupalPost($confirm_url, NULL, t('Subscribe'));
  $this
    ->assertRaw(t('%user was added to the %newsletter mailing list.', array(
    '%user' => $mail,
    '%newsletter' => $newsletter->name,
  )), t('Anonymous subscriber added to newsletter'));
}