Completed
Push — master ( ba78f6...0a7dac )
by Łukasz
35:28 queued 20:34
created

RoleTest::getPartlyMockedLimitationService()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Repository\Tests\Service\Mock;
8
9
use ArrayIterator;
10
use eZ\Publish\API\Repository\Repository;
11
use eZ\Publish\API\Repository\UserService;
12
use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation;
13
use eZ\Publish\API\Repository\Values\User\PolicyCreateStruct;
14
use eZ\Publish\API\Repository\Values\User\PolicyDraft;
15
use eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct;
16
use eZ\Publish\API\Repository\Values\User\Role;
17
use eZ\Publish\API\Repository\Values\User\RoleCreateStruct;
18
use eZ\Publish\API\Repository\Values\User\RoleDraft;
19
use eZ\Publish\API\Repository\Values\User\User;
20
use eZ\Publish\API\Repository\Values\User\UserGroup;
21
use eZ\Publish\Core\Repository\Helper\RoleDomainMapper;
22
use eZ\Publish\Core\Repository\Permission\LimitationService;
23
use eZ\Publish\Core\Repository\RoleService;
24
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
25
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
26
use eZ\Publish\SPI\Persistence\User as SPIUser;
27
use eZ\Publish\SPI\Persistence\User\Role as SPIRole;
28
use eZ\Publish\SPI\Limitation\Type as SPIType;
29
30
/**
31
 * Mock test case for Role service.
32
 */
