Completed
Push — EZP-31383-roles-copying ( d0932a )
by
unknown
12:44
created

testUnassignRoleByAssignmentThrowsNotFoundException()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 4
nop 0
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RoleServiceTest 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\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Values\User\Limitation;
12
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
13
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
14
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
15
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation;
16
use eZ\Publish\API\Repository\Values\User\Policy;
17
use eZ\Publish\API\Repository\Values\User\Role;
18
use eZ\Publish\API\Repository\Values\User\RoleCopyStruct;
19
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
20
use Exception;
21
22
/**
23
 * Test case for operations in the RoleService using in memory storage.
24
 *
25
 * The following IDs from the default eZ community edition database are used in
26
 * this test:
27
 *
28
 * <ul>
29
 *   <li>
30
 *     ContentType
31
 *     <ul>
32
 *       <li><strong>28</strong>: File</li>
33
 *       <li><strong>29</strong>: Flash</li>
34
 *       <li><strong>30</strong>: Image</li>
35
 *     </ul>
36
 *   </li>
37
 * <ul>
38
 *
39
 * @see eZ\Publish\API\Repository\RoleService
40
 * @group role
41
 */
42
class RoleServiceTest extends BaseTest
43
{
44
    /**
45
     * Test for the newRoleCreateStruct() method.
46
     *
47
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
48
     */
49
    public function testNewRoleCreateStruct()
50
    {
51
        $repository = $this->getRepository();
52
53
        $roleService = $repository->getRoleService();
54
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
55
56
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleCreateStruct', $roleCreate);
57
    }
58
59
    /**
60
     * Test for the newRoleCopyStruct() method.
61
     *
62
     * @covers \eZ\Publish\API\Repository\RoleService::newRoleCopyStruct
63
     */
64
    public function testNewRoleCopyStruct()
65
    {
66
        $repository = $this->getRepository();
67
68
        /* BEGIN: Use Case */
69
        $roleService = $repository->getRoleService();
70
        $roleCopy = $roleService->newRoleCopyStruct('copiedRole');
71
        /* END: Use Case */
72
73
        $this->assertInstanceOf(RoleCopyStruct::class, $roleCopy);
74
    }
75
76
    /**
77
     * Test for the newRoleCreateStruct() method.
78
     *
79
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
80
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
81
     */
82
    public function testNewRoleCreateStructSetsNamePropertyOnStruct()
83
    {
84
        $repository = $this->getRepository();
85
86
        /* BEGIN: Use Case */
87
88
        $roleService = $repository->getRoleService();
89
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
90
91
        /* END: Use Case */
92
93
        $this->assertEquals('roleName', $roleCreate->identifier);
94
    }
95
96
    /**
97
     * Test for the createRole() method.
98
     *
99
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
100
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
101
     */
102
    public function testCreateRole()
103
    {
104
        $repository = $this->getRepository();
105
106
        /* BEGIN: Use Case */
107
108
        $roleService = $repository->getRoleService();
109
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
110
111
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
112
        // $roleCreate->mainLanguageCode = 'eng-US';
113
114
        $role = $roleService->createRole($roleCreate);
115
116
        /* END: Use Case */
117
118
        $this->assertInstanceOf(
119
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
120
            $role
121
        );
122
123
        return [
124
            'createStruct' => $roleCreate,
125
            'role' => $role,
126
        ];
127
    }
128
129
    /**
130
     * Test for the createRole() method.
131
     *
132
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
133
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
134
     */
135
    public function testRoleCreateStructValues(array $data)
136
    {
137
        $createStruct = $data['createStruct'];
138
        $role = $data['role'];
139
140
        $this->assertEquals(
141
            [
142
                'identifier' => $createStruct->identifier,
143
                'policies' => $createStruct->policies,
144
            ],
145
            [
146
                'identifier' => $role->identifier,
147
                'policies' => $role->policies,
148
            ]
149
        );
150
        $this->assertNotNull($role->id);
151
152
        return $data;
153
    }
154
155
    /**
156
     * Test for the createRole() method.
157
     *
158
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
159
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
160
     */
161
    public function testCreateRoleWithPolicy()
162
    {
163
        $repository = $this->getRepository();
164
165
        /* BEGIN: Use Case */
166
167
        $roleService = $repository->getRoleService();
168
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
169
170
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
171
        // $roleCreate->mainLanguageCode = 'eng-US';
172
173
        // Create new subtree limitation
174
        $limitation = new SubtreeLimitation(
175
            [
176
                'limitationValues' => ['/1/2/'],
177
            ]
178
        );
179
180
        // Create policy create struct and add limitation to it
181
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'read');
182
        $policyCreate->addLimitation($limitation);
183
184
        // Add policy create struct to role create struct
185
        $roleCreate->addPolicy($policyCreate);
186
187
        $role = $roleService->createRole($roleCreate);
188
189
        /* END: Use Case */
190
191
        $this->assertInstanceOf(
192
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
193
            $role
194
        );
195
196
        return [
197
            'createStruct' => $roleCreate,
198
            'role' => $role,
199
        ];
200
    }
201
202
    /**
203
     * Test for the createRole() method.
204
     *
205
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
206
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithPolicy
207
     */
208
    public function testRoleCreateStructValuesWithPolicy(array $data)
209
    {
210
        $createStruct = $data['createStruct'];
211
        $role = $data['role'];
212
213
        $this->assertEquals(
214
            [
215
                'identifier' => $createStruct->identifier,
216
                'policy_module' => $createStruct->policies[0]->module,
217
                'policy_function' => $createStruct->policies[0]->function,
218
                'policy_limitation' => array_values($createStruct->policies[0]->limitations),
219
            ],
220
            [
221
                'identifier' => $role->identifier,
222
                'policy_module' => $role->policies[0]->module,
223
                'policy_function' => $role->policies[0]->function,
224
                'policy_limitation' => array_values($role->policies[0]->limitations),
225
            ]
226
        );
227
        $this->assertNotNull($role->id);
228
229
        return $data;
230
    }
231
232
    /**
233
     * Test creating a role with multiple policies.
234
     *
235
     * @covers \eZ\Publish\API\Repository\RoleService::createRole
236
     */
237
    public function testCreateRoleWithMultiplePolicies()
238
    {
239
        $repository = $this->getRepository();
240
        $roleService = $repository->getRoleService();
241
242
        $limitation1 = new Limitation\ContentTypeLimitation();
243
        $limitation1->limitationValues = ['1', '3', '13'];
244
245
        $limitation2 = new Limitation\SectionLimitation();
246
        $limitation2->limitationValues = ['2', '3'];
247
248
        $limitation3 = new Limitation\OwnerLimitation();
249
        $limitation3->limitationValues = ['1', '2'];
250
251
        $limitation4 = new Limitation\UserGroupLimitation();
252
        $limitation4->limitationValues = ['1'];
253
254
        $policyCreateStruct1 = $roleService->newPolicyCreateStruct('content', 'read');
255
        $policyCreateStruct1->addLimitation($limitation1);
256
        $policyCreateStruct1->addLimitation($limitation2);
257
258
        $policyCreateStruct2 = $roleService->newPolicyCreateStruct('content', 'edit');
259
        $policyCreateStruct2->addLimitation($limitation3);
260
        $policyCreateStruct2->addLimitation($limitation4);
261
262
        $roleCreateStruct = $roleService->newRoleCreateStruct('ultimate_permissions');
263
        $roleCreateStruct->addPolicy($policyCreateStruct1);
264
        $roleCreateStruct->addPolicy($policyCreateStruct2);
265
266
        $createdRole = $roleService->createRole($roleCreateStruct);
267
268
        self::assertInstanceOf(Role::class, $createdRole);
269
        self::assertGreaterThan(0, $createdRole->id);
270
271
        $this->assertPropertiesCorrect(
272
            [
273
                'identifier' => $roleCreateStruct->identifier,
274
            ],
275
            $createdRole
276
        );
277
278
        self::assertCount(2, $createdRole->getPolicies());
279
280
        foreach ($createdRole->getPolicies() as $policy) {
281
            self::assertInstanceOf(Policy::class, $policy);
282
            self::assertGreaterThan(0, $policy->id);
283
            self::assertEquals($createdRole->id, $policy->roleId);
284
285
            self::assertCount(2, $policy->getLimitations());
286
287
            foreach ($policy->getLimitations() as $limitation) {
288
                self::assertInstanceOf(Limitation::class, $limitation);
289
290
                if ($policy->module == 'content' && $policy->function == 'read') {
291 View Code Duplication
                    switch ($limitation->getIdentifier()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
292
                        case Limitation::CONTENTTYPE:
293
                            self::assertEquals($limitation1->limitationValues, $limitation->limitationValues);
294
                            break;
295
296
                        case Limitation::SECTION:
297
                            self::assertEquals($limitation2->limitationValues, $limitation->limitationValues);
298
                            break;
299
300
                        default:
301
                            self::fail('Created role contains limitations not defined with create struct');
302
                    }
303
                } elseif ($policy->module == 'content' && $policy->function == 'edit') {
304 View Code Duplication
                    switch ($limitation->getIdentifier()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
305
                        case Limitation::OWNER:
306
                            self::assertEquals($limitation3->limitationValues, $limitation->limitationValues);
307
                            break;
308
309
                        case Limitation::USERGROUP:
310
                            self::assertEquals($limitation4->limitationValues, $limitation->limitationValues);
311
                            break;
312
313
                        default:
314
                            self::fail('Created role contains limitations not defined with create struct');
315
                    }
316
                } else {
317
                    self::fail('Created role contains policy not defined with create struct');
318
                }
319
            }
320
        }
321
    }
322
323
    /**
324
     * Test for the createRoleDraft() method.
325
     *
326
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
327
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
328
     */
329 View Code Duplication
    public function testCreateRoleDraft()
330
    {
331
        $repository = $this->getRepository();
332
333
        /* BEGIN: Use Case */
334
335
        $roleService = $repository->getRoleService();
336
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
337
338
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
339
        // $roleCreate->mainLanguageCode = 'eng-US';
340
341
        $roleDraft = $roleService->createRole($roleCreate);
342
        $roleService->publishRoleDraft($roleDraft);
343
        $role = $roleService->loadRole($roleDraft->id);
344
        $newRoleDraft = $roleService->createRoleDraft($role);
345
346
        /* END: Use Case */
347
348
        $this->assertInstanceOf(
349
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
350
            $newRoleDraft
351
        );
352
    }
353
354
    /**
355
     * Test for the createRole() method.
356
     *
357
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
358
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
359
     */
360
    public function testCreateRoleThrowsInvalidArgumentException()
361
    {
362
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
363
364
        $repository = $this->getRepository();
365
366
        /* BEGIN: Use Case */
367
368
        $roleService = $repository->getRoleService();
369
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
370
371
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
372
        // $roleCreate->mainLanguageCode = 'eng-US';
373
374
        // This call will fail with an InvalidArgumentException, because Editor exists
375
        $roleService->createRole($roleCreate);
376
377
        /* END: Use Case */
378
    }
379
380
    /**
381
     * Test for the createRoleDraft() method.
382
     *
383
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
384
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
385
     */
