NotificationsTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\Federation\ICloudFederationFactory;
  29. use OCP\Federation\ICloudFederationProviderManager;
  30. use OCP\Http\Client\IClientService;
  31. use OCP\OCS\IDiscoveryService;
  32. class NotificationsTest extends \Test\TestCase {
  33. /** @var AddressHandler | \PHPUnit_Framework_MockObject_MockObject */
  34. private $addressHandler;
  35. /** @var IClientService | \PHPUnit_Framework_MockObject_MockObject*/
  36. private $httpClientService;
  37. /** @var IDiscoveryService | \PHPUnit_Framework_MockObject_MockObject */
  38. private $discoveryService;
  39. /** @var IJobList | \PHPUnit_Framework_MockObject_MockObject */
  40. private $jobList;
  41. /** @var ICloudFederationProviderManager|\PHPUnit_Framework_MockObject_MockObject */
  42. private $cloudFederationProviderManager;
  43. /** @var ICloudFederationFactory|\PHPUnit_Framework_MockObject_MockObject */
  44. private $cloudFederationFactory;
  45. public function setUp() {
  46. parent::setUp();
  47. $this->jobList = $this->getMockBuilder('OCP\BackgroundJob\IJobList')->getMock();
  48. $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  49. $this->httpClientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->getMock();
  50. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  51. ->disableOriginalConstructor()->getMock();
  52. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  53. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  54. }
  55. /**
  56. * get instance of Notifications class
  57. *
  58. * @param array $mockedMethods methods which should be mocked
  59. * @return Notifications | \PHPUnit_Framework_MockObject_MockObject
  60. */
  61. private function getInstance(array $mockedMethods = []) {
  62. if (empty($mockedMethods)) {
  63. $instance = new Notifications(
  64. $this->addressHandler,
  65. $this->httpClientService,
  66. $this->discoveryService,
  67. $this->jobList,
  68. $this->cloudFederationProviderManager,
  69. $this->cloudFederationFactory
  70. );
  71. } else {
  72. $instance = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
  73. ->setConstructorArgs(
  74. [
  75. $this->addressHandler,
  76. $this->httpClientService,
  77. $this->discoveryService,
  78. $this->jobList,
  79. $this->cloudFederationProviderManager,
  80. $this->cloudFederationFactory
  81. ]
  82. )->setMethods($mockedMethods)->getMock();
  83. }
  84. return $instance;
  85. }
  86. /**
  87. * @dataProvider dataTestSendUpdateToRemote
  88. *
  89. * @param int $try
  90. * @param array $httpRequestResult
  91. * @param bool $expected
  92. */
  93. public function testSendUpdateToRemote($try, $httpRequestResult, $expected) {
  94. $remote = 'http://remote';
  95. $id = 42;
  96. $timestamp = 63576;
  97. $token = 'token';
  98. $action = 'unshare';
  99. $instance = $this->getInstance(['tryHttpPostToShareEndpoint', 'getTimestamp']);
  100. $instance->expects($this->any())->method('getTimestamp')->willReturn($timestamp);
  101. $instance->expects($this->once())->method('tryHttpPostToShareEndpoint')
  102. ->with($remote, '/'.$id.'/unshare', ['token' => $token, 'data1Key' => 'data1Value', 'remoteId' => $id], $action)
  103. ->willReturn($httpRequestResult);
  104. // only add background job on first try
  105. if ($try === 0 && $expected === false) {
  106. $this->jobList->expects($this->once())->method('add')
  107. ->with(
  108. 'OCA\FederatedFileSharing\BackgroundJob\RetryJob',
  109. [
  110. 'remote' => $remote,
  111. 'remoteId' => $id,
  112. 'action' => 'unshare',
  113. 'data' => json_encode(['data1Key' => 'data1Value']),
  114. 'token' => $token,
  115. 'try' => $try,
  116. 'lastRun' => $timestamp
  117. ]
  118. );
  119. } else {
  120. $this->jobList->expects($this->never())->method('add');
  121. }
  122. $this->assertSame($expected,
  123. $instance->sendUpdateToRemote($remote, $id, $token, $action, ['data1Key' => 'data1Value'], $try)
  124. );
  125. }
  126. public function dataTestSendUpdateToRemote() {
  127. return [
  128. // test if background job is added correctly
  129. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  130. [1, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  131. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  132. [1, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  133. // test all combinations of 'statuscode' and 'success'
  134. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  135. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], true],
  136. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  137. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  138. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], false],
  139. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  140. ];
  141. }
  142. }