Completed
Push — master ( 83fb2b...0f847f )
by
unknown
42:12 queued 20:32
created

BaseLimitationIntegrationTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
cc 1
nc 1
nop 0
rs 10
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
declare(strict_types=1);
8
9
namespace eZ\Publish\API\Repository\Tests\Limitation\PermissionResolver;
10
11
use eZ\Publish\API\Repository\Tests\BaseTest;
12
use eZ\Publish\API\Repository\Values\ValueObject;
13
14
/**
15
 * Base class for all Limitation integration tests.
16
 */
17
abstract class BaseLimitationIntegrationTest extends BaseTest
18
{
19
    /**
20
     * @var \eZ\Publish\API\Repository\PermissionResolver
21
     */
22
    protected $permissionResolver;
23
24
    public function setUp(): void
25
    {
26
        $repository = $this->getRepository(false);
27
        $this->permissionResolver = $repository->getPermissionResolver();
28
    }
29
30
    /**
31
     * Map Limitations list to readable string for debugging purposes.
32
     *
33
     * @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations
34
     *
35
     * @return string
36
     */
37
    protected function getLimitationsListAsString(array $limitations): string
38
    {
39
        $str = '';
40
        foreach ($limitations as $limitation) {
41
            $str .= sprintf(
42
                '%s[%s]',
43
                get_class($limitation),
44
                implode(', ', $limitation->limitationValues)
45
            );
46
        }
47
48
        return $str;
49
    }
50
51
    /**
52
     * Create Editor user with the given Policy and Limitations and set it as current user.
53
     *
54
     * @param string $module
55
     * @param string $function
56
     * @param array $limitations
57
     *
58
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
59
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
60
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
61
     */
62
    protected function loginAsEditorUserWithLimitations(string $module, string $function, array $limitations = []): void
63
    {
64
        $user = $this->createUserWithPolicies(
65
            uniqid('editor'),
66
            [
67
                ['module' => $module, 'function' => $function, 'limitations' => $limitations],
68
            ]
69
        );
70
71
        $this->permissionResolver->setCurrentUserReference($user);
72
    }
73
74
    /**
75
     * @param bool $expectedResult
76
     * @param string $module
77
     * @param string $function
78
     * @param array $limitations
79
     * @param \eZ\Publish\API\Repository\Values\ValueObject $object
80
     * @param array $targets
81
     *
82
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
83
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
84
     */
85
    protected function assertCanUser(
86
        bool $expectedResult,
87
        string $module,
88
        string $function,
89
        array $limitations,
90
        ValueObject $object,
91
        array $targets = []
92
    ): void {
93
        self::assertEquals(
94
            $expectedResult,
95
            $this->permissionResolver->canUser($module, $function, $object, $targets),
96
            sprintf(
97
                'Failure for %s/%s with limitations: %s',
98
                $module,
99
                $function,
100
                $this->getLimitationsListAsString($limitations)
101
            )
102
        );
103
    }
104
}
105