Completed
Push — master ( 59965c...4339a8 )
by
unknown
114:08 queued 95:41
created

UserHandlerTest   F

Complexity

Total Complexity 57

Size/Duplication

Total Lines 1132
Duplicated Lines 10.16 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 115
loc 1132
rs 1.8082
c 0
b 0
f 0
wmc 57
lcom 1
cbo 16

52 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserHandler() 0 11 1
A getValidUser() 0 13 1
A getValidUserToken() 0 9 1
A testCreateUser() 0 17 1
A testCreateDuplicateUser() 0 7 1
A testInsertIncompleteUser() 0 9 1
A testLoadUser() 0 10 1
A testLoadUnknownUser() 0 6 1
A testLoadUserByLogin() 0 11 1
A testLoadUserByEmailNotFound() 0 7 1
A testLoadUserByEmail() 0 11 1
A testLoadUserByTokenNotFound() 0 8 1
A testLoadUserByToken() 0 12 1
A testUpdateUserToken() 20 20 1
A testExpireUserToken() 20 20 1
B testCreateAndDeleteUser() 0 30 1
A testDeleteNonExistingUser() 11 11 1
A testUpdateUser() 15 15 1
A testUpdateUserSettings() 15 15 1
A testSilentlyUpdateNotExistingUser() 10 10 1
A testCreateNewRoleWithoutPolicies() 0 15 1
A testCreateRoleDraftWithoutPolicies() 0 22 1
A testCreateNewRoleRoleId() 0 11 1
A testLoadRole() 0 16 1
B testLoadRoleWithPolicies() 0 33 1
B testLoadRoleWithPoliciesAndGroups() 0 37 1
B testLoadRoleWithPolicyLimitations() 0 40 1
A testLoadRoles() 0 21 1
A testUpdateRole() 0 23 1
B testDeleteRole() 0 26 1
B testDeleteRoleDraft() 0 27 1
A testAddPolicyToRoleLimitations() 0 23 1
A testAddPolicyPolicyId() 0 19 1
B testAddPolicyLimitations() 0 30 1
B testAddPolicyLimitationValues() 0 31 1
B createRole() 0 25 1
A testImplicitlyCreatePolicies() 0 13 1
A testDeletePolicy() 0 16 1
A testDeletePolicyLimitations() 12 12 1
A testDeletePolicyLimitationValues() 12 12 1
B testUpdatePolicies() 0 29 1
A testAddRoleToUser() 0 19 1
B testAddRoleToUserWithLimitation() 0 25 1
B testAddRoleToUserWithComplexLimitation() 0 28 1
B testRemoveUserRoleAssociation() 0 26 1
B testLoadPoliciesForUser() 0 24 2
B testLoadRoleAssignmentsByGroupId() 0 43 1
A testLoadRoleAssignmentsByGroupIdInherited() 0 18 1
A testLoadComplexRoleAssignments() 0 67 1
B testLoadRoleAssignmentsByRoleId() 0 32 1
A testLoadRoleDraftByRoleId() 0 12 1
B testRoleDraftOnlyHavePolicyDraft() 0 36 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like UserHandlerTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UserHandlerTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\User\UserHandlerTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Tests\User;
10
11
use eZ\Publish\API\Repository\Values\User\Role as APIRole;
12
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase;
13
use eZ\Publish\Core\Persistence\Legacy\User;
14
use eZ\Publish\Core\Persistence\Legacy\User\Role\LimitationConverter;
15
use eZ\Publish\Core\Persistence\Legacy\User\Role\LimitationHandler\ObjectStateHandler as ObjectStateLimitationHandler;
16
use eZ\Publish\SPI\Persistence;
17
18
/**
19
 * Test case for UserHandlerTest.
20
 */
