You are here

function SimplenewsSubscribeTestCase::testSubscribeAnonymous in Simplenews 7.2

Same name and namespace in other branches
  1. 6.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 524
Simplenews test functions.

Class

SimplenewsSubscribeTestCase

Code

function testSubscribeAnonymous() {

  // 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',
    'previous issues' => FALSE,
  );
  $newsletter_id = $this
    ->getRandomNewsletter();
  $this
    ->setupSubscriptionBlock($newsletter_id, $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 e-mail shortly containing further instructions on how to complete your subscription.'), t('Subscription confirmation e-mail sent.'));
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $subscription = db_query('SELECT * FROM {simplenews_subscription} WHERE snid = :snid AND newsletter_id = :newsletter_id', array(
    ':snid' => $subscriber->snid,
    ':newsletter_id' => $newsletter_id,
  ))
    ->fetchObject();
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED, $subscription->status, t('Subscription is unconfirmed'));
  $mails = $this
    ->drupalGetMails();
  $confirm_url = $this
    ->extractConfirmationLink($mails[0]['body']);
  $body = $mails[0]['body'];
  $this
    ->drupalGet($confirm_url);
  $newsletter = simplenews_newsletter_load($newsletter_id);
  $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'));

  // Test that it is possible to register with a mail address that is already
  // a subscriber.
  variable_set('user_register', 1);
  variable_set('user_email_verification', FALSE);
  $edit = array(
    'name' => $this
      ->randomName(),
    'mail' => $mail,
    'pass[pass1]' => $pass = $this
      ->randomName(),
    'pass[pass2]' => $pass,
  );
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));

  // Verify confirmation messages.
  $this
    ->assertText(t('Registration successful. You are now logged in.'));

  // Verify that the subscriber has been updated and references to the correct
  // user.
  entity_get_controller('simplenews_subscriber')
    ->resetCache();
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $account = user_load_by_mail($mail);
  $this
    ->assertEqual($subscriber->uid, $account->uid);
  $this
    ->assertEqual($account->name, $edit['name']);
  $this
    ->drupalLogout();

  // 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[{$newsletter_id}]" => '1',
    'mail' => $mail,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'), t('Subscription confirmation e-mail sent.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[2]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $newsletter = simplenews_newsletter_load($newsletter_id);
  $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 newsletter block.
  $edit = array(
    'blocks[simplenews_' . $newsletter_id . '][region]' => -1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save blocks'));
  $this
    ->drupalLogout();

  // Try to submit multi-signup form without selecting a newsletter.
  $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[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[3]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $newsletter = simplenews_newsletter_load($newsletter_id);
  $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'));

  // Try to subscribe again, this should not re-set the status to unconfirmed.
  $edit = array(
    'mail' => $mail,
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'));
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $subscriber->newsletter_subscription[$newsletter_id]->status);

  // 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[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[5]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $newsletter = simplenews_newsletter_load($newsletter_id);
  $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'));

  // Test unsubscribe on newsletter/subscriptions page.
  $edit = array(
    'mail' => $mail,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Unsubscribe'));
  $this
    ->assertText(t('You must select at least one newsletter.'));

  // Now fill out the form and try again.
  $edit = array(
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Unsubscribe'));
  $this
    ->assertText(t('You will receive a confirmation e-mail shortly containing further instructions on how to cancel your subscription.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[6]['body'];
  $this
    ->assertTrue(strpos($body, t('We have received a request to remove the @mail', array(
    '@mail' => $mail,
  ))) === 0);
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $newsletter = simplenews_newsletter_load($newsletter_id);
  $this
    ->assertRaw(t('Are you sure you want to remove %user from the %newsletter mailing list?', array(
    '%user' => simplenews_mask_mail($mail),
    '%newsletter' => $newsletter->name,
  )), t('Subscription confirmation found.'));
  $this
    ->drupalPost($confirm_url, NULL, t('Unsubscribe'));
  $this
    ->assertRaw(t('%user was unsubscribed from the %newsletter mailing list.', array(
    '%user' => $mail,
    '%newsletter' => $newsletter->name,
  )), t('Anonymous subscriber remved from newsletter'));

  // Visit the newsletter/subscriptions page with the hash.
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $hash = simplenews_generate_hash($subscriber->mail, 'manage');
  $this
    ->drupalGet('newsletter/subscriptions/' . $subscriber->snid . '/' . REQUEST_TIME . '/' . $hash);
  $this
    ->assertText(t('Subscriptions for @mail', array(
    '@mail' => $mail,
  )));
  $edit = array(
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $mail,
  )));

  // Make sure the subscription is confirmed.
  entity_get_controller('simplenews_subscriber')
    ->resetCache();
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $this
    ->assertTrue(isset($subscriber->newsletter_ids[$newsletter_id]));
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $subscriber->newsletter_subscription[$newsletter_id]->status);

  // Attemt to fetch the page using a wrong hash but correct format.
  $hash = simplenews_generate_hash($subscriber->mail . 'a', 'manage');
  $this
    ->drupalGet('newsletter/subscriptions/' . $subscriber->snid . '/' . REQUEST_TIME . '/' . $hash);
  $this
    ->assertResponse(404);

  // Attempt to unsubscribe a non-existing subscriber.
  $mail = $this
    ->randomEmail();
  $edit = array(
    'mail' => $mail,
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Unsubscribe'));
  $this
    ->assertText(t('You will receive a confirmation e-mail shortly containing further instructions on how to cancel your subscription.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[7]['body'];

  // Remove line breaks from body in case the string is split.
  $body = str_replace("\n", ' ', $body);
  $this
    ->assertTrue(strpos($body, 'is not subscribed to this mailing list') !== FALSE);

  // Test expired confirmation links.
  $edit = array(
    'mail' => $mail,
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Subscribe'));
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $expired_timestamp = REQUEST_TIME - 86401;
  $hash = simplenews_generate_hash($subscriber->mail, 'add', $expired_timestamp);
  $url = 'newsletter/confirm/add/' . $subscriber->snid . '/' . $newsletter_id . '/' . $expired_timestamp . '/' . $hash;
  $this
    ->drupalGet($url);
  $this
    ->assertText(t('This link has expired.'));
  $this
    ->drupalPost(NULL, array(), t('Request new confirmation mail'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[9]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $newsletter = simplenews_newsletter_load($newsletter_id);
  $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'));

  // Make sure the subscription is confirmed now.
  entity_get_controller('simplenews_subscriber')
    ->resetCache();
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $this
    ->assertTrue(isset($subscriber->newsletter_ids[$newsletter_id]));
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $subscriber->newsletter_subscription[$newsletter_id]->status);

  // Make sure that old links still work.
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $url = 'newsletter/confirm/remove/' . simplenews_generate_old_hash($mail, $subscriber->snid, $newsletter_id);
  $this
    ->drupalGet($url);
  $this
    ->assertText(t('This link has expired.'));
  $this
    ->drupalPost(NULL, array(), t('Request new confirmation mail'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[10]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $newsletter = simplenews_newsletter_load($newsletter_id);
  $this
    ->assertRaw(t('Are you sure you want to remove %user from the %newsletter mailing list?', array(
    '%user' => simplenews_mask_mail($mail),
    '%newsletter' => $newsletter->name,
  )), t('Subscription confirmation found.'));
  $this
    ->drupalPost($confirm_url, NULL, t('Unsubscribe'));
  $this
    ->assertRaw(t('%user was unsubscribed from the %newsletter mailing list.', array(
    '%user' => $mail,
    '%newsletter' => $newsletter->name,
  )), t('Anonymous subscriber removed from newsletter'));

  // Make sure the subscription is confirmed now.
  entity_get_controller('simplenews_subscriber')
    ->resetCache();
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $this
    ->assertFalse(isset($subscriber->newsletter_ids[$newsletter_id]));
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, $subscriber->newsletter_subscription[$newsletter_id]->status);
}