386 View Code Duplication
    public function testCreateRoleDraftThrowsInvalidArgumentException()
387
    {
388
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
389
390
        $repository = $this->getRepository();
391
392
        /* BEGIN: Use Case */
393
394
        $roleService = $repository->getRoleService();
395
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
396
397
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
398
        // $roleCreate->mainLanguageCode = 'eng-US';
399
400
        $roleDraft = $roleService->createRole($roleCreate);
401
        $roleService->publishRoleDraft($roleDraft);
402
        $role = $roleService->loadRole($roleDraft->id);
403
        $roleService->createRoleDraft($role); // First role draft
404
405
        // This call will fail with an InvalidArgumentException, because there is already a draft
406
        $roleService->createRoleDraft($role);
407
408
        /* END: Use Case */
409
    }
410
411
    /**
412
     * Test for the createRole() method.
413
     *
414
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
415
     */
416 View Code Duplication
    public function testCreateRoleThrowsLimitationValidationException()
417
    {
418
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
419
420
        $repository = $this->getRepository();
421
422
        /* BEGIN: Use Case */
423
        $roleService = $repository->getRoleService();
424
425
        // Create new role create struct
426
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
427
428
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
429
        // $roleCreate->mainLanguageCode = 'eng-US';
430
431
        // Create new subtree limitation
432
        $limitation = new SubtreeLimitation(
433
            [
434
                'limitationValues' => ['/mountain/forest/tree/42/'],
435
            ]
436
        );
437
438
        // Create policy create struct and add limitation to it
439
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
440
        $policyCreate->addLimitation($limitation);
441
442
        // Add policy create struct to role create struct
443
        $roleCreate->addPolicy($policyCreate);
444
445
        // This call will fail with an LimitationValidationException, because subtree
446
        // "/mountain/forest/tree/42/" does not exist
447
        $roleService->createRole($roleCreate);
448
        /* END: Use Case */
449
    }
450
451
    /**
452
     * Test for the createRole() method.
453
     *
454
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
455
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
456
     */
457 View Code Duplication
    public function testCreateRoleInTransactionWithRollback()
458
    {
459
        $repository = $this->getRepository();
460
461
        /* BEGIN: Use Case */
462
463
        $roleService = $repository->getRoleService();
464
465
        $repository->beginTransaction();
466
467
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
468
469
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
470
        // $roleCreate->mainLanguageCode = 'eng-US';
471
472
        $createdRoleId = $roleService->createRole($roleCreate)->id;
473
474
        $repository->rollback();
475
476
        try {
477
            // This call will fail with a "NotFoundException"
478
            $role = $roleService->loadRole($createdRoleId);
0 ignored issues
show
Unused Code introduced by
$role is not used, you could remove the assignment.

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

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

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

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

Loading history...
479
        } catch (NotFoundException $e) {
480
            return;
481
        }
482
        /* END: Use Case */
483
484
        $this->fail('Role object still exists after rollback.');
485
    }
486
487
    /**
488
     * Test for the createRoleDraft() method.
489
     *
490
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
491
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
492
     */
493 View Code Duplication
    public function testCreateRoleDraftInTransactionWithRollback()
494
    {
495
        $repository = $this->getRepository();
496
497
        /* BEGIN: Use Case */
498
499
        $roleService = $repository->getRoleService();
500
501
        $repository->beginTransaction();
502
503
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
504
505
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
506
        // $roleCreate->mainLanguageCode = 'eng-US';
507
508
        $createdRoleId = $roleService->createRole($roleCreate)->id;
509
510
        $repository->rollback();
511
512
        try {
513
            // This call will fail with a "NotFoundException"
514
            $role = $roleService->loadRoleDraft($createdRoleId);
0 ignored issues
show
Unused Code introduced by
$role is not used, you could remove the assignment.

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

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

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

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

Loading history...
515
        } catch (NotFoundException $e) {
516
            return;
517
        }
518
        /* END: Use Case */
519
520
        $this->fail('Role draft object still exists after rollback.');
521
    }
522
523
    /**
524
     * Test for the copyRole() method.
525
     *
526
     * @see \eZ\Publish\API\Repository\RoleService::copyRole()
527
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCopyStruct
528
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
529
     */
530
    public function testCopyRole()
531
    {
532
        $repository = $this->getRepository();
533
534
        /* BEGIN: Use Case */
535
        $roleService = $repository->getRoleService();
536
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
537
538
        $roleDraft = $roleService->createRole($roleCreate);
539
540
        $roleService->publishRoleDraft($roleDraft);
541
        $role = $roleService->loadRole($roleDraft->id);
542
543
        $roleCopy = $roleService->newRoleCopyStruct('copiedRole');
544
545
        $copiedRole = $roleService->copyRole($role, $roleCopy);
546
        /* END: Use Case */
547
548
        // Now verify that our change was saved
549
        $role = $roleService->loadRoleByIdentifier('copiedRole');
550
551
        $this->assertEquals($role->id, $copiedRole->id);
552
    }
553
554
    /**
555
     * Test for the copyRole() method with added policies.
556
     *
557
     * @see \eZ\Publish\API\Repository\RoleService::copyRole()
558
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCopyStruct
559
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
560
     */
561
    public function testCopyRoleWithPolicies()
562
    {
563
        $repository = $this->getRepository();
564
565
        /* BEGIN: Use Case */
566
        $roleService = $repository->getRoleService();
567
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
568
569
        $policyCreateStruct1 = $roleService->newPolicyCreateStruct('content', 'read');
570
        $policyCreateStruct2 = $roleService->newPolicyCreateStruct('content', 'edit');
571
572
        $roleCreate->addPolicy($policyCreateStruct1);
573
        $roleCreate->addPolicy($policyCreateStruct2);
574
575
        $roleDraft = $roleService->createRole($roleCreate);
576
577
        $roleService->publishRoleDraft($roleDraft);
578
        $role = $roleService->loadRole($roleDraft->id);
579
580
        $roleCopy = $roleService->newRoleCopyStruct('copiedRole');
581
582
        $copiedRole = $roleService->copyRole($role, $roleCopy);
583
        /* END: Use Case */
584
585
        // Now verify that our change was saved
586
        $role = $roleService->loadRoleByIdentifier('copiedRole');
587
588
        $this->assertEquals($role->getPolicies(), $copiedRole->getPolicies());
589
    }
590
591
    /**
592
     * Test for the copyRole() method with added policies and limitations.
593
     *
594
     * @see \eZ\Publish\API\Repository\RoleService::copyRole()
595
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCopyStruct
596
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
597
     */
598
    public function testCopyRoleWithPoliciesAndLimitations()
599
    {
600
        $repository = $this->getRepository();
601
602
        /* BEGIN: Use Case */
603
        $roleService = $repository->getRoleService();
604
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
605
606
        $policyCreateStruct1 = $roleService->newPolicyCreateStruct('content', 'read');
607
        $roleLimitations = [
608
            new SectionLimitation(['limitationValues' => [2]]),
609
            new SubtreeLimitation(['limitationValues' => ['/1/2/']]),
610
        ];
611
        foreach ($roleLimitations as $roleLimitation) {
612
            $policyCreateStruct1->addLimitation($roleLimitation);
613
        }
614
        $policyCreateStruct2 = $roleService->newPolicyCreateStruct('content', 'edit');
615
616
        $roleCreate->addPolicy($policyCreateStruct1);
617
        $roleCreate->addPolicy($policyCreateStruct2);
618
619
        $roleDraft = $roleService->createRole($roleCreate);
620
621
        $roleService->publishRoleDraft($roleDraft);
622
        $role = $roleService->loadRole($roleDraft->id);
623
624
        $roleCopy = $roleService->newRoleCopyStruct('copiedRole');
625
        $copiedRole = $roleService->copyRole($role, $roleCopy);
626
        /* END: Use Case */
627
628
        // Now verify that our change was saved
629
        $role = $roleService->loadRoleByIdentifier('copiedRole');
630
631
        $limitations = [];
632
        foreach ($role->getPolicies() as $policy) {
633
            $limitations[] = $policy->getLimitations();
634
        }
635
        $limitationsCopied = [];
636
        foreach ($copiedRole->getPolicies() as $policy) {
637
            $limitationsCopied[] = $policy->getLimitations();
638
        }
639
640
        $this->assertEquals($limitations, $limitationsCopied);
641
    }
642
643
    /**
644
     * Test for the loadRole() method.
645
     *
646
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
647
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
648
     */
649 View Code Duplication
    public function testLoadRole()
650
    {
651
        $repository = $this->getRepository();
652
653
        /* BEGIN: Use Case */
654
655
        $roleService = $repository->getRoleService();
656
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
657
658
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
659
        // $roleCreate->mainLanguageCode = 'eng-US';
660
661
        $roleDraft = $roleService->createRole($roleCreate);
662
        $roleService->publishRoleDraft($roleDraft);
663
664
        // Load the newly created role by its ID
665
        $role = $roleService->loadRole($roleDraft->id);
666
667
        /* END: Use Case */
668
669
        $this->assertEquals('roleName', $role->identifier);
670
    }
671
672
    /**
673
     * Test for the loadRoleDraft() method.
674
     *
675
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
676
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
677
     */
678
    public function testLoadRoleDraft()
679
    {
680
        $repository = $this->getRepository();
681
682
        /* BEGIN: Use Case */
683
684
        $roleService = $repository->getRoleService();
685
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
686
687
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
688
        // $roleCreate->mainLanguageCode = 'eng-US';
689
690
        $roleDraft = $roleService->createRole($roleCreate);
691
692
        // Load the newly created role by its ID
693
        $role = $roleService->loadRoleDraft($roleDraft->id);
694
695
        /* END: Use Case */
696
697
        $this->assertEquals('roleName', $role->identifier);
698
    }
699
700
    public function testLoadRoleDraftByRoleId()
701
    {
702
        $repository = $this->getRepository();
703
704
        /* BEGIN: Use Case */
705
706
        $roleService = $repository->getRoleService();
707
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
708
709
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
710
        // $roleCreate->mainLanguageCode = 'eng-US';
711
712
        $role = $roleService->createRole($roleCreate);
713
        $roleService->publishRoleDraft($role);
714
715
        // Now create a new draft based on the role
716
        $newDraft = $roleService->createRoleDraft($role);
717
        $loadedRoleDraft = $roleService->loadRoleDraftByRoleId($role->id);
718
719
        /* END: Use Case */
720
721
        self::assertEquals('roleName', $role->identifier);
722
        self::assertInstanceOf('eZ\Publish\API\Repository\Values\User\RoleDraft', $loadedRoleDraft);
723
        self::assertEquals($newDraft, $loadedRoleDraft);
724
    }
725
726
    /**
727
     * Test for the loadRole() method.
728
     *
729
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
730
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
731
     */
732
    public function testLoadRoleThrowsNotFoundException()
733
    {
734
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
735
736
        $repository = $this->getRepository();
737
738
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
739
        /* BEGIN: Use Case */
740
741
        $roleService = $repository->getRoleService();
742
743
        // This call will fail with a NotFoundException, because no such role exists.
744
        $roleService->loadRole($nonExistingRoleId);
745
746
        /* END: Use Case */
747
    }
