Completed
Push — ezp30878_cant_add_image_with_p... ( e19ea7...263f1b )
by
unknown
20:16
created

PermissionTest::createRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Repository\Tests\Service\Mock;
8
9
use eZ\Publish\API\Repository\Repository;
10
use eZ\Publish\Core\Repository\Permission\PermissionResolver;
11
use eZ\Publish\Core\Repository\Values\User\UserReference;
12
use eZ\Publish\Core\Base\Exceptions\NotFound\LimitationNotFoundException;
13
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
14
use eZ\Publish\SPI\Persistence\User\RoleAssignment;
15
use eZ\Publish\SPI\Persistence\User\Role;
16
use eZ\Publish\SPI\Persistence\User\Policy;
17
18
/**
19
 * Mock test case for PermissionResolver.
20
 *
21
 * @todo Move to "Tests/Permission/"
22
 */
23
class PermissionTest extends BaseServiceMockTest
24
{
25
    public function providerForTestHasAccessReturnsTrue()
26
    {
27
        return [
28
            [
29
                [
30
                    25 => $this->createRole(
31
                        [
32
                            ['dummy-module', 'dummy-function', 'dummy-limitation'],
33
                            ['dummy-module2', 'dummy-function2', 'dummy-limitation2'],
34
                        ],
35
                        25
36
                    ),
37
                    26 => $this->createRole(
38
                        [
39
                            ['*', 'dummy-function', 'dummy-limitation'],
40
                        ],
41
                        26
42
                    ),
43
                ],
44
                [
45
                    new RoleAssignment(
46
                        [
47
                            'roleId' => 25,
48
                        ]
49
                    ),
50
                    new RoleAssignment(
51
                        [
52
                            'roleId' => 26,
53
                        ]
54
                    ),
55
                ],
56
            ],
57
            [
58
                [
59
                    27 => $this->createRole(
60
                        [
61
                            ['test-module', '*', 'dummy-limitation'],
62
                        ],
63
                        27
64
                    ),
65
                ],
66
                [
67
                    new RoleAssignment(
68
                        [
69
                            'roleId' => 27,
70
                        ]
71
                    ),
72
                ],
73
            ],
74
            [
75
                [
76
                    28 => $this->createRole(
77
                        [
78
                            ['test-module', 'test-function', '*'],
79
                        ],
80
                        28
81
                    ),
82
                ],
83
                [
84
                    new RoleAssignment(
85
                        [
86
                            'roleId' => 28,
87
                        ]
88
                    ),
89
                ],
90
            ],
91
        ];
92
    }
93
94
    /**
95
     * Test for the hasAccess() method.
96
     *
97
     * @dataProvider providerForTestHasAccessReturnsTrue
98
     */
99 View Code Duplication
    public function testHasAccessReturnsTrue(array $roles, array $roleAssignments)
100
    {
101
        /** @var $userHandlerMock \PHPUnit_Framework_MockObject_MockObject */
102
        $userHandlerMock = $this->getPersistenceMock()->userHandler();
103
        $userReferenceMock = $this->getUserReferenceMock();
104
        $mockedService = $this->getPermissionResolverMock(null);
105
106
        $userReferenceMock
107
            ->expects($this->once())
108
            ->method('getUserId')
109
            ->will($this->returnValue(10));
110
111
        $userHandlerMock
112
            ->expects($this->once())
113
            ->method('loadRoleAssignmentsByGroupId')
114
            ->with($this->equalTo(10), $this->equalTo(true))
115
            ->will($this->returnValue($roleAssignments));
116
117
        foreach ($roleAssignments as $at => $roleAssignment) {
118
            $userHandlerMock
119
                ->expects($this->at($at + 1))
120
                ->method('loadRole')
121
                ->with($roleAssignment->roleId)
122
                ->will($this->returnValue($roles[$roleAssignment->roleId]));
123
        }
124
125
        $result = $mockedService->hasAccess('test-module', 'test-function');
126
127
        self::assertEquals(true, $result);
128
    }
129
130
    public function providerForTestHasAccessReturnsFalse()
131
    {
132
        return [
133
            [[], []],
134
            [
135
                [
136
                    29 => $this->createRole(
137
                        [
138
                            ['dummy-module', 'dummy-function', 'dummy-limitation'],
139
                        ],
140
                        29
141
                    ),
142
                ],
143
                [
144
                    new RoleAssignment(
145
                        [
146
                            'roleId' => 29,
147
                        ]
148
                    ),
149
                ],
150
            ],
151
            [
152
                [
153
                    30 => $this->createRole(
154
                        [
155
                            ['test-module', 'dummy-function', 'dummy-limitation'],
156
                        ],
157
                        30
158
                    ),
159
                ],
160
                [
161
                    new RoleAssignment(
162
                        [
163
                            'roleId' => 30,
164
                        ]
165
                    ),
166
                ],
167
            ],
168
        ];
169
    }
170
171
    /**
172
     * Test for the hasAccess() method.
173
     *
174
     * @dataProvider providerForTestHasAccessReturnsFalse
175
     */
176 View Code Duplication
    public function testHasAccessReturnsFalse(array $roles, array $roleAssignments)
177
    {
178
        /** @var $userHandlerMock \PHPUnit_Framework_MockObject_MockObject */
179
        $userHandlerMock = $this->getPersistenceMock()->userHandler();
180
        $userReferenceMock = $this->getUserReferenceMock();
181
        $service = $this->getPermissionResolverMock(null);
182
183
        $userReferenceMock
184
            ->expects($this->once())
185
            ->method('getUserId')
186
            ->will($this->returnValue(10));
187
188
        $userHandlerMock
189
            ->expects($this->once())
190
            ->method('loadRoleAssignmentsByGroupId')
191
            ->with($this->equalTo(10), $this->equalTo(true))
192
            ->will($this->returnValue($roleAssignments));
193
194
        foreach ($roleAssignments as $at => $roleAssignment) {
195
            $userHandlerMock
196
                ->expects($this->at($at + 1))
197
                ->method('loadRole')
198
                ->with($roleAssignment->roleId)
199
                ->will($this->returnValue($roles[$roleAssignment->roleId]));
200
        }
201
202
        $result = $service->hasAccess('test-module', 'test-function');
203
204
        self::assertEquals(false, $result);
205
    }
206
207
    /**
208
     * Test for the sudo() & hasAccess() method.
209
     */
210
    public function testHasAccessReturnsFalseButSudoSoTrue()
211
    {
212
        /** @var $userHandlerMock \PHPUnit_Framework_MockObject_MockObject */
213
        $userHandlerMock = $this->getPersistenceMock()->userHandler();
214
        $service = $this->getPermissionResolverMock(null);
215
        $repositoryMock = $this->getRepositoryMock();
216
        $repositoryMock
217
            ->expects($this->any())
218
            ->method('getPermissionResolver')
219
            ->will($this->returnValue($service));
220
221
        $userHandlerMock
222
            ->expects($this->never())
223
            ->method($this->anything());
224
225
        $result = $service->sudo(
226
            function (Repository $repo) {
227
                return $repo->hasAccess('test-module', 'test-function');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
228
            },
229
            $repositoryMock
230
        );
231
232
        self::assertEquals(true, $result);
233
    }
234
235
    /**
236
     * @return array
237
     */
238 View Code Duplication
    public function providerForTestHasAccessReturnsPermissionSets()
239
    {
240
        return [
241
            [
242
                [
243
                    31 => $this->createRole(
244
                        [
245
                            ['test-module', 'test-function', 'test-limitation'],
246
                        ],
247
                        31
248
                    ),
249
                ],
250
                [
251
                    new RoleAssignment(
252
                        [
253
                            'roleId' => 31,
254
                        ]
255
                    ),
256
                ],
257
            ],
258
            [
259
                [
260
                    31 => $this->createRole(
261
                        [
262
                            ['test-module', 'test-function', 'test-limitation'],
263
                        ],
264
                        31
265
                    ),
266
                    32 => $this->createRole(
267
                        [
268
                            ['test-module', 'test-function', 'test-limitation2'],
269
                        ],
270
                        32
271
                    ),
272
                ],
273
                [
274
                    new RoleAssignment(
275
                        [
276
                            'roleId' => 31,
277
                        ]
278
                    ),
279
                    new RoleAssignment(
280
                        [
281
                            'roleId' => 32,
282
                        ]
283
                    ),
284
                ],
285
            ],
286
        ];
287
    }
288
289
    /**
290
     * Test for the hasAccess() method.
291
     *
292
     * @dataProvider providerForTestHasAccessReturnsPermissionSets
293
     */
294
    public function testHasAccessReturnsPermissionSets(array $roles, array $roleAssignments)
295
    {
296
        /** @var $userHandlerMock \PHPUnit_Framework_MockObject_MockObject */
297
        $userHandlerMock = $this->getPersistenceMock()->userHandler();
298
        $roleDomainMapper = $this->getRoleDomainMapperMock(['buildDomainPolicyObject']);
299
        $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']);
300
301
        $permissionResolverMock
302
            ->expects($this->once())
303
            ->method('getCurrentUserReference')
304
            ->will($this->returnValue(new UserReference(14)));
305
306
        $userHandlerMock
307
            ->expects($this->once())
308
            ->method('loadRoleAssignmentsByGroupId')
309
            ->with($this->isType('integer'), $this->equalTo(true))
310
            ->will($this->returnValue($roleAssignments));
311
312
        foreach ($roleAssignments as $at => $roleAssignment) {
313
            $userHandlerMock
314
                ->expects($this->at($at + 1))
315
                ->method('loadRole')
316
                ->with($roleAssignment->roleId)
317
                ->will($this->returnValue($roles[$roleAssignment->roleId]));
318
        }
319
320
        $permissionSets = [];
321
        $count = 0;
322
        /* @var $roleAssignments \eZ\Publish\SPI\Persistence\User\RoleAssignment[] */
323
        foreach ($roleAssignments as $i => $roleAssignment) {
324
            $permissionSet = ['limitation' => null];
325
            foreach ($roles[$roleAssignment->roleId]->policies as $k => $policy) {
326
                $policyName = 'policy-' . $i . '-' . $k;
327
                $return = $this->returnValue($policyName);
328
                $permissionSet['policies'][] = $policyName;
329
330
                $roleDomainMapper
331
                    ->expects($this->at($count++))
332
                    ->method('buildDomainPolicyObject')
333
                    ->with($policy)
334
                    ->will($return);
335
            }
336
337
            if (!empty($permissionSet['policies'])) {
338
                $permissionSets[] = $permissionSet;
339
            }
340
        }
341
342
        /* @var $repositoryMock \eZ\Publish\Core\Repository\Repository */
343
        self::assertEquals(
344
            $permissionSets,
345
            $permissionResolverMock->hasAccess('test-module', 'test-function')
346
        );
347
    }
348
349
    /**
350
     * @return array
351
     */
352 View Code Duplication
    public function providerForTestHasAccessReturnsException()
353
    {
354
        return [
355
            [
356
                [
357
                    31 => $this->createRole(
358
                        [
359
                            ['test-module', 'test-function', 'notfound'],
360
                        ],
361
                        31
362
                    ),
363
                ],
364
                [
365
                    new RoleAssignment(
366
                        [
367
                            'roleId' => 31,
368
                        ]
369
                    ),
370
                ],
371
            ],
372
            [
373
                [
374
                    31 => $this->createRole(
375
                        [
376
                            ['test-module', 'test-function', 'test-limitation'],
377
                        ],
378
                        31
379
                    ),
380
                    32 => $this->createRole(
381
                        [
382
                            ['test-module', 'test-function', 'notfound'],
383
                        ],
384
                        32
385
                    ),
386
                ],
387
                [
388
                    new RoleAssignment(
389
                        [
390
                            'roleId' => 31,
391
                        ]
392
                    ),
393
                    new RoleAssignment(
394
                        [
395
                            'roleId' => 32,
396
                        ]
397
                    ),
398
                ],
399
            ],
400
        ];
401
    }
402
403
    /**
404
     * Test for the hasAccess() method.
405
     *
406
     * @dataProvider providerForTestHasAccessReturnsException
407
     * @expectedException \eZ\Publish\Core\Base\Exceptions\NotFound\LimitationNotFoundException
408
     */
409
    public function testHasAccessReturnsException(array $roles, array $roleAssignments)
410
    {
411
        /** @var $userHandlerMock \PHPUnit_Framework_MockObject_MockObject */
412
        $userHandlerMock = $this->getPersistenceMock()->userHandler();
413
        $roleDomainMapper = $this->getRoleDomainMapperMock();
414
        $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']);
415
416
        $permissionResolverMock
417
            ->expects($this->once())
418
            ->method('getCurrentUserReference')
419
            ->will($this->returnValue(new UserReference(14)));
420
421
        $userHandlerMock
422
            ->expects($this->once())
423
            ->method('loadRoleAssignmentsByGroupId')
424
            ->with($this->isType('integer'), $this->equalTo(true))
425
            ->will($this->returnValue($roleAssignments));
426
427
        foreach ($roleAssignments as $at => $roleAssignment) {
428
            $userHandlerMock
429
                ->expects($this->at($at + 1))
430
                ->method('loadRole')
431
                ->with($roleAssignment->roleId)
432
                ->will($this->returnValue($roles[$roleAssignment->roleId]));
433
        }
434
435
        $count = 0;
436
        /* @var $roleAssignments \eZ\Publish\SPI\Persistence\User\RoleAssignment[] */
437
        foreach ($roleAssignments as $i => $roleAssignment) {
438
            $permissionSet = ['limitation' => null];
439
            foreach ($roles[$roleAssignment->roleId]->policies as $k => $policy) {
440
                $policyName = 'policy-' . $i . '-' . $k;
441
                if ($policy->limitations === 'notfound') {
442
                    $return = $this->throwException(new LimitationNotFoundException('notfound'));
443
                } else {
444
                    $return = $this->returnValue($policyName);
445
                    $permissionSet['policies'][] = $policyName;
446
                }
447
448
                $roleDomainMapper
449
                    ->expects($this->at($count++))
450
                    ->method('buildDomainPolicyObject')
451
                    ->with($policy)
452
                    ->will($return);
453
454
                if ($policy->limitations === 'notfound') {
455
                    break 2; // no more execution after exception
456
                }
457
            }
458
        }
459
460
        $permissionResolverMock->hasAccess('test-module', 'test-function');
461
    }
462
463 View Code Duplication
    public function providerForTestHasAccessReturnsPermissionSetsWithRoleLimitation()
464
    {
465
        return [
466
            [
467
                [
468
                    32 => $this->createRole(
469
                        [
470
                            ['test-module', 'test-function', 'test-limitation'],
471
                        ],
472
                        32
473
                    ),
474
                ],
475
                [
476
                    new RoleAssignment(
477
                        [
478
                            'roleId' => 32,
479
                            'limitationIdentifier' => 'test-role-limitation',
480
                            'values' => ['test-role-limitation-value'],
481
                        ]
482
                    ),
483
                ],
484
            ],
485
            [
486
                [
487
                    33 => $this->createRole([['*', '*', '*']], 33),
488
                ],
489
                [
490
                    new RoleAssignment(
491
                        [
492
                            'roleId' => 33,
493
                            'limitationIdentifier' => 'test-role-limitation',
494
                            'values' => ['test-role-limitation-value'],
495
                        ]
496
                    ),
497
                ],
498
            ],
499
        ];
500
    }
501
502
    /**
503
     * Test for the hasAccess() method.
504
     *
505
     * @dataProvider providerForTestHasAccessReturnsPermissionSetsWithRoleLimitation
506
     */
507
    public function testHasAccessReturnsPermissionSetsWithRoleLimitation(array $roles, array $roleAssignments)
508
    {
509
        /** @var $userHandlerMock \PHPUnit_Framework_MockObject_MockObject */
510
        $userHandlerMock = $this->getPersistenceMock()->userHandler();
511
        $limitationTypeMock = $this->getMock('eZ\\Publish\\SPI\\Limitation\\Type');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
512
        $limitationService = $this->getLimitationServiceMock();
513
        $roleDomainMapper = $this->getRoleDomainMapperMock();
514
        $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']);
515
516
        $permissionResolverMock
517
            ->expects($this->once())
518
            ->method('getCurrentUserReference')
519
            ->will($this->returnValue(new UserReference(14)));
520
521
        $userHandlerMock
522
            ->expects($this->once())
523
            ->method('loadRoleAssignmentsByGroupId')
524
            ->with($this->isType('integer'), $this->equalTo(true))
525
            ->will($this->returnValue($roleAssignments));
526
527
        foreach ($roleAssignments as $at => $roleAssignment) {
528
            $userHandlerMock
529
                ->expects($this->at($at + 1))
530
                ->method('loadRole')
531
                ->with($roleAssignment->roleId)
532
                ->will($this->returnValue($roles[$roleAssignment->roleId]));
533
        }
534
535
        $permissionSets = [];
536
        /** @var $roleAssignments \eZ\Publish\SPI\Persistence\User\RoleAssignment[] */
537
        foreach ($roleAssignments as $i => $roleAssignment) {
538
            $permissionSet = [];
539
            foreach ($roles[$roleAssignment->roleId]->policies as $k => $policy) {
540
                $policyName = "policy-{$i}-{$k}";
541
                $permissionSet['policies'][] = $policyName;
542
                $roleDomainMapper
543
                    ->expects($this->at($k))
544
                    ->method('buildDomainPolicyObject')
545
                    ->with($policy)
546
                    ->will($this->returnValue($policyName));
547
            }
548
549
            $permissionSet['limitation'] = "limitation-{$i}";
550
            $limitationTypeMock
551
                ->expects($this->at($i))
552
                ->method('buildValue')
553
                ->with($roleAssignment->values)
554
                ->will($this->returnValue($permissionSet['limitation']));
555
            $limitationService
556
                ->expects($this->any())
557
                ->method('getLimitationType')
558
                ->with($roleAssignment->limitationIdentifier)
559
                ->will($this->returnValue($limitationTypeMock));
560
561
            $permissionSets[] = $permissionSet;
562
        }
563
564
        self::assertEquals(
565
            $permissionSets,
566
            $permissionResolverMock->hasAccess('test-module', 'test-function')
567
        );
568
    }
569
570
    /**
571
     * Returns Role stub.
572
     *
573
     * @param array $policiesData
574
     * @param mixed $roleId
575
     *
576
     * @return \eZ\Publish\SPI\Persistence\User\Role
577
     */
578
    private function createRole(array $policiesData, $roleId = null)
579
    {
580
        $policies = [];
581
        foreach ($policiesData as $policyData) {
582
            $policies[] = new Policy(
583
                [
584
                    'module' => $policyData[0],
585
                    'function' => $policyData[1],
586
                    'limitations' => $policyData[2],
587
                ]
588
            );
589
        }
590
591
        return new Role(
592
            [
593
                'id' => $roleId,
594
                'policies' => $policies,
595
            ]
596
        );
597
    }
598
599 View Code Duplication
    public function providerForTestCanUserSimple()
600
    {
601
        return [
602
            [true, true],
603
            [false, false],
604
            [[], false],
605
        ];
606
    }
607
608
    /**
609
     * Test for the canUser() method.
610
     *
611
     * Tests execution paths with permission sets equaling to boolean value or empty array.
612
     *
613
     * @dataProvider providerForTestCanUserSimple
614
     */
615 View Code Duplication
    public function testCanUserSimple($permissionSets, $result)
616
    {
617
        $permissionResolverMock = $this->getPermissionResolverMock(['hasAccess']);
618
619
        $permissionResolverMock
620
            ->expects($this->once())
621
            ->method('hasAccess')
622
            ->with($this->equalTo('test-module'), $this->equalTo('test-function'))
623
            ->will($this->returnValue($permissionSets));
624
625
        /** @var $valueObject \eZ\Publish\API\Repository\Values\ValueObject */
626
        $valueObject = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\ValueObject');
627
628
        self::assertEquals(
629
            $result,
630
            $permissionResolverMock->canUser('test-module', 'test-function', $valueObject, [$valueObject])
631
        );
632
    }
633
634
    /**
635
     * Test for the canUser() method.
636
     *
637
     * Tests execution path with permission set defining no limitations.
638
     */
639
    public function testCanUserWithoutLimitations()
640
    {
641
        $permissionResolverMock = $this->getPermissionResolverMock(
642
            [
643
                'hasAccess',
644
                'getCurrentUserReference',
645
            ]
646
        );
647
648
        $policyMock = $this->getMock(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
649
            'eZ\\Publish\\SPI\\Persistence\\User\\Policy',
650
            ['getLimitations'],
651
            [],
652
            '',
653
            false
654
        );
655
        $policyMock
656
            ->expects($this->once())
657
            ->method('getLimitations')
658
            ->will($this->returnValue('*'));
659
        $permissionSets = [
660
            [
661
                'limitation' => null,
662
                'policies' => [$policyMock],
663
            ],
664
        ];
665
        $permissionResolverMock
666
            ->expects($this->once())
667
            ->method('hasAccess')
668
            ->with($this->equalTo('test-module'), $this->equalTo('test-function'))
669
            ->will($this->returnValue($permissionSets));
670
671
        $permissionResolverMock
672
            ->expects($this->once())
673
            ->method('getCurrentUserReference')
674
            ->will($this->returnValue(new UserReference(14)));
675
676
        /** @var $valueObject \eZ\Publish\API\Repository\Values\ValueObject */
677
        $valueObject = $this->getMockForAbstractClass(
678
            'eZ\\Publish\\API\\Repository\\Values\\ValueObject'
679
        );
680
681
        self::assertTrue(
682
            $permissionResolverMock->canUser(
683
                'test-module',
684
                'test-function',
685
                $valueObject,
686
                [$valueObject]
687
            )
688
        );
689
    }
690
691
    /**
692
     * @return array
693
     */
694
    private function getPermissionSetsMock()
695
    {
696
        $roleLimitationMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\Limitation');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
697
        $roleLimitationMock
698
            ->expects($this->any())
699
            ->method('getIdentifier')
700
            ->will($this->returnValue('test-role-limitation-identifier'));
701
702
        $policyLimitationMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\Limitation');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
703
        $policyLimitationMock
704
            ->expects($this->any())
705
            ->method('getIdentifier')
706
            ->will($this->returnValue('test-policy-limitation-identifier'));
707
708
        $policyMock = $this->getMock(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
709
            'eZ\\Publish\\SPI\\Persistence\\User\\Policy',
710
            ['getLimitations'],
711
            [],
712
            '',
713
            false
714
        );
715
        $policyMock
716
            ->expects($this->any())
717
            ->method('getLimitations')
718
            ->will($this->returnValue([$policyLimitationMock, $policyLimitationMock]));
719
720
        $permissionSet = [
721
            'limitation' => clone $roleLimitationMock,
722
            'policies' => [$policyMock, $policyMock],
723
        ];
724
        $permissionSets = [$permissionSet, $permissionSet];
725
726
        return $permissionSets;
727
    }
728
729
    /**
730
     * Provides evaluation results for two permission sets, each with a role limitation and two policies,
731
     * with two limitations per policy.
732
     *
733
     * @return array
734
     */
735
    public function providerForTestCanUserComplex()
736
    {
737
        return [
738
            [
739
                [true, true],
740
                [
741
                    [
742
                        [true, true],
743
                        [true, true],
744
                    ],
745
                    [
746
                        [true, true],
747
                        [true, true],
748
                    ],
749
                ],
750
                true,
751
            ],
752
            [
753
                [false, false],
754
                [
755
                    [
756
                        [true, true],
757
                        [true, true],
758
                    ],
759
                    [
760
                        [true, true],
761
                        [true, true],
762
                    ],
763
                ],
764
                false,
765
            ],
766
            [
767
                [false, true],
768
                [
769
                    [
770
                        [true, true],
771
                        [true, true],
772
                    ],
773
                    [
774
                        [true, true],
775
                        [true, true],
776
                    ],
777
                ],
778
                true,
779
            ],
780
            [
781
                [false, true],
782
                [
783
                    [
784
                        [true, true],
785
                        [true, true],
786
                    ],
787
                    [
788
                        [true, false],
789
                        [true, true],
790
                    ],
791
                ],
792
                true,
793
            ],
794
            [
795
                [true, false],
796
                [
797
                    [
798
                        [true, false],
799
                        [false, true],
800
                    ],
801
                    [
802
                        [true, true],
803
                        [true, true],
804
                    ],
805
                ],
806
                false,
807
            ],
808
        ];
809
    }
810
811
    /**
812
     * Test for the canUser() method.
813
     *
814
     * Tests execution paths with permission sets containing limitations.
815
     *
816
     * @dataProvider providerForTestCanUserComplex
817
     */
818
    public function testCanUserComplex(array $roleLimitationEvaluations, array $policyLimitationEvaluations, $userCan)
819
    {
820
        /** @var $valueObject \eZ\Publish\API\Repository\Values\ValueObject */
821
        $valueObject = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\ValueObject');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
822
        $limitationServiceMock = $this->getLimitationServiceMock();
823
        $permissionResolverMock = $this->getPermissionResolverMock(
824
            [
825
                'hasAccess',
826
                'getCurrentUserReference',
827
            ]
828
        );
829
830
        $permissionSets = $this->getPermissionSetsMock();
831
        $permissionResolverMock
832
            ->expects($this->once())
833
            ->method('hasAccess')
834
            ->with($this->equalTo('test-module'), $this->equalTo('test-function'))
835
            ->will($this->returnValue($permissionSets));
836
837
        $userRef = new UserReference(14);
838
        $permissionResolverMock
839
            ->expects($this->once())
840
            ->method('getCurrentUserReference')
841
            ->will($this->returnValue(new UserReference(14)));
842
843
        $invocation = 0;
844
        for ($i = 0; $i < count($permissionSets); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
845
            $limitation = $this->getMock('eZ\\Publish\\SPI\\Limitation\\Type');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
846
            $limitation
847
                ->expects($this->once())
848
                ->method('evaluate')
849
                ->with($permissionSets[$i]['limitation'], $userRef, $valueObject, [$valueObject])
850
                ->will($this->returnValue($roleLimitationEvaluations[$i]));
851
            $limitationServiceMock
852
                ->expects($this->at($invocation++))
853
                ->method('getLimitationType')
854
                ->with('test-role-limitation-identifier')
855
                ->will($this->returnValue($limitation));
856
857
            if (!$roleLimitationEvaluations[$i]) {
858
                continue;
859
            }
860
861
            for ($j = 0; $j < count($permissionSets[$i]['policies']); ++$j) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
862
                /** @var $policy \eZ\Publish\API\Repository\Values\User\Policy */
863
                $policy = $permissionSets[$i]['policies'][$j];
864
                $limitations = $policy->getLimitations();
865
                for ($k = 0; $k < count($limitations); ++$k) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
866
                    $limitationsPass = true;
867
                    $limitation = $this->getMock('eZ\\Publish\\SPI\\Limitation\\Type');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
868
                    $limitation
869
                        ->expects($this->once())
870
                        ->method('evaluate')
871
                        ->with($limitations[$k], $userRef, $valueObject, [$valueObject])
872
                        ->will($this->returnValue($policyLimitationEvaluations[$i][$j][$k]));
873
                    $limitationServiceMock
874
                        ->expects($this->at($invocation++))
875
                        ->method('getLimitationType')
876
                        ->with('test-policy-limitation-identifier')
877
                        ->will($this->returnValue($limitation));
878
879
                    if (!$policyLimitationEvaluations[$i][$j][$k]) {
880
                        $limitationsPass = false;
881
                        break;
882
                    }
883
                }
884
885
                /** @var $limitationsPass */
886
                if ($limitationsPass) {
0 ignored issues
show
Bug introduced by
The variable $limitationsPass does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
887
                    break 2;
888
                }
889
            }
890
        }
