Completed
Push — EZP-30808 ( b0c1fa...68b9c2 )
by
unknown
49:57 queued 28:53
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
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
42
     */
43
    public function testCreateRoleThrowsLimitationValidationException()
44
    {
45
        $limitationMock = $this->createMock(RoleLimitation::class);
46
        $limitationTypeMock = $this->createMock(SPIType::class);
47
48
        $limitationMock->expects($this->any())
49
            ->method('getIdentifier')
50
            ->will($this->returnValue('mockIdentifier'));
51
52
        $limitationTypeMock->expects($this->once())
53
            ->method('acceptValue')
54
            ->with($this->equalTo($limitationMock));
55
        $limitationTypeMock->expects($this->once())
56
            ->method('validate')
57
            ->with($this->equalTo($limitationMock))
58
            ->will($this->returnValue([42]));
59
60
        $settings = [
61
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
62
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
63
        ];
64
65
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRoleByIdentifier'], $settings);
66
67
        /** @var \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStructMock */
68
        $roleCreateStructMock = $this->createMock(RoleCreateStruct::class);
69
        $policyCreateStructMock = $this->createMock(PolicyCreateStruct::class);
70
71
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
72
        $policyCreateStructMock->module = 'mockModule';
73
        $policyCreateStructMock->function = 'mockFunction';
74
        $roleCreateStructMock->identifier = 'mockIdentifier';
75
        $roleServiceMock->expects($this->once())
76
            ->method('loadRoleByIdentifier')
77
            ->with($this->equalTo('mockIdentifier'))
78
            ->will($this->throwException(new NotFoundException('Role', 'mockIdentifier')));
79
80
        /* @var \PHPUnit\Framework\MockObject\MockObject $roleCreateStructMock */
81
        $roleCreateStructMock->expects($this->once())
82
            ->method('getPolicies')
83
            ->will($this->returnValue([$policyCreateStructMock]));
84
85
        /* @var \PHPUnit\Framework\MockObject\MockObject $policyCreateStructMock */
86
        $policyCreateStructMock->expects($this->once())
87
            ->method('getLimitations')
88
            ->will($this->returnValue([$limitationMock]));
89
90
        $permissionResolverMock = $this->getPermissionResolverMock();
91
        $permissionResolverMock->expects($this->once())
92
            ->method('canUser')
93
            ->with(
94
                $this->equalTo('role'),
95
                $this->equalTo('create'),
96
                $this->equalTo($roleCreateStructMock)
97
            )->will($this->returnValue(true));
98
99
        /* @var \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStructMock */
100
        $roleServiceMock->createRole($roleCreateStructMock);
101
    }
102
103
    /**
104
     * Test for the addPolicy() method.
105
     *
106
     * @covers \eZ\Publish\Core\Repository\RoleService::addPolicy
107
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitations
108
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
109
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
110
     */
111
    public function testAddPolicyThrowsLimitationValidationException()
112
    {
113
        $limitationMock = $this->createMock(RoleLimitation::class);
114
        $limitationTypeMock = $this->createMock(SPIType::class);
115
116
        $limitationTypeMock->expects($this->once())
117
            ->method('acceptValue')
118
            ->with($this->equalTo($limitationMock));
119
        $limitationTypeMock->expects($this->once())
120
            ->method('validate')
121
            ->with($this->equalTo($limitationMock))
122
            ->will($this->returnValue([42]));
123
124
        $limitationMock->expects($this->any())
125
            ->method('getIdentifier')
126
            ->will($this->returnValue('mockIdentifier'));
127
128
        $settings = [
129
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
130
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
131
        ];
132
133
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRole'], $settings);
134
135
        $roleMock = $this->createMock(Role::class);
136
        $policyCreateStructMock = $this->createMock(PolicyCreateStruct::class);
137
138
        $roleMock->expects($this->any())
139
            ->method('__get')
140
            ->with('id')
141
            ->will($this->returnValue(42));
142
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
143
        $policyCreateStructMock->module = 'mockModule';
144
        $policyCreateStructMock->function = 'mockFunction';
145
146
        $roleServiceMock->expects($this->once())
147
            ->method('loadRole')
148
            ->with($this->equalTo(42))
149
            ->will($this->returnValue($roleMock));
150
151
        /* @var \PHPUnit\Framework\MockObject\MockObject $policyCreateStructMock */
152
        $policyCreateStructMock->expects($this->once())
153
            ->method('getLimitations')
154
            ->will($this->returnValue([$limitationMock]));
155
156
        $permissionResolverMock = $this->getPermissionResolverMock();
157
        $permissionResolverMock->expects($this->once())
158
            ->method('canUser')
159
            ->with(
160
                $this->equalTo('role'),
161
                $this->equalTo('update'),
162
                $this->equalTo($roleMock)
163
            )->will($this->returnValue(true));
164
165
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
166
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
167
        $roleServiceMock->addPolicy($roleMock, $policyCreateStructMock);
168
    }
