fix: remind house managers of their own pending list at the teamer reminder pace

This commit is contained in:
fromme committed 2026-11-11 14:36:18 +01:00
1 parent ab6ffd4eda
commit 77d7000df3
11 files changed
+515 -73

No files matched your search

+1 -1
View File
@@ -40,7 +40,7 @@ class MailerTest extends KernelTestCase
{
self::bootKernel();
return self::getContainer()->get(Mailer::class)->create(['feedbacks' => []], [
return self::getContainer()->get(Mailer::class)->create(['travels' => []], [
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'Betreff',
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Tests\Email;
use App\Entity\Destination;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Mime\BodyRendererInterface;
/**
* The house manager reminder has to say what is pending - it used to be a bare headline.
*/
class ReminderPendingFeedbackTemplateTest extends KernelTestCase
{
public function testItListsEachTravelWithWhatIsOpen(): void
{
$destination = (new Destination())
->setProduct('Davos Klosters')
->setHotel('Schwendi')
->setDateFrom(new \DateTimeImmutable('2026-11-06'))
->setDateTo(new \DateTimeImmutable('2026-11-13'))
;
// rendered the way the mailer does, which also hands the layout its "email" variable
$email = (new TemplatedEmail())
->htmlTemplate('email/reminder_pending_feedback.html.twig')
->context(['travels' => [
['destination' => $destination, 'questionnaire' => 'draft', 'openRatings' => 2],
]])
;
self::getContainer()->get(BodyRendererInterface::class)->render($email);
$html = (string) $email->getHtmlBody();
$this->assertStringContainsString('06.11.2026 - 13.11.2026, Davos Klosters (Schwendi)', $html);
$this->assertStringContainsString('Fragebogen als Entwurf gespeichert, noch nicht abgeschickt', $html);
$this->assertStringContainsString('2 Teamer:innen noch nicht bewertet', $html);
$this->assertStringContainsString('/house-manager/pending-feedback', $html);
}
}
@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace App\Tests\Repository;
use App\Entity\Destination;
use App\Entity\Teamer;
use App\Entity\User;
use App\Repository\DestinationRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Who the nightly house manager reminder writes to and what it reminds them of, against db_test
* with real rows.
*
* Every test runs inside a transaction that is rolled back, so db_test - shared between
* branches - keeps nothing.
*/
class HouseManagerReminderQueryTest extends KernelTestCase
{
use QuestionnaireFixturesTrait;
private EntityManagerInterface $entityManager;
private Teamer $teamer;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->entityManager->getConnection()->beginTransaction();
$this->teamer = (new Teamer())->setFirstName('Kim')->setLastName('Muster')->setGender('d');
$this->entityManager->persist($this->teamer);
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->rollBack();
parent::tearDown();
}
/**
* A house manager who worked on the travel themselves does not rate their own placement, so
* it must not keep the travel pending for them - only someone else's unrated one does.
*/
public function testTheHouseManagersOwnPlacementDoesNotKeepATravelPending(): void
{
$houseManager = $this->houseManager('[email protected]');
$ownOnly = $this->escort(teamer: $houseManager->getTeamer(), hotel: 'Nur selbst')->getAssignment()->getDestination();
$withOther = $this->escort(hotel: 'Mit anderen')->getAssignment()->getDestination();
$this->entityManager->flush();
$pending = array_column($this->destinationRepository()->findPendingForHouseManager(
$houseManager,
new \DateTimeImmutable('2099-01-01'), // questionnaire not live: only ratings count
new \DateTimeImmutable('today')
), 0);
$this->assertContains($withOther, $pending);
$this->assertNotContains($ownOnly, $pending);
}
public function testBlockedAndDeletedHouseManagersAreNotReminded(): void
{
$active = $this->houseManager('[email protected]');
$blocked = $this->houseManager('[email protected]')->setDisabledAt(new \DateTimeImmutable());
$deleted = $this->houseManager('[email protected]')->setDeletedAt(new \DateTimeImmutable());
$this->entityManager->flush();
/** @var UserRepository $repository */
$repository = $this->entityManager->getRepository(User::class);
$reminded = $repository->findHouseManagersToRemind();
$this->assertContains($active, $reminded);
$this->assertNotContains($blocked, $reminded);
$this->assertNotContains($deleted, $reminded);
}
private function houseManager(string $email): User
{
$teamer = (new Teamer())->setFirstName('Hanna')->setLastName('Haus')->setGender('d');
$user = (new User())
->setFirstName('Hanna')
->setLastName('Haus')
->setEmail(uniqid('', true).'.'.$email)
->setRoles(['ROLE_HOUSE_MANAGER'])
->setHotelCodes(['DKI'])
->setBusProAddressId(1)
->setBusProPersonId(1)
->setTeamer($teamer)
;
$teamer->setUser($user);
$this->entityManager->persist($teamer);
$this->entityManager->persist($user);
return $user;
}
private function destinationRepository(): DestinationRepository
{
return $this->entityManager->getRepository(Destination::class);
}
}
@@ -0,0 +1,178 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service\Cron;
use App\Email\Mailer;
use App\Entity\Destination;
use App\Entity\Teamer;
use App\Entity\User;
use App\Repository\DestinationRepository;
use App\Repository\UserRepository;
use App\Service\Cron\FeedbackReminderService;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
/**
* Which house managers are reminded, on which nights. What is pending for one of them is the
* repository's rule, the same as on their dashboard.
*/
#[AllowMockObjectsWithoutExpectations]
class FeedbackReminderServiceTest extends TestCase
{
private const string TODAY = '2026-11-20';
private DestinationRepository&MockObject $destinationRepository;
private UserRepository&MockObject $userRepository;
private Mailer&MockObject $mailer;
private FeedbackReminderService $service;
protected function setUp(): void
{
$this->destinationRepository = $this->createMock(DestinationRepository::class);
$this->userRepository = $this->createMock(UserRepository::class);
$this->mailer = $this->createMock(Mailer::class);
$this->service = new FeedbackReminderService(
$this->destinationRepository,
$this->userRepository,
$this->mailer,
new NullLogger(),
'2026-11-11'
);
}
/**
* Their own hotel codes and their own teamer, so their own placement on a travel is never
* among what they are reminded of; travels ended yesterday or earlier.
*/
public function testEachHouseManagerIsAskedAboutWithTheirOwnCodesAndTeamer(): void
{
$houseManager = $this->houseManager('[email protected]', ['DKI']);
$this->userRepository->method('findHouseManagersToRemind')->willReturn([$houseManager]);
$this->destinationRepository
->expects($this->once())
->method('findPendingForHouseManager')
->with($houseManager, new \DateTimeImmutable('2026-11-11'), new \DateTimeImmutable('2026-11-19'))
->willReturn([])
;
$this->assertSame('Sent feedback reminders to 0 house managers', $this->service->sendFeedbackReminders(new \DateTimeImmutable(self::TODAY.' 01:00')));
}
public function testMutedHouseManagersAndThoseWithoutHotelCodesAreSkipped(): void
{
$this->userRepository->method('findHouseManagersToRemind')->willReturn([
$this->houseManager('[email protected]', ['DKI'])->setMuteNotifications(true),
$this->houseManager('[email protected]', []),
]);
$this->destinationRepository->expects($this->never())->method('findPendingForHouseManager');
$this->mailer->expects($this->never())->method('createAndSendEmail');
// skipped ones are not counted as reminded
$this->assertSame('Sent feedback reminders to 0 house managers', $this->service->sendFeedbackReminders(new \DateTimeImmutable(self::TODAY)));
}
/**
* The teamer reminder's pace: nothing is due on night 10, so no mail, though the travel is
* still pending.
*/
public function testNoMailOnANightNothingIsDue(): void
{
$this->userRepository->method('findHouseManagersToRemind')->willReturn([$this->houseManager('[email protected]', ['DKI'])]);
$this->destinationRepository->method('findPendingForHouseManager')->willReturn([$this->row($this->travel(10))]);
$this->mailer->expects($this->never())->method('createAndSendEmail');
$this->assertSame('Sent feedback reminders to 0 house managers', $this->service->sendFeedbackReminders(new \DateTimeImmutable(self::TODAY)));
}
/**
* One travel due tonight brings the whole list along, the one on its quiet night 10 included.
*/
public function testADueTravelBringsTheWholeListAlong(): void
{
$due = $this->travel(14);
$quiet = $this->travel(10);
$this->userRepository->method('findHouseManagersToRemind')->willReturn([$this->houseManager('[email protected]', ['DKI'])]);
$this->destinationRepository->method('findPendingForHouseManager')->willReturn([$this->row($quiet), $this->row($due)]);
$this->mailer
->expects($this->once())
->method('createAndSendEmail')
->willReturnCallback(function (array $context, array $options) use ($due, $quiet): void {
$this->assertSame('[email protected]', $options['to']);
$this->assertSame(Mailer::MAILJET_TRANSPORT, $options['transport']);
$this->assertSame([$quiet, $due], array_column($context['travels'], 'destination'));
})
;
$this->assertSame('Sent feedback reminders to 1 house managers', $this->service->sendFeedbackReminders(new \DateTimeImmutable(self::TODAY)));
}
/**
* The questionnaire is only owed for travels ended since it went live (2026-11-11 here), and
* a draft is told apart from one never started.
*/
public function testEachTravelSaysWhatIsStillOpen(): void
{
$beforeGoLive = $this->travel(14); // ended 2026-11-06
$draft = $this->travel(7); // ended 2026-11-13
$untouched = $this->travel(3); // ended 2026-11-17
$submitted = $this->travel(1); // ended 2026-11-19
$this->userRepository->method('findHouseManagersToRemind')->willReturn([$this->houseManager('[email protected]', ['DKI'])]);
$this->destinationRepository->method('findPendingForHouseManager')->willReturn([
$this->row($beforeGoLive, null, 2),
$this->row($draft, 'draft', 0),
$this->row($untouched, null, 1),
$this->row($submitted, 'submitted', 3),
]);
$travels = null;
$this->mailer
->method('createAndSendEmail')
->willReturnCallback(function (array $context) use (&$travels): void {
$travels = $context['travels'];
})
;
$this->service->sendFeedbackReminders(new \DateTimeImmutable(self::TODAY));
$this->assertSame(
[[null, 2], ['draft', 0], ['open', 1], [null, 3]],
array_map(fn (array $travel) => [$travel['questionnaire'], $travel['openRatings']], $travels)
);
}
/**
* @param list<string> $hotelCodes
*/
private function houseManager(string $email, array $hotelCodes): User
{
return (new User())
->setEmail($email)
->setRoles(['ROLE_HOUSE_MANAGER'])
->setHotelCodes($hotelCodes)
->setTeamer(new Teamer())
;
}
/**
* @return array{0: Destination, questionnaireStatus: ?string, placements: int, openFeedbacks: int}
*/
private function row(Destination $destination, ?string $questionnaireStatus = null, int $openFeedbacks = 1): array
{
return [$destination, 'questionnaireStatus' => $questionnaireStatus, 'placements' => 2, 'openFeedbacks' => $openFeedbacks];
}
private function travel(int $endedNightsAgo): Destination
{
$dateTo = (new \DateTimeImmutable(self::TODAY))->modify(sprintf('-%d days', $endedNightsAgo));
return (new Destination())->setDateFrom($dateTo->modify('-7 days'))->setDateTo($dateTo);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service\Cron;
use App\Service\Cron\ReminderCadence;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class ReminderCadenceTest extends TestCase
{
/**
* @return iterable<string, array{int, bool}>
*/
public static function nights(): iterable
{
yield 'the day the trip ends' => [0, false];
yield 'first night after' => [1, true];
yield 'last daily night' => [7, true];
yield 'first quiet night' => [8, false];
yield 'still quiet' => [13, false];
yield 'two weeks' => [14, true];
yield 'three weeks' => [21, true];
yield 'between the weeks' => [22, false];
yield 'months later' => [70, true];
}
#[DataProvider('nights')]
public function testDailyForAWeekThenWeekly(int $nights, bool $due): void
{
$today = new \DateTimeImmutable('2026-11-20');
$this->assertSame($due, ReminderCadence::isDue($today->modify(sprintf('-%d days', $nights)), $today));
}
public function testTheTimeOfDayDoesNotCount(): void
{
$this->assertTrue(ReminderCadence::isDue(new \DateTimeImmutable('2026-11-13 23:59'), new \DateTimeImmutable('2026-11-20 01:00')));
}
}