Completed
Push — ezp30878_cant_add_image_with_p... ( e19ea7...263f1b )
by
unknown
20:16
created

UserHandlerTest::testUpdatePolicies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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