You are here

function SimpleNewsAdministrationTestCase::testSubscriptionManagement in Simplenews 6.2

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

Test newsletter subscription management.

Steps performed:

File

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

Class

SimpleNewsAdministrationTestCase
@todo: Newsletter node create, send draft, send final

Code

function testSubscriptionManagement() {
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer newsletters',
    'administer simplenews settings',
    'administer simplenews subscriptions',
    'administer taxonomy',
  ));
  $this
    ->drupalLogin($admin_user);

  // Create a second category.
  $edit = array(
    'name' => $name = $this
      ->randomName(),
  );
  $this
    ->drupalPost('admin/content/simplenews/types/add', $edit, t('Save'));

  // Add a number of users to each category separately and then add another
  // bunch to both.
  $subscribers = array();

  // Can't use the api function due to taxonomy static caches, assume id's.

  //$categories =simplenews_get_newsletters(variable_get('simplenews_vid', ''), TRUE, TRUE);
  $categories = array(
    1,
    2,
  );
  $groups = array();
  foreach ($categories as $tid) {
    $groups[$tid] = array(
      $tid,
    );
  }
  $groups['all'] = array_keys($groups);
  $subscribers_flat = array();
  foreach ($groups as $key => $group) {
    for ($i = 0; $i < 5; $i++) {
      $mail = $this
        ->randomEmail();
      $subscribers[$key][$mail] = $mail;
      $subscribers_flat[$mail] = $mail;
    }
  }

  // Create a user and assign him one of the mail addresses of the all group.
  $user = $this
    ->drupalCreateUser(array(
    'subscribe to newsletters',
  ));

  // Make sure that user_save() does not update the user object, as it will
  // override the pass_raw property which we'll need to log this user in
  // later on.
  $user_mail = current($subscribers['all']);
  user_save(clone $user, array(
    'mail' => $user_mail,
  ));
  $delimiters = array(
    ',',
    ' ',
    "\n",
  );
  $this
    ->drupalGet('admin/content/simplenews/users');
  $i = 0;
  foreach ($groups as $key => $group) {
    $this
      ->clickLink(t('Mass subscribe'));
    $edit = array(
      // Implode with a different, supported delimiter for each group.
      'emails' => implode($delimiters[$i++], $subscribers[$key]),
    );
    foreach ($group as $tid) {
      $edit['newsletters[' . $tid . ']'] = TRUE;
    }
    $this
      ->drupalPost(NULL, $edit, t('Subscribe'));
  }

  // 6.x-2.x does not redirect back to the list.
  $this
    ->clickLink('List');

  // The user to which the mail was assigned should be listed too.
  $this
    ->assertText($user->name);

  // Verify that all addresses are displayed in the table.
  $mail_addresses = $this
    ->xpath('//tr/td[2]');
  $this
    ->assertEqual(15, count($mail_addresses));
  foreach ($mail_addresses as $mail_address) {
    $mail_address = (string) $mail_address;
    $this
      ->assertTrue(isset($subscribers_flat[$mail_address]));
    unset($subscribers_flat[$mail_address]);
  }

  // All entries of the array should be removed by now.
  $this
    ->assertTrue(empty($subscribers_flat));

  // Limit list to subscribers of the first category only.
  reset($groups);
  $first = key($groups);

  // Build a flat list of the subscribers of this list.
  $subscribers_flat = array_merge($subscribers[$first], $subscribers['all']);
  $edit = array(
    'newsletter' => 'tid-' . $first,
  );
  $this
    ->drupalPost(NULL, $edit, t('Filter'));

  // Verify that all addresses are displayed in the table.
  $mail_addresses = $this
    ->xpath('//tr/td[2]');
  $this
    ->assertEqual(10, count($mail_addresses));
  foreach ($mail_addresses as $mail_address) {
    $mail_address = (string) $mail_address;
    $this
      ->assertTrue(isset($subscribers_flat[$mail_address]));
    unset($subscribers_flat[$mail_address]);
  }

  // All entries of the array should be removed by now.
  $this
    ->assertTrue(empty($subscribers_flat));

  // Filter a single mail address, the one assigned to a user.
  $edit = array(
    'email' => substr(current($subscribers['all']), 0, 4),
  );
  $this
    ->drupalPost(NULL, $edit, t('Filter'));
  $rows = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(1, count($rows));
  $this
    ->assertEqual(current($subscribers['all']), (string) $rows[0]->td[1]);
  $this
    ->assertEqual($user->name, (string) $rows[0]->td[2]->a);

  // Reset the filter.
  $this
    ->drupalPost(NULL, array(), t('Reset'));

  // Test mass-unsubscribe, unsubscribe one from the first group and one from
  // the all group, but only from the first newsletter category.
  $first_mail = array_rand($subscribers[$first]);
  $all_mail = array_rand($subscribers['all']);
  unset($subscribers[$first][$first_mail]);
  $edit = array(
    'emails' => $first_mail . ', ' . $all_mail,
    "newsletters[{$first}]" => TRUE,
  );
  $this
    ->clickLink(t('Mass unsubscribe'));
  $this
    ->drupalPost(NULL, $edit, t('Unsubscribe'));

  // The all mail is still displayed because it's still subscribed to the
  // second category. Reload the page to get rid of the confirmation
  // message.
  $this
    ->drupalGet('admin/content/simplenews/users');

  // @todo disabled.

  //$this->assertNoText($first_mail);
  $this
    ->assertText($all_mail);

  // Limit to first category, the all mail shouldn't be shown anymore.
  $edit = array(
    'newsletter' => 'tid-' . $first,
  );
  $this
    ->drupalPost(NULL, $edit, t('Filter'));
  $this
    ->assertNoText($first_mail);
  $this
    ->assertNoText($all_mail);

  // Check exporting.
  $this
    ->clickLink(t('Export'));
  $edit = array(
    'states[active]' => TRUE,
    'newsletters[' . $first . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Export'));
  $export_field = $this
    ->xpath($this
    ->constructFieldXpath('name', 'emails'));
  $exported_mails = (string) $export_field[0];
  foreach ($subscribers[$first] as $mail) {
    $this
      ->assertTrue(strpos($exported_mails, $mail) !== FALSE, t('Mail address exported correctly.'));
  }
  foreach ($subscribers['all'] as $mail) {
    if ($mail != $all_mail) {
      $this
        ->assertTrue(strpos($exported_mails, $mail) !== FALSE, t('Mail address exported correctly.'));
    }
    else {
      $this
        ->assertFALSE(strpos($exported_mails, $mail) !== FALSE, t('Unsubscribed mail address not exported.'));
    }
  }

  // Delete newsletter category.
  $this
    ->drupalPost('admin/content/taxonomy/edit/term/' . $first, array(), t('Delete'));
  $this
    ->drupalPost(NULL, array(), t('Delete'));
  $this
    ->assertText(t('All subscriptions to newsletter Drupal newsletter have been deleted.'));

  // Verify that all related data has been deleted.
  $this
    ->assertFalse(db_result(db_query("SELECT * FROM {blocks} WHERE module = '%s' AND delta = %d", 'simplenews', $first)));

  // Verify that all subscriptions of that category have been removed.
  $this
    ->drupalGet('admin/content/simplenews/users');
  foreach ($subscribers[$first] as $mail) {
    $this
      ->assertNoText($mail);
  }

  // @todo Test Admin subscriber edit category
  // @todo Test Admin subscriber edit preferred language $subscription->language
  // @todo Test Admin subscriber edit Active / Inactive
  // Register a subscriber with an insecure e-mail address through the API
  // and make sure the address is correctly encoded.
  $xss_mail = "<script>alert('XSS');</script>";
  simplenews_subscribe_user($xss_mail, $this
    ->getRandomNewsletter(), FALSE);
  $this
    ->drupalGet('admin/content/simplenews/users');
  $this
    ->assertNoRaw($xss_mail);
  $this
    ->assertRaw(check_plain($xss_mail));
  $xss_subscriber = simplenews_get_subscription(_simplenews_user_load($xss_mail));
  $this
    ->drupalGet('admin/content/simplenews/users/edit/' . $xss_subscriber->snid);
  $this
    ->assertNoRaw($xss_mail);
  $this
    ->assertRaw(check_plain($xss_mail));
}