891
892
        self::assertEquals(
893
            $userCan,
894
            $permissionResolverMock->canUser(
895
                'test-module',
896
                'test-function',
897
                $valueObject,
898
                [$valueObject]
899
            )
900
        );
901
    }
902
903
    /**
904
     * Test for the setCurrentUserReference() and getCurrentUserReference() methods.
905
     */
906
    public function testSetAndGetCurrentUserReference()
907
    {
908
        $permissionResolverMock = $this->getPermissionResolverMock(null);
909
        $userReferenceMock = $this->getUserReferenceMock();
910
911
        $userReferenceMock
912
            ->expects($this->once())
913
            ->method('getUserId')
914
            ->will($this->returnValue(42));
915
916
        $permissionResolverMock->setCurrentUserReference($userReferenceMock);
917
918
        self::assertSame(
919
            $userReferenceMock,
920
            $permissionResolverMock->getCurrentUserReference()
921
        );
922
    }
923
924
    /**
925
     * Test for the getCurrentUserReference() method.
926
     */
927
    public function testGetCurrentUserReferenceReturnsAnonymousUser()
928
    {
929
        $permissionResolverMock = $this->getPermissionResolverMock(null);
930
        $userReferenceMock = $this->getUserReferenceMock();
931
932
        self::assertSame(
933
            $userReferenceMock,
934
            $permissionResolverMock->getCurrentUserReference()
935
        );
936
    }