748
749
    /**
750
     * Test for the loadRoleDraft() method.
751
     *
752
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
753
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
754
     */
755
    public function testLoadRoleDraftThrowsNotFoundException()
756
    {
757
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
758
759
        $repository = $this->getRepository();
760
761
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
762
        /* BEGIN: Use Case */
763
764
        $roleService = $repository->getRoleService();
765
766
        // This call will fail with a NotFoundException, because no such role exists.
767
        $roleService->loadRoleDraft($nonExistingRoleId);
768
769
        /* END: Use Case */
770
    }
771
772
    public function testLoadRoleDraftByRoleIdThrowsNotFoundException()
773
    {
774
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
775
776
        $repository = $this->getRepository();
777
778
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
779
        /* BEGIN: Use Case */
780
781
        $roleService = $repository->getRoleService();
782
783
        // This call will fail with a NotFoundException, because no such role exists.
784
        $roleService->loadRoleDraftByRoleId($nonExistingRoleId);
785
786
        /* END: Use Case */
787
    }
788
789
    /**
790
     * Test for the loadRoleByIdentifier() method.
791
     *
792
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
793
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
794
     */
795 View Code Duplication
    public function testLoadRoleByIdentifier()
796
    {
797
        $repository = $this->getRepository();
798
799
        /* BEGIN: Use Case */
800
801
        $roleService = $repository->getRoleService();
802
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
803
804
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
805
        // $roleCreate->mainLanguageCode = 'eng-US';
806
807
        $roleDraft = $roleService->createRole($roleCreate);
808
        $roleService->publishRoleDraft($roleDraft);
809
810
        // Load the newly created role by its identifier
811
        $role = $roleService->loadRoleByIdentifier('roleName');
812
813
        /* END: Use Case */
814
815
        $this->assertEquals('roleName', $role->identifier);
816
    }
817
818
    /**
819
     * Test for the loadRoleByIdentifier() method.
820
     *
821
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
822
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
823
     */
824
    public function testLoadRoleByIdentifierThrowsNotFoundException()
825
    {
826
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
827
828
        $repository = $this->getRepository();
829
830
        /* BEGIN: Use Case */
831
832
        $roleService = $repository->getRoleService();
833
834
        // This call will fail with a NotFoundException, because no such role exists.
835
        $roleService->loadRoleByIdentifier('MissingRole');
836
837
        /* END: Use Case */
838
    }
839
840
    /**
841
     * Test for the loadRoles() method.
842
     *
843
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
844
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
845
     */
846
    public function testLoadRoles()
847
    {
848
        $repository = $this->getRepository();
849
850
        /* BEGIN: Use Case */
851
852
        // First create a custom role
853
        $roleService = $repository->getRoleService();
854
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
855
856
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
857
        // $roleCreate->mainLanguageCode = 'eng-US';
858
859
        $roleDraft = $roleService->createRole($roleCreate);
860
        $roleService->publishRoleDraft($roleDraft);
861
862
        // Now load all available roles
863
        $roles = $roleService->loadRoles();
864
865
        foreach ($roles as $role) {
866
            if ($role->identifier === 'roleName') {
867
                break;
868
            }
869
        }
870
871
        /* END: Use Case */
872
873
        $this->assertEquals('roleName', $role->identifier);
0 ignored issues
show
Bug introduced by
The variable $role seems to be defined by a foreach iteration on line 865. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
874
    }
875
876
    /**
877
     * Test for the loadRoles() method.
878
     *
879
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
880
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
881
     */
882
    public function testLoadRolesReturnsExpectedSetOfDefaultRoles()
883
    {
884
        $repository = $this->getRepository();
885
886
        /* BEGIN: Use Case */
887
        $roleService = $repository->getRoleService();
888
889
        $roles = $roleService->loadRoles();
890
891
        $roleNames = [];
892
        foreach ($roles as $role) {
893
            $roleNames[] = $role->identifier;
894
        }
895
        /* END: Use Case */
896
897
        $this->assertEqualsCanonicalizing(
898
            [
899
                'Administrator',
900
                'Anonymous',
901
                'Editor',
902
                'Member',
903
                'Partner',
904
            ],
905
            $roleNames
906
        );
907
    }
908
909
    /**
910
     * Test for the newRoleUpdateStruct() method.
911
     *
912
     * @see \eZ\Publish\API\Repository\RoleService::newRoleUpdateStruct()
913
     */
914
    public function testNewRoleUpdateStruct()
915
    {
916
        $repository = $this->getRepository();
917
918
        /* BEGIN: Use Case */
919
        $roleService = $repository->getRoleService();
920
        $roleUpdate = $roleService->newRoleUpdateStruct('newRole');
921
        /* END: Use Case */
922
923
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleUpdateStruct', $roleUpdate);
924
    }
925
926
    /**
927
     * Test for the updateRoleDraft() method.
928
     *
929
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
930
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleUpdateStruct
931
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
932
     */
933
    public function testUpdateRoleDraft()
934
    {
935
        $repository = $this->getRepository();
936
937
        /* BEGIN: Use Case */
938
        $roleService = $repository->getRoleService();
939
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
940
941
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
942
        // $roleCreate->mainLanguageCode = 'eng-US';
943
944
        $roleDraft = $roleService->createRole($roleCreate);
945
946
        $roleUpdate = $roleService->newRoleUpdateStruct();
947
        $roleUpdate->identifier = 'updatedRole';
948
949
        $updatedRole = $roleService->updateRoleDraft($roleDraft, $roleUpdate);
950
        /* END: Use Case */
951
952
        // Now verify that our change was saved
953
        $role = $roleService->loadRoleDraft($updatedRole->id);
954
955
        $this->assertEquals($role->identifier, 'updatedRole');
956
    }
957
958
    /**
959
     * Test for the updateRoleDraft() method.
960
     *
961
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
962
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRoleDraft
963
     */
964
    public function testUpdateRoleDraftThrowsInvalidArgumentException()
965
    {
966
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
967
968
        $repository = $this->getRepository();
969
970
        /* BEGIN: Use Case */
971
        $roleService = $repository->getRoleService();
972
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
973
974
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
975
        // $roleCreate->mainLanguageCode = 'eng-US';
976
977
        $roleDraft = $roleService->createRole($roleCreate);
978
979
        $roleUpdate = $roleService->newRoleUpdateStruct();
980
        $roleUpdate->identifier = 'Editor';
981
982
        // This call will fail with an InvalidArgumentException, because Editor is a predefined role
983
        $roleService->updateRoleDraft($roleDraft, $roleUpdate);
984
        /* END: Use Case */
985
    }
986
987
    /**
988
     * Test for the deleteRole() method.
989
     *
990
     * @see \eZ\Publish\API\Repository\RoleService::deleteRole()
991
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
992
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
993
     */
994
    public function testDeleteRole()
995
    {
996
        $repository = $this->getRepository();
997
998
        /* BEGIN: Use Case */
999
        $roleService = $repository->getRoleService();
1000
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1001
1002
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1003
        // $roleCreate->mainLanguageCode = 'eng-US';
1004
1005
        $roleDraft = $roleService->createRole($roleCreate);
1006
        $roleService->publishRoleDraft($roleDraft);
1007
        $role = $roleService->loadRole($roleDraft->id);
1008
1009
        $roleService->deleteRole($role);
1010
        /* END: Use Case */
1011
1012
        $this->assertCount(5, $roleService->loadRoles());
1013
    }
1014
1015
    /**
1016
     * Test for the deleteRoleDraft() method.
1017
     *
1018
     * @see \eZ\Publish\API\Repository\RoleService::deleteRoleDraft()
1019
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
1020
     */
1021
    public function testDeleteRoleDraft()
1022
    {
1023
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
1024
1025
        $repository = $this->getRepository();
1026
1027
        /* BEGIN: Use Case */
1028
        $roleService = $repository->getRoleService();
1029
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1030
1031
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1032
        // $roleCreate->mainLanguageCode = 'eng-US';
1033
1034
        $roleDraft = $roleService->createRole($roleCreate);
1035
        $roleID = $roleDraft->id;
1036
        $roleService->deleteRoleDraft($roleDraft);
1037
1038
        // This call will fail with a NotFoundException, because the draft no longer exists
1039
        $roleService->loadRoleDraft($roleID);
1040
        /* END: Use Case */
1041
    }
1042
1043
    /**
1044
     * Test for the newPolicyCreateStruct() method.
1045
     *
1046
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
1047
     */
1048
    public function testNewPolicyCreateStruct()
1049
    {
1050
        $repository = $this->getRepository();
1051
1052
        /* BEGIN: Use Case */
1053
        $roleService = $repository->getRoleService();
1054
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1055
        /* END: Use Case */
1056
1057
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyCreateStruct', $policyCreate);
1058
    }
1059
1060
    /**
1061
     * Test for the newPolicyCreateStruct() method.
1062
     *
1063
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
1064
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1065
     */
1066
    public function testNewPolicyCreateStructSetsStructProperties()
1067
    {
1068
        $repository = $this->getRepository();
1069
1070
        /* BEGIN: Use Case */
1071
        $roleService = $repository->getRoleService();
1072
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1073
        /* END: Use Case */
1074
1075
        $this->assertEquals(
1076
            ['content', 'create'],
1077
            [$policyCreate->module, $policyCreate->function]
1078
        );
1079
    }
1080
1081
    /**
1082
     * Test for the addPolicyByRoleDraft() method.
1083
     *
1084
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1085
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
1086
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1087
     */
1088
    public function testAddPolicyByRoleDraft()
1089
    {
1090
        $repository = $this->getRepository();
1091
1092
        /* BEGIN: Use Case */
1093
        $roleService = $repository->getRoleService();
1094
1095
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1096
1097
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1098
        // $roleCreate->mainLanguageCode = 'eng-US';
1099
1100
        $roleDraft = $roleService->createRole($roleCreate);
1101
1102
        $roleDraft = $roleService->addPolicyByRoleDraft(
1103
            $roleDraft,
1104
            $roleService->newPolicyCreateStruct('content', 'delete')
1105
        );
1106
        $roleDraft = $roleService->addPolicyByRoleDraft(
1107
            $roleDraft,
1108
            $roleService->newPolicyCreateStruct('content', 'create')
1109
        );
1110
        /* END: Use Case */
1111
1112
        $actual = [];
1113 View Code Duplication
        foreach ($roleDraft->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1114
            $actual[] = [
1115
                'module' => $policy->module,
1116
                'function' => $policy->function,
1117
            ];
1118
        }
1119
        usort(
1120
            $actual,
1121
            function ($p1, $p2) {
1122
                return strcasecmp($p1['function'], $p2['function']);
1123
            }
1124
        );
1125
1126
        $this->assertEquals(
1127
            [
1128
                [
1129
                    'module' => 'content',
1130
                    'function' => 'create',
1131
                ],
1132
                [
1133
                    'module' => 'content',
1134
                    'function' => 'delete',
1135
                ],
1136
            ],
1137
            $actual
1138
        );
1139
    }
1140
1141
    /**
1142
     * Test for the addPolicyByRoleDraft() method.
1143
     *
1144
     * @return array [\eZ\Publish\API\Repository\Values\User\RoleDraft, \eZ\Publish\API\Repository\Values\User\Policy]
1145
     *
1146
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1147
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1148
     */
