Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[oc] Sync deathdate and anniversary to birthday calendar #1742

Merged
merged 1 commit into from
Oct 18, 2016
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
65 changes: 43 additions & 22 deletions apps/dav/lib/CalDAV/BirthdayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,19 @@ public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDa
* @param string $cardData
*/
public function onCardChanged($addressBookId, $cardUri, $cardData) {

$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);

$book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
$targetPrincipals[] = $book['principaluri'];
$datesToSync = [
['postfix' => '', 'field' => 'BDAY', 'symbol' => '*'],
['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†"],
['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭"],
];
foreach ($targetPrincipals as $principalUri) {
$calendar = $this->ensureCalendarExists($principalUri);
$objectUri = $book['uri'] . '-' . $cardUri. '.ics';
$calendarData = $this->buildBirthdayFromContact($cardData);
$existing = $this->calDavBackEnd->getCalendarObject($calendar['id'], $objectUri);
if (is_null($calendarData)) {
if (!is_null($existing)) {
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri);
}
} else {
if (is_null($existing)) {
$this->calDavBackEnd->createCalendarObject($calendar['id'], $objectUri, $calendarData->serialize());
} else {
if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) {
$this->calDavBackEnd->updateCalendarObject($calendar['id'], $objectUri, $calendarData->serialize());
}
}
foreach ($datesToSync as $type) {
$this->updateCalendar($cardUri, $cardData, $book, $calendar['id'], $type);
}
}
}
Expand All @@ -98,8 +89,10 @@ public function onCardDeleted($addressBookId, $cardUri) {
$targetPrincipals[] = $book['principaluri'];
foreach ($targetPrincipals as $principalUri) {
$calendar = $this->ensureCalendarExists($principalUri);
$objectUri = $book['uri'] . '-' . $cardUri . '.ics';
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri);
foreach (['', '-death', '-anniversary'] as $tag) {
$objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics';
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri);
}
}
}

