feat: list the other recipients and reply to the sender in assignment mailings
This commit is contained in:
1 parent
e0496dc61f
commit
f45ac6ac0b
15 files changed
+311
-19
No files matched your search
@@ -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\Email;
|
||||
|
||||
/**
|
||||
* The assignment mailing from picking assignments on the overview to the queued mails, for a
|
||||
@@ -37,6 +38,8 @@ class AssignmentMailingControllerTest extends WebTestCase
|
||||
|
||||
private JobProfile $jobProfile;
|
||||
|
||||
private User $sender;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->client = static::createClient(server: ['HTTPS' => true]);
|
||||
@@ -217,6 +220,10 @@ class AssignmentMailingControllerTest extends WebTestCase
|
||||
$this->assertSame('/administrative/assignment', $this->client->getResponse()->headers->get('HX-Redirect'));
|
||||
$this->assertEmailCount(1);
|
||||
$this->assertSame('Hallo Anna', trim((new Crawler($this->getMailerMessage()->getHtmlBody()))->filter('p')->first()->text()));
|
||||
// answers reach the manager who sent it
|
||||
$this->assertSame($this->sender->getEmail(), $this->getMailerMessage()->getReplyTo()[0]->getAddress());
|
||||
// alone in the mailing, so nobody to list
|
||||
$this->assertStringNotContainsString('Deine Kolleg:innen', (string) $this->getMailerMessage()->getHtmlBody());
|
||||
|
||||
$mailings = $this->entityManager->getRepository(Mailing::class)->findAll();
|
||||
$this->assertCount(1, $mailings);
|
||||
@@ -225,6 +232,72 @@ class AssignmentMailingControllerTest extends WebTestCase
|
||||
$this->assertCount(2, $mailings[0]->getAssignments());
|
||||
}
|
||||
|
||||
/**
|
||||
* The list is the mailing's recipients, not the teamers of each assignment: the body is the
|
||||
* same in every mail.
|
||||
*/
|
||||
public function testEachMailListsTheOtherRecipientsOfTheMailing(): void
|
||||
{
|
||||
$first = $this->assignment('Schwendi');
|
||||
$second = $this->assignment('Alpenrose');
|
||||
$this->place($this->teamer('Anna'), $first, Disposition::STATUS_CONFIRMED);
|
||||
$this->place($this->teamer('Cem'), $second, Disposition::STATUS_CONFIRMED);
|
||||
$this->place($this->teamer('Bea'), $second, Disposition::STATUS_CALLED_OFF);
|
||||
$this->login('ROLE_MANAGER');
|
||||
|
||||
$crawler = $this->startMailing($first, $second);
|
||||
|
||||
$this->client->request('POST', self::COMPOSE_URL.'/send', [
|
||||
'assignment_mailing' => $this->mailingFields($crawler, 'Treffpunkt', 'Hallo'),
|
||||
]);
|
||||
|
||||
$this->assertEmailCount(2);
|
||||
$this->assertSame(['Cem Muster ([email protected])'], $this->colleaguesIn('[email protected]'));
|
||||
$this->assertSame(['Anna Muster ([email protected])'], $this->colleaguesIn('[email protected]'));
|
||||
}
|
||||
|
||||
public function testColleagueNamesAreEscaped(): void
|
||||
{
|
||||
$assignment = $this->assignment('Schwendi');
|
||||
$this->place($this->teamer('Anna'), $assignment, Disposition::STATUS_CONFIRMED);
|
||||
$this->place($this->teamer('Bea')->setLastName('<em>Muster</em>'), $assignment, Disposition::STATUS_CONFIRMED);
|
||||
$this->login('ROLE_MANAGER');
|
||||
|
||||
$crawler = $this->startMailing($assignment);
|
||||
|
||||
$this->client->request('POST', self::COMPOSE_URL.'/send', [
|
||||
'assignment_mailing' => $this->mailingFields($crawler, 'Treffpunkt', 'Hallo'),
|
||||
]);
|
||||
|
||||
$this->assertSame(['Bea <em>Muster</em> ([email protected])'], $this->colleaguesIn('[email protected]'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The composing user is no recipient, so the preview lists all of them - and follows the
|
||||
* list when an assignment is removed.
|
||||
*/
|
||||
public function testThePreviewListsTheRecipientsAsColleagues(): void
|
||||
{
|
||||
$first = $this->assignment('Schwendi');
|
||||
$second = $this->assignment('Alpenrose');
|
||||
$this->place($this->teamer('Anna'), $first, Disposition::STATUS_CONFIRMED);
|
||||
$this->place($this->teamer('Cem'), $second, Disposition::STATUS_CONFIRMED);
|
||||
$this->login('ROLE_MANAGER');
|
||||
|
||||
$crawler = $this->startMailing($first, $second);
|
||||
|
||||
$preview = $crawler->filter('#mailing-preview')->text();
|
||||
$this->assertStringContainsString('Deine Kolleg:innen sind:', $preview);
|
||||
$this->assertStringContainsString('Anna Muster ([email protected])', $preview);
|
||||
$this->assertStringContainsString('Cem Muster ([email protected])', $preview);
|
||||
|
||||
$response = $this->client->request('POST', sprintf('%s/remove/%s', self::COMPOSE_URL, $second->getUuid()));
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
// one recipient left, whose mail lists nobody
|
||||
$this->assertStringNotContainsString('Deine Kolleg:innen', $response->filter('#mailing-preview')->text());
|
||||
}
|
||||
|
||||
public function testAMailingWithoutRecipientsIsNotSent(): void
|
||||
{
|
||||
$assignment = $this->assignment('Schwendi');
|
||||
@@ -265,6 +338,24 @@ class AssignmentMailingControllerTest extends WebTestCase
|
||||
$this->assertResponseRedirects('/teamer');
|
||||
}
|
||||
|
||||
/**
|
||||
* The colleagues section of the mail sent to the given address, one entry per line.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
private function colleaguesIn(string $to): array
|
||||
{
|
||||
foreach ($this->getMailerMessages() as $message) {
|
||||
if ($message instanceof Email && $to === $message->getTo()[0]->getAddress()) {
|
||||
return (new Crawler((string) $message->getHtmlBody()))
|
||||
->filter('h3:contains("Deine Kolleg:innen sind:") + ul li')
|
||||
->each(fn (Crawler $item) => trim($item->text()));
|
||||
}
|
||||
}
|
||||
|
||||
$this->fail(sprintf('No mail to %s', $to));
|
||||
}
|
||||
|
||||
private function startMailing(Assignment ...$assignments): Crawler
|
||||
{
|
||||
$this->entityManager->flush();
|
||||
@@ -308,6 +399,7 @@ class AssignmentMailingControllerTest extends WebTestCase
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->client->loginUser($user);
|
||||
$this->sender = $user;
|
||||
}
|
||||
|
||||
private function assignment(string $hotel): Assignment
|
||||
|
||||
@@ -84,6 +84,7 @@ class EmailTextMailRenderingTest extends KernelTestCase
|
||||
'attachments' => [],
|
||||
'transport' => null,
|
||||
'bus_transport' => null,
|
||||
'reply_to' => null,
|
||||
]);
|
||||
|
||||
$html = $email->getHtmlBody();
|
||||
|
||||
@@ -49,6 +49,7 @@ class MailerTest extends KernelTestCase
|
||||
'attachments' => [],
|
||||
'transport' => $transport,
|
||||
'bus_transport' => null,
|
||||
'reply_to' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -247,6 +247,97 @@ class TeamerMailingServiceTest extends TestCase
|
||||
$this->assertSame([], $mailing->getAttachments());
|
||||
}
|
||||
|
||||
public function testSendMailingListsEveryOtherRecipientAsColleague(): void
|
||||
{
|
||||
$colleagues = [];
|
||||
$this->mailer
|
||||
->expects($this->exactly(3))
|
||||
->method('createAndSendEmail')
|
||||
->willReturnCallback(function (array $context, array $options) use (&$colleagues): void {
|
||||
$colleagues[$options['to']] = array_column($context['colleagues'], 'email', 'fullName');
|
||||
})
|
||||
;
|
||||
|
||||
$recipients = (new TeamerMailingRecipientsDto())
|
||||
->addEligible($this->createRecipient('Anna', 'Berg', '[email protected]'))
|
||||
->addEligible($this->createRecipient('Bea', 'Ohm', '[email protected]'))
|
||||
->addEligible($this->createRecipient('Cem', 'Dal', '[email protected]'))
|
||||
;
|
||||
|
||||
$this->service->sendMailing(SendTeamerMailing::fromRecipients('Betreff', 'Nachricht', $recipients, listColleagues: true));
|
||||
|
||||
$this->assertSame([
|
||||
'[email protected]' => ['Bea Ohm' => '[email protected]', 'Cem Dal' => '[email protected]'],
|
||||
'[email protected]' => ['Anna Berg' => '[email protected]', 'Cem Dal' => '[email protected]'],
|
||||
'[email protected]' => ['Anna Berg' => '[email protected]', 'Bea Ohm' => '[email protected]'],
|
||||
], $colleagues);
|
||||
}
|
||||
|
||||
public function testASingleRecipientHasNoColleagues(): void
|
||||
{
|
||||
$this->mailer
|
||||
->expects($this->once())
|
||||
->method('createAndSendEmail')
|
||||
->willReturnCallback(function (array $context): void {
|
||||
$this->assertSame([], $context['colleagues']);
|
||||
})
|
||||
;
|
||||
|
||||
$recipients = (new TeamerMailingRecipientsDto())->addEligible($this->createRecipient('Anna', 'Berg', '[email protected]'));
|
||||
|
||||
$this->service->sendMailing(SendTeamerMailing::fromRecipients('Betreff', 'Nachricht', $recipients, listColleagues: true));
|
||||
}
|
||||
|
||||
/**
|
||||
* The teamer mailing writes to whoever a filter matched - people who need not know each other.
|
||||
*/
|
||||
public function testAMailingWithoutTheFlagListsNoColleagues(): void
|
||||
{
|
||||
$this->mailer
|
||||
->expects($this->exactly(2))
|
||||
->method('createAndSendEmail')
|
||||
->willReturnCallback(function (array $context): void {
|
||||
$this->assertSame([], $context['colleagues']);
|
||||
})
|
||||
;
|
||||
|
||||
$this->service->sendMailing($this->createMailing(
|
||||
'Betreff',
|
||||
'Nachricht',
|
||||
$this->createRecipient('Anna', 'Berg', '[email protected]'),
|
||||
$this->createRecipient('Bea', 'Ohm', '[email protected]')
|
||||
));
|
||||
}
|
||||
|
||||
public function testAMailingQueuedWithoutTheNewOptionsHasNone(): void
|
||||
{
|
||||
$mailing = (new \ReflectionClass(SendTeamerMailing::class))->newInstanceWithoutConstructor();
|
||||
|
||||
$this->assertFalse($mailing->listsColleagues());
|
||||
$this->assertNull($mailing->getReplyTo());
|
||||
}
|
||||
|
||||
public function testSendMailingSetsTheReplyToOnEveryMail(): void
|
||||
{
|
||||
$replyTo = [];
|
||||
$this->mailer
|
||||
->expects($this->exactly(2))
|
||||
->method('createAndSendEmail')
|
||||
->willReturnCallback(function (array $context, array $options) use (&$replyTo): void {
|
||||
$replyTo[] = $options['reply_to'];
|
||||
})
|
||||
;
|
||||
|
||||
$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, replyTo: '[email protected]'));
|
||||
|
||||
$this->assertSame(['[email protected]', '[email protected]'], $replyTo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -296,7 +387,7 @@ class TeamerMailingServiceTest extends TestCase
|
||||
->expects($this->once())
|
||||
->method('createAndSendEmail')
|
||||
->with(
|
||||
['bodyHtml' => '<p>Servus Rita Kern</p>'],
|
||||
['bodyHtml' => '<p>Servus Rita Kern</p>', 'colleagues' => []],
|
||||
$this->callback(function (array $options): bool {
|
||||
$this->assertSame('[email protected]', $options['to']);
|
||||
$this->assertSame('[Vorschau] Hallo Rita', $options['subject']);
|
||||
|
||||
Reference in new issue
Block a user