2016-04-18 21:08:37 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-04-18 21:08:37 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 14:04:15 +00:00
|
|
|
namespace OCA\DAV\Tests\unit\DAV;
|
2016-04-18 21:08:37 +00:00
|
|
|
|
2016-04-21 10:34:20 +00:00
|
|
|
use OCA\DAV\CalDAV\BirthdayService;
|
2016-04-18 21:08:37 +00:00
|
|
|
use OCA\DAV\CalDAV\CalDavBackend;
|
|
|
|
use OCA\DAV\CardDAV\CardDavBackend;
|
|
|
|
use OCA\DAV\CardDAV\SyncService;
|
|
|
|
use OCA\DAV\HookManager;
|
2016-09-22 09:24:33 +00:00
|
|
|
use OCP\IUser;
|
2016-04-18 21:08:37 +00:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class HookManagerTest extends TestCase {
|
|
|
|
public function test() {
|
2016-09-22 09:24:33 +00:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-04-18 21:08:37 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())->method('getUID')->willReturn('newUser');
|
|
|
|
|
|
|
|
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
|
2016-09-22 09:24:33 +00:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
2016-04-18 21:08:37 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$userManager->expects($this->once())->method('get')->willReturn($user);
|
|
|
|
|
|
|
|
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
|
2016-09-22 09:24:33 +00:00
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
2016-04-18 21:08:37 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
|
2016-09-22 09:24:33 +00:00
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
2016-04-18 21:08:37 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 13:11:33 +00:00
|
|
|
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
|
2016-04-18 21:08:37 +00:00
|
|
|
$cal->expects($this->once())->method('createCalendar')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'personal', ['{DAV:}displayname' => 'Personal']);
|
|
|
|
|
|
|
|
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
|
2016-09-22 09:24:33 +00:00
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
2016-04-18 21:08:37 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 13:11:33 +00:00
|
|
|
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
|
2016-04-18 21:08:37 +00:00
|
|
|
$card->expects($this->once())->method('createAddressBook')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'contacts', ['{DAV:}displayname' => 'Contacts']);
|
|
|
|
|
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card);
|
|
|
|
$hm->postLogin(['uid' => 'newUser']);
|
|
|
|
}
|
2016-04-21 10:34:20 +00:00
|
|
|
|
|
|
|
public function testWithExisting() {
|
2016-09-22 09:24:33 +00:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())->method('getUID')->willReturn('newUser');
|
|
|
|
|
|
|
|
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
|
2016-09-22 09:24:33 +00:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$userManager->expects($this->once())->method('get')->willReturn($user);
|
|
|
|
|
|
|
|
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
|
2016-09-22 09:24:33 +00:00
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
|
2016-09-22 09:24:33 +00:00
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 13:11:33 +00:00
|
|
|
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
|
2016-04-21 10:34:20 +00:00
|
|
|
$cal->expects($this->never())->method('createCalendar');
|
|
|
|
|
|
|
|
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
|
2016-09-22 09:24:33 +00:00
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 13:11:33 +00:00
|
|
|
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
|
2016-04-21 10:34:20 +00:00
|
|
|
$card->expects($this->never())->method('createAddressBook');
|
|
|
|
|
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card);
|
|
|
|
$hm->postLogin(['uid' => 'newUser']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithBirthdayCalendar() {
|
2016-09-22 09:24:33 +00:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())->method('getUID')->willReturn('newUser');
|
|
|
|
|
|
|
|
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
|
2016-09-22 09:24:33 +00:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$userManager->expects($this->once())->method('get')->willReturn($user);
|
|
|
|
|
|
|
|
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
|
2016-09-22 09:24:33 +00:00
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
|
2016-09-22 09:24:33 +00:00
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 13:11:33 +00:00
|
|
|
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
|
2016-04-21 10:34:20 +00:00
|
|
|
$cal->expects($this->once())->method('createCalendar')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'personal', ['{DAV:}displayname' => 'Personal']);
|
|
|
|
|
|
|
|
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
|
2016-09-22 09:24:33 +00:00
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
2016-04-21 10:34:20 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 13:11:33 +00:00
|
|
|
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
|
2016-04-21 10:34:20 +00:00
|
|
|
$card->expects($this->once())->method('createAddressBook')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'contacts', ['{DAV:}displayname' => 'Contacts']);
|
|
|
|
|
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card);
|
|
|
|
$hm->postLogin(['uid' => 'newUser']);
|
|
|
|
}
|
2016-09-22 09:24:33 +00:00
|
|
|
|
|
|
|
public function testDeleteCalendar() {
|
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
|
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$userManager->expects($this->once())->method('get')->willReturn($user);
|
|
|
|
|
|
|
|
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
|
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$syncService->expects($this->once())
|
|
|
|
->method('deleteUser');
|
|
|
|
|
|
|
|
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
|
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$cal->expects($this->once())->method('getCalendarsForUser')->willReturn([
|
|
|
|
['id' => 'personal']
|
|
|
|
]);
|
|
|
|
$cal->expects($this->once())->method('deleteCalendar');
|
2016-09-23 12:30:24 +00:00
|
|
|
$cal->expects($this->once())->method('deleteAllSharesForUser');
|
2016-09-22 09:24:33 +00:00
|
|
|
|
|
|
|
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
|
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$card->expects($this->once())->method('getAddressBooksForUser')->willReturn([
|
|
|
|
['id' => 'personal']
|
|
|
|
]);
|
|
|
|
$card->expects($this->once())->method('deleteAddressBook');
|
|
|
|
|
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
|
|
|
|
$hm->preDeleteUser(['uid' => 'newUser']);
|
|
|
|
$hm->postDeleteUser(['uid' => 'newUser']);
|
|
|
|
}
|
2016-04-18 21:08:37 +00:00
|
|
|
}
|