diff --git a/src/Controller/Administrative/AssignmentMailing/AbstractAssignmentMailingController.php b/src/Controller/Administrative/AssignmentMailing/AbstractAssignmentMailingController.php index ec82037..d8d006a 100644 --- a/src/Controller/Administrative/AssignmentMailing/AbstractAssignmentMailingController.php +++ b/src/Controller/Administrative/AssignmentMailing/AbstractAssignmentMailingController.php @@ -5,9 +5,11 @@ namespace App\Controller\Administrative\AssignmentMailing; use App\Controller\Traits\AuthenticatedUserTrait; use App\Email\MailBodyRenderer; use App\Entity\Upload; +use App\Entity\User; use App\Form\AssignmentMailingAddType; use App\Form\AssignmentMailingType; use App\Model\AssignmentMailingDto; +use App\Model\TeamerMailingRecipientsDto; use App\Service\Teamer\AssignmentMailingDraftHandler; use App\Service\Teamer\AssignmentMailingService; use App\Service\Teamer\MailingAttachmentHandler; @@ -81,8 +83,23 @@ abstract class AbstractAssignmentMailingController extends AbstractController { return [ 'mailingDto' => $mailingDto, - 'recipients' => $this->mailingService->resolveRecipients($mailingDto), 'addForm' => $this->createForm(AssignmentMailingAddType::class)->createView(), + ] + $this->recipientsContext($mailingDto); + } + + /** + * The recipients and the house managers in CC, named on the compose page and in the + * confirmation. + * + * @return array{recipients: TeamerMailingRecipientsDto, houseManagers: User[]} + */ + protected function recipientsContext(AssignmentMailingDto $mailingDto): array + { + $recipients = $this->mailingService->resolveRecipients($mailingDto); + + return [ + 'recipients' => $recipients, + 'houseManagers' => array_merge(...array_values($this->mailingService->resolveHouseManagerCc($mailingDto, $recipients))), ]; } diff --git a/src/Controller/Administrative/AssignmentMailing/ConfirmController.php b/src/Controller/Administrative/AssignmentMailing/ConfirmController.php index 78b77bf..e719f80 100644 --- a/src/Controller/Administrative/AssignmentMailing/ConfirmController.php +++ b/src/Controller/Administrative/AssignmentMailing/ConfirmController.php @@ -26,7 +26,6 @@ class ConfirmController extends AbstractAssignmentMailingController 'valid' => $form->isSubmitted() && $form->isValid(), 'attachmentError' => $this->mailingService->validateAttachments($mailingDto), 'attachmentSize' => $this->mailingService->getAttachmentSize($mailingDto), - 'recipients' => $this->mailingService->resolveRecipients($mailingDto), - ] + $this->attachmentsContext($mailingDto)); + ] + $this->recipientsContext($mailingDto) + $this->attachmentsContext($mailingDto)); } } diff --git a/src/Email/Mailer.php b/src/Email/Mailer.php index dca5862..5f5b500 100644 --- a/src/Email/Mailer.php +++ b/src/Email/Mailer.php @@ -104,6 +104,11 @@ class Mailer $email->replyTo($config['reply_to']); } + // on every mail of a multi-recipient 'to', like the rest of the mail + if ([] !== $config['cc']) { + $email->cc(...$config['cc']); + } + foreach ($config['attachments'] as $attachment) { /* @var EmailAttachmentInterface $attachment */ $attachment->attachTo($email); @@ -117,13 +122,14 @@ class Mailer public function send(TemplatedEmail $email): void { $recipients = array_map(fn (Address $address) => $address->toString(), $email->getTo()); + $cc = array_map(fn (Address $address) => $address->toString(), $email->getCc()); try { $this->mailer->send($email); $this->logger->info('Send email', [ 'to' => $recipients, 'subject' => $email->getSubject(), - ]); + ] + ([] !== $cc ? ['cc' => $cc] : [])); } catch (TransportExceptionInterface $e) { $this->logger->error('Email could not be sent', [ 'to' => $recipients, @@ -146,6 +152,7 @@ class Mailer 'transport' => null, 'bus_transport' => null, 'reply_to' => null, + 'cc' => [], ]) ->setRequired([ 'template', @@ -159,6 +166,7 @@ class Mailer ->setAllowedTypes('transport', ['null', 'string']) ->setAllowedTypes('bus_transport', ['null', 'string']) ->setAllowedTypes('reply_to', ['null', 'string']) + ->setAllowedTypes('cc', 'string[]') ; return $resolver->resolve($options); diff --git a/src/Message/SendTeamerMailing.php b/src/Message/SendTeamerMailing.php index cef19a0..c61e15a 100644 --- a/src/Message/SendTeamerMailing.php +++ b/src/Message/SendTeamerMailing.php @@ -19,12 +19,15 @@ use App\Model\TeamerMailingRecipientsDto; * With $listColleagues every mail ends with the names and addresses of the other recipients, * taken from the same snapshot. $replyTo is decided when the mailing is queued, so a deploy * that changes it does not reroute answers to a mailing already on its way. + * + * A recipient's 'cc' holds the addresses that get their mail in copy - the house managers of an + * assignment mailing, each on one mail only, see AssignmentMailingService::resolveHouseManagerCc(). */ class SendTeamerMailing { /** - * @param array $recipients - * @param array $attachments + * @param array}> $recipients + * @param array $attachments */ public function __construct( private readonly string $subject, @@ -38,6 +41,7 @@ class SendTeamerMailing /** * @param array $attachments + * @param array> $cc teamer id => addresses */ public static function fromRecipients( string $subject, @@ -46,6 +50,7 @@ class SendTeamerMailing array $attachments = [], bool $listColleagues = false, ?string $replyTo = null, + array $cc = [], ): self { $snapshot = []; @@ -60,6 +65,7 @@ class SendTeamerMailing 'firstName' => $recipient->getFirstName(), 'lastName' => $recipient->getLastName(), 'fullName' => $recipient->getFullName(), + 'cc' => $cc[$recipient->getId()] ?? [], ]; } @@ -77,7 +83,7 @@ class SendTeamerMailing } /** - * @return array + * @return array}> */ public function getRecipients(): array { diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index 0cf7460..9bcaf62 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -122,13 +122,13 @@ class UserRepository extends ServiceEntityRepository } /** - * The house managers the nightly reminder may write to: not deleted and not blocked - a blocked - * account cannot log in to do what it is reminded of. Hotel codes and muted notifications are - * the caller's to check. + * The house managers still in office: not deleted and not blocked. The nightly reminder writes + * to them (a blocked account cannot log in to do what it is reminded of) and an assignment + * mailing puts them in CC. Hotel codes and muted notifications are the caller's to check. * * @return User[] */ - public function findHouseManagersToRemind(): array + public function findActiveHouseManagers(): array { $qb = $this->createQueryBuilder('user'); diff --git a/src/Service/Cron/FeedbackReminderService.php b/src/Service/Cron/FeedbackReminderService.php index b18dd8b..144bd0b 100644 --- a/src/Service/Cron/FeedbackReminderService.php +++ b/src/Service/Cron/FeedbackReminderService.php @@ -41,7 +41,7 @@ class FeedbackReminderService $today = ($today ?? new \DateTimeImmutable('today'))->setTime(0, 0); $count = 0; - foreach ($this->userRepository->findHouseManagersToRemind() as $houseManager) { + foreach ($this->userRepository->findActiveHouseManagers() as $houseManager) { if (true === $houseManager->isMuteNotifications() || [] === $houseManager->getHotelCodes()) { continue; } diff --git a/src/Service/Teamer/AssignmentMailingService.php b/src/Service/Teamer/AssignmentMailingService.php index 7108a5b..4f857b9 100644 --- a/src/Service/Teamer/AssignmentMailingService.php +++ b/src/Service/Teamer/AssignmentMailingService.php @@ -8,6 +8,7 @@ use App\Entity\User; use App\Message\SendTeamerMailing; use App\Model\AssignmentMailingDto; use App\Model\TeamerMailingRecipientsDto; +use App\Repository\UserRepository; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\Messenger\MessageBusInterface; @@ -15,8 +16,8 @@ use Symfony\Component\Messenger\MessageBusInterface; /** * Sends a mailing to the teamers confirmed on a set of assignments. Recipients, placeholders and * the fan-out in the worker are the teamer mailing's, see TeamerMailingService; what this adds is - * the attachments, the list of colleagues at the end of each mail and the Mailing record of what - * went out. + * the attachments, the list of colleagues at the end of each mail, the house managers in CC and + * the Mailing record of what went out. */ class AssignmentMailingService { @@ -26,6 +27,7 @@ class AssignmentMailingService private readonly EntityManagerInterface $entityManager, private readonly MessageBusInterface $messageBus, private readonly LoggerInterface $logger, + private readonly UserRepository $userRepository, private readonly string $assignmentMailingReplyTo, ) { } @@ -35,6 +37,73 @@ class AssignmentMailingService return $this->teamerMailingService->resolveAssignmentRecipients($mailingDto->getAssignments()); } + /** + * The house managers who get the mailing in CC, by the teamer whose mail carries them. + * + * Every house manager of a house on the selected assignments (User::hasHotelCodeMatch(), the + * rule that gives them access to the house's teamers) is in CC on exactly one mail: that of + * the first recipient confirmed on an assignment of their house, in the recipients' order. + * Muted notifications do not count, this is no notification. Left out is a house manager who + * gets the mailing anyway, as one of the teamers or under one of their addresses. + * + * @return array teamer id => house managers + */ + public function resolveHouseManagerCc(AssignmentMailingDto $mailingDto, TeamerMailingRecipientsDto $recipients): array + { + $recipientEmails = []; + $hotelCodes = []; + + foreach ($recipients->getEligible() as $recipient) { + $recipientEmails[] = mb_strtolower((string) $recipient->getEmail()); + // in the recipients' order, which decides whose mail carries a house manager + $hotelCodes[$recipient->getId()] = []; + } + + foreach ($mailingDto->getAssignments() as $assignment) { + $hotelCode = (string) $assignment->getDestination()?->getHotelCode(); + + if ('' === $hotelCode) { + continue; + } + + foreach ($assignment->getConfirmedDispositions() as $disposition) { + $teamerId = $disposition->getTeamer()?->getId(); + + if (null !== $teamerId && isset($hotelCodes[$teamerId])) { + $hotelCodes[$teamerId][] = $hotelCode; + } + } + } + + $cc = []; + + foreach ($this->userRepository->findActiveHouseManagers() as $houseManager) { + $email = (string) $houseManager->getEmail(); + $teamerId = $houseManager->getTeamer()?->getId(); + + if ( + '' === trim($email) + || in_array(mb_strtolower($email), $recipientEmails, true) + || (null !== $teamerId && isset($hotelCodes[$teamerId])) + ) { + continue; + } + + foreach ($hotelCodes as $recipientId => $codes) { + foreach ($codes as $hotelCode) { + if ($houseManager->hasHotelCodeMatch($hotelCode)) { + $cc[$recipientId][] = $houseManager; + + continue 3; + } + } + } + } + + // in the recipients' order, as the compose page names them + return array_filter(array_replace(array_fill_keys(array_keys($hotelCodes), []), $cc)); + } + /** * The template's files plus the ones dropped for this mailing, in bytes. */ @@ -118,6 +187,11 @@ class AssignmentMailingService $this->entityManager->persist($mailing); $this->entityManager->flush(); + $cc = array_map( + fn (array $houseManagers) => array_map(fn (User $houseManager) => (string) $houseManager->getEmail(), $houseManagers), + $this->resolveHouseManagerCc($mailingDto, $recipients) + ); + $this->messageBus->dispatch(SendTeamerMailing::fromRecipients( (string) $mailingDto->getSubject(), (string) $mailingDto->getMessage(), @@ -125,7 +199,8 @@ class AssignmentMailingService $this->attachmentHandler->toMessageAttachments($attachments), listColleagues: true, // answers go to the travel management inbox, not to the app's sender address - replyTo: $this->assignmentMailingReplyTo + replyTo: $this->assignmentMailingReplyTo, + cc: $cc )); $this->logger->info('Send assignment mailing', [ @@ -134,6 +209,7 @@ class AssignmentMailingService 'assignment_ids' => $mailing->getAssignments()->map(fn ($assignment) => $assignment->getId())->toArray(), 'attachments' => count($attachments), 'recipients' => $mailing->getRecipientCount(), + 'house_managers_cc' => count($cc, COUNT_RECURSIVE) - count($cc), ]); return $mailing; diff --git a/src/Service/Teamer/TeamerMailingService.php b/src/Service/Teamer/TeamerMailingService.php index 0346b5e..d740604 100644 --- a/src/Service/Teamer/TeamerMailingService.php +++ b/src/Service/Teamer/TeamerMailingService.php @@ -133,7 +133,9 @@ class TeamerMailingService self::TRANSPORT, $this->mailingBusTransport, $mailing->listsColleagues() ? self::colleaguesOf($recipients, $index) : [], - $mailing->getReplyTo() + $mailing->getReplyTo(), + // a mailing queued before the CC has none + $recipient['cc'] ?? [] ); } @@ -254,6 +256,7 @@ class TeamerMailingService * * @param EmailAttachmentInterface[] $attachments * @param array $colleagues + * @param list $cc */ private function sendTo( string $email, @@ -264,6 +267,7 @@ class TeamerMailingService ?string $busTransport = null, array $colleagues = [], ?string $replyTo = null, + array $cc = [], ): void { // $bodyHtml is already rendered and escaped mail HTML, see MailBodyRenderer - // email/teamer_mailing.html.twig prints it with |raw. The colleagues are plain values, @@ -279,6 +283,7 @@ class TeamerMailingService 'transport' => $transport, 'bus_transport' => $busTransport, 'reply_to' => $replyTo, + 'cc' => $cc, ]); } } diff --git a/templates/administrative/assignment_mailing/_assignments.html.twig b/templates/administrative/assignment_mailing/_assignments.html.twig index 86a3e9a..0233a2a 100644 --- a/templates/administrative/assignment_mailing/_assignments.html.twig +++ b/templates/administrative/assignment_mailing/_assignments.html.twig @@ -16,6 +16,12 @@ {{ recipients.eligible|map(recipient => recipient.fullName)|join(', ') }} {% endif %} + {% if houseManagers is not empty %} +
+ In Kopie (Hausleitung): + {{ include('administrative/assignment_mailing/_house_managers.html.twig') }} +
+ {% endif %}
    {% for assignment in mailingDto.assignments %} diff --git a/templates/administrative/assignment_mailing/_house_managers.html.twig b/templates/administrative/assignment_mailing/_house_managers.html.twig new file mode 100644 index 0000000..a619113 --- /dev/null +++ b/templates/administrative/assignment_mailing/_house_managers.html.twig @@ -0,0 +1,2 @@ +{# A house manager without a name on their account is named by their address. #} +{{ houseManagers|map(houseManager => houseManager.fullName|trim ?: houseManager.email)|join(', ') }} diff --git a/templates/administrative/assignment_mailing/modal_confirm.html.twig b/templates/administrative/assignment_mailing/modal_confirm.html.twig index 801d3b3..8a6c1b2 100644 --- a/templates/administrative/assignment_mailing/modal_confirm.html.twig +++ b/templates/administrative/assignment_mailing/modal_confirm.html.twig @@ -21,6 +21,9 @@
    Die Mail geht an {{ recipients.eligibleCount }} Teamer:innen auf {{ mailingDto.assignments|length }} {{ mailingDto.assignments|length == 1 ? 'Einsatz' : 'Einsätzen' }}. + {% if houseManagers is not empty %} + In Kopie geht sie an die Hausleitung: {{ include('administrative/assignment_mailing/_house_managers.html.twig') }}. + {% endif %} Das lässt sich nicht zurücknehmen.
    diff --git a/tests/Controller/Administrative/AssignmentMailing/AssignmentMailingControllerTest.php b/tests/Controller/Administrative/AssignmentMailing/AssignmentMailingControllerTest.php index 43a3824..96f4b69 100644 --- a/tests/Controller/Administrative/AssignmentMailing/AssignmentMailingControllerTest.php +++ b/tests/Controller/Administrative/AssignmentMailing/AssignmentMailingControllerTest.php @@ -18,6 +18,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\DomCrawler\Crawler; +use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; /** @@ -254,6 +255,69 @@ class AssignmentMailingControllerTest extends WebTestCase $this->assertSame(['Anna Muster (anna.muster@example.com)'], $this->colleaguesIn('cem.muster@example.com')); } + /** + * Each house manager once per mailing, on the mail of the first recipient of their house - + * muted or not - and never one of another house or a blocked one. + */ + public function testHouseManagersAreInCcOnOneMailOfTheirHouse(): void + { + // codes of no real house: other tests leave house managers of real ones in db_test + $schwendi = $this->assignment('Schwendi', 'XQSWE'); + $rotbach = $this->assignment('Rotbach', 'XQR'); + $this->place($this->teamer('Anna'), $schwendi, Disposition::STATUS_CONFIRMED); + $this->place($this->teamer('Cem'), $schwendi, Disposition::STATUS_CONFIRMED); + $this->place($this->teamer('Dora'), $rotbach, Disposition::STATUS_CONFIRMED); + $this->houseManager('Greta', ['XQR']); + $this->houseManager('Hugo', ['XQS']); + $this->houseManager('Hanna', ['XQS'])->setMuteNotifications(true); + $this->houseManager('Paul', ['XQP']); + $this->houseManager('Dieter', ['XQS'])->setDisabledAt(new \DateTimeImmutable()); + $this->login('ROLE_MANAGER'); + + $crawler = $this->startMailing($schwendi, $rotbach); + + $this->assertStringContainsString( + 'In Kopie (Hausleitung): Hugo Hausleitung, Hanna Hausleitung, Greta Hausleitung', + $crawler->filter('#mailing-assignments')->text() + ); + + $this->client->request('POST', self::COMPOSE_URL.'/send', [ + 'assignment_mailing' => $this->mailingFields($crawler, 'Treffpunkt', 'Hallo'), + ]); + + $this->assertEmailCount(3); + $this->assertSame(['hugo.hausleitung@example.com', 'hanna.hausleitung@example.com'], $this->ccIn('anna.muster@example.com')); + $this->assertSame([], $this->ccIn('cem.muster@example.com')); + $this->assertSame(['greta.hausleitung@example.com'], $this->ccIn('dora.muster@example.com')); + } + + /** + * Placed on the travel as a teamer, or writing under the address of one, the house manager has + * the mail already. + */ + public function testAHouseManagerWhoIsARecipientIsNotInCc(): void + { + $assignment = $this->assignment('Schwendi', 'XQS'); + $anna = $this->teamer('Anna'); + $this->place($anna, $assignment, Disposition::STATUS_CONFIRMED); + $this->place($this->teamer('Cem'), $assignment, Disposition::STATUS_CONFIRMED); + $this->houseManager('Hugo', ['XQS'])->setTeamer($anna); + $this->houseManager('Hanna', ['XQS'])->setEmail('cem.muster@example.com'); + $this->login('ROLE_MANAGER'); + + $crawler = $this->startMailing($assignment); + + $this->assertStringNotContainsString('In Kopie', $crawler->filter('#mailing-assignments')->text()); + + $this->client->request('POST', self::COMPOSE_URL.'/send', [ + 'assignment_mailing' => $this->mailingFields($crawler, 'Treffpunkt', 'Hallo'), + ]); + + $this->assertEmailCount(2); + $this->assertSame([], $this->ccIn('anna.muster@example.com')); + $this->assertSame([], $this->ccIn('cem.muster@example.com')); + } + public function testColleagueNamesAreEscaped(): void { $assignment = $this->assignment('Schwendi'); @@ -354,6 +418,22 @@ class AssignmentMailingControllerTest extends WebTestCase $this->fail(sprintf('No mail to %s', $to)); } + /** + * The CC addresses of the mail sent to the given address. + * + * @return string[] + */ + private function ccIn(string $to): array + { + foreach ($this->getMailerMessages() as $message) { + if ($message instanceof Email && $to === $message->getTo()[0]->getAddress()) { + return array_map(fn (Address $address) => $address->getAddress(), $message->getCc()); + } + } + + $this->fail(sprintf('No mail to %s', $to)); + } + private function startMailing(Assignment ...$assignments): Crawler { $this->entityManager->flush(); @@ -399,12 +479,12 @@ class AssignmentMailingControllerTest extends WebTestCase $this->client->loginUser($user); } - private function assignment(string $hotel): Assignment + private function assignment(string $hotel, string $hotelCode = 'DKI'): Assignment { $destination = (new Destination()) ->setProduct('Davos Klosters') ->setHotel($hotel) - ->setHotelCode('DKI') + ->setHotelCode($hotelCode) ->setHotelBusProId(1) ->setCountry('CH') ->setDateFrom(new \DateTimeImmutable('+7 days')) @@ -424,6 +504,25 @@ class AssignmentMailingControllerTest extends WebTestCase return $assignment; } + /** + * @param string[] $hotelCodes + */ + private function houseManager(string $firstName, array $hotelCodes): User + { + $user = (new User()) + ->setFirstName($firstName) + ->setLastName('Hausleitung') + ->setEmail(strtolower($firstName).'.hausleitung@example.com') + ->setRoles(['ROLE_HOUSE_MANAGER']) + ->setHotelCodes($hotelCodes) + ->setBusProAddressId(1) + ->setBusProPersonId(1) + ; + $this->entityManager->persist($user); + + return $user; + } + private function teamer(string $firstName): Teamer { $teamer = (new Teamer()) diff --git a/tests/Email/EmailTextMailRenderingTest.php b/tests/Email/EmailTextMailRenderingTest.php index 381d815..3e08b8e 100644 --- a/tests/Email/EmailTextMailRenderingTest.php +++ b/tests/Email/EmailTextMailRenderingTest.php @@ -85,6 +85,7 @@ class EmailTextMailRenderingTest extends KernelTestCase 'transport' => null, 'bus_transport' => null, 'reply_to' => null, + 'cc' => [], ]); $html = $email->getHtmlBody(); diff --git a/tests/Email/MailerTest.php b/tests/Email/MailerTest.php index a05bb18..381651a 100644 --- a/tests/Email/MailerTest.php +++ b/tests/Email/MailerTest.php @@ -50,6 +50,7 @@ class MailerTest extends KernelTestCase 'transport' => $transport, 'bus_transport' => null, 'reply_to' => null, + 'cc' => [], ]); } } diff --git a/tests/Repository/HouseManagerReminderQueryTest.php b/tests/Repository/HouseManagerReminderQueryTest.php index b7c8b5a..0adb6b2 100644 --- a/tests/Repository/HouseManagerReminderQueryTest.php +++ b/tests/Repository/HouseManagerReminderQueryTest.php @@ -73,7 +73,7 @@ class HouseManagerReminderQueryTest extends KernelTestCase /** @var UserRepository $repository */ $repository = $this->entityManager->getRepository(User::class); - $reminded = $repository->findHouseManagersToRemind(); + $reminded = $repository->findActiveHouseManagers(); $this->assertContains($active, $reminded); $this->assertNotContains($blocked, $reminded); diff --git a/tests/Service/Cron/FeedbackReminderServiceTest.php b/tests/Service/Cron/FeedbackReminderServiceTest.php index b944212..d199982 100644 --- a/tests/Service/Cron/FeedbackReminderServiceTest.php +++ b/tests/Service/Cron/FeedbackReminderServiceTest.php @@ -53,7 +53,7 @@ class FeedbackReminderServiceTest extends TestCase { $houseManager = $this->houseManager('anna@example.org', ['DKI']); - $this->userRepository->method('findHouseManagersToRemind')->willReturn([$houseManager]); + $this->userRepository->method('findActiveHouseManagers')->willReturn([$houseManager]); $this->destinationRepository ->expects($this->once()) ->method('findPendingForHouseManager') @@ -66,7 +66,7 @@ class FeedbackReminderServiceTest extends TestCase public function testMutedHouseManagersAndThoseWithoutHotelCodesAreSkipped(): void { - $this->userRepository->method('findHouseManagersToRemind')->willReturn([ + $this->userRepository->method('findActiveHouseManagers')->willReturn([ $this->houseManager('muted@example.org', ['DKI'])->setMuteNotifications(true), $this->houseManager('nohouse@example.org', []), ]); @@ -83,7 +83,7 @@ class FeedbackReminderServiceTest extends TestCase */ public function testNoMailOnANightNothingIsDue(): void { - $this->userRepository->method('findHouseManagersToRemind')->willReturn([$this->houseManager('anna@example.org', ['DKI'])]); + $this->userRepository->method('findActiveHouseManagers')->willReturn([$this->houseManager('anna@example.org', ['DKI'])]); $this->destinationRepository->method('findPendingForHouseManager')->willReturn([$this->row($this->travel(10))]); $this->mailer->expects($this->never())->method('createAndSendEmail'); @@ -98,7 +98,7 @@ class FeedbackReminderServiceTest extends TestCase $due = $this->travel(14); $quiet = $this->travel(10); - $this->userRepository->method('findHouseManagersToRemind')->willReturn([$this->houseManager('anna@example.org', ['DKI'])]); + $this->userRepository->method('findActiveHouseManagers')->willReturn([$this->houseManager('anna@example.org', ['DKI'])]); $this->destinationRepository->method('findPendingForHouseManager')->willReturn([$this->row($quiet), $this->row($due)]); $this->mailer ->expects($this->once()) @@ -124,7 +124,7 @@ class FeedbackReminderServiceTest extends TestCase $untouched = $this->travel(3); // ended 2026-11-17 $submitted = $this->travel(1); // ended 2026-11-19 - $this->userRepository->method('findHouseManagersToRemind')->willReturn([$this->houseManager('anna@example.org', ['DKI'])]); + $this->userRepository->method('findActiveHouseManagers')->willReturn([$this->houseManager('anna@example.org', ['DKI'])]); $this->destinationRepository->method('findPendingForHouseManager')->willReturn([ $this->row($beforeGoLive, null, 2), $this->row($draft, 'draft', 0), diff --git a/tests/Service/Teamer/TeamerMailingServiceTest.php b/tests/Service/Teamer/TeamerMailingServiceTest.php index 6e14306..a501c8c 100644 --- a/tests/Service/Teamer/TeamerMailingServiceTest.php +++ b/tests/Service/Teamer/TeamerMailingServiceTest.php @@ -338,6 +338,52 @@ class TeamerMailingServiceTest extends TestCase $this->assertSame(['mia@example.org', 'mia@example.org'], $replyTo); } + public function testSendMailingPutsEachRecipientsCcOnTheirMailOnly(): void + { + $cc = []; + $this->mailer + ->expects($this->exactly(2)) + ->method('createAndSendEmail') + ->willReturnCallback(function (array $context, array $options) use (&$cc): void { + $cc[$options['to']] = $options['cc']; + }) + ; + + $anna = $this->createRecipient('Anna', 'Berg', 'anna@example.org'); + $recipients = (new TeamerMailingRecipientsDto()) + ->addEligible($anna) + ->addEligible($this->createRecipient('Bea', 'Ohm', 'bea@example.org')) + ; + + $this->service->sendMailing(SendTeamerMailing::fromRecipients( + 'Betreff', + 'Nachricht', + $recipients, + cc: [$anna->getId() => ['hugo@example.org', 'hanna@example.org']] + )); + + $this->assertSame([ + 'anna@example.org' => ['hugo@example.org', 'hanna@example.org'], + 'bea@example.org' => [], + ], $cc); + } + + public function testAMailingQueuedWithoutTheCcSendsNone(): void + { + $this->mailer + ->expects($this->once()) + ->method('createAndSendEmail') + ->willReturnCallback(function (array $context, array $options): void { + $this->assertSame([], $options['cc']); + }) + ; + + // the snapshot as it was queued before the 'cc' key existed + $this->service->sendMailing(new SendTeamerMailing('Betreff', 'Nachricht', [ + ['email' => 'anna@example.org', 'firstName' => 'Anna', 'lastName' => 'Berg', 'fullName' => 'Anna Berg'], + ])); + } + /** * 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.