Completed
Push — ezp25533-remove_http_cache_bun... ( 829bcd...807c99 )
by
unknown
98:53 queued 73:41
created

RoleContext::iDontSeeRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 * @version //autogentag//
6
 */
7
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
8
9
use Behat\Behat\Context\Context;
10
use PHPUnit_Framework_Assert as Assertion;
11
use EzSystems\PlatformBehatBundle\Context\RepositoryContext;
12
use eZ\Publish\API\Repository\Repository;
13
use eZ\Publish\API\Repository\RoleService;
14
use eZ\Publish\API\Repository\Exceptions as ApiExceptions;
15
16
/**
17
 * Sentences for Roles.
18
 */
19
class RoleContext implements Context
20
{
21
    use RepositoryContext;
22
23
    /**
24
     * @var \eZ\Publish\API\Repository\roleService
25
     */
26
    protected $roleService;
27
28
    /**
29
     * @injectService $repository @ezpublish.api.repository
30
     * @injectService $roleService @ezpublish.api.service.role
31
     */
32
    public function __construct(Repository $repository, RoleService $roleService)
33
    {
34
        $this->setRepository($repository);
35
        $this->roleService = $roleService;
36
    }
37
38
    /**
39
     * Make sure a Role with name $name exists in parent group.
40
     *
41
     * @param string $name Role identifier
42
     *
43
     * @return \eZ\Publish\API\Repository\Values\User\Role
44
     */
45
    public function ensureRoleExists($name)
46
    {
47
        try {
48
            $role = $this->roleService->loadRoleByIdentifier($name);
49
        } catch (ApiExceptions\NotFoundException $e) {
50
            $roleCreateStruct = $this->roleService->newRoleCreateStruct($name);
51
            $roleDraft = $this->roleService->createRole($roleCreateStruct);
52
            $this->roleService->publishRoleDraft($roleDraft);
53
            $role = $this->roleService->loadRole($roleDraft->id);
54
        }
55
56
        return $role;
57
    }
58
59
    /**
60
     * Fetches the role with identifier.
61
     *
62
     * @param string $identifier Role identifier
63
     *
64
     * @return \eZ\Publish\API\Repository\Values\User\Role
65
     */
66
    public function getRole($identifier)
67
    {
68
        $role = null;
69
        try {
70
            $role = $this->roleService->loadRoleByIdentifier($identifier);
71
        } catch (ApiExceptions\NotFoundException $e) {
72
            // Role not found, do nothing, returns null
73
        }
74
75
        return $role;
76
    }
77
78
    /**
79
     * @Given a/an :name role exists
80
     *
81
     * Ensures a role exists with name ':name', creating a new one if necessary.
82
     *
83
     * @return \eZ\Publish\API\Repository\Values\User\Role
84
     */
85
    public function iHaveRole($name)
86
    {
87
        return $this->ensureRoleExists($name);
88
    }
89
90
    /**
91
     * @Then I see that a/an :name role exists
92
     *
93
     * Verifies that a role with $name exists.
94
     */
95
    public function iSeeRole($name)
96
    {
97
        $role = $this->getRole($name);
98
        Assertion::assertNotNull(
99
            $role,
100
            "Couldn't find Role with name $name"
101
        );
102
    }
103
104
    /**
105
     * @Given :name do not have any assigned policies
106
     */
107
    public function noAssginedPolicies($name)
108
    {
109
        $role = $this->getRole($name);
110
        Assertion::assertNotNull(
111
            $role,
112
            "Couldn't find Role with name $name"
113
        );
114
        $policies = $role->getPolicies();
115
        Assertion::assertEmpty($policies, "Role $name has policies associated");
116
    }
117
118
    /**
119
     * @Given :name do not have any assigned Users and groups
120
     */
121
    public function noAssigneGroups($name)
122
    {
123
        $role = $this->getRole($name);
124
        Assertion::assertNotNull(
125
            $role,
126
            "Couldn't find Role with name $name"
127
        );
128
        $roleAssigments = $this->roleService->getRoleAssignments($role);
129
        Assertion::assertEmpty($roleAssigments, "Role $name has Users or groups associated");
130
    }
131
132
    /**
133
     * @Then I see that a/an :name role does not exists
134
     *
135
     * Verifies that a role with $name exists.
136
     */
137
    public function iDontSeeRole($name)
138
    {
139
        $role = $this->getRole($name);
140
        Assertion::assertNull(
141
            $role,
142
            "Found Role with name $name"
143
        );
144
    }
145
}
146