UniLoginTest::testConvertRoleValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 22
c 3
b 0
f 0
nc 2
nop 0
dl 0
loc 24
rs 9.568
1
<?php
2
App::uses('UniLogin', 'UniLoginWebservice.Model');
3
App::uses('PHPUnitUtil', 'Lib');
4
5
class TestUniLogin extends UniLogin {
6
7
/**
8
 * Public test double of `parent::_getAuthParameters`.
9
 *
10
 */
11
	public function getAuthParameters() {
12
		return parent::_getAuthParameters();
13
	}
14
15
/**
16
 * Public test double of `parent::_convertUserList`.
17
 *
18
 */
19
	public function convertUserList($users) {
20
		return parent::_convertUserList($users);
21
	}
22
23
/**
24
 * Public test double of `parent::_convertInstitution`.
25
 *
26
 */
27
	public function convertInstitution($role, $minimal = false) {
28
		return parent::_convertInstitution($role, $minimal);
29
	}
30
31
/**
32
 * Public test double of `parent::_convertInstitutionList`.
33
 *
34
 */
35
	public function convertInstitutionList($institutions) {
36
		return parent::_convertInstitutionList($institutions);
37
	}
38
39
/**
40
 * Public test double of `parent::_convertRole`.
41
 *
42
 */
43
	public function convertRole($role) {
44
		return parent::_convertRole($role);
45
	}
46
47
/**
48
 * Public test double of `parent::_convertUser`.
49
 *
50
 */
51
	public function convertUser($user, $minimal = false) {
52
		return parent::_convertUser($user, $minimal);
53
	}
54
55
/**
56
 * Public test double of `parent::_parseDate`.
57
 *
58
 */
59
	public function parseDate($dateString) {
60
		return parent::_parseDate($dateString);
61
	}
62
63
}
64
65
/**
66
 * UniLogin Test.
67
 *
68
 */
