NotificationsTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\FederatedFileSharing\Tests;
  25. use OCA\FederatedFileSharing\AddressHandler;
  26. use OCA\FederatedFileSharing\Notifications;
  27. use OCP\BackgroundJob\IJobList;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\Federation\ICloudFederationFactory;
  30. use OCP\Federation\ICloudFederationProviderManager;
  31. use OCP\Http\Client\IClientService;
  32. use OCP\OCS\IDiscoveryService;
  33. class NotificationsTest extends \Test\TestCase {
  34. /** @var AddressHandler | \PHPUnit_Framework_MockObject_MockObject */
  35. private $addressHandler;
  36. /** @var IClientService | \PHPUnit_Framework_MockObject_MockObject*/
  37. private $httpClientService;
  38. /** @var IDiscoveryService | \PHPUnit_Framework_MockObject_MockObject */
  39. private $discoveryService;
  40. /** @var IJobList | \PHPUnit_Framework_MockObject_MockObject */
  41. private $jobList;
  42. /** @var ICloudFederationProviderManager|\PHPUnit_Framework_MockObject_MockObject */
  43. private $cloudFederationProviderManager;
  44. /** @var ICloudFederationFactory|\PHPUnit_Framework_MockObject_MockObject */
  45. private $cloudFederationFactory;
  46. /** @var IEventDispatcher|\PHPUnit_Framework_MockObject_MockObject */
  47. private $eventDispatcher;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->jobList = $this->getMockBuilder('OCP\BackgroundJob\IJobList')->getMock();
  51. $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  52. $this->httpClientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->getMock();
  53. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  54. ->disableOriginalConstructor()->getMock();
  55. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  56. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  57. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  58. }
  59. /**
  60. * get instance of Notifications class
  61. *
  62. * @param array $mockedMethods methods which should be mocked
  63. * @return Notifications | \PHPUnit_Framework_MockObject_MockObject
  64. */
  65. private function getInstance(array $mockedMethods = []) {
  66. if (empty($mockedMethods)) {
  67. $instance = new Notifications(
  68. $this->addressHandler,
  69. $this->httpClientService,
  70. $this->discoveryService,
  71. $this->jobList,
  72. $this->cloudFederationProviderManager,
  73. $this->cloudFederationFactory,
  74. $this->eventDispatcher
  75. );
  76. } else {
  77. $instance = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
  78. ->setConstructorArgs(
  79. [
  80. $this->addressHandler,
  81. $this->httpClientService,
  82. $this->discoveryService,
  83. $this->jobList,
  84. $this->cloudFederationProviderManager,
  85. $this->cloudFederationFactory,
  86. $this->eventDispatcher
  87. ]
  88. )->setMethods($mockedMethods)->getMock();
  89. }
  90. return $instance;
  91. }
  92. /**
  93. * @dataProvider dataTestSendUpdateToRemote
  94. *
  95. * @param int $try
  96. * @param array $httpRequestResult
  97. * @param bool $expected
  98. */
  99. public function testSendUpdateToRemote($try, $httpRequestResult, $expected) {
  100. $remote = 'http://remote';
  101. $id = 42;
  102. $timestamp = 63576;
  103. $token = 'token';
  104. $action = 'unshare';
  105. $instance = $this->getInstance(['tryHttpPostToShareEndpoint', 'getTimestamp']);
  106. $instance->expects($this->any())->method('getTimestamp')->willReturn($timestamp);
  107. $instance->expects($this->once())->method('tryHttpPostToShareEndpoint')
  108. ->with($remote, '/'.$id.'/unshare', ['token' => $token, 'data1Key' => 'data1Value', 'remoteId' => $id], $action)
  109. ->willReturn($httpRequestResult);
  110. // only add background job on first try
  111. if ($try === 0 && $expected === false) {
  112. $this->jobList->expects($this->once())->method('add')
  113. ->with(
  114. 'OCA\FederatedFileSharing\BackgroundJob\RetryJob',
  115. [
  116. 'remote' => $remote,
  117. 'remoteId' => $id,
  118. 'action' => 'unshare',
  119. 'data' => json_encode(['data1Key' => 'data1Value']),
  120. 'token' => $token,
  121. 'try' => $try,
  122. 'lastRun' => $timestamp
  123. ]
  124. );
  125. } else {
  126. $this->jobList->expects($this->never())->method('add');
  127. }
  128. $this->assertSame($expected,
  129. $instance->sendUpdateToRemote($remote, $id, $token, $action, ['data1Key' => 'data1Value'], $try)
  130. );
  131. }
  132. public function dataTestSendUpdateToRemote() {
  133. return [
  134. // test if background job is added correctly
  135. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  136. [1, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  137. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  138. [1, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  139. // test all combinations of 'statuscode' and 'success'
  140. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  141. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], true],
  142. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  143. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  144. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], false],
  145. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  146. ];
  147. }
  148. }