Problem/Motivation

Sending a test email to an email of the user should be sent in the context of that user, same as normal emails.

Proposed resolution

Remaining tasks

User interface changes

API changes

Comments

miro_dietiker’s picture

Priority: Normal » Major

Pushing to major as this leads to wrong test results.

arla’s picture

The current user is set to the test address user, but only if that user is also a subscriber.

Simplified snippet from the current Drupal\simplenews\Mail\Mailer class:

  public function sendTest(NodeInterface $node, array $test_addresses) {
    foreach ($test_addresses as $mail) {
      $subscriber = simplenews_subscriber_load_by_mail($mail);
      if (!$subscriber) {
        $subscriber = Subscriber::create(array());
        $subscriber->setUserId(0);
        $subscriber->setMail($mail);
      }
      ...

You can work around this by just subscribing the user of the test address to a newsletter and then unsubscribing again. That will create a Subscriber entity so simplenews_subscriber_load_by_mail() will return it.

Not sure which sort of fix is adequate:

  1. Create subscriber entities for all users on install, or
  2. Try to load a user with the given address and use it for the stub Subscriber entity
arla’s picture

Status: Active » Needs review
StatusFileSize
new4.81 KB

Using user_load_by_mail() and a new convenience method Subscriber::fillFromUser().

Status: Needs review » Needs work

The last submitted patch, 3: test-send-user-2421477-3.patch, failed testing.

miro_dietiker’s picture

Direction makes sense... but there are test fails introduced. :-)

arla’s picture

Status: Needs work » Needs review

Didn't get the fail locally. Retested and now it seems fine?

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Mail/Mailer.php
@@ -316,10 +316,11 @@ class Mailer implements MailerInterface {
+          // Create a stub subscriber. Use values from the user having the given
+          // address, or if there is no such user, the current (anonymous) user.
+          $user = user_load_by_mail($mail) ?: \Drupal::currentUser();
+          $subscriber = Subscriber::create()->fillFromAccount($user);

i'm not sure the fallback here is correct.

If you select some arbitrary email, I'm not sure if the desired behavior is that it really uses your account if none is available that uses that mail?

I think the fallback should be the current code, basically.

arla’s picture

Status: Needs work » Needs review
StatusFileSize
new1.02 KB
new4.89 KB

It wouldn't use "your account" because a few lines earlier it switches to anonymous. But on the other hand, it then sets the mail to empty, which is not very good for sending ;)

Fixed and tested manually.

berdir’s picture

Status: Needs review » Fixed

That makes more sense. Committed.

  • Berdir committed f9d5a35 on 8.x-1.x authored by Arla
    Issue #2421477 by Arla: Test emails should use user context
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.