69
class UniLoginTest extends CakeTestCase {
0 ignored issues
show
Bug introduced by
The type CakeTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
71
/**
72
 * Fixtures.
73
 *
74
 * @var array
75
 */
76
	public $fixtures = [];
77
78
/**
79
 * setUp method
80
 *
81
 * @return void
82
 */
83
	public function setUp() {
84
		parent::setUp();
85
86
		$this->UniLogin = ClassRegistry::init('TestUniLogin');
0 ignored issues
show
Bug introduced by
The type ClassRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug Best Practice introduced by
The property UniLogin does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
	}
88
89
/**
90
 * tearDown method
91
 *
92
 * @return void
93
 */
94
	public function tearDown() {
95
		unset($this->UniLogin);
96
97
		parent::tearDown();
98
	}
99
100
/**
101
 * Converts a (minimal) user array to a user object.
102
 *
103
 * @param array $user User data
104
 * @return \stdClass User object
105
 */
106
	protected function _convertUserMinimal($user) {
107
		$result = new stdClass();
108
		$result->Brugerid = $user['uni_login_key'];
109
		$result->Navn = $user['full_name'];
110
111
		return $result;
112
	}
113
114
/**
115
 * Converts a (minimal) institution array to a institution object.
116
 *
117
 * @param array $institution Institution data
118
 * @return \stdClass Institution object
119
 */
120
	protected function _convertInstitutionMinimal($institution) {
121
		$result = new stdClass();
122
		$result->Instnr = $institution['uni_login_key'];
123
		$result->Navn = $institution['name'];
124
125
		return $result;
126
	}
127
128
/**
129
 * Tests `UniLogin::getAuthParameters`.
130
 *
131
 * @return void
132
 */
133
	public function testGetAuthParameters() {
134
		$restore = Configure::read('UniLoginWebservice.wsBrugerid');
0 ignored issues
show
Bug introduced by
The type Configure was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
135
136
		Configure::write('UniLoginWebservice.wsBrugerid', 'wsBrugerid');
137
		Configure::write('UniLoginWebservice.wsPassword', 'wsPassword');
138
139
		$expected = [
140
			'wsBrugerid' => 'wsBrugerid',
141
			'wsPassword' => 'wsPassword'
142
		];
143
		$resut = $this->UniLogin->getAuthParameters();
144
		$this->assertEquals($expected, $resut);
145
146
		Configure::write('UniLoginWebservice', $restore);
147
	}
148
149
/**
150
 * Tests `UniLogin::convertUserList`.
151
 *
152
 * @return void
153
 */
154
	public function testConvertUserList() {
155
		$expected = [];
156
		$expected[] = [
157
			'uni_login_key' => '123456',
158
			'full_name' => 'My Full Name'
159
		];
160
		$userList = new stdClass();
161
		$userList->PersonSimpel = [];
162
163
		foreach ($expected as $item) {
164
			$userList->PersonSimpel[] = $this->_convertUserMinimal($item);
165
		}
166
		$result = $this->UniLogin->convertUserList($userList);
167
		$this->assertEquals($expected, $result);
168
	}
169
170
/**
171
 * Tests `UniLogin::convertUserList`.
172
 *
173
 * @return void
174
 */
175
	public function testConvertUserListValid() {
176
		$expected = [
177
			[
178
				'uni_login_key' => 'Fe5Echew',
179
				'full_name' => 'Amalie Jansson'
180
			],
181
			[
182
				'uni_login_key' => 'naJutr4s',
183
				'full_name' => 'Katrine Juncker'
184
			]
185
		];
186
		$userList = new stdClass();
187
		$userList->PersonSimpel = [];
188
		$user = new stdClass();
189
		$user->Navn = 'Amalie Jansson';
190
		$user->Brugerid = 'Fe5Echew';
191
		$userList->PersonSimpel[] = $user;
192
		$user = new stdClass();
193
		$user->Navn = 'Katrine Juncker';
194
		$user->Brugerid = 'naJutr4s';
195
		$userList->PersonSimpel[] = $user;
196
		$result = $this->UniLogin->convertUserList($userList);
197
		$this->assertEquals($expected, $result);
198
	}
199
200
/**
201
 * Tests `UniLogin::convertRole`.
202
 *
203
 * @return void
204
 */
205
	public function testConvertRoleInvalid() {
206
		$role = 'unknown';
207
		$result = $this->UniLogin->convertRole($role);
208
		$this->assertFalse($result);
209
	}
210
211
/**
212
 * Tests `UniLogin::convertRole`.
213
 *
214
 * @return void
215
 */
216
	public function testConvertRoleValid() {
217
		$mapping = [
218
			'lærer' => 'teacher',
219
			'tap' => 'technical / administrative employee',
220
			'pæd' => 'educator', // Pædagog
221
			'elev' => 'pupil',
222
			'stud' => 'student',
223
			'kursist' => 'anonymous user with limited lifespan',
224
			'klasse' => 'class',
225
			'skole' => 'common school login',
226
			'Instleder' => 'director at institution',
227
			'Instledelse' => 'board of directors',
228
			'Brugeradm' => 'user administrator',
229
			'brugeradm_sup' => 'additional user administrator',
230
			'Kontakt' => 'contact person at institution',
231
			'uni_server_adm' => 'UNI-Server administrator',
232
			'uni_server_indholds_adm' => 'UNI-Server Content administrator',
233
			'hjpc_ansv' => 'HomePC responsible',
234
			'hjpc_ansv_a' => 'HomePC responsible for A-leg',
235
			'hjpc_ansv_p' => 'HomePC responsible for P-leg'
236
		];
237
		foreach ($mapping as $role => $expected) {
238
			$result = $this->UniLogin->convertRole($role);
239
			$this->assertEquals($expected, $result);
240
		}
241
	}
242
243
/**
244
 * Tests `UniLogin::convertInstitution`.
245
 *
246
 * @return void
247
 */
248
	public function testConvertInstitutionInvalid() {
249
		$institution = null;
250
		$result = $this->UniLogin->convertInstitution($institution);
251
		$this->assertFalse($result);
252
253
		$institution = new stdClass();
254
		$result = $this->UniLogin->convertInstitution($institution);
255
		$this->assertFalse($result);
256
	}
257
258
/**
259
 * Tests `UniLogin::convertInstitution`.
260
 *
261
 * @return void
262
 */
263
	public function testConvertInstitutionMinimalValid() {
264
		$minimal = true;
265
		$institution = new stdClass();
266
		$institution->Instnr = '101001';
267
		$institution->Navn = 'Name of institution.';
268
		$expected = [
269
			'uni_login_key' => '101001',
270
			'name' => 'Name of institution.'
271
		];
272
		$result = $this->UniLogin->convertInstitution($institution, $minimal);
273
		$this->assertEquals($expected, $result);
274
	}
275
276
/**
277
 * Tests `UniLogin::convertInstitution`.
278
 *
279
 * @return void
280
 */
281
	public function testConvertInstitutionValid() {
282
		$institution = new stdClass();
283
		$institution->Instnr = '101001';
284
		$institution->Navn = 'Name of institution.';
285
		$institution->Type = '121';
286
		$institution->Typenavn = 'Grundskoler';
287
		$institution->Adresse = 'Address';
288
		$institution->Bynavn = 'City name';
289
		$institution->Postnr = 'Zip code';
290
		$institution->Telefonnr = 'Phone number';
291
		$institution->Faxnr = 'Fax number';
292
		$institution->Mailadresse = 'Mail address';
293
		$institution->Www = 'URL of institution';
294
		$institution->Hovedinstitutionsnr = '101004';
295
		$institution->Kommunenr = '123';
296
		$institution->Kommune = 'Name of municipal';
297
		$institution->Admkommunenr = '456';
298
		$institution->Admkommune = 'Name of the administrating municipal';
299
		$institution->Regionsnr = '1234';
300
		$institution->Region = 'Name of the region';
301
		$expected = [
302
			'uni_login_key' => '101001',
303
			'name' => 'Name of institution.',
304
			'type' => '121',
305
			'type_name' => 'Grundskoler',
306
			'address' => 'Address',
307
			'city' => 'City name',
308
			'zip_code' => 'Zip code',
309
			'phone_number' => 'Phone number',
310
			'fax_number' => 'Fax number',
311
			'mail_address' => 'Mail address',
312
			'website' => 'URL of institution',
313
			'parent_institution_uni_login_key' => '101004',
314
			'municipal' => '123',
315
			'municipal_name' => 'Name of municipal',
316
			'administrating_municipal' => '456',
317
			'administrating_municipal_name' => 'Name of the administrating municipal',
318
			'region' => '1234',
319
			'region_name' => 'Name of the region',
320
		];
321
		$result = $this->UniLogin->convertInstitution($institution);
322
		$this->assertEquals($expected, $result);
323
	}
324
325
/**
326
 * Tests `UniLogin::convertUserList`.
327
 *
328
 * @return void
329
 */
330
	public function testConvertInstitutionListMultiple() {
331
		$expected = [];
332
		$expected[] = [
333
			'uni_login_key' => '123456',
334
			'name' => 'Name'
335
		];
336
		$institutionList = new stdClass();
337
		$institutionList->InstitutionSimpel = [];
338
339
		foreach ($expected as $item) {
340
			$institutionList->InstitutionSimpel[] = $this->_convertInstitutionMinimal($item);
341
		}
342
		$result = $this->UniLogin->convertInstitutionList($institutionList);
343
		$this->assertEquals($expected, $result);
344
	}
345
346
/**
347
 * Tests `UniLogin::convertUserList`.
348
 *
349
 * @return void
350
 */
351
	public function testConvertInstitutionListSingle() {
352
		$item = [
353
			'uni_login_key' => '123456',
354
			'name' => 'Name'
355
		];
356
		$institutionList = new stdClass();
357
358
		$institutionList->InstitutionSimpel = $this->_convertInstitutionMinimal($item);
359
		$result = $this->UniLogin->convertInstitutionList($institutionList);
360
		$expected = [$item];
361
		$this->assertEquals($expected, $result);
362
	}
363
364
/**
365
 * Tests `UniLogin::convertUser`.
366
 *
367
 * @return void
368
 */
369
	public function testConvertUserInvalid() {
370
		$user = null;
371
		$result = $this->UniLogin->convertUser($user);
372
		$this->assertFalse($result);
373
374
		$user = new stdClass();
375
		$result = $this->UniLogin->convertUser($user);
376
		$this->assertFalse($result);
377
378
		$user = new stdClass();
379
		$user->brugerid = '101001';
380
		$user->navn = 'Svend Hansen';
381
		$result = $this->UniLogin->convertUser($user);
382
		$this->assertFalse($result);
383
	}
384
385
/**
386
 * Tests `UniLogin::convertUser`.
387
 *
388
 * @return void
389
 */
390
	public function testConvertUserMinimalValid() {
391
		$minimal = true;
392
		$user = new stdClass();
393
		$user->Brugerid = '101001';
394
		$user->Navn = 'Svend Hansen';
395
		$expected = [
396
			'uni_login_key' => '101001',
397
			'full_name' => 'Svend Hansen'
398
		];
399
		$result = $this->UniLogin->convertUser($user, $minimal);
400
		$this->assertEquals($expected, $result);
401
	}
402
403
/**
404
 * Tests `UniLogin::convertUser`.
405
 *
406
 * @return void
407
 */
408
	public function testConvertUserValid() {
409
		$user = new stdClass();
410
		$user->Brugerid = '101001';
411
		$user->Navn = 'Svend Hansen';
412
		$user->Fornavn = 'Svend';
413
		$user->Efternavn = 'Hansen';
414
		$user->SkolekomNavn = 'Svend Hansen12';
415
		$user->Mailadresse = '[email protected]';
416
		$user->Instnr = '101005';
417
		$user->Funktionsmarkering = 'kursist';
418
		$user->Foedselsdag = '130597';
419
		$expected = [
420
			'uni_login_key' => '101001',
421
			'full_name' => 'Svend Hansen',
422
			'first_name' => 'Svend',
423
			'last_name' => 'Hansen',
424
			'username' => 'Svend Hansen12',
425
			'email' => '[email protected]',
426
			'school_uni_login_key' => '101005',
427
			'uni_login_role' => 'kursist',
428
			'role' => 'anonymous user with limited lifespan',
429
			'date_of_birth' => '1997-05-13'
430
		];
431
		$result = $this->UniLogin->convertUser($user);
432
		$this->assertEquals($expected, $result);
433
	}
434
435
/**
436
 * Tests `UniLogin::parseDate`.
437
 *
438
 * @return void
439
 */
440
	public function testParseDateInvalid() {
441
		$date = '';
442
		$result = $this->UniLogin->parseDate($date);
443
		$this->assertFalse($result);
444
445
		$date = 'abc';
446
		$result = $this->UniLogin->parseDate($date);
447
		$this->assertFalse($result);
448
	}
449
450
/**
451
 * Tests `UniLogin::parseDate`.
452
 *
453
 * @return void
454
 */
455
	public function testParseDateValid() {
456
		$expected = '2003-02-01';
457
		$date = '010203';
458
		$result = $this->UniLogin->parseDate($date);
459
		$this->assertEquals($expected, $result);
460
461
		$expected = '1990-10-30';
462
		$date = '301090';
463
		$result = $this->UniLogin->parseDate($date);
464
		$this->assertEquals($expected, $result);
465
	}
466
467
}
468