NotificationsTest.php 5.2 KB

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