Merge pull request #24192 from owncloud/create-personal-calendar-stable9

Personal calendar should be generated even if the birthday calendar a…
This commit is contained in:
Thomas Müller 2016-04-22 14:23:09 +02:00
commit bbc720d2d3
2 changed files with 170 additions and 17 deletions

View File

@ -20,6 +20,7 @@
*/
namespace OCA\DAV;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\SyncService;
@ -100,24 +101,24 @@ class HookManager {
public function postLogin($params) {
$user = $this->userManager->get($params['uid']);
$principal = 'principals/users/' . $user->getUID();
$calendars = $this->calDav->getCalendarsForUser($principal);
if (empty($calendars)) {
try {
$this->calDav->createCalendar($principal, 'default', []);
} catch (\Exception $ex) {
\OC::$server->getLogger()->logException($ex);
if (!is_null($user)) {
$principal = 'principals/users/' . $user->getUID();
$calendars = $this->calDav->getCalendarsForUser($principal);
if (empty($calendars) || (count($calendars) === 1 && $calendars[0]['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI)) {
try {
$this->calDav->createCalendar($principal, 'default', []);
} catch (\Exception $ex) {
\OC::$server->getLogger()->logException($ex);
}
}
$books = $this->cardDav->getAddressBooksForUser($principal);
if (empty($books)) {
try {
$this->cardDav->createAddressBook($principal, 'default', []);
} catch (\Exception $ex) {
\OC::$server->getLogger()->logException($ex);
}
}
}
$books = $this->cardDav->getAddressBooksForUser($principal);
if (empty($books)) {
try {
$this->cardDav->createAddressBook($principal, 'default', []);
} catch (\Exception $ex) {
\OC::$server->getLogger()->logException($ex);
}
}
}
}

View File

@ -0,0 +1,152 @@
<?php
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @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;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\HookManager;
use OCP\IUserManager;
use Test\TestCase;
class HookManagerTest extends TestCase {
public function test() {
$user = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())->method('getUID')->willReturn('newUser');
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
$userManager = $this->getMockBuilder('\OCP\IUserManager')
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
$syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
->disableOriginalConstructor()
->getMock();
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
$cal = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')
->disableOriginalConstructor()
->getMock();
$cal->expects($this->once())->method('getCalendarsForUser')->willReturn([]);
$cal->expects($this->once())->method('createCalendar')->with(
'principals/users/newUser',
'default', []);
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
$card = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
->disableOriginalConstructor()
->getMock();
$card->expects($this->once())->method('getAddressBooksForUser')->willReturn([]);
$card->expects($this->once())->method('createAddressBook')->with(
'principals/users/newUser',
'default', []);
$hm = new HookManager($userManager, $syncService, $cal, $card);
$hm->postLogin(['uid' => 'newUser']);
}
public function testWithExisting() {
$user = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())->method('getUID')->willReturn('newUser');
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
$userManager = $this->getMockBuilder('\OCP\IUserManager')
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
$syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
->disableOriginalConstructor()
->getMock();
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
$cal = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')
->disableOriginalConstructor()
->getMock();
$cal->expects($this->once())->method('getCalendarsForUser')->willReturn([
['uri' => 'my-events']
]);
$cal->expects($this->never())->method('createCalendar');
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
$card = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
->disableOriginalConstructor()
->getMock();
$card->expects($this->once())->method('getAddressBooksForUser')->willReturn([
['uri' => 'my-contacts']
]);
$card->expects($this->never())->method('createAddressBook');
$hm = new HookManager($userManager, $syncService, $cal, $card);
$hm->postLogin(['uid' => 'newUser']);
}
public function testWithBirthdayCalendar() {
$user = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())->method('getUID')->willReturn('newUser');
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
$userManager = $this->getMockBuilder('\OCP\IUserManager')
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
$syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
->disableOriginalConstructor()
->getMock();
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
$cal = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')
->disableOriginalConstructor()
->getMock();
$cal->expects($this->once())->method('getCalendarsForUser')->willReturn([
['uri' => BirthdayService::BIRTHDAY_CALENDAR_URI]
]);
$cal->expects($this->once())->method('createCalendar')->with(
'principals/users/newUser',
'default', []);
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
$card = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
->disableOriginalConstructor()
->getMock();
$card->expects($this->once())->method('getAddressBooksForUser')->willReturn([]);
$card->expects($this->once())->method('createAddressBook')->with(
'principals/users/newUser',
'default', []);
$hm = new HookManager($userManager, $syncService, $cal, $card);
$hm->postLogin(['uid' => 'newUser']);
}
}