Completed
Push — ezp-30928-as_a_developer_i_wan... ( 0570c5 )
by
unknown
13:48
created

testRemovePolicyByRoleDraftWithRollback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 54
rs 9.0036
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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