2015-12-10 17:53:44 +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>
|
2020-04-29 12:57:22 +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 Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2015-12-10 17:53:44 +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/>
|
2015-12-10 17:53:44 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\CardDAV;
|
|
|
|
|
2016-11-17 12:27:46 +03:00
|
|
|
use OC\Accounts\AccountManager;
|
2016-05-25 17:04:15 +03:00
|
|
|
use OCA\DAV\CardDAV\CardDavBackend;
|
|
|
|
use OCA\DAV\CardDAV\SyncService;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\ILogger;
|
2016-01-14 15:01:10 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2015-12-10 17:53:44 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class SyncServiceTest extends TestCase {
|
|
|
|
public function testEmptySync() {
|
|
|
|
$backend = $this->getBackendMock(0, 0, 0);
|
|
|
|
|
|
|
|
$ss = $this->getSyncServiceMock($backend, []);
|
2017-03-09 13:36:54 +03:00
|
|
|
$return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
2015-12-10 17:53:44 +03:00
|
|
|
$this->assertEquals('sync-token-1', $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSyncWithNewElement() {
|
|
|
|
$backend = $this->getBackendMock(1, 0, 0);
|
|
|
|
$backend->method('getCard')->willReturn(false);
|
|
|
|
|
|
|
|
$ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
|
2017-03-09 13:36:54 +03:00
|
|
|
$return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
2015-12-10 17:53:44 +03:00
|
|
|
$this->assertEquals('sync-token-1', $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSyncWithUpdatedElement() {
|
|
|
|
$backend = $this->getBackendMock(0, 1, 0);
|
|
|
|
$backend->method('getCard')->willReturn(true);
|
|
|
|
|
|
|
|
$ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
|
2017-03-09 13:36:54 +03:00
|
|
|
$return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
2015-12-10 17:53:44 +03:00
|
|
|
$this->assertEquals('sync-token-1', $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSyncWithDeletedElement() {
|
|
|
|
$backend = $this->getBackendMock(0, 0, 1);
|
|
|
|
|
|
|
|
$ss = $this->getSyncServiceMock($backend, ['0' => [404 => '']]);
|
2017-03-09 13:36:54 +03:00
|
|
|
$return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
2015-12-10 17:53:44 +03:00
|
|
|
$this->assertEquals('sync-token-1', $return);
|
|
|
|
}
|
|
|
|
|
2016-01-13 13:55:06 +03:00
|
|
|
public function testEnsureSystemAddressBookExists() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $backend */
|
2016-10-13 13:15:10 +03:00
|
|
|
$backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
|
2016-01-13 13:55:06 +03:00
|
|
|
$backend->expects($this->exactly(1))->method('createAddressBook');
|
|
|
|
$backend->expects($this->at(0))->method('getAddressBooksByUri')->willReturn(null);
|
|
|
|
$backend->expects($this->at(1))->method('getAddressBooksByUri')->willReturn([]);
|
|
|
|
|
2016-01-14 15:01:10 +03:00
|
|
|
/** @var IUserManager $userManager */
|
2017-10-24 16:26:53 +03:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();
|
2016-11-17 12:27:46 +03:00
|
|
|
$ss = new SyncService($backend, $userManager, $logger, $accountManager);
|
2017-11-06 11:43:45 +03:00
|
|
|
$ss->ensureSystemAddressBookExists('principals/users/adam', 'contacts', []);
|
2016-01-13 13:55:06 +03:00
|
|
|
}
|
|
|
|
|
2018-08-06 13:34:56 +03:00
|
|
|
public function dataActivatedUsers() {
|
|
|
|
return [
|
|
|
|
[true, 1, 1, 1],
|
|
|
|
[false, 0, 0, 3],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataActivatedUsers
|
|
|
|
*
|
|
|
|
* @param boolean $activated
|
|
|
|
* @param integer $createCalls
|
|
|
|
* @param integer $updateCalls
|
|
|
|
* @param integer $deleteCalls
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUpdateAndDeleteUser($activated, $createCalls, $updateCalls, $deleteCalls) {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $backend */
|
2016-10-13 13:15:10 +03:00
|
|
|
$backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
|
2016-01-14 15:01:10 +03:00
|
|
|
|
2018-08-06 13:34:56 +03:00
|
|
|
$backend->expects($this->exactly($createCalls))->method('createCard');
|
|
|
|
$backend->expects($this->exactly($updateCalls))->method('updateCard');
|
|
|
|
$backend->expects($this->exactly($deleteCalls))->method('deleteCard');
|
2016-01-14 15:01:10 +03:00
|
|
|
|
|
|
|
$backend->method('getCard')->willReturnOnConsecutiveCalls(false, [
|
|
|
|
'carddata' => "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.8//EN\r\nUID:test-user\r\nFN:test-user\r\nN:test-user;;;;\r\nEND:VCARD\r\n\r\n"
|
|
|
|
]);
|
|
|
|
|
2019-11-22 14:32:35 +03:00
|
|
|
$backend->method('getAddressBooksByUri')
|
|
|
|
->with('principals/system/system', 'system')
|
|
|
|
->willReturn(['id' => -1]);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
|
2017-10-24 16:26:53 +03:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
|
2016-01-14 15:01:10 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUser | \PHPUnit\Framework\MockObject\MockObject $user */
|
2017-10-24 16:26:53 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
|
2016-01-14 15:01:10 +03:00
|
|
|
$user->method('getBackendClassName')->willReturn('unittest');
|
|
|
|
$user->method('getUID')->willReturn('test-user');
|
2016-11-17 12:27:46 +03:00
|
|
|
$user->method('getCloudId')->willReturn('cloudId');
|
2017-03-13 16:58:37 +03:00
|
|
|
$user->method('getDisplayName')->willReturn('test-user');
|
2018-08-06 13:34:56 +03:00
|
|
|
$user->method('isEnabled')->willReturn($activated);
|
2017-10-24 16:26:53 +03:00
|
|
|
$accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();
|
2016-11-17 12:27:46 +03:00
|
|
|
$accountManager->expects($this->any())->method('getUser')
|
|
|
|
->willReturn([
|
2020-04-09 10:22:29 +03:00
|
|
|
AccountManager::PROPERTY_DISPLAYNAME =>
|
|
|
|
[
|
|
|
|
'value' => $user->getDisplayName(),
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_ADDRESS =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_WEBSITE =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_EMAIL =>
|
|
|
|
[
|
|
|
|
'value' => $user->getEMailAddress(),
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_AVATAR =>
|
|
|
|
[
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_PHONE =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_TWITTER =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
]
|
2017-03-13 16:58:37 +03:00
|
|
|
);
|
2016-11-17 12:27:46 +03:00
|
|
|
|
|
|
|
$ss = new SyncService($backend, $userManager, $logger, $accountManager);
|
2016-01-14 15:01:10 +03:00
|
|
|
$ss->updateUser($user);
|
|
|
|
|
|
|
|
$ss->updateUser($user);
|
|
|
|
|
|
|
|
$ss->deleteUser($user);
|
|
|
|
}
|
|
|
|
|
2015-12-10 17:53:44 +03:00
|
|
|
/**
|
|
|
|
* @param int $createCount
|
|
|
|
* @param int $updateCount
|
|
|
|
* @param int $deleteCount
|
2020-08-11 22:32:18 +03:00
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject
|
2015-12-10 17:53:44 +03:00
|
|
|
*/
|
|
|
|
private function getBackendMock($createCount, $updateCount, $deleteCount) {
|
2016-10-13 13:15:10 +03:00
|
|
|
$backend = $this->getMockBuilder(CardDavBackend::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-12-10 17:53:44 +03:00
|
|
|
$backend->expects($this->exactly($createCount))->method('createCard');
|
|
|
|
$backend->expects($this->exactly($updateCount))->method('updateCard');
|
|
|
|
$backend->expects($this->exactly($deleteCount))->method('deleteCard');
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $backend
|
|
|
|
* @param $response
|
2020-08-11 22:32:18 +03:00
|
|
|
* @return SyncService|\PHPUnit\Framework\MockObject\MockObject
|
2015-12-10 17:53:44 +03:00
|
|
|
*/
|
|
|
|
private function getSyncServiceMock($backend, $response) {
|
2017-10-24 16:26:53 +03:00
|
|
|
$userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $ss */
|
2016-10-13 13:15:10 +03:00
|
|
|
$ss = $this->getMockBuilder(SyncService::class)
|
2017-03-23 18:10:53 +03:00
|
|
|
->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download', 'getCertPath'])
|
2016-11-17 12:27:46 +03:00
|
|
|
->setConstructorArgs([$backend, $userManager, $logger, $accountManager])
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2015-12-10 17:53:44 +03:00
|
|
|
$ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']);
|
|
|
|
$ss->method('ensureSystemAddressBookExists')->willReturn(['id' => 1]);
|
|
|
|
$ss->method('download')->willReturn([
|
|
|
|
'body' => '',
|
|
|
|
'statusCode' => 200,
|
|
|
|
'headers' => []
|
|
|
|
]);
|
2017-03-23 18:10:53 +03:00
|
|
|
$ss->method('getCertPath')->willReturn('');
|
2015-12-10 17:53:44 +03:00
|
|
|
return $ss;
|
|
|
|
}
|
|
|
|
}
|