1149
    public function testAddPolicyByRoleDraftUpdatesRole()
1150
    {
1151
        $repository = $this->getRepository();
1152
1153
        /* BEGIN: Use Case */
1154
        $roleService = $repository->getRoleService();
1155
1156
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1157
1158
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1159
        // $roleCreate->mainLanguageCode = 'eng-US';
1160
1161
        $roleDraft = $roleService->createRole($roleCreate);
1162
1163
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1164
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreate);
1165
1166
        $policy = null;
1167
        foreach ($roleDraft->getPolicies() as $policy) {
1168
            if ($policy->module === 'content' && $policy->function === 'create') {
1169
                break;
1170
            }
1171
        }
1172
        /* END: Use Case */
1173
1174
        $this->assertInstanceOf(
1175
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1176
            $policy
1177
        );
1178
1179
        return [$roleDraft, $policy];
1180
    }
1181
1182
    /**
1183
     * Test for the addPolicyByRoleDraft() method.
1184
     *
1185
     * @param array $roleAndPolicy
1186
     *
1187
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1188
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1189
     */
1190
    public function testAddPolicyByRoleDraftSetsPolicyProperties($roleAndPolicy)
1191
    {
1192
        list($role, $policy) = $roleAndPolicy;
1193
1194
        $this->assertEquals(
1195
            [$role->id, 'content', 'create'],
1196
            [$policy->roleId, $policy->module, $policy->function]
1197
        );
1198
    }
1199
1200
    /**
1201
     * Test for the addPolicyByRoleDraft() method.
1202
     *
1203
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1204
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1205
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
1206
     */
1207 View Code Duplication
    public function testAddPolicyByRoleDraftThrowsLimitationValidationException()
1208
    {
1209
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
1210
1211
        $repository = $this->getRepository();
1212
1213
        /* BEGIN: Use Case */
1214
        $roleService = $repository->getRoleService();
1215
1216
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
1217
1218
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1219
        // $roleCreate->mainLanguageCode = 'eng-US';
1220
1221
        $roleDraft = $roleService->createRole($roleCreate);
1222
1223
        // Create new subtree limitation
1224
        $limitation = new SubtreeLimitation(
1225
            [
1226
                'limitationValues' => ['/mountain/forest/tree/42/'],
1227
            ]
1228
        );
1229
1230
        // Create policy create struct and add limitation to it
1231
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'remove');
1232
        $policyCreateStruct->addLimitation($limitation);
1233
1234
        // This call will fail with an LimitationValidationException, because subtree
1235
        // "/mountain/forest/tree/42/" does not exist
1236
        $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
1237
        /* END: Use Case */
1238
    }
1239
1240
    /**
1241
     * Test for the createRole() method.
1242
     *
1243
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
1244
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1245
     */
1246
    public function testCreateRoleWithAddPolicy()
1247
    {
1248
        $repository = $this->getRepository();
1249
1250
        /* BEGIN: Use Case */
1251
        $roleService = $repository->getRoleService();
1252
1253
        // Instantiate a new create struct
1254
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1255
1256
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1257
        // $roleCreate->mainLanguageCode = 'eng-US';
1258
1259
        // Add some role policies
1260
        $roleCreate->addPolicy(
1261
            $roleService->newPolicyCreateStruct(
1262
                'content',
1263
                'read'
1264
            )
1265
        );
1266
        $roleCreate->addPolicy(
1267
            $roleService->newPolicyCreateStruct(
1268
                'content',
1269
                'translate'
1270
            )
1271
        );
1272
1273
        // Create new role instance
1274
        $roleDraft = $roleService->createRole($roleCreate);
1275
        $roleService->publishRoleDraft($roleDraft);
1276
        $role = $roleService->loadRole($roleDraft->id);
1277
1278
        $policies = [];
1279 View Code Duplication
        foreach ($role->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1280
            $policies[] = ['module' => $policy->module, 'function' => $policy->function];
1281
        }
1282
        /* END: Use Case */
1283
        array_multisort($policies);
1284
1285
        $this->assertEquals(
1286
            [
1287
                [
1288
                    'module' => 'content',
1289
                    'function' => 'read',
1290
                ],
1291
                [
1292
                    'module' => 'content',
1293
                    'function' => 'translate',
1294
                ],
1295
            ],
1296
            $policies
1297
        );
1298
    }
1299
1300
    /**
1301
     * Test for the createRoleDraft() method.
1302
     *
1303
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
1304
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1305
     */
1306
    public function testCreateRoleDraftWithAddPolicy()
1307
    {
1308
        $repository = $this->getRepository();
1309
1310
        /* BEGIN: Use Case */
1311
        $roleService = $repository->getRoleService();
1312
1313
        // Instantiate a new create struct
1314
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1315
1316
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1317
        // $roleCreate->mainLanguageCode = 'eng-US';
1318
1319
        // Add some role policies
1320
        $roleCreate->addPolicy(
1321
            $roleService->newPolicyCreateStruct(
1322
                'content',
1323
                'read'
1324
            )
1325
        );
1326
        $roleCreate->addPolicy(
1327
            $roleService->newPolicyCreateStruct(
1328
                'content',
1329
                'translate'
1330
            )
1331
        );
1332
1333
        // Create new role instance
1334
        $roleDraft = $roleService->createRole($roleCreate);
1335
1336
        $policies = [];
1337 View Code Duplication
        foreach ($roleDraft->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1338
            $policies[] = ['module' => $policy->module, 'function' => $policy->function];
1339
        }
1340
        /* END: Use Case */
1341
1342
        $this->assertEquals(
1343
            [
1344
                [
1345
                    'module' => 'content',
1346
                    'function' => 'read',
1347
                ],
1348
                [
1349
                    'module' => 'content',
1350
                    'function' => 'translate',
1351
                ],
1352
            ],
1353
            $policies
1354
        );
1355
    }
1356
1357
    /**
1358
     * Test for the newPolicyUpdateStruct() method.
1359
     *
1360
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyUpdateStruct()
1361
     */
1362
    public function testNewPolicyUpdateStruct()
1363
    {
1364
        $repository = $this->getRepository();
1365
1366
        /* BEGIN: Use Case */
1367
        $roleService = $repository->getRoleService();
1368
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1369
        /* END: Use Case */
1370
1371
        $this->assertInstanceOf(
1372
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyUpdateStruct',
1373
            $policyUpdate
1374
        );
1375
    }
1376
1377
    public function testUpdatePolicyByRoleDraftNoLimitation()
1378
    {
1379
        $repository = $this->getRepository();
1380
1381
        /* BEGIN: Use Case */
1382
        $roleService = $repository->getRoleService();
1383
1384
        // Instantiate new policy create
1385
        $policyCreate = $roleService->newPolicyCreateStruct('foo', 'bar');
1386
1387
        // Instantiate a role create and add the policy create
1388
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1389
1390
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1391
        // $roleCreate->mainLanguageCode = 'eng-US';
1392
1393
        $roleCreate->addPolicy($policyCreate);
1394
1395
        // Create a new role instance.
1396
        $roleDraft = $roleService->createRole($roleCreate);
1397
        $roleService->publishRoleDraft($roleDraft);
1398
        $role = $roleService->loadRole($roleDraft->id);
1399
1400
        $roleDraft = $roleService->createRoleDraft($role);
1401
        // Search for the new policy instance
1402
        $policy = null;
1403
        /** @var \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy */
1404
        foreach ($roleDraft->getPolicies() as $policy) {
1405
            if ($policy->module === 'foo' && $policy->function === 'bar') {
1406
                break;
1407
            }
1408
        }
1409
1410
        // Create an update struct
1411
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1412
1413
        // Update the the policy
1414
        $policy = $roleService->updatePolicyByRoleDraft(
1415
            $roleDraft,
1416
            $policy,
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1417
            $policyUpdate
1418
        );
1419
        $roleService->publishRoleDraft($roleDraft);
1420
1421
        /* END: Use Case */
1422
1423
        $this->assertInstanceOf(
1424
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1425
            $policy
1426
        );
1427
1428
        self::assertEquals([], $policy->getLimitations());
1429
    }
1430
1431
    /**
1432
     * @return array
1433
     *
1434
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft()
1435
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1436
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1437
     */
1438
    public function testUpdatePolicyByRoleDraft()
1439
    {
1440
        $repository = $this->getRepository();
1441
1442
        /* BEGIN: Use Case */
1443
        $roleService = $repository->getRoleService();
1444
1445
        // Instantiate new policy create
1446
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'translate');
1447
1448
        // Add some limitations for the new policy
1449
        $policyCreate->addLimitation(
1450
            new LanguageLimitation(
1451
                [
1452
                    'limitationValues' => ['eng-US', 'eng-GB'],
1453
                ]
1454
            )
1455
        );
1456
1457
        // Instantiate a role create and add the policy create
1458
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1459
1460
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1461
        // $roleCreate->mainLanguageCode = 'eng-US';
1462
1463
        $roleCreate->addPolicy($policyCreate);
1464
1465
        // Create a new role instance.
1466
        $roleDraft = $roleService->createRole($roleCreate);
1467
        $roleService->publishRoleDraft($roleDraft);
1468
        $role = $roleService->loadRole($roleDraft->id);
1469
1470
        $roleDraft = $roleService->createRoleDraft($role);
1471
        // Search for the new policy instance
1472
        $policy = null;
1473
        /** @var \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy */
1474
        foreach ($roleDraft->getPolicies() as $policy) {
1475
            if ($policy->module === 'content' && $policy->function === 'translate') {
1476
                break;
1477
            }
1478
        }
1479
1480
        // Create an update struct and set a modified limitation
1481
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1482
        $policyUpdate->addLimitation(
1483
            new ContentTypeLimitation(
1484
                [
1485
                    'limitationValues' => [29, 30],
1486
                ]
1487
            )
1488
        );
1489
1490
        // Update the the policy
1491
        $policy = $roleService->updatePolicyByRoleDraft(
1492
            $roleDraft,
1493
            $policy,
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1494
            $policyUpdate
1495
        );
1496
        $roleService->publishRoleDraft($roleDraft);
1497
1498
        /* END: Use Case */
1499
1500
        $this->assertInstanceOf(
1501
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1502
            $policy
1503
        );
1504
1505
        return [$roleService->loadRole($role->id), $policy];
1506
    }
1507
1508
    /**
1509
     * @param array $roleAndPolicy
1510
     *
1511
     * @see \eZ\Publish\API\Repository\RoleService::testUpdatePolicyByRoleDraft()
1512
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyByRoleDraft
1513
     */
1514
    public function testUpdatePolicyUpdatesLimitations($roleAndPolicy)
1515
    {
1516
        list($role, $policy) = $roleAndPolicy;
1517
1518
        $this->assertEquals(
1519
            [
1520
                new ContentTypeLimitation(
1521
                    [
1522
                        'limitationValues' => [29, 30],
1523
                    ]
1524
                ),
1525
            ],
1526
            $policy->getLimitations()
1527
        );
1528
1529
        return $role;
1530
    }
