SyncServiceTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Citharel <nextcloud@tcit.fr>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\CardDAV;
  29. use OC\Accounts\AccountManager;
  30. use OCA\DAV\CardDAV\CardDavBackend;
  31. use OCA\DAV\CardDAV\SyncService;
  32. use OCP\ILogger;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use Test\TestCase;
  36. class SyncServiceTest extends TestCase {
  37. public function testEmptySync() {
  38. $backend = $this->getBackendMock(0, 0, 0);
  39. $ss = $this->getSyncServiceMock($backend, []);
  40. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  41. $this->assertEquals('sync-token-1', $return);
  42. }
  43. public function testSyncWithNewElement() {
  44. $backend = $this->getBackendMock(1, 0, 0);
  45. $backend->method('getCard')->willReturn(false);
  46. $ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
  47. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  48. $this->assertEquals('sync-token-1', $return);
  49. }
  50. public function testSyncWithUpdatedElement() {
  51. $backend = $this->getBackendMock(0, 1, 0);
  52. $backend->method('getCard')->willReturn(true);
  53. $ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
  54. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  55. $this->assertEquals('sync-token-1', $return);
  56. }
  57. public function testSyncWithDeletedElement() {
  58. $backend = $this->getBackendMock(0, 0, 1);
  59. $ss = $this->getSyncServiceMock($backend, ['0' => [404 => '']]);
  60. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  61. $this->assertEquals('sync-token-1', $return);
  62. }
  63. public function testEnsureSystemAddressBookExists() {
  64. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $backend */
  65. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  66. $backend->expects($this->exactly(1))->method('createAddressBook');
  67. $backend->expects($this->at(0))->method('getAddressBooksByUri')->willReturn(null);
  68. $backend->expects($this->at(1))->method('getAddressBooksByUri')->willReturn([]);
  69. /** @var IUserManager $userManager */
  70. $userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  71. $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
  72. $accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();
  73. $ss = new SyncService($backend, $userManager, $logger, $accountManager);
  74. $ss->ensureSystemAddressBookExists('principals/users/adam', 'contacts', []);
  75. }
  76. public function dataActivatedUsers() {
  77. return [
  78. [true, 1, 1, 1],
  79. [false, 0, 0, 3],
  80. ];
  81. }
  82. /**
  83. * @dataProvider dataActivatedUsers
  84. *
  85. * @param boolean $activated
  86. * @param integer $createCalls
  87. * @param integer $updateCalls
  88. * @param integer $deleteCalls
  89. * @return void
  90. */
  91. public function testUpdateAndDeleteUser($activated, $createCalls, $updateCalls, $deleteCalls) {
  92. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $backend */
  93. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  94. $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
  95. $backend->expects($this->exactly($createCalls))->method('createCard');
  96. $backend->expects($this->exactly($updateCalls))->method('updateCard');
  97. $backend->expects($this->exactly($deleteCalls))->method('deleteCard');
  98. $backend->method('getCard')->willReturnOnConsecutiveCalls(false, [
  99. '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"
  100. ]);
  101. $backend->method('getAddressBooksByUri')
  102. ->with('principals/system/system', 'system')
  103. ->willReturn(['id' => -1]);
  104. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
  105. $userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  106. /** @var IUser | \PHPUnit\Framework\MockObject\MockObject $user */
  107. $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  108. $user->method('getBackendClassName')->willReturn('unittest');
  109. $user->method('getUID')->willReturn('test-user');
  110. $user->method('getCloudId')->willReturn('cloudId');
  111. $user->method('getDisplayName')->willReturn('test-user');
  112. $user->method('isEnabled')->willReturn($activated);
  113. $accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();
  114. $accountManager->expects($this->any())->method('getUser')
  115. ->willReturn([
  116. AccountManager::PROPERTY_DISPLAYNAME =>
  117. [
  118. 'value' => $user->getDisplayName(),
  119. 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
  120. ],
  121. AccountManager::PROPERTY_ADDRESS =>
  122. [
  123. 'value' => '',
  124. 'scope' => AccountManager::VISIBILITY_PRIVATE,
  125. ],
  126. AccountManager::PROPERTY_WEBSITE =>
  127. [
  128. 'value' => '',
  129. 'scope' => AccountManager::VISIBILITY_PRIVATE,
  130. ],
  131. AccountManager::PROPERTY_EMAIL =>
  132. [
  133. 'value' => $user->getEMailAddress(),
  134. 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
  135. ],
  136. AccountManager::PROPERTY_AVATAR =>
  137. [
  138. 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY
  139. ],
  140. AccountManager::PROPERTY_PHONE =>
  141. [
  142. 'value' => '',
  143. 'scope' => AccountManager::VISIBILITY_PRIVATE,
  144. ],
  145. AccountManager::PROPERTY_TWITTER =>
  146. [
  147. 'value' => '',
  148. 'scope' => AccountManager::VISIBILITY_PRIVATE,
  149. ],
  150. ]
  151. );
  152. $ss = new SyncService($backend, $userManager, $logger, $accountManager);
  153. $ss->updateUser($user);
  154. $ss->updateUser($user);
  155. $ss->deleteUser($user);
  156. }
  157. /**
  158. * @param int $createCount
  159. * @param int $updateCount
  160. * @param int $deleteCount
  161. * @return \PHPUnit\Framework\MockObject\MockObject
  162. */
  163. private function getBackendMock($createCount, $updateCount, $deleteCount) {
  164. $backend = $this->getMockBuilder(CardDavBackend::class)
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $backend->expects($this->exactly($createCount))->method('createCard');
  168. $backend->expects($this->exactly($updateCount))->method('updateCard');
  169. $backend->expects($this->exactly($deleteCount))->method('deleteCard');
  170. return $backend;
  171. }
  172. /**
  173. * @param $backend
  174. * @param $response
  175. * @return SyncService|\PHPUnit\Framework\MockObject\MockObject
  176. */
  177. private function getSyncServiceMock($backend, $response) {
  178. $userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  179. $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
  180. $accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();
  181. /** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $ss */
  182. $ss = $this->getMockBuilder(SyncService::class)
  183. ->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download', 'getCertPath'])
  184. ->setConstructorArgs([$backend, $userManager, $logger, $accountManager])
  185. ->getMock();
  186. $ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']);
  187. $ss->method('ensureSystemAddressBookExists')->willReturn(['id' => 1]);
  188. $ss->method('download')->willReturn([
  189. 'body' => '',
  190. 'statusCode' => 200,
  191. 'headers' => []
  192. ]);
  193. $ss->method('getCertPath')->willReturn('');
  194. return $ss;
  195. }
  196. }