Skip to content

Commit

Permalink
generate birthday calendars in a background job after admin enabled them
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Ehrke <[email protected]>
  • Loading branch information
georgehrke committed Nov 11, 2017
1 parent a87d986 commit 2b51d84
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 4 deletions.
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* @copyright 2017 Georg Ehrke <[email protected]>
*
* @author Georg Ehrke <[email protected]>
*
* @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);
}
}
16 changes: 16 additions & 0 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,22 @@ public function updateProperties($calendarId, $objectUri, $calendarData) {
}
}

/**
* 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
*
Expand Down
41 changes: 38 additions & 3 deletions apps/dav/lib/Controller/BirthdayCalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -42,19 +47,43 @@ 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;
}

/**
Expand All @@ -63,7 +92,12 @@ public function __construct($appName, IRequest $request,
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([]);
}
Expand All @@ -74,7 +108,8 @@ public function enable() {
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([]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* @copyright Copyright (c) 2017, Georg Ehrke
*
* @author Georg Ehrke <[email protected]>
*
* @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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand All @@ -49,16 +63,45 @@ public function setUp() {
$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() {
$this->config->expects($this->once())
->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);
}
Expand All @@ -67,6 +110,11 @@ public function testDisable() {
$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);
Expand Down

0 comments on commit 2b51d84

Please sign in to comment.