summaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2019-02-20 10:16:47 +0100
committerGitHub <noreply@github.com>2019-02-20 10:16:47 +0100
commit2b76e27aad331704ca647746010e68629577ec0c (patch)
treeba3fcae625789789a53686a60e5b733c698d0c60 /apps/dav/tests/unit
parent10ae7af87f2d152e7b76e1105b051a1e07fbaa27 (diff)
parent3acde071f306a2b0092eb5c81116c8ded971eaeb (diff)
downloadnextcloud-server-2b76e27aad331704ca647746010e68629577ec0c.tar.gz
nextcloud-server-2b76e27aad331704ca647746010e68629577ec0c.zip
Merge pull request #11832 from nextcloud/bugfix/9849/birthday_without_year
set birthday year to 1970 if no year, take X-APPLE-OMIT-YEAR into account
Diffstat (limited to 'apps/dav/tests/unit')
-rw-r--r--apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php26
-rw-r--r--apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php92
-rw-r--r--apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php137
3 files changed, 235 insertions, 20 deletions
diff --git a/apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php b/apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php
index 010289a745a..1d672ff22a4 100644
--- a/apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php
+++ b/apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php
@@ -62,6 +62,10 @@ class GenerateBirthdayCalendarBackgroundJobTest extends TestCase {
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
+ $this->birthdayService->expects($this->never())
+ ->method('resetForUser')
+ ->with('user123');
+
$this->birthdayService->expects($this->once())
->method('syncUser')
->with('user123');
@@ -69,6 +73,28 @@ class GenerateBirthdayCalendarBackgroundJobTest extends TestCase {
$this->backgroundJob->run(['userId' => 'user123']);
}
+ public function testRunAndReset() {
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with('dav', 'generateBirthdayCalendar', 'yes')
+ ->will($this->returnValue('yes'));
+
+ $this->config->expects($this->once())
+ ->method('getUserValue')
+ ->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
+ ->will($this->returnValue('yes'));
+
+ $this->birthdayService->expects($this->once())
+ ->method('resetForUser')
+ ->with('user123');
+
+ $this->birthdayService->expects($this->once())
+ ->method('syncUser')
+ ->with('user123');
+
+ $this->backgroundJob->run(['userId' => 'user123', 'purgeBeforeGenerating' => true]);
+ }
+
public function testRunGloballyDisabled() {
$this->config->expects($this->once())
->method('getAppValue')
diff --git a/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php b/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php
index 651fbf5eaf8..46aa06e8537 100644
--- a/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php
+++ b/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php
@@ -29,6 +29,7 @@ use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\DAV\GroupPrincipalBackend;
use OCP\IConfig;
+use OCP\IDBConnection;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Reader;
use Test\TestCase;
@@ -45,6 +46,8 @@ class BirthdayServiceTest extends TestCase {
private $groupPrincipalBackend;
/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
private $config;
+ /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject */
+ private $dbConnection;
public function setUp() {
parent::setUp();
@@ -53,18 +56,25 @@ class BirthdayServiceTest extends TestCase {
$this->cardDav = $this->createMock(CardDavBackend::class);
$this->groupPrincipalBackend = $this->createMock(GroupPrincipalBackend::class);
$this->config = $this->createMock(IConfig::class);
+ $this->dbConnection = $this->createMock(IDBConnection::class);
$this->service = new BirthdayService($this->calDav, $this->cardDav,
- $this->groupPrincipalBackend, $this->config);
+ $this->groupPrincipalBackend, $this->config, $this->dbConnection);
}
/**
* @dataProvider providesVCards
- * @param boolean $expectedSummary
+ * @param string $expectedSummary
+ * @param string $expectedDTStart
+ * @param string $expectedFieldType
+ * @param string $expectedUnknownYear
+ * @param string $expectedOriginalYear
* @param string | null $data
*/
- public function testBuildBirthdayFromContact($expectedSummary, $data) {
- $cal = $this->service->buildDateFromContact($data, 'BDAY', '', '*');
+ public function testBuildBirthdayFromContact($expectedSummary, $expectedDTStart, $expectedFieldType, $expectedUnknownYear, $expectedOriginalYear, $data, $supports4Bytes) {
+ $this->dbConnection->method('supports4ByteText')->willReturn($supports4Bytes);
+ $cal = $this->service->buildDateFromContact($data, 'BDAY', '', '*', '🎂');
+
if ($expectedSummary === null) {
$this->assertNull($cal);
} else {
@@ -72,6 +82,14 @@ class BirthdayServiceTest extends TestCase {
$this->assertTrue(isset($cal->VEVENT));
$this->assertEquals('FREQ=YEARLY', $cal->VEVENT->RRULE->getValue());
$this->assertEquals($expectedSummary, $cal->VEVENT->SUMMARY->getValue());
+ $this->assertEquals($expectedDTStart, $cal->VEVENT->DTSTART->getValue());
+ $this->assertEquals($expectedFieldType, $cal->VEVENT->{'X-NEXTCLOUD-BC-FIELD-TYPE'}->getValue());
+ $this->assertEquals($expectedUnknownYear, $cal->VEVENT->{'X-NEXTCLOUD-BC-UNKNOWN-YEAR'}->getValue());
+
+ if ($expectedOriginalYear) {
+ $this->assertEquals($expectedOriginalYear, $cal->VEVENT->{'X-NEXTCLOUD-BC-YEAR'}->getValue());
+ }
+
$this->assertEquals('TRANSPARENT', $cal->VEVENT->TRANSP->getValue());
}
}
@@ -151,7 +169,7 @@ class BirthdayServiceTest extends TestCase {
$service = $this->getMockBuilder(BirthdayService::class)
->setMethods(['buildDateFromContact', 'birthdayEvenChanged'])
- ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config])
+ ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config, $this->dbConnection])
->getMock();
$service->onCardChanged(666, 'gump.vcf', '');
@@ -180,7 +198,7 @@ class BirthdayServiceTest extends TestCase {
/** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject $service */
$service = $this->getMockBuilder(BirthdayService::class)
->setMethods(['buildDateFromContact', 'birthdayEvenChanged'])
- ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config])
+ ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config, $this->dbConnection])
->getMock();
$service->onCardChanged(666, 'gump.vcf', '');
@@ -216,7 +234,7 @@ class BirthdayServiceTest extends TestCase {
/** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject $service */
$service = $this->getMockBuilder(BirthdayService::class)
->setMethods(['buildDateFromContact', 'birthdayEvenChanged'])
- ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config])
+ ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config, $this->dbConnection])
->getMock();
if ($expectedOp === 'delete') {
@@ -311,6 +329,32 @@ class BirthdayServiceTest extends TestCase {
$this->service->ensureCalendarExists('principal001');
}
+ public function testResetForUser() {
+ $this->calDav->expects($this->at(0))
+ ->method('getCalendarByUri')
+ ->with('principals/users/user123', 'contact_birthdays')
+ ->willReturn(['id' => 42]);
+
+ $this->calDav->expects($this->at(1))
+ ->method('getCalendarObjects')
+ ->with(42, 0)
+ ->willReturn([['uri' => '1.ics'], ['uri' => '2.ics'], ['uri' => '3.ics']]);
+
+ $this->calDav->expects($this->at(2))
+ ->method('deleteCalendarObject')
+ ->with(42, '1.ics', 0);
+
+ $this->calDav->expects($this->at(3))
+ ->method('deleteCalendarObject')
+ ->with(42, '2.ics', 0);
+
+ $this->calDav->expects($this->at(4))
+ ->method('deleteCalendarObject')
+ ->with(42, '3.ics', 0);
+
+ $this->service->resetForUser('user123');
+ }
+
public function providesBirthday() {
return [
[true,
@@ -338,19 +382,27 @@ class BirthdayServiceTest extends TestCase {
public function providesVCards() {
return [
- [null, null],
- [null, ''],
- [null, 'yasfewf'],
- [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:someday\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- ['12345 (*1900)', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19000101\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- ['12345 (*1900)', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19001231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- ['12345 *', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:--1231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- ['12345 *', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY;X-APPLE-OMIT-YEAR=1604:16041231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:;VALUE=text:circa 1800\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nN:12345;;;;\r\nBDAY:20031231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
- ['12345 (*900)', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:09001231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
+ // $expectedSummary, $expectedDTStart, $expectedFieldType, $expectedUnknownYear, $expectedOriginalYear, $data, $supports4Byte
+ [null, null, null, null, null, null, true],
+ [null, null, null, null, null, '', true],
+ [null, null, null, null, null, 'yasfewf', true],
+ [null, null, null, null, null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", true],
+ [null, null, null, null, null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:\r\nEND:VCARD\r\n", true],
+ [null, null, null, null, null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:someday\r\nEND:VCARD\r\n", true],
+ ['🎂 12345 (1900)', '19700101', 'BDAY', '0', '1900', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19000101\r\nEND:VCARD\r\n", true],
+ ['🎂 12345 (1900)', '19701231', 'BDAY', '0', '1900', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19001231\r\nEND:VCARD\r\n", true],
+ ['🎂 12345', '19701231', 'BDAY', '1', null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:--1231\r\nEND:VCARD\r\n", true],
+ ['🎂 12345', '19701231', 'BDAY', '1', null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY;X-APPLE-OMIT-YEAR=1604:16041231\r\nEND:VCARD\r\n", true],
+ [null, null, null, null, null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:;VALUE=text:circa 1800\r\nEND:VCARD\r\n", true],
+ [null, null, null, null, null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nN:12345;;;;\r\nBDAY:20031231\r\nEND:VCARD\r\n", true],
+ ['🎂 12345 (900)', '19701231', 'BDAY', '0', '900', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:09001231\r\nEND:VCARD\r\n", true],
+ ['12345 (*1900)', '19700101', 'BDAY', '0', '1900', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19000101\r\nEND:VCARD\r\n", false],
+ ['12345 (*1900)', '19701231', 'BDAY', '0', '1900', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19001231\r\nEND:VCARD\r\n", false],
+ ['12345 *', '19701231', 'BDAY', '1', null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:--1231\r\nEND:VCARD\r\n", false],
+ ['12345 *', '19701231', 'BDAY', '1', null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY;X-APPLE-OMIT-YEAR=1604:16041231\r\nEND:VCARD\r\n", false],
+ [null, null, null, null, null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:;VALUE=text:circa 1800\r\nEND:VCARD\r\n", false],
+ [null, null, null, null, null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nN:12345;;;;\r\nBDAY:20031231\r\nEND:VCARD\r\n", false],
+ ['12345 (*900)', '19701231', 'BDAY', '0', '900', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:09001231\r\nEND:VCARD\r\n", false],
];
}
}
diff --git a/apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php b/apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php
new file mode 100644
index 00000000000..05e12bbda6e
--- /dev/null
+++ b/apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php
@@ -0,0 +1,137 @@
+<?php
+/**
+ * @copyright Copyright (c) 2018, Georg Ehrke
+ *
+ * @author Georg Ehrke <oc.list@georgehrke.com>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\Tests\unit\DAV\Migration;
+
+use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
+use OCA\DAV\Migration\RefreshWebcalJobRegistrar;
+use OCA\DAV\Migration\RegenerateBirthdayCalendars;
+use OCP\BackgroundJob\IJobList;
+use OCP\IConfig;
+use OCP\IUser;
+use OCP\IUserManager;
+use OCP\Migration\IOutput;
+use Test\TestCase;
+
+class RegenerateBirthdayCalendarsTest extends TestCase {
+
+ /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
+ private $userManager;
+
+ /** @var IJobList | \PHPUnit_Framework_MockObject_MockObject */
+ private $jobList;
+
+ /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
+ private $config;
+
+ /** @var RefreshWebcalJobRegistrar */
+ private $migration;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->jobList = $this->createMock(IJobList::class);
+ $this->config = $this->createMock(IConfig::class);
+
+ $this->migration = new RegenerateBirthdayCalendars($this->userManager,
+ $this->jobList, $this->config);
+ }
+
+ public function testGetName() {
+ $this->assertEquals(
+ 'Regenerating birthday calendars to use new icons and fix old birthday events without year',
+ $this->migration->getName()
+ );
+ }
+
+ public function testRun() {
+ $this->config->expects($this->at(0))
+ ->method('getAppValue')
+ ->with('dav', 'regeneratedBirthdayCalendarsForYearFix')
+ ->willReturn(null);
+
+ $output = $this->createMock(IOutput::class);
+ $output->expects($this->once())
+ ->method('info')
+ ->with('Adding background jobs to regenerate birthday calendar');
+
+ $this->userManager->expects($this->once())
+ ->method('callForAllUsers')
+ ->will($this->returnCallback(function($closure) {
+ $user1 = $this->createMock(IUser::class);
+ $user1->method('getUID')->will($this->returnValue('uid1'));
+ $user2 = $this->createMock(IUser::class);
+ $user2->method('getUID')->will($this->returnValue('uid2'));
+ $user3 = $this->createMock(IUser::class);
+ $user3->method('getUID')->will($this->returnValue('uid3'));
+
+ $closure($user1);
+ $closure($user2);
+ $closure($user3);
+ }));
+
+ $this->jobList->expects($this->at(0))
+ ->method('add')
+ ->with(GenerateBirthdayCalendarBackgroundJob::class, [
+ 'userId' => 'uid1',
+ 'purgeBeforeGenerating' => true
+ ]);
+ $this->jobList->expects($this->at(1))
+ ->method('add')
+ ->with(GenerateBirthdayCalendarBackgroundJob::class, [
+ 'userId' => 'uid2',
+ 'purgeBeforeGenerating' => true
+ ]);
+ $this->jobList->expects($this->at(2))
+ ->method('add')
+ ->with(GenerateBirthdayCalendarBackgroundJob::class, [
+ 'userId' => 'uid3',
+ 'purgeBeforeGenerating' => true
+ ]);
+
+ $this->config->expects($this->at(1))
+ ->method('setAppValue')
+ ->with('dav', 'regeneratedBirthdayCalendarsForYearFix', 'yes');
+
+ $this->migration->run($output);
+ }
+
+ public function testRunSecondTime() {
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with('dav', 'regeneratedBirthdayCalendarsForYearFix')
+ ->willReturn('yes');
+
+ $output = $this->createMock(IOutput::class);
+ $output->expects($this->once())
+ ->method('info')
+ ->with('Repair step already executed');
+
+ $this->userManager->expects($this->never())
+ ->method('callForAllUsers');
+
+ $this->migration->run($output);
+ }
+
+
+} \ No newline at end of file