You are here

function SimplenewsSubscribeTestCase::testSubscribeMultiple in Simplenews 7.2

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

Subscribe to multiple newsletters at the same time.

File

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

Class

SimplenewsSubscribeTestCase

Code

function testSubscribeMultiple() {
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer blocks',
    'administer content types',
    'administer nodes',
    'access administration pages',
    'administer permissions',
    'administer newsletters',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->setAnonymousUserSubscription(TRUE);
  $this
    ->drupalGet('admin/config/services/simplenews');
  for ($i = 0; $i < 5; $i++) {
    $this
      ->clickLink(t('Add newsletter'));
    $edit = array(
      'name' => $this
        ->randomName(),
      'description' => $this
        ->randomString(20),
      'opt_inout' => 'double',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
  }
  $newsletters = simplenews_newsletter_get_all();
  $this
    ->drupalLogout();
  $enable = array_rand($newsletters, 3);
  $mail = $this
    ->randomEmail(8, 'testmail');
  $edit = array(
    'mail' => $mail,
  );
  foreach ($enable as $newsletter_id) {
    $edit['newsletters[' . $newsletter_id . ']'] = TRUE;
  }
  $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[0]['body'];

  // Verify listed changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    $pos = strpos($body, t('Subscribe to @name', array(
      '@name' => $newsletter->name,
    )));
    if (in_array($newsletter_id, $enable)) {
      $this
        ->assertTrue($pos);
    }
    else {
      $this
        ->assertFalse($pos);
    }
  }
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $this
    ->assertRaw(t('Are you sure you want to confirm the following subscription changes for %user?', array(
    '%user' => simplenews_mask_mail($mail),
  )), t('Subscription confirmation found.'));

  // Verify listed changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    if (in_array($newsletter_id, $enable)) {
      $this
        ->assertText(t('Subscribe to @name', array(
        '@name' => $newsletter->name,
      )));
    }
    else {
      $this
        ->assertNoText(t('Subscribe to @name', array(
        '@name' => $newsletter->name,
      )));
    }
  }
  $this
    ->drupalPost($confirm_url, NULL, t('Confirm'));
  $this
    ->assertRaw(t('Subscription changes confirmed for %user.', array(
    '%user' => $mail,
  )), t('Anonymous subscriber added to newsletter'));

  // Verify subscription changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    $is_subscribed = simplenews_user_is_subscribed($mail, $newsletter_id);
    if (in_array($newsletter_id, $enable)) {
      $this
        ->assertTrue($is_subscribed);
    }
    else {
      $this
        ->assertFalse($is_subscribed);
    }
  }

  // Go to the manage page and submit without changes.
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $hash = simplenews_generate_hash($subscriber->mail, 'manage');
  $this
    ->drupalPost('newsletter/subscriptions/' . $subscriber->snid . '/' . REQUEST_TIME . '/' . $hash, array(), t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $mail,
  )));
  $mails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(1, count($mails), t('No confirmation mails have been sent.'));

  // Unsubscribe from two of the three enabled newsletters.
  $disable = array_rand(array_flip($enable), 2);
  $edit = array(
    'mail' => $mail,
  );
  foreach ($disable as $newsletter_id) {
    $edit['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.'), t('Subscription confirmation e-mail sent.'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[1]['body'];

  // Verify listed changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    $pos = strpos($body, t('Unsubscribe from @name', array(
      '@name' => $newsletter->name,
    )));
    if (in_array($newsletter_id, $disable)) {
      $this
        ->assertTrue($pos);
    }
    else {
      $this
        ->assertFalse($pos);
    }
  }
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $this
    ->assertRaw(t('Are you sure you want to confirm the following subscription changes for %user?', array(
    '%user' => simplenews_mask_mail($mail),
  )), t('Subscription confirmation found.'));

  // Verify listed changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    if (in_array($newsletter_id, $disable)) {
      $this
        ->assertText(t('Unsubscribe from @name', array(
        '@name' => $newsletter->name,
      )));
    }
    else {
      $this
        ->assertNoText(t('Unsubscribe from @name', array(
        '@name' => $newsletter->name,
      )));
    }
  }
  $this
    ->drupalPost($confirm_url, NULL, t('Confirm'));
  $this
    ->assertRaw(t('Subscription changes confirmed for %user.', array(
    '%user' => $mail,
  )), t('Anonymous subscriber added to newsletter'));

  // Verify subscription changes.
  entity_get_controller('simplenews_subscriber')
    ->resetCache();
  drupal_static_reset('simplenews_user_is_subscribed');
  $still_enabled = array_diff($enable, $disable);
  foreach ($newsletters as $newsletter_id => $newsletter) {
    $is_subscribed = simplenews_user_is_subscribed($mail, $newsletter_id);
    if (in_array($newsletter_id, $still_enabled)) {
      $this
        ->assertTrue($is_subscribed);
    }
    else {
      $this
        ->assertFalse($is_subscribed);
    }
  }

  // Make sure that a single change results in a non-multi confirmation mail.
  $newsletter_id = reset($disable);
  $edit = array(
    'mail' => $mail,
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Subscribe'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[2]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);

  // Change behavior to always use combined mails.
  variable_set('simplenews_use_combined', 'always');
  $edit = array(
    'mail' => $mail,
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Subscribe'));
  $mails = $this
    ->drupalGetMails();
  $body = $mails[3]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);

  // Change behavior to never, should send two separate mails.
  variable_set('simplenews_use_combined', 'never');
  $edit = array(
    'mail' => $mail,
  );
  foreach ($disable as $newsletter_id) {
    $edit['newsletters[' . $newsletter_id . ']'] = TRUE;
  }
  $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();
  foreach (array(
    4,
    5,
  ) as $id) {
    $body = $mails[$id]['body'];
    $this
      ->extractConfirmationLink($body);
  }

  // Make sure that the /ok suffix works, subscribe from everything.
  variable_del('simplenews_use_combined');
  $edit = array(
    'mail' => $mail,
  );
  foreach (array_keys($newsletters) as $newsletter_id) {
    $edit['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[6]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $this
    ->drupalGet($confirm_url . '/ok');
  $this
    ->assertRaw(t('Subscription changes confirmed for %user.', array(
    '%user' => $mail,
  )), t('Confirmation message displayed.'));

  // Verify subscription changes.
  entity_get_controller('simplenews_subscriber')
    ->resetCache();
  drupal_static_reset('simplenews_user_is_subscribed');
  foreach (array_keys($newsletters) as $newsletter_id) {
    $this
      ->assertFalse(simplenews_user_is_subscribed($mail, $newsletter_id));
  }

  // Call confirmation url after it is allready used.
  // Using direct url.
  $this
    ->drupalGet($confirm_url . '/ok');
  $this
    ->assertNoResponse(404, 'Redirected after calling confirmation url more than once.');
  $this
    ->assertRaw(t('All changes to your subscriptions where already applied. No changes made.'));

  // Using confirmation page.
  $this
    ->drupalGet($confirm_url);
  $this
    ->assertNoResponse(404, 'Redirected after calling confirmation url more than once.');
  $this
    ->assertRaw(t('All changes to your subscriptions where already applied. No changes made.'));

  // Test expired confirmation links.
  $enable = array_rand($newsletters, 3);
  $mail = $this
    ->randomEmail(8, 'testmail');
  $edit = array(
    'mail' => $mail,
  );
  foreach ($enable as $newsletter_id) {
    $edit['newsletters[' . $newsletter_id . ']'] = TRUE;
  }
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Subscribe'));
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $expired_timestamp = REQUEST_TIME - 86401;
  $changes = $subscriber->changes;
  $hash = simplenews_generate_hash($subscriber->mail, 'combined' . serialize($subscriber->changes), $expired_timestamp);
  $url = 'newsletter/confirm/combined/' . $subscriber->snid . '/' . $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[8]['body'];
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);

  // Verify listed changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    $pos = strpos($body, t('Subscribe to @name', array(
      '@name' => $newsletter->name,
    )));
    if (in_array($newsletter_id, $enable)) {
      $this
        ->assertTrue($pos);
    }
    else {
      $this
        ->assertFalse($pos);
    }
  }
  $confirm_url = $this
    ->extractConfirmationLink($body);
  $this
    ->drupalGet($confirm_url);
  $this
    ->assertRaw(t('Are you sure you want to confirm the following subscription changes for %user?', array(
    '%user' => simplenews_mask_mail($mail),
  )), t('Subscription confirmation found.'));

  // Verify listed changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    if (in_array($newsletter_id, $enable)) {
      $this
        ->assertText(t('Subscribe to @name', array(
        '@name' => $newsletter->name,
      )));
    }
    else {
      $this
        ->assertNoText(t('Subscribe to @name', array(
        '@name' => $newsletter->name,
      )));
    }
  }
  $this
    ->drupalPost($confirm_url, NULL, t('Confirm'));
  $this
    ->assertRaw(t('Subscription changes confirmed for %user.', array(
    '%user' => $mail,
  )), t('Anonymous subscriber added to newsletter'));

  // Make sure that old links still work.
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  foreach ($changes as &$action) {
    $action = 'unsubscribe';
  }
  $subscriber->changes = $changes;
  simplenews_subscriber_save($subscriber);
  $url = 'newsletter/confirm/combined/' . 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[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 confirm the following subscription changes for %user?', array(
    '%user' => simplenews_mask_mail($mail),
  )), t('Subscription confirmation found.'));

  // Verify listed changes.
  foreach ($newsletters as $newsletter_id => $newsletter) {
    if (in_array($newsletter_id, $enable)) {
      $this
        ->assertText(t('Unsubscribe from @name', array(
        '@name' => $newsletter->name,
      )));
    }
    else {
      $this
        ->assertNoText(t('Unsubscribe from @name', array(
        '@name' => $newsletter->name,
      )));
    }
  }
}