diff options
author | Georg Ehrke <developer@georgehrke.com> | 2017-11-11 11:25:40 +0100 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2017-11-11 16:17:18 +0100 |
commit | 2b51d84b98a5a44c2a42a8498164a35b6822e760 (patch) | |
tree | 1a51737c0bdf59b74e1152d27f98fe5f1914263c /apps/dav | |
parent | a87d9860418948a72fbe3d29a2cc5d589be147ec (diff) | |
download | nextcloud-server-2b51d84b98a5a44c2a42a8498164a35b6822e760.tar.gz nextcloud-server-2b51d84b98a5a44c2a42a8498164a35b6822e760.zip |
generate birthday calendars in a background job after admin enabled them
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
Diffstat (limited to 'apps/dav')
7 files changed, 277 insertions, 4 deletions
diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index 9a9daf3faf6..7ef99895942 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -11,6 +11,7 @@ return array( 'OCA\\DAV\\Avatars\\AvatarHome' => $baseDir . '/../lib/Avatars/AvatarHome.php', 'OCA\\DAV\\Avatars\\AvatarNode' => $baseDir . '/../lib/Avatars/AvatarNode.php', 'OCA\\DAV\\Avatars\\RootCollection' => $baseDir . '/../lib/Avatars/RootCollection.php', + 'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => $baseDir . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php', 'OCA\\DAV\\CalDAV\\Activity\\Backend' => $baseDir . '/../lib/CalDAV/Activity/Backend.php', 'OCA\\DAV\\CalDAV\\Activity\\Filter\\Calendar' => $baseDir . '/../lib/CalDAV/Activity/Filter/Calendar.php', 'OCA\\DAV\\CalDAV\\Activity\\Filter\\Todo' => $baseDir . '/../lib/CalDAV/Activity/Filter/Todo.php', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index a940da7971d..388e1f01301 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -26,6 +26,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\Avatars\\AvatarHome' => __DIR__ . '/..' . '/../lib/Avatars/AvatarHome.php', 'OCA\\DAV\\Avatars\\AvatarNode' => __DIR__ . '/..' . '/../lib/Avatars/AvatarNode.php', 'OCA\\DAV\\Avatars\\RootCollection' => __DIR__ . '/..' . '/../lib/Avatars/RootCollection.php', + 'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php', 'OCA\\DAV\\CalDAV\\Activity\\Backend' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Backend.php', 'OCA\\DAV\\CalDAV\\Activity\\Filter\\Calendar' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Filter/Calendar.php', 'OCA\\DAV\\CalDAV\\Activity\\Filter\\Todo' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Filter/Todo.php', diff --git a/apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php b/apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php new file mode 100644 index 00000000000..c4279c5108c --- /dev/null +++ b/apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php @@ -0,0 +1,69 @@ +<?php +/** + * @copyright 2017 Georg Ehrke <oc.list@georgehrke.com> + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\DAV\BackgroundJob; + +use OC\BackgroundJob\QueuedJob; +use OCA\DAV\CalDAV\BirthdayService; +use OCP\IConfig; + +class GenerateBirthdayCalendarBackgroundJob extends QueuedJob { + + /** @var BirthdayService */ + private $birthdayService; + + /** @var IConfig */ + private $config; + + /** + * GenerateAllBirthdayCalendarsBackgroundJob constructor. + * + * @param BirthdayService $birthdayService + * @param IConfig $config + */ + public function __construct(BirthdayService $birthdayService, + IConfig $config) { + $this->birthdayService = $birthdayService; + $this->config = $config; + } + + /** + * @param array $arguments + */ + public function run($arguments) { + $userId = $arguments['userId']; + + // make sure admin didn't change his mind + $isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); + if ($isGloballyEnabled !== 'yes') { + return; + } + + // did the user opt out? + $isUserEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes'); + if ($isUserEnabled !== 'yes') { + return; + } + + $this->birthdayService->syncUser($userId); + } +} diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 9505a00ea55..73f0a287fae 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -2077,6 +2077,22 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription } /** + * deletes all birthday calendars + */ + public function deleteAllBirthdayCalendars() { + $query = $this->db->getQueryBuilder(); + $result = $query->select(['id'])->from('calendars') + ->where($query->expr()->eq('uri', + $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) + ->execute(); + + $ids = $result->fetchAll(); + foreach($ids as $id) { + $this->deleteCalendar($id['id']); + } + } + + /** * read VCalendar data into a VCalendar object * * @param string $objectData diff --git a/apps/dav/lib/Controller/BirthdayCalendarController.php b/apps/dav/lib/Controller/BirthdayCalendarController.php index c7172c29053..244111e3aec 100644 --- a/apps/dav/lib/Controller/BirthdayCalendarController.php +++ b/apps/dav/lib/Controller/BirthdayCalendarController.php @@ -23,12 +23,17 @@ namespace OCA\DAV\Controller; +use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob; +use OCA\DAV\CalDAV\CalDavBackend; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\Response; +use OCP\BackgroundJob\IJobList; use OCP\IConfig; use OCP\IDBConnection; use OCP\IRequest; +use OCP\IUser; +use OCP\IUserManager; class BirthdayCalendarController extends Controller { @@ -43,18 +48,42 @@ class BirthdayCalendarController extends Controller { protected $config; /** + * @var IUserManager + */ + protected $userManager; + + /** + * @var CalDavBackend + */ + protected $caldavBackend; + + /** + * @var IJobList + */ + protected $jobList; + + /** * BirthdayCalendar constructor. * * @param string $appName * @param IRequest $request * @param IDBConnection $db * @param IConfig $config + * @param IJobList $jobList + * @param IUserManager $userManager + * @param CalDavBackend $calDavBackend */ public function __construct($appName, IRequest $request, - IDBConnection $db, IConfig $config){ + IDBConnection $db, IConfig $config, + IJobList $jobList, + IUserManager $userManager, + CalDavBackend $calDavBackend){ parent::__construct($appName, $request); $this->db = $db; $this->config = $config; + $this->userManager = $userManager; + $this->jobList = $jobList; + $this->caldavBackend = $calDavBackend; } /** @@ -63,7 +92,12 @@ class BirthdayCalendarController extends Controller { public function enable() { $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes'); - // TODO schedule background job to regenerate + // add background job for each user + $this->userManager->callForAllUsers(function(IUser $user) { + $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [ + 'userId' => $user->getUID(), + ]); + }); return new JSONResponse([]); } @@ -74,7 +108,8 @@ class BirthdayCalendarController extends Controller { public function disable() { $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no'); - // TODO delete all birthday calendars + $this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class); + $this->caldavBackend->deleteAllBirthdayCalendars(); return new JSONResponse([]); } diff --git a/apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php b/apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php new file mode 100644 index 00000000000..010289a745a --- /dev/null +++ b/apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php @@ -0,0 +1,103 @@ +<?php +/** + * @copyright Copyright (c) 2017, 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\BackgroundJob; + +use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob; +use OCA\DAV\CalDAV\BirthdayService; +use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\CalDAV\CalendarHome; +use OCP\IConfig; +use Sabre\DAV\MkCol; +use Test\TestCase; + +class GenerateBirthdayCalendarBackgroundJobTest extends TestCase { + + /** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject */ + private $birthdayService; + + /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ + private $config; + + /** @var \OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob */ + private $backgroundJob; + + protected function setUp() { + parent::setUp(); + + $this->birthdayService = $this->createMock(BirthdayService::class); + $this->config = $this->createMock(IConfig::class); + + $this->backgroundJob = new GenerateBirthdayCalendarBackgroundJob( + $this->birthdayService, $this->config); + } + + public function testRun() { + $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('syncUser') + ->with('user123'); + + $this->backgroundJob->run(['userId' => 'user123']); + } + + public function testRunGloballyDisabled() { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('no')); + + $this->config->expects($this->never()) + ->method('getUserValue'); + + $this->birthdayService->expects($this->never()) + ->method('syncUser'); + + $this->backgroundJob->run(['userId' => 'user123']); + } + + public function testRunUserDisabled() { + $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('no')); + + $this->birthdayService->expects($this->never()) + ->method('syncUser'); + + $this->backgroundJob->run(['userId' => 'user123']); + } +} diff --git a/apps/dav/tests/unit/Controller/BirthdayCalendarControllerTest.php b/apps/dav/tests/unit/Controller/BirthdayCalendarControllerTest.php index 8963546ffee..46ed58df4f9 100644 --- a/apps/dav/tests/unit/Controller/BirthdayCalendarControllerTest.php +++ b/apps/dav/tests/unit/Controller/BirthdayCalendarControllerTest.php @@ -23,10 +23,15 @@ namespace OCA\DAV\Tests\Unit\DAV\Controller; +use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob; +use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\Controller\BirthdayCalendarController; +use OCP\BackgroundJob\IJobList; use OCP\IConfig; use OCP\IDBConnection; use OCP\IRequest; +use OCP\IUser; +use OCP\IUserManager; use Test\TestCase; class BirthdayCalendarControllerTest extends TestCase { @@ -40,6 +45,15 @@ class BirthdayCalendarControllerTest extends TestCase { /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */ private $db; + /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */ + private $jobList; + + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + + /** @var CalDavBackend|\PHPUnit_Framework_MockObject_MockObject */ + private $caldav; + /** @var BirthdayCalendarController|\PHPUnit_Framework_MockObject_MockObject */ private $controller; @@ -49,9 +63,13 @@ class BirthdayCalendarControllerTest extends TestCase { $this->config = $this->createMock(IConfig::class); $this->request = $this->createMock(IRequest::class); $this->db = $this->createMock(IDBConnection::class); + $this->jobList = $this->createMock(IJobList::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->caldav = $this->createMock(CalDavBackend::class); $this->controller = new BirthdayCalendarController('dav', - $this->request, $this->db, $this->config); + $this->request, $this->db, $this->config, $this->jobList, + $this->userManager, $this->caldav); } public function testEnable() { @@ -59,6 +77,31 @@ class BirthdayCalendarControllerTest extends TestCase { ->method('setAppValue') ->with('dav', 'generateBirthdayCalendar', 'yes'); + $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']); + $this->jobList->expects($this->at(1)) + ->method('add') + ->with(GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid2']); + $this->jobList->expects($this->at(2)) + ->method('add') + ->with(GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid3']); + $response = $this->controller->enable(); $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response); } @@ -67,6 +110,11 @@ class BirthdayCalendarControllerTest extends TestCase { $this->config->expects($this->once()) ->method('setAppValue') ->with('dav', 'generateBirthdayCalendar', 'no'); + $this->jobList->expects($this->once()) + ->method('remove') + ->with(GenerateBirthdayCalendarBackgroundJob::class); + $this->caldav->expects($this->once()) + ->method('deleteAllBirthdayCalendars'); $response = $this->controller->disable(); $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response); |