From 3c8e17bae8c4a7c77262196ce2ac53914b9b9b12 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Thu, 19 Nov 2026 23:48:43 +0100 Subject: [PATCH 1/5] Set the birthday calendar backend in the admin address book actions Saving or deleting an address book from the admin syncs the user's birthday calendar, but the birthday service only gets its CalDAV backend from the DAV server plugin and the sync command. With a contact that has a birthday, the sync then failed with "Call to a member function createCalendarObject() on null" and the admin showed a 500. Set the backend the same way the sync command does before both syncUser() calls. Fixes #328 --- psalm-baseline.xml | 1 + .../Admin/AddressBookController.php | 14 +++ .../Controllers/AddressBookControllerTest.php | 93 +++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 9cb8fdd2..b67b0a3c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -36,6 +36,7 @@ + diff --git a/src/Controller/Admin/AddressBookController.php b/src/Controller/Admin/AddressBookController.php index 3d1eed8c..c59f87b2 100644 --- a/src/Controller/Admin/AddressBookController.php +++ b/src/Controller/Admin/AddressBookController.php @@ -9,6 +9,7 @@ use App\Form\AddressBookType; use App\Services\BirthdayService; use Doctrine\Persistence\ManagerRegistry; +use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Symfony\Bridge\Doctrine\Attribute\MapEntity; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -79,6 +80,7 @@ public function addressbookCreate(ManagerRegistry $doctrine, Request $request, # if ($isBirthdayCalendarEnabled) { // Let's sync the user birthday calendar if needed + $this->setBirthdayCalendarBackend($doctrine, $birthdayService); $birthdayService->syncUser($username); } @@ -131,9 +133,21 @@ public function addressbookDelete(ManagerRegistry $doctrine, Request $request, # $isBirthdayCalendarEnabled = $this->getParameter('caldav_enabled') && $this->getParameter('carddav_enabled'); if ($isBirthdayCalendarEnabled) { // Let's sync the user birthday calendar if needed + $this->setBirthdayCalendarBackend($doctrine, $birthdayService); $birthdayService->syncUser($username); } return $this->redirectToRoute('addressbook_index', ['userId' => $userId]); } + + /** + * The birthday service writes the events through the CalDAV backend, which is only set by the + * DAV server and the sync command, so it has to be set here too. + */ + private function setBirthdayCalendarBackend(ManagerRegistry $doctrine, BirthdayService $birthdayService): void + { + /** @var \PDO $pdo */ + $pdo = $doctrine->getManager()->getConnection()->getNativeConnection(); + $birthdayService->setBackend(new CalendarBackend($pdo)); + } } diff --git a/tests/Functional/Controllers/AddressBookControllerTest.php b/tests/Functional/Controllers/AddressBookControllerTest.php index c5046050..ea4aa8b4 100644 --- a/tests/Functional/Controllers/AddressBookControllerTest.php +++ b/tests/Functional/Controllers/AddressBookControllerTest.php @@ -2,7 +2,11 @@ namespace App\Tests\Functional; +use App\Constants; use App\Entity\AddressBook; +use App\Entity\CalendarInstance; +use App\Entity\CalendarObject; +use App\Entity\Card; use App\Entity\Principal; use App\Entity\User; use App\Security\AdminUser; @@ -253,6 +257,95 @@ public function testTheBirthdayCalendarFlagIsSaved(): void } } + private function addBirthdayCard(AddressBook $book): void + { + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $cardData = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Birthday Contact\r\nUID:birthday-1\r\nBDAY:19900615\r\nEND:VCARD\r\n"; + $card = (new Card()) + ->setAddressBook($book) + ->setUri('birthday-contact.vcf') + ->setCarddata($cardData) + ->setLastmodified(time()) + ->setSize(strlen($cardData)) + ->setEtag(md5($cardData)); + $em->persist($card); + $em->flush(); + } + + private function assertBirthdayEventExists(): void + { + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $instance = $em->getRepository(CalendarInstance::class)->findOneBy([ + 'principalUri' => Principal::PREFIX.'test_user', + 'uri' => Constants::BIRTHDAY_CALENDAR_URI, + ]); + $this->assertNotNull($instance, 'Birthday calendar instance not found'); + $this->assertNotNull($em->getRepository(CalendarObject::class)->findOneBy([ + 'calendar' => $instance->getCalendar(), + 'uri' => 'default-birthday-contact.vcf.ics', + ]), 'Birthday event not found'); + } + + /** + * Issue #328: with a contact that has a birthday, saving the address book with the flag on + * gave a 500, because the birthday service had no calendar backend to write the event with. + */ + public function testSavingAnAddressBookWithABirthdayAddsItToTheBirthdayCalendar(): void + { + $client = static::createClient(); + $client->loginUser(new AdminUser('admin', 'test')); + + $userId = $this->getUserId($client, 'test_user'); + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $book = $em->getRepository(AddressBook::class)->findOneBy([ + 'principalUri' => Principal::PREFIX.'test_user', + 'uri' => 'default', + ]); + $this->addBirthdayCard($book); + + $crawler = $client->request('GET', '/addressbooks/'.$userId.'/edit/'.$book->getId()); + $form = $crawler->selectButton('address_book_save')->form(); + + $client->submit($form, [ + 'address_book[uri]' => 'default', + 'address_book[displayName]' => 'Default', + 'address_book[includedInBirthdayCalendar]' => 1, + ]); + $this->assertResponseRedirects('/addressbooks/'.$userId); + + $this->assertBirthdayEventExists(); + } + + /** + * Deleting an address book syncs the birthday calendar the same way, so it needs the backend too. + */ + public function testDeletingAnAddressBookKeepsTheOtherBirthdays(): void + { + $client = static::createClient(); + $client->loginUser(new AdminUser('admin', 'test')); + + $userId = $this->getUserId($client, 'test_user'); + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $book = $em->getRepository(AddressBook::class)->findOneBy([ + 'principalUri' => Principal::PREFIX.'test_user', + 'uri' => 'default', + ]); + $book->setIncludedInBirthdayCalendar(true); + $this->addBirthdayCard($book); + + $other = (new AddressBook()) + ->setPrincipalUri(Principal::PREFIX.'test_user') + ->setUri('other') + ->setDisplayName('Other'); + $em->persist($other); + $em->flush(); + + $this->postAdmin($client, '/addressbooks/'.$userId.'/delete/'.$other->getId()); + $this->assertResponseRedirects('/addressbooks/'.$userId); + + $this->assertBirthdayEventExists(); + } + public function testAddressBookDelete(): void { $user = new AdminUser('admin', 'test'); From 7b55276c8a607848d8a81a538b04172e4d7dfb0c Mon Sep 17 00:00:00 2001 From: 0z5a Date: Sun, 22 Nov 2026 22:14:53 +0100 Subject: [PATCH 2/5] Inject the CalDAV backend into the birthday service The birthday service only got its CalDAV backend through setBackend(), which the DAV server plugin and the sync command called, so any other caller had to remember it. Get the backend from the container instead, as a constructor argument, and remove setBackend(). The plugin and the command no longer deal with the backend, and the admin address book actions no longer need to set it. The service is lazy: building the backend opens the database connection, and the sync command is built on every console run. --- config/services.yaml | 10 ++++ psalm-baseline.xml | 2 - src/Command/SyncBirthdayCalendars.php | 6 --- .../Admin/AddressBookController.php | 14 ----- src/Controller/DAVController.php | 2 +- src/Plugins/BirthdayCalendarPlugin.php | 4 +- src/Services/BirthdayService.php | 11 +--- .../Commands/SyncBirthdayCalendarTest.php | 7 --- tests/Functional/DavTest.php | 54 +++++++++++++++++++ .../Plugins/BirthdayCalendarPluginTest.php | 6 +-- .../Service/BirthdayServiceTest.php | 5 -- 11 files changed, 68 insertions(+), 53 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 8ff9d287..63782df2 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -116,7 +116,17 @@ services: - { name: monolog.processor } App\Services\BirthdayService: + # Building the CalDAV backend opens the database connection, so the service is only + # built when one of its methods is called, not whenever something depends on it + lazy: true arguments: + # The same sabre/dav backend the DAV server writes calendar objects with + $calendarBackend: !service + class: Sabre\CalDAV\Backend\PDO + arguments: + - !service + class: PDO + factory: ['@doctrine.dbal.default_connection', 'getNativeConnection'] $birthdayReminderOffset: "%birthday_reminder_offset%" App\Security\ApiKeyAuthenticator: diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b67b0a3c..0d412431 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -22,7 +22,6 @@ - @@ -36,7 +35,6 @@ - diff --git a/src/Command/SyncBirthdayCalendars.php b/src/Command/SyncBirthdayCalendars.php index aea5d578..ed074906 100644 --- a/src/Command/SyncBirthdayCalendars.php +++ b/src/Command/SyncBirthdayCalendars.php @@ -5,7 +5,6 @@ use App\Entity\User; use App\Services\BirthdayService; use Doctrine\Persistence\ManagerRegistry; -use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; @@ -19,11 +18,6 @@ public function __construct( private BirthdayService $birthdayService, ) { parent::__construct(); - - $em = $doctrine->getManager(); - /** @var \PDO $pdo */ - $pdo = $em->getConnection()->getNativeConnection(); - $this->birthdayService->setBackend(new CalendarBackend($pdo)); } protected function configure(): void diff --git a/src/Controller/Admin/AddressBookController.php b/src/Controller/Admin/AddressBookController.php index c59f87b2..3d1eed8c 100644 --- a/src/Controller/Admin/AddressBookController.php +++ b/src/Controller/Admin/AddressBookController.php @@ -9,7 +9,6 @@ use App\Form\AddressBookType; use App\Services\BirthdayService; use Doctrine\Persistence\ManagerRegistry; -use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Symfony\Bridge\Doctrine\Attribute\MapEntity; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -80,7 +79,6 @@ public function addressbookCreate(ManagerRegistry $doctrine, Request $request, # if ($isBirthdayCalendarEnabled) { // Let's sync the user birthday calendar if needed - $this->setBirthdayCalendarBackend($doctrine, $birthdayService); $birthdayService->syncUser($username); } @@ -133,21 +131,9 @@ public function addressbookDelete(ManagerRegistry $doctrine, Request $request, # $isBirthdayCalendarEnabled = $this->getParameter('caldav_enabled') && $this->getParameter('carddav_enabled'); if ($isBirthdayCalendarEnabled) { // Let's sync the user birthday calendar if needed - $this->setBirthdayCalendarBackend($doctrine, $birthdayService); $birthdayService->syncUser($username); } return $this->redirectToRoute('addressbook_index', ['userId' => $userId]); } - - /** - * The birthday service writes the events through the CalDAV backend, which is only set by the - * DAV server and the sync command, so it has to be set here too. - */ - private function setBirthdayCalendarBackend(ManagerRegistry $doctrine, BirthdayService $birthdayService): void - { - /** @var \PDO $pdo */ - $pdo = $doctrine->getManager()->getConnection()->getNativeConnection(); - $birthdayService->setBackend(new CalendarBackend($pdo)); - } } diff --git a/src/Controller/DAVController.php b/src/Controller/DAVController.php index 91053da5..5eb85f2f 100644 --- a/src/Controller/DAVController.php +++ b/src/Controller/DAVController.php @@ -330,7 +330,7 @@ private function initServer(string $authMethod, string $authRealm = User::DEFAUL } if ($this->cardDAVEnabled && null !== $calendarBackend) { - $this->server->addPlugin(new BirthdayCalendarPlugin($this->birthdayService, $calendarBackend)); + $this->server->addPlugin(new BirthdayCalendarPlugin($this->birthdayService)); } // WebDAV plugins diff --git a/src/Plugins/BirthdayCalendarPlugin.php b/src/Plugins/BirthdayCalendarPlugin.php index 879b5a15..11aefcde 100644 --- a/src/Plugins/BirthdayCalendarPlugin.php +++ b/src/Plugins/BirthdayCalendarPlugin.php @@ -3,7 +3,6 @@ namespace App\Plugins; use App\Services\BirthdayService; -use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Sabre\CardDAV; use Sabre\DAV; @@ -19,10 +18,9 @@ class BirthdayCalendarPlugin extends DAV\ServerPlugin */ protected $server; - public function __construct(BirthdayService $birthdayService, CalendarBackend $calendarBackend) + public function __construct(BirthdayService $birthdayService) { $this->birthdayService = $birthdayService; - $this->birthdayService->setBackend($calendarBackend); } public function initialize(DAV\Server $server) diff --git a/src/Services/BirthdayService.php b/src/Services/BirthdayService.php index a218ada6..b856f024 100644 --- a/src/Services/BirthdayService.php +++ b/src/Services/BirthdayService.php @@ -32,22 +32,13 @@ class BirthdayService { - /** - * @var CalendarBackend - */ - private $calendarBackend; - public function __construct( private ManagerRegistry $doctrine, + private CalendarBackend $calendarBackend, private string $birthdayReminderOffset, ) { } - public function setBackend(CalendarBackend $calendarBackend) - { - $this->calendarBackend = $calendarBackend; - } - public function onCardChanged(int $addressBookId, string $cardUri, string $cardData): void { $book = $this->doctrine->getRepository(AddressBook::class)->findOneById($addressBookId); diff --git a/tests/Functional/Commands/SyncBirthdayCalendarTest.php b/tests/Functional/Commands/SyncBirthdayCalendarTest.php index 93c48ffc..6b0812d0 100644 --- a/tests/Functional/Commands/SyncBirthdayCalendarTest.php +++ b/tests/Functional/Commands/SyncBirthdayCalendarTest.php @@ -11,9 +11,7 @@ use App\Entity\Card; use App\Entity\Principal; use App\Entity\User; -use App\Services\BirthdayService; use Doctrine\ORM\EntityManagerInterface; -use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Console\Tester\CommandTester; @@ -29,11 +27,6 @@ protected function setUp(): void $this->em = static::getContainer()->get(EntityManagerInterface::class); - $birthdayService = static::getContainer()->get(BirthdayService::class); - /** @var \PDO $pdo */ - $pdo = $this->em->getConnection()->getNativeConnection(); - $birthdayService->setBackend(new CalendarBackend($pdo)); - $application = new Application($kernel); $command = $application->find('dav:sync-birthday-calendar'); $this->commandTester = new CommandTester($command); diff --git a/tests/Functional/DavTest.php b/tests/Functional/DavTest.php index e8113297..b38be121 100644 --- a/tests/Functional/DavTest.php +++ b/tests/Functional/DavTest.php @@ -2,9 +2,13 @@ namespace App\Tests\Functional; +use App\Constants; +use App\Entity\AddressBook; use App\Entity\CalendarInstance; use App\Entity\CalendarObject; +use App\Entity\Card; use App\Entity\Principal; +use App\Services\BirthdayService; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -282,4 +286,54 @@ public function testTheSabreVersionIsNotAdvertised(): void $this->assertFalse($client->getResponse()->headers->has('X-Sabre-Version')); $this->assertFalse(\Sabre\DAV\Server::$exposeVersion, 'The DAV server must be built with version exposure off'); } + + /** + * The birthday plugin of the DAV server writes through the backend the birthday service gets + * from the container: deleting a card over CardDAV removes its birthday event. + */ + public function testDeletingACardOverCardDavRemovesItsBirthdayEvent(): void + { + $client = static::createClient(); + + /** @var EntityManagerInterface $em */ + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $book = $em->getRepository(AddressBook::class)->findOneBy([ + 'principalUri' => Principal::PREFIX.'test_user', + 'uri' => 'default', + ]); + $this->assertNotNull($book, 'Fixture address book for test_user is missing'); + $book->setIncludedInBirthdayCalendar(true); + + $cardData = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Birthday Contact\r\nUID:birthday-1\r\nBDAY:19900615\r\nEND:VCARD\r\n"; + $em->persist((new Card()) + ->setAddressBook($book) + ->setUri('birthday-contact.vcf') + ->setCarddata($cardData) + ->setLastmodified(time()) + ->setSize(strlen($cardData)) + ->setEtag(md5($cardData))); + $em->flush(); + + static::getContainer()->get(BirthdayService::class)->syncUser('test_user'); + $this->assertSame(1, $this->countBirthdayEvents(), 'The sync must create the birthday event'); + + static::requestDav($client, 'DELETE', '/dav/addressbooks/test_user/default/birthday-contact.vcf', 'test_user:password'); + + $this->assertResponseStatusCodeSame(204); + $this->assertSame(0, $this->countBirthdayEvents()); + } + + private function countBirthdayEvents(): int + { + /** @var EntityManagerInterface $em */ + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $em->clear(); + $instance = $em->getRepository(CalendarInstance::class)->findOneBy([ + 'principalUri' => Principal::PREFIX.'test_user', + 'uri' => Constants::BIRTHDAY_CALENDAR_URI, + ]); + $this->assertNotNull($instance, 'Birthday calendar instance not found'); + + return count($em->getRepository(CalendarObject::class)->findBy(['calendar' => $instance->getCalendar()])); + } } diff --git a/tests/Functional/Plugins/BirthdayCalendarPluginTest.php b/tests/Functional/Plugins/BirthdayCalendarPluginTest.php index a2852266..8dfddf19 100644 --- a/tests/Functional/Plugins/BirthdayCalendarPluginTest.php +++ b/tests/Functional/Plugins/BirthdayCalendarPluginTest.php @@ -9,7 +9,6 @@ use App\Plugins\BirthdayCalendarPlugin; use App\Services\BirthdayService; use Doctrine\ORM\EntityManagerInterface; -use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Sabre\CardDAV; use Sabre\DAV; use Sabre\DAVACL\PrincipalBackend\PDO as PrincipalBackend; @@ -45,10 +44,7 @@ protected function setUp(): void ]); $this->server->setBaseUri('/dav/'); $this->server->addPlugin(new CardDAV\Plugin()); - $this->server->addPlugin(new BirthdayCalendarPlugin( - $container->get(BirthdayService::class), - new CalendarBackend($pdo) - )); + $this->server->addPlugin(new BirthdayCalendarPlugin($container->get(BirthdayService::class))); } private function addressBook(): AddressBook diff --git a/tests/Functional/Service/BirthdayServiceTest.php b/tests/Functional/Service/BirthdayServiceTest.php index d62d81a1..6c2067a0 100644 --- a/tests/Functional/Service/BirthdayServiceTest.php +++ b/tests/Functional/Service/BirthdayServiceTest.php @@ -13,7 +13,6 @@ use App\Entity\Principal; use App\Services\BirthdayService; use Doctrine\ORM\EntityManagerInterface; -use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Sabre\VObject\Component\VCalendar; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -29,10 +28,6 @@ protected function setUp(): void $this->em = static::getContainer()->get(EntityManagerInterface::class); $this->service = static::getContainer()->get(BirthdayService::class); - /** @var \PDO $pdo */ - $pdo = $this->em->getConnection()->getNativeConnection(); - $this->service->setBackend(new CalendarBackend($pdo)); - $this->em->getConnection()->beginTransaction(); } From 49301a7202a32771434e5762840d15783f391de6 Mon Sep 17 00:00:00 2001 From: tchapi Date: Thu, 3 Dec 2026 22:46:36 +0100 Subject: [PATCH 3/5] Build the birthday service's CalDAV backend on first use The backend was built by the container, from two inline services in services.yaml, and the service had to be lazy so that depending on it would not open the database connection. Inject the DBAL connection instead and build the backend in the service the first time it is needed. The service definition goes back to what it was, and no lazy proxy is involved. --- config/services.yaml | 10 ---------- src/Services/BirthdayService.php | 30 +++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 63782df2..8ff9d287 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -116,17 +116,7 @@ services: - { name: monolog.processor } App\Services\BirthdayService: - # Building the CalDAV backend opens the database connection, so the service is only - # built when one of its methods is called, not whenever something depends on it - lazy: true arguments: - # The same sabre/dav backend the DAV server writes calendar objects with - $calendarBackend: !service - class: Sabre\CalDAV\Backend\PDO - arguments: - - !service - class: PDO - factory: ['@doctrine.dbal.default_connection', 'getNativeConnection'] $birthdayReminderOffset: "%birthday_reminder_offset%" App\Security\ApiKeyAuthenticator: diff --git a/src/Services/BirthdayService.php b/src/Services/BirthdayService.php index b856f024..b9e039f6 100644 --- a/src/Services/BirthdayService.php +++ b/src/Services/BirthdayService.php @@ -19,6 +19,7 @@ use App\Entity\CalendarObject; use App\Entity\Card; use App\Entity\Principal; +use Doctrine\DBAL\Connection; use Doctrine\Persistence\ManagerRegistry; use Sabre\CalDAV\Backend\PDO as CalendarBackend; use Sabre\DAV\Sharing\Plugin as SharingPlugin; @@ -32,13 +33,32 @@ class BirthdayService { + private ?CalendarBackend $calendarBackend = null; + public function __construct( private ManagerRegistry $doctrine, - private CalendarBackend $calendarBackend, + private Connection $connection, private string $birthdayReminderOffset, ) { } + /** + * The sabre/dav backend that calendar objects are written with, so that sync tokens and + * changes are maintained the same way as for the DAV server. + * + * Built on first use, as getting the native connection opens the database connection. + */ + private function getCalendarBackend(): CalendarBackend + { + if (null === $this->calendarBackend) { + /** @var \PDO $pdo */ + $pdo = $this->connection->getNativeConnection(); + $this->calendarBackend = new CalendarBackend($pdo); + } + + return $this->calendarBackend; + } + public function onCardChanged(int $addressBookId, string $cardUri, string $cardData): void { $book = $this->doctrine->getRepository(AddressBook::class)->findOneById($addressBookId); @@ -74,7 +94,7 @@ public function onCardDeleted(int $addressBookId, string $cardUri): void // This is the structure that needs to be passed to the backend methods $calendarId = [$calendar->getId(), $calendarInstance->getId()]; - $this->calendarBackend->deleteCalendarObject( + $this->getCalendarBackend()->deleteCalendarObject( $calendarId, $objectUri ); @@ -335,21 +355,21 @@ private function updateCalendar(string $cardUri, string $cardData, AddressBook $ if (null === $calendarData) { if (null !== $existing) { - $this->calendarBackend->deleteCalendarObject( + $this->getCalendarBackend()->deleteCalendarObject( [$calendar->getId(), $calendarInstance->getId()], $objectUri ); } } else { if (null === $existing) { - $this->calendarBackend->createCalendarObject( + $this->getCalendarBackend()->createCalendarObject( [$calendar->getId(), $calendarInstance->getId()], $objectUri, $calendarData->serialize() ); } else { if ($this->birthdayEventChanged($existing->getCalendarData(), $calendarData)) { - $this->calendarBackend->updateCalendarObject( + $this->getCalendarBackend()->updateCalendarObject( [$calendar->getId(), $calendarInstance->getId()], $objectUri, $calendarData->serialize() From 48c2b7563930c1e2931bc7e19931f60e7bcd7085 Mon Sep 17 00:00:00 2001 From: tchapi Date: Thu, 3 Dec 2026 22:46:57 +0100 Subject: [PATCH 4/5] Update the Psalm baseline after the birthday calendar deletion fix --- psalm-baseline.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 0d412431..b3ff086f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -183,8 +183,8 @@ + getCalendarData()]]> - getCalendar()]]> @@ -236,10 +236,13 @@ getId()]]> getId()]]> getId()]]> + getId()]]> getCalendarData()]]> getCalendarData()]]> + + From 5084ba577390ccfe7624cbd9496df84421370ae9 Mon Sep 17 00:00:00 2001 From: tchapi Date: Thu, 3 Dec 2026 22:56:30 +0100 Subject: [PATCH 5/5] Test the birthday calendar deletion from the admin and with shared instances --- .../Controllers/AddressBookControllerTest.php | 43 +++++++++++++++++++ .../Service/BirthdayServiceTest.php | 27 ++++++++++++ 2 files changed, 70 insertions(+) diff --git a/tests/Functional/Controllers/AddressBookControllerTest.php b/tests/Functional/Controllers/AddressBookControllerTest.php index ea4aa8b4..ea06d86c 100644 --- a/tests/Functional/Controllers/AddressBookControllerTest.php +++ b/tests/Functional/Controllers/AddressBookControllerTest.php @@ -346,6 +346,49 @@ public function testDeletingAnAddressBookKeepsTheOtherBirthdays(): void $this->assertBirthdayEventExists(); } + /** + * Issue #331: turning the flag off deletes the birthday calendar, which gave a 500 once the + * calendar had events, as its objects and changes still referenced it. + */ + public function testExcludingTheLastAddressBookDeletesTheBirthdayCalendarAndItsEvents(): void + { + $client = static::createClient(); + $client->loginUser(new AdminUser('admin', 'test')); + + $userId = $this->getUserId($client, 'test_user'); + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $book = $em->getRepository(AddressBook::class)->findOneBy([ + 'principalUri' => Principal::PREFIX.'test_user', + 'uri' => 'default', + ]); + $this->addBirthdayCard($book); + + foreach ([true, false] as $included) { + $crawler = $client->request('GET', '/addressbooks/'.$userId.'/edit/'.$book->getId()); + $form = $crawler->selectButton('address_book_save')->form(); + + $client->submit($form, [ + 'address_book[uri]' => 'default', + 'address_book[displayName]' => 'Default', + 'address_book[includedInBirthdayCalendar]' => $included ? 1 : 0, + ]); + $this->assertResponseRedirects('/addressbooks/'.$userId); + + if ($included) { + $this->assertBirthdayEventExists(); + } + } + + $em = static::getContainer()->get('doctrine.orm.entity_manager'); + $this->assertNull($em->getRepository(CalendarInstance::class)->findOneBy([ + 'principalUri' => Principal::PREFIX.'test_user', + 'uri' => Constants::BIRTHDAY_CALENDAR_URI, + ]), 'The birthday calendar should be gone'); + $this->assertNull($em->getRepository(CalendarObject::class)->findOneBy([ + 'uri' => 'default-birthday-contact.vcf.ics', + ]), 'The birthday event should be gone'); + } + public function testAddressBookDelete(): void { $user = new AdminUser('admin', 'test'); diff --git a/tests/Functional/Service/BirthdayServiceTest.php b/tests/Functional/Service/BirthdayServiceTest.php index 6c2067a0..c151e991 100644 --- a/tests/Functional/Service/BirthdayServiceTest.php +++ b/tests/Functional/Service/BirthdayServiceTest.php @@ -13,6 +13,7 @@ use App\Entity\Principal; use App\Services\BirthdayService; use Doctrine\ORM\EntityManagerInterface; +use Sabre\DAV\Sharing\Plugin as SharingPlugin; use Sabre\VObject\Component\VCalendar; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -383,4 +384,30 @@ public function testSyncPrincipalDeletesBirthdayCalendarIfNoAddressBooksIncluded $this->assertCount(0, $this->em->getRepository(CalendarObject::class)->findBy(['calendar' => $calendarId])); $this->assertCount(0, $this->em->getRepository(CalendarChange::class)->findBy(['calendar' => $calendarId])); } + + public function testSyncPrincipalDeletesTheSharedInstancesOfTheBirthdayCalendar(): void + { + $this->createAddressBook(includedInBirthdayCalendar: false); + $calendar = $this->service->ensureBirthdayCalendarExists('principals/testuser')->getCalendar(); + $this->assertNotNull($calendar); + $calendarId = $calendar->getId(); + + // The same calendar, shared with someone else: it references the calendar too + $shared = (new CalendarInstance()) + ->setPrincipalUri('principals/otheruser') + ->setUri('shared-birthdays') + ->setDisplayName('Shared birthdays') + ->setAccess(SharingPlugin::ACCESS_READ) + ->setShareInviteStatus(SharingPlugin::INVITE_ACCEPTED) + ->setCalendar($calendar); + $this->em->persist($shared); + $this->em->flush(); + + $this->service->syncPrincipal('principals/testuser'); + + $this->em->clear(); + + $this->assertNull($this->em->getRepository(Calendar::class)->find($calendarId)); + $this->assertCount(0, $this->em->getRepository(CalendarInstance::class)->findBy(['calendar' => $calendarId])); + } }