937
938
    protected $permissionResolverMock;
939
940
    /**
941
     * @return \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit_Framework_MockObject_MockObject
942
     */
943
    protected function getPermissionResolverMock($methods = [])
944
    {
945
        if ($this->permissionResolverMock === null) {
946
            $this->permissionResolverMock = $this
947
                ->getMockBuilder(PermissionResolver::class)
948
                ->setMethods($methods)
949
                ->setConstructorArgs(
950
                    [
951
                        $this->getRoleDomainMapperMock(),
952
                        $this->getLimitationServiceMock(),
953
                        $this->getPersistenceMock()->userHandler(),
954
                        $this->getUserReferenceMock(),
955
                    ]
956
                )
957
                ->getMock();
958
        }
959
960
        return $this->permissionResolverMock;
961
    }
962
963
    protected $userReferenceMock;
964
965
    protected function getUserReferenceMock()
966
    {
967
        if ($this->userReferenceMock === null) {
968
            $this->userReferenceMock = $this
969
                ->getMockBuilder('eZ\Publish\API\Repository\Values\User\UserReference')
970
                ->getMock();
971
        }
972
973
        return $this->userReferenceMock;
974
    }
975
976
    protected $repositoryMock;
977
978
    /**
979
     * @return \eZ\Publish\API\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
980
     */