169
170
    /**
171
     * Test for the updatePolicy() method.
172
     *
173
     * @covers \eZ\Publish\Core\Repository\RoleService::updatePolicy
174
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitations
175
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
176
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
177
     */
178
    public function testUpdatePolicyThrowsLimitationValidationException()
179
    {
180
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
181
        $userHandlerMock->expects($this->once())
182
            ->method('loadRole')
183
            ->with($this->equalTo(17))
184
            ->will($this->returnValue(new SPIRole(['id' => 17])));
185
186
        $limitationMock = $this->createMock(RoleLimitation::class);
187
        $limitationTypeMock = $this->createMock(SPIType::class);
188
189
        $limitationTypeMock->expects($this->once())
190
            ->method('acceptValue')
191
            ->with($this->equalTo($limitationMock));
192
        $limitationTypeMock->expects($this->once())
193
            ->method('validate')
194
            ->with($this->equalTo($limitationMock))
195
            ->will($this->returnValue([42]));
196
197
        $limitationMock->expects($this->any())
198
            ->method('getIdentifier')
199
            ->will($this->returnValue('mockIdentifier'));
200
201
        $settings = [
202
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
203
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
204
        ];
205
206
        $roleMock = $this->createMock(Role::class);
207
        $roleDomainMapper = $this->createMock(RoleDomainMapper::class);
208
        $roleDomainMapper
209
            ->method('buildDomainRoleObject')
210
            ->willReturn($roleMock);
211
212
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRole'], $settings, $roleDomainMapper);
213
214
        $policyMock = $this->createMock(Policy::class);
215
        $policyUpdateStructMock = $this->createMock(PolicyUpdateStruct::class);
216
217
        $policyMock->expects($this->any())
218
            ->method('__get')
219
            ->will(
220
                $this->returnCallback(
221
                    function ($propertyName) {
222
                        switch ($propertyName) {
223
                            case 'module':
224
                                return 'mockModule';
225
                            case 'function':
226
                                return 'mockFunction';
227
                            case 'roleId':
228
                                return 17;
229
                        }
230
231
                        return null;
232
                    }
233
                )
234
            );
235
236
        /* @var \PHPUnit\Framework\MockObject\MockObject $policyCreateStructMock */
237
        $policyUpdateStructMock->expects($this->once())
238
            ->method('getLimitations')
239
            ->will($this->returnValue([$limitationMock]));
240
241
        $permissionResolverMock = $this->getPermissionResolverMock();
242
        $permissionResolverMock->expects($this->once())
243
            ->method('canUser')
244
            ->with(
245
                $this->equalTo('role'),
246
                $this->equalTo('update'),
247
                $this->equalTo($roleMock)
248
            )->will($this->returnValue(true));
249
250
        /* @var \eZ\Publish\API\Repository\Values\User\Policy $policyMock */
251
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStructMock */
252
        $roleServiceMock->updatePolicy($policyMock, $policyUpdateStructMock);
253
    }
254
255
    /**
256
     * Test for the assignRoleToUser() method.
257
     *
258
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
259
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
260
     */
261
    public function testAssignRoleToUserThrowsUnauthorizedException()
262
    {
263
        $roleServiceMock = $this->getPartlyMockedRoleService();
264
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
265
        $roleMock = $this->createMock(Role::class);
266
        /** @var \eZ\Publish\API\Repository\Values\User\User $userMock */
267
        $userMock = $this->createMock(User::class);
268
269
        $permissionResolverMock = $this->getPermissionResolverMock();
270
        $permissionResolverMock->expects($this->once())
271
            ->method('canUser')
272
            ->with(
273
                $this->equalTo('role'),
274
                $this->equalTo('assign'),
275
                $this->equalTo($userMock),
276
                $this->equalTo([$roleMock])
277
            )->will($this->returnValue(false));
278
279
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, null);
280
    }
281
282
    /**
283
     * Test for the assignRoleToUser() method.
284
     *
285
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
286
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
287
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
288
     */
289
    public function testAssignRoleToUserThrowsLimitationValidationException()