33
class RoleTest extends BaseServiceMockTest
34
{
35
    /**
36
     * Test for the createRole() method.
37
     *
38
     * @covers \eZ\Publish\Core\Repository\RoleService::createRole
39
     * @covers \eZ\Publish\Core\Repository\RoleService::validateRoleCreateStruct
40
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitations
41
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
42
     */
43
    public function testCreateRoleThrowsLimitationValidationException()
44
    {
45
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
46
47
        $limitationMock = $this->createMock(RoleLimitation::class);
48
        $limitationTypeMock = $this->createMock(SPIType::class);
49
50
        $limitationMock->expects($this->any())
51
            ->method('getIdentifier')
52
            ->will($this->returnValue('mockIdentifier'));
53
54
        $limitationTypeMock->expects($this->once())
55
            ->method('acceptValue')
56
            ->with($this->equalTo($limitationMock));
57
        $limitationTypeMock->expects($this->once())
58
            ->method('validate')
59
            ->with($this->equalTo($limitationMock))
60
            ->will($this->returnValue([42]));
61
62
        $settings = [
63
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
64
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
65
        ];
66
67
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRoleByIdentifier'], $settings);
68
69
        /** @var \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStructMock */
70
        $roleCreateStructMock = $this->createMock(RoleCreateStruct::class);
71
        $policyCreateStructMock = $this->createMock(PolicyCreateStruct::class);
72
73
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
74
        $policyCreateStructMock->module = 'mockModule';
75
        $policyCreateStructMock->function = 'mockFunction';
76
        $roleCreateStructMock->identifier = 'mockIdentifier';
77
        $roleServiceMock->expects($this->once())
78
            ->method('loadRoleByIdentifier')
79
            ->with($this->equalTo('mockIdentifier'))
80
            ->will($this->throwException(new NotFoundException('Role', 'mockIdentifier')));
81
82
        /* @var \PHPUnit\Framework\MockObject\MockObject $roleCreateStructMock */
83
        $roleCreateStructMock->expects($this->once())
84
            ->method('getPolicies')
85
            ->will($this->returnValue([$policyCreateStructMock]));
86
87
        /* @var \PHPUnit\Framework\MockObject\MockObject $policyCreateStructMock */
88
        $policyCreateStructMock->expects($this->once())
89
            ->method('getLimitations')
90
            ->will($this->returnValue([$limitationMock]));
91
92
        $permissionResolverMock = $this->getPermissionResolverMock();
93
        $permissionResolverMock->expects($this->once())
94
            ->method('canUser')
95
            ->with(
96
                $this->equalTo('role'),
97
                $this->equalTo('create'),
98
                $this->equalTo($roleCreateStructMock)
99
            )->will($this->returnValue(true));
100
101
        /* @var \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStructMock */
102
        $roleServiceMock->createRole($roleCreateStructMock);
103
    }
104
105
    /**
106
     * Test for the addPolicy() method.
107
     *
108
     * @covers \eZ\Publish\Core\Repository\RoleService::addPolicyByRoleDraft
109
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitations
110
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
111
     */
112
    public function testAddPolicyThrowsLimitationValidationException()
113
    {
114
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
115
116
        $limitationMock = $this->createMock(RoleLimitation::class);
117
        $limitationTypeMock = $this->createMock(SPIType::class);
118
119
        $limitationTypeMock->expects($this->once())
120
            ->method('acceptValue')
121
            ->with($this->equalTo($limitationMock));
122
        $limitationTypeMock->expects($this->once())
123
            ->method('validate')
124
            ->with($this->equalTo($limitationMock))
125
            ->will($this->returnValue([42]));
126
127
        $limitationMock->expects($this->any())
128
            ->method('getIdentifier')
129
            ->will($this->returnValue('mockIdentifier'));
130
131
        $settings = [
132
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
133
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
134
        ];
135
136
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRoleDraft'], $settings);
137
138
        $roleDraftMock = $this->createMock(RoleDraft::class);
139
        $policyCreateStructMock = $this->createMock(PolicyCreateStruct::class);
140
141
        $roleDraftMock->expects($this->any())
142
            ->method('__get')
143
            ->with('id')
144
            ->will($this->returnValue(42));
145
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
146
        $policyCreateStructMock->module = 'mockModule';
147
        $policyCreateStructMock->function = 'mockFunction';
148
149
        $roleServiceMock->expects($this->once())
150
            ->method('loadRoleDraft')
151
            ->with($this->equalTo(42))
152
            ->will($this->returnValue($roleDraftMock));
153
154
        /* @var \PHPUnit\Framework\MockObject\MockObject $policyCreateStructMock */
155
        $policyCreateStructMock->expects($this->once())
156
            ->method('getLimitations')
157
            ->will($this->returnValue([$limitationMock]));
158
159
        $permissionResolverMock = $this->getPermissionResolverMock();
160
        $permissionResolverMock->expects($this->once())
161
            ->method('canUser')
162
            ->with(
163
                $this->equalTo('role'),
164
                $this->equalTo('update'),
165
                $this->equalTo($roleDraftMock)
166
            )->will($this->returnValue(true));
167
168
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleDraftMock */
169
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
170
        $roleServiceMock->addPolicyByRoleDraft($roleDraftMock, $policyCreateStructMock);
171
    }
172
173
    /**
174
     * Test for the updatePolicyByRoleDraft() method.
175
     *
176
     * @covers \eZ\Publish\Core\Repository\RoleService::updatePolicyByRoleDraft
177
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitations
178
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
179
     */
180
    public function testUpdatePolicyThrowsLimitationValidationException()
181
    {
182
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
183
184
        $limitationMock = $this->createMock(RoleLimitation::class);
185
        $limitationTypeMock = $this->createMock(SPIType::class);
186
187
        $limitationTypeMock->expects($this->once())
188
            ->method('acceptValue')
189
            ->with($this->equalTo($limitationMock));
190
        $limitationTypeMock->expects($this->once())
191
            ->method('validate')
192
            ->with($this->equalTo($limitationMock))
193
            ->will($this->returnValue([42]));
194
195
        $limitationMock->expects($this->any())
196
            ->method('getIdentifier')
197
            ->will($this->returnValue('mockIdentifier'));
198
199
        $settings = [
200
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
201
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
202
        ];
203
204
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRole'], $settings);
205
206
        $roleDraftMock = $this->createMock(RoleDraft::class);
207
        $policyDraftMock = $this->createMock(PolicyDraft::class);
208
        $policyUpdateStructMock = $this->createMock(PolicyUpdateStruct::class);
209
210
        $policyDraftMock->expects($this->any())
211
            ->method('__get')
212
            ->will(
213
                $this->returnCallback(
214
                    function ($propertyName) {
215
                        switch ($propertyName) {
216
                            case 'module':
217
                                return 'mockModule';
218
                            case 'function':
219
                                return 'mockFunction';
220
                        }
221
222
                        return null;
223
                    }
224
                )
225
            );
226
227
        /* @var \PHPUnit\Framework\MockObject\MockObject $policyCreateStructMock */
228
        $policyUpdateStructMock->expects($this->once())
229
            ->method('getLimitations')
230
            ->will($this->returnValue([$limitationMock]));
231
232
        $permissionResolverMock = $this->getPermissionResolverMock();
233
        $permissionResolverMock->expects($this->once())
234
            ->method('canUser')
235
            ->with(
236
                $this->equalTo('role'),
237
                $this->equalTo('update'),
238
                $this->equalTo($roleDraftMock)
239
            )->will($this->returnValue(true));
240
241
        /* @var \eZ\Publish\API\Repository\Values\User\Policy $policyDraftMock */
242
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStructMock */
243
        $roleServiceMock->updatePolicyByRoleDraft(
244
            $roleDraftMock,
245
            $policyDraftMock,
246
            $policyUpdateStructMock
247
        );
248
    }
249
250
    /**
251
     * Test for the assignRoleToUser() method.
252
     *
253
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
254
     */
255
    public function testAssignRoleToUserThrowsUnauthorizedException()
256
    {
257
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
258
259
        $roleServiceMock = $this->getPartlyMockedRoleService();
260
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
261
        $roleMock = $this->createMock(Role::class);
262
        /** @var \eZ\Publish\API\Repository\Values\User\User $userMock */
263
        $userMock = $this->createMock(User::class);
264
265
        $permissionResolverMock = $this->getPermissionResolverMock();
266
        $permissionResolverMock->expects($this->once())
267
            ->method('canUser')
268
            ->with(
269
                $this->equalTo('role'),
270
                $this->equalTo('assign'),
271
                $this->equalTo($userMock),
272
                $this->equalTo([$roleMock])
273
            )->will($this->returnValue(false));
274
275
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, null);
276
    }
