diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 9cb8fdd2..b3ff086f 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -22,7 +22,6 @@
-
@@ -184,8 +183,8 @@
+
getCalendarData()]]>
- getCalendar()]]>
@@ -237,10 +236,13 @@
getId()]]>
getId()]]>
getId()]]>
+ getId()]]>
getCalendarData()]]>
getCalendarData()]]>
+
+
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/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..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,20 +33,30 @@
class BirthdayService
{
- /**
- * @var CalendarBackend
- */
- private $calendarBackend;
+ private ?CalendarBackend $calendarBackend = null;
public function __construct(
private ManagerRegistry $doctrine,
+ private Connection $connection,
private string $birthdayReminderOffset,
) {
}
- public function setBackend(CalendarBackend $calendarBackend)
+ /**
+ * 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
{
- $this->calendarBackend = $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
@@ -83,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
);
@@ -344,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()
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/Controllers/AddressBookControllerTest.php b/tests/Functional/Controllers/AddressBookControllerTest.php
index c5046050..ea06d86c 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,138 @@ 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();
+ }
+
+ /**
+ * 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/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..c151e991 100644
--- a/tests/Functional/Service/BirthdayServiceTest.php
+++ b/tests/Functional/Service/BirthdayServiceTest.php
@@ -13,7 +13,7 @@
use App\Entity\Principal;
use App\Services\BirthdayService;
use Doctrine\ORM\EntityManagerInterface;
-use Sabre\CalDAV\Backend\PDO as CalendarBackend;
+use Sabre\DAV\Sharing\Plugin as SharingPlugin;
use Sabre\VObject\Component\VCalendar;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -29,10 +29,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();
}
@@ -388,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]));
+ }
}