SyncFederationAddressbooksTest.php 4.0 KB

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