1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
<?php
declare(strict_types=1);
/**
*
* @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Tests\php\Collaboration\Collaborators;
use OCA\Talk\Collaboration\Collaborators\RoomPlugin;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IShare;
class RoomPluginTest extends \Test\TestCase {
/** @var Manager */
protected $manager;
/** @var IUserSession */
protected $userSession;
/** @var IUser */
protected $user;
/** @var ISearchResult */
protected $searchResult;
/** @var RoomPlugin */
protected $plugin;
public function setUp(): void {
parent::setUp();
$this->manager = $this->createMock(Manager::class);
$this->user = $this->createMock(IUser::class);
$this->user->expects($this->any())
->method('getUID')
->willReturn('user0');
$this->userSession = $this->createMock(IUserSession::class);
$this->userSession->expects($this->any())
->method('getUser')
->willReturn($this->user);
$this->searchResult = $this->createMock(ISearchResult::class);
$this->plugin = new RoomPlugin($this->manager, $this->userSession);
}
private function newRoom(int $type, string $token, string $name): Room {
$room = $this->createMock(Room::class);
$room->expects($this->any())
->method('getType')
->willReturn($type);
$room->expects($this->any())
->method('getToken')
->willReturn($token);
$room->expects($this->any())
->method('getDisplayName')
->willReturn($name);
return $room;
}
private function newResult(string $label, string $shareWith): array {
return [
'label' => $label,
'value' => [
'shareType' => IShare::TYPE_ROOM,
'shareWith' => $shareWith
]
];
}
public function searchProvider(): array {
return [
// Empty search term with no rooms
['', 2, 0, [], [], [], false],
// Empty search term with rooms
['', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Room name')
], [], [], false],
// Search term with no matches
['Unmatched search term', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Unmatched name')
], [], [], false],
// Search term with single wide match
['room', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken2', 'Unmatched name')
], [], [
$this->newResult('Room name', 'roomToken')
], false],
// Search term with single exact match
['room name', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Unmatched name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken2', 'Room name')
], [
$this->newResult('Room name', 'roomToken2')
], [], false],
// Search term with single exact match and single wide match
['room name', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Room name that also matches'),
$this->newRoom(Room::GROUP_CALL, 'roomToken2', 'Room name')
], [
$this->newResult('Room name', 'roomToken2')
], [
$this->newResult('Room name that also matches', 'roomToken')
], false],
// Search term matching one-to-one rooms (not possible in practice
// as one-to-one rooms do not have a name, but it would be if they
// had, so it is included here for completeness).
['room name', 2, 0, [
$this->newRoom(Room::ONE_TO_ONE_CALL, 'roomToken', 'Room name that also matches'),
$this->newRoom(Room::ONE_TO_ONE_CALL, 'roomToken2', 'Room name')
], [
$this->newResult('Room name', 'roomToken2')
], [
$this->newResult('Room name that also matches', 'roomToken')
], false],
// Search term matching public rooms
['room name', 2, 0, [
$this->newRoom(Room::PUBLIC_CALL, 'roomToken', 'Room name that also matches'),
$this->newRoom(Room::PUBLIC_CALL, 'roomToken2', 'Room name')
], [
$this->newResult('Room name', 'roomToken2')
], [
$this->newResult('Room name that also matches', 'roomToken')
], false],
// Search term with several wide matches
['room', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken2', 'Another room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken3', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken4', 'Another room name')
], [], [
$this->newResult('Room name', 'roomToken'),
$this->newResult('Another room name', 'roomToken2'),
$this->newResult('Room name', 'roomToken3'),
$this->newResult('Another room name', 'roomToken4'),
], false],
// Search term with several exact matches
['room name', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken2', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken3', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken4', 'Room name')
], [
$this->newResult('Room name', 'roomToken'),
$this->newResult('Room name', 'roomToken2'),
$this->newResult('Room name', 'roomToken3'),
$this->newResult('Room name', 'roomToken4')
], [], false],
// Search term with several matches
['room name', 2, 0, [
$this->newRoom(Room::GROUP_CALL, 'roomToken', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken2', 'Unmatched name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken3', 'Another room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken4', 'Room name'),
$this->newRoom(Room::ONE_TO_ONE_CALL, 'roomToken5', 'Room name'),
$this->newRoom(Room::PUBLIC_CALL, 'roomToken6', 'Room name'),
$this->newRoom(Room::GROUP_CALL, 'roomToken7', 'Another unmatched name'),
$this->newRoom(Room::ONE_TO_ONE_CALL, 'roomToken8', 'Another unmatched name'),
$this->newRoom(Room::PUBLIC_CALL, 'roomToken9', 'Another unmatched name'),
$this->newRoom(Room::ONE_TO_ONE_CALL, 'roomToken10', 'Another room name'),
$this->newRoom(Room::PUBLIC_CALL, 'roomToken11', 'Another room name')
], [
$this->newResult('Room name', 'roomToken'),
$this->newResult('Room name', 'roomToken4'),
$this->newResult('Room name', 'roomToken5'),
$this->newResult('Room name', 'roomToken6')
], [
$this->newResult('Another room name', 'roomToken3'),
$this->newResult('Another room name', 'roomToken10'),
$this->newResult('Another room name', 'roomToken11')
], false],
];
}
/**
* @dataProvider searchProvider
*
* @param string $searchTerm
* @param int $limit
* @param int $offset
* @param array $roomsForParticipant
* @param array $expectedMatchesExact
* @param array $expectedMatches
* @param bool $expectedHasMoreResults
*/
public function testSearch(
string $searchTerm,
int $limit,
int $offset,
array $roomsForParticipant,
array $expectedMatchesExact,
array $expectedMatches,
bool $expectedHasMoreResults
) {
$this->manager->expects($this->any())
->method('getRoomsForParticipant')
->with('user0')
->willReturn($roomsForParticipant);
$this->searchResult->expects($this->any())
->method('addResultSet')
->with(
$this->callback(
function(SearchResultType $searchResultType) {
return $searchResultType->getLabel() === 'rooms';
}
),
$expectedMatches,
$expectedMatchesExact
);
$hasMoreResults = $this->plugin->search($searchTerm, $limit, $offset, $this->searchResult);
$this->assertSame($expectedHasMoreResults, $hasMoreResults);
}
}
|