|
| 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 | + |
| 24 | +namespace Test\Collaboration\Collaborators; |
| 25 | + |
| 26 | + |
| 27 | +use OC\Collaboration\Collaborators\LookupPlugin; |
| 28 | +use OCP\Collaboration\Collaborators\ISearchResult; |
| 29 | +use OCP\Collaboration\Collaborators\SearchResultType; |
| 30 | +use OCP\Http\Client\IClient; |
| 31 | +use OCP\Http\Client\IClientService; |
| 32 | +use OCP\Http\Client\IResponse; |
| 33 | +use OCP\IConfig; |
| 34 | +use OCP\Share; |
| 35 | +use Test\TestCase; |
| 36 | + |
| 37 | +class LookupPluginTest extends TestCase { |
| 38 | + |
| 39 | + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ |
| 40 | + protected $config; |
| 41 | + /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */ |
| 42 | + protected $clientService; |
| 43 | + /** @var LookupPlugin */ |
| 44 | + protected $plugin; |
| 45 | + |
| 46 | + public function setUp() { |
| 47 | + parent::setUp(); |
| 48 | + |
| 49 | + $this->config = $this->createMock(IConfig::class); |
| 50 | + $this->clientService = $this->createMock(IClientService::class); |
| 51 | + |
| 52 | + $this->plugin = new LookupPlugin($this->config, $this->clientService); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @dataProvider searchDataProvider |
| 57 | + * @param array $searchParams |
| 58 | + */ |
| 59 | + public function testSearch(array $searchParams) { |
| 60 | + $type = new SearchResultType('lookup'); |
| 61 | + |
| 62 | + /** @var ISearchResult|\PHPUnit_Framework_MockObject_MockObject $searchResult */ |
| 63 | + $searchResult = $this->createMock(ISearchResult::class); |
| 64 | + $searchResult->expects($this->once()) |
| 65 | + ->method('addResultSet') |
| 66 | + ->with($type, $searchParams['expectedResult'], []); |
| 67 | + |
| 68 | + $this->config->expects($this->once()) |
| 69 | + ->method('getAppValue') |
| 70 | + ->with('files_sharing', 'lookupServerEnabled', 'no') |
| 71 | + ->willReturn('yes'); |
| 72 | + $this->config->expects($this->once()) |
| 73 | + ->method('getSystemValue') |
| 74 | + ->with('lookup_server', 'https://lookup.nextcloud.com') |
| 75 | + ->willReturn($searchParams['server']); |
| 76 | + |
| 77 | + $response = $this->createMock(IResponse::class); |
| 78 | + $response->expects($this->once()) |
| 79 | + ->method('getBody') |
| 80 | + ->willReturn(json_encode($searchParams['resultBody'])); |
| 81 | + |
| 82 | + $client = $this->createMock(IClient::class); |
| 83 | + $client->expects($this->once()) |
| 84 | + ->method('get') |
| 85 | + ->willReturnCallback(function($url) use ($searchParams, $response) { |
| 86 | + $this->assertSame(strpos($url, $searchParams['server'] . '/users?search='), 0); |
| 87 | + $this->assertNotFalse(strpos($url, urlencode($searchParams['search']))); |
| 88 | + return $response; |
| 89 | + }); |
| 90 | + |
| 91 | + $this->clientService->expects($this->once()) |
| 92 | + ->method('newClient') |
| 93 | + ->willReturn($client); |
| 94 | + |
| 95 | + $moreResults = $this->plugin->search( |
| 96 | + $searchParams['search'], |
| 97 | + $searchParams['limit'], |
| 98 | + $searchParams['offset'], |
| 99 | + $searchResult |
| 100 | + ); |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + $this->assertFalse($moreResults); |
| 105 | + } |
| 106 | + |
| 107 | + public function testSearchLookupServerDisabled() { |
| 108 | + $this->config->expects($this->once()) |
| 109 | + ->method('getAppValue') |
| 110 | + ->with('files_sharing', 'lookupServerEnabled', 'no') |
| 111 | + ->willReturn('no'); |
| 112 | + |
| 113 | + /** @var ISearchResult|\PHPUnit_Framework_MockObject_MockObject $searchResult */ |
| 114 | + $searchResult = $this->createMock(ISearchResult::class); |
| 115 | + $searchResult->expects($this->never()) |
| 116 | + ->method('addResultSet'); |
| 117 | + $searchResult->expects($this->never()) |
| 118 | + ->method('markExactIdMatch'); |
| 119 | + |
| 120 | + $this->assertFalse($this->plugin->search('irr', 10, 0, $searchResult)); |
| 121 | + } |
| 122 | + |
| 123 | + public function searchDataProvider() { |
| 124 | + $fedIDs = [ |
| 125 | + 'foo@enceladus.moon', |
| 126 | + 'foobar@enceladus.moon', |
| 127 | + 'foongus@enceladus.moon', |
| 128 | + ]; |
| 129 | + |
| 130 | + return [ |
| 131 | + // #0, standard search with results |
| 132 | + [[ |
| 133 | + 'search' => 'foo', |
| 134 | + 'limit' => 10, |
| 135 | + 'offset' => 0, |
| 136 | + 'server' => 'https://lookup.example.io', |
| 137 | + 'resultBody' => [ |
| 138 | + [ 'federationId' => $fedIDs[0] ], |
| 139 | + [ 'federationId' => $fedIDs[1] ], |
| 140 | + [ 'federationId' => $fedIDs[2] ], |
| 141 | + ], |
| 142 | + 'expectedResult' => [ |
| 143 | + [ |
| 144 | + 'label' => $fedIDs[0], |
| 145 | + 'value' => [ |
| 146 | + 'shareType' => Share::SHARE_TYPE_REMOTE, |
| 147 | + 'shareWith' => $fedIDs[0] |
| 148 | + ], |
| 149 | + 'extra' => ['federationId' => $fedIDs[0]], |
| 150 | + ], |
| 151 | + [ |
| 152 | + 'label' => $fedIDs[1], |
| 153 | + 'value' => [ |
| 154 | + 'shareType' => Share::SHARE_TYPE_REMOTE, |
| 155 | + 'shareWith' => $fedIDs[1] |
| 156 | + ], |
| 157 | + 'extra' => ['federationId' => $fedIDs[1]], |
| 158 | + ], |
| 159 | + [ |
| 160 | + 'label' => $fedIDs[2], |
| 161 | + 'value' => [ |
| 162 | + 'shareType' => Share::SHARE_TYPE_REMOTE, |
| 163 | + 'shareWith' => $fedIDs[2] |
| 164 | + ], |
| 165 | + 'extra' => ['federationId' => $fedIDs[2]], |
| 166 | + ], |
| 167 | + ] |
| 168 | + ]], |
| 169 | + // #1, search without results |
| 170 | + [[ |
| 171 | + 'search' => 'foo', |
| 172 | + 'limit' => 10, |
| 173 | + 'offset' => 0, |
| 174 | + 'server' => 'https://lookup.example.io', |
| 175 | + 'resultBody' => [], |
| 176 | + 'expectedResult' => [], |
| 177 | + ]], |
| 178 | + ]; |
| 179 | + } |
| 180 | +} |
0 commit comments