function SimpleNewsAdministrationTestCase::testNewsletterSettings in Simplenews 7.2
Test various combinations of newsletter settings.
File
- tests/
simplenews.test, line 1102 - Simplenews test functions.
Class
- SimpleNewsAdministrationTestCase
- @todo: Newsletter node create, send draft, send final
Code
function testNewsletterSettings() {
// 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 newsletter 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'));
$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_newsletter_load_multiple');
$newsletters = simplenews_newsletter_get_all();
// Check block settings.
$this
->drupalGet('admin/structure/block');
//blocks[simplenews_42][region]
foreach ($newsletters as $newsletter) {
if (strpos($newsletter->name, '-') === FALSE) {
continue;
}
list($new_account_setting, $opt_inout_setting, $block) = explode('-', $newsletter->name);
if ($block == 'block' && $opt_inout_setting != 'hidden') {
$this
->assertField('blocks[simplenews_' . $newsletter->newsletter_id . '][region]', t('Block is displayed for newsletter'));
}
else {
$this
->assertNoField('blocks[simplenews_' . $newsletter->newsletter_id . '][region]', t('Block is not displayed for newsletter'));
}
}
// Check registration form.
$this
->drupalLogout();
$this
->drupalGet('user/register');
foreach ($newsletters as $newsletter) {
if (strpos($newsletter->name, '-') === FALSE) {
continue;
}
// Explicitly subscribe to the off-double-block newsletter.
if ($newsletter->name == 'off-double-block') {
$off_double_block_newsletter_id = $newsletter->newsletter_id;
}
list($new_account_setting, $opt_inout_setting, $block) = explode('-', $newsletter->name);
if ($newsletter->new_account == 'on' && $newsletter->opt_inout != 'hidden') {
$this
->assertFieldChecked('edit-newsletters-' . $newsletter->newsletter_id);
}
elseif ($newsletter->new_account == 'off' && $newsletter->opt_inout != 'hidden') {
$this
->assertNoFieldChecked('edit-newsletters-' . $newsletter->newsletter_id);
}
else {
$this
->assertNoField('newsletters[' . $newsletter->newsletter_id . ']', t('Hidden or silent newsletter 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_newsletter_id . ']' => $off_double_block_newsletter_id,
);
$this
->drupalPost(NULL, $edit, t('Create new account'));
// Verify confirmation messages.
$this
->assertText(t('Registration successful. You are now logged in.'));
foreach ($newsletters as $newsletter) {
// Check confirmation message for all on and non-hidden newsletters and
// the one that was explicitly selected.
if ($newsletter->new_account == 'on' && $newsletter->opt_inout != 'hidden' || $newsletter->name == 'off-double-block') {
$this
->assertText(t('You have been subscribed to @name.', array(
'@name' => $newsletter->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' => $newsletter->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 ($newsletters as $newsletter) {
if (strpos($newsletter->name, '-') === FALSE) {
continue;
}
list($new_account_setting, $opt_inout_setting, $block) = explode('-', $newsletter->name);
if ($newsletter->opt_inout == 'hidden') {
$this
->assertNoField('newsletters[' . $newsletter->newsletter_id . ']', t('Hidden newsletter is not shown.'));
}
elseif ($newsletter->new_account == 'on' || $newsletter->name == 'off-double-block' || $newsletter->new_account == 'silent') {
// All on, silent and the explicitly selected newsletter should be checked.
$this
->assertFieldChecked('edit-newsletters-' . $newsletter->newsletter_id);
}
else {
$this
->assertNoFieldChecked('edit-newsletters-' . $newsletter->newsletter_id);
}
}
}
// Unsubscribe from a newsletter.
$edit = array(
'newsletters[' . $off_double_block_newsletter_id . ']' => FALSE,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertNoFieldChecked('edit-newsletters-' . $off_double_block_newsletter_id);
// Get a newsletter which has the block enabled.
foreach ($newsletters as $newsletter) {
// The default newsletter is missing the from mail address. Use another one.
if ($newsletter->block == TRUE && $newsletter->newsletter_id != 1 && $newsletter->opt_inout != 'hidden') {
$edit_newsletter = $newsletter;
break;
}
}
$this
->drupalLogin($admin_user);
$this
->setupSubscriptionBlock($edit_newsletter->newsletter_id, $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,
'simplenews_newsletter[und]' => $edit_newsletter->newsletter_id,
'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_newsletter->newsletter_id . '/edit');
$this
->assertFieldByName('name', $edit_newsletter->name, t('Newsletter name is displayed when editing'));
$this
->assertFieldByName('description', $edit_newsletter->description, t('Newsletter description is displayed when editing'));
$edit = array(
'block' => FALSE,
);
$this
->drupalPost(NULL, $edit, t('Save'));
entity_get_controller('simplenews_newsletter')
->resetCache();
$updated_newsletter = simplenews_newsletter_load($edit_newsletter->newsletter_id);
$this
->assertEqual(0, $updated_newsletter->block, t('Block for newsletter disabled'));
$this
->drupalGet('admin/structure/block');
$this
->assertNoText($edit_newsletter->name, t('Newsletter block was removed'));
// Delete a newsletter.
$this
->drupalGet('admin/config/services/simplenews/categories/' . $edit_newsletter->newsletter_id . '/edit');
$edit = array();
$this
->drupalPost(NULL, $edit, t('Delete'));
$this
->drupalPost(NULL, $edit, t('Delete'));
// Verify that the newsletter has been deleted.
entity_get_controller('simplenews_newsletter')
->resetCache();
$this
->assertFalse(simplenews_newsletter_load($edit_newsletter->newsletter_id));
$this
->assertFalse(db_query('SELECT newsletter_id FROM {simplenews_newsletter} WHERE newsletter_id = :newsletter_id', array(
':newsletter_id' => $edit_newsletter->newsletter_id,
))
->fetchField());
}