277
278
    /**
279
     * Test for the assignRoleToUser() method.
280
     *
281
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
282
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
283
     */
284
    public function testAssignRoleToUserThrowsLimitationValidationException()
285
    {
286
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
287
288
        $limitationMock = $this->createMock(RoleLimitation::class);
289
        $limitationTypeMock = $this->createMock(SPIType::class);
290
291
        $limitationTypeMock->expects($this->once())
292
            ->method('acceptValue')
293
            ->with($this->equalTo($limitationMock));
294
        $limitationTypeMock->expects($this->once())
295
            ->method('validate')
296
            ->with($this->equalTo($limitationMock))
297
            ->will($this->returnValue([42]));
298
299
        $limitationMock->expects($this->once())
300
            ->method('getIdentifier')
301
            ->will($this->returnValue('testIdentifier'));
302
303
        $settings = [
304
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
305
        ];
306
307
        $roleServiceMock = $this->getPartlyMockedRoleService(null, $settings);
308
309
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
310
        $roleMock = $this->createMock(Role::class);
311
        /** @var \eZ\Publish\API\Repository\Values\User\User $userMock */
312
        $userMock = $this->createMock(User::class);
313
314
        $permissionResolverMock = $this->getPermissionResolverMock();
315
        $permissionResolverMock->expects($this->once())
316
            ->method('canUser')
317
            ->with(
318
                $this->equalTo('role'),
319
                $this->equalTo('assign'),
320
                $this->equalTo($userMock),
321
                $this->equalTo([$roleMock])
322
            )->will($this->returnValue(true));
323
324
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
325
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, $limitationMock);
326
    }
327
328
    /**
329
     * Test for the assignRoleToUser() method.
330
     *
331
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
332
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
333
     */
334
    public function testAssignRoleToUserThrowsBadStateException()
335
    {
336
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\BadStateException::class);
337
338
        $roleServiceMock = $this->getPartlyMockedRoleService();
339
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
340
        $roleMock = $this->createMock(Role::class);
341
        /** @var \eZ\Publish\API\Repository\Values\User\User $userMock */
342
        $userMock = $this->createMock(User::class);
343
        $limitationMock = $this->createMock(RoleLimitation::class);
344
345
        $limitationMock->expects($this->once())
346
            ->method('getIdentifier')
347
            ->will($this->returnValue('testIdentifier'));
348
349
        $permissionResolverMock = $this->getPermissionResolverMock();
350
        $permissionResolverMock->expects($this->once())
351
            ->method('canUser')
352
            ->with(
353
                $this->equalTo('role'),
354
                $this->equalTo('assign'),
355
                $this->equalTo($userMock),
356
                $this->equalTo([$roleMock])
357
            )->will($this->returnValue(true));
358
359
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
360
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, $limitationMock);
361
    }
362
363
    /**
364
     * Test for the assignRoleToUser() method.
365
     *
366
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
367
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
368
     */
369
    public function testAssignRoleToUser()
370
    {
371
        $limitationMock = $this->createMock(RoleLimitation::class);
372
        $limitationTypeMock = $this->createMock(SPIType::class);
373
374
        $limitationTypeMock->expects($this->once())
375
            ->method('acceptValue')
376
            ->with($this->equalTo($limitationMock));
377
        $limitationTypeMock->expects($this->once())
378
            ->method('validate')
379
            ->with($this->equalTo($limitationMock))
380
            ->will($this->returnValue([]));
381
382
        $limitationMock->expects($this->exactly(2))
383
            ->method('getIdentifier')
384
            ->will($this->returnValue('testIdentifier'));
385
386
        $settings = [
387
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
388
        ];
389
390
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues'], $settings);
391
392
        $repository = $this->getRepositoryMock();
393
        $roleMock = $this->createMock(Role::class);
394
        $userMock = $this->createMock(User::class);
395
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
396
397
        $userMock->expects($this->any())
398
            ->method('__get')
399
            ->with('id')
400
            ->will($this->returnValue(24));
401
402
        $permissionResolverMock = $this->getPermissionResolverMock();
403
        $permissionResolverMock->expects($this->once())
404
            ->method('canUser')
405
            ->with(
406
                $this->equalTo('role'),
407
                $this->equalTo('assign'),
408
                $this->equalTo($userMock),
409
                $this->equalTo([$roleMock])
410
            )->will($this->returnValue(true));
411
412
        $roleMock->expects($this->any())
413
            ->method('__get')
414
            ->with('id')
415
            ->will($this->returnValue(42));
416
417
        $userHandlerMock->expects($this->once())
418
            ->method('loadRole')
419
            ->with($this->equalTo(42))
420
            ->will($this->returnValue(new SPIRole(['id' => 42])));
421
422
        $userHandlerMock->expects($this->once())
423
            ->method('load')
424
            ->with($this->equalTo(24))
425
            ->will($this->returnValue(new SPIUser(['id' => 24])));
426
427
        $roleServiceMock->expects($this->once())
428
            ->method('checkAssignmentAndFilterLimitationValues')
429
            ->with(24, $this->isInstanceOf(SPIRole::class), ['testIdentifier' => []])
430
            ->will($this->returnValue(['testIdentifier' => []]));
431
432
        $repository->expects($this->once())->method('beginTransaction');
433
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
434
        $userHandlerMock->expects($this->once())
435
            ->method('assignRole')
436
            ->with(
437
                $this->equalTo(24),
438
                $this->equalTo(42),
439
                $this->equalTo(['testIdentifier' => []])
440
            );
441
        $repository->expects($this->once())->method('commit');
442
443
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
444
        /* @var \eZ\Publish\API\Repository\Values\User\User $userMock */
445
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
446
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, $limitationMock);
447
    }
