LookupPluginTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Collaboration\Collaborators;
  24. use OC\Collaboration\Collaborators\LookupPlugin;
  25. use OCP\Collaboration\Collaborators\ISearchResult;
  26. use OCP\Collaboration\Collaborators\SearchResultType;
  27. use OCP\Http\Client\IClient;
  28. use OCP\Http\Client\IClientService;
  29. use OCP\Http\Client\IResponse;
  30. use OCP\IConfig;
  31. use OCP\Share;
  32. use Test\TestCase;
  33. class LookupPluginTest extends TestCase {
  34. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $config;
  36. /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $clientService;
  38. /** @var LookupPlugin */
  39. protected $plugin;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->config = $this->createMock(IConfig::class);
  43. $this->clientService = $this->createMock(IClientService::class);
  44. $this->plugin = new LookupPlugin($this->config, $this->clientService);
  45. }
  46. /**
  47. * @dataProvider searchDataProvider
  48. * @param array $searchParams
  49. */
  50. public function testSearch(array $searchParams) {
  51. $type = new SearchResultType('lookup');
  52. /** @var ISearchResult|\PHPUnit_Framework_MockObject_MockObject $searchResult */
  53. $searchResult = $this->createMock(ISearchResult::class);
  54. $searchResult->expects($this->once())
  55. ->method('addResultSet')
  56. ->with($type, $searchParams['expectedResult'], []);
  57. $this->config->expects($this->once())
  58. ->method('getAppValue')
  59. ->with('files_sharing', 'lookupServerEnabled', 'no')
  60. ->willReturn('yes');
  61. $this->config->expects($this->once())
  62. ->method('getSystemValue')
  63. ->with('lookup_server', 'https://lookup.nextcloud.com')
  64. ->willReturn($searchParams['server']);
  65. $response = $this->createMock(IResponse::class);
  66. $response->expects($this->once())
  67. ->method('getBody')
  68. ->willReturn(json_encode($searchParams['resultBody']));
  69. $client = $this->createMock(IClient::class);
  70. $client->expects($this->once())
  71. ->method('get')
  72. ->willReturnCallback(function($url) use ($searchParams, $response) {
  73. $this->assertSame(strpos($url, $searchParams['server'] . '/users?search='), 0);
  74. $this->assertNotFalse(strpos($url, urlencode($searchParams['search'])));
  75. return $response;
  76. });
  77. $this->clientService->expects($this->once())
  78. ->method('newClient')
  79. ->willReturn($client);
  80. $moreResults = $this->plugin->search(
  81. $searchParams['search'],
  82. $searchParams['limit'],
  83. $searchParams['offset'],
  84. $searchResult
  85. );
  86. $this->assertFalse($moreResults);
  87. }
  88. public function testSearchLookupServerDisabled() {
  89. $this->config->expects($this->once())
  90. ->method('getAppValue')
  91. ->with('files_sharing', 'lookupServerEnabled', 'no')
  92. ->willReturn('no');
  93. /** @var ISearchResult|\PHPUnit_Framework_MockObject_MockObject $searchResult */
  94. $searchResult = $this->createMock(ISearchResult::class);
  95. $searchResult->expects($this->never())
  96. ->method('addResultSet');
  97. $searchResult->expects($this->never())
  98. ->method('markExactIdMatch');
  99. $this->assertFalse($this->plugin->search('irr', 10, 0, $searchResult));
  100. }
  101. public function searchDataProvider() {
  102. $fedIDs = [
  103. 'foo@enceladus.moon',
  104. 'foobar@enceladus.moon',
  105. 'foongus@enceladus.moon',
  106. ];
  107. return [
  108. // #0, standard search with results
  109. [[
  110. 'search' => 'foo',
  111. 'limit' => 10,
  112. 'offset' => 0,
  113. 'server' => 'https://lookup.example.io',
  114. 'resultBody' => [
  115. [ 'federationId' => $fedIDs[0] ],
  116. [ 'federationId' => $fedIDs[1] ],
  117. [ 'federationId' => $fedIDs[2] ],
  118. ],
  119. 'expectedResult' => [
  120. [
  121. 'label' => $fedIDs[0],
  122. 'value' => [
  123. 'shareType' => Share::SHARE_TYPE_REMOTE,
  124. 'shareWith' => $fedIDs[0]
  125. ],
  126. 'extra' => ['federationId' => $fedIDs[0]],
  127. ],
  128. [
  129. 'label' => $fedIDs[1],
  130. 'value' => [
  131. 'shareType' => Share::SHARE_TYPE_REMOTE,
  132. 'shareWith' => $fedIDs[1]
  133. ],
  134. 'extra' => ['federationId' => $fedIDs[1]],
  135. ],
  136. [
  137. 'label' => $fedIDs[2],
  138. 'value' => [
  139. 'shareType' => Share::SHARE_TYPE_REMOTE,
  140. 'shareWith' => $fedIDs[2]
  141. ],
  142. 'extra' => ['federationId' => $fedIDs[2]],
  143. ],
  144. ]
  145. ]],
  146. // #1, search without results
  147. [[
  148. 'search' => 'foo',
  149. 'limit' => 10,
  150. 'offset' => 0,
  151. 'server' => 'https://lookup.example.io',
  152. 'resultBody' => [],
  153. 'expectedResult' => [],
  154. ]],
  155. ];
  156. }
  157. }