summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-10 15:53:44 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-01-12 14:24:47 +0100
commit451ab84d112b1362edc58eb48a970e186d6f6701 (patch)
tree06afac12503ccca90f5b0ec6531dbd7d84c1635e /apps
parent1a20af253bc90de35a1bf3b65adb33ab44d17c1d (diff)
downloadnextcloud-server-451ab84d112b1362edc58eb48a970e186d6f6701.tar.gz
nextcloud-server-451ab84d112b1362edc58eb48a970e186d6f6701.zip
Adding unit tests for the sync service
Diffstat (limited to 'apps')
-rw-r--r--apps/dav/lib/carddav/syncservice.php2
-rw-r--r--apps/dav/tests/unit/carddav/syncservicetest.php93
2 files changed, 94 insertions, 1 deletions
diff --git a/apps/dav/lib/carddav/syncservice.php b/apps/dav/lib/carddav/syncservice.php
index bfaf09f4c0e..dbe10bbcc81 100644
--- a/apps/dav/lib/carddav/syncservice.php
+++ b/apps/dav/lib/carddav/syncservice.php
@@ -96,7 +96,7 @@ class SyncService {
* @param string $syncToken
* @return array
*/
- private function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
+ protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
$settings = [
'baseUri' => $url,
'userName' => $userName,
diff --git a/apps/dav/tests/unit/carddav/syncservicetest.php b/apps/dav/tests/unit/carddav/syncservicetest.php
new file mode 100644
index 00000000000..f1ef934b8c6
--- /dev/null
+++ b/apps/dav/tests/unit/carddav/syncservicetest.php
@@ -0,0 +1,93 @@
+<?php
+/**
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2015, 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\CardDAV;
+
+use Test\TestCase;
+
+class SyncServiceTest extends TestCase {
+ public function testEmptySync() {
+ $backend = $this->getBackendMock(0, 0, 0);
+
+ $ss = $this->getSyncServiceMock($backend, []);
+ $return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
+ $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 => '']]);
+ $return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
+ $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 => '']]);
+ $return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
+ $this->assertEquals('sync-token-1', $return);
+ }
+
+ public function testSyncWithDeletedElement() {
+ $backend = $this->getBackendMock(0, 0, 1);
+// $backend->method('getCard')->willReturn(true);
+
+ $ss = $this->getSyncServiceMock($backend, ['0' => [404 => '']]);
+ $return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
+ $this->assertEquals('sync-token-1', $return);
+ }
+
+ /**
+ * @param int $createCount
+ * @param int $updateCount
+ * @param int $deleteCount
+ * @return \PHPUnit_Framework_MockObject_MockObject
+ */
+ private function getBackendMock($createCount, $updateCount, $deleteCount) {
+ $backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDAVBackend')->disableOriginalConstructor()->getMock();
+ $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
+ * @return SyncService|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private function getSyncServiceMock($backend, $response) {
+ /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $ss */
+ $ss = $this->getMock('OCA\DAV\CardDAV\SyncService', ['ensureSystemAddressBookExists', 'requestSyncReport', 'download'], [$backend]);
+ $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' => []
+ ]);
+ return $ss;
+ }
+
+}