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');