Completed
Push — stable8.2 ( b4bbd4...3350d6 )
by
unknown
59:02
created

Test_User_User::testUpdateQuotaNoneConfigured()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1
Metric Value
dl 0
loc 34
ccs 23
cts 23
cp 1
rs 8.8571
cc 1
eloc 24
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2015, ownCloud, Inc.
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\user_ldap\tests;
25
26
use OCA\user_ldap\lib\user\User;
27
28
class Test_User_User extends \Test\TestCase {
29
30 26
	private function getTestInstances() {
31 26
		$access  = $this->getMock('\OCA\user_ldap\lib\user\IUserTools');
32 26
		$config  = $this->getMock('\OCP\IConfig');
33 26
		$filesys = $this->getMock('\OCA\user_ldap\lib\FilesystemHelper');
34 26
		$log     = $this->getMock('\OCA\user_ldap\lib\LogWrapper');
35 26
		$avaMgr  = $this->getMock('\OCP\IAvatarManager');
36 26
		$image   = $this->getMock('\OCP\Image');
37 26
		$dbc     = $this->getMock('\OCP\IDBConnection');
38
39 26
		return array($access, $config, $filesys, $image, $log, $avaMgr, $dbc);
40
	}
41
42 20
	private function getAdvancedMocks($cfMock, $fsMock, $logMock, $avaMgr, $dbc) {
43 20
		static $conMethods;
44 20
		static $accMethods;
45 20
		static $umMethods;
46
47 20 View Code Duplication
		if(is_null($conMethods) || is_null($accMethods)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48 1
			$conMethods = get_class_methods('\OCA\user_ldap\lib\Connection');
49 1
			$accMethods = get_class_methods('\OCA\user_ldap\lib\Access');
50
			//getConnection shall not be replaced
51 1
			unset($accMethods[array_search('getConnection', $accMethods)]);
52 1
			$umMethods = get_class_methods('\OCA\user_ldap\lib\user\Manager');
53 1
		}
54 20
		$lw = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper');
55 20
		$im = $this->getMock('\OCP\Image');
56 20
		$um = $this->getMock('\OCA\user_ldap\lib\user\Manager',
57 20
			$umMethods, array($cfMock, $fsMock, $logMock, $avaMgr, $im, $dbc));
58 20
		$connector = $this->getMock('\OCA\user_ldap\lib\Connection',
59 20
			$conMethods, array($lw, null, null));
60 20
		$access = $this->getMock('\OCA\user_ldap\lib\Access',
61 20
			$accMethods, array($connector, $lw, $um));
62
63 20
		return array($access, $connector);
64
	}
65
66 1 View Code Duplication
	public function testGetDNandUsername() {
67
		list($access, $config, $filesys, $image, $log, $avaMgr) =
68 1
			$this->getTestInstances();
69
70 1
		$uid = 'alice';
71 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
72
73 1
		$user = new User(
74 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
75
76 1
		$this->assertSame($dn, $user->getDN());
77 1
		$this->assertSame($uid, $user->getUsername());
78 1
	}
79
80 1
	public function testUpdateEmailProvided() {
81
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
82 1
			$this->getTestInstances();
83
84
		list($access, $connection) =
85 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
86
87 1
		$connection->expects($this->once())
88 1
			->method('__get')
89 1
			->with($this->equalTo('ldapEmailAttribute'))
90 1
			->will($this->returnValue('email'));
91
92 1
		$access->expects($this->once())
93 1
			->method('readAttribute')
94 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
95 1
				$this->equalTo('email'))
96 1
			->will($this->returnValue(array('[email protected]')));
97
98 1
		$config->expects($this->once())
99 1
			->method('setUserValue')
100 1
			->with($this->equalTo('alice'), $this->equalTo('settings'),
101 1
				$this->equalTo('email'),
102 1
				$this->equalTo('[email protected]'))
103 1
			->will($this->returnValue(true));
104
105 1
		$uid = 'alice';
106 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
107
108 1
		$user = new User(
109 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
110
111 1
		$user->updateEmail();
112 1
	}
113
114 1
	public function testUpdateEmailNotProvided() {
115
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
116 1
			$this->getTestInstances();
117
118
		list($access, $connection) =
119 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
120
121 1
		$connection->expects($this->once())
122 1
			->method('__get')
123 1
			->with($this->equalTo('ldapEmailAttribute'))
124 1
			->will($this->returnValue('email'));
125
126 1
		$access->expects($this->once())
127 1
			->method('readAttribute')
128 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
129 1
				$this->equalTo('email'))
130 1
			->will($this->returnValue(false));
131
132 1
		$config->expects($this->never())
133 1
			->method('setUserValue');
134
135 1
		$uid = 'alice';
136 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
137
138 1
		$user = new User(
139 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
140
141 1
		$user->updateEmail();
142 1
	}
143
144 2
	public function testUpdateEmailNotConfigured() {
145
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
146 1
			$this->getTestInstances();
147
148 1
		list($access, $connection) =
149 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
150
151 1
		$connection->expects($this->once())
152 1
			->method('__get')
153 1
			->with($this->equalTo('ldapEmailAttribute'))
154 1
			->will($this->returnValue(''));
155
156 1
		$access->expects($this->never())
157 1
			->method('readAttribute');
158
159 1
		$config->expects($this->never())
160 1
			->method('setUserValue');
161
162 1
		$uid = 'alice';
163 2
		$dn  = 'uid=alice,dc=foo,dc=bar';
164
165 1
		$user = new User(
166 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
167
168 1
		$user->updateEmail();
169 1
	}
170
171 2 View Code Duplication
	public function testUpdateQuotaAllProvided() {
172
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
173 1
			$this->getTestInstances();
174
175
		list($access, $connection) =
176 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
177
178 1
		$connection->expects($this->at(0))
179 1
			->method('__get')
180 1
			->with($this->equalTo('ldapQuotaDefault'))
181 1
			->will($this->returnValue('23 GB'));
182
183 1
		$connection->expects($this->at(1))
184 1
			->method('__get')
185 1
			->with($this->equalTo('ldapQuotaAttribute'))
186 1
			->will($this->returnValue('myquota'));
187
188 1
		$connection->expects($this->exactly(2))
189 2
			->method('__get');
190
191 1
		$access->expects($this->once())
192 1
			->method('readAttribute')
193 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
194 1
				$this->equalTo('myquota'))
195 1
			->will($this->returnValue(array('42 GB')));
196
197 1
		$config->expects($this->once())
198 1
			->method('setUserValue')
199 1
			->with($this->equalTo('alice'),
200 1
				$this->equalTo('files'),
201 1
				$this->equalTo('quota'),
202 1
				$this->equalTo('42 GB'))
203 1
			->will($this->returnValue(true));
204
205 1
		$uid = 'alice';
206 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
207
208 1
		$user = new User(
209 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
210
211 1
		$user->updateQuota();
212 1
	}
213
214 5 View Code Duplication
	public function testUpdateQuotaDefaultProvided() {
215
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
216 1
			$this->getTestInstances();
217
218
		list($access, $connection) =
219 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
220
221 1
		$connection->expects($this->at(0))
222 1
			->method('__get')
223 1
			->with($this->equalTo('ldapQuotaDefault'))
224 1
			->will($this->returnValue('25 GB'));
225
226 1
		$connection->expects($this->at(1))
227 1
			->method('__get')
228 1
			->with($this->equalTo('ldapQuotaAttribute'))
229 1
			->will($this->returnValue('myquota'));
230
231 1
		$connection->expects($this->exactly(2))
232 1
			->method('__get');
233
234 1
		$access->expects($this->once())
235 1
			->method('readAttribute')
236 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
237 1
				$this->equalTo('myquota'))
238 1
			->will($this->returnValue(false));
239
240 1
		$config->expects($this->once())
241 1
			->method('setUserValue')
242 1
			->with($this->equalTo('alice'),
243 5
				$this->equalTo('files'),
244 4
				$this->equalTo('quota'),
245 1
				$this->equalTo('25 GB'))
246 3
			->will($this->returnValue(true));
247
248 1
		$uid = 'alice';
249 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
250
251 1
		$user = new User(
252 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
253
254 1
		$user->updateQuota();
255 1
	}
256
257 1 View Code Duplication
	public function testUpdateQuotaIndividualProvided() {
258
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
259 1
			$this->getTestInstances();
260
261
		list($access, $connection) =
262 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
263
264 1
		$connection->expects($this->at(0))
265 1
			->method('__get')
266 1
			->with($this->equalTo('ldapQuotaDefault'))
267 1
			->will($this->returnValue(''));
268
269 1
		$connection->expects($this->at(1))
270 1
			->method('__get')
271 1
			->with($this->equalTo('ldapQuotaAttribute'))
272 1
			->will($this->returnValue('myquota'));
273
274 1
		$connection->expects($this->exactly(2))
275 1
			->method('__get');
276
277 1
		$access->expects($this->once())
278 1
			->method('readAttribute')
279 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
280 1
				$this->equalTo('myquota'))
281 1
			->will($this->returnValue(array('27 GB')));
282
283 1
		$config->expects($this->once())
284 1
			->method('setUserValue')
285 1
			->with($this->equalTo('alice'),
286 1
				$this->equalTo('files'),
287 1
				$this->equalTo('quota'),
288 1
				$this->equalTo('27 GB'))
289 1
			->will($this->returnValue(true));
290
291 1
		$uid = 'alice';
292 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
293
294 1
		$user = new User(
295 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
296
297 1
		$user->updateQuota();
298 1
	}
299
300 1
	public function testUpdateQuotaNoneProvided() {
301
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
302 1
			$this->getTestInstances();
303
304
		list($access, $connection) =
305 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
306
307 1
		$connection->expects($this->at(0))
308 1
			->method('__get')
309 1
			->with($this->equalTo('ldapQuotaDefault'))
310 1
			->will($this->returnValue(''));
311
312 1
		$connection->expects($this->at(1))
313 1
			->method('__get')
314 1
			->with($this->equalTo('ldapQuotaAttribute'))
315 1
			->will($this->returnValue('myquota'));
316
317 1
		$connection->expects($this->exactly(2))
318 1
			->method('__get');
319
320 1
		$access->expects($this->once())
321 1
			->method('readAttribute')
322 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
323 1
				$this->equalTo('myquota'))
324 1
			->will($this->returnValue(false));
325
326 1
		$config->expects($this->never())
327 1
			->method('setUserValue');
328
329 1
		$uid = 'alice';
330 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
331
332 1
		$user = new User(
333 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
334
335 1
		$user->updateQuota();
336 1
	}
337
338 1
	public function testUpdateQuotaNoneConfigured() {
339
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
340 1
			$this->getTestInstances();
341
342
		list($access, $connection) =
343 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
344
345 1
		$connection->expects($this->at(0))
346 1
			->method('__get')
347 1
			->with($this->equalTo('ldapQuotaDefault'))
348 1
			->will($this->returnValue(''));
349
350 1
		$connection->expects($this->at(1))
351 1
			->method('__get')
352 1
			->with($this->equalTo('ldapQuotaAttribute'))
353 1
			->will($this->returnValue(''));
354
355 1
		$connection->expects($this->exactly(2))
356 1
			->method('__get');
357
358 1
		$access->expects($this->never())
359 1
			->method('readAttribute');
360
361 1
		$config->expects($this->never())
362 1
			->method('setUserValue');
363
364 1
		$uid = 'alice';
365 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
366
367 1
		$user = new User(
368 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
369
370 1
		$user->updateQuota();
371 1
	}
372
373 1
	public function testUpdateQuotaFromValue() {
374
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
375 1
			$this->getTestInstances();
376
377
		list($access, $connection) =
378 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
379
380 1
		$readQuota = '19 GB';
381
382 1
		$connection->expects($this->at(0))
383 1
			->method('__get')
384 1
			->with($this->equalTo('ldapQuotaDefault'))
385 1
			->will($this->returnValue(''));
386
387 1
		$connection->expects($this->once(1))
388 1
			->method('__get')
389 1
			->with($this->equalTo('ldapQuotaDefault'))
390 1
			->will($this->returnValue(null));
391
392 1
		$access->expects($this->never())
393 1
			->method('readAttribute');
394
395 1
		$config->expects($this->once())
396 1
			->method('setUserValue')
397 1
			->with($this->equalTo('alice'),
398 1
				$this->equalTo('files'),
399 1
				$this->equalTo('quota'),
400 1
				$this->equalTo($readQuota))
401 1
			->will($this->returnValue(true));
402
403 1
		$uid = 'alice';
404 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
405
406 1
		$user = new User(
407 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
408
409 1
		$user->updateQuota($readQuota);
410 1
	}
411
412
	//the testUpdateAvatar series also implicitely tests getAvatarImage
413 7
	public function testUpdateAvatarJpegPhotoProvided() {
414
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
415 1
			$this->getTestInstances();
416
417
		list($access, $connection) =
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
418 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
419
420 1
		$access->expects($this->once())
421 1
			->method('readAttribute')
422 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
423 1
				$this->equalTo('jpegPhoto'))
424 1
			->will($this->returnValue(array('this is a photo')));
425
426 1
		$image->expects($this->once())
427 1
			->method('valid')
428 1
			->will($this->returnValue(true));
429 1
		$image->expects($this->once())
430 1
			->method('width')
431 1
			->will($this->returnValue(128));
432 1
		$image->expects($this->once())
433 1
			->method('height')
434 1
			->will($this->returnValue(128));
435 1
		$image->expects($this->once())
436 7
			->method('centerCrop')
437 1
			->will($this->returnValue(true));
438
439 1
		$filesys->expects($this->once())
440 1
			->method('isLoaded')
441 6
			->will($this->returnValue(true));
442
443 1
		$avatar = $this->getMock('\OCP\IAvatar');
444 1
		$avatar->expects($this->once())
445 1
			->method('set')
446 1
			->with($this->isInstanceOf($image));
447
448 1
		$avaMgr->expects($this->once())
449 1
			->method('getAvatar')
450 1
			->with($this->equalTo('alice'))
451 1
			->will($this->returnValue($avatar));
452
453 1
		$uid = 'alice';
454 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
455
456 1
		$user = new User(
457 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
458
459 1
		$user->updateAvatar();
460 1
	}
461
462 1
	public function testUpdateAvatarThumbnailPhotoProvided() {
463
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
464 1
			$this->getTestInstances();
465
466
		list($access, $connection) =
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
467 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
468
469 1
		$access->expects($this->at(0))
470 1
			->method('readAttribute')
471 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
472 1
				$this->equalTo('jpegPhoto'))
473 1
			->will($this->returnValue(false));
474
475 1
		$access->expects($this->at(1))
476 1
			->method('readAttribute')
477 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
478 1
				$this->equalTo('thumbnailPhoto'))
479 1
			->will($this->returnValue(array('this is a photo')));
480
481 1
		$access->expects($this->exactly(2))
482 1
			->method('readAttribute');
483
484 1
		$image->expects($this->once())
485 1
			->method('valid')
486 1
			->will($this->returnValue(true));
487 1
		$image->expects($this->once())
488 1
			->method('width')
489 1
			->will($this->returnValue(128));
490 1
		$image->expects($this->once())
491 1
			->method('height')
492 1
			->will($this->returnValue(128));
493 1
		$image->expects($this->once())
494 1
			->method('centerCrop')
495 1
			->will($this->returnValue(true));
496
497 1
		$filesys->expects($this->once())
498 1
			->method('isLoaded')
499 1
			->will($this->returnValue(true));
500
501 1
		$avatar = $this->getMock('\OCP\IAvatar');
502 1
		$avatar->expects($this->once())
503 1
			->method('set')
504 1
			->with($this->isInstanceOf($image));
505
506 1
		$avaMgr->expects($this->once())
507 1
			->method('getAvatar')
508 1
			->with($this->equalTo('alice'))
509 1
			->will($this->returnValue($avatar));
510
511 1
		$uid = 'alice';
512 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
513
514 1
		$user = new User(
515 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
516
517 1
		$user->updateAvatar();
518 1
	}
519
520 1
	public function testUpdateAvatarNotProvided() {
521
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
522 1
			$this->getTestInstances();
523
524
		list($access, $connection) =
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
525 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
526
527 1
		$access->expects($this->at(0))
528 1
			->method('readAttribute')
529 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
530 1
				$this->equalTo('jpegPhoto'))
531 1
			->will($this->returnValue(false));
532
533 1
		$access->expects($this->at(1))
534 1
			->method('readAttribute')
535 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
536 1
				$this->equalTo('thumbnailPhoto'))
537 1
			->will($this->returnValue(false));
538
539 1
		$access->expects($this->exactly(2))
540 1
			->method('readAttribute');
541
542 1
		$image->expects($this->never())
543 1
			->method('valid');
544 1
		$image->expects($this->never())
545 1
			->method('width');
546 1
		$image->expects($this->never())
547 1
			->method('height');
548 1
		$image->expects($this->never())
549 1
			->method('centerCrop');
550
551 1
		$filesys->expects($this->never())
552 1
			->method('isLoaded');
553
554 1
		$avaMgr->expects($this->never())
555 1
			->method('getAvatar');
556
557 1
		$uid = 'alice';
558 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
559
560 1
		$user = new User(
561 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
562
563 1
		$user->updateAvatar();
564 1
	}
565
566 1 View Code Duplication
	public function testUpdateBeforeFirstLogin() {
567
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
568 1
			$this->getTestInstances();
569
570
		list($access, $connection) =
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
571 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
572
573 1
		$config->expects($this->at(0))
574 1
			->method('getUserValue')
575 1
			->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
576 1
				$this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
577 1
				$this->equalTo(0))
578 1
			->will($this->returnValue(0));
579
580 1
		$config->expects($this->at(1))
581 1
			->method('getUserValue')
582 1
			->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
583 1
				$this->equalTo(User::USER_PREFKEY_LASTREFRESH),
584 1
				$this->equalTo(0))
585 1
			->will($this->returnValue(0));
586
587 1
		$config->expects($this->exactly(2))
588 1
			->method('getUserValue');
589
590 1
		$config->expects($this->never())
591 1
			->method('setUserValue');
592
593 1
		$uid = 'alice';
594 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
595
596 1
		$user = new User(
597 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
598
599 1
		$user->update();
600 1
	}
601
602 1
	public function testUpdateAfterFirstLogin() {
603
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
604 1
			$this->getTestInstances();
605
606
		list($access, $connection) =
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
607 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
608
609 1
		$config->expects($this->at(0))
610 1
			->method('getUserValue')
611 1
			->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
612 1
				$this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
613 1
				$this->equalTo(0))
614 1
			->will($this->returnValue(1));
615
616 1
		$config->expects($this->at(1))
617 1
			->method('getUserValue')
618 1
			->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
619 1
				$this->equalTo(User::USER_PREFKEY_LASTREFRESH),
620 1
				$this->equalTo(0))
621 1
			->will($this->returnValue(0));
622
623 1
		$config->expects($this->exactly(2))
624 1
			->method('getUserValue');
625
626 1
		$config->expects($this->once())
627 1
			->method('setUserValue')
628 1
			->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
629 1
				$this->equalTo(User::USER_PREFKEY_LASTREFRESH),
630 1
				$this->anything())
