SyncFederationAddressbooksTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Federation\Tests;
  30. use OC\OCS\DiscoveryService;
  31. use OCA\Federation\DbHandler;
  32. use OCA\Federation\SyncFederationAddressBooks;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Psr\Log\LoggerInterface;
  35. class SyncFederationAddressbooksTest extends \Test\TestCase {
  36. /** @var array */
  37. private $callBacks = [];
  38. /** @var MockObject | DiscoveryService */
  39. private $discoveryService;
  40. /** @var MockObject|LoggerInterface */
  41. private $logger;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->discoveryService = $this->getMockBuilder(DiscoveryService::class)
  45. ->disableOriginalConstructor()->getMock();
  46. $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
  47. $this->logger = $this->createMock(LoggerInterface::class);
  48. }
  49. public function testSync() {
  50. /** @var DbHandler | MockObject $dbHandler */
  51. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $dbHandler->method('getAllServer')
  55. ->willReturn([
  56. [
  57. 'url' => 'https://cloud.drop.box',
  58. 'url_hash' => 'sha1',
  59. 'shared_secret' => 'iloveowncloud',
  60. 'sync_token' => '0'
  61. ]
  62. ]);
  63. $dbHandler->expects($this->once())->method('setServerStatus')->
  64. with('https://cloud.drop.box', 1, '1');
  65. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  69. ->willReturn('1');
  70. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  71. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
  72. $s->syncThemAll(function ($url, $ex) {
  73. $this->callBacks[] = [$url, $ex];
  74. });
  75. $this->assertEquals('1', count($this->callBacks));
  76. }
  77. public function testException() {
  78. /** @var DbHandler | MockObject $dbHandler */
  79. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')->
  80. disableOriginalConstructor()->
  81. getMock();
  82. $dbHandler->method('getAllServer')->
  83. willReturn([
  84. [
  85. 'url' => 'https://cloud.drop.box',
  86. 'url_hash' => 'sha1',
  87. 'shared_secret' => 'iloveowncloud',
  88. 'sync_token' => '0'
  89. ]
  90. ]);
  91. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  95. ->willThrowException(new \Exception('something did not work out'));
  96. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  97. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
  98. $s->syncThemAll(function ($url, $ex) {
  99. $this->callBacks[] = [$url, $ex];
  100. });
  101. $this->assertEquals(2, count($this->callBacks));
  102. }
  103. public function testSuccessfulSyncWithoutChangesAfterFailure() {
  104. /** @var DbHandler | MockObject $dbHandler */
  105. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $dbHandler->method('getAllServer')
  109. ->willReturn([
  110. [
  111. 'url' => 'https://cloud.drop.box',
  112. 'url_hash' => 'sha1',
  113. 'shared_secret' => 'ilovenextcloud',
  114. 'sync_token' => '0'
  115. ]
  116. ]);
  117. $dbHandler->method('getServerStatus')->willReturn(\OCA\Federation\TrustedServers::STATUS_FAILURE);
  118. $dbHandler->expects($this->once())->method('setServerStatus')->
  119. with('https://cloud.drop.box', 1);
  120. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  124. ->willReturn('0');
  125. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  126. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
  127. $s->syncThemAll(function ($url, $ex) {
  128. $this->callBacks[] = [$url, $ex];
  129. });
  130. $this->assertEquals('1', count($this->callBacks));
  131. }
  132. }