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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
<?php
/**
* @author Lukas Reschke
* @copyright 2014 Lukas Reschke lukas@owncloud.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Tests\Settings\Controller;
use OC\Group\MetaData;
use OC\Settings\Controller\GroupsController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;
/**
* @package Tests\Settings\Controller
*/
class GroupsControllerTest extends \Test\TestCase {
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
private $groupManager;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
private $userSession;
/** @var GroupsController */
private $groupsController;
protected function setUp() {
parent::setUp();
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$l = $this->createMock(IL10N::class);
$l->method('t')
->will($this->returnCallback(function($text, $parameters = []) {
return vsprintf($text, $parameters);
}));
$this->groupsController = new GroupsController(
'settings',
$this->createMock(IRequest::class),
$this->groupManager,
$this->userSession,
true,
$l
);
}
/**
* TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked
* to test for subadmins. Thus the test always assumes you have admin permissions...
*/
public function testIndexSortByName() {
$firstGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$firstGroup
->method('getGID')
->will($this->returnValue('firstGroup'));
$firstGroup
->method('count')
->will($this->returnValue(12));
$secondGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$secondGroup
->method('getGID')
->will($this->returnValue('secondGroup'));
$secondGroup
->method('count')
->will($this->returnValue(25));
$thirdGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$thirdGroup
->method('getGID')
->will($this->returnValue('thirdGroup'));
$thirdGroup
->method('count')
->will($this->returnValue(14));
$fourthGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$fourthGroup
->method('getGID')
->will($this->returnValue('admin'));
$fourthGroup
->method('count')
->will($this->returnValue(18));
/** @var \OC\Group\Group[] $groups */
$groups = array();
$groups[] = $firstGroup;
$groups[] = $secondGroup;
$groups[] = $thirdGroup;
$groups[] = $fourthGroup;
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$this->userSession
->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('MyAdminUser'));
$this->groupManager->method('search')
->will($this->returnValue($groups));
$expectedResponse = new DataResponse(
array(
'data' => array(
'adminGroups' => array(
0 => array(
'id' => 'admin',
'name' => 'admin',
'usercount' => 0,//User count disabled 18,
)
),
'groups' =>
array(
0 => array(
'id' => 'firstGroup',
'name' => 'firstGroup',
'usercount' => 0,//User count disabled 12,
),
1 => array(
'id' => 'secondGroup',
'name' => 'secondGroup',
'usercount' => 0,//User count disabled 25,
),
2 => array(
'id' => 'thirdGroup',
'name' => 'thirdGroup',
'usercount' => 0,//User count disabled 14,
),
)
)
)
);
$response = $this->groupsController->index('', false, MetaData::SORT_GROUPNAME);
$this->assertEquals($expectedResponse, $response);
}
/**
* TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked
* to test for subadmins. Thus the test always assumes you have admin permissions...
*/
public function testIndexSortbyCount() {
$firstGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$firstGroup
->method('getGID')
->will($this->returnValue('firstGroup'));
$firstGroup
->method('count')
->will($this->returnValue(12));
$secondGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$secondGroup
->method('getGID')
->will($this->returnValue('secondGroup'));
$secondGroup
->method('count')
->will($this->returnValue(25));
$thirdGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$thirdGroup
->method('getGID')
->will($this->returnValue('thirdGroup'));
$thirdGroup
->method('count')
->will($this->returnValue(14));
$fourthGroup = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$fourthGroup
->method('getGID')
->will($this->returnValue('admin'));
$fourthGroup
->method('count')
->will($this->returnValue(18));
/** @var \OC\Group\Group[] $groups */
$groups = array();
$groups[] = $firstGroup;
$groups[] = $secondGroup;
$groups[] = $thirdGroup;
$groups[] = $fourthGroup;
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$this->userSession
->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
$user
->expects($this->once())
->method('getUID')
->will($this->returnValue('MyAdminUser'));
$this->groupManager
->method('search')
->will($this->returnValue($groups));
$expectedResponse = new DataResponse(
array(
'data' => array(
'adminGroups' => array(
0 => array(
'id' => 'admin',
'name' => 'admin',
'usercount' => 18,
)
),
'groups' =>
array(
0 => array(
'id' => 'secondGroup',
'name' => 'secondGroup',
'usercount' => 25,
),
1 => array(
'id' => 'thirdGroup',
'name' => 'thirdGroup',
'usercount' => 14,
),
2 => array(
'id' => 'firstGroup',
'name' => 'firstGroup',
'usercount' => 12,
),
)
)
)
);
$response = $this->groupsController->index();
$this->assertEquals($expectedResponse, $response);
}
public function testCreateWithExistingGroup() {
$this->groupManager
->expects($this->once())
->method('groupExists')
->with('ExistingGroup')
->will($this->returnValue(true));
$expectedResponse = new DataResponse(
array(
'message' => 'Group already exists.'
),
Http::STATUS_CONFLICT
);
$response = $this->groupsController->create('ExistingGroup');
$this->assertEquals($expectedResponse, $response);
}
public function testCreateSuccessful() {
$this->groupManager
->expects($this->once())
->method('groupExists')
->with('NewGroup')
->will($this->returnValue(false));
$this->groupManager
->expects($this->once())
->method('createGroup')
->with('NewGroup')
->will($this->returnValue(true));
$expectedResponse = new DataResponse(
array(
'groupname' => 'NewGroup'
),
Http::STATUS_CREATED
);
$response = $this->groupsController->create('NewGroup');
$this->assertEquals($expectedResponse, $response);
}
public function testCreateUnsuccessful() {
$this->groupManager
->expects($this->once())
->method('groupExists')
->with('NewGroup')
->will($this->returnValue(false));
$this->groupManager
->expects($this->once())
->method('createGroup')
->with('NewGroup')
->will($this->returnValue(false));
$expectedResponse = new DataResponse(
array(
'status' => 'error',
'data' => array('message' => 'Unable to add group.')
),
Http::STATUS_FORBIDDEN
);
$response = $this->groupsController->create('NewGroup');
$this->assertEquals($expectedResponse, $response);
}
public function testDestroySuccessful() {
$group = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock();
$this->groupManager
->expects($this->once())
->method('get')
->with('ExistingGroup')
->will($this->returnValue($group));
$group
->expects($this->once())
->method('delete')
->will($this->returnValue(true));
$expectedResponse = new DataResponse(
array(
'status' => 'success',
'data' => array('groupname' => 'ExistingGroup')
),
Http::STATUS_NO_CONTENT
);
$response = $this->groupsController->destroy('ExistingGroup');
$this->assertEquals($expectedResponse, $response);
}
public function testDestroyUnsuccessful() {
$this->groupManager
->expects($this->once())
->method('get')
->with('ExistingGroup')
->will($this->returnValue(null));
$expectedResponse = new DataResponse(
array(
'status' => 'error',
'data' => array('message' => 'Unable to delete group.')
),
Http::STATUS_FORBIDDEN
);
$response = $this->groupsController->destroy('ExistingGroup');
$this->assertEquals($expectedResponse, $response);
}
}
|