631 1
			->will($this->returnValue(true));
632
633 1
		$uid = 'alice';
634 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
635
636 1
		$user = new User(
637 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
638
639 1
		$user->update();
640 1
	}
641
642 1 View Code Duplication
	public function testUpdateNoRefresh() {
643
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
644 1
			$this->getTestInstances();
645
646
		list($access, $connection) =
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
647 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
648
649 1
		$config->expects($this->at(0))
650 1
			->method('getUserValue')
651 1
			->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
652 1
				$this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
653 1
				$this->equalTo(0))
654 1
			->will($this->returnValue(1));
655
656 1
		$config->expects($this->at(1))
657 1
			->method('getUserValue')
658 1
			->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
659 1
				$this->equalTo(User::USER_PREFKEY_LASTREFRESH),
660 1
				$this->equalTo(0))
661 1
			->will($this->returnValue(time()));
662
663 1
		$config->expects($this->exactly(2))
664 1
			->method('getUserValue');
665
666 1
		$config->expects($this->never())
667 1
			->method('setUserValue');
668
669 1
		$uid = 'alice';
670 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
671
672 1
		$user = new User(
673 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
674
675 1
		$user->update();
676 1
	}
677
678 1
	public function testMarkLogin() {
679
		list($access, $config, $filesys, $image, $log, $avaMgr) =
680 1
			$this->getTestInstances();
681
682 1
		$config->expects($this->once())
683 1
			->method('setUserValue')
684 1
			->with($this->equalTo('alice'),
685 1
				$this->equalTo('user_ldap'),
686 1
				$this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
687 1
				$this->equalTo(1))
688 1
			->will($this->returnValue(true));
689
690 1
		$uid = 'alice';
691 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
692
693 1
		$user = new User(
694 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
695
696 1
		$user->markLogin();
697 1
	}
698
699 1
	public function testGetAvatarImageProvided() {
700
		list($access, $config, $filesys, $image, $log, $avaMgr) =
701 1
			$this->getTestInstances();
702
703 1
		$access->expects($this->once())
704 1
			->method('readAttribute')
705 1
			->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
706 1
				$this->equalTo('jpegPhoto'))
707 1
			->will($this->returnValue(array('this is a photo')));
708
709 1
		$uid = 'alice';
710 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
711
712 1
		$user = new User(
713 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
714
715 1
		$photo = $user->getAvatarImage();
716 1
		$this->assertSame('this is a photo', $photo);
717
		//make sure readAttribute is not called again but the already fetched
718
		//photo is returned
719 1
		$photo = $user->getAvatarImage();
0 ignored issues
show
Unused Code introduced by
$photo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
720 1
	}
721
722 1
	public function testProcessAttributes() {
723
		list(, $config, $filesys, $image, $log, $avaMgr, $dbc) =
724 1
			$this->getTestInstances();
725
726
		list($access, $connection) =
727 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
728
729 1
		$uid = 'alice';
730 1
		$dn = 'uid=alice';
731
732
		$requiredMethods = array(
733 1
			'markRefreshTime',
734 1
			'updateQuota',
735 1
			'updateEmail',
736 1
			'composeAndStoreDisplayName',
737 1
			'storeLDAPUserName',
738 1
			'getHomePath',
739
			'updateAvatar'
740 1
		);
741
742 1
		$userMock = $this->getMockBuilder('OCA\user_ldap\lib\user\User')
743 1
			->setConstructorArgs(array($uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr))
744 1
			->setMethods($requiredMethods)
745 1
			->getMock();
746
747 1
		$connection->setConfiguration(array(
748
			'homeFolderNamingRule' => 'homeDirectory'
749 1
		));
750
751 1
		$connection->expects($this->any())
752 1
			->method('__get')
753
			//->will($this->returnArgument(0));
754 1
			->will($this->returnCallback(function($name) {
755 1
				if($name === 'homeFolderNamingRule') {
756 1
					return 'attr:homeDirectory';
757
				}
758 1
				return $name;
759 1
			}));
760
761
		$record = array(
762 1
			strtolower($connection->ldapQuotaAttribute) => array('4096'),
763 1
			strtolower($connection->ldapEmailAttribute) => array('[email protected]'),
764 1
			strtolower($connection->ldapUserDisplayName) => array('Aaaaalice'),
765 1
			'uid' => array($uid),
766 1
			'homedirectory' => array('Alice\'s Folder'),
767 1
			'memberof' => array('cn=groupOne', 'cn=groupTwo'),
768 1
			'jpegphoto' => array('here be an image')
769 1
		);
770
771 1
		foreach($requiredMethods as $method) {
772 1
			$userMock->expects($this->once())
773 1
				->method($method);
774 1
		}
775
776 1
		$userMock->processAttributes($record);
777 1
	}
778
779
	public function emptyHomeFolderAttributeValueProvider() {
780
		return array(
781
			'empty' => array(''),
782
			'prefixOnly' => array('attr:'),
783
		);
784
	}
785
786
	/**
787
	 * @dataProvider emptyHomeFolderAttributeValueProvider
788
	 */
789 2
	public function testGetHomePathNotConfigured($attributeValue) {
790
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
791 2
			$this->getTestInstances();
792
793
		list($access, $connection) =
794 2
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
795
796 2
		$connection->expects($this->any())
797 2
			->method('__get')
798 2
			->with($this->equalTo('homeFolderNamingRule'))
799 2
			->will($this->returnValue($attributeValue));
800
801 2
		$access->expects($this->never())
802 2
			->method('readAttribute');
803
804 2
		$config->expects($this->never())
805 2
			->method('getAppValue');
806
807 2
		$uid = 'alice';
808 2
		$dn  = 'uid=alice,dc=foo,dc=bar';
809
810 2
		$user = new User(
811 2
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
812
813 2
		$path = $user->getHomePath();
814 2
		$this->assertSame($path, false);
815 2
	}
816
817 1 View Code Duplication
	public function testGetHomePathConfiguredNotAvailableAllowed() {
818
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
819 1
			$this->getTestInstances();
820
821
		list($access, $connection) =
822 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
823
824 1
		$connection->expects($this->any())
825 1
			->method('__get')
826 1
			->with($this->equalTo('homeFolderNamingRule'))
827 1
			->will($this->returnValue('attr:foobar'));
828
829 1
		$access->expects($this->once())
830 1
			->method('readAttribute')
831 1
			->will($this->returnValue(false));
832
833
		// asks for "enforce_home_folder_naming_rule"
834 1
		$config->expects($this->once())
835 1
			->method('getAppValue')
836 1
			->will($this->returnValue(false));
837
838 1
		$uid = 'alice';
839 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
840
841 1
		$user = new User(
842 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
843
844 1
		$path = $user->getHomePath();
845
846 1
		$this->assertSame($path, false);
847 1
	}
848
849
	/**
850
	 * @expectedException \Exception
851
	 */
852 1 View Code Duplication
	public function testGetHomePathConfiguredNotAvailableNotAllowed() {
853
		list($access, $config, $filesys, $image, $log, $avaMgr, $dbc) =
854 1
			$this->getTestInstances();
855
856
		list($access, $connection) =
857 1
			$this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
858
859 1
		$connection->expects($this->any())
860 1
			->method('__get')
861 1
			->with($this->equalTo('homeFolderNamingRule'))
862 1
			->will($this->returnValue('attr:foobar'));
863
864 1
		$access->expects($this->once())
865 1
			->method('readAttribute')
866 1
			->will($this->returnValue(false));
867
868
		// asks for "enforce_home_folder_naming_rule"
869 1
		$config->expects($this->once())
870 1
			->method('getAppValue')
871 1
			->will($this->returnValue(true));
872
873 1
		$uid = 'alice';
874 1
		$dn  = 'uid=alice,dc=foo,dc=bar';
875
876 1
		$user = new User(
877 1
			$uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr);
878
879 1
		$user->getHomePath();
880
	}
881
882 View Code Duplication
	public function displayNameProvider() {
883
		return [
884
			['Roland Deschain', '', 'Roland Deschain'],
885
			['Roland Deschain', null, 'Roland Deschain'],
886
			['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])'],
887
		];
888
	}
889
890
	/**
891
	 * @dataProvider displayNameProvider
892
	 */
893 3 View Code Duplication
	public function testComposeAndStoreDisplayName($part1, $part2, $expected) {
894
		list($access, $config, $filesys, $image, $log, $avaMgr) =
895 3
			$this->getTestInstances();
896
897 3
		$config->expects($this->once())
898 3
			->method('setUserValue');
899
900 3
		$user = new User(
901 3
			'user', 'cn=user', $access, $config, $filesys, $image, $log, $avaMgr);
902
903 3
		$displayName = $user->composeAndStoreDisplayName($part1, $part2);
904 3
		$this->assertSame($expected, $displayName);
905 3
	}
906
}
907