Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
</PossiblyNullArgument>
<UndefinedInterfaceMethod>
<code><![CDATA[findOneByUsername]]></code>
<code><![CDATA[getConnection]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Controller/Admin/AddressBookController.php">
Expand Down Expand Up @@ -184,8 +183,8 @@
</file>
<file src="src/Services/BirthdayService.php">
<PossiblyNullArgument>
<code><![CDATA[$calendar]]></code>
<code><![CDATA[$existing->getCalendarData()]]></code>
<code><![CDATA[$instance->getCalendar()]]></code>
</PossiblyNullArgument>
<PossiblyNullReference>
<code><![CDATA[getId]]></code>
Expand Down Expand Up @@ -237,10 +236,13 @@
<code><![CDATA[$addressBook->getId()]]></code>
<code><![CDATA[$addressBook->getId()]]></code>
<code><![CDATA[$addressBook->getId()]]></code>
<code><![CDATA[$addressBook->getId()]]></code>
<code><![CDATA[$object->getCalendarData()]]></code>
<code><![CDATA[$object->getCalendarData()]]></code>
</PossiblyNullArgument>
<PossiblyNullReference>
<code><![CDATA[getCalendar]]></code>
<code><![CDATA[getId]]></code>
<code><![CDATA[getSynctoken]]></code>
<code><![CDATA[getSynctoken]]></code>
</PossiblyNullReference>
Expand Down
6 changes: 0 additions & 6 deletions src/Command/SyncBirthdayCalendars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/DAVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/Plugins/BirthdayCalendarPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Plugins;

use App\Services\BirthdayService;
use Sabre\CalDAV\Backend\PDO as CalendarBackend;
use Sabre\CardDAV;
use Sabre\DAV;

Expand All @@ -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)
Expand Down
31 changes: 21 additions & 10 deletions src/Services/BirthdayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 0 additions & 7 deletions tests/Functional/Commands/SyncBirthdayCalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
136 changes: 136 additions & 0 deletions tests/Functional/Controllers/AddressBookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
Loading