290
    {
291
        $limitationMock = $this->createMock(RoleLimitation::class);
292
        $limitationTypeMock = $this->createMock(SPIType::class);
293
294
        $limitationTypeMock->expects($this->once())
295
            ->method('acceptValue')
296
            ->with($this->equalTo($limitationMock));
297
        $limitationTypeMock->expects($this->once())
298
            ->method('validate')
299
            ->with($this->equalTo($limitationMock))
300
            ->will($this->returnValue([42]));
301
302
        $limitationMock->expects($this->once())
303
            ->method('getIdentifier')
304
            ->will($this->returnValue('testIdentifier'));
305
306
        $settings = [
307
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
308
        ];
309
310
        $roleServiceMock = $this->getPartlyMockedRoleService(null, $settings);
311
312
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
313
        $roleMock = $this->createMock(Role::class);
314
        /** @var \eZ\Publish\API\Repository\Values\User\User $userMock */
315
        $userMock = $this->createMock(User::class);
316
317
        $permissionResolverMock = $this->getPermissionResolverMock();
318
        $permissionResolverMock->expects($this->once())
319
            ->method('canUser')
320
            ->with(
321
                $this->equalTo('role'),
322
                $this->equalTo('assign'),
323
                $this->equalTo($userMock),
324
                $this->equalTo([$roleMock])
325
            )->will($this->returnValue(true));
326
327
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
328
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, $limitationMock);
329
    }
330
331
    /**
332
     * Test for the assignRoleToUser() method.
333
     *
334
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
335
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
336
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
337
     */
338
    public function testAssignRoleToUserThrowsBadStateException()
339
    {
340
        $roleServiceMock = $this->getPartlyMockedRoleService();
341
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
342
        $roleMock = $this->createMock(Role::class);
343
        /** @var \eZ\Publish\API\Repository\Values\User\User $userMock */
344
        $userMock = $this->createMock(User::class);
345
        $limitationMock = $this->createMock(RoleLimitation::class);
346
347
        $limitationMock->expects($this->once())
348
            ->method('getIdentifier')
349
            ->will($this->returnValue('testIdentifier'));
350
351
        $permissionResolverMock = $this->getPermissionResolverMock();
352
        $permissionResolverMock->expects($this->once())
353
            ->method('canUser')
354
            ->with(
355
                $this->equalTo('role'),
356
                $this->equalTo('assign'),
357
                $this->equalTo($userMock),
358
                $this->equalTo([$roleMock])
359
            )->will($this->returnValue(true));
360
361
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
362
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, $limitationMock);
363
    }
364
365
    /**
366
     * Test for the assignRoleToUser() method.
367
     *
368
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
369
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
370
     */
371
    public function testAssignRoleToUser()
372
    {
373
        $limitationMock = $this->createMock(RoleLimitation::class);
374
        $limitationTypeMock = $this->createMock(SPIType::class);
375
376
        $limitationTypeMock->expects($this->once())
377
            ->method('acceptValue')
378
            ->with($this->equalTo($limitationMock));
379
        $limitationTypeMock->expects($this->once())
380
            ->method('validate')
381
            ->with($this->equalTo($limitationMock))
382
            ->will($this->returnValue([]));
383
384
        $limitationMock->expects($this->exactly(2))
385
            ->method('getIdentifier')
386
            ->will($this->returnValue('testIdentifier'));
387
388
        $settings = [
389
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
390
        ];
391
392
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues'], $settings);
393
394
        $repository = $this->getRepositoryMock();
395
        $roleMock = $this->createMock(Role::class);
396
        $userMock = $this->createMock(User::class);
397
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
398
399
        $userMock->expects($this->any())
400
            ->method('__get')
401
            ->with('id')
402
            ->will($this->returnValue(24));
403
404
        $permissionResolverMock = $this->getPermissionResolverMock();
405
        $permissionResolverMock->expects($this->once())
406
            ->method('canUser')
407
            ->with(
408
                $this->equalTo('role'),
409
                $this->equalTo('assign'),
410
                $this->equalTo($userMock),
411
                $this->equalTo([$roleMock])
412
            )->will($this->returnValue(true));
413
414
        $roleMock->expects($this->any())
415
            ->method('__get')
416
            ->with('id')
417
            ->will($this->returnValue(42));
418
419
        $userHandlerMock->expects($this->once())
420
            ->method('loadRole')
421
            ->with($this->equalTo(42))
422
            ->will($this->returnValue(new SPIRole(['id' => 42])));
423
424
        $userHandlerMock->expects($this->once())
425
            ->method('load')
426
            ->with($this->equalTo(24))
427
            ->will($this->returnValue(new SPIUser(['id' => 24])));
428
429
        $roleServiceMock->expects($this->once())
430
            ->method('checkAssignmentAndFilterLimitationValues')
431
            ->with(24, $this->isInstanceOf(SPIRole::class), ['testIdentifier' => []])
432
            ->will($this->returnValue(['testIdentifier' => []]));
433
434
        $repository->expects($this->once())->method('beginTransaction');
435
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
436
        $userHandlerMock->expects($this->once())
437
            ->method('assignRole')
438
            ->with(
439
                $this->equalTo(24),
440
                $this->equalTo(42),
441
                $this->equalTo(['testIdentifier' => []])
442
            );
443
        $repository->expects($this->once())->method('commit');
444
445
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
446
        /* @var \eZ\Publish\API\Repository\Values\User\User $userMock */
447
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
448
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, $limitationMock);
449
    }
