2016-04-19 00:08:37 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2016-04-19 00:08:37 +03: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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-04-19 00:08:37 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\DAV;
|
2016-04-19 00:08:37 +03:00
|
|
|
|
|
|
|
use OCA\DAV\CalDAV\CalDavBackend;
|
|
|
|
use OCA\DAV\CardDAV\CardDavBackend;
|
|
|
|
use OCA\DAV\CardDAV\SyncService;
|
|
|
|
use OCA\DAV\HookManager;
|
2016-09-27 22:12:24 +03:00
|
|
|
use OCP\IL10N;
|
2016-09-22 12:24:33 +03:00
|
|
|
use OCP\IUser;
|
2016-04-19 00:08:37 +03:00
|
|
|
use OCP\IUserManager;
|
2019-05-28 20:46:36 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-04-19 00:08:37 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class HookManagerTest extends TestCase {
|
2016-09-27 22:12:24 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var EventDispatcherInterface | \PHPUnit\Framework\MockObject\MockObject */
|
2016-11-17 12:27:46 +03:00
|
|
|
private $eventDispatcher;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-09-27 22:12:24 +03:00
|
|
|
parent::setUp();
|
2019-05-28 20:46:36 +03:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2016-09-27 22:12:24 +03:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
$this->l10n
|
|
|
|
->expects($this->any())
|
|
|
|
->method('t')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($text, $parameters = []) {
|
2016-09-27 22:12:24 +03:00
|
|
|
return vsprintf($text, $parameters);
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-09-27 22:12:24 +03:00
|
|
|
}
|
|
|
|
|
2016-04-19 00:08:37 +03:00
|
|
|
public function test() {
|
2016-09-22 12:24:33 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-04-19 00:08:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())->method('getUID')->willReturn('newUser');
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
|
2016-09-22 12:24:33 +03:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
2016-04-19 00:08:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
|
2016-09-22 12:24:33 +03:00
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
2016-04-19 00:08:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject $cal */
|
2016-09-22 12:24:33 +03:00
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
2016-04-19 00:08:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 16:11:33 +03:00
|
|
|
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
|
2016-04-19 00:08:37 +03:00
|
|
|
$cal->expects($this->once())->method('createCalendar')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'personal', ['{DAV:}displayname' => 'Personal']);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
|
2016-09-22 12:24:33 +03:00
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
2016-04-19 00:08:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 16:11:33 +03:00
|
|
|
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
|
2016-04-19 00:08:37 +03:00
|
|
|
$card->expects($this->once())->method('createAddressBook')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'contacts', ['{DAV:}displayname' => 'Contacts']);
|
|
|
|
|
2016-11-17 12:27:46 +03:00
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
|
2016-09-23 18:21:07 +03:00
|
|
|
$hm->firstLogin($user);
|
2016-04-19 00:08:37 +03:00
|
|
|
}
|
2016-04-21 13:34:20 +03:00
|
|
|
|
|
|
|
public function testWithExisting() {
|
2016-09-22 12:24:33 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())->method('getUID')->willReturn('newUser');
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
|
2016-09-22 12:24:33 +03:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
|
2016-09-22 12:24:33 +03:00
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject $cal */
|
2016-09-22 12:24:33 +03:00
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 16:11:33 +03:00
|
|
|
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
|
2016-04-21 13:34:20 +03:00
|
|
|
$cal->expects($this->never())->method('createCalendar');
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
|
2016-09-22 12:24:33 +03:00
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 16:11:33 +03:00
|
|
|
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
|
2016-04-21 13:34:20 +03:00
|
|
|
$card->expects($this->never())->method('createAddressBook');
|
|
|
|
|
2016-11-17 12:27:46 +03:00
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
|
2016-09-23 18:21:07 +03:00
|
|
|
$hm->firstLogin($user);
|
2016-04-21 13:34:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithBirthdayCalendar() {
|
2016-09-22 12:24:33 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())->method('getUID')->willReturn('newUser');
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
|
2016-09-22 12:24:33 +03:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
|
2016-09-22 12:24:33 +03:00
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject $cal */
|
2016-09-22 12:24:33 +03:00
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 16:11:33 +03:00
|
|
|
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
|
2016-04-21 13:34:20 +03:00
|
|
|
$cal->expects($this->once())->method('createCalendar')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'personal', ['{DAV:}displayname' => 'Personal']);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
|
2016-09-22 12:24:33 +03:00
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
2016-04-21 13:34:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 16:11:33 +03:00
|
|
|
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
|
2016-04-21 13:34:20 +03:00
|
|
|
$card->expects($this->once())->method('createAddressBook')->with(
|
|
|
|
'principals/users/newUser',
|
|
|
|
'contacts', ['{DAV:}displayname' => 'Contacts']);
|
|
|
|
|
2016-11-17 12:27:46 +03:00
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
|
2016-09-23 18:21:07 +03:00
|
|
|
$hm->firstLogin($user);
|
2016-04-21 13:34:20 +03:00
|
|
|
}
|
2016-09-22 12:24:33 +03:00
|
|
|
|
|
|
|
public function testDeleteCalendar() {
|
|
|
|
$user = $this->getMockBuilder(IUser::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
|
2016-09-22 12:24:33 +03:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$userManager->expects($this->once())->method('get')->willReturn($user);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
|
2016-09-22 12:24:33 +03:00
|
|
|
$syncService = $this->getMockBuilder(SyncService::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$syncService->expects($this->once())
|
|
|
|
->method('deleteUser');
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject $cal */
|
2016-09-22 12:24:33 +03:00
|
|
|
$cal = $this->getMockBuilder(CalDavBackend::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-25 14:35:53 +03:00
|
|
|
$cal->expects($this->once())->method('getUsersOwnCalendars')->willReturn([
|
2016-09-22 12:24:33 +03:00
|
|
|
['id' => 'personal']
|
|
|
|
]);
|
|
|
|
$cal->expects($this->once())->method('deleteCalendar');
|
2016-09-28 16:32:03 +03:00
|
|
|
$cal->expects($this->once())->method('deleteAllSharesByUser');
|
2016-09-22 12:24:33 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
|
2016-09-22 12:24:33 +03:00
|
|
|
$card = $this->getMockBuilder(CardDavBackend::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-02-14 12:47:07 +03:00
|
|
|
$card->expects($this->once())->method('getUsersOwnAddressBooks')->willReturn([
|
2016-09-22 12:24:33 +03:00
|
|
|
['id' => 'personal']
|
|
|
|
]);
|
|
|
|
$card->expects($this->once())->method('deleteAddressBook');
|
|
|
|
|
2016-11-17 12:27:46 +03:00
|
|
|
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
|
2016-09-22 12:24:33 +03:00
|
|
|
$hm->preDeleteUser(['uid' => 'newUser']);
|
|
|
|
$hm->postDeleteUser(['uid' => 'newUser']);
|
|
|
|
}
|
2016-04-19 00:08:37 +03:00
|
|
|
}
|