1531
1532
    /**
1533
     * Test for the updatePolicy() method.
1534
     *
1535
     * @param \eZ\Publish\API\Repository\Values\User\Role $role
1536
     *
1537
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft()
1538
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyUpdatesLimitations
1539
     */
1540
    public function testUpdatePolicyUpdatesRole($role)
1541
    {
1542
        $limitations = [];
1543
        foreach ($role->getPolicies() as $policy) {
1544
            foreach ($policy->getLimitations() as $limitation) {
1545
                $limitations[] = $limitation;
1546
            }
1547
        }
1548
1549
        $this->assertCount(1, $limitations);
1550
        $this->assertInstanceOf(
1551
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Limitation',
1552
            $limitations[0]
1553
        );
1554
1555
        $expectedData = [
1556
            'limitationValues' => [29, 30],
1557
        ];
1558
        $this->assertPropertiesCorrectUnsorted(
1559
            $expectedData,
1560
            $limitations[0]
1561
        );
1562
    }
1563
1564
    /**
1565
     * Test for the updatePolicy() method.
1566
     *
1567
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft()
1568
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1569
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1570
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1571
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
1572
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
1573
     */
1574
    public function testUpdatePolicyByRoleDraftThrowsLimitationValidationException()
1575
    {
1576
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
1577
1578
        $repository = $this->getRepository();
1579
1580
        /* BEGIN: Use Case */
1581
        $roleService = $repository->getRoleService();
1582
1583
        // Instantiate new policy create
1584
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
1585
1586
        // Add some limitations for the new policy
1587
        $policyCreate->addLimitation(
1588
            new SubtreeLimitation(
1589
                [
1590
                    'limitationValues' => ['/1/2/'],
1591
                ]
1592
            )
1593
        );
1594
1595
        // Instantiate a role create and add the policy create
1596
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1597
1598
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1599
        // $roleCreate->mainLanguageCode = 'eng-US';
1600
1601
        $roleCreate->addPolicy($policyCreate);
1602
1603
        // Create a new role instance.
1604
        $roleDraft = $roleService->createRole($roleCreate);
1605
        $roleService->publishRoleDraft($roleDraft);
1606
        $role = $roleService->loadRole($roleDraft->id);
1607
        $roleDraft = $roleService->createRoleDraft($role);
1608
        // Search for the new policy instance
1609
        $policy = null;
1610
        /** @var \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy */
1611
        foreach ($roleDraft->getPolicies() as $policy) {
1612
            if ($policy->module === 'content' && $policy->function === 'remove') {
1613
                break;
1614
            }
1615
        }
1616
1617
        // Create an update struct and set a modified limitation
1618
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1619
        $policyUpdate->addLimitation(
1620
            new SubtreeLimitation(
1621
                [
1622
                    'limitationValues' => ['/mountain/forest/tree/42/'],
1623
                ]
1624
            )
1625
        );
1626
1627
        // This call will fail with an LimitationValidationException, because subtree
1628
        // "/mountain/forest/tree/42/" does not exist
1629
        $policy = $roleService->updatePolicyByRoleDraft(
0 ignored issues
show
Unused Code introduced by
$policy is not used, you could remove the assignment.

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

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

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

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

Loading history...
1630
            $roleDraft,
1631
            $policy,
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1632
            $policyUpdate
1633
        );
1634
        /* END: Use Case */
1635
    }
1636
1637
    /**
1638
     * Test for the removePolicyByRoleDraft() method.
1639
     *
1640
     * @see \eZ\Publish\API\Repository\RoleService::removePolicyByRoleDraft()
1641
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1642
     */
1643 View Code Duplication
    public function testRemovePolicyByRoleDraft()
1644
    {
1645
        $repository = $this->getRepository();
1646
1647
        /* BEGIN: Use Case */
1648
        $roleService = $repository->getRoleService();
1649
1650
        // Instantiate a new role create
1651
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1652
1653
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1654
        // $roleCreate->mainLanguageCode = 'eng-US';
1655
1656
        // Create a new role with two policies
1657
        $roleDraft = $roleService->createRole($roleCreate);
1658
        $roleService->addPolicyByRoleDraft(
1659
            $roleDraft,
1660
            $roleService->newPolicyCreateStruct('content', 'create')
1661
        );
1662
        $roleService->addPolicyByRoleDraft(
1663
            $roleDraft,
1664
            $roleService->newPolicyCreateStruct('content', 'delete')
1665
        );
1666
1667
        // Delete all policies from the new role
1668
        foreach ($roleDraft->getPolicies() as $policy) {
1669
            $roleDraft = $roleService->removePolicyByRoleDraft($roleDraft, $policy);
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1670
        }
1671
        /* END: Use Case */
1672
1673
        $this->assertSame([], $roleDraft->getPolicies());
1674
    }
1675
1676
    /**
1677
     * Test for the addPolicyByRoleDraft() method.
1678
     *
1679
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1680
     */
1681
    public function testAddPolicyWithRoleAssignment()
1682
    {
1683
        $repository = $this->getRepository();
1684
1685
        /* BEGIN: Use Case */
1686
        $roleService = $repository->getRoleService();
1687
        $userService = $repository->getUserService();
1688
1689
        /* Create new user group */
1690
        $mainGroupId = $this->generateId('group', 4);
1691
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
1692
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
1693
        $userGroupCreate->setField('name', 'newUserGroup');
1694
        $userGroup = $userService->createUserGroup($userGroupCreate, $parentUserGroup);
1695
1696
        /* Create Role */
1697
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1698
        $roleDraft = $roleService->createRole($roleCreate);
1699
        $roleService->publishRoleDraft($roleDraft);
1700
1701
        $role = $roleService->loadRole($roleDraft->id);
1702
        $roleService->assignRoleToUserGroup($role, $userGroup);
1703
1704
        $roleAssignmentsBeforeNewPolicy = $roleService->getRoleAssignments($role)[0];
1705
1706
        /* Add new policy to existing role */
1707
        $roleUpdateDraft = $roleService->createRoleDraft($role);
1708
        $roleUpdateDraft = $roleService->addPolicyByRoleDraft(
1709
            $roleUpdateDraft,
1710
            $roleService->newPolicyCreateStruct('content', 'create')
1711
        );
1712
        $roleService->publishRoleDraft($roleUpdateDraft);
1713
1714
        $roleAfterUpdate = $roleService->loadRole($role->id);
1715
        $roleAssignmentsAfterNewPolicy = $roleService->getRoleAssignments($roleAfterUpdate)[0];
1716
        /* END: Use Case */
1717
1718
        $this->assertNotEquals($roleAssignmentsBeforeNewPolicy->id, $roleAssignmentsAfterNewPolicy->id);
1719
    }
1720
1721
    /**
1722
     * Test loading user/group role assignments.
1723
     *
1724
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment
1725
     *
1726
     * @covers \eZ\Publish\API\Repository\RoleService::loadRoleAssignment
1727
     */
1728
    public function testLoadRoleAssignment()
1729
    {
1730
        $repository = $this->getRepository();
1731
1732
        /* BEGIN: Use Case */
1733
        $roleService = $repository->getRoleService();
1734
        $user = $repository->getUserService()->loadUser(14);
1735
1736
        // Check inital empty assigments (also warms up potential cache to validate it is correct below)
1737
        $this->assertCount(0, $roleService->getRoleAssignmentsForUser($user));
1738
1739
        // Assignment to user group
1740
        $groupRoleAssignment = $roleService->loadRoleAssignment(25);
1741
1742
        // Assignment to user
1743
        $role = $roleService->loadRole(2);
1744
        $roleService->assignRoleToUser($role, $user);
1745
        $userRoleAssignments = $roleService->getRoleAssignmentsForUser($user);
1746
1747
        $userRoleAssignment = $roleService->loadRoleAssignment($userRoleAssignments[0]->id);
1748
        /* END: Use Case */
1749
1750
        $this->assertInstanceOf(
1751
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1752
            $groupRoleAssignment
1753
        );
1754
1755
        $this->assertEquals(
1756
            [
1757
                12,
1758
                2,
1759
                25,
1760
            ],
1761
            [
1762
                $groupRoleAssignment->userGroup->id,
0 ignored issues
show
Documentation introduced by
The property userGroup does not exist on object<eZ\Publish\API\Re...es\User\RoleAssignment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1763
                $groupRoleAssignment->role->id,
1764
                $groupRoleAssignment->id,
1765
            ]
1766
        );
1767
1768
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment', $userRoleAssignment);
1769
        self::assertEquals(14, $userRoleAssignment->user->id);
0 ignored issues
show
Documentation introduced by
The property user does not exist on object<eZ\Publish\API\Re...es\User\RoleAssignment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1770
1771
        return $groupRoleAssignment;
1772
    }
1773
1774
    /**
1775
     * Test for the getRoleAssignments() method.
1776
     *
1777
     * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[]
1778
     *
1779
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1780
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1781
     */
1782
    public function testGetRoleAssignments()
1783
    {
1784
        $repository = $this->getRepository();
1785
1786
        /* BEGIN: Use Case */
1787
        $roleService = $repository->getRoleService();
1788
1789
        // Load the editor role
1790
        $role = $roleService->loadRoleByIdentifier('Editor');
1791
1792
        // Load all assigned users and user groups
1793
        $roleAssignments = $roleService->getRoleAssignments($role);
1794
1795
        /* END: Use Case */
1796
1797
        $this->assertCount(2, $roleAssignments);
1798
        $this->assertInstanceOf(
1799
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1800
            $roleAssignments[0]
1801
        );
1802
        $this->assertInstanceOf(
1803
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1804
            $roleAssignments[1]
1805
        );
1806
1807
        return $roleAssignments;
1808
    }
1809
1810
    /**
1811
     * Test for the getRoleAssignments() method.
1812
     *
1813
     * @param \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $roleAssignments
1814
     *
1815
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1816
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1817
     */
1818
    public function testGetRoleAssignmentsContainExpectedLimitation(array $roleAssignments)
1819
    {
1820
        $this->assertEquals(
1821
            'Subtree',
1822
            reset($roleAssignments)->limitation->getIdentifier()
1823
        );
1824
    }
1825
1826
    /**
1827
     * Test for the assignRoleToUser() method.
1828
     *
1829
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser()
1830
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1831
     */
1832 View Code Duplication
    public function testAssignRoleToUser()
1833
    {
1834
        $repository = $this->getRepository();
1835
        $roleService = $repository->getRoleService();
1836
1837
        /* BEGIN: Use Case */
1838
        $user = $this->createUserVersion1();
1839
1840
        // Load the existing "Administrator" role
1841
        $role = $roleService->loadRoleByIdentifier('Administrator');
1842
1843
        // Assign the "Administrator" role to the newly created user
1844
        $roleService->assignRoleToUser($role, $user);
1845
1846
        // The assignments array will contain the new role<->user assignment
1847
        $roleAssignments = $roleService->getRoleAssignments($role);
1848
        /* END: Use Case */
1849
1850
        // Administrator + Example User
1851
        $this->assertCount(2, $roleAssignments);
1852
    }
1853
1854
    /**
1855
     * Test for the assignRoleToUser() method.
1856
     *
1857
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1858
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1859
     */
