/**
  * Tests that fields are synchronized using the Subscriber form.
  */
 public function testSubscriberFormFieldSync()
 {
     // Create a subscriber for the user.
     $subscriber = Subscriber::create(array('uid' => $this->user->id(), 'mail' => '*****@*****.**'));
     $subscriber->save();
     // Edit subscriber field and assert user field is changed accordingly.
     $this->drupalLogin($this->user);
     $this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
     $this->assertField('field_shared[0][value]');
     $this->assertRaw($this->user->field_shared->value);
     $new_value = $this->randomMachineName();
     $this->drupalPostForm(NULL, array('field_shared[0][value]' => $new_value), t('Save'));
     $this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
     $this->assertRaw($new_value);
     $this->user = User::load($this->user->id());
     $this->assertEqual($this->user->field_shared->value, $new_value);
     // Unset the sync setting and assert field is not synced.
     $this->drupalPostForm('admin/config/people/simplenews/settings/subscriber', array('simplenews_sync_fields' => FALSE), t('Save configuration'));
     $unsynced_value = $this->randomMachineName();
     $this->drupalPostForm('admin/people/simplenews/edit/' . $subscriber->id(), array('field_shared[0][value]' => $unsynced_value), t('Save'));
     $this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
     $this->assertRaw($unsynced_value);
     $this->user = User::load($this->user->id());
     $this->assertEqual($this->user->field_shared->value, $new_value);
     $this->assertNotEqual($this->user->field_shared->value, $unsynced_value);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $snid = NULL, $timestamp = NULL, $hash = NULL)
 {
     $user = \Drupal::currentUser();
     if ($subscriber = simplenews_subscriber_load_by_uid($user->id())) {
         $this->setEntity($subscriber);
     } elseif ($mail = $user->getEmail()) {
         $this->setEntity(Subscriber::create(array('mail' => $mail)));
     } elseif ($snid && $timestamp && $hash) {
         $subscriber = simplenews_subscriber_load($snid);
         if ($subscriber && $hash == simplenews_generate_hash($subscriber->getMail(), 'manage', $timestamp)) {
             $this->setEntity($subscriber);
         } else {
             throw new NotFoundHttpException();
         }
     }
     return parent::buildForm($form, $form_state);
 }
 /**
  * Unsets the sync setting and asserts that fields are not synced.
  */
 public function testDisableSync()
 {
     // Disable sync.
     $this->config('simplenews.settings')->set('subscriber.sync_fields', FALSE)->save();
     // Create and attach a field to both.
     $this->addField('string', 'field_on_both', 'simplenews_subscriber');
     $this->addField('string', 'field_on_both', 'user');
     // Create a user with a value for the field.
     $user = User::create(array('name' => 'user', 'field_on_both' => 'foo', 'mail' => '*****@*****.**'));
     $user->save();
     // Create a subscriber.
     $subscriber = Subscriber::create(array('mail' => '*****@*****.**'));
     // Assert that the shared field does not get the value from the user.
     $this->assertNull($subscriber->get('field_on_both')->value);
     // Update the subscriber and assert that it is not synced to the user.
     $subscriber->set('field_on_both', 'bar');
     $subscriber->save();
     $user = User::load($user->id());
     $this->assertEqual($user->get('field_on_both')->value, 'foo');
     // Create a subscriber with a value for the field.
     $subscriber = Subscriber::create(array('field_on_both' => 'foo', 'mail' => '*****@*****.**'));
     $subscriber->save();
     // Create a user.
     $user = User::create(array('name' => 'user2', 'mail' => '*****@*****.**'));
     // Assert that the shared field does not get the value from the subscriber.
     $this->assertNull($user->get('field_on_both')->value);
     // Update the user and assert that it is not synced to the subscriber.
     $user->set('field_on_both', 'bar');
     $user->save();
     $subscriber = Subscriber::load($subscriber->id());
     $this->assertEqual($subscriber->get('field_on_both')->value, 'foo');
 }
 /**
  * {@inheritdoc}
  */
 public function sendConfirmations()
 {
     foreach ($this->confirmations as $mail => $changes) {
         $subscriber = simplenews_subscriber_load_by_mail($mail);
         if (!$subscriber) {
             $subscriber = Subscriber::create(array());
             $subscriber->setMail($mail);
             $subscriber->setLangcode($this->languageManager->getCurrentLanguage());
             $subscriber->save();
         }
         $subscriber->setChanges($changes);
         $this->mailer->sendCombinedConfirmation($subscriber);
         // Save the changes in the subscriber if there is a real subscriber object.
         if ($subscriber && $subscriber->id()) {
             $subscriber->save();
         }
     }
     $sent = !empty($this->confirmations);
     $this->confirmations = array();
     return $sent;
 }
 /**
  * {@inheritdoc}
  */
 public function build()
 {
     /** @var \Drupal\simplenews\Form\SubscriptionsBlockForm $form_object */
     $form_object = \Drupal::entityManager()->getFormObject('simplenews_subscriber', 'block');
     $form_object->setUniqueId($this->configuration['unique_id']);
     $form_object->setNewsletterIds($this->configuration['newsletters']);
     $form_object->message = $this->configuration['message'];
     // Set the entity on the form.
     if ($user = \Drupal::currentUser()) {
         if ($subscriber = simplenews_subscriber_load_by_uid($user->id())) {
             $form_object->setEntity($subscriber);
         } else {
             $form_object->setEntity(Subscriber::create(array('mail' => $user->getEmail())));
         }
     } else {
         $form_object->setEntity(Subscriber::create());
     }
     return $this->formBuilder->getForm($form_object);
 }
Пример #6
0
 /**
  * {@inheritdoc}
  */
 public function sendTest(NodeInterface $node, array $test_addresses)
 {
     // Force the current user to anonymous to ensure consistent permissions.
     $this->accountSwitcher->switchTo(new AnonymousUserSession());
     // Send the test newsletter to the test address(es) specified in the node.
     // Build array of test email addresses.
     // Send newsletter to test addresses.
     // Emails are send direct, not using the spool.
     $recipients = array('anonymous' => array(), 'user' => array());
     foreach ($test_addresses as $mail) {
         $mail = trim($mail);
         if (!empty($mail)) {
             $subscriber = simplenews_subscriber_load_by_mail($mail);
             if (!$subscriber) {
                 // @todo: Find a cleaner way to do this.
                 $subscriber = Subscriber::create(array());
                 $subscriber->setUserId(0);
                 $subscriber->setMail($mail);
                 $subscriber->setLangcode(\Drupal::languageManager()->getCurrentLanguage());
             }
             if ($subscriber->getUserId()) {
                 $account = $subscriber->uid->entity;
                 $recipients['user'][] = $account->getUserName() . ' <' . $mail . '>';
             } else {
                 $recipients['anonymous'][] = $mail;
             }
             $mail = new MailEntity($node, $subscriber, \Drupal::service('simplenews.mail_cache'));
             $mail->setKey('test');
             $this->sendMail($mail);
         }
     }
     if (count($recipients['user'])) {
         $recipients_txt = implode(', ', $recipients['user']);
         drupal_set_message(t('Test newsletter sent to user %recipient.', array('%recipient' => $recipients_txt)));
     }
     if (count($recipients['anonymous'])) {
         $recipients_txt = implode(', ', $recipients['anonymous']);
         drupal_set_message(t('Test newsletter sent to anonymous %recipient.', array('%recipient' => $recipients_txt)));
     }
     $this->accountSwitcher->switchBack();
 }
 /**
  * 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') . ')');
 }