/**
  * Select randomly one of the available newsletters.
  *
  * @return string
  *   The ID of a newsletter.
  */
 function getRandomNewsletter()
 {
     if ($newsletters = array_keys(simplenews_newsletter_get_all())) {
         return $newsletters[array_rand($newsletters)];
     }
     return 0;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $removed = array();
     $invalid = array();
     $checked_lists = array_keys(array_filter($form_state->getValue('newsletters')));
     /** @var \Drupal\simplenews\Subscription\SubscriptionManagerInterface $subscription_manager */
     $subscription_manager = \Drupal::service('simplenews.subscription_manager');
     $emails = preg_split("/[\\s,]+/", $form_state->getValue('emails'));
     foreach ($emails as $email) {
         $email = trim($email);
         if (valid_email_address($email)) {
             foreach ($checked_lists as $newsletter_id) {
                 $subscription_manager->unsubscribe($email, $newsletter_id, FALSE, 'mass unsubscribe');
                 $removed[] = $email;
             }
         } else {
             $invalid[] = $email;
         }
     }
     if ($removed) {
         $removed = implode(", ", $removed);
         drupal_set_message(t('The following addresses were unsubscribed: %removed.', array('%removed' => $removed)));
         $newsletters = simplenews_newsletter_get_all();
         $list_names = array();
         foreach ($checked_lists as $newsletter_id) {
             $list_names[] = $newsletters[$newsletter_id]->label();
         }
         drupal_set_message(t('The addresses were unsubscribed from the following newsletters: %newsletters.', array('%newsletters' => implode(', ', $list_names))));
     } else {
         drupal_set_message(t('No addresses were removed.'));
     }
     if ($invalid) {
         $invalid = implode(", ", $invalid);
         drupal_set_message(t('The following addresses were invalid: %invalid.', array('%invalid' => $invalid)), 'error');
     }
     // Return to the parent page.
     $form_state->setRedirect('view.simplenews_subscribers.page_1');
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $form['emails'] = array('#type' => 'textarea', '#title' => t('Email addresses'), '#cols' => 60, '#rows' => 5, '#description' => t('Email addresses must be separated by comma, space or newline.'));
     $form['newsletters'] = array('#type' => 'checkboxes', '#title' => t('Subscribe to'), '#options' => simplenews_newsletter_list(), '#required' => TRUE);
     foreach (simplenews_newsletter_get_all() as $id => $newsletter) {
         $form['newsletters'][$id]['#description'] = SafeMarkup::checkPlain($newsletter->description);
     }
     $form['resubscribe'] = array('#type' => 'checkbox', '#title' => t('Force resubscription'), '#description' => t('If checked, previously unsubscribed e-mail addresses will be resubscribed. Consider that this might be against the will of your users.'));
     // Include language selection when the site is multilingual.
     // Default value is the empty string which will result in receiving emails
     // in the site's default language.
     if (\Drupal::languageManager()->isMultilingual()) {
         $options[''] = t('Site default language');
         $languages = \Drupal::languageManager()->getLanguages();
         foreach ($languages as $langcode => $language) {
             $options[$langcode] = $language->getName();
         }
         $form['language'] = array('#type' => 'radios', '#title' => t('Anonymous user preferred language'), '#default_value' => '', '#options' => $options, '#description' => t('New subscriptions will be subscribed with the selected preferred language. The language of existing subscribers is unchanged.'));
     } else {
         $form['language'] = array('#type' => 'value', '#value' => '');
     }
     $form['submit'] = array('#type' => 'submit', '#value' => t('Subscribe'));
     return $form;
 }
 /**
  * testSubscribeAuthenticated
  *
  * Steps performed:
  *   0. Preparation
  *   1. Subscribe authenticated via block
  *   2. Unsubscribe authenticated via subscription page
  *   3. Subscribe authenticated via subscription page
  *   4. Unsubscribe authenticated via account page
  *   5. Subscribe authenticated via account page
  *   6. Subscribe authenticated via multi block
  */
 function testSubscribeAuthenticated()
 {
     // 0. Preparation
     // Login admin
     // Set permission for anonymous to subscribe
     // Enable newsletter block
     // Logout admin
     // Login Subscriber
     $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer content types', 'administer nodes', 'access administration pages', 'administer permissions', 'administer newsletters'));
     $this->drupalLogin($admin_user);
     // Create some newsletters for multi-sign up block.
     $this->drupalGet('admin/config/services/simplenews');
     for ($i = 0; $i < 5; $i++) {
         $this->clickLink(t('Add newsletter'));
         $name = $this->randomMachineName();
         $edit = array('name' => $name, 'id' => strtolower($name), 'description' => $this->randomString(20), 'opt_inout' => 'double');
         $this->drupalPostForm(NULL, $edit, t('Save'));
     }
     $newsletter_id = $this->getRandomNewsletter();
     $this->drupalLogout();
     // Setup subscription block with subscription form.
     $block_settings = array('newsletters' => array($newsletter_id), 'message' => $this->randomMachineName(4));
     $single_block = $this->setupSubscriptionBlock($block_settings);
     $subscriber_user = $this->drupalCreateUser(array('subscribe to newsletters'));
     $this->drupalLogin($subscriber_user);
     // 1. Subscribe authenticated via block
     // Subscribe + submit
     // Assert confirmation message
     $this->drupalPostForm(NULL, [], t('Subscribe'));
     $this->assertText(t('You have been subscribed.'), t('Authenticated user subscribed using the subscription block.'));
     // 2. Unsubscribe authenticated via subscription page
     // Unsubscribe + submit
     // Assert confirmation message
     $edit = array("subscriptions[{$newsletter_id}]" => 0);
     $this->drupalPostForm('newsletter/subscriptions', $edit, t('Update'));
     $this->assertRaw(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $subscriber_user->getEmail())), t('Authenticated user unsubscribed on the subscriptions page.'));
     // 3. Subscribe authenticated via subscription page
     // Subscribe + submit
     // Assert confirmation message
     $edit = array("subscriptions[{$newsletter_id}]" => '1');
     $this->drupalPostForm('newsletter/subscriptions', $edit, t('Update'));
     $this->assertRaw(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $subscriber_user->getEmail())), t('Authenticated user subscribed on the subscriptions page.'));
     // 4. Unsubscribe authenticated via account page
     // Unsubscribe + submit
     // Assert confirmation message
     $edit = array("subscriptions[{$newsletter_id}]" => FALSE);
     $url = 'user/' . $subscriber_user->id() . '/simplenews';
     $this->drupalPostForm($url, $edit, t('Save'));
     $this->assertRaw(t('Your newsletter subscriptions have been updated.', array('%mail' => $subscriber_user->getEmail())), t('Authenticated user unsubscribed on the account page.'));
     $subscriber = simplenews_subscriber_load_by_mail($subscriber_user->getEmail());
     $subscription = $subscriber->getSubscription($newsletter_id);
     $this->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, $subscription->status, t('Subscription is unsubscribed'));
     // 5. Subscribe authenticated via account page
     // Subscribe + submit
     // Assert confirmation message
     $edit = array("subscriptions[{$newsletter_id}]" => '1');
     $url = 'user/' . $subscriber_user->id() . '/simplenews';
     $this->drupalPostForm($url, $edit, t('Save'));
     $this->assertRaw(t('Your newsletter subscriptions have been updated.', array('%mail' => $subscriber_user->getEmail())), t('Authenticated user unsubscribed on the account page.'));
     // Disable the newsletter block.
     $single_block->delete();
     // Setup subscription block with subscription form.
     $block_settings = array('newsletters' => array_keys(simplenews_newsletter_get_all()), 'message' => $this->randomMachineName(4));
     $multi_block = $this->setupSubscriptionBlock($block_settings);
     // Try to submit multi-signup form without selecting a newsletter.
     $subscriber_user2 = $this->drupalCreateUser(array('subscribe to newsletters'));
     $this->drupalLogin($subscriber_user2);
     // Check that the user has only access to his own subscriptions page.
     $this->drupalGet('user/' . $subscriber_user->id() . '/simplenews');
     $this->assertResponse(403);
     $this->drupalGet('user/' . $subscriber_user2->id() . '/simplenews');
     $this->assertResponse(200);
     $this->assertNoField('mail[0][value]');
     $this->drupalPostForm(NULL, array(), t('Update'));
     $this->assertText(t('The newsletter subscriptions for @mail have been updated.', array('@mail' => $subscriber_user2->getEmail())));
     // Nothing should have happened.
     $this->assertNoFieldChecked('edit-subscriptions-' . $newsletter_id);
     // Now fill out the form and try again.
     $edit = array('subscriptions[' . $newsletter_id . ']' => TRUE);
     $this->drupalPostForm(NULL, $edit, t('Update'));
     $this->assertText(t('The newsletter subscriptions for @mail have been updated.', array('@mail' => $subscriber_user2->getEmail())));
     $this->assertFieldChecked('edit-subscriptions-' . $newsletter_id);
     // Unsubscribe.
     $edit = array('subscriptions[' . $newsletter_id . ']' => FALSE);
     $this->drupalPostForm(NULL, $edit, t('Update'));
     $this->assertText(t('The newsletter subscriptions for @mail have been updated.', array('@mail' => $subscriber_user2->getEmail())));
     $this->assertNoFieldChecked('edit-subscriptions-' . $newsletter_id);
     // And now the same for the newsletter/subscriptions page.
     $subscriber_user3 = $this->drupalCreateUser(array('subscribe to newsletters'));
     $this->drupalLogin($subscriber_user3);
     $this->assertNoField('mail[0][value]');
     $this->drupalPostForm('newsletter/subscriptions', array(), t('Update'));
     $this->assertText(t('The newsletter subscriptions for @mail have been updated.', array('@mail' => $subscriber_user3->getEmail())));
     // Nothing should have happened.
     $this->assertNoFieldChecked('edit-subscriptions-' . $newsletter_id);
     // Now fill out the form and try again.
     $edit = array('subscriptions[' . $newsletter_id . ']' => TRUE);
     $this->drupalPostForm('newsletter/subscriptions', $edit, t('Update'));
     $this->assertText(t('The newsletter subscriptions for @mail have been updated.', array('@mail' => $subscriber_user3->getEmail())));
     $this->assertFieldChecked('edit-subscriptions-' . $newsletter_id);
     // Unsubscribe.
     $edit = array('subscriptions[' . $newsletter_id . ']' => FALSE);
     $this->drupalPostForm('newsletter/subscriptions', $edit, t('Update'));
     $this->assertText(t('The newsletter subscriptions for @mail have been updated.', array('@mail' => $subscriber_user3->getEmail())));
     $this->assertNoFieldChecked('edit-subscriptions-' . $newsletter_id);
 }
 /**
  * Setting Up the subscription for the subscriber.
  *
  * Assigning the two newsletters to the three subscribers,
  * Where first two subscribers subscribe the first two newsletters
  * And third subscriber subscribe both newsletters.
  */
 public function setUpSubscribersWithMultiNewsletters()
 {
     // Intilizing required variables.
     $count_subscribers = 0;
     $newsletters_total = array();
     // Creating three subscibers.
     $this->subscribers = array();
     for ($i = 0; $i < 3; $i++) {
         $mail = $this->randomEmail();
         $this->subscribers[] = $mail;
     }
     // Fetching newletters into an array.
     foreach (simplenews_newsletter_get_all() as $key => $value) {
         $newsletters_total[] = $key;
     }
     // Looping the subscriber for the first newsletter.
     foreach ($this->subscribers as $subscribersname) {
         $this->drupalGet('admin/people/simplenews');
         $this->clickLink(t('Mass subscribe'));
         // Ignoring the second subscriber for first newletters.
         if (++$count_subscribers == 2) {
             continue;
         }
         // Subscription array for the first newsletters.
         $edit = array('emails' => $subscribersname, 'newsletters[' . $newsletters_total[0] . ']' => TRUE);
         $this->drupalPostForm(NULL, $edit, t('Subscribe'));
     }
     // Re-setting subscriber count.
     $count_subscribers = 0;
     // Looping the subscriber for the second newsletter.
     foreach ($this->subscribers as $subscribersname) {
         $this->drupalGet('admin/people/simplenews');
         $this->clickLink(t('Mass subscribe'));
         // Ignoring the first subscriber for first newletters.
         if (++$count_subscribers == 1) {
             continue;
         }
         // Subscription array for the second newsletters.
         $edit = array('emails' => $subscribersname, 'newsletters[' . $newsletters_total[1] . ']' => TRUE);
         $this->drupalPostForm(NULL, $edit, t('Subscribe'));
     }
 }
 /**
  * Test content subscription status filter in subscriber view.
  */
 function testSubscriberStatusFilter()
 {
     // Make sure subscription overview can't be accessed without permission.
     $this->drupalGet('admin/people/simplenews');
     $this->assertResponse(403);
     $admin_user = $this->drupalCreateUser(array('administer newsletters', 'create simplenews_issue content', 'administer nodes', 'administer simplenews subscriptions'));
     $this->drupalLogin($admin_user);
     $subscribers = array();
     // Create some subscribers.
     for ($i = 0; $i < 3; $i++) {
         $subscribers[] = Subscriber::create(array('mail' => $this->randomEmail()));
     }
     foreach ($subscribers as $subscriber) {
         $subscriber->setStatus(SubscriberInterface::ACTIVE);
     }
     // Subscribe to the default newsletter and set subscriber status.
     $subscribers[0]->subscribe('default', SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED);
     $subscribers[1]->subscribe('default', SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED);
     $subscribers[2]->subscribe('default', SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED);
     foreach ($subscribers as $subscriber) {
         $subscriber->save();
     }
     $newsletters = simplenews_newsletter_get_all();
     // Filter out subscribers by their subscription status and assert the output.
     $this->drupalGet('admin/people/simplenews', array('query' => array('subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED)));
     $row = $this->xpath('//tbody/tr');
     $this->assertEqual(1, count($row));
     $this->assertEqual($subscribers[0]->getMail(), trim((string) $row[0]->td[0]));
     $this->drupalGet('admin/people/simplenews', array('query' => array('subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED)));
     $row = $this->xpath('//tbody/tr');
     $this->assertEqual(1, count($row));
     $this->assertEqual($subscribers[1]->getMail(), trim((string) $row[0]->td[0]));
     $this->assertText($newsletters['default']->name . ' (' . t('Unconfirmed') . ')');
     $this->drupalGet('admin/people/simplenews', array('query' => array('subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED)));
     $row = $this->xpath('//tbody/tr');
     $this->assertEqual(1, count($row));
     $this->assertEqual($subscribers[2]->getMail(), trim((string) $row[0]->td[0]));
     $this->assertText($newsletters['default']->name . ' (' . t('Unsubscribed') . ')');
 }