Expand All @@ -124,9 +117,11 @@ public function ensureCalendarExists($principal) {

/**
* @param string $cardData
* @param string $dateField
* @param string $summarySymbol
* @return null|VCalendar
*/
public function buildBirthdayFromContact($cardData) {
public function buildDateFromContact($cardData, $dateField, $summarySymbol) {
if (empty($cardData)) {
return null;
}
Expand All @@ -136,10 +131,10 @@ public function buildBirthdayFromContact($cardData) {
return null;
}

if (!isset($doc->BDAY)) {
if (!isset($doc->{$dateField})) {
return null;
}
$birthday = $doc->BDAY;
$birthday = $doc->{$dateField};
if (!(string)$birthday) {
return null;
}
Expand Down Expand Up @@ -168,7 +163,7 @@ public function buildBirthdayFromContact($cardData) {
$vEvent->DTEND['VALUE'] = 'DATE';
$vEvent->{'UID'} = $doc->UID;
$vEvent->{'RRULE'} = 'FREQ=YEARLY';
$vEvent->{'SUMMARY'} = $title . ' (*' . $date->format('Y') . ')';
$vEvent->{'SUMMARY'} = $title . ' (' . $summarySymbol . $date->format('Y') . ')';
$vEvent->{'TRANSP'} = 'TRANSPARENT';
$alarm = $vCal->createComponent('VALARM');
$alarm->add($vCal->createProperty('TRIGGER', '-PT0M', ['VALUE' => 'DURATION']));
Expand Down Expand Up @@ -233,4 +228,30 @@ protected function getAllAffectedPrincipals($addressBookId) {
return array_values(array_unique($targetPrincipals, SORT_STRING));
}

/**
* @param string $cardUri
* @param string $cardData
* @param array $book
* @param int $calendarId
* @param string $type
*/
private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) {
$objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics';
$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['symbol']);
$existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri);
if (is_null($calendarData)) {
if (!is_null($existing)) {
$this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri);
}
} else {
if (is_null($existing)) {
$this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize());
} else {
if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) {
$this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize());
}
}
}
}

}
55 changes: 34 additions & 21 deletions apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ class BirthdayServiceTest extends TestCase {
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject */
private $cardDav;
/** @var GroupPrincipalBackend | \PHPUnit_Framework_MockObject_MockObject */
private $groupPrincialBackend;
private $groupPrincipalBackend;

public function setUp() {
parent::setUp();

$this->calDav = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')->disableOriginalConstructor()->getMock();
$this->cardDav = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')->disableOriginalConstructor()->getMock();
$this->groupPrincialBackend = $this->getMockBuilder('OCA\DAV\DAV\GroupPrincipalBackend')->disableOriginalConstructor()->getMock();
$this->calDav = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
$this->cardDav = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
$this->groupPrincipalBackend = $this->getMockBuilder(GroupPrincipalBackend::class)->disableOriginalConstructor()->getMock();

$this->service = new BirthdayService($this->calDav, $this->cardDav, $this->groupPrincialBackend);
$this->service = new BirthdayService($this->calDav, $this->cardDav, $this->groupPrincipalBackend);
}

/**
Expand All @@ -59,7 +59,7 @@ public function setUp() {
* @param string | null $data
*/
public function testBuildBirthdayFromContact($nullExpected, $data) {
$cal = $this->service->buildBirthdayFromContact($data);
$cal = $this->service->buildDateFromContact($data, 'BDAY', '*');
if ($nullExpected) {
$this->assertNull($cal);
} else {
Expand All @@ -83,7 +83,9 @@ public function testOnCardDeleted() {
->willReturn([
'id' => 1234
]);
$this->calDav->expects($this->once())->method('deleteCalendarObject')->with(1234, 'default-gump.vcf.ics');
$this->calDav->expects($this->at(1))->method('deleteCalendarObject')->with(1234, 'default-gump.vcf.ics');
$this->calDav->expects($this->at(2))->method('deleteCalendarObject')->with(1234, 'default-gump.vcf-death.ics');
$this->calDav->expects($this->at(3))->method('deleteCalendarObject')->with(1234, 'default-gump.vcf-anniversary.ics');
$this->cardDav->expects($this->once())->method('getShares')->willReturn([]);

$this->service->onCardDeleted(666, 'gump.vcf');
Expand All @@ -107,26 +109,37 @@ public function testOnCardChanged($expectedOp) {
$this->cardDav->expects($this->once())->method('getShares')->willReturn([]);

/** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject $service */
$service = $this->getMockBuilder('\OCA\DAV\CalDAV\BirthdayService')
->setMethods(['buildBirthdayFromContact', 'birthdayEvenChanged'])
->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincialBackend])
$service = $this->getMockBuilder(BirthdayService::class)
->setMethods(['buildDateFromContact', 'birthdayEvenChanged'])
->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend])
->getMock();

if ($expectedOp === 'delete') {
$this->calDav->expects($this->once())->method('getCalendarObject')->willReturn('');
$service->expects($this->once())->method('buildBirthdayFromContact')->willReturn(null);
$this->calDav->expects($this->once())->method('deleteCalendarObject')->with(1234, 'default-gump.vcf.ics');
$this->calDav->expects($this->exactly(3))->method('getCalendarObject')->willReturn('');
$service->expects($this->exactly(3))->method('buildDateFromContact')->willReturn(null);
$this->calDav->expects($this->exactly(3))->method('deleteCalendarObject')->withConsecutive(
[1234, 'default-gump.vcf.ics'],
[1234, 'default-gump.vcf-death.ics'],
[1234, 'default-gump.vcf-anniversary.ics']
);
}
if ($expectedOp === 'create') {
$service->expects($this->once())->method('buildBirthdayFromContact')->willReturn(new VCalendar());
$this->calDav->expects($this->once())->method('createCalendarObject')->with(1234, 'default-gump.vcf.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n");
$service->expects($this->exactly(3))->method('buildDateFromContact')->willReturn(new VCalendar());
$this->calDav->expects($this->exactly(3))->method('createCalendarObject')->withConsecutive(
[1234, 'default-gump.vcf.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
[1234, 'default-gump.vcf-death.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
[1234, 'default-gump.vcf-anniversary.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"]
);
}
if ($expectedOp === 'update') {
$service->expects($this->once())->method('buildBirthdayFromContact')->willReturn(new VCalendar());
$service->expects($this->once())->method('birthdayEvenChanged')->willReturn(true);
$this->calDav->expects($this->once())->method('getCalendarObject')->willReturn([
'calendardata' => '']);
$this->calDav->expects($this->once())->method('updateCalendarObject')->with(1234, 'default-gump.vcf.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n");
$service->expects($this->exactly(3))->method('buildDateFromContact')->willReturn(new VCalendar());
$service->expects($this->exactly(3))->method('birthdayEvenChanged')->willReturn(true);
$this->calDav->expects($this->exactly(3))->method('getCalendarObject')->willReturn(['calendardata' => '']);
$this->calDav->expects($this->exactly(3))->method('updateCalendarObject')->withConsecutive(
[1234, 'default-gump.vcf.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
[1234, 'default-gump.vcf-death.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
[1234, 'default-gump.vcf-anniversary.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"]
);
}

$service->onCardChanged(666, 'gump.vcf', '');
Expand Down Expand Up @@ -162,7 +175,7 @@ public function testGetAllAffectedPrincipals() {
'{http://owncloud.org/ns}principal' => 'principals/groups/users'
],
]);
$this->groupPrincialBackend->expects($this->once())->method('getGroupMemberSet')
$this->groupPrincipalBackend->expects($this->once())->method('getGroupMemberSet')
->willReturn([
[
'uri' => 'principals/users/user01',
Expand Down