View source
<?php
namespace Drupal\Tests\simplenews\Functional;
class SimplenewsRecipientHandlerTest extends SimplenewsTestBase {
public static $modules = [
'simplenews_demo',
];
protected function setUp() {
parent::setUp();
$ids = \Drupal::entityQuery('user')
->condition('uid', 0, '>')
->execute();
$storage = \Drupal::entityTypeManager()
->getStorage('user');
$entities = $storage
->loadMultiple($ids);
$storage
->delete($entities);
simplenews_cron();
$this->container
->get('state')
->set('system.test_mail_collector', []);
$admin_user = $this
->drupalCreateUser([
'send newsletter',
'create simplenews_issue content',
'edit any simplenews_issue content',
]);
$this
->drupalLogin($admin_user);
}
public function testSiteMail() {
$this
->drupalGet('node/add/simplenews_issue');
$this
->assertText(t('Recipients'));
$this
->assertText(t('How recipients should be selected.'));
$edit = [
'title[0][value]' => $this
->randomString(10),
'simplenews_issue[target_id]' => 'default',
'simplenews_issue[handler]' => 'simplenews_site_mail',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->clickLink(t('Newsletter'));
$this
->assertText(t('Send newsletter issue to 1 subscribers.'));
$this
->drupalPostForm(NULL, [], t('Send now'));
$this
->checkRecipients([
'simpletest@example.com' => 1,
]);
}
public function testNewUsers() {
$users = $this
->createUsers();
foreach (array_slice($users, -2) as $user) {
$user
->setLastAccessTime(time())
->save();
}
$this
->drupalGet('node/add/simplenews_issue');
$edit = [
'title[0][value]' => $this
->randomString(10),
'simplenews_issue[target_id]' => 'default',
'simplenews_issue[handler]' => 'simplenews_new_users',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->clickLink(t('Newsletter'));
$this
->assertText(t('Send newsletter issue to 3 subscribers.'));
$this
->drupalPostForm(NULL, [], t('Send now'));
$this
->checkRecipients(array_slice($users, 0, 3));
}
public function testSubscribersByRole() {
$users = $this
->createUsers('subscribe');
$recipients = array_slice($users, 2, 2);
$rid = $this
->createRole([]);
foreach ($recipients as $user) {
$user
->addRole($rid);
$user
->save();
}
$this
->drupalGet('node/add/simplenews_issue');
$edit = [
'title[0][value]' => $this
->randomString(10),
'simplenews_issue[target_id]' => 'default',
'simplenews_issue[handler]' => 'simplenews_subscribers_by_role',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->clickLink(t('Edit'));
$this
->assertText(t('Role'));
$edit = [
'simplenews_issue[handler_settings][role]' => $rid,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->clickLink(t('Newsletter'));
$this
->assertText(t('Send newsletter issue to 2 subscribers.'));
$this
->drupalPostForm(NULL, [], t('Send now'));
$this
->checkRecipients($recipients);
}
protected function createUsers($subscribe = FALSE) {
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
do {
$new_user = $this
->drupalCreateUser([]);
if ($subscribe) {
$subscription_manager
->subscribe($new_user
->getEmail(), 'default', FALSE);
}
$users[$new_user
->getEmail()] = $new_user;
} while (count($users) < 5);
return $users;
}
protected function checkRecipients(array $expected) {
simplenews_cron();
$mails = $this
->getMails();
$this
->assertEqual(count($expected), count($mails), t('All mails were sent.'));
foreach ($mails as $mail) {
$this
->assertTrue(isset($expected[$mail['to']]), t('Found valid recipient @recip', [
'@recip' => $mail['to'],
]));
unset($expected[$mail['to']]);
}
}
}