1
0

SyncServiceTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  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 OCA\DAV\CardDAV\CardDavBackend;
  30. use OCA\DAV\CardDAV\Converter;
  31. use OCA\DAV\CardDAV\SyncService;
  32. use OCP\IDBConnection;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use Psr\Log\LoggerInterface;
  36. use Sabre\VObject\Component\VCard;
  37. use Test\TestCase;
  38. class SyncServiceTest extends TestCase {
  39. public function testEmptySync(): void {
  40. $backend = $this->getBackendMock(0, 0, 0);
  41. $ss = $this->getSyncServiceMock($backend, []);
  42. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  43. $this->assertEquals('sync-token-1', $return);
  44. }
  45. public function testSyncWithNewElement(): void {
  46. $backend = $this->getBackendMock(1, 0, 0);
  47. $backend->method('getCard')->willReturn(false);
  48. $ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
  49. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  50. $this->assertEquals('sync-token-1', $return);
  51. }
  52. public function testSyncWithUpdatedElement(): void {
  53. $backend = $this->getBackendMock(0, 1, 0);
  54. $backend->method('getCard')->willReturn(true);
  55. $ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
  56. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  57. $this->assertEquals('sync-token-1', $return);
  58. }
  59. public function testSyncWithDeletedElement(): void {
  60. $backend = $this->getBackendMock(0, 0, 1);
  61. $ss = $this->getSyncServiceMock($backend, ['0' => [404 => '']]);
  62. $return = $ss->syncRemoteAddressBook('', 'system', 'system', '1234567890', null, '1', 'principals/system/system', []);
  63. $this->assertEquals('sync-token-1', $return);
  64. }
  65. public function testEnsureSystemAddressBookExists(): void {
  66. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $backend */
  67. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  68. $backend->expects($this->exactly(1))->method('createAddressBook');
  69. $backend->expects($this->exactly(2))
  70. ->method('getAddressBooksByUri')
  71. ->willReturnOnConsecutiveCalls(
  72. null,
  73. [],
  74. );
  75. /** @var IUserManager $userManager */
  76. $userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  77. $dbConnection = $this->createMock(IDBConnection::class);
  78. $logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  79. $converter = $this->createMock(Converter::class);
  80. $ss = new SyncService($backend, $userManager, $dbConnection, $logger, $converter);
  81. $ss->ensureSystemAddressBookExists('principals/users/adam', 'contacts', []);
  82. }
  83. public function dataActivatedUsers() {
  84. return [
  85. [true, 1, 1, 1],
  86. [false, 0, 0, 3],
  87. ];
  88. }
  89. /**
  90. * @dataProvider dataActivatedUsers
  91. *
  92. * @param boolean $activated
  93. * @param integer $createCalls
  94. * @param integer $updateCalls
  95. * @param integer $deleteCalls
  96. * @return void
  97. */
  98. public function testUpdateAndDeleteUser($activated, $createCalls, $updateCalls, $deleteCalls): void {
  99. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $backend */
  100. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  101. $logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  102. $backend->expects($this->exactly($createCalls))->method('createCard');
  103. $backend->expects($this->exactly($updateCalls))->method('updateCard');
  104. $backend->expects($this->exactly($deleteCalls))->method('deleteCard');
  105. $backend->method('getCard')->willReturnOnConsecutiveCalls(false, [
  106. '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"
  107. ]);
  108. $backend->method('getAddressBooksByUri')
  109. ->with('principals/system/system', 'system')
  110. ->willReturn(['id' => -1]);
  111. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
  112. $userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  113. $dbConnection = $this->createMock(IDBConnection::class);
  114. /** @var IUser | \PHPUnit\Framework\MockObject\MockObject $user */
  115. $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  116. $user->method('getBackendClassName')->willReturn('unittest');
  117. $user->method('getUID')->willReturn('test-user');
  118. $user->method('getCloudId')->willReturn('cloudId');
  119. $user->method('getDisplayName')->willReturn('test-user');
  120. $user->method('isEnabled')->willReturn($activated);
  121. $converter = $this->createMock(Converter::class);
  122. $converter->expects($this->any())
  123. ->method('createCardFromUser')
  124. ->willReturn($this->createMock(VCard::class));
  125. $ss = new SyncService($backend, $userManager, $dbConnection, $logger, $converter);
  126. $ss->updateUser($user);
  127. $ss->updateUser($user);
  128. $ss->deleteUser($user);
  129. }
  130. /**
  131. * @param int $createCount
  132. * @param int $updateCount
  133. * @param int $deleteCount
  134. * @return \PHPUnit\Framework\MockObject\MockObject|CardDavBackend
  135. */
  136. private function getBackendMock($createCount, $updateCount, $deleteCount) {
  137. $backend = $this->getMockBuilder(CardDavBackend::class)
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $backend->expects($this->exactly($createCount))->method('createCard');
  141. $backend->expects($this->exactly($updateCount))->method('updateCard');
  142. $backend->expects($this->exactly($deleteCount))->method('deleteCard');
  143. return $backend;
  144. }
  145. /**
  146. * @param $backend
  147. * @param $response
  148. * @return SyncService|\PHPUnit\Framework\MockObject\MockObject
  149. */
  150. private function getSyncServiceMock($backend, $response) {
  151. $userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  152. $dbConnection = $this->createMock(IDBConnection::class);
  153. $logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  154. $converter = $this->createMock(Converter::class);
  155. /** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $ss */
  156. $ss = $this->getMockBuilder(SyncService::class)
  157. ->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download', 'getCertPath'])
  158. ->setConstructorArgs([$backend, $userManager, $dbConnection, $logger, $converter])
  159. ->getMock();
  160. $ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']);
  161. $ss->method('ensureSystemAddressBookExists')->willReturn(['id' => 1]);
  162. $ss->method('download')->willReturn([
  163. 'body' => '',
  164. 'statusCode' => 200,
  165. 'headers' => []
  166. ]);
  167. $ss->method('getCertPath')->willReturn('');
  168. return $ss;
  169. }
  170. }