function SimplenewsAdministrationTest::testNewsletterSettings in Simplenews 8
Test various combinations of newsletter settings.
File
- src/
Tests/ SimplenewsAdministrationTest.php, line 44
Class
- SimplenewsAdministrationTest
- Managing of newsletter categories and content types.
Namespace
Drupal\simplenews\TestsCode
function testNewsletterSettings() {
// Allow registration of new accounts without approval.
$site_config = $this
->config('user.settings');
$site_config
->set('verify_mail', FALSE);
$site_config
->save();
$admin_user = $this
->drupalCreateUser(array(
'administer blocks',
'administer content types',
'administer nodes',
'access administration pages',
'administer permissions',
'administer newsletters',
'administer simplenews subscriptions',
'create simplenews_issue content',
'send newsletter',
));
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/services/simplenews');
// Check if the help text is displayed.
$this
->assertText('Newsletter allow you to send periodic e-mails to subscribers.');
// Create a newsletter 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,
)),
'id' => implode('_', array(
$new_account_setting,
$opt_inout_setting,
)),
'description' => $this
->randomString(20),
'new_account' => $new_account_setting,
'opt_inout' => $opt_inout_setting,
'priority' => rand(0, 5),
'receipt' => rand(0, 1) ? TRUE : FALSE,
'from_name' => $this
->randomMachineName(),
'from_address' => $this
->randomEmail(),
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
}
// New title should be saved correctly.
$this
->drupalPostForm('admin/config/services/simplenews/manage/default', [
'subject' => 'Edited subject',
], t('Save'));
$this
->drupalGet('admin/config/services/simplenews/manage/default');
$this
->assertFieldByName('subject', 'Edited subject');
$newsletters = simplenews_newsletter_get_all();
// 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 newsletter.
if ($newsletter->name == 'off-double') {
$off_double_newsletter_id = $newsletter
->id();
}
list($new_account_setting, $opt_inout_setting) = explode('-', $newsletter->name);
if ($newsletter->new_account == 'on' && $newsletter->opt_inout != 'hidden') {
$this
->assertFieldChecked($this
->getNewsletterFieldId($newsletter
->id()));
}
elseif ($newsletter->new_account == 'off' && $newsletter->opt_inout != 'hidden') {
$this
->assertNoFieldChecked($this
->getNewsletterFieldId($newsletter
->id()));
}
else {
$this
->assertNoField('subscriptions[' . $newsletter
->id() . ']', t('Hidden or silent newsletter is not shown.'));
}
}
// Register a new user through the form.
$edit = array(
'name' => $this
->randomMachineName(),
'mail' => $this
->randomEmail(),
'pass[pass1]' => $pass = $this
->randomMachineName(),
'pass[pass2]' => $pass,
'subscriptions[' . $off_double_newsletter_id . ']' => $off_double_newsletter_id,
);
$this
->drupalPostForm(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') {
$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();
$user = user_load_by_name($edit['name']);
// 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
->id() . '/simplenews',
) as $path) {
$this
->drupalGet($path);
foreach ($newsletters as $newsletter) {
if (strpos($newsletter->name, '-') === FALSE) {
continue;
}
list($new_account_setting, $opt_inout_setting) = explode('-', $newsletter->name);
if ($newsletter->opt_inout == 'hidden') {
$this
->assertNoField('subscriptions[' . $newsletter
->id() . ']', t('Hidden newsletter is not shown.'));
}
elseif ($newsletter->new_account == 'on' || $newsletter->name == 'off-double' || $newsletter->new_account == 'silent') {
// All on, silent and the explicitly selected newsletter should be checked.
$this
->assertFieldChecked($this
->getNewsletterFieldId($newsletter
->id()));
}
else {
$this
->assertNoFieldChecked($this
->getNewsletterFieldId($newsletter
->id()));
}
}
}
// Unsubscribe from a newsletter.
$edit = array(
'subscriptions[' . $off_double_newsletter_id . ']' => FALSE,
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->drupalGet('user/' . $user
->id() . '/simplenews');
$this
->assertNoFieldChecked($this
->getNewsletterFieldId($off_double_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;
}
}*/
// Check saving the subscriber as admin does not wipe the hidden newsletter settings.
$this
->drupalLogin($admin_user);
$subscriber = simplenews_subscriber_load_by_mail($user
->getEmail());
$this
->drupalGet('admin/people/simplenews/edit/' . $subscriber
->id());
$this
->assertNoField($this
->getNewsletterFieldId('on_hidden'));
$this
->assertNoField('mail');
$this
->drupalPostForm('admin/people/simplenews/edit/' . $subscriber
->id(), [], t('Save'));
$this
->drupalGet('admin/people/simplenews/edit/' . $subscriber
->id());
$this
->assertTrue($subscriber
->isSubscribed('on_hidden'));
$this
->assertTrue($subscriber
->isUnsubscribed($off_double_newsletter_id));
/*$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->randomMachineName();
$generated_names[] = $name;
$this->drupalGet('node/add/simplenews_issue');
$edit = array(
'title' => $name,
'simplenews_newsletter[und]' => $edit_newsletter->newsletter_id,
'date' => date('c', strtotime('+' . $index . ' day', $date)),
);
$this->drupalPostForm(NULL, $edit, ('Save'));
$this->clickLink(t('Newsletter'));
$this->drupalPostForm(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/manage/' . $edit_newsletter->id());
$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->drupalPostForm(NULL, $edit, t('Save'));
\Drupal::entityManager()->getStorage('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/manage/' . $edit_newsletter->id());
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, array(), t('Delete'));
// Verify that the newsletter has been deleted.
\Drupal::entityManager()->getStorage('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());*/
// Check if the help text is displayed.
$this
->drupalGet('admin/help/simplenews');
$this
->assertText('Simplenews adds elements to the newsletter node add/edit');
$this
->drupalGet('admin/config/services/simplenews/add');
$this
->assertText('You can create different newsletters (or subjects)');
}