Skip to content
Open
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
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<code><![CDATA[findByPrincipalUri]]></code>
<code><![CDATA[findOneByUri]]></code>
<code><![CDATA[findOneByUri]]></code>
<code><![CDATA[getConnection]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Controller/Admin/AppKeyController.php">
Expand Down
14 changes: 14 additions & 0 deletions src/Controller/Admin/AddressBookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
}
}
93 changes: 93 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,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');
Expand Down