public function SimplenewsSubscribeTest::testSubscribeMultiple in Simplenews 3.x
Same name and namespace in other branches
- 8.2 tests/src/Functional/SimplenewsSubscribeTest.php \Drupal\Tests\simplenews\Functional\SimplenewsSubscribeTest::testSubscribeMultiple()
Subscribe to multiple newsletters at the same time.
File
- tests/
src/ Functional/ SimplenewsSubscribeTest.php, line 22
Class
- SimplenewsSubscribeTest
- Un/subscription of anonymous and authenticated users.
Namespace
Drupal\Tests\simplenews\FunctionalCode
public function testSubscribeMultiple() {
$admin_user = $this
->drupalCreateUser([
'administer blocks',
'administer content types',
'administer nodes',
'access administration pages',
'administer permissions',
'administer newsletters',
'administer simplenews subscriptions',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/services/simplenews');
for ($i = 0; $i < 5; $i++) {
$this
->clickLink(t('Add newsletter'));
$name = $this
->randomMachineName();
$edit = [
'name' => $name,
'id' => strtolower($name),
'description' => $this
->randomString(20),
'access' => 'default',
];
$this
->submitForm($edit, 'Save');
}
$newsletters = simplenews_newsletter_get_all();
$this
->drupalLogout();
$enable = array_rand($newsletters, 3);
$mail = $this
->randomEmail(8);
$this
->subscribe($enable, $mail);
$this
->assertText(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'));
$this
->assertMailText(t('We have received a request to subscribe @user', [
'@user' => $mail,
]));
$mails = $this
->getMails();
$this
->assertEqual($mails[0]['from'], 'simpletest@example.com');
$this
->assertEqual($mails[0]['headers']['From'], '"Drupal" <simpletest@example.com>');
$confirm_url = $this
->extractConfirmationLink($this
->getMail(0));
$this
->drupalGet($confirm_url);
$this
->assertRaw(t('Are you sure you want to confirm your subscription for %user?', [
'%user' => simplenews_mask_mail($mail),
]));
$this
->submitForm([], 'Confirm');
$this
->assertRaw(t('Subscription changes confirmed for %user.', [
'%user' => $mail,
]));
/** @var \Drupal\simplenews\Subscription\SubscriptionManagerInterface $subscription_manager */
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
$subscription_manager
->reset();
$subscriber_storage = \Drupal::entityTypeManager()
->getStorage('simplenews_subscriber');
$subscriber_storage
->resetCache();
// Verify subscription changes.
foreach ($newsletters as $newsletter_id => $newsletter) {
$is_subscribed = $subscription_manager
->isSubscribed($mail, $newsletter_id);
$subscription_newsletter = $subscriber_storage
->getSubscriptionsByNewsletter($newsletter_id);
if (in_array($newsletter_id, $enable)) {
$this
->assertTrue($is_subscribed);
$this
->assertEquals(1, count($subscription_newsletter));
}
else {
$this
->assertFalse($is_subscribed);
$this
->assertEquals(0, count($subscription_newsletter));
}
}
// Go to the manage page and submit without changes.
$subscriber = Subscriber::loadByMail($mail);
$hash = simplenews_generate_hash($subscriber
->getMail(), 'manage');
$this
->drupalGet('newsletter/subscriptions/' . $subscriber
->id() . '/' . REQUEST_TIME . '/' . $hash);
$this
->submitForm([], 'Update');
$this
->assertText(t('Your newsletter subscriptions have been updated.'));
$this
->assertEquals(1, count($this
->getMails()), t('No confirmation mails have been sent.'));
// Unsubscribe from two of the three enabled newsletters.
$disable = array_rand(array_flip($enable), 2);
$edit = [
'mail[0][value]' => $mail,
];
foreach ($disable as $newsletter_id) {
$edit['subscriptions[' . $newsletter_id . ']'] = FALSE;
}
$this
->drupalGet('newsletter/subscriptions/' . $subscriber
->id() . '/' . REQUEST_TIME . '/' . $hash);
$this
->submitForm($edit, t('Update'));
// Verify subscription changes.
$subscriber_storage
->resetCache();
$subscription_manager
->reset();
$still_enabled = array_diff($enable, $disable);
foreach ($newsletters as $newsletter_id => $newsletter) {
$is_subscribed = $subscription_manager
->isSubscribed($mail, $newsletter_id);
if (in_array($newsletter_id, $still_enabled)) {
$this
->assertTrue($is_subscribed);
}
else {
$this
->assertFalse($is_subscribed);
}
}
// Unsubscribe with no confirmed email.
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
try {
$subscription_manager
->unsubscribe('new@email.com', $newsletter_id, FALSE);
$this
->fail('Exception not thrown.');
} catch (\Exception $e) {
$this
->assertEqual($e
->getMessage(), 'The subscriber does not exist.');
}
// Test expired confirmation links.
$enable = array_rand($newsletters, 3);
$mail = $this
->randomEmail(8);
foreach ($enable as $newsletter_id) {
$edit['subscriptions[' . $newsletter_id . ']'] = TRUE;
}
$this
->subscribe($enable, $mail);
$subscriber = Subscriber::loadByMail($mail);
$expired_timestamp = REQUEST_TIME - 86401;
$hash = simplenews_generate_hash($subscriber
->getMail(), 'combined' . serialize($subscriber
->getChanges()), $expired_timestamp);
$url = 'newsletter/confirm/combined/' . $subscriber
->id() . '/' . $expired_timestamp . '/' . $hash;
$this
->drupalGet($url);
$this
->assertText(t('This link has expired.'));
$this
->submitForm([], 'Request new confirmation mail');
$confirm_url = $this
->extractConfirmationLink($this
->getMail());
$this
->assertMailText(t('We have received a request to subscribe @user', [
'@user' => $mail,
]));
$this
->drupalGet($confirm_url);
$this
->assertRaw(t('Are you sure you want to confirm your subscription for %user?', [
'%user' => simplenews_mask_mail($mail),
]));
$this
->drupalGet($confirm_url);
$this
->submitForm([], 'Confirm');
$this
->assertRaw(t('Subscription changes confirmed for %user.', [
'%user' => $mail,
]));
}