feat: add mailing templates and mail the confirmed teamers of selected assignments

This commit is contained in:
fromme committed 2026-11-11 14:02:55 +01:00
1 parent 6c89f1b8a9
commit e285646bb2
57 files changed
+3525 -31

No files matched your search

@@ -0,0 +1,168 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service\Teamer;
use App\Entity\MailingTemplate;
use App\Entity\Upload;
use App\Entity\User;
use App\Model\UploadDto;
use App\Model\UploadSessionDto;
use App\Repository\MailingRepository;
use App\Service\Teamer\MailingAttachmentHandler;
use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface;
use Flagception\Manager\FeatureManagerInterface;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
#[AllowMockObjectsWithoutExpectations]
class MailingAttachmentHandlerTest extends TestCase
{
private const int MB = 1024 * 1024;
private UploadSessionDto $uploadSession;
private UploadHandler&MockObject $uploadHandler;
private MailingRepository&MockObject $mailingRepository;
private EntityManagerInterface&MockObject $entityManager;
private MailingAttachmentHandler $handler;
/**
* @var string[] filenames the orphanage no longer holds
*/
private array $goneFromOrphanage = [];
protected function setUp(): void
{
$this->uploadSession = new UploadSessionDto();
$this->uploadHandler = $this->createMock(UploadHandler::class);
$this->uploadHandler->method('getUploadSession')->willReturnCallback(fn () => $this->uploadSession);
$this->uploadHandler->method('findOrphanageFilepath')->willReturnCallback(
fn (string $type, string $filename) => in_array($filename, $this->goneFromOrphanage, true) ? null : '/orphanage/'.$filename
);
$this->mailingRepository = $this->createMock(MailingRepository::class);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->handler = new MailingAttachmentHandler(
$this->uploadHandler,
$this->mailingRepository,
$this->entityManager,
$this->createStub(FeatureManagerInterface::class),
);
}
public function testAttachmentsWithinTheLimitPass(): void
{
$this->drop('a.pdf', 4 * self::MB, Upload::TYPE_MAILING_ATTACHMENT);
$this->assertNull($this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 6 * self::MB));
}
public function testStoredAndPendingFilesTogetherMayNotPassTheLimit(): void
{
$this->drop('a.pdf', 4 * self::MB, Upload::TYPE_MAILING_ATTACHMENT);
$error = $this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 6 * self::MB + 1);
$this->assertSame(
'Die Anhänge sind zusammen 10,1 MB groß, erlaubt sind 10,0 MB - sonst kommt die Mail nicht an',
$error
);
}
/**
* The template form and the compose page drop into the same session, told apart by type
* only. One page's files must not count against - or be saved by - the other.
*/
public function testOnlyThePendingFilesOfTheGivenTypeCount(): void
{
$this->drop('template.pdf', 5 * self::MB, Upload::TYPE_MAILING_TEMPLATE);
$this->drop('mailing.pdf', 1 * self::MB, Upload::TYPE_MAILING_ATTACHMENT);
$this->assertSame(1 * self::MB, $this->handler->getPendingSize(Upload::TYPE_MAILING_ATTACHMENT));
$this->assertNull($this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 9 * self::MB));
}
public function testAPendingFileGoneFromTheOrphanageIsNamedAndDropped(): void
{
$this->drop('a.pdf', self::MB, Upload::TYPE_MAILING_ATTACHMENT, 'Packliste.pdf');
$this->goneFromOrphanage = ['a.pdf'];
$this->uploadHandler
->expects($this->once())
->method('removeUploadFromSession')
->with('uuid-a.pdf')
;
$this->assertSame(
'Bitte lade Packliste.pdf noch einmal hoch, sie liegt nicht mehr bereit',
$this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 0)
);
}
public function testFilesTickedForRemovalNoLongerCountTowardsTheTemplateLimit(): void
{
$kept = $this->stored(6 * self::MB);
$removed = $this->stored(3 * self::MB);
$template = (new MailingTemplate())->addAttachment($kept)->addAttachment($removed);
$this->drop('a.pdf', 3 * self::MB, Upload::TYPE_MAILING_TEMPLATE);
$this->assertNotNull($this->handler->validateTemplateAttachments($template, []));
$this->assertNull($this->handler->validateTemplateAttachments($template, [$removed]));
}
/**
* A sent mailing lists the files it went out with; taking one off the template must not take
* it out of that record.
*/
public function testAFileASentMailingListsIsDetachedButKept(): void
{
$upload = $this->stored(self::MB);
$template = (new MailingTemplate())->addAttachment($upload);
$this->mailingRepository->method('hasAttachment')->willReturn(true);
$this->entityManager->expects($this->never())->method('remove');
$this->uploadHandler->expects($this->never())->method('removeUploadedFile');
$this->handler->saveTemplateAttachments($template, [$upload], new User());
$this->assertTrue($template->getAttachments()->isEmpty());
$this->assertNull($upload->getMailingTemplate());
}
public function testAFileNoMailingListsIsDeleted(): void
{
$upload = $this->stored(self::MB);
$template = (new MailingTemplate())->addAttachment($upload);
$this->mailingRepository->method('hasAttachment')->willReturn(false);
$this->entityManager->expects($this->once())->method('remove')->with($upload);
$this->uploadHandler->expects($this->once())->method('removeUploadedFile')->with($upload);
$this->handler->saveTemplateAttachments($template, [$upload], new User());
}
private function drop(string $filename, int $size, string $type, ?string $originalFilename = null): void
{
$this->uploadSession->addUpload(new UploadDto(
'uuid-'.$filename,
$filename,
$originalFilename ?? $filename,
'application/pdf',
$size,
$type
));
}
private function stored(int $size): Upload
{
return (new Upload())
->setType(Upload::TYPE_MAILING_TEMPLATE)
->setFilename(uniqid().'.pdf')
->setOriginalFilename('Datei.pdf')
->setSize($size)
;
}
}
@@ -6,9 +6,11 @@ namespace App\Tests\Service\Teamer;
use App\Email\MailBodyRenderer;
use App\Email\Mailer;
use App\Entity\Assignment;
use App\Entity\Teamer;
use App\Entity\User;
use App\Message\SendTeamerMailing;
use App\Model\EmailPathAttachmentDto;
use App\Model\TeamerFilterDto;
use App\Model\TeamerMailingDto;
use App\Model\TeamerMailingRecipient;
@@ -168,6 +170,83 @@ class TeamerMailingServiceTest extends TestCase
$this->service->sendMailing($this->createMailing('Betreff', 'Nachricht'));
}
public function testResolveAssignmentRecipientsPartitionsTheConfirmedTeamers(): void
{
$eligible = $this->createRecipient('Anna', 'Berg', '[email protected]');
$disabled = $this->createRecipient('Cem', 'Dal', '[email protected]', disabled: true);
$this->teamerRepository
->method('getAssignmentMailingRecipients')
->willReturn([$eligible, $disabled])
;
$recipients = $this->service->resolveAssignmentRecipients([new Assignment()]);
$this->assertSame([$eligible], $recipients->getEligible());
$this->assertSame(1, $recipients->getSkippedDisabled());
}
/**
* By path and nothing else: the queued mail of every recipient has to stay small, the file is
* read only when that mail is sent.
*/
public function testSendMailingAttachesTheFilesByPathToEveryMail(): void
{
$attached = [];
$this->mailer
->expects($this->exactly(2))
->method('createAndSendEmail')
->willReturnCallback(function (array $context, array $options) use (&$attached): void {
$attached[] = array_map(
fn (EmailPathAttachmentDto $attachment) => [$attachment->getPath(), $attachment->getName()],
$options['attachments']
);
})
;
$recipients = (new TeamerMailingRecipientsDto())
->addEligible($this->createRecipient('Anna', 'Berg', '[email protected]'))
->addEligible($this->createRecipient('Bea', 'Ohm', '[email protected]'))
;
$this->service->sendMailing(SendTeamerMailing::fromRecipients('Betreff', 'Nachricht', $recipients, [
['path' => '/uploads/mailing_template/a/a.pdf', 'name' => 'Packliste.pdf'],
]));
$this->assertSame([
[['/uploads/mailing_template/a/a.pdf', 'Packliste.pdf']],
[['/uploads/mailing_template/a/a.pdf', 'Packliste.pdf']],
], $attached);
}
public function testSendMailingWithoutAttachmentsAttachesNothing(): void
{
$this->mailer
->expects($this->once())
->method('createAndSendEmail')
->willReturnCallback(function (array $context, array $options): void {
$this->assertSame([], $options['attachments']);
})
;
$this->service->sendMailing($this->createMailing(
'Betreff',
'Nachricht',
$this->createRecipient('Anna', 'Berg', '[email protected]')
));
}
/**
* A mailing queued before the attachments existed comes out of the queue without the
* property; it has to send as it would have, not fail in the worker.
*/
public function testAMailingQueuedWithoutAttachmentsHasNone(): void
{
$mailing = (new \ReflectionClass(SendTeamerMailing::class))->newInstanceWithoutConstructor();
$this->assertSame([], $mailing->getAttachments());
}
/**
* A preview has to stay on the default relay and the default queue, or composing a
* mailing would wait on the same worker the mailing itself is queued behind.