1860 View Code Duplication
    public function testAssignRoleToUserWithRoleLimitation()
1861
    {
1862
        $repository = $this->getRepository();
1863
        $roleService = $repository->getRoleService();
1864
1865
        /* BEGIN: Use Case */
1866
        $user = $this->createUserVersion1();
1867
1868
        // Load the existing "Anonymous" role
1869
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1870
1871
        // Assign the "Anonymous" role to the newly created user
1872
        $roleService->assignRoleToUser(
1873
            $role,
1874
            $user,
1875
            new SubtreeLimitation(
1876
                [
1877
                    'limitationValues' => ['/1/43/'],
1878
                ]
1879
            )
1880
        );
1881
1882
        // The assignments array will contain the new role<->user assignment
1883
        $roleAssignments = $roleService->getRoleAssignments($role);
1884
        /* END: Use Case */
1885
1886
        // Members + Partners + Anonymous + Example User
1887
        $this->assertCount(4, $roleAssignments);
1888
1889
        // Get the role limitation
1890
        $roleLimitation = null;
1891
        foreach ($roleAssignments as $roleAssignment) {
1892
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1893
            if ($roleLimitation) {
1894
                $this->assertInstanceOf(
1895
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1896
                    $roleAssignment
1897
                );
1898
                break;
1899
            }
1900
        }
1901
1902
        $this->assertEquals(
1903
            new SubtreeLimitation(
1904
                [
1905
                    'limitationValues' => ['/1/43/'],
1906
                ]
1907
            ),
1908
            $roleLimitation
1909
        );
1910
1911
        // Test again to see values being merged
1912
        $roleService->assignRoleToUser(
1913
            $role,
1914
            $user,
1915
            new SubtreeLimitation(
1916
                [
1917
                    'limitationValues' => ['/1/43/', '/1/2/'],
1918
                ]
1919
            )
1920
        );
1921
1922
        // The assignments array will contain the new role<->user assignment
1923
        $roleAssignments = $roleService->getRoleAssignments($role);
1924
1925
        // Members + Partners + Anonymous + Example User
1926
        $this->assertCount(5, $roleAssignments);
1927
1928
        // Get the role limitation
1929
        $roleLimitations = [];
1930
        foreach ($roleAssignments as $roleAssignment) {
1931
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1932
            if ($roleLimitation) {
1933
                $this->assertInstanceOf(
1934
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1935
                    $roleAssignment
1936
                );
1937
                $roleLimitations[] = $roleLimitation;
1938
            }
1939
        }
1940
        array_multisort($roleLimitations);
1941
1942
        $this->assertEquals(
1943
            [
1944
                new SubtreeLimitation(
1945
                    [
1946
                        'limitationValues' => ['/1/2/'],
1947
                    ]
1948
                ),
1949
                new SubtreeLimitation(
1950
                    [
1951
                        'limitationValues' => ['/1/43/'],
1952
                    ]
1953
                ),
1954
            ],
1955
            $roleLimitations
1956
        );
1957
    }
1958
1959
    /**
1960
     * Test for the assignRoleToUser() method.
1961
     *
1962
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1963
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1964
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1965
     */
1966
    public function testAssignRoleToUserWithRoleLimitationThrowsLimitationValidationException()
1967
    {
1968
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
1969
1970
        $repository = $this->getRepository();
1971
1972
        /* BEGIN: Use Case */
1973
        $roleService = $repository->getRoleService();
1974
1975
        // Load the existing "Anonymous" role
1976
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1977
1978
        // Get current user
1979
        $permissionResolver = $this->getRepository()->getPermissionResolver();
1980
        $userService = $repository->getUserService();
1981
        $currentUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
1982
1983
        // Assign the "Anonymous" role to the current user
1984
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
1985
        // does not exists
1986
        $roleService->assignRoleToUser(
1987
            $role,
1988
            $currentUser,
1989
            new SubtreeLimitation(
1990
                [
1991
                    'limitationValues' => ['/lorem/ipsum/42/'],
1992
                ]
1993
            )
1994
        );
1995
        /* END: Use Case */
1996
    }
1997
1998
    /**
1999
     * Test for the assignRoleToUser() method.
2000
     *
2001
     * Makes sure assigning role several times throws.
2002
     *
2003
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2004
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2005
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2006
     */
2007
    public function testAssignRoleToUserThrowsInvalidArgumentException()
2008
    {
2009
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2010
2011
        $repository = $this->getRepository();
2012
2013
        /* BEGIN: Use Case */
2014
        $roleService = $repository->getRoleService();
2015
2016
        // Load the existing "Anonymous" role
2017
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2018
2019
        // Get current user
2020
        $permissionResolver = $this->getRepository()->getPermissionResolver();
2021
        $userService = $repository->getUserService();
2022
        $currentUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
2023
2024
        // Assign the "Anonymous" role to the current user
2025
        try {
2026
            $roleService->assignRoleToUser(
2027
                $role,
2028
                $currentUser
2029
            );
2030
        } catch (Exception $e) {
2031
            $this->fail('Got exception at first valid attempt to assign role');
2032
        }
2033
2034
        // Re-Assign the "Anonymous" role to the current user
2035
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2036
        $roleService->assignRoleToUser(
2037
            $role,
2038
            $currentUser
2039
        );
2040
        /* END: Use Case */
2041
    }
2042
2043
    /**
2044
     * Test for the assignRoleToUser() method.
2045
     *
2046
     * Makes sure assigning role several times with same limitations throws.
2047
     *
2048
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2049
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2050
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2051
     */
2052
    public function testAssignRoleToUserWithRoleLimitationThrowsInvalidArgumentException()
2053
    {
2054
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2055
2056
        $repository = $this->getRepository();
2057
2058
        /* BEGIN: Use Case */
2059
        $roleService = $repository->getRoleService();
2060
2061
        // Load the existing "Anonymous" role
2062
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2063
2064
        // Get current user
2065
        $permissionResolver = $this->getRepository()->getPermissionResolver();
2066
        $userService = $repository->getUserService();
2067
        $currentUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
2068
2069
        // Assign the "Anonymous" role to the current user
2070
        try {
2071
            $roleService->assignRoleToUser(
2072
                $role,
2073
                $currentUser,
2074
                new SubtreeLimitation(
2075
                    [
2076
                        'limitationValues' => ['/1/43/', '/1/2/'],
2077
                    ]
2078
                )
2079
            );
2080
        } catch (Exception $e) {
2081
            $this->fail('Got exception at first valid attempt to assign role');
2082
        }
2083
2084
        // Re-Assign the "Anonymous" role to the current user
2085
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2086
        $roleService->assignRoleToUser(
2087
            $role,
2088
            $currentUser,
2089
            new SubtreeLimitation(
2090
                [
2091
                    'limitationValues' => ['/1/43/'],
2092
                ]
2093
            )
2094
        );
2095
        /* END: Use Case */
2096
    }
2097
2098
    /**
2099
     * Test for the removeRoleAssignment() method.
2100
     *
2101
     * @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment()
2102
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2103
     */
2104 View Code Duplication
    public function testRemoveRoleAssignment()
2105
    {
2106
        $repository = $this->getRepository();
2107
        $roleService = $repository->getRoleService();
2108
2109
        /* BEGIN: Use Case */
2110
        $user = $this->createUserVersion1();
2111
2112
        // Load the existing "Member" role
2113
        $role = $roleService->loadRoleByIdentifier('Member');
2114
2115
        // Assign the "Member" role to the newly created user
2116
        $roleService->assignRoleToUser($role, $user);
2117
2118
        // Unassign user from role
2119
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user);
2120
        foreach ($roleAssignments as $roleAssignment) {
2121
            if ($roleAssignment->role->id === $role->id) {
2122
                $roleService->removeRoleAssignment($roleAssignment);
2123
            }
2124
        }
2125
        // The assignments array will not contain the new role<->user assignment
2126
        $roleAssignments = $roleService->getRoleAssignments($role);
2127
        /* END: Use Case */
2128
2129
        // Members + Editors + Partners
2130
        $this->assertCount(3, $roleAssignments);
2131
    }
2132
2133
    /**
2134
     * Test for the getRoleAssignmentsForUser() method.
2135
     *
2136
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2137
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2138
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2139
     */
2140
    public function testGetRoleAssignmentsForUserDirect()
2141
    {
2142
        $repository = $this->getRepository();
2143
        $roleService = $repository->getRoleService();
2144
2145
        /* BEGIN: Use Case */
2146
        $user = $this->createUserVersion1();
2147
2148
        // Instantiate a role create and add some policies
2149
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2150
2151
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2152
        // $roleCreate->mainLanguageCode = 'eng-US';
2153
2154
        $roleCreate->addPolicy(
2155
            $roleService->newPolicyCreateStruct('user', 'login')
2156
        );
2157
        $roleCreate->addPolicy(
2158
            $roleService->newPolicyCreateStruct('content', 'read')
2159
        );
2160
        $roleCreate->addPolicy(
2161
            $roleService->newPolicyCreateStruct('content', 'edit')
2162
        );
2163
2164
        // Create the new role instance
2165
        $roleDraft = $roleService->createRole($roleCreate);
2166
        $roleService->publishRoleDraft($roleDraft);
2167
        $role = $roleService->loadRole($roleDraft->id);
2168
2169
        // Check inital empty assigments (also warms up potential cache to validate it is correct below)
2170
        $this->assertCount(0, $roleService->getRoleAssignmentsForUser($user));
2171
        $this->assertCount(0, $roleService->getRoleAssignments($role));
2172
2173
        // Assign role to new user
2174
        $roleService->assignRoleToUser($role, $user);
2175
2176
        // Load the currently assigned role
2177
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user);
2178
        /* END: Use Case */
2179
2180
        $this->assertCount(1, $roleAssignments);
2181
        $this->assertInstanceOf(
2182
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2183
            reset($roleAssignments)
2184
        );
2185
        $this->assertCount(1, $roleService->getRoleAssignments($role));
2186
    }
2187
2188
    /**
2189
     * Test for the getRoleAssignmentsForUser() method.
2190
     *
2191
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2192
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2193
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2194
     */
2195 View Code Duplication
    public function testGetRoleAssignmentsForUserEmpty()
2196
    {
2197
        $repository = $this->getRepository();
2198
        $roleService = $repository->getRoleService();
2199
2200
        /* BEGIN: Use Case */
2201
        $permissionResolver = $this->getRepository()->getPermissionResolver();
2202
        $userService = $repository->getUserService();
2203
        $adminUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
2204
2205
        // Load the currently assigned role
2206
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser);
2207
        /* END: Use Case */
2208
2209
        $this->assertCount(0, $roleAssignments);
2210
    }
2211
2212
    /**
2213
     * Test for the getRoleAssignmentsForUser() method.
2214
     *
2215
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2216
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2217
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2218
     */
2219
    public function testGetRoleAssignmentsForUserInherited()
2220
    {
2221
        $repository = $this->getRepository();
2222
        $roleService = $repository->getRoleService();
2223
2224
        /* BEGIN: Use Case */
2225
        $permissionResolver = $this->getRepository()->getPermissionResolver();
2226
        $userService = $repository->getUserService();
2227
        $adminUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
2228
2229
        // Load the currently assigned role + inherited role assignments
2230
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser, true);
2231
        /* END: Use Case */