21
class UserHandlerTest extends TestCase
22
{
23
    protected function getUserHandler()
24
    {
25
        $dbHandler = $this->getDatabaseHandler();
26
27
        return new User\Handler(
28
            new User\Gateway\DoctrineDatabase($dbHandler),
29
            new User\Role\Gateway\DoctrineDatabase($dbHandler),
30
            new User\Mapper(),
31
            new LimitationConverter(array(new ObjectStateLimitationHandler($dbHandler)))
32
        );
33
    }
34
35
    protected function getValidUser()
36
    {
37
        $user = new Persistence\User();
38
        $user->id = 42;
39
        $user->login = 'kore';
40
        $user->email = '[email protected]';
41
        $user->passwordHash = '1234567890';
42
        $user->hashAlgorithm = 2;
43
        $user->isEnabled = true;
44
        $user->maxLogin = 23;
45
46
        return $user;
47
    }
48
49
    protected function getValidUserToken($time = null)
50
    {
51
        $userToken = new Persistence\User\UserTokenUpdateStruct();
52
        $userToken->userId = 42;
53
        $userToken->hashKey = md5('hash');
54
        $userToken->time = $time ?? (new \DateTime())->add(new \DateInterval('P1D'))->getTimestamp();
55
56
        return $userToken;
57
    }
58
59
    public function testCreateUser()
60
    {
61
        $handler = $this->getUserHandler();
62
63
        $handler->create($this->getValidUser());
64
        $this->assertQueryResult(
65
            array(array(1)),
66
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser'),
67
            'Expected one user to be created.'
68
        );
69
70
        $this->assertQueryResult(
71
            array(array(1)),
72
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser_setting'),
73
            'Expected one user setting to be created.'
74
        );
75
    }
76
77
    /**
78
     * @expectedException \Doctrine\DBAL\DBALException
79
     */
80
    public function testCreateDuplicateUser()
81
    {
82
        $handler = $this->getUserHandler();
83
84
        $handler->create($user = $this->getValidUser());
85
        $handler->create($user);
86
    }
87
88
    /**
89
     * @expectedException \Doctrine\DBAL\DBALException
90
     */
91
    public function testInsertIncompleteUser()
92
    {
93
        $handler = $this->getUserHandler();
94
95
        $user = new Persistence\User();
96
        $user->id = 42;
97
98
        $handler->create($user);
99
    }
100
101
    public function testLoadUser()
102
    {
103
        $handler = $this->getUserHandler();
104
        $handler->create($user = $this->getValidUser());
105
106
        $this->assertEquals(
107
            $user,
108
            $handler->load($user->id)
109
        );
110
    }
111
112
    /**
113
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
114
     */
115
    public function testLoadUnknownUser()
116
    {
117
        $handler = $this->getUserHandler();
118
119
        $handler->load(1337);
120
    }
121
122
    public function testLoadUserByLogin()
123
    {
124
        $handler = $this->getUserHandler();
125
        $handler->create($user = $this->getValidUser());
126
127
        $loadedUser = $handler->loadByLogin($user->login);
128
        $this->assertEquals(
129
            $user,
130
            $loadedUser
131
        );
132
    }
133
134
    /**
135
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
136
     */
137
    public function testLoadUserByEmailNotFound()
138
    {
139
        $handler = $this->getUserHandler();
140
        $handler->create($user = $this->getValidUser());
141
142
        $handler->loadByLogin($user->email);
143
    }
144
145
    public function testLoadUserByEmail()
146
    {
147
        $handler = $this->getUserHandler();
148
        $handler->create($user = $this->getValidUser());
149
150
        $users = $handler->loadByEmail($user->email);
151
        $this->assertEquals(
152
            $user,
153
            $users[0]
154
        );
155
    }
156
157
    /**
158
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
159
     */
160
    public function testLoadUserByTokenNotFound()
161
    {
162
        $handler = $this->getUserHandler();
163
        $handler->create($user = $this->getValidUser());
164
        $handler->updateUserToken($this->getValidUserToken());
165
166
        $handler->loadUserByToken('asd');
167
    }
168
169
    public function testLoadUserByToken()
170
    {
171
        $handler = $this->getUserHandler();
172
        $handler->create($user = $this->getValidUser());
173
        $handler->updateUserToken($userToken = $this->getValidUserToken());
174
175
        $loadedUser = $handler->loadUserByToken($userToken->hashKey);
176
        $this->assertEquals(
177
            $user,
178
            $loadedUser
179
        );
180
    }
181
182 View Code Duplication
    public function testUpdateUserToken()
183
    {
184
        $handler = $this->getUserHandler();
185
186
        $handler->updateUserToken($userToken = $this->getValidUserToken(1234567890));
187
188
        $this->assertQueryResult(
189
            [['0800fc577294c34e0b28ad2839435945', 1, 1234567890, 42]],
190
            $this->handler->createSelectQuery()->select('*')->from('ezuser_accountkey'),
191
            'Expected user data to be updated.'
192
        );
193
194
        $handler->updateUserToken($userToken = $this->getValidUserToken(2234567890));
195
196
        $this->assertQueryResult(
197
            [['0800fc577294c34e0b28ad2839435945', 1, 2234567890, 42]],
198
            $this->handler->createSelectQuery()->select('*')->from('ezuser_accountkey'),
199
            'Expected user token data to be updated.'
200
        );
201
    }
202
203 View Code Duplication
    public function testExpireUserToken()
204
    {
205
        $handler = $this->getUserHandler();
206
207
        $handler->updateUserToken($userToken = $this->getValidUserToken(1234567890));
208
209
        $this->assertQueryResult(
210
            [['0800fc577294c34e0b28ad2839435945', 1, 1234567890, 42]],
211
            $this->handler->createSelectQuery()->select('*')->from('ezuser_accountkey'),
212
            'Expected user data to be updated.'
213
        );
214
215
        $handler->expireUserToken($userToken->hashKey);
216
217
        $this->assertQueryResult(
218
            [['0800fc577294c34e0b28ad2839435945', 1, 0, 42]],
219
            $this->handler->createSelectQuery()->select('*')->from('ezuser_accountkey'),
220
            'Expected user token to be expired.'
221
        );
222
    }
223
224
    public function testCreateAndDeleteUser()
225
    {
226
        $handler = $this->getUserHandler();
227
228
        $handler->create($user = $this->getValidUser());
229
        $this->assertQueryResult(
230
            array(array(1)),
231
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser'),
232
            'Expected one user to be created.'
233
        );
234
235
        $this->assertQueryResult(
236
            array(array(1)),
237
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser_setting'),
238
            'Expected one user setting to be created.'
239
        );
240
241
        $handler->delete($user->id);
242
        $this->assertQueryResult(
243
            array(array(0)),
244
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser'),
245
            'Expected one user to be removed.'
246
        );
247
248
        $this->assertQueryResult(
249
            array(array(0)),
250
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser_setting'),
251
            'Expected one user setting to be removed.'
252
        );
253
    }
254
255 View Code Duplication
    public function testDeleteNonExistingUser()
256
    {
257
        $handler = $this->getUserHandler();
258
259
        $handler->delete(1337);
260
        $this->assertQueryResult(
261
            array(array(0)),
262
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser'),
263
            'Expected no existing user.'
264
        );
265
    }
266
267 View Code Duplication
    public function testUpdateUser()
268
    {
269
        $handler = $this->getUserHandler();
270
271
        $handler->create($user = $this->getValidUser());
272
273
        $user->login = 'New_lögin';
274
        $handler->update($user);
275
276
        $this->assertQueryResult(
277
            array(array(42, '[email protected]', 'New_lögin', 1234567890, '2')),
278
            $this->handler->createSelectQuery()->select('*')->from('ezuser'),
279
            'Expected user data to be updated.'
280
        );
281
    }
282
283 View Code Duplication
    public function testUpdateUserSettings()
284
    {
285
        $handler = $this->getUserHandler();
286
287
        $handler->create($user = $this->getValidUser());
288
289
        $user->maxLogin = 42;
290
        $handler->update($user);
291
292
        $this->assertQueryResult(
293
            array(array(1, 42, 42)),
294
            $this->handler->createSelectQuery()->select('*')->from('ezuser_setting'),
295
            'Expected user data to be updated.'
296
        );
297
    }
298
299 View Code Duplication
    public function testSilentlyUpdateNotExistingUser()
300
    {
301
        $handler = $this->getUserHandler();
302
        $handler->update($this->getValidUser());
303
        $this->assertQueryResult(
304
            array(array(0)),
305
            $this->handler->createSelectQuery()->select('COUNT( * )')->from('ezuser'),
306
            'Expected no existing user.'
307
        );
308
    }
309
310
    public function testCreateNewRoleWithoutPolicies()
311
    {
312
        $handler = $this->getUserHandler();
313
314
        $createStruct = new Persistence\User\RoleCreateStruct();
315
        $createStruct->identifier = 'Test';
316
317
        $handler->createRole($createStruct);
318
319
        $this->assertQueryResult(
320
            array(array(1, 'Test', -1)),
321
            $this->handler->createSelectQuery()->select('id', 'name', 'version')->from('ezrole'),
322
            'Expected a new role draft.'
323
        );
324
    }
325
326
    public function testCreateRoleDraftWithoutPolicies()
327
    {
328
        $handler = $this->getUserHandler();
329
330
        $createStruct = new Persistence\User\RoleCreateStruct();
331
        $createStruct->identifier = 'Test';
332
333
        $roleDraft = $handler->createRole($createStruct);
334
        $handler->publishRoleDraft($roleDraft->id);
335
336
        $handler->createRoleDraft($roleDraft->id);
337
338
        $publishedRoleId = 1;
339
        $this->assertQueryResult(
340
            [
341
                [$publishedRoleId, 'Test', APIRole::STATUS_DEFINED],
342
                [2, 'Test', $publishedRoleId],
343
            ],
344
            $this->handler->createSelectQuery()->select('id', 'name', 'version')->from('ezrole'),
345
            'Expected a role and a role draft.'
346
        );
347
    }
348
349
    public function testCreateNewRoleRoleId()
350
    {
351
        $handler = $this->getUserHandler();
352
353
        $createStruct = new Persistence\User\RoleCreateStruct();
354
        $createStruct->identifier = 'Test';
355
356
        $roleDraft = $handler->createRole($createStruct);
357
358
        $this->assertSame('1', $roleDraft->id);
359
    }
360
361
    public function testLoadRole()
362
    {
363
        $handler = $this->getUserHandler();
364
365
        $createStruct = new Persistence\User\RoleCreateStruct();
366
        $createStruct->identifier = 'Test';
367
368
        $roleDraft = $handler->createRole($createStruct);
369
        $handler->publishRoleDraft($roleDraft->id);
370
        $role = $handler->loadRole($roleDraft->id);
371
372
        $this->assertEquals(
373
            $roleDraft->id,
374
            $role->id
375
        );
376
    }
377
378
    public function testLoadRoleWithPolicies()
379
    {
380
        $handler = $this->getUserHandler();
381
382
        $createStruct = new Persistence\User\RoleCreateStruct();
383
        $createStruct->identifier = 'Test';
384
385
        $roleDraft = $handler->createRole($createStruct);
386
387
        $policy = new Persistence\User\Policy();
388
        $policy->module = 'foo';
389
        $policy->function = 'bar';
390
391
        $handler->addPolicyByRoleDraft($roleDraft->id, $policy);
392
        $handler->publishRoleDraft($roleDraft->id);
393
394
        $loaded = $handler->loadRole($roleDraft->id);
395
        $this->assertEquals(
396
            array(
397
                new Persistence\User\Policy(
398
                    array(
399
                        'id' => 1,
400
                        'roleId' => 1,
401
                        'module' => 'foo',
402
                        'function' => 'bar',
403
                        'limitations' => '*',
404
                        'originalId' => null,
405
                    )
406
                ),
407
            ),
408
            $loaded->policies
409
        );
410
    }
411
412
    public function testLoadRoleWithPoliciesAndGroups()
413
    {
414
        $handler = $this->getUserHandler();
415
416
        $createStruct = new Persistence\User\RoleCreateStruct();
417
        $createStruct->identifier = 'Test';
418
419
        $roleDraft = $handler->createRole($createStruct);
420
421
        $policy = new Persistence\User\Policy();
422
        $policy->module = 'foo';
423
        $policy->function = 'bar';
424
425
        $handler->addPolicyByRoleDraft($roleDraft->id, $policy);
426
427
        $handler->assignRole(23, $roleDraft->id);
428
        $handler->assignRole(42, $roleDraft->id);
429
430
        $handler->publishRoleDraft($roleDraft->id);
431
432
        $loaded = $handler->loadRole($roleDraft->id);
433
        $this->assertEquals(
434
            array(
435
                new Persistence\User\Policy(
436
                    array(
437
                        'id' => 1,
438
                        'roleId' => 1,
439
                        'module' => 'foo',
440
                        'function' => 'bar',
441
                        'limitations' => '*',
442
                        'originalId' => null,
443
                    )
444
                ),
445
            ),
446
            $loaded->policies
447
        );
448
    }
449
450
    public function testLoadRoleWithPolicyLimitations()
451
    {
452
        $handler = $this->getUserHandler();
453
454
        $createStruct = new Persistence\User\RoleCreateStruct();
455
        $createStruct->identifier = 'Test';
456
457
        $roleDraft = $handler->createRole($createStruct);
458
459
        $policy = new Persistence\User\Policy();
460
        $policy->module = 'foo';
461
        $policy->function = 'bar';
462
        $policy->limitations = array(
463
            'Subtree' => array('/1', '/1/2'),
464
            'Foo' => array('Bar'),
465
        );
466
467
        $handler->addPolicyByRoleDraft($roleDraft->id, $policy);
468
        $handler->publishRoleDraft($roleDraft->id);
469
470
        $loaded = $handler->loadRole($roleDraft->id);
471
        $this->assertEquals(
472
            array(
473
                new Persistence\User\Policy(
474
                    array(
475
                        'id' => 1,
476
                        'roleId' => 1,
477
                        'module' => 'foo',
478
                        'function' => 'bar',
479
                        'limitations' => array(
480
                            'Subtree' => array('/1', '/1/2'),
481
                            'Foo' => array('Bar'),
482
                        ),
483
                        'originalId' => null,
484
                    )
485
                ),
486
            ),
487
            $loaded->policies
488
        );
489
    }
490
491
    public function testLoadRoles()
492
    {
493
        $handler = $this->getUserHandler();
494
495
        $this->assertEquals(
496
            array(),
497
            $handler->loadRoles()
498
        );
499
500
        $createStruct = new Persistence\User\RoleCreateStruct();
501
        $createStruct->identifier = 'Test';
502
503
        $roleDraft = $handler->createRole($createStruct);
504
        $handler->publishRoleDraft($roleDraft->id);
505
        $role = $handler->loadRole($roleDraft->id);
506
507
        $this->assertEquals(
508
            array($role),
509
            $handler->loadRoles()
510
        );
511
    }
512
513
    public function testUpdateRole()
514
    {
515
        $handler = $this->getUserHandler();
516
517
        $createStruct = new Persistence\User\RoleCreateStruct();
518
        $createStruct->identifier = 'Test';
519
520
        $roleDraft = $handler->createRole($createStruct);
521
        $handler->publishRoleDraft($roleDraft->id);
522
        $role = $handler->loadRole($roleDraft->id);
523
524
        $update = new Persistence\User\RoleUpdateStruct();
525
        $update->id = $role->id;
526
        $update->identifier = 'Changed';
527
528
        $handler->updateRole($update);
529
530
        $this->assertQueryResult(
531
            array(array(1, 'Changed')),
532
            $this->handler->createSelectQuery()->select('id', 'name')->from('ezrole'),
533
            'Expected a changed role.'
534
        );
535
    }
536
537
    public function testDeleteRole()
538
    {
539
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
540
        $handler = $this->getUserHandler();
541
542
        // 3 is the ID of Editor role
543
        $handler->deleteRole(3);
544
545
        $this->assertQueryResult(
546
            array(),
547
            $this->handler->createSelectQuery()->select('id')->from('ezrole')->where('id = 3'),
548
            'Expected an empty set.'
549
        );
550
551
        $this->assertQueryResult(
552
            array(),
553
            $this->handler->createSelectQuery()->select('role_id')->from('ezpolicy')->where('role_id = 3'),
554
            'Expected an empty set.'
555
        );
556
557
        $this->assertQueryResult(
558
            array(),
559
            $this->handler->createSelectQuery()->select('role_id')->from('ezuser_role')->where('role_id = 3'),
560
            'Expected an empty set.'
561
        );
562
    }
563
564
    public function testDeleteRoleDraft()
565
    {
566
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
567
        $handler = $this->getUserHandler();
568
569
        // 3 is the ID of Editor role
570
        $roleDraft = $handler->createRoleDraft(3);
571
        $handler->deleteRole($roleDraft->id, APIRole::STATUS_DRAFT);
572
573
        $this->assertQueryResult(
574
            [['3', APIRole::STATUS_DEFINED]],
575
            $this->handler->createSelectQuery()->select('id, version')->from('ezrole')->where('id = 3'),
576
            'Expected a published role.'
577
        );
578
579
        $this->assertQueryResult(
580
            [[implode("\n", array_fill(0, 28, '3, ' . APIRole::STATUS_DEFINED))]],
581
            $this->handler->createSelectQuery()->select('role_id, original_id')->from('ezpolicy')->where('role_id = 3'),
582
            'Expected 28 policies for the published role.'
583
        );
584
585
        $this->assertQueryResult(
586
            [[3], [3]],
587
            $this->handler->createSelectQuery()->select('role_id')->from('ezuser_role')->where('role_id = 3'),
588
            'Expected that role assignments still exist.'
589
        );
590
    }
591
592
    public function testAddPolicyToRoleLimitations()
593
    {
594
        $handler = $this->getUserHandler();
595
596
        $createStruct = new Persistence\User\RoleCreateStruct();
597
        $createStruct->identifier = 'Test';
598
599
        $roleDraft = $handler->createRole($createStruct);
600
        $handler->publishRoleDraft($roleDraft->id);
601
        $role = $handler->loadRole($roleDraft->id);
602
603
        $policy = new Persistence\User\Policy();
604
        $policy->module = 'foo';
605
        $policy->function = 'bar';
606
607
        $handler->addPolicy($role->id, $policy);
608
609
        $this->assertQueryResult(
610
            array(array(1, 'foo', 'bar', 1)),
611
            $this->handler->createSelectQuery()->select('id', 'module_name', 'function_name', 'role_id')->from('ezpolicy'),
612
            'Expected a new policy.'
613
        );
614
    }
615
616
    public function testAddPolicyPolicyId()
617
    {
618
        $handler = $this->getUserHandler();
619
620
        $createStruct = new Persistence\User\RoleCreateStruct();
621
        $createStruct->identifier = 'Test';
622
623
        $roleDraft = $handler->createRole($createStruct);
624
        $handler->publishRoleDraft($roleDraft->id);
625
        $role = $handler->loadRole($roleDraft->id);
626
627
        $policy = new Persistence\User\Policy();
628
        $policy->module = 'foo';
629
        $policy->function = 'bar';
630
631
        $policy = $handler->addPolicy($role->id, $policy);
632
633
        $this->assertEquals(1, $policy->id);
634
    }
635
636
    public function testAddPolicyLimitations()
637
    {
638
        $handler = $this->getUserHandler();
639
640
        $createStruct = new Persistence\User\RoleCreateStruct();
641
        $createStruct->identifier = 'Test';
642
643
        $roleDraft = $handler->createRole($createStruct);
644
        $handler->publishRoleDraft($roleDraft->id);
645
        $role = $handler->loadRole($roleDraft->id);
646
647
        $policy = new Persistence\User\Policy();
648
        $policy->module = 'foo';
649
        $policy->function = 'bar';
650
        $policy->limitations = array(
651
            'Subtree' => array('/1', '/1/2'),
652
            'Foo' => array('Bar'),
653
        );
654
655
        $handler->addPolicy($role->id, $policy);
656
657
        $this->assertQueryResult(
658
            array(
659
                array(1, 'Subtree', 1),
660
                array(2, 'Foo', 1),
661
            ),
662
            $this->handler->createSelectQuery()->select('id', 'identifier', 'policy_id')->from('ezpolicy_limitation'),
663
            'Expected a new policy.'
664
        );
665
    }
666
667
    public function testAddPolicyLimitationValues()
668
    {
669
        $handler = $this->getUserHandler();
670
671
        $createStruct = new Persistence\User\RoleCreateStruct();
672
        $createStruct->identifier = 'Test';
673
674
        $roleDraft = $handler->createRole($createStruct);
675
        $handler->publishRoleDraft($roleDraft->id);
676
        $role = $handler->loadRole($roleDraft->id);
677
678
        $policy = new Persistence\User\Policy();
679
        $policy->module = 'foo';
680
        $policy->function = 'bar';
681
        $policy->limitations = array(
682
            'Subtree' => array('/1', '/1/2'),
683
            'Foo' => array('Bar'),
684
        );
685
686
        $handler->addPolicy($role->id, $policy);
687
688
        $this->assertQueryResult(
689
            array(
690
                array(1, '/1', 1),
691
                array(2, '/1/2', 1),
692
                array(3, 'Bar', 2),
693
            ),
694
            $this->handler->createSelectQuery()->select('id', 'value', 'limitation_id')->from('ezpolicy_limitation_value'),
695
            'Expected a new policy.'
696
        );
697
    }
698
699
    protected function createRole()
700
    {
701
        $handler = $this->getUserHandler();
702
703
        $policy1 = new Persistence\User\Policy();
704
        $policy1->module = 'foo';
705
        $policy1->function = 'bar';
706
        $policy1->limitations = array(
707
            'Subtree' => array('/1', '/1/2'),
708
            'Foo' => array('Bar'),
709
        );
710
711
        $policy2 = new Persistence\User\Policy();
712
        $policy2->module = 'foo';
713
        $policy2->function = 'blubb';
714
        $policy2->limitations = array(
715
            'Foo' => array('Blubb'),
716
        );
717
718
        $createStruct = new Persistence\User\RoleCreateStruct();
719
        $createStruct->identifier = 'Test';
720
        $createStruct->policies = array($policy1, $policy2);
721
722
        return $handler->createRole($createStruct);
723
    }
724
725
    public function testImplicitlyCreatePolicies()
726
    {
727
        $this->createRole();
728
729
        $this->assertQueryResult(
730
            array(
731
                array(1, 'foo', 'bar', 1),
732
                array(2, 'foo', 'blubb', 1),
733
            ),
734
            $this->handler->createSelectQuery()->select('id', 'module_name', 'function_name', 'role_id')->from('ezpolicy'),
735
            'Expected a new policy.'
736
        );
737
    }
738
739
    public function testDeletePolicy()
740
    {
741
        $handler = $this->getUserHandler();
742
743
        $roleDraft = $this->createRole();
744
        $handler->publishRoleDraft($roleDraft->id);
745
        $handler->deletePolicy($roleDraft->policies[0]->id, $roleDraft->policies[0]->roleId);
746
747
        $this->assertQueryResult(
748
            array(
749
                array(2, 'foo', 'blubb', 1),
750
            ),
751
            $this->handler->createSelectQuery()->select('id', 'module_name', 'function_name', 'role_id')->from('ezpolicy')->where('original_id = 0'),
752
            'Expected a new policy.'
753
        );
754
    }
755
756 View Code Duplication
    public function testDeletePolicyLimitations()
757
    {
758
        $handler = $this->getUserHandler();
759
760
        $roleDraft = $this->createRole();
761
        $handler->deletePolicy($roleDraft->policies[0]->id, $roleDraft->policies[0]->roleId);
762
763
        $this->assertQueryResult(
764
            array(array(3, 'Foo', 2)),
765
            $this->handler->createSelectQuery()->select('*')->from('ezpolicy_limitation')
766
        );
767
    }
768
769 View Code Duplication
    public function testDeletePolicyLimitationValues()
770
    {
771
        $handler = $this->getUserHandler();
772
773
        $roleDraft = $this->createRole();
774
        $handler->deletePolicy($roleDraft->policies[0]->id, $roleDraft->policies[0]->roleId);
775
776
        $this->assertQueryResult(
777
            array(array(4, 3, 'Blubb')),
778
            $this->handler->createSelectQuery()->select('*')->from('ezpolicy_limitation_value')
779
        );
780
    }
781
782
    public function testUpdatePolicies()
783
    {
784
        $handler = $this->getUserHandler();
785
786
        $roleDraft = $this->createRole();
787
788
        $policy = $roleDraft->policies[0];
789
        $policy->limitations = array(
790
            'new' => array('something'),
791
        );
792
793
        $handler->updatePolicy($policy);
794
795
        $this->assertQueryResult(
796
            array(
797
                array(3, 'Foo', 2),
798
                array(4, 'new', 1),
799
            ),
800
            $this->handler->createSelectQuery()->select('*')->from('ezpolicy_limitation')
801
        );
802
803
        $this->assertQueryResult(
804
            array(
805
                array(4, 3, 'Blubb'),
806
                array(5, 4, 'something'),
807
            ),
808
            $this->handler->createSelectQuery()->select('*')->from('ezpolicy_limitation_value')
809
        );
810
    }
811
812
    public function testAddRoleToUser()
813
    {
814
        $handler = $this->getUserHandler();
815
816
        $roleDraft = $this->createRole();
817
        $handler->publishRoleDraft($roleDraft->id);
818
        $role = $handler->loadRole($roleDraft->id);
819
        $handler->create($user = $this->getValidUser());
820
821
        $handler->assignRole($user->id, $role->id, array());
822
823
        $this->assertQueryResult(
824
            array(
825
                array(1, 42, 1, null, null),
826
            ),
827
            $this->handler->createSelectQuery()->select('id', 'contentobject_id', 'role_id', 'limit_identifier', 'limit_value')->from('ezuser_role'),
828
            'Expected a new user policy association.'
829
        );
830
    }
831
832
    public function testAddRoleToUserWithLimitation()
833
    {
834
        $handler = $this->getUserHandler();
835
836
        $roleDraft = $this->createRole();
837
        $handler->publishRoleDraft($roleDraft->id);
838
        $role = $handler->loadRole($roleDraft->id);
839
        $handler->create($user = $this->getValidUser());
840
841
        $handler->assignRole(
842
            $user->id,
843
            $role->id,
844
            array(
845
                'Subtree' => array('/1'),
846
            )
847
        );
848
849
        $this->assertQueryResult(
850
            array(
851
                array(1, 42, 1, 'Subtree', '/1'),
852
            ),
853
            $this->handler->createSelectQuery()->select('id', 'contentobject_id', 'role_id', 'limit_identifier', 'limit_value')->from('ezuser_role'),
854
            'Expected a new user policy association.'
855
        );
856
    }
857
858
    public function testAddRoleToUserWithComplexLimitation()
859
    {
860
        $handler = $this->getUserHandler();
861
862
        $roleDraft = $this->createRole();
863
        $handler->publishRoleDraft($roleDraft->id);
864
        $role = $handler->loadRole($roleDraft->id);
865
        $handler->create($user = $this->getValidUser());
866
867
        $handler->assignRole(
868
            $user->id,
869
            $role->id,
870
            array(
871
                'Subtree' => array('/1', '/1/2'),
872
                'Foo' => array('Bar'),
873
            )
874
        );
875
876
        $this->assertQueryResult(
877
            array(
878
                array(1, 42, 1, 'Subtree', '/1'),
879
                array(2, 42, 1, 'Subtree', '/1/2'),
880
                array(3, 42, 1, 'Foo', 'Bar'),
881
            ),
882
            $this->handler->createSelectQuery()->select('id', 'contentobject_id', 'role_id', 'limit_identifier', 'limit_value')->from('ezuser_role'),
883
            'Expected a new user policy association.'
884
        );
885
    }
886
887
    public function testRemoveUserRoleAssociation()
888
    {
889
        $handler = $this->getUserHandler();
890
891
        $roleDraft = $this->createRole();
892
        $handler->publishRoleDraft($roleDraft->id);
893
        $role = $handler->loadRole($roleDraft->id);
894
        $handler->create($user = $this->getValidUser());
895
896
        $handler->assignRole(
897
            $user->id,
898
            $role->id,
899
            array(
900
                'Subtree' => array('/1', '/1/2'),
901
                'Foo' => array('Bar'),
902
            )
903
        );
904
905
        $handler->unassignRole($user->id, $role->id);
906
907
        $this->assertQueryResult(
908
            array(),
909
            $this->handler->createSelectQuery()->select('id', 'contentobject_id', 'role_id', 'limit_identifier', 'limit_value')->from('ezuser_role'),
910
            'Expected no user policy associations.'
911
        );
912
    }
913
914
    public function testLoadPoliciesForUser()
915
    {
916
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
917
        $handler = $this->getUserHandler();
918
919
        $policies = $handler->loadPoliciesByUserId(10); // Anonymous user
920
921
        // Verify, that we received an array of Policy objects
922
        $this->assertTrue(
923
            array_reduce(
924
                array_map(
925
                    function ($policy) {
926
                        return $policy instanceof Persistence\User\Policy;
927
                    },
928
                    $policies
929
                ),
930
                function ($a, $b) {
931
                    return $a && $b;
932
                },
933
                true
934
            )
935
        );
936
        $this->assertEquals(8, count($policies));
937
    }
938
939
    public function testLoadRoleAssignmentsByGroupId()
940
    {
941
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
942
        $handler = $this->getUserHandler();
943
944
        $this->assertEquals(
945
            array(
946
                new Persistence\User\RoleAssignment(
947
                    array(
948
                        'id' => 28,
949
                        'roleId' => 1,
950
                        'contentId' => 11,
951
                    )
952
                ),
953
                new Persistence\User\RoleAssignment(
954
                    array(
955
                        'id' => 34,
956
                        'roleId' => 5,
957
                        'contentId' => 11,
958
                    )
959
                ),
960
            ),
961
            $handler->loadRoleAssignmentsByGroupId(11)// 11: Members
962
        );
963
964
        $this->assertEquals(
965
            array(
966
                new Persistence\User\RoleAssignment(
967
                    array(
968
                        'id' => 31,
969
                        'roleId' => 1,
970
                        'contentId' => 42,
971
                    )
972
                ),
973
            ),
974
            $handler->loadRoleAssignmentsByGroupId(42)// 42: Anonymous Users
975
        );
976
977
        $this->assertEquals(
978
            array(),
979
            $handler->loadRoleAssignmentsByGroupId(10)// 10: Anonymous User
980
        );
981
    }
982
983
    public function testLoadRoleAssignmentsByGroupIdInherited()
984
    {
985
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
986
        $handler = $this->getUserHandler();
987
988
        $this->assertEquals(
989
            array(
990
                new Persistence\User\RoleAssignment(
991
                    array(
992
                        'id' => 31,
993
                        'roleId' => 1,
994
                        'contentId' => 42,
995
                    )
996
                ),
997
            ),
998
            $handler->loadRoleAssignmentsByGroupId(10, true)// 10: Anonymous User
999
        );
1000
    }
1001
1002
    public function testLoadComplexRoleAssignments()
1003
    {
1004
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
1005
        $handler = $this->getUserHandler();
1006
1007
        $this->assertEquals(
1008
            array(
1009
                new Persistence\User\RoleAssignment(
1010
                    array(
1011
                        'id' => 32,
1012
                        'roleId' => 3,
1013
                        'contentId' => 13,
1014
                        'limitationIdentifier' => 'Subtree',
1015
                        'values' => array('/1/2/'),
1016
                    )
1017
                ),
1018
                new Persistence\User\RoleAssignment(
1019
                    array(
1020
                        'id' => 33,
1021
                        'roleId' => 3,
1022
                        'contentId' => 13,
1023
                        'limitationIdentifier' => 'Subtree',
1024
                        'values' => array('/1/43/'),
1025
                    )
1026
                ),
1027
                new Persistence\User\RoleAssignment(
1028
                    array(
1029
                        'id' => 38,
1030
                        'roleId' => 5,
1031
                        'contentId' => 13,
1032
                    )
1033
                ),
1034
            ),
1035
            $handler->loadRoleAssignmentsByGroupId(13)
1036
        );
1037
1038
        $this->assertEquals(
1039
            array(
1040
                new Persistence\User\RoleAssignment(
1041
                    array(
1042
                        'id' => 32,
1043
                        'roleId' => 3,
1044
                        'contentId' => 13,
1045
                        'limitationIdentifier' => 'Subtree',
1046
                        'values' => array('/1/2/'),
1047
                    )
1048
                ),
1049
                new Persistence\User\RoleAssignment(
1050
                    array(
1051
                        'id' => 33,
1052
                        'roleId' => 3,
1053
                        'contentId' => 13,
1054
                        'limitationIdentifier' => 'Subtree',
1055
                        'values' => array('/1/43/'),
1056
                    )
1057
                ),
1058
                new Persistence\User\RoleAssignment(
1059
                    array(
1060
                        'id' => 38,
1061
                        'roleId' => 5,
1062
                        'contentId' => 13,
1063
                    )
1064
                ),
1065
            ),
1066
            $handler->loadRoleAssignmentsByGroupId(13, true)
1067
        );
1068
    }
1069
1070
    public function testLoadRoleAssignmentsByRoleId()
1071
    {
1072
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
1073
        $handler = $this->getUserHandler();
1074
1075
        $this->assertEquals(
1076
            array(
1077
                new Persistence\User\RoleAssignment(
1078
                    array(
1079
                        'id' => 28,
1080
                        'roleId' => 1,
1081
                        'contentId' => 11,
1082
                    )
1083
                ),
1084
                new Persistence\User\RoleAssignment(
1085
                    array(
1086
                        'id' => 31,
1087
                        'roleId' => 1,
1088
                        'contentId' => 42,
1089
                    )
1090
                ),
1091
                new Persistence\User\RoleAssignment(
1092
                    array(
1093
                        'id' => 37,
1094
                        'roleId' => 1,
1095
                        'contentId' => 59,
1096
                    )
1097
                ),
1098
            ),
1099
            $handler->loadRoleAssignmentsByRoleId(1)
1100
        );
1101
    }
1102
1103
    public function testLoadRoleDraftByRoleId()
1104
    {
1105
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
1106
        $handler = $this->getUserHandler();
1107
1108
        // 3 is the ID of Editor role
1109
        $originalRoleId = 3;
1110
        $draft = $handler->createRoleDraft($originalRoleId);
1111
        $loadedDraft = $handler->loadRoleDraftByRoleId($originalRoleId);
1112
        self::assertSame($loadedDraft->originalId, $originalRoleId);
1113
        self::assertEquals($draft, $loadedDraft);
1114
    }
1115
1116
    public function testRoleDraftOnlyHavePolicyDraft()
1117
    {
1118
        $this->insertDatabaseFixture(__DIR__ . '/../../../../Repository/Tests/Service/Integration/Legacy/_fixtures/clean_ezdemo_47_dump.php');
1119
        $handler = $this->getUserHandler();
1120
        $originalRoleId = 3;
1121
        $originalRole = $handler->loadRole($originalRoleId);
1122
        $originalPolicies = [];
1123
        foreach ($originalRole->policies as $policy) {
1124
            $originalPolicies[$policy->id] = $policy;
1125
        }
1126
1127
        $draft = $handler->createRoleDraft($originalRoleId);
1128
        $loadedDraft = $handler->loadRole($draft->id, Persistence\User\Role::STATUS_DRAFT);
1129
        self::assertSame($loadedDraft->originalId, $originalRoleId);
1130
        self::assertEquals($draft, $loadedDraft);
1131
        foreach ($loadedDraft->policies as $policy) {
1132
            self::assertTrue(isset($originalPolicies[$policy->originalId]));
1133
        }
1134
1135
        // Now add a new policy. Original ID of the new one must be the same as its actual ID.
1136
        $newPolicyModule = 'foo';
1137
        $newPolicyFunction = 'bar';
1138
        $policy = new Persistence\User\Policy(['module' => $newPolicyModule, 'function' => $newPolicyFunction]);
1139
        $policyDraft = $handler->addPolicyByRoleDraft($loadedDraft->id, $policy);
1140
1141
        // Test again by reloading the draft.
1142
        $loadedDraft = $handler->loadRole($draft->id, Persistence\User\Role::STATUS_DRAFT);
1143
        foreach ($loadedDraft->policies as $policy) {
1144
            if ($policy->id != $policyDraft->id) {
1145
                continue;
1146
            }
1147
1148
            self::assertNotNull($policy->originalId);
1149
            self::assertSame($policy->id, $policy->originalId);
1150
        }
1151
    }
1152
}
1153