You are here

function SimpleNewsAdministrationTestCase::testCategorySettings in Simplenews 6.2

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

Test various combinations of newsletter category settings.

File

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

Class

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

Code

function testCategorySettings() {

  // Allow registration of new accounts without approval.
  variable_set('user_register', 1);
  variable_set('user_email_verification', FALSE);

  // Allow authenticated users to subscribe.
  $this
    ->setAuthenticatedUserSubscription(TRUE);
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer blocks',
    'administer content types',
    'administer nodes',
    'access administration pages',
    'administer permissions',
    'administer newsletters',
    'administer simplenews subscriptions',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('admin/content/simplenews/types');

  // Create a category for all possible setting combinations.
  $new_account = array(
    'none',
    'off',
    'on',
    'silent',
  );
  $opt_inout = array(
    'hidden',
    'single',
    'double',
  );
  foreach ($new_account as $new_account_setting) {
    foreach ($opt_inout as $opt_inout_setting) {
      $this
        ->clickLink(t('Add newsletter'));
      $edit = array(
        'name' => implode('-', array(
          $new_account_setting,
          $opt_inout_setting,
        )),
        'description' => $this
          ->randomString(20),
        'simplenews_new_account_' => $new_account_setting,
        'simplenews_opt_inout_' => $opt_inout_setting,
        'simplenews_from_name_' => $this
          ->randomName(),
        'simplenews_from_address_' => $this
          ->randomEmail(),
      );
      $this
        ->drupalPost(NULL, $edit, t('Save'));
    }
  }
  $categories = array();
  $result = db_query('SELECT * FROM {term_data} WHERE vid = %d', variable_get('simplenews_vid', ''));
  while ($category = db_fetch_object($result)) {
    $categories[$category->tid] = $category;
  }

  // Check block settings.
  $this
    ->drupalGet('admin/build/block');
  foreach ($categories as $category) {
    if (strpos($category->name, '-') === FALSE) {
      continue;
    }
    $opt_inout = variable_get('simplenews_opt_inout_' . $category->tid, 'double');
    if ($opt_inout != 'hidden') {
      $this
        ->assertField('simplenews_' . $category->tid . '[region]', t('Block is displayed for category'));
    }
    else {
      $this
        ->assertNoField('simplenews_' . $category->tid . '[region]', t('Block is not displayed for category'));
    }
  }

  // Check registration form.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('user/register');
  foreach ($categories as $category) {
    if (strpos($category->name, '-') === FALSE) {
      continue;
    }

    // Explicitly subscribe to the off-double newsletter.
    if ($category->name == 'off-double') {
      $off_double_tid = $category->tid;
    }
    list($new_account_setting, $opt_inout_setting) = explode('-', $category->name);
    $new_account = variable_get('simplenews_new_account_' . $category->tid, 'on');
    $opt_inout = variable_get('simplenews_opt_inout_' . $category->tid, 'double');
    if ($new_account == 'on' && $opt_inout != 'hidden') {
      $this
        ->assertFieldChecked('edit-newsletters-' . $category->tid);
    }
    else {
      if ($new_account == 'off' && $opt_inout != 'hidden') {
        $this
          ->assertNoFieldChecked('edit-newsletters-' . $category->tid);
      }
      else {
        $this
          ->assertNoField('newsletters[' . $category->tid . ']', t('Hidden or silent newsletter category is not shown.'));
      }
    }
  }

  // Register a new user through the form.
  $edit = array(
    'name' => $this
      ->randomName(),
    'mail' => $this
      ->randomEmail(),
    'pass[pass1]' => $pass = $this
      ->randomName(),
    'pass[pass2]' => $pass,
    'newsletters[' . $off_double_tid . ']' => $off_double_tid,
  );
  $this
    ->drupalPost(NULL, $edit, t('Create new account'));

  // Verify confirmation messages.
  $this
    ->assertText(t('Registration successful. You are now logged in.'));
  foreach ($categories as $category) {
    if (strpos($category->name, '-') === FALSE) {
      continue;
    }

    // Check confirmation message for all on and non-hidden newsletters and
    // the one that was explicitly selected.
    $new_account = variable_get('simplenews_new_account_' . $category->tid, 'on');
    $opt_inout = variable_get('simplenews_opt_inout_' . $category->tid, 'double');
    if ($new_account == 'on' && $opt_inout != 'hidden' || $category->name == 'off-double') {
      $this
        ->assertText(t('You have been subscribed to @name.', array(
        '@name' => $category->name,
      )));
    }
    else {

      // All other newsletters must not show a message, e.g. those which were
      // subscribed silently.
      $this
        ->assertNoText(t('You have been subscribed to @name.', array(
        '@name' => $category->name,
      )));
    }
  }

  // Log out again.
  $this
    ->drupalLogout();

  // Get the user id and do a login through the drupalLogin() function.
  $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $edit['name']));
  $user = user_load($uid);

  // Set the password so that the login works.
  $user->pass_raw = $edit['pass[pass1]'];

  // Verify newsletter subscription pages.
  $this
    ->drupalLogin($user);
  foreach (array(
    'newsletter/subscriptions',
    'user/' . $user->uid . '/edit/newsletter',
  ) as $path) {
    $this
      ->drupalGet($path);
    foreach ($categories as $category) {
      if (strpos($category->name, '-') === FALSE) {
        continue;
      }
      list($new_account_setting, $opt_inout_setting) = explode('-', $category->name);
      $new_account = variable_get('simplenews_new_account_' . $category->tid, 'on');
      $opt_inout = variable_get('simplenews_opt_inout_' . $category->tid, 'double');
      if ($opt_inout == 'hidden') {
        $this
          ->assertNoField('newsletters[' . $category->tid . ']', t('Hidden newsletter category is not shown.'));
      }
      else {
        if ($new_account == 'on' || $category->name == 'off-double' || $new_account == 'silent') {

          // All on, silent and the explicitly selected category should be checked.
          $this
            ->assertFieldChecked('edit-newsletters-' . $category->tid);
        }
        else {
          $this
            ->assertNoFieldChecked('edit-newsletters-' . $category->tid);
        }
      }
    }
  }

  // Unsubscribe from a newsletter category.
  $edit = array(
    'newsletters[' . $off_double_tid . ']' => FALSE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->assertNoFieldChecked('edit-newsletters-' . $off_double_tid);

  // Get a category which has the block enabled.
  foreach ($categories as $category) {

    // The default category is missing the from mail address. Use another one.
    if ($category->tid != 1) {
      $edit_category = $category;
      break;
    }
  }
}