Completed
Push — 7.0 ( acf4a7...ccc80a )
by André
12:25
created

RoleTest::testRemovePolicyWithRollback()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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