Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Email Failed regular mark email as failed #11285

Open
wants to merge 3 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions app/bundles/EmailBundle/EventListener/EmailSubscriber.php
Expand Up @@ -114,11 +114,9 @@ public function onEmailFailed(Events\QueueEmailEvent $event)
if (isset($message->leadIdHash)) {
$stat = $this->emailModel->getEmailStatus($message->leadIdHash);

if (null !== $stat) {
$reason = $this->translator->trans('mautic.email.dnc.failed', [
'%subject%' => EmojiHelper::toShort($message->getSubject()),
]);
$this->emailModel->setDoNotContact($stat, $reason);
if (null !== $stat && true !== $stat->getIsFailed()) {
$stat->setIsFailed(true);
$this->emailModel->getStatRepository()->saveEntity($stat);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/bundles/EmailBundle/Swiftmailer/Message/MauticMessage.php
Expand Up @@ -14,6 +14,11 @@ class MauticMessage extends \Swift_Message
*/
protected $attachments = [];

/**
* @var string|int
*/
public $leadIdHash;

/**
* Create a new Message.
*
Expand Down
Expand Up @@ -11,6 +11,7 @@
use Mautic\EmailBundle\Event\QueueEmailEvent;
use Mautic\EmailBundle\EventListener\EmailSubscriber;
use Mautic\EmailBundle\Model\EmailModel;
use Mautic\EmailBundle\Swiftmailer\Message\MauticMessage;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Translation\TranslatorInterface;

Expand Down Expand Up @@ -42,7 +43,7 @@ final class EmailSubscriberTest extends \PHPUnit\Framework\TestCase
private $em;

/**
* @var MockObject|\Swift_Message
* @var MockObject|MauticMessage
*/
private $mockSwiftMessage;

Expand All @@ -60,7 +61,7 @@ protected function setup(): void
$this->emailModel = $this->createMock(EmailModel::class);
$this->translator = $this->createMock(TranslatorInterface::class);
$this->em = $this->createMock(EntityManager::class);
$this->mockSwiftMessage = $this->createMock(\Swift_Message::class);
$this->mockSwiftMessage = $this->createMock(MauticMessage::class);
$this->subscriber = new EmailSubscriber($this->ipLookupHelper, $this->auditLogModel, $this->emailModel, $this->translator, $this->em);
}

Expand Down Expand Up @@ -101,4 +102,28 @@ public function testOnEmailResendWhenShouldNotTryAgain(): void
$this->subscriber->onEmailResend($queueEmailEvent);
$this->assertFalse($queueEmailEvent->shouldTryAgain());
}

public function testOnEmailFailed(): void
{
$this->mockSwiftMessage->leadIdHash = 'idhash';
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do something like this:

        $message               = new class() extends \Swift_Message {
            public $leadIdHash = 'some-hash';
        };

Instead of modifying the actual class to make a test work?


$queueEmailEvent = new QueueEmailEvent($this->mockSwiftMessage);

$stat = new Stat();

$this->emailModel->expects($this->once())
->method('getEmailStatus')
->willReturn($stat);

$this->emailModel->expects($this->once())
->method('getStatRepository')
->willReturn(new class() {
public function saveEntity(Stat $stat): void
{
}
});

$this->subscriber->onEmailFailed($queueEmailEvent);
$this->assertTrue($stat->isFailed());
}
}