NotificationsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing\Tests;
  8. use OCA\FederatedFileSharing\AddressHandler;
  9. use OCA\FederatedFileSharing\Notifications;
  10. use OCP\BackgroundJob\IJobList;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\Federation\ICloudFederationFactory;
  13. use OCP\Federation\ICloudFederationProviderManager;
  14. use OCP\Http\Client\IClientService;
  15. use OCP\OCS\IDiscoveryService;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Psr\Log\LoggerInterface;
  18. class NotificationsTest extends \Test\TestCase {
  19. /** @var AddressHandler|MockObject */
  20. private $addressHandler;
  21. /** @var IClientService|MockObject */
  22. private $httpClientService;
  23. /** @var IDiscoveryService|MockObject */
  24. private $discoveryService;
  25. /** @var IJobList|MockObject */
  26. private $jobList;
  27. /** @var ICloudFederationProviderManager|MockObject */
  28. private $cloudFederationProviderManager;
  29. /** @var ICloudFederationFactory|MockObject */
  30. private $cloudFederationFactory;
  31. /** @var IEventDispatcher|MockObject */
  32. private $eventDispatcher;
  33. /** @var LoggerInterface|MockObject */
  34. private $logger;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->jobList = $this->getMockBuilder('OCP\BackgroundJob\IJobList')->getMock();
  38. $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  39. $this->httpClientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->getMock();
  40. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  41. ->disableOriginalConstructor()->getMock();
  42. $this->logger = $this->createMock(LoggerInterface::class);
  43. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  44. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  45. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  46. }
  47. /**
  48. * get instance of Notifications class
  49. *
  50. * @param array $mockedMethods methods which should be mocked
  51. * @return Notifications | \PHPUnit\Framework\MockObject\MockObject
  52. */
  53. private function getInstance(array $mockedMethods = []) {
  54. if (empty($mockedMethods)) {
  55. $instance = new Notifications(
  56. $this->addressHandler,
  57. $this->httpClientService,
  58. $this->discoveryService,
  59. $this->jobList,
  60. $this->cloudFederationProviderManager,
  61. $this->cloudFederationFactory,
  62. $this->eventDispatcher,
  63. $this->logger,
  64. );
  65. } else {
  66. $instance = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
  67. ->setConstructorArgs(
  68. [
  69. $this->addressHandler,
  70. $this->httpClientService,
  71. $this->discoveryService,
  72. $this->jobList,
  73. $this->cloudFederationProviderManager,
  74. $this->cloudFederationFactory,
  75. $this->eventDispatcher,
  76. $this->logger,
  77. ]
  78. )->setMethods($mockedMethods)->getMock();
  79. }
  80. return $instance;
  81. }
  82. /**
  83. * @dataProvider dataTestSendUpdateToRemote
  84. *
  85. * @param int $try
  86. * @param array $httpRequestResult
  87. * @param bool $expected
  88. */
  89. public function testSendUpdateToRemote($try, $httpRequestResult, $expected): void {
  90. $remote = 'http://remote';
  91. $id = 42;
  92. $timestamp = 63576;
  93. $token = 'token';
  94. $action = 'unshare';
  95. $instance = $this->getInstance(['tryHttpPostToShareEndpoint', 'getTimestamp']);
  96. $instance->expects($this->any())->method('getTimestamp')->willReturn($timestamp);
  97. $instance->expects($this->once())->method('tryHttpPostToShareEndpoint')
  98. ->with($remote, '/' . $id . '/unshare', ['token' => $token, 'data1Key' => 'data1Value', 'remoteId' => $id], $action)
  99. ->willReturn($httpRequestResult);
  100. // only add background job on first try
  101. if ($try === 0 && $expected === false) {
  102. $this->jobList->expects($this->once())->method('add')
  103. ->with(
  104. 'OCA\FederatedFileSharing\BackgroundJob\RetryJob',
  105. [
  106. 'remote' => $remote,
  107. 'remoteId' => $id,
  108. 'action' => 'unshare',
  109. 'data' => json_encode(['data1Key' => 'data1Value']),
  110. 'token' => $token,
  111. 'try' => $try,
  112. 'lastRun' => $timestamp
  113. ]
  114. );
  115. } else {
  116. $this->jobList->expects($this->never())->method('add');
  117. }
  118. $this->assertSame($expected,
  119. $instance->sendUpdateToRemote($remote, $id, $token, $action, ['data1Key' => 'data1Value'], $try)
  120. );
  121. }
  122. public function dataTestSendUpdateToRemote() {
  123. return [
  124. // test if background job is added correctly
  125. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  126. [1, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  127. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  128. [1, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  129. // test all combinations of 'statuscode' and 'success'
  130. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  131. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], true],
  132. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  133. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  134. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], false],
  135. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  136. ];
  137. }
  138. }