Completed
Push — ezp_30827 ( c6ab75...809fcf )
by
unknown
14:52
created

RoleTest::getRepositoryMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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