981
    protected function getRepositoryMock($methods = [])
0 ignored issues
show
Unused Code introduced by
The parameter $methods is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
982
    {
983
        if ($this->repositoryMock === null) {
984
            $this->repositoryMock = $this
985
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Repository')
986
                ->setMethods(['getPermissionResolver'])
987
                ->disableOriginalConstructor()
988
                ->getMock();
989
        }
990
991
        return $this->repositoryMock;
992
    }
993
994
    protected $roleDomainMapperMock;
995
996
    /**
997
     * @return \eZ\Publish\Core\Repository\Helper\RoleDomainMapper|\PHPUnit_Framework_MockObject_MockObject
998
     */
999
    protected function getRoleDomainMapperMock($methods = [])
1000
    {
1001
        if ($this->roleDomainMapperMock === null) {
1002
            $this->roleDomainMapperMock = $this
1003
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\RoleDomainMapper')
1004
                ->setMethods($methods)
1005
                ->disableOriginalConstructor()
1006
                ->getMock();
1007
        }
1008
1009
        return $this->roleDomainMapperMock;
1010
    }
1011
1012
    protected $limitationServiceMock;
1013
1014
    /**
1015
     * @return \eZ\Publish\Core\Repository\Helper\LimitationService|\PHPUnit_Framework_MockObject_MockObject
1016
     */
1017 View Code Duplication
    protected function getLimitationServiceMock($methods = [])
1018
    {
1019
        if ($this->limitationServiceMock === null) {
1020
            $this->limitationServiceMock = $this
1021
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\LimitationService')
1022
                ->setMethods($methods)
1023
                ->disableOriginalConstructor()
1024
                ->getMock();
1025
        }
1026
1027
        return $this->limitationServiceMock;
1028
    }
1029
}
1030