Completed
Push — readme ( 4da83f...7adc69 )
by
unknown
28:00 queued 04:49
created

RoleTest::testRemovePolicyByRoleDraft()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 58
rs 8.9163
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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