448
449
    /**
450
     * Test for the assignRoleToUser() method.
451
     *
452
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
453
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
454
     */
455
    public function testAssignRoleToUserWithNullLimitation()
456
    {
457
        $repository = $this->getRepositoryMock();
458
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
459
        $roleMock = $this->createMock(Role::class);
460
        $userMock = $this->createMock(User::class);
461
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
462
463
        $userMock->expects($this->any())
464
            ->method('__get')
465
            ->with('id')
466
            ->will($this->returnValue(24));
467
468
        $permissionResolverMock = $this->getPermissionResolverMock();
469
        $permissionResolverMock->expects($this->once())
470
            ->method('canUser')
471
            ->with(
472
                $this->equalTo('role'),
473
                $this->equalTo('assign'),
474
                $this->equalTo($userMock),
475
                $this->equalTo([$roleMock])
476
            )->will($this->returnValue(true));
477
478
        $roleMock->expects($this->any())
479
            ->method('__get')
480
            ->with('id')
481
            ->will($this->returnValue(42));
482
483
        $userHandlerMock->expects($this->once())
484
            ->method('loadRole')
485
            ->with($this->equalTo(42))
486
            ->will($this->returnValue(new SPIRole(['id' => 42])));
487
488
        $userHandlerMock->expects($this->once())
489
            ->method('load')
490
            ->with($this->equalTo(24))
491
            ->will($this->returnValue(new SPIUser(['id' => 24])));
492
493
        $roleServiceMock->expects($this->once())
494
            ->method('checkAssignmentAndFilterLimitationValues')
495
            ->with(24, $this->isInstanceOf(SPIRole::class), null)
496
            ->will($this->returnValue(null));
497
498
        $repository->expects($this->once())->method('beginTransaction');
499
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
500
        $userHandlerMock->expects($this->once())
501
            ->method('assignRole')
502
            ->with(
503
                $this->equalTo(24),
504
                $this->equalTo(42),
505
                $this->equalTo(null)
506
            );
507
        $repository->expects($this->once())->method('commit');
508
509
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
510
        /* @var \eZ\Publish\API\Repository\Values\User\User $userMock */
511
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, null);
512
    }
513
514
    /**
515
     * Test for the assignRoleToUser() method.
516
     *
517
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
518
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
519
     */
520
    public function testAssignRoleToUserWithRollback()
521
    {
522
        $this->expectException(\Exception::class);
523
524
        $repository = $this->getRepositoryMock();
525
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
526
        $roleMock = $this->createMock(Role::class);
527
        $userMock = $this->createMock(User::class);
528
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
529
530
        $userMock->expects($this->any())
531
            ->method('__get')
532
            ->with('id')
533
            ->will($this->returnValue(24));
534
535
        $permissionResolverMock = $this->getPermissionResolverMock();
536
        $permissionResolverMock->expects($this->once())
537
            ->method('canUser')
538
            ->with(
539
                $this->equalTo('role'),
540
                $this->equalTo('assign'),
541
                $this->equalTo($userMock),
542
                $this->equalTo([$roleMock])
543
            )->will($this->returnValue(true));
544
545
        $roleMock->expects($this->any())
546
            ->method('__get')
547
            ->with('id')
548
            ->will($this->returnValue(42));
549
550
        $userHandlerMock->expects($this->once())
551
            ->method('loadRole')
552
            ->with($this->equalTo(42))
553
            ->will($this->returnValue(new SPIRole(['id' => 42])));
554
555
        $userHandlerMock->expects($this->once())
556
            ->method('load')
557
            ->with($this->equalTo(24))
558
            ->will($this->returnValue(new SPIUser(['id' => 24])));
559
560
        $roleServiceMock->expects($this->once())
561
            ->method('checkAssignmentAndFilterLimitationValues')
562
            ->with(24, $this->isInstanceOf(SPIRole::class), null)
563
            ->will($this->returnValue(null));
564
565
        $repository->expects($this->once())->method('beginTransaction');
566
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
567
        $userHandlerMock->expects($this->once())
568
            ->method('assignRole')
569
            ->with(
570
                $this->equalTo(24),
571
                $this->equalTo(42),
572
                $this->equalTo(null)
573
            )->will($this->throwException(new \Exception()));
574
        $repository->expects($this->once())->method('rollback');
575
576
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
577
        /* @var \eZ\Publish\API\Repository\Values\User\User $userMock */
578
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, null);
579
    }
