You are here

function SimpleNewsAdministrationTestCase::testCategorySettings in Simplenews 7

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

Test various combinations of newsletter category settings.

File

tests/simplenews.test, line 1009
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',
    'create simplenews content',
    'send newsletter',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('admin/config/services/simplenews');

  // Create a category for all possible setting combinations.
  $new_account = array(
    'none',
    'off',
    'on',
    'silent',
  );
  $opt_inout = array(
    'hidden',
    'single',
    'double',
  );
  $block = array(
    'block' => TRUE,
    'noblock' => FALSE,
  );
  foreach ($new_account as $new_account_setting) {
    foreach ($opt_inout as $opt_inout_setting) {
      foreach ($block as $name => $value) {
        $this
          ->clickLink(t('Add newsletter category'));
        $edit = array(
          'name' => implode('-', array(
            $new_account_setting,
            $opt_inout_setting,
            $name,
          )),
          'description' => $this
            ->randomString(20),
          'new_account' => $new_account_setting,
          'opt_inout' => $opt_inout_setting,
          'block' => $value,
          'priority' => rand(0, 5),
          'receipt' => rand(0, 1) ? TRUE : FALSE,
          'from_name' => $this
            ->randomName(),
          'from_address' => $this
            ->randomEmail(),
        );
        $this
          ->drupalPost(NULL, $edit, t('Save'));
      }
    }
  }
  drupal_static_reset('simplenews_categories_load_multiple');
  $categories = simplenews_categories_load_multiple();

  // Check block settings.
  $this
    ->drupalGet('admin/structure/block');

  //blocks[simplenews_42][region]
  foreach ($categories as $category) {
    if (strpos($category->name, '-') === FALSE) {
      continue;
    }
    list($new_account_setting, $opt_inout_setting, $block) = explode('-', $category->name);
    if ($block == 'block' && $opt_inout_setting != 'hidden') {
      $this
        ->assertField('blocks[simplenews_' . $category->tid . '][region]', t('Block is displayed for category'));
    }
    else {
      $this
        ->assertNoField('blocks[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-block newsletter.
    if ($category->name == 'off-double-block') {
      $off_double_block_tid = $category->tid;
    }
    list($new_account_setting, $opt_inout_setting, $block) = explode('-', $category->name);
    if ($category->new_account == 'on' && $category->opt_inout != 'hidden') {
      $this
        ->assertFieldChecked('edit-newsletters-' . $category->tid);
    }
    elseif ($category->new_account == 'off' && $category->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_block_tid . ']' => $off_double_block_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) {

    // Check confirmation message for all on and non-hidden newsletters and
    // the one that was explicitly selected.
    if ($category->new_account == 'on' && $category->opt_inout != 'hidden' || $category->name == 'off-double-block') {
      $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_query('SELECT uid FROM {users} WHERE name = :name', array(
    ':name' => $edit['name'],
  ))
    ->fetchField();
  $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/simplenews',
  ) as $path) {
    $this
      ->drupalGet($path);
    foreach ($categories as $category) {
      if (strpos($category->name, '-') === FALSE) {
        continue;
      }
      list($new_account_setting, $opt_inout_setting, $block) = explode('-', $category->name);
      if ($category->opt_inout == 'hidden') {
        $this
          ->assertNoField('newsletters[' . $category->tid . ']', t('Hidden newsletter category is not shown.'));
      }
      elseif ($category->new_account == 'on' || $category->name == 'off-double-block' || $category->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_block_tid . ']' => FALSE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->assertNoFieldChecked('edit-newsletters-' . $off_double_block_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->block == TRUE && $category->tid != 1 && $category->opt_inout != 'hidden') {
      $edit_category = $category;
      break;
    }
  }
  $this
    ->drupalLogin($admin_user);
  $this
    ->setupSubscriptionBlock($edit_category->tid, $settings = array(
    'issue count' => 2,
    'previous issues' => 1,
  ));

  // Create a bunch of newsletters.
  $generated_names = array();
  $date = strtotime('monday this week');
  for ($index = 0; $index < 3; $index++) {
    $name = $this
      ->randomName();
    $generated_names[] = $name;
    $this
      ->drupalGet('node/add/simplenews');
    $edit = array(
      'title' => $name,
      'field_simplenews_term[und]' => $edit_category->tid,
      'date' => date('c', strtotime('+' . $index . ' day', $date)),
    );
    $this
      ->drupalPost(NULL, $edit, 'Save');
    $this
      ->clickLink(t('Newsletter'));
    $this
      ->drupalPost(NULL, array(
      'simplenews[send]' => SIMPLENEWS_COMMAND_SEND_NOW,
    ), t('Submit'));
  }

  // Display the two recent issues.
  $this
    ->drupalGet('');
  $this
    ->assertText(t('Previous issues'), 'Should display recent issues.');
  $displayed_issues = $this
    ->xpath("//div[@class='issues-list']/div/ul/li/a");
  $this
    ->assertEqual(count($displayed_issues), 2, 'Displys two recent issues.');
  $this
    ->assertFalse(in_array($generated_names[0], $displayed_issues));
  $this
    ->assertTrue(in_array($generated_names[1], $displayed_issues));
  $this
    ->assertTrue(in_array($generated_names[2], $displayed_issues));
  $this
    ->drupalGet('admin/config/services/simplenews/categories/' . $edit_category->tid . '/edit');
  $this
    ->assertFieldByName('name', $edit_category->name, t('Category name is displayed when editing'));
  $this
    ->assertFieldByName('description', $edit_category->description, t('Category description is displayed when editing'));
  $edit = array(
    'block' => FALSE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $updated_category = simplenews_category_load($edit_category->tid);
  $this
    ->assertEqual(0, $updated_category->block, t('Block for category disabled'));
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->assertNoText($edit_category->name, t('Category block was removed'));

  // Delete a category.
  $this
    ->drupalGet('admin/config/services/simplenews/categories/' . $edit_category->tid . '/edit');
  $edit = array();
  $this
    ->drupalPost(NULL, $edit, t('Delete'));
  $this
    ->drupalPost(NULL, $edit, t('Delete'));

  // Verify that the category has been deleted.
  $this
    ->assertFalse(simplenews_category_load($edit_category->tid));
  $this
    ->assertFalse(db_query('SELECT tid FROM {simplenews_category} WHERE tid = :tid', array(
    ':tid' => $edit_category->tid,
  ))
    ->fetchField());
}