function SimplenewsSubscribeTestCase::testSubscribeAnonymous in Simplenews 7
Same name and namespace in other branches
- 6.2 tests/simplenews.test \SimplenewsSubscribeTestCase::testSubscribeAnonymous()
- 7.2 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 447 - Simplenews test functions.
Class
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',
'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 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 tid = :tid', array(
':snid' => $subscriber->snid,
':tid' => $tid,
))
->fetchObject();
$this
->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED, $subscription->status, t('Subscription is unconfirmed'));
$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_term_load($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'));
// 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.
$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[{$tid}]" => '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'];
$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_term_load($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(
'blocks[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 e-mail 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_term_load($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'));
// Try to subscribe again, this should not re-set the status to unconfirmed.
$edit = array(
'mail' => $mail,
'newsletters[' . $tid . ']' => 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[$tid]->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[' . $tid . ']' => 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'];
$pattern = '@newsletter/confirm/add/[0-9,a-f]+t[0-9]+@';
$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_term_load($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'));
// 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[' . $tid . ']' => 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);
$pattern = '@newsletter/confirm/remove/[0-9,a-f]+t[0-9]+@';
$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_term_load($tid);
$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, $subscriber->snid, $tid);
$this
->drupalGet('newsletter/subscriptions/' . $hash);
$this
->assertText(t('Subscriptions for @mail', array(
'@mail' => $mail,
)));
$edit = array(
'newsletters[' . $tid . ']' => TRUE,
);
$this
->drupalPost(NULL, $edit, t('Update'));
// Make sure the user is not yet subscribed.
$subscriber = simplenews_subscriber_load_by_mail($mail);
$this
->assertTrue(empty($subscriber->tids));
$this
->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED, $subscriber->newsletter_subscription[$tid]->status);
$mails = $this
->drupalGetMails();
$body = $mails[7]['body'];
$pattern = '@newsletter/confirm/add/[0-9,a-f]+t[0-9]+@';
$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_term_load($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'));
// Make sure the subscription is confirmed now.
$subscriber = simplenews_subscriber_load_by_mail($mail);
$this
->assertTrue(isset($subscriber->tids[$tid]));
$this
->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $subscriber->newsletter_subscription[$tid]->status);
// Attemt to fetch the page using an invalid hash format.
$this
->drupalGet('newsletter/subscriptions/' . strrev($hash));
$this
->assertResponse(404);
// Attemt to fetch the page using a wrong hash but correct format.
$this
->drupalGet('newsletter/subscriptions/' . simplenews_generate_hash($subscriber->mail . 'a', $subscriber->snid, $tid));
$this
->assertResponse(404);
// Attempt to unsubscribe a non-existing subscriber.
$mail = $this
->randomEmail();
$edit = array(
'mail' => $mail,
'newsletters[' . $tid . ']' => 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[8]['body'];
// Remove line breaks from body in case the string is split.
$body = str_replace("\n", ' ', $body);
debug($body);
$this
->assertTrue(strpos($body, 'is not subscribed to this mailing list') !== FALSE);
}