580
581
    /**
582
     * Test for the assignRoleToUserGroup() method.
583
     *
584
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
585
     */
586
    public function testAssignRoleToUserGroupThrowsUnauthorizedException()
587
    {
588
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
589
590
        $repository = $this->getRepositoryMock();
0 ignored issues
show
Unused Code introduced by
$repository 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...
591
        $roleServiceMock = $this->getPartlyMockedRoleService();
592
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
593
        $roleMock = $this->createMock(Role::class);
594
        /** @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
595
        $userGroupMock = $this->createMock(UserGroup::class);
596
597
        $permissionResolverMock = $this->getPermissionResolverMock();
598
        $permissionResolverMock->expects($this->once())
599
            ->method('canUser')
600
            ->with(
601
                $this->equalTo('role'),
602
                $this->equalTo('assign'),
603
                $this->equalTo($userGroupMock),
604
                $this->equalTo([$roleMock])
605
            )->will($this->returnValue(false));
606
607
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, null);
608
    }
609
610
    /**
611
     * Test for the assignRoleToUserGroup() method.
612
     *
613
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
614
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
615
     */
616
    public function testAssignRoleToUserGroupThrowsLimitationValidationException()
617
    {
618
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
619
620
        $limitationMock = $this->createMock(RoleLimitation::class);
621
        $limitationTypeMock = $this->createMock(SPIType::class);
622
623
        $limitationTypeMock->expects($this->once())
624
            ->method('acceptValue')
625
            ->with($this->equalTo($limitationMock));
626
        $limitationTypeMock->expects($this->once())
627
            ->method('validate')
628
            ->with($this->equalTo($limitationMock))
629
            ->will($this->returnValue([42]));
630
631
        $limitationMock->expects($this->once())
632
            ->method('getIdentifier')
633
            ->will($this->returnValue('testIdentifier'));
634
635
        $settings = [
636
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
637
        ];
638
639
        $roleServiceMock = $this->getPartlyMockedRoleService(null, $settings);
640
641
        $repository = $this->getRepositoryMock();
0 ignored issues
show
Unused Code introduced by
$repository 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...
642
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
643
        $roleMock = $this->createMock(Role::class);
644
        /** @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
645
        $userGroupMock = $this->createMock(UserGroup::class);
646
647
        $permissionResolverMock = $this->getPermissionResolverMock();
648
        $permissionResolverMock->expects($this->once())
649
            ->method('canUser')
650
            ->with(
651
                $this->equalTo('role'),
652
                $this->equalTo('assign'),
653
                $this->equalTo($userGroupMock),
654
                $this->equalTo([$roleMock])
655
            )->will($this->returnValue(true));
656
657
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
658
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, $limitationMock);
659
    }
660
661
    /**
662
     * Test for the assignRoleToUserGroup() method.
663
     *
664
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
665
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
666
     */
667
    public function testAssignRoleGroupToUserThrowsBadStateException()
668
    {
669
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\BadStateException::class);
670
671
        $repository = $this->getRepositoryMock();
0 ignored issues
show
Unused Code introduced by
$repository 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...
672
        $roleServiceMock = $this->getPartlyMockedRoleService();
673
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
674
        $roleMock = $this->createMock(Role::class);
675
        /** @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
676
        $userGroupMock = $this->createMock(UserGroup::class);
677
        $limitationMock = $this->createMock(RoleLimitation::class);
678
679
        $limitationMock->expects($this->once())
680
            ->method('getIdentifier')
681
            ->will($this->returnValue('testIdentifier'));
682
683
        $permissionResolverMock = $this->getPermissionResolverMock();
684
        $permissionResolverMock->expects($this->once())
685
            ->method('canUser')
686
            ->with(
687
                $this->equalTo('role'),
688
                $this->equalTo('assign'),
689
                $this->equalTo($userGroupMock),
690
                $this->equalTo([$roleMock])
691
            )->will($this->returnValue(true));
692
693
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
694
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, $limitationMock);
695
    }
696
697
    /**
698
     * Test for the assignRoleToUserGroup() method.
699
     *
700
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
701
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
702
     */
703
    public function testAssignRoleToUserGroup()
704
    {
705
        $limitationMock = $this->createMock(RoleLimitation::class);
706
        $limitationTypeMock = $this->createMock(SPIType::class);
707
708
        $limitationTypeMock->expects($this->once())
709
            ->method('acceptValue')
710
            ->with($this->equalTo($limitationMock));
711
        $limitationTypeMock->expects($this->once())
712
            ->method('validate')
713
            ->with($this->equalTo($limitationMock))
714
            ->will($this->returnValue([]));
715
716
        $limitationMock->expects($this->exactly(2))
717
            ->method('getIdentifier')
718
            ->will($this->returnValue('testIdentifier'));
719
720
        $settings = [
721
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
722
        ];
723
724
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues'], $settings);
725
726
        $repository = $this->getRepositoryMock();
727
        $roleMock = $this->createMock(Role::class);
728
        $userGroupMock = $this->createMock(UserGroup::class);
729
        $userServiceMock = $this->createMock(UserService::class);
730
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
731
732
        $repository->expects($this->once())
733
            ->method('getUserService')
734
            ->will($this->returnValue($userServiceMock));
735
        $userGroupMock->expects($this->any())
736
            ->method('__get')
737
            ->with('id')
738
            ->will($this->returnValue(24));
739
740
        $permissionResolverMock = $this->getPermissionResolverMock();
741
        $permissionResolverMock->expects($this->once())
742
            ->method('canUser')
743
            ->with(
744
                $this->equalTo('role'),
745
                $this->equalTo('assign'),
746
                $this->equalTo($userGroupMock),
747
                $this->equalTo([$roleMock])
748
            )->will($this->returnValue(true));
749
750
        $roleMock->expects($this->any())
751
            ->method('__get')
752
            ->with('id')
753
            ->will($this->returnValue(42));
754
755
        $userHandlerMock->expects($this->once())
756
            ->method('loadRole')
757
            ->with($this->equalTo(42))
758
            ->will($this->returnValue(new SPIRole(['id' => 42])));
759
760
        $userServiceMock->expects($this->once())
761
            ->method('loadUserGroup')
762
            ->with($this->equalTo(24))
763
            ->will($this->returnValue($userGroupMock));
764
765
        $roleServiceMock->expects($this->once())
766
            ->method('checkAssignmentAndFilterLimitationValues')
767
            ->with(24, $this->isInstanceOf(SPIRole::class), ['testIdentifier' => []])
768
            ->will($this->returnValue(['testIdentifier' => []]));
769
770
        $repository->expects($this->once())->method('beginTransaction');
771
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
772
        $userHandlerMock->expects($this->once())
773
            ->method('assignRole')
774
            ->with(
775
                $this->equalTo(24),
776
                $this->equalTo(42),
777
                $this->equalTo(['testIdentifier' => []])
778
            );
779
        $repository->expects($this->once())->method('commit');
780
781
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
782
        /* @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
783
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
784
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, $limitationMock);
785
    }
786
787
    /**
788
     * Test for the assignRoleToUserGroup() method.
789
     *
790
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
791
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
792
     */
793
    public function testAssignRoleToUserGroupWithNullLimitation()
794
    {
795
        $repository = $this->getRepositoryMock();
796
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
797
        $roleMock = $this->createMock(Role::class);
798
        $userGroupMock = $this->createMock(UserGroup::class);
799
        $userServiceMock = $this->createMock(UserService::class);
800
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
801
802
        $repository->expects($this->once())
803
            ->method('getUserService')
804
            ->will($this->returnValue($userServiceMock));
805
        $userGroupMock->expects($this->any())
806
            ->method('__get')
807
            ->with('id')
808
            ->will($this->returnValue(24));
809
810
        $permissionResolverMock = $this->getPermissionResolverMock();
811
        $permissionResolverMock->expects($this->once())
812
            ->method('canUser')
813
            ->with(
814
                $this->equalTo('role'),
815
                $this->equalTo('assign'),
816
                $this->equalTo($userGroupMock),
817
                $this->equalTo([$roleMock])
818
            )->will($this->returnValue(true));
819
820
        $roleMock->expects($this->any())
821
            ->method('__get')
822
            ->with('id')
823
            ->will($this->returnValue(42));
824
825
        $userHandlerMock->expects($this->once())
826
            ->method('loadRole')
827
            ->with($this->equalTo(42))
828
            ->will($this->returnValue(new SPIRole(['id' => 42])));
829
830
        $userServiceMock->expects($this->once())
831
            ->method('loadUserGroup')
832
            ->with($this->equalTo(24))
833
            ->will($this->returnValue($userGroupMock));
834
835
        $roleServiceMock->expects($this->once())
836
            ->method('checkAssignmentAndFilterLimitationValues')
837
            ->with(24, $this->isInstanceOf(SPIRole::class), null)
838
            ->will($this->returnValue(null));
839
840
        $repository->expects($this->once())->method('beginTransaction');
841
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
842
        $userHandlerMock->expects($this->once())
843
            ->method('assignRole')
844
            ->with(
845
                $this->equalTo(24),
846
                $this->equalTo(42),
847
                $this->equalTo(null)
848
            );
849
        $repository->expects($this->once())->method('commit');
850
851
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
852
        /* @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
853
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, null);
854
    }
855
856
    /**
857
     * Test for the assignRoleToUserGroup() method.
858
     *
859
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
860
     * @covers \eZ\Publish\Core\Repository\Permission\LimitationService::validateLimitation
861
     */
862
    public function testAssignRoleToUserGroupWithRollback()
863
    {
864
        $this->expectException(\Exception::class);
865
866
        $repository = $this->getRepositoryMock();
867
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
868
        $roleMock = $this->createMock(Role::class);
869
        $userGroupMock = $this->createMock(UserGroup::class);
870
        $userServiceMock = $this->createMock(UserService::class);
871
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
872
873
        $repository->expects($this->once())
874
            ->method('getUserService')
875
            ->will($this->returnValue($userServiceMock));
876
        $userGroupMock->expects($this->any())
877
            ->method('__get')
878
            ->with('id')
879
            ->will($this->returnValue(24));
880
881
        $permissionResolverMock = $this->getPermissionResolverMock();
882
        $permissionResolverMock->expects($this->once())
883
            ->method('canUser')
884
            ->with(
885
                $this->equalTo('role'),
886
                $this->equalTo('assign'),
887
                $this->equalTo($userGroupMock),
888
                $this->equalTo([$roleMock])
889
            )->will($this->returnValue(true));
890
891
        $roleMock->expects($this->any())
892
            ->method('__get')
893
            ->with('id')
894
            ->will($this->returnValue(42));
895
896
        $userHandlerMock->expects($this->once())
897
            ->method('loadRole')
898
            ->with($this->equalTo(42))
899
            ->will($this->returnValue(new SPIRole(['id' => 42])));
900
901
        $userServiceMock->expects($this->once())
902
            ->method('loadUserGroup')
903
            ->with($this->equalTo(24))
904
            ->will($this->returnValue($userGroupMock));
905
906
        $roleServiceMock->expects($this->once())
907
            ->method('checkAssignmentAndFilterLimitationValues')
908
            ->with(24, $this->isInstanceOf(SPIRole::class), null)
909
            ->will($this->returnValue(null));
910
911
        $repository->expects($this->once())->method('beginTransaction');
912
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
913
        $userHandlerMock->expects($this->once())
914
            ->method('assignRole')
915
            ->with(
916
                $this->equalTo(24),
917
                $this->equalTo(42),
918
                $this->equalTo(null)
919
            )->will($this->throwException(new \Exception()));
920
        $repository->expects($this->once())->method('rollback');
921
922
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
923
        /* @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
924
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, null);
925
    }
926
927
    /**
928
     * @covers \eZ\Publish\Core\Repository\RoleService::removePolicyByRoleDraft
929
     */
930
    public function testRemovePolicyByRoleDraftThrowsUnauthorizedException()
931
    {
932
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
933
934
        $roleDraftMock = $this->createMock(RoleDraft::class);
935
        $roleDomainMapper = $this->createMock(RoleDomainMapper::class);
936
        $roleDomainMapper
937
            ->method('buildDomainRoleObject')
938
            ->willReturn($roleDraftMock);
939
940
        $roleServiceMock = $this->getPartlyMockedRoleService(null, [], $roleDomainMapper);
941
        $policyDraftMock = $this->createMock(PolicyDraft::class);
942
943
        $policyDraftMock->expects($this->any())
944
            ->method('__get')
945
            ->will(
946
                $this->returnValueMap(
947
                    [
948
                        ['roleId', 17],
949
                    ]
950
                )
951
            );
952
953
        $permissionResolverMock = $this->getPermissionResolverMock();
954
        $permissionResolverMock->expects($this->once())
955
            ->method('canUser')
956
            ->with(
957
                $this->equalTo('role'),
958
                $this->equalTo('update'),
959
                $this->equalTo($roleDraftMock)
960
            )->will($this->returnValue(false));
961
962
        /* @var \eZ\Publish\API\Repository\Values\User\Policy $policyMock */
963
        $roleServiceMock->removePolicyByRoleDraft($roleDraftMock, $policyDraftMock);
964
    }
965
966
    /**
967
     * Test for the removePolicyByRoleDraft() method.
968
     *
969
     * @covers \eZ\Publish\Core\Repository\RoleService::removePolicyByRoleDraft
970
     */
971
    public function testRemovePolicyByRoleDraftWithRollback()
972
    {
973
        $this->expectException(\Exception::class);
974
        $this->expectExceptionMessage('Handler threw an exception');
975
976
        $repository = $this->getRepositoryMock();
977
        $roleDraftMock = $this->createMock(RoleDraft::class);
978
        $roleDraftMock->expects($this->any())
979
            ->method('__get')
980
            ->with('id')
981
            ->willReturn(17);
982
983
        $roleDomainMapper = $this->createMock(RoleDomainMapper::class);
984
        $roleDomainMapper
985
            ->method('buildDomainRoleObject')
986
            ->willReturn($roleDraftMock);
987
        $roleServiceMock = $this->getPartlyMockedRoleService(null, [], $roleDomainMapper);
988
989
        $policyDraftMock = $this->createMock(PolicyDraft::class);
990
        $policyDraftMock->expects($this->any())
991
            ->method('__get')
992
            ->will(
993
                $this->returnValueMap(
994
                    [
995
                        ['id', 42],
996
                        ['roleId', 17],
997
                    ]
998
                )
999
            );
1000
1001
        $permissionResolverMock = $this->getPermissionResolverMock();
1002
        $permissionResolverMock->expects($this->once())
1003
            ->method('canUser')
1004
            ->with(
1005
                $this->equalTo('role'),
1006
                $this->equalTo('update'),
1007
                $this->equalTo($roleDraftMock)
1008
            )->will($this->returnValue(true));
1009
1010
        $repository->expects($this->once())->method('beginTransaction');
1011
1012
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
1013
1014
        $userHandlerMock->expects($this->once())
1015
            ->method('deletePolicy')
1016
            ->with(
1017
                $this->equalTo(42)
1018
            )->will($this->throwException(new \Exception('Handler threw an exception')));
1019
1020
        $repository->expects($this->once())->method('rollback');
1021
1022
        /* @var \eZ\Publish\API\Repository\Values\User\Policy $policyDraftMock */
1023
        $roleServiceMock->removePolicyByRoleDraft($roleDraftMock, $policyDraftMock);
0 ignored issues
show
Compatibility introduced by
$policyDraftMock 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...
1024
    }
1025
1026
    /**
1027
     * @covers \eZ\Publish\Core\Repository\RoleService::removePolicyByRoleDraft
1028
     */
1029
    public function testRemovePolicyByRoleDraft()
1030
    {
1031
        $repository = $this->getRepositoryMock();
1032
        $roleDraftMock = $this->createMock(RoleDraft::class);
1033
        $roleDraftMock
1034
            ->expects($this->any())
1035
            ->method('__get')
1036
            ->with('id')
1037
            ->willReturn(17);
1038
1039
        $roleDomainMapper = $this->createMock(RoleDomainMapper::class);
1040
        $roleDomainMapper
1041
            ->method('buildDomainRoleObject')
1042
            ->willReturn($roleDraftMock);
1043
1044
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRoleDraft'], [], $roleDomainMapper);
1045
1046
        $policyDraftMock = $this->createMock(PolicyDraft::class);
1047
        $policyDraftMock->expects($this->any())
1048
            ->method('__get')
1049
            ->will(
1050
                $this->returnValueMap(
1051
                    [
1052
                        ['id', 42],
1053
                        ['roleId', 17],
1054
                    ]
1055
                )
1056
            );
1057
1058
        $permissionResolverMock = $this->getPermissionResolverMock();
1059
        $permissionResolverMock->expects($this->once())
1060
            ->method('canUser')
1061
            ->with(
1062
                $this->equalTo('role'),
1063
                $this->equalTo('update'),
1064
                $this->equalTo($roleDraftMock)
1065
            )->will($this->returnValue(true));
1066
1067
        $repository->expects($this->once())->method('beginTransaction');
1068
1069
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
1070
1071
        $userHandlerMock->expects($this->once())
1072
            ->method('deletePolicy')
1073
            ->with(
1074
                $this->equalTo(42)
1075
            );
1076
1077
        $roleServiceMock->expects($this->once())
1078
            ->method('loadRoleDraft')
1079
            ->with($this->equalTo(17))
1080
            ->will($this->returnValue($roleDraftMock));
1081
1082
        $repository->expects($this->once())->method('commit');
1083
1084
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyDraft $policyDraftMock */
1085
        $roleServiceMock->removePolicyByRoleDraft($roleDraftMock, $policyDraftMock);
1086
    }
1087
1088
    /** @var \eZ\Publish\Core\Repository\RoleService */
1089
    protected $partlyMockedRoleService;
1090
1091
    /**
1092
     * Returns the role service to test with $methods mocked.
1093
     *
1094
     * Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()}
1095
     *
1096
     * @param string[] $methods
1097
     * @param array $settings
1098
     * @param \eZ\Publish\Core\Repository\Helper\RoleDomainMapper|null $roleDomainMapper
1099
     *
1100
     * @return \eZ\Publish\Core\Repository\RoleService|\PHPUnit\Framework\MockObject\MockObject
1101
     */
1102
    protected function getPartlyMockedRoleService(
1103
        array $methods = null,
1104
        array $settings = [],
1105
        ?RoleDomainMapper $roleDomainMapper = null
1106
    ) {
1107
        if (!isset($this->partlyMockedRoleService) || !empty($settings) || $roleDomainMapper) {
1108
            $limitationService = new LimitationService(
1109
                new ArrayIterator($settings['limitationTypes'] ?? [])
1110
            );
1111
            if ($roleDomainMapper === null) {
1112
                $roleDomainMapper = $this->getMockBuilder(RoleDomainMapper::class)
1113
                    ->setMethods([])
1114
                    ->setConstructorArgs([$limitationService])
1115
                    ->getMock();
1116
            }
1117
1118
            $this->partlyMockedRoleService = $this->getMockBuilder(RoleService::class)
1119
                ->setMethods($methods)
1120
                ->setConstructorArgs(
1121
                    [
1122
                        $this->getRepositoryMock(),
1123
                        $this->getPersistenceMockHandler('User\\Handler'),
1124
                        $limitationService,
1125
                        $roleDomainMapper,
1126
                        $settings,
1127
                    ]
1128
                )
1129
                ->getMock();
1130
        }
1131
1132
        return $this->partlyMockedRoleService;
1133
    }
1134
1135
    /**
1136
     * @return \eZ\Publish\API\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject
1137
     */
1138
    protected function getRepositoryMock(): Repository
1139
    {
1140
        $repositoryMock = parent::getRepositoryMock();
1141
        $repositoryMock
1142
            ->expects($this->any())
1143
            ->method('getPermissionResolver')
1144
            ->willReturn($this->getPermissionResolverMock());
1145
1146
        return $repositoryMock;
1147
    }
1148
}
1149