2232
2233
        $this->assertCount(1, $roleAssignments);
2234
        $this->assertInstanceOf(
2235
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2236
            reset($roleAssignments)
2237
        );
2238
    }
2239
2240
    /**
2241
     * Test for the assignRoleToUserGroup() method.
2242
     *
2243
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
2244
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
2245
     */
2246 View Code Duplication
    public function testAssignRoleToUserGroup()
2247
    {
2248
        $repository = $this->getRepository();
2249
        $roleService = $repository->getRoleService();
2250
2251
        /* BEGIN: Use Case */
2252
        $userGroup = $this->createUserGroupVersion1();
2253
2254
        // Load the existing "Administrator" role
2255
        $role = $roleService->loadRoleByIdentifier('Administrator');
2256
2257
        // Assign the "Administrator" role to the newly created user group
2258
        $roleService->assignRoleToUserGroup($role, $userGroup);
2259
2260
        // The assignments array will contain the new role<->group assignment
2261
        $roleAssignments = $roleService->getRoleAssignments($role);
2262
        /* END: Use Case */
2263
2264
        // Administrator + Example Group
2265
        $this->assertCount(2, $roleAssignments);
2266
    }
2267
2268
    /**
2269
     * Test for the assignRoleToUserGroup() method.
2270
     *
2271
     * Related issue: EZP-29113
2272
     *
2273
     * @covers \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
2274
     */
2275
    public function testAssignRoleToUserGroupAffectsRoleAssignmentsForUser()
2276
    {
2277
        $roleService = $this->getRepository()->getRoleService();
2278
2279
        /* BEGIN: Use Case */
2280
        $userGroup = $this->createUserGroupVersion1();
2281
        $user = $this->createUser('user', 'John', 'Doe', $userGroup);
2282
2283
        $initRoleAssignments = $roleService->getRoleAssignmentsForUser($user, true);
2284
2285
        // Load the existing "Administrator" role
2286
        $role = $roleService->loadRoleByIdentifier('Administrator');
2287
2288
        // Assign the "Administrator" role to the newly created user group
2289
        $roleService->assignRoleToUserGroup($role, $userGroup);
2290
2291
        $updatedRoleAssignments = $roleService->getRoleAssignmentsForUser($user, true);
2292
        /* END: Use Case */
2293
2294
        $this->assertEmpty($initRoleAssignments);
2295
        $this->assertCount(1, $updatedRoleAssignments);
2296
    }
2297
2298
    /**
2299
     * Test for the assignRoleToUserGroup() method.
2300
     *
2301
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2302
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2303
     */
2304 View Code Duplication
    public function testAssignRoleToUserGroupWithRoleLimitation()
2305
    {
2306
        $repository = $this->getRepository();
2307
        $roleService = $repository->getRoleService();
2308
2309
        /* BEGIN: Use Case */
2310
        $userGroup = $this->createUserGroupVersion1();
2311
2312
        // Load the existing "Anonymous" role
2313
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2314
2315
        // Assign the "Anonymous" role to the newly created user group
2316
        $roleService->assignRoleToUserGroup(
2317
            $role,
2318
            $userGroup,
2319
            new SubtreeLimitation(
2320
                [
2321
                    'limitationValues' => ['/1/43/'],
2322
                ]
2323
            )
2324
        );
2325
2326
        // The assignments array will contain the new role<->group assignment
2327
        $roleAssignments = $roleService->getRoleAssignments($role);
2328
        /* END: Use Case */
2329
2330
        // Members + Partners + Anonymous + Example Group
2331
        $this->assertCount(4, $roleAssignments);
2332
2333
        // Get the role limitation
2334
        $roleLimitation = null;
2335
        foreach ($roleAssignments as $roleAssignment) {
2336
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2337
            if ($roleLimitation) {
2338
                break;
2339
            }
2340
        }
2341
2342
        $this->assertEquals(
2343
            new SubtreeLimitation(
2344
                [
2345
                    'limitationValues' => ['/1/43/'],
2346
                ]
2347
            ),
2348
            $roleLimitation
2349
        );
2350
2351
        // Test again to see values being merged
2352
        $roleService->assignRoleToUserGroup(
2353
            $role,
2354
            $userGroup,
2355
            new SubtreeLimitation(
2356
                [
2357
                    'limitationValues' => ['/1/43/', '/1/2/'],
2358
                ]
2359
            )
2360
        );
2361
2362
        // The assignments array will contain the new role<->user assignment
2363
        $roleAssignments = $roleService->getRoleAssignments($role);
2364
2365
        // Members + Partners + Anonymous + Example User
2366
        $this->assertCount(5, $roleAssignments);
2367
2368
        // Get the role limitation
2369
        $roleLimitations = [];
2370
        foreach ($roleAssignments as $roleAssignment) {
2371
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2372
            if ($roleLimitation) {
2373
                $this->assertInstanceOf(
2374
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2375
                    $roleAssignment
2376
                );
2377
                $roleLimitations[] = $roleLimitation;
2378
            }
2379
        }
2380
        array_multisort($roleLimitations);
2381
2382
        $this->assertEquals(
2383
            [
2384
                new SubtreeLimitation(
2385
                    [
2386
                        'limitationValues' => ['/1/2/'],
2387
                    ]
2388
                ),
2389
                new SubtreeLimitation(
2390
                    [
2391
                        'limitationValues' => ['/1/43/'],
2392
                    ]
2393
                ),
2394
            ],
2395
            $roleLimitations
2396
        );
2397
    }
2398
2399
    /**
2400
     * Test for the assignRoleToUserGroup() method.
2401
     *
2402
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2403
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2404
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2405
     */
2406 View Code Duplication
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsLimitationValidationException()
2407
    {
2408
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
2409
2410
        $repository = $this->getRepository();
2411
2412
        $mainGroupId = $this->generateId('group', 4);
2413
        /* BEGIN: Use Case */
2414
        // $mainGroupId is the ID of the main "Users" group
2415
2416
        $userService = $repository->getUserService();
2417
        $roleService = $repository->getRoleService();
2418
2419
        $userGroup = $userService->loadUserGroup($mainGroupId);
2420
2421
        // Load the existing "Anonymous" role
2422
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2423
2424
        // Assign the "Anonymous" role to the newly created user group
2425
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
2426
        // does not exists
2427
        $roleService->assignRoleToUserGroup(
2428
            $role,
2429
            $userGroup,
2430
            new SubtreeLimitation(
2431
                [
2432
                    'limitationValues' => ['/lorem/ipsum/42/'],
2433
                ]
2434
            )
2435
        );
2436
        /* END: Use Case */
2437
    }
2438
2439
    /**
2440
     * Test for the assignRoleToUserGroup() method.
2441
     *
2442
     * Makes sure assigning role several times throws.
2443
     *
2444
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2445
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2446
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2447
     */
2448 View Code Duplication
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2449
    {
2450
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2451
2452
        $repository = $this->getRepository();
2453
2454
        $mainGroupId = $this->generateId('group', 4);
2455
        /* BEGIN: Use Case */
2456
        // $mainGroupId is the ID of the main "Users" group
2457
2458
        $userService = $repository->getUserService();
2459
        $roleService = $repository->getRoleService();
2460
2461
        $userGroup = $userService->loadUserGroup($mainGroupId);
2462
2463
        // Load the existing "Anonymous" role
2464
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2465
2466
        // Assign the "Anonymous" role to the newly created user group
2467
        try {
2468
            $roleService->assignRoleToUserGroup(
2469
                $role,
2470
                $userGroup
2471
            );
2472
        } catch (Exception $e) {
2473
            $this->fail('Got exception at first valid attempt to assign role');
2474
        }
2475
2476
        // Re-Assign the "Anonymous" role to the newly created user group
2477
        // This call will fail with an InvalidArgumentException, because role is already assigned
2478
        $roleService->assignRoleToUserGroup(
2479
            $role,
2480
            $userGroup
2481
        );
2482
        /* END: Use Case */
2483
    }
2484
2485
    /**
2486
     * Test for the assignRoleToUserGroup() method.
2487
     *
2488
     * Makes sure assigning role several times with same limitations throws.
2489
     *
2490
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2491
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2492
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2493
     */
2494
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsInvalidArgumentException()
2495
    {
2496
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2497
2498
        $repository = $this->getRepository();
2499
2500
        $mainGroupId = $this->generateId('group', 4);
2501
        /* BEGIN: Use Case */
2502
        // $mainGroupId is the ID of the main "Users" group
2503
2504
        $userService = $repository->getUserService();
2505
        $roleService = $repository->getRoleService();
2506
2507
        $userGroup = $userService->loadUserGroup($mainGroupId);
2508
2509
        // Load the existing "Anonymous" role
2510
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2511
2512
        // Assign the "Anonymous" role to the newly created user group
2513
        try {
2514
            $roleService->assignRoleToUserGroup(
2515
                $role,
2516
                $userGroup,
2517
                new SubtreeLimitation(
2518
                    [
2519
                        'limitationValues' => ['/1/43/', '/1/2/'],
2520
                    ]
2521
                )
2522
            );
2523
        } catch (Exception $e) {
2524
            $this->fail('Got exception at first valid attempt to assign role');
2525
        }
2526
2527
        // Re-Assign the "Anonymous" role to the newly created user group
2528
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2529
        $roleService->assignRoleToUserGroup(
2530
            $role,
2531
            $userGroup,
2532
            new SubtreeLimitation(
2533
                [
2534
                    'limitationValues' => ['/1/43/'],
2535
                ]
2536
            )
2537
        );
2538
        /* END: Use Case */
2539
    }
2540
2541
    /**
2542
     * Test for the removeRoleAssignment() method.
2543
     *
2544
     * @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment()
2545
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2546
     */
2547
    public function testRemoveRoleAssignmentFromUserGroup()
2548
    {
2549
        $repository = $this->getRepository();
2550
        $roleService = $repository->getRoleService();
2551
2552
        /* BEGIN: Use Case */
2553
        $userGroup = $this->createUserGroupVersion1();
2554
2555
        // Load the existing "Member" role
2556
        $role = $roleService->loadRoleByIdentifier('Member');
2557
2558
        // Assign the "Member" role to the newly created user group
2559
        $roleService->assignRoleToUserGroup($role, $userGroup);
2560
2561
        // Unassign group from role
2562
        $roleAssignments = $roleService->getRoleAssignmentsForUserGroup($userGroup);
2563
2564
        // This call will fail with an "UnauthorizedException"
2565
        foreach ($roleAssignments as $roleAssignment) {
2566
            if ($roleAssignment->role->id === $role->id) {
2567
                $roleService->removeRoleAssignment($roleAssignment);
2568
            }
2569
        }
2570
        // The assignments array will not contain the new role<->group assignment
2571
        $roleAssignments = $roleService->getRoleAssignments($role);
2572
        /* END: Use Case */
2573
2574
        // Members + Editors + Partners
2575
        $this->assertCount(3, $roleAssignments);
2576
    }
2577
2578
    /**
2579
     * Test unassigning role by assignment.
2580
     *
2581
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2582
     */
2583
    public function testUnassignRoleByAssignment()
2584
    {
2585
        $repository = $this->getRepository();
2586
        $roleService = $repository->getRoleService();
2587
2588
        $role = $roleService->loadRole(2);
2589
        $user = $repository->getUserService()->loadUser(14);
2590
2591
        $originalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2592
2593
        $roleService->assignRoleToUser($role, $user);
2594
        $newAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2595
        self::assertEquals($originalAssignmentCount + 1, $newAssignmentCount);
2596
2597
        $assignments = $roleService->getRoleAssignmentsForUser($user);
2598
        $roleService->removeRoleAssignment($assignments[0]);
2599
        $finalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2600
        self::assertEquals($newAssignmentCount - 1, $finalAssignmentCount);
2601
    }
2602
2603
    /**
2604
     * Test unassigning role by assignment.
2605
     *
2606
     * But on current admin user so he lacks access to read roles.
2607
     *
2608
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2609
     */
2610 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsUnauthorizedException()
2611
    {
2612
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
2613
2614
        $repository = $this->getRepository();
2615
        $roleService = $repository->getRoleService();
2616
2617
        try {
2618
            $adminUserGroup = $repository->getUserService()->loadUserGroup(12);
2619
            $assignments = $roleService->getRoleAssignmentsForUserGroup($adminUserGroup);
2620
            $roleService->removeRoleAssignment($assignments[0]);
2621
        } catch (Exception $e) {
2622
            self::fail(
2623
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2624
            );
2625
        }
2626
2627
        $roleService->removeRoleAssignment($assignments[0]);
0 ignored issues
show
Bug introduced by
The variable $assignments does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
2628
    }
2629
2630
    /**
2631
     * Test unassigning role by non-existing assignment.
2632
     *
2633
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2634
     */
2635 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsNotFoundException()
2636
    {
2637
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
2638
2639
        $repository = $this->getRepository();
2640
        $roleService = $repository->getRoleService();
2641
2642
        try {
2643
            $editorsUserGroup = $repository->getUserService()->loadUserGroup(13);
2644
            $assignments = $roleService->getRoleAssignmentsForUserGroup($editorsUserGroup);
2645
            $roleService->removeRoleAssignment($assignments[0]);
2646
        } catch (Exception $e) {
2647
            self::fail(
2648
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2649
            );
2650
        }
2651
2652
        $roleService->removeRoleAssignment($assignments[0]);
0 ignored issues
show
Bug introduced by
The variable $assignments does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
2653
    }
2654
2655
    /**
2656
     * Test for the getRoleAssignmentsForUserGroup() method.
2657
     *
2658
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup()
2659
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2660
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2661
     */
2662
    public function testGetRoleAssignmentsForUserGroup()
2663
    {
2664
        $repository = $this->getRepository();
2665
        $roleService = $repository->getRoleService();
2666
2667
        /* BEGIN: Use Case */
2668
        $userGroup = $this->createUserGroupVersion1();
2669
2670
        // Instantiate a role create and add some policies
2671
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2672
2673
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2674
        // $roleCreate->mainLanguageCode = 'eng-US';
2675
2676
        $roleCreate->addPolicy(
2677
            $roleService->newPolicyCreateStruct('user', 'login')
2678
        );
2679
        $roleCreate->addPolicy(
2680
            $roleService->newPolicyCreateStruct('content', 'read')
2681
        );
2682
        $roleCreate->addPolicy(
2683
            $roleService->newPolicyCreateStruct('content', 'edit')
2684
        );
2685
2686
        // Create the new role instance
2687
        $roleDraft = $roleService->createRole($roleCreate);
2688
        $roleService->publishRoleDraft($roleDraft);
2689
        $role = $roleService->loadRole($roleDraft->id);
2690
2691
        // Assign role to new user group
2692
        $roleService->assignRoleToUserGroup($role, $userGroup);
2693
2694
        // Load the currently assigned role
2695
        $roleAssignments = $roleService->getRoleAssignmentsForUserGroup($userGroup);
2696
        /* END: Use Case */
2697
2698
        $this->assertCount(1, $roleAssignments);
2699
        $this->assertInstanceOf(
2700
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2701
            reset($roleAssignments)
2702
        );
2703
    }
2704
2705
    /**
2706
     * Test for the getRoleAssignmentsForUser() method.
2707
     *
2708
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2709
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2710
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2711
     */
2712
    public function testLoadPoliciesByUserId()
2713
    {
2714
        $repository = $this->getRepository();
2715
2716
        $anonUserId = $this->generateId('user', 10);
2717
        /* BEGIN: Use Case */
2718
        // $anonUserId is the ID of the "Anonymous" user.
2719
2720
        $userService = $repository->getUserService();
2721
        $roleService = $repository->getRoleService();
2722
2723
        // Load "Anonymous" user
2724
        $user = $userService->loadUser($anonUserId);
2725
2726
        // Instantiate a role create and add some policies
2727
        $roleCreate = $roleService->newRoleCreateStruct('User Role');
2728
2729
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2730
        // $roleCreate->mainLanguageCode = 'eng-US';
2731
2732
        $roleCreate->addPolicy(
2733
            $roleService->newPolicyCreateStruct('notification', 'use')
2734
        );
2735
        $roleCreate->addPolicy(
2736
            $roleService->newPolicyCreateStruct('user', 'password')
2737
        );
2738
        $roleCreate->addPolicy(
2739
            $roleService->newPolicyCreateStruct('user', 'selfedit')
2740
        );
2741
2742
        // Create the new role instance
2743
        $roleDraft = $roleService->createRole($roleCreate);
2744
        $roleService->publishRoleDraft($roleDraft);
2745
        $role = $roleService->loadRole($roleDraft->id);
2746
2747
        // Assign role to anon user
2748
        $roleService->assignRoleToUser($role, $user);
2749
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user, true);
2750
2751
        $policies = [];
2752
        foreach ($roleAssignments as $roleAssignment) {
2753
            $policies[] = $roleAssignment->getRole()->getPolicies();
2754
        }
2755
        $policies = array_merge(...$policies);
2756
2757
        $simplePolicyList = [];
2758
        foreach ($policies as $simplePolicy) {
2759
            $simplePolicyList[] = [$simplePolicy->roleId, $simplePolicy->module, $simplePolicy->function];
2760
        }
2761
        /* END: Use Case */
2762
        array_multisort($simplePolicyList);
2763
2764
        $this->assertEquals(
2765
            [
2766
                [1, 'content', 'pdf'],
2767
                [1, 'content', 'read'],
2768
                [1, 'content', 'read'],
2769
                [1, 'rss', 'feed'],
2770
                [1, 'user', 'login'],
2771
                [1, 'user', 'login'],
2772
                [1, 'user', 'login'],
2773
                [1, 'user', 'login'],
2774
                [$role->id, 'notification', 'use'],
2775
                [$role->id, 'user', 'password'],
2776
                [$role->id, 'user', 'selfedit'],
2777
            ],
2778
            $simplePolicyList
2779
        );
2780
    }
2781
2782
    /**
2783
     * Test for the publishRoleDraft() method.
2784
     *
2785
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2786
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2787
     */
2788 View Code Duplication
    public function testPublishRoleDraft()
2789
    {
2790
        $repository = $this->getRepository();
2791
2792
        /* BEGIN: Use Case */
2793
        $roleService = $repository->getRoleService();
2794
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2795
2796
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2797
        // $roleCreate->mainLanguageCode = 'eng-US';
2798
2799
        $roleDraft = $roleService->createRole($roleCreate);
2800
2801
        $roleDraft = $roleService->addPolicyByRoleDraft(
2802
            $roleDraft,
2803
            $roleService->newPolicyCreateStruct('content', 'delete')
2804
        );
2805
        $roleDraft = $roleService->addPolicyByRoleDraft(
2806
            $roleDraft,
2807
            $roleService->newPolicyCreateStruct('content', 'create')
2808
        );
2809
2810
        $roleService->publishRoleDraft($roleDraft);
2811
        /* END: Use Case */
2812
2813
        $this->assertInstanceOf(
2814
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Role',
2815
            $roleService->loadRoleByIdentifier($roleCreate->identifier)
2816
        );
2817
    }
2818
2819
    /**
2820
     * Test for the publishRoleDraft() method.
2821
     *
2822
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2823
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2824
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
2825
     */
2826
    public function testPublishRoleDraftAddPolicies()
2827
    {
2828
        $repository = $this->getRepository();
2829
2830
        /* BEGIN: Use Case */
2831
        $roleService = $repository->getRoleService();
2832
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2833
2834
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2835
        // $roleCreate->mainLanguageCode = 'eng-US';
2836
2837
        $roleDraft = $roleService->createRole($roleCreate);
2838
2839
        $roleDraft = $roleService->addPolicyByRoleDraft(
2840
            $roleDraft,
2841
            $roleService->newPolicyCreateStruct('content', 'delete')
2842
        );
2843
        $roleDraft = $roleService->addPolicyByRoleDraft(
2844
            $roleDraft,
2845
            $roleService->newPolicyCreateStruct('content', 'create')
2846
        );
2847
2848
        $roleService->publishRoleDraft($roleDraft);
2849
        $role = $roleService->loadRoleByIdentifier($roleCreate->identifier);
2850
        /* END: Use Case */
2851
2852
        $actual = [];
2853
        foreach ($role->getPolicies() as $policy) {
2854
            $actual[] = [
2855
                'module' => $policy->module,
2856
                'function' => $policy->function,
2857
            ];
2858
        }
2859
        usort(
2860
            $actual,
2861
            function ($p1, $p2) {
2862
                return strcasecmp($p1['function'], $p2['function']);
2863
            }
2864
        );
2865
2866
        $this->assertEquals(
2867
            [
2868
                [
2869
                    'module' => 'content',
2870
                    'function' => 'create',
2871
                ],
2872
                [
2873
                    'module' => 'content',
2874
                    'function' => 'delete',
2875
                ],
2876
            ],
2877
            $actual
2878
        );
2879
    }
2880
2881
    /**
2882
     * Create a user group fixture in a variable named <b>$userGroup</b>,.
2883
     *
2884
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
2885
     */
2886
    private function createUserGroupVersion1()
2887
    {
2888
        $repository = $this->getRepository();
2889
2890
        $mainGroupId = $this->generateId('group', 4);
2891
        /* BEGIN: Inline */
2892
        // $mainGroupId is the ID of the main "Users" group
2893
2894
        $roleService = $repository->getRoleService();
0 ignored issues
show
Unused Code introduced by
$roleService is not used, you could remove the assignment.

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

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

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

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

Loading history...
2895
        $userService = $repository->getUserService();
2896
2897
        // Load main group
2898
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
2899
2900
        // Instantiate a new create struct
2901
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
2902
        $userGroupCreate->setField('name', 'Example Group');
2903
2904
        // Create the new user group
2905
        $userGroup = $userService->createUserGroup(
2906
            $userGroupCreate,
2907
            $parentUserGroup
2908
        );
2909
        /* END: Inline */
2910
2911
        return $userGroup;
2912
    }
2913
}
2914