SyncFederationAddressbooksTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Tests;
  8. use OC\OCS\DiscoveryService;
  9. use OCA\Federation\DbHandler;
  10. use OCA\Federation\SyncFederationAddressBooks;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use Psr\Log\LoggerInterface;
  13. class SyncFederationAddressbooksTest extends \Test\TestCase {
  14. /** @var array */
  15. private $callBacks = [];
  16. /** @var MockObject | DiscoveryService */
  17. private $discoveryService;
  18. /** @var MockObject|LoggerInterface */
  19. private $logger;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->discoveryService = $this->getMockBuilder(DiscoveryService::class)
  23. ->disableOriginalConstructor()->getMock();
  24. $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
  25. $this->logger = $this->createMock(LoggerInterface::class);
  26. }
  27. public function testSync(): void {
  28. /** @var DbHandler | MockObject $dbHandler */
  29. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $dbHandler->method('getAllServer')
  33. ->willReturn([
  34. [
  35. 'url' => 'https://cloud.drop.box',
  36. 'url_hash' => 'sha1',
  37. 'shared_secret' => 'iloveowncloud',
  38. 'sync_token' => '0'
  39. ]
  40. ]);
  41. $dbHandler->expects($this->once())->method('setServerStatus')->
  42. with('https://cloud.drop.box', 1, '1');
  43. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  47. ->willReturn('1');
  48. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  49. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
  50. $s->syncThemAll(function ($url, $ex) {
  51. $this->callBacks[] = [$url, $ex];
  52. });
  53. $this->assertEquals('1', count($this->callBacks));
  54. }
  55. public function testException(): void {
  56. /** @var DbHandler | MockObject $dbHandler */
  57. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')->
  58. disableOriginalConstructor()->
  59. getMock();
  60. $dbHandler->method('getAllServer')->
  61. willReturn([
  62. [
  63. 'url' => 'https://cloud.drop.box',
  64. 'url_hash' => 'sha1',
  65. 'shared_secret' => 'iloveowncloud',
  66. 'sync_token' => '0'
  67. ]
  68. ]);
  69. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  73. ->willThrowException(new \Exception('something did not work out'));
  74. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  75. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
  76. $s->syncThemAll(function ($url, $ex) {
  77. $this->callBacks[] = [$url, $ex];
  78. });
  79. $this->assertEquals(2, count($this->callBacks));
  80. }
  81. public function testSuccessfulSyncWithoutChangesAfterFailure(): void {
  82. /** @var DbHandler | MockObject $dbHandler */
  83. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $dbHandler->method('getAllServer')
  87. ->willReturn([
  88. [
  89. 'url' => 'https://cloud.drop.box',
  90. 'url_hash' => 'sha1',
  91. 'shared_secret' => 'ilovenextcloud',
  92. 'sync_token' => '0'
  93. ]
  94. ]);
  95. $dbHandler->method('getServerStatus')->willReturn(\OCA\Federation\TrustedServers::STATUS_FAILURE);
  96. $dbHandler->expects($this->once())->method('setServerStatus')->
  97. with('https://cloud.drop.box', 1);
  98. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  102. ->willReturn('0');
  103. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  104. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
  105. $s->syncThemAll(function ($url, $ex) {
  106. $this->callBacks[] = [$url, $ex];
  107. });
  108. $this->assertEquals('1', count($this->callBacks));
  109. }
  110. }