From f3f1c3cc9afaf275685c7105ed74c0b4d69fde48 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Thu, 19 Nov 2026 23:48:43 +0100 Subject: [PATCH] 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');