450
451
    /**
452
     * Test for the assignRoleToUser() method.
453
     *
454
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
455
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
456
     */
457
    public function testAssignRoleToUserWithNullLimitation()
458
    {
459
        $repository = $this->getRepositoryMock();
460
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
461
        $roleMock = $this->createMock(Role::class);
462
        $userMock = $this->createMock(User::class);
463
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
464
465
        $userMock->expects($this->any())
466
            ->method('__get')
467
            ->with('id')
468
            ->will($this->returnValue(24));
469
470
        $permissionResolverMock = $this->getPermissionResolverMock();
471
        $permissionResolverMock->expects($this->once())
472
            ->method('canUser')
473
            ->with(
474
                $this->equalTo('role'),
475
                $this->equalTo('assign'),
476
                $this->equalTo($userMock),
477
                $this->equalTo([$roleMock])
478
            )->will($this->returnValue(true));
479
480
        $roleMock->expects($this->any())
481
            ->method('__get')
482
            ->with('id')
483
            ->will($this->returnValue(42));
484
485
        $userHandlerMock->expects($this->once())
486
            ->method('loadRole')
487
            ->with($this->equalTo(42))
488
            ->will($this->returnValue(new SPIRole(['id' => 42])));
489
490
        $userHandlerMock->expects($this->once())
491
            ->method('load')
492
            ->with($this->equalTo(24))
493
            ->will($this->returnValue(new SPIUser(['id' => 24])));
494
495
        $roleServiceMock->expects($this->once())
496
            ->method('checkAssignmentAndFilterLimitationValues')
497
            ->with(24, $this->isInstanceOf(SPIRole::class), null)
498
            ->will($this->returnValue(null));
499
500
        $repository->expects($this->once())->method('beginTransaction');
501
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
502
        $userHandlerMock->expects($this->once())
503
            ->method('assignRole')
504
            ->with(
505
                $this->equalTo(24),
506
                $this->equalTo(42),
507
                $this->equalTo(null)
508
            );
509
        $repository->expects($this->once())->method('commit');
510
511
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
512
        /* @var \eZ\Publish\API\Repository\Values\User\User $userMock */
513
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, null);
514
    }
515
516
    /**
517
     * Test for the assignRoleToUser() method.
518
     *
519
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
520
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
521
     * @expectedException \Exception
522
     */
523
    public function testAssignRoleToUserWithRollback()
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
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
587
     */
588
    public function testAssignRoleToUserGroupThrowsUnauthorizedException()
589
    {
590
        $repository = $this->getRepositoryMock();
0 ignored issues
show
Unused Code introduced by
$repository is not used, you could remove the assignment.

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

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

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

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

Loading history...
591
        $roleServiceMock = $this->getPartlyMockedRoleService();
592
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
593
        $roleMock = $this->createMock(Role::class);
594
        /** @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
595
        $userGroupMock = $this->createMock(UserGroup::class);
596
597
        $permissionResolverMock = $this->getPermissionResolverMock();
598
        $permissionResolverMock->expects($this->once())
599
            ->method('canUser')
600
            ->with(
601
                $this->equalTo('role'),
602
                $this->equalTo('assign'),
603
                $this->equalTo($userGroupMock),
604
                $this->equalTo([$roleMock])
605
            )->will($this->returnValue(false));
606
607
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, null);
608
    }
609
610
    /**
611
     * Test for the assignRoleToUserGroup() method.
612
     *
613
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
614
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
615
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
616
     */
617
    public function testAssignRoleToUserGroupThrowsLimitationValidationException()
618
    {
619
        $limitationMock = $this->createMock(RoleLimitation::class);
620
        $limitationTypeMock = $this->createMock(SPIType::class);
621
622
        $limitationTypeMock->expects($this->once())
623
            ->method('acceptValue')
624
            ->with($this->equalTo($limitationMock));
625
        $limitationTypeMock->expects($this->once())
626
            ->method('validate')
627
            ->with($this->equalTo($limitationMock))
628
            ->will($this->returnValue([42]));
629
630
        $limitationMock->expects($this->once())
631
            ->method('getIdentifier')
632
            ->will($this->returnValue('testIdentifier'));
633
634
        $settings = [
635
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
636
        ];
637
638
        $roleServiceMock = $this->getPartlyMockedRoleService(null, $settings);
639
640
        $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...
641
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
642
        $roleMock = $this->createMock(Role::class);
643
        /** @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
644
        $userGroupMock = $this->createMock(UserGroup::class);
645
646
        $permissionResolverMock = $this->getPermissionResolverMock();
647
        $permissionResolverMock->expects($this->once())
648
            ->method('canUser')
649
            ->with(
650
                $this->equalTo('role'),
651
                $this->equalTo('assign'),
652
                $this->equalTo($userGroupMock),
653
                $this->equalTo([$roleMock])
654
            )->will($this->returnValue(true));
655
656
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
657
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, $limitationMock);
658
    }
659
660
    /**
661
     * Test for the assignRoleToUserGroup() method.
662
     *
663
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
664
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
665
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
666
     */
667
    public function testAssignRoleGroupToUserThrowsBadStateException()
668
    {
669
        $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...
670
        $roleServiceMock = $this->getPartlyMockedRoleService();
671
        /** @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
672
        $roleMock = $this->createMock(Role::class);
673
        /** @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
674
        $userGroupMock = $this->createMock(UserGroup::class);
675
        $limitationMock = $this->createMock(RoleLimitation::class);
676
677
        $limitationMock->expects($this->once())
678
            ->method('getIdentifier')
679
            ->will($this->returnValue('testIdentifier'));
680
681
        $permissionResolverMock = $this->getPermissionResolverMock();
682
        $permissionResolverMock->expects($this->once())
683
            ->method('canUser')
684
            ->with(
685
                $this->equalTo('role'),
686
                $this->equalTo('assign'),
687
                $this->equalTo($userGroupMock),
688
                $this->equalTo([$roleMock])
689
            )->will($this->returnValue(true));
690
691
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
692
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, $limitationMock);
693
    }
694
695
    /**
696
     * Test for the assignRoleToUserGroup() method.
697
     *
698
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
699
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
700
     */
701
    public function testAssignRoleToUserGroup()
702
    {
703
        $limitationMock = $this->createMock(RoleLimitation::class);
704
        $limitationTypeMock = $this->createMock(SPIType::class);
705
706
        $limitationTypeMock->expects($this->once())
707
            ->method('acceptValue')
708
            ->with($this->equalTo($limitationMock));
709
        $limitationTypeMock->expects($this->once())
710
            ->method('validate')
711
            ->with($this->equalTo($limitationMock))
712
            ->will($this->returnValue([]));
713
714
        $limitationMock->expects($this->exactly(2))
715
            ->method('getIdentifier')
716
            ->will($this->returnValue('testIdentifier'));
717
718
        $settings = [
719
            'limitationTypes' => ['testIdentifier' => $limitationTypeMock],
720
        ];
721
722
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues'], $settings);
723
724
        $repository = $this->getRepositoryMock();
725
        $roleMock = $this->createMock(Role::class);
726
        $userGroupMock = $this->createMock(UserGroup::class);
727
        $userServiceMock = $this->createMock(UserService::class);
728
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
729
730
        $repository->expects($this->once())
731
            ->method('getUserService')
732
            ->will($this->returnValue($userServiceMock));
733
        $userGroupMock->expects($this->any())
734
            ->method('__get')
735
            ->with('id')
736
            ->will($this->returnValue(24));
737
738
        $permissionResolverMock = $this->getPermissionResolverMock();
739
        $permissionResolverMock->expects($this->once())
740
            ->method('canUser')
741
            ->with(
742
                $this->equalTo('role'),
743
                $this->equalTo('assign'),
744
                $this->equalTo($userGroupMock),
745
                $this->equalTo([$roleMock])
746
            )->will($this->returnValue(true));
747
748
        $roleMock->expects($this->any())
749
            ->method('__get')
750
            ->with('id')
751
            ->will($this->returnValue(42));
752
753
        $userHandlerMock->expects($this->once())
754
            ->method('loadRole')
755
            ->with($this->equalTo(42))
756
            ->will($this->returnValue(new SPIRole(['id' => 42])));
757
758
        $userServiceMock->expects($this->once())
759
            ->method('loadUserGroup')
760
            ->with($this->equalTo(24))
761
            ->will($this->returnValue($userGroupMock));
762
763
        $roleServiceMock->expects($this->once())
764
            ->method('checkAssignmentAndFilterLimitationValues')
765
            ->with(24, $this->isInstanceOf(SPIRole::class), ['testIdentifier' => []])
766
            ->will($this->returnValue(['testIdentifier' => []]));
767
768
        $repository->expects($this->once())->method('beginTransaction');
769
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
770
        $userHandlerMock->expects($this->once())
771
            ->method('assignRole')
772
            ->with(
773
                $this->equalTo(24),
774
                $this->equalTo(42),
775
                $this->equalTo(['testIdentifier' => []])
776
            );
777
        $repository->expects($this->once())->method('commit');
778
779
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
780
        /* @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
781
        /* @var \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $limitationMock */
782
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, $limitationMock);
783
    }
784
785
    /**
786
     * Test for the assignRoleToUserGroup() method.
787
     *
788
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
789
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
790
     */
791
    public function testAssignRoleToUserGroupWithNullLimitation()
792
    {
793
        $repository = $this->getRepositoryMock();
794
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
795
        $roleMock = $this->createMock(Role::class);
796
        $userGroupMock = $this->createMock(UserGroup::class);
797
        $userServiceMock = $this->createMock(UserService::class);
798
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
799
800
        $repository->expects($this->once())
801
            ->method('getUserService')
802
            ->will($this->returnValue($userServiceMock));
803
        $userGroupMock->expects($this->any())
804
            ->method('__get')
805
            ->with('id')
806
            ->will($this->returnValue(24));
807
808
        $permissionResolverMock = $this->getPermissionResolverMock();
809
        $permissionResolverMock->expects($this->once())
810
            ->method('canUser')
811
            ->with(
812
                $this->equalTo('role'),
813
                $this->equalTo('assign'),
814
                $this->equalTo($userGroupMock),
815
                $this->equalTo([$roleMock])
816
            )->will($this->returnValue(true));
817
818
        $roleMock->expects($this->any())
819
            ->method('__get')
820
            ->with('id')
821
            ->will($this->returnValue(42));
822
823
        $userHandlerMock->expects($this->once())
824
            ->method('loadRole')
825
            ->with($this->equalTo(42))
826
            ->will($this->returnValue(new SPIRole(['id' => 42])));
827
828
        $userServiceMock->expects($this->once())
829
            ->method('loadUserGroup')
830
            ->with($this->equalTo(24))
831
            ->will($this->returnValue($userGroupMock));
832
833
        $roleServiceMock->expects($this->once())
834
            ->method('checkAssignmentAndFilterLimitationValues')
835
            ->with(24, $this->isInstanceOf(SPIRole::class), null)
836
            ->will($this->returnValue(null));
837
838
        $repository->expects($this->once())->method('beginTransaction');
839
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
840
        $userHandlerMock->expects($this->once())
841
            ->method('assignRole')
842
            ->with(
843
                $this->equalTo(24),
844
                $this->equalTo(42),
845
                $this->equalTo(null)
846
            );
847
        $repository->expects($this->once())->method('commit');
848
849
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
850
        /* @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
851
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, null);
852
    }
853
854
    /**
855
     * Test for the assignRoleToUserGroup() method.
856
     *
857
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUserGroup
858
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
859
     * @expectedException \Exception
860
     */
861
    public function testAssignRoleToUserGroupWithRollback()
862
    {
863
        $repository = $this->getRepositoryMock();
864
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
865
        $roleMock = $this->createMock(Role::class);
866
        $userGroupMock = $this->createMock(UserGroup::class);
867
        $userServiceMock = $this->createMock(UserService::class);
868
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
869
870
        $repository->expects($this->once())
871
            ->method('getUserService')
872
            ->will($this->returnValue($userServiceMock));
873
        $userGroupMock->expects($this->any())
874
            ->method('__get')
875
            ->with('id')
876
            ->will($this->returnValue(24));
877
878
        $permissionResolverMock = $this->getPermissionResolverMock();
879
        $permissionResolverMock->expects($this->once())
880
            ->method('canUser')
881
            ->with(
882
                $this->equalTo('role'),
883
                $this->equalTo('assign'),
884
                $this->equalTo($userGroupMock),
885
                $this->equalTo([$roleMock])
886
            )->will($this->returnValue(true));
887
888
        $roleMock->expects($this->any())
889
            ->method('__get')
890
            ->with('id')
891
            ->will($this->returnValue(42));
892
893
        $userHandlerMock->expects($this->once())
894
            ->method('loadRole')
895
            ->with($this->equalTo(42))
896
            ->will($this->returnValue(new SPIRole(['id' => 42])));
897
898
        $userServiceMock->expects($this->once())
899
            ->method('loadUserGroup')
900
            ->with($this->equalTo(24))
901
            ->will($this->returnValue($userGroupMock));
902
903
        $roleServiceMock->expects($this->once())
904
            ->method('checkAssignmentAndFilterLimitationValues')
905
            ->with(24, $this->isInstanceOf(SPIRole::class), null)
906
            ->will($this->returnValue(null));
907
908
        $repository->expects($this->once())->method('beginTransaction');
909
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
910
        $userHandlerMock->expects($this->once())
911
            ->method('assignRole')
912
            ->with(
913
                $this->equalTo(24),
914
                $this->equalTo(42),
915
                $this->equalTo(null)
916
            )->will($this->throwException(new \Exception()));
917
        $repository->expects($this->once())->method('rollback');
918
919
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
920
        /* @var \eZ\Publish\API\Repository\Values\User\UserGroup $userGroupMock */
921
        $roleServiceMock->assignRoleToUserGroup($roleMock, $userGroupMock, null);
922
    }
923
924
    /**
925
     * Test for the deletePolicy() method.
926
     *
927
     * @covers \eZ\Publish\Core\Repository\RoleService::deletePolicy
928
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
929
     */
930
    public function testDeletePolicyThrowsUnauthorizedException()
931
    {
932
        $roleMock = $this->createMock(Role::class);
933
        $roleDomainMapper = $this->createMock(RoleDomainMapper::class);
934
        $roleDomainMapper
935
            ->method('buildDomainRoleObject')
936
            ->willReturn($roleMock);
937
938
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRole'], [], $roleDomainMapper);
939
        $policyMock = $this->createMock(Policy::class);
940
        $policyMock->expects($this->any())
941
            ->method('__get')
942
            ->will(
943
                $this->returnValueMap(
944
                    [
945
                        ['roleId', 17],
946
                    ]
947
                )
948
            );
949
950
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
951
        $userHandlerMock->expects($this->once())
952
            ->method('loadRole')
953
            ->with($this->equalTo(17))
954
            ->will($this->returnValue(new SPIRole(['id' => 17])));
955
956
        $permissionResolverMock = $this->getPermissionResolverMock();
957
        $permissionResolverMock->expects($this->once())
958
            ->method('canUser')
959
            ->with(
960
                $this->equalTo('role'),
961
                $this->equalTo('update'),
962
                $this->equalTo($roleMock)
963
            )->will($this->returnValue(false));
964
965
        /* @var \eZ\Publish\API\Repository\Values\User\Policy $policyMock */
966
        $roleServiceMock->deletePolicy($policyMock);
967
    }
968
969
    /**
970
     * Test for the deletePolicy() method.
971
     *
972
     * @covers \eZ\Publish\Core\Repository\RoleService::deletePolicy
973
     * @expectedException \Exception
974
     * @expectedExceptionMessage Handler threw an exception
975
     */
976
    public function testDeletePolicyWithRollback()
977
    {
978
        $repository = $this->getRepositoryMock();
979
        $roleMock = $this->createMock(Role::class);
980
        $roleDomainMapper = $this->createMock(RoleDomainMapper::class);
981
        $roleDomainMapper
982
            ->method('buildDomainRoleObject')
983
            ->willReturn($roleMock);
984
        $roleServiceMock = $this->getPartlyMockedRoleService(null, [], $roleDomainMapper);
985
986
        $policyMock = $this->createMock(Policy::class);
987
        $policyMock->expects($this->any())
988
            ->method('__get')
989
            ->will(
990
                $this->returnValueMap(
991
                    [
992
                        ['id', 42],
993
                        ['roleId', 17],
994
                    ]
995
                )
996
            );
997
998
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
999
        $userHandlerMock->expects($this->once())
1000
            ->method('loadRole')
1001
            ->with($this->equalTo(17))
1002
            ->will($this->returnValue(new SPIRole(['id' => 17])));
1003
1004
        $permissionResolverMock = $this->getPermissionResolverMock();
1005
        $permissionResolverMock->expects($this->once())
1006
            ->method('canUser')
1007
            ->with(
1008
                $this->equalTo('role'),
1009
                $this->equalTo('update'),
1010
                $this->equalTo($roleMock)
1011
            )->will($this->returnValue(true));
1012
1013
        $repository->expects($this->once())->method('beginTransaction');
1014
1015
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
1016
1017
        $userHandlerMock->expects($this->once())
1018
            ->method('deletePolicy')
1019
            ->with(
1020
                $this->equalTo(42)
1021
            )->will($this->throwException(new \Exception('Handler threw an exception')));
1022
1023
        $repository->expects($this->once())->method('rollback');
1024
1025
        /* @var \eZ\Publish\API\Repository\Values\User\Policy $policyMock */
1026
        $roleServiceMock->deletePolicy($policyMock);
1027
    }
1028
1029
    /**
1030
     * Test for the deletePolicy() method.
1031
     *
1032
     * @covers \eZ\Publish\Core\Repository\RoleService::deletePolicy
1033
     */
1034
    public function testDeletePolicy() /// FIX THIS
1035
    {
1036
        $repository = $this->getRepositoryMock();
1037
        $roleMock = $this->createMock(Role::class);
1038
        $roleDomainMapper = $this->createMock(RoleDomainMapper::class);
1039
        $roleDomainMapper
1040
            ->method('buildDomainRoleObject')
1041
            ->willReturn($roleMock);
1042
1043
        $roleServiceMock = $this->getPartlyMockedRoleService(null, [], $roleDomainMapper);
1044
1045
        $policyMock = $this->createMock(Policy::class);
1046
        $policyMock->expects($this->any())
1047
            ->method('__get')
1048
            ->will(
1049
                $this->returnValueMap(
1050
                    [
1051
                        ['id', 42],
1052
                        ['roleId', 17],
1053
                    ]
1054
                )
1055
            );
1056
1057
        $permissionResolverMock = $this->getPermissionResolverMock();
1058
        $permissionResolverMock->expects($this->once())
1059
            ->method('canUser')
1060
            ->with(
1061
                $this->equalTo('role'),
1062
                $this->equalTo('update'),
1063
                $this->equalTo($roleMock)
1064
            )->will($this->returnValue(true));
1065
1066
        $repository->expects($this->once())->method('beginTransaction');
1067
1068
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
1069
1070
        $userHandlerMock->expects($this->once())
1071
            ->method('deletePolicy')
1072
            ->with(
1073
                $this->equalTo(42)
1074
            );
1075
1076
        $userHandlerMock->expects($this->once())
1077
            ->method('loadRole')
1078
            ->with($this->equalTo(17))
1079
            ->will($this->returnValue(new SPIRole(['id' => 17])));
1080
1081
        $repository->expects($this->once())->method('commit');
1082
1083
        /* @var \eZ\Publish\API\Repository\Values\User\Policy $policyMock */
1084
        $roleServiceMock->deletePolicy($policyMock);
1085
    }
1086
1087
    /** @var \eZ\Publish\Core\Repository\RoleService */
1088
    protected $partlyMockedRoleService;
1089
1090
    /**
1091
     * Returns the role service to test with $methods mocked.
1092
     *
1093
     * Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()}
1094
     *
1095
     * @param string[] $methods
1096
     * @param array $settings
1097
     * @param \eZ\Publish\Core\Repository\Helper\RoleDomainMapper|null $roleDomainMapper
1098
     *
1099
     * @return \eZ\Publish\Core\Repository\RoleService|\PHPUnit\Framework\MockObject\MockObject
1100
     */
1101
    protected function getPartlyMockedRoleService(
1102
        array $methods = null,
1103
        array $settings = [],
1104
        ?RoleDomainMapper $roleDomainMapper = null
1105
    ) {
1106
        if (!isset($this->partlyMockedRoleService) || !empty($settings) || $roleDomainMapper) {
1107
            $limitationService = $this->getPartlyMockedLimitationService($methods, $settings);
1108
            if ($roleDomainMapper === null) {
1109
                $roleDomainMapper = $this->getMockBuilder(RoleDomainMapper::class)
1110
                    ->setMethods([])
1111
                    ->setConstructorArgs([$limitationService])
1112
                    ->getMock();
1113
            }
1114
1115
            $this->partlyMockedRoleService = $this->getMockBuilder(RoleService::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\e... $settings))->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\Repository\RoleService> of property $partlyMockedRoleService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1116
                ->setMethods($methods)
1117
                ->setConstructorArgs(
1118
                    [
1119
                        $this->getRepositoryMock(),
1120
                        $this->getPersistenceMockHandler('User\\Handler'),
1121
                        $limitationService,
1122
                        $roleDomainMapper,
1123
                        $settings,
1124
                    ]
1125
                )
1126
                ->getMock();
1127
        }
1128
1129
        return $this->partlyMockedRoleService;
1130
    }
1131
1132
    /** @var \eZ\Publish\Core\Repository\RoleService */
1133
    protected $partlyMockedLimitationService;
1134
1135
    /**
1136
     * Return mocked LimitationService.
1137
     *
1138
     * @param string[] $methods
1139
     * @param array $settings
1140
     *
1141
     * @return \eZ\Publish\Core\Repository\Helper\LimitationService|\PHPUnit\Framework\MockObject\MockObject
1142
     */
1143
    protected function getPartlyMockedLimitationService(array $methods = null, array $settings = [])
1144
    {
1145
        if (!isset($this->partlyMockedLimitationService) || !empty($settings)) {
1146
            $this->partlyMockedLimitationService = $this->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\LimitationService')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('e...($settings))->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\Repository\RoleService> of property $partlyMockedLimitationService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1147
                ->setMethods($methods)
1148
                ->setConstructorArgs(
1149
                    [
1150
                        $settings,
1151
                    ]
1152
                )
1153
                ->getMock();
1154
        }
1155
1156
        return $this->partlyMockedLimitationService;
1157
    }
1158
1159
    /**
1160
     * @return \eZ\Publish\API\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject
1161
     */
1162
    protected function getRepositoryMock(): Repository
1163
    {
1164
        $repositoryMock = parent::getRepositoryMock();
1165
        $repositoryMock
1166
            ->expects($this->any())
1167
            ->method('getPermissionResolver')
1168
            ->willReturn($this->getPermissionResolverMock());
1169
1170
        return $repositoryMock;
1171
    }
1172
}
1173