Completed
Push — master ( 7c0e42...8c347f )
by André
14:17
created

RoleServiceTest::testAssignRoleToUserGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 8

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 21
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RoleServiceTest 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\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Values\User\Limitation;
12
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
13
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
14
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation;
15
use eZ\Publish\API\Repository\Values\User\Policy;
16
use eZ\Publish\API\Repository\Values\User\Role;
17
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18
use Exception;
19
20
/**
21
 * Test case for operations in the RoleService using in memory storage.
22
 *
23
 * The following IDs from the default eZ community edition database are used in
24
 * this test:
25
 *
26
 * <ul>
27
 *   <li>
28
 *     ContentType
29
 *     <ul>
30
 *       <li><strong>28</strong>: File</li>
31
 *       <li><strong>29</strong>: Flash</li>
32
 *       <li><strong>30</strong>: Image</li>
33
 *     </ul>
34
 *   </li>
35
 * <ul>
36
 *
37
 * @see eZ\Publish\API\Repository\RoleService
38
 * @group role
39
 */
40
class RoleServiceTest extends BaseTest
41
{
42
    /**
43
     * Test for the newRoleCreateStruct() method.
44
     *
45
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
46
     */
47
    public function testNewRoleCreateStruct()
48
    {
49
        $repository = $this->getRepository();
50
51
        $roleService = $repository->getRoleService();
52
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
53
54
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleCreateStruct', $roleCreate);
55
    }
56
57
    /**
58
     * Test for the newRoleCreateStruct() method.
59
     *
60
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
61
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
62
     */
63
    public function testNewRoleCreateStructSetsNamePropertyOnStruct()
64
    {
65
        $repository = $this->getRepository();
66
67
        /* BEGIN: Use Case */
68
69
        $roleService = $repository->getRoleService();
70
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
71
72
        /* END: Use Case */
73
74
        $this->assertEquals('roleName', $roleCreate->identifier);
75
    }
76
77
    /**
78
     * Test for the createRole() method.
79
     *
80
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
81
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
82
     */
83
    public function testCreateRole()
84
    {
85
        $repository = $this->getRepository();
86
87
        /* BEGIN: Use Case */
88
89
        $roleService = $repository->getRoleService();
90
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
91
92
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
93
        // $roleCreate->mainLanguageCode = 'eng-US';
94
95
        $role = $roleService->createRole($roleCreate);
96
97
        /* END: Use Case */
98
99
        $this->assertInstanceOf(
100
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
101
            $role
102
        );
103
104
        return [
105
            'createStruct' => $roleCreate,
106
            'role' => $role,
107
        ];
108
    }
109
110
    /**
111
     * Test for the createRole() method.
112
     *
113
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
114
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
115
     */
116
    public function testRoleCreateStructValues(array $data)
117
    {
118
        $createStruct = $data['createStruct'];
119
        $role = $data['role'];
120
121
        $this->assertEquals(
122
            [
123
                'identifier' => $createStruct->identifier,
124
                'policies' => $createStruct->policies,
125
            ],
126
            [
127
                'identifier' => $role->identifier,
128
                'policies' => $role->policies,
129
            ]
130
        );
131
        $this->assertNotNull($role->id);
132
133
        return $data;
134
    }
135
136
    /**
137
     * Test for the createRole() method.
138
     *
139
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
140
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
141
     */
142
    public function testCreateRoleWithPolicy()
143
    {
144
        $repository = $this->getRepository();
145
146
        /* BEGIN: Use Case */
147
148
        $roleService = $repository->getRoleService();
149
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
150
151
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
152
        // $roleCreate->mainLanguageCode = 'eng-US';
153
154
        // Create new subtree limitation
155
        $limitation = new SubtreeLimitation(
156
            array(
157
                'limitationValues' => array('/1/2/'),
158
            )
159
        );
160
161
        // Create policy create struct and add limitation to it
162
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'read');
163
        $policyCreate->addLimitation($limitation);
164
165
        // Add policy create struct to role create struct
166
        $roleCreate->addPolicy($policyCreate);
167
168
        $role = $roleService->createRole($roleCreate);
169
170
        /* END: Use Case */
171
172
        $this->assertInstanceOf(
173
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
174
            $role
175
        );
176
177
        return [
178
            'createStruct' => $roleCreate,
179
            'role' => $role,
180
        ];
181
    }
182
183
    /**
184
     * Test for the createRole() method.
185
     *
186
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
187
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithPolicy
188
     */
189
    public function testRoleCreateStructValuesWithPolicy(array $data)
190
    {
191
        $createStruct = $data['createStruct'];
192
        $role = $data['role'];
193
194
        $this->assertEquals(
195
            [
196
                'identifier' => $createStruct->identifier,
197
                'policy_module' => $createStruct->policies[0]->module,
198
                'policy_function' => $createStruct->policies[0]->function,
199
                'policy_limitation' => array_values($createStruct->policies[0]->limitations),
200
            ],
201
            [
202
                'identifier' => $role->identifier,
203
                'policy_module' => $role->policies[0]->module,
204
                'policy_function' => $role->policies[0]->function,
205
                'policy_limitation' => array_values($role->policies[0]->limitations),
206
            ]
207
        );
208
        $this->assertNotNull($role->id);
209
210
        return $data;
211
    }
212
213
    /**
214
     * Test creating a role with multiple policies.
215
     *
216
     * @covers \eZ\Publish\API\Repository\RoleService::createRole
217
     */
218
    public function testCreateRoleWithMultiplePolicies()
219
    {
220
        $repository = $this->getRepository();
221
        $roleService = $repository->getRoleService();
222
223
        $limitation1 = new Limitation\ContentTypeLimitation();
224
        $limitation1->limitationValues = ['1', '3', '13'];
225
226
        $limitation2 = new Limitation\SectionLimitation();
227
        $limitation2->limitationValues = ['2', '3'];
228
229
        $limitation3 = new Limitation\OwnerLimitation();
230
        $limitation3->limitationValues = ['1', '2'];
231
232
        $limitation4 = new Limitation\UserGroupLimitation();
233
        $limitation4->limitationValues = ['1'];
234
235
        $policyCreateStruct1 = $roleService->newPolicyCreateStruct('content', 'read');
236
        $policyCreateStruct1->addLimitation($limitation1);
237
        $policyCreateStruct1->addLimitation($limitation2);
238
239
        $policyCreateStruct2 = $roleService->newPolicyCreateStruct('content', 'edit');
240
        $policyCreateStruct2->addLimitation($limitation3);
241
        $policyCreateStruct2->addLimitation($limitation4);
242
243
        $roleCreateStruct = $roleService->newRoleCreateStruct('ultimate_permissions');
244
        $roleCreateStruct->addPolicy($policyCreateStruct1);
245
        $roleCreateStruct->addPolicy($policyCreateStruct2);
246
247
        $createdRole = $roleService->createRole($roleCreateStruct);
248
249
        self::assertInstanceOf(Role::class, $createdRole);
250
        self::assertGreaterThan(0, $createdRole->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
251
252
        $this->assertPropertiesCorrect(
253
            [
254
                'identifier' => $roleCreateStruct->identifier,
255
            ],
256
            $createdRole
257
        );
258
259
        self::assertCount(2, $createdRole->getPolicies());
260
261
        foreach ($createdRole->getPolicies() as $policy) {
262
            self::assertInstanceOf(Policy::class, $policy);
263
            self::assertGreaterThan(0, $policy->id);
264
            self::assertEquals($createdRole->id, $policy->roleId);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
265
266
            self::assertCount(2, $policy->getLimitations());
267
268
            foreach ($policy->getLimitations() as $limitation) {
269
                self::assertInstanceOf(Limitation::class, $limitation);
270
271
                if ($policy->module == 'content' && $policy->function == 'read') {
272 View Code Duplication
                    switch ($limitation->getIdentifier()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
                        case Limitation::CONTENTTYPE:
274
                            self::assertEquals($limitation1->limitationValues, $limitation->limitationValues);
275
                            break;
276
277
                        case Limitation::SECTION:
278
                            self::assertEquals($limitation2->limitationValues, $limitation->limitationValues);
279
                            break;
280
281
                        default:
282
                            self::fail('Created role contains limitations not defined with create struct');
283
                    }
284
                } elseif ($policy->module == 'content' && $policy->function == 'edit') {
285 View Code Duplication
                    switch ($limitation->getIdentifier()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
286
                        case Limitation::OWNER:
287
                            self::assertEquals($limitation3->limitationValues, $limitation->limitationValues);
288
                            break;
289
290
                        case Limitation::USERGROUP:
291
                            self::assertEquals($limitation4->limitationValues, $limitation->limitationValues);
292
                            break;
293
294
                        default:
295
                            self::fail('Created role contains limitations not defined with create struct');
296
                    }
297
                } else {
298
                    self::fail('Created role contains policy not defined with create struct');
299
                }
300
            }
301
        }
302
    }
303
304
    /**
305
     * Test for the createRoleDraft() method.
306
     *
307
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
308
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
309
     */
310 View Code Duplication
    public function testCreateRoleDraft()
311
    {
312
        $repository = $this->getRepository();
313
314
        /* BEGIN: Use Case */
315
316
        $roleService = $repository->getRoleService();
317
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
318
319
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
320
        // $roleCreate->mainLanguageCode = 'eng-US';
321
322
        $roleDraft = $roleService->createRole($roleCreate);
323
        $roleService->publishRoleDraft($roleDraft);
324
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
325
        $newRoleDraft = $roleService->createRoleDraft($role);
326
327
        /* END: Use Case */
328
329
        $this->assertInstanceOf(
330
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
331
            $newRoleDraft
332
        );
333
    }
334
335
    /**
336
     * Test for the createRole() method.
337
     *
338
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
339
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
340
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
341
     */
342
    public function testCreateRoleThrowsInvalidArgumentException()
343
    {
344
        $repository = $this->getRepository();
345
346
        /* BEGIN: Use Case */
347
348
        $roleService = $repository->getRoleService();
349
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
350
351
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
352
        // $roleCreate->mainLanguageCode = 'eng-US';
353
354
        // This call will fail with an InvalidArgumentException, because Editor exists
355
        $roleService->createRole($roleCreate);
356
357
        /* END: Use Case */
358
    }
359
360
    /**
361
     * Test for the createRoleDraft() method.
362
     *
363
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
364
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
365
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
366
     */
367 View Code Duplication
    public function testCreateRoleDraftThrowsInvalidArgumentException()
368
    {
369
        $repository = $this->getRepository();
370
371
        /* BEGIN: Use Case */
372
373
        $roleService = $repository->getRoleService();
374
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
375
376
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
377
        // $roleCreate->mainLanguageCode = 'eng-US';
378
379
        $roleDraft = $roleService->createRole($roleCreate);
380
        $roleService->publishRoleDraft($roleDraft);
381
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
382
        $roleService->createRoleDraft($role); // First role draft
383
384
        // This call will fail with an InvalidArgumentException, because there is already a draft
385
        $roleService->createRoleDraft($role);
386
387
        /* END: Use Case */
388
    }
389
390
    /**
391
     * Test for the createRole() method.
392
     *
393
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
394
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
395
     */
396 View Code Duplication
    public function testCreateRoleThrowsLimitationValidationException()
397
    {
398
        $repository = $this->getRepository();
399
400
        /* BEGIN: Use Case */
401
        $roleService = $repository->getRoleService();
402
403
        // Create new role create struct
404
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
405
406
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
407
        // $roleCreate->mainLanguageCode = 'eng-US';
408
409
        // Create new subtree limitation
410
        $limitation = new SubtreeLimitation(
411
            array(
412
                'limitationValues' => array('/mountain/forest/tree/42/'),
413
            )
414
        );
415
416
        // Create policy create struct and add limitation to it
417
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
418
        $policyCreate->addLimitation($limitation);
419
420
        // Add policy create struct to role create struct
421
        $roleCreate->addPolicy($policyCreate);
422
423
        // This call will fail with an LimitationValidationException, because subtree
424
        // "/mountain/forest/tree/42/" does not exist
425
        $roleService->createRole($roleCreate);
426
        /* END: Use Case */
427
    }
428
429
    /**
430
     * Test for the createRole() method.
431
     *
432
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
433
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
434
     */
435
    public function testCreateRoleInTransactionWithRollback()
436
    {
437
        $repository = $this->getRepository();
438
439
        /* BEGIN: Use Case */
440
441
        $roleService = $repository->getRoleService();
442
443
        $repository->beginTransaction();
444
445
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
446
447
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
448
        // $roleCreate->mainLanguageCode = 'eng-US';
449
450
        $createdRoleId = $roleService->createRole($roleCreate)->id;
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
451
452
        $repository->rollback();
453
454
        try {
455
            // This call will fail with a "NotFoundException"
456
            $role = $roleService->loadRole($createdRoleId);
0 ignored issues
show
Unused Code introduced by
$role is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
457
        } catch (NotFoundException $e) {
458
            return;
459
        }
460
        /* END: Use Case */
461
462
        $this->fail('Role object still exists after rollback.');
463
    }
464
465
    /**
466
     * Test for the createRoleDraft() method.
467
     *
468
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
469
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
470
     */
471
    public function testCreateRoleDraftInTransactionWithRollback()
472
    {
473
        $repository = $this->getRepository();
474
475
        /* BEGIN: Use Case */
476
477
        $roleService = $repository->getRoleService();
478
479
        $repository->beginTransaction();
480
481
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
482
483
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
484
        // $roleCreate->mainLanguageCode = 'eng-US';
485
486
        $createdRoleId = $roleService->createRole($roleCreate)->id;
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
487
488
        $repository->rollback();
489
490
        try {
491
            // This call will fail with a "NotFoundException"
492
            $role = $roleService->loadRoleDraft($createdRoleId);
0 ignored issues
show
Unused Code introduced by
$role is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
493
        } catch (NotFoundException $e) {
494
            return;
495
        }
496
        /* END: Use Case */
497
498
        $this->fail('Role draft object still exists after rollback.');
499
    }
500
501
    /**
502
     * Test for the loadRole() method.
503
     *
504
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
505
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
506
     */
507
    public function testLoadRole()
508
    {
509
        $repository = $this->getRepository();
510
511
        /* BEGIN: Use Case */
512
513
        $roleService = $repository->getRoleService();
514
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
515
516
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
517
        // $roleCreate->mainLanguageCode = 'eng-US';
518
519
        $roleDraft = $roleService->createRole($roleCreate);
520
        $roleService->publishRoleDraft($roleDraft);
521
522
        // Load the newly created role by its ID
523
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
524
525
        /* END: Use Case */
526
527
        $this->assertEquals('roleName', $role->identifier);
528
    }
529
530
    /**
531
     * Test for the loadRoleDraft() method.
532
     *
533
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
534
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
535
     */
536
    public function testLoadRoleDraft()
537
    {
538
        $repository = $this->getRepository();
539
540
        /* BEGIN: Use Case */
541
542
        $roleService = $repository->getRoleService();
543
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
544
545
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
546
        // $roleCreate->mainLanguageCode = 'eng-US';
547
548
        $roleDraft = $roleService->createRole($roleCreate);
549
550
        // Load the newly created role by its ID
551
        $role = $roleService->loadRoleDraft($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
552
553
        /* END: Use Case */
554
555
        $this->assertEquals('roleName', $role->identifier);
0 ignored issues
show
Documentation introduced by
The property $identifier is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
556
    }
557
558
    public function testLoadRoleDraftByRoleId()
559
    {
560
        $repository = $this->getRepository();
561
562
        /* BEGIN: Use Case */
563
564
        $roleService = $repository->getRoleService();
565
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
566
567
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
568
        // $roleCreate->mainLanguageCode = 'eng-US';
569
570
        $role = $roleService->createRole($roleCreate);
571
        $roleService->publishRoleDraft($role);
572
573
        // Now create a new draft based on the role
574
        $newDraft = $roleService->createRoleDraft($role);
575
        $loadedRoleDraft = $roleService->loadRoleDraftByRoleId($role->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
576
577
        /* END: Use Case */
578
579
        self::assertEquals('roleName', $role->identifier);
0 ignored issues
show
Documentation introduced by
The property $identifier is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
580
        self::assertInstanceOf('eZ\Publish\API\Repository\Values\User\RoleDraft', $loadedRoleDraft);
581
        self::assertEquals($newDraft, $loadedRoleDraft);
582
    }
583
584
    /**
585
     * Test for the loadRole() method.
586
     *
587
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
588
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
589
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
590
     */
591 View Code Duplication
    public function testLoadRoleThrowsNotFoundException()
592
    {
593
        $repository = $this->getRepository();
594
595
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
596
        /* BEGIN: Use Case */
597
598
        $roleService = $repository->getRoleService();
599
600
        // This call will fail with a NotFoundException, because no such role exists.
601
        $roleService->loadRole($nonExistingRoleId);
602
603
        /* END: Use Case */
604
    }
605
606
    /**
607
     * Test for the loadRoleDraft() method.
608
     *
609
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
610
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
611
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
612
     */
613 View Code Duplication
    public function testLoadRoleDraftThrowsNotFoundException()
614
    {
615
        $repository = $this->getRepository();
616
617
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
618
        /* BEGIN: Use Case */
619
620
        $roleService = $repository->getRoleService();
621
622
        // This call will fail with a NotFoundException, because no such role exists.
623
        $roleService->loadRoleDraft($nonExistingRoleId);
624
625
        /* END: Use Case */
626
    }
627
628
    /**
629
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
630
     */
631 View Code Duplication
    public function testLoadRoleDraftByRoleIdThrowsNotFoundException()
632
    {
633
        $repository = $this->getRepository();
634
635
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
636
        /* BEGIN: Use Case */
637
638
        $roleService = $repository->getRoleService();
639
640
        // This call will fail with a NotFoundException, because no such role exists.
641
        $roleService->loadRoleDraftByRoleId($nonExistingRoleId);
642
643
        /* END: Use Case */
644
    }
645
646
    /**
647
     * Test for the loadRoleByIdentifier() method.
648
     *
649
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
650
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
651
     */
652 View Code Duplication
    public function testLoadRoleByIdentifier()
653
    {
654
        $repository = $this->getRepository();
655
656
        /* BEGIN: Use Case */
657
658
        $roleService = $repository->getRoleService();
659
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
660
661
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
662
        // $roleCreate->mainLanguageCode = 'eng-US';
663
664
        $roleDraft = $roleService->createRole($roleCreate);
665
        $roleService->publishRoleDraft($roleDraft);
666
667
        // Load the newly created role by its identifier
668
        $role = $roleService->loadRoleByIdentifier('roleName');
669
670
        /* END: Use Case */
671
672
        $this->assertEquals('roleName', $role->identifier);
673
    }
674
675
    /**
676
     * Test for the loadRoleByIdentifier() method.
677
     *
678
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
679
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
680
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
681
     */
682
    public function testLoadRoleByIdentifierThrowsNotFoundException()
683
    {
684
        $repository = $this->getRepository();
685
686
        /* BEGIN: Use Case */
687
688
        $roleService = $repository->getRoleService();
689
690
        // This call will fail with a NotFoundException, because no such role exists.
691
        $roleService->loadRoleByIdentifier('MissingRole');
692
693
        /* END: Use Case */
694
    }
695
696
    /**
697
     * Test for the loadRoles() method.
698
     *
699
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
700
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
701
     */
702
    public function testLoadRoles()
703
    {
704
        $repository = $this->getRepository();
705
706
        /* BEGIN: Use Case */
707
708
        // First create a custom role
709
        $roleService = $repository->getRoleService();
710
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
711
712
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
713
        // $roleCreate->mainLanguageCode = 'eng-US';
714
715
        $roleDraft = $roleService->createRole($roleCreate);
716
        $roleService->publishRoleDraft($roleDraft);
717
718
        // Now load all available roles
719
        $roles = $roleService->loadRoles();
720
721
        foreach ($roles as $role) {
722
            if ($role->identifier === 'roleName') {
723
                break;
724
            }
725
        }
726
727
        /* END: Use Case */
728
729
        $this->assertEquals('roleName', $role->identifier);
0 ignored issues
show
Bug introduced by
The variable $role seems to be defined by a foreach iteration on line 721. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
730
    }
731
732
    /**
733
     * Test for the loadRoles() method.
734
     *
735
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
736
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
737
     */
738
    public function testLoadRolesReturnsExpectedSetOfDefaultRoles()
739
    {
740
        $repository = $this->getRepository();
741
742
        /* BEGIN: Use Case */
743
        $roleService = $repository->getRoleService();
744
745
        $roles = $roleService->loadRoles();
746
747
        $roleNames = array();
748
        foreach ($roles as $role) {
749
            $roleNames[] = $role->identifier;
750
        }
751
        /* END: Use Case */
752
753
        sort($roleNames);
754
755
        $this->assertEquals(
756
            array(
757
                'Administrator',
758
                'Anonymous',
759
                'Editor',
760
                'Member',
761
                'Partner',
762
            ),
763
            $roleNames
764
        );
765
    }
766
767
    /**
768
     * Test for the newRoleUpdateStruct() method.
769
     *
770
     * @see \eZ\Publish\API\Repository\RoleService::newRoleUpdateStruct()
771
     */
772
    public function testNewRoleUpdateStruct()
773
    {
774
        $repository = $this->getRepository();
775
776
        /* BEGIN: Use Case */
777
        $roleService = $repository->getRoleService();
778
        $roleUpdate = $roleService->newRoleUpdateStruct('newRole');
779
        /* END: Use Case */
780
781
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleUpdateStruct', $roleUpdate);
782
    }
783
784
    /**
785
     * Test for the updateRole() method.
786
     *
787
     * @see \eZ\Publish\API\Repository\RoleService::updateRole()
788
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleUpdateStruct
789
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
790
     */
791
    public function testUpdateRole()
792
    {
793
        $repository = $this->getRepository();
794
795
        /* BEGIN: Use Case */
796
        $roleService = $repository->getRoleService();
797
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
798
799
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
800
        // $roleCreate->mainLanguageCode = 'eng-US';
801
802
        $roleDraft = $roleService->createRole($roleCreate);
803
        $roleService->publishRoleDraft($roleDraft);
804
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
805
806
        $roleUpdate = $roleService->newRoleUpdateStruct();
807
        $roleUpdate->identifier = 'updatedRole';
808
809
        $updatedRole = $roleService->updateRole($role, $roleUpdate);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...leService::updateRole() has been deprecated with message: since 6.0, use {@see updateRoleDraft}

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...
810
        /* END: Use Case */
811
812
        // Now verify that our change was saved
813
        $role = $roleService->loadRoleByIdentifier('updatedRole');
814
815
        $this->assertEquals($role->id, $updatedRole->id);
816
    }
817
818
    /**
819
     * Test for the updateRoleDraft() method.
820
     *
821
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
822
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleUpdateStruct
823
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
824
     */
825 View Code Duplication
    public function testUpdateRoleDraft()
826
    {
827
        $repository = $this->getRepository();
828
829
        /* BEGIN: Use Case */
830
        $roleService = $repository->getRoleService();
831
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
832
833
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
834
        // $roleCreate->mainLanguageCode = 'eng-US';
835
836
        $roleDraft = $roleService->createRole($roleCreate);
837
838
        $roleUpdate = $roleService->newRoleUpdateStruct();
839
        $roleUpdate->identifier = 'updatedRole';
840
841
        $updatedRole = $roleService->updateRoleDraft($roleDraft, $roleUpdate);
842
        /* END: Use Case */
843
844
        // Now verify that our change was saved
845
        $role = $roleService->loadRoleDraft($updatedRole->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
846
847
        $this->assertEquals($role->identifier, 'updatedRole');
0 ignored issues
show
Documentation introduced by
The property $identifier is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
848
    }
849
850
    /**
851
     * Test for the updateRole() method.
852
     *
853
     * @see \eZ\Publish\API\Repository\RoleService::updateRole()
854
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
855
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRole
856
     */
857 View Code Duplication
    public function testUpdateRoleThrowsInvalidArgumentException()
858
    {
859
        $repository = $this->getRepository();
860
861
        /* BEGIN: Use Case */
862
        $roleService = $repository->getRoleService();
863
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
864
865
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
866
        // $roleCreate->mainLanguageCode = 'eng-US';
867
868
        $roleDraft = $roleService->createRole($roleCreate);
869
        $roleService->publishRoleDraft($roleDraft);
870
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
871
872
        $roleUpdate = $roleService->newRoleUpdateStruct();
873
        $roleUpdate->identifier = 'Editor';
874
875
        // This call will fail with an InvalidArgumentException, because Editor is a predefined role
876
        $roleService->updateRole($role, $roleUpdate);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...leService::updateRole() has been deprecated with message: since 6.0, use {@see updateRoleDraft}

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...
877
        /* END: Use Case */
878
    }
879
880
    /**
881
     * Test for the updateRoleDraft() method.
882
     *
883
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
884
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
885
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRoleDraft
886
     */
887
    public function testUpdateRoleDraftThrowsInvalidArgumentException()
888
    {
889
        $repository = $this->getRepository();
890
891
        /* BEGIN: Use Case */
892
        $roleService = $repository->getRoleService();
893
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
894
895
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
896
        // $roleCreate->mainLanguageCode = 'eng-US';
897
898
        $roleDraft = $roleService->createRole($roleCreate);
899
900
        $roleUpdate = $roleService->newRoleUpdateStruct();
901
        $roleUpdate->identifier = 'Editor';
902
903
        // This call will fail with an InvalidArgumentException, because Editor is a predefined role
904
        $roleService->updateRoleDraft($roleDraft, $roleUpdate);
905
        /* END: Use Case */
906
    }
907
908
    /**
909
     * Test for the deleteRole() method.
910
     *
911
     * @see \eZ\Publish\API\Repository\RoleService::deleteRole()
912
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
913
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
914
     */
915 View Code Duplication
    public function testDeleteRole()
916
    {
917
        $repository = $this->getRepository();
918
919
        /* BEGIN: Use Case */
920
        $roleService = $repository->getRoleService();
921
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
922
923
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
924
        // $roleCreate->mainLanguageCode = 'eng-US';
925
926
        $roleDraft = $roleService->createRole($roleCreate);
927
        $roleService->publishRoleDraft($roleDraft);
928
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
929
930
        $roleService->deleteRole($role);
931
        /* END: Use Case */
932
933
        $this->assertEquals(5, count($roleService->loadRoles()));
934
    }
935
936
    /**
937
     * Test for the deleteRoleDraft() method.
938
     *
939
     * @see \eZ\Publish\API\Repository\RoleService::deleteRoleDraft()
940
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
941
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
942
     */
943
    public function testDeleteRoleDraft()
944
    {
945
        $repository = $this->getRepository();
946
947
        /* BEGIN: Use Case */
948
        $roleService = $repository->getRoleService();
949
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
950
951
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
952
        // $roleCreate->mainLanguageCode = 'eng-US';
953
954
        $roleDraft = $roleService->createRole($roleCreate);
955
        $roleID = $roleDraft->id;
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
956
        $roleService->deleteRoleDraft($roleDraft);
957
958
        // This call will fail with a NotFoundException, because the draft no longer exists
959
        $roleService->loadRoleDraft($roleID);
960
        /* END: Use Case */
961
    }
962
963
    /**
964
     * Test for the newPolicyCreateStruct() method.
965
     *
966
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
967
     */
968
    public function testNewPolicyCreateStruct()
969
    {
970
        $repository = $this->getRepository();
971
972
        /* BEGIN: Use Case */
973
        $roleService = $repository->getRoleService();
974
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
975
        /* END: Use Case */
976
977
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyCreateStruct', $policyCreate);
978
    }
979
980
    /**
981
     * Test for the newPolicyCreateStruct() method.
982
     *
983
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
984
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
985
     */
986
    public function testNewPolicyCreateStructSetsStructProperties()
987
    {
988
        $repository = $this->getRepository();
989
990
        /* BEGIN: Use Case */
991
        $roleService = $repository->getRoleService();
992
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
993
        /* END: Use Case */
994
995
        $this->assertEquals(
996
            array('content', 'create'),
997
            array($policyCreate->module, $policyCreate->function)
998
        );
999
    }
1000
1001
    /**
1002
     * Test for the addPolicy() method.
1003
     *
1004
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
1005
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1006
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1007
     */
1008 View Code Duplication
    public function testAddPolicy()
1009
    {
1010
        $repository = $this->getRepository();
1011
1012
        /* BEGIN: Use Case */
1013
        $roleService = $repository->getRoleService();
1014
1015
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1016
1017
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1018
        // $roleCreate->mainLanguageCode = 'eng-US';
1019
1020
        $roleDraft = $roleService->createRole($roleCreate);
1021
        $roleService->publishRoleDraft($roleDraft);
1022
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1023
1024
        $role = $roleService->addPolicy(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

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...
1025
            $role,
1026
            $roleService->newPolicyCreateStruct('content', 'delete')
1027
        );
1028
        $role = $roleService->addPolicy(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

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...
1029
            $role,
1030
            $roleService->newPolicyCreateStruct('content', 'create')
1031
        );
1032
        /* END: Use Case */
1033
1034
        $actual = array();
1035
        foreach ($role->getPolicies() as $policy) {
1036
            $actual[] = array(
1037
                'module' => $policy->module,
1038
                'function' => $policy->function,
1039
            );
1040
        }
1041
        usort(
1042
            $actual,
1043
            function ($p1, $p2) {
1044
                return strcasecmp($p1['function'], $p2['function']);
1045
            }
1046
        );
1047
1048
        $this->assertEquals(
1049
            array(
1050
                array(
1051
                    'module' => 'content',
1052
                    'function' => 'create',
1053
                ),
1054
                array(
1055
                    'module' => 'content',
1056
                    'function' => 'delete',
1057
                ),
1058
            ),
1059
            $actual
1060
        );
1061
    }
1062
1063
    /**
1064
     * Test for the addPolicyByRoleDraft() method.
1065
     *
1066
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1067
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
1068
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1069
     */
1070
    public function testAddPolicyByRoleDraft()
1071
    {
1072
        $repository = $this->getRepository();
1073
1074
        /* BEGIN: Use Case */
1075
        $roleService = $repository->getRoleService();
1076
1077
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1078
1079
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1080
        // $roleCreate->mainLanguageCode = 'eng-US';
1081
1082
        $roleDraft = $roleService->createRole($roleCreate);
1083
1084
        $roleDraft = $roleService->addPolicyByRoleDraft(
1085
            $roleDraft,
1086
            $roleService->newPolicyCreateStruct('content', 'delete')
1087
        );
1088
        $roleDraft = $roleService->addPolicyByRoleDraft(
1089
            $roleDraft,
1090
            $roleService->newPolicyCreateStruct('content', 'create')
1091
        );
1092
        /* END: Use Case */
1093
1094
        $actual = array();
1095 View Code Duplication
        foreach ($roleDraft->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1096
            $actual[] = array(
1097
                'module' => $policy->module,
1098
                'function' => $policy->function,
1099
            );
1100
        }
1101
        usort(
1102
            $actual,
1103
            function ($p1, $p2) {
1104
                return strcasecmp($p1['function'], $p2['function']);
1105
            }
1106
        );
1107
1108
        $this->assertEquals(
1109
            array(
1110
                array(
1111
                    'module' => 'content',
1112
                    'function' => 'create',
1113
                ),
1114
                array(
1115
                    'module' => 'content',
1116
                    'function' => 'delete',
1117
                ),
1118
            ),
1119
            $actual
1120
        );
1121
    }
1122
1123
    /**
1124
     * Test for the addPolicy() method.
1125
     *
1126
     * @return array [\eZ\Publish\API\Repository\Values\User\Role, \eZ\Publish\API\Repository\Values\User\Policy]
1127
     *
1128
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
1129
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1130
     */
1131
    public function testAddPolicyUpdatesRole()
1132
    {
1133
        $repository = $this->getRepository();
1134
1135
        /* BEGIN: Use Case */
1136
        $roleService = $repository->getRoleService();
1137
1138
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1139
1140
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1141
        // $roleCreate->mainLanguageCode = 'eng-US';
1142
1143
        $roleDraft = $roleService->createRole($roleCreate);
1144
        $roleService->publishRoleDraft($roleDraft);
1145
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1146
1147
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1148
        $role = $roleService->addPolicy($role, $policyCreate);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

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...
1149
1150
        $policy = null;
1151
        foreach ($role->getPolicies() as $policy) {
1152
            if ($policy->module === 'content' && $policy->function === 'create') {
1153
                break;
1154
            }
1155
        }
1156
        /* END: Use Case */
1157
1158
        $this->assertInstanceOf(
1159
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1160
            $policy
1161
        );
1162
1163
        return array($role, $policy);
1164
    }
1165
1166
    /**
1167
     * Test for the addPolicyByRoleDraft() method.
1168
     *
1169
     * @return array [\eZ\Publish\API\Repository\Values\User\RoleDraft, \eZ\Publish\API\Repository\Values\User\Policy]
1170
     *
1171
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1172
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1173
     */
1174
    public function testAddPolicyByRoleDraftUpdatesRole()
1175
    {
1176
        $repository = $this->getRepository();
1177
1178
        /* BEGIN: Use Case */
1179
        $roleService = $repository->getRoleService();
1180
1181
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1182
1183
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1184
        // $roleCreate->mainLanguageCode = 'eng-US';
1185
1186
        $roleDraft = $roleService->createRole($roleCreate);
1187
1188
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1189
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreate);
1190
1191
        $policy = null;
1192
        foreach ($roleDraft->getPolicies() as $policy) {
1193
            if ($policy->module === 'content' && $policy->function === 'create') {
1194
                break;
1195
            }
1196
        }
1197
        /* END: Use Case */
1198
1199
        $this->assertInstanceOf(
1200
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1201
            $policy
1202
        );
1203
1204
        return array($roleDraft, $policy);
1205
    }
1206
1207
    /**
1208
     * Test for the addPolicy() method.
1209
     *
1210
     * @param array $roleAndPolicy
1211
     *
1212
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
1213
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyUpdatesRole
1214
     */
1215 View Code Duplication
    public function testAddPolicySetsPolicyProperties($roleAndPolicy)
1216
    {
1217
        list($role, $policy) = $roleAndPolicy;
1218
1219
        $this->assertEquals(
1220
            array($role->id, 'content', 'create'),
1221
            array($policy->roleId, $policy->module, $policy->function)
1222
        );
1223
    }
1224
1225
    /**
1226
     * Test for the addPolicyByRoleDraft() method.
1227
     *
1228
     * @param array $roleAndPolicy
1229
     *
1230
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1231
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1232
     */
1233 View Code Duplication
    public function testAddPolicyByRoleDraftSetsPolicyProperties($roleAndPolicy)
1234
    {
1235
        list($role, $policy) = $roleAndPolicy;
1236
1237
        $this->assertEquals(
1238
            array($role->id, 'content', 'create'),
1239
            array($policy->roleId, $policy->module, $policy->function)
1240
        );
1241
    }
1242
1243
    /**
1244
     * Test for the addPolicy() method.
1245
     *
1246
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
1247
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
1248
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1249
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
1250
     */
1251
    public function testAddPolicyThrowsLimitationValidationException()
1252
    {
1253
        $repository = $this->getRepository();
1254
1255
        /* BEGIN: Use Case */
1256
        $roleService = $repository->getRoleService();
1257
1258
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
1259
1260
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1261
        // $roleCreate->mainLanguageCode = 'eng-US';
1262
1263
        $roleDraft = $roleService->createRole($roleCreate);
1264
        $roleService->publishRoleDraft($roleDraft);
1265
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1266
1267
        // Create new subtree limitation
1268
        $limitation = new SubtreeLimitation(
1269
            array(
1270
                'limitationValues' => array('/mountain/forest/tree/42/'),
1271
            )
1272
        );
1273
1274
        // Create policy create struct and add limitation to it
1275
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'remove');
1276
        $policyCreateStruct->addLimitation($limitation);
1277
1278
        // This call will fail with an LimitationValidationException, because subtree
1279
        // "/mountain/forest/tree/42/" does not exist
1280
        $roleService->addPolicy($role, $policyCreateStruct);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

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...
1281
        /* END: Use Case */
1282
    }
1283
1284
    /**
1285
     * Test for the addPolicyByRoleDraft() method.
1286
     *
1287
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1288
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
1289
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1290
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
1291
     */
1292 View Code Duplication
    public function testAddPolicyByRoleDraftThrowsLimitationValidationException()
1293
    {
1294
        $repository = $this->getRepository();
1295
1296
        /* BEGIN: Use Case */
1297
        $roleService = $repository->getRoleService();
1298
1299
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
1300
1301
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1302
        // $roleCreate->mainLanguageCode = 'eng-US';
1303
1304
        $roleDraft = $roleService->createRole($roleCreate);
1305
1306
        // Create new subtree limitation
1307
        $limitation = new SubtreeLimitation(
1308
            array(
1309
                'limitationValues' => array('/mountain/forest/tree/42/'),
1310
            )
1311
        );
1312
1313
        // Create policy create struct and add limitation to it
1314
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'remove');
1315
        $policyCreateStruct->addLimitation($limitation);
1316
1317
        // This call will fail with an LimitationValidationException, because subtree
1318
        // "/mountain/forest/tree/42/" does not exist
1319
        $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
1320
        /* END: Use Case */
1321
    }
1322
1323
    /**
1324
     * Test for the createRole() method.
1325
     *
1326
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
1327
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyUpdatesRole
1328
     */
1329
    public function testCreateRoleWithAddPolicy()
1330
    {
1331
        $repository = $this->getRepository();
1332
1333
        /* BEGIN: Use Case */
1334
        $roleService = $repository->getRoleService();
1335
1336
        // Instantiate a new create struct
1337
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1338
1339
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1340
        // $roleCreate->mainLanguageCode = 'eng-US';
1341
1342
        // Add some role policies
1343
        $roleCreate->addPolicy(
1344
            $roleService->newPolicyCreateStruct(
1345
                'content',
1346
                'read'
1347
            )
1348
        );
1349
        $roleCreate->addPolicy(
1350
            $roleService->newPolicyCreateStruct(
1351
                'content',
1352
                'translate'
1353
            )
1354
        );
1355
1356
        // Create new role instance
1357
        $roleDraft = $roleService->createRole($roleCreate);
1358
        $roleService->publishRoleDraft($roleDraft);
1359
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1360
1361
        $policies = array();
1362 View Code Duplication
        foreach ($role->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1363
            $policies[] = array('module' => $policy->module, 'function' => $policy->function);
1364
        }
1365
        /* END: Use Case */
1366
        array_multisort($policies);
1367
1368
        $this->assertEquals(
1369
            array(
1370
                array(
1371
                    'module' => 'content',
1372
                    'function' => 'read',
1373
                ),
1374
                array(
1375
                    'module' => 'content',
1376
                    'function' => 'translate',
1377
                ),
1378
            ),
1379
            $policies
1380
        );
1381
    }
1382
1383
    /**
1384
     * Test for the createRoleDraft() method.
1385
     *
1386
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
1387
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1388
     */
1389
    public function testCreateRoleDraftWithAddPolicy()
1390
    {
1391
        $repository = $this->getRepository();
1392
1393
        /* BEGIN: Use Case */
1394
        $roleService = $repository->getRoleService();
1395
1396
        // Instantiate a new create struct
1397
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1398
1399
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1400
        // $roleCreate->mainLanguageCode = 'eng-US';
1401
1402
        // Add some role policies
1403
        $roleCreate->addPolicy(
1404
            $roleService->newPolicyCreateStruct(
1405
                'content',
1406
                'read'
1407
            )
1408
        );
1409
        $roleCreate->addPolicy(
1410
            $roleService->newPolicyCreateStruct(
1411
                'content',
1412
                'translate'
1413
            )
1414
        );
1415
1416
        // Create new role instance
1417
        $roleDraft = $roleService->createRole($roleCreate);
1418
1419
        $policies = array();
1420 View Code Duplication
        foreach ($roleDraft->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1421
            $policies[] = array('module' => $policy->module, 'function' => $policy->function);
1422
        }
1423
        /* END: Use Case */
1424
1425
        $this->assertEquals(
1426
            array(
1427
                array(
1428
                    'module' => 'content',
1429
                    'function' => 'read',
1430
                ),
1431
                array(
1432
                    'module' => 'content',
1433
                    'function' => 'translate',
1434
                ),
1435
            ),
1436
            $policies
1437
        );
1438
    }
1439
1440
    /**
1441
     * Test for the newPolicyUpdateStruct() method.
1442
     *
1443
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyUpdateStruct()
1444
     */
1445
    public function testNewPolicyUpdateStruct()
1446
    {
1447
        $repository = $this->getRepository();
1448
1449
        /* BEGIN: Use Case */
1450
        $roleService = $repository->getRoleService();
1451
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1452
        /* END: Use Case */
1453
1454
        $this->assertInstanceOf(
1455
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyUpdateStruct',
1456
            $policyUpdate
1457
        );
1458
    }
1459
1460
    public function testUpdatePolicyNoLimitation()
1461
    {
1462
        $repository = $this->getRepository();
1463
1464
        /* BEGIN: Use Case */
1465
        $roleService = $repository->getRoleService();
1466
1467
        // Instantiate new policy create
1468
        $policyCreate = $roleService->newPolicyCreateStruct('foo', 'bar');
1469
1470
        // Instantiate a role create and add the policy create
1471
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1472
1473
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1474
        // $roleCreate->mainLanguageCode = 'eng-US';
1475
1476
        $roleCreate->addPolicy($policyCreate);
1477
1478
        // Create a new role instance.
1479
        $roleDraft = $roleService->createRole($roleCreate);
1480
        $roleService->publishRoleDraft($roleDraft);
1481
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1482
1483
        // Search for the new policy instance
1484
        $policy = null;
1485
        foreach ($role->getPolicies() as $policy) {
1486
            if ($policy->module === 'foo' && $policy->function === 'bar') {
1487
                break;
1488
            }
1489
        }
1490
1491
        // Create an update struct
1492
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1493
1494
        // Update the the policy
1495
        $policy = $roleService->updatePolicy($policy, $policyUpdate);
0 ignored issues
show
Bug introduced by
It seems like $policy can be null; however, updatePolicy() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::updatePolicy() has been deprecated with message: since 6.0, use {@link updatePolicyByRoleDraft()} 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...
1496
        /* END: Use Case */
1497
1498
        $this->assertInstanceOf(
1499
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1500
            $policy
1501
        );
1502
1503
        self::assertEquals(array(), $policy->getLimitations());
1504
    }
1505
1506
    /**
1507
     * Test for the updatePolicy() method.
1508
     *
1509
     * @return array
1510
     *
1511
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1512
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1513
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1514
     */
1515
    public function testUpdatePolicy()
1516
    {
1517
        $repository = $this->getRepository();
1518
1519
        /* BEGIN: Use Case */
1520
        $roleService = $repository->getRoleService();
1521
1522
        // Instantiate new policy create
1523
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'translate');
1524
1525
        // Add some limitations for the new policy
1526
        $policyCreate->addLimitation(
1527
            new LanguageLimitation(
1528
                array(
1529
                    'limitationValues' => array('eng-US', 'eng-GB'),
1530
                )
1531
            )
1532
        );
1533
1534
        // Instantiate a role create and add the policy create
1535
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1536
1537
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1538
        // $roleCreate->mainLanguageCode = 'eng-US';
1539
1540
        $roleCreate->addPolicy($policyCreate);
1541
1542
        // Create a new role instance.
1543
        $roleDraft = $roleService->createRole($roleCreate);
1544
        $roleService->publishRoleDraft($roleDraft);
1545
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1546
1547
        // Search for the new policy instance
1548
        $policy = null;
1549
        foreach ($role->getPolicies() as $policy) {
1550
            if ($policy->module === 'content' && $policy->function === 'translate') {
1551
                break;
1552
            }
1553
        }
1554
1555
        // Create an update struct and set a modified limitation
1556
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1557
        $policyUpdate->addLimitation(
1558
            new ContentTypeLimitation(
1559
                array(
1560
                    'limitationValues' => array(29, 30),
1561
                )
1562
            )
1563
        );
1564
1565
        // Update the the policy
1566
        $policy = $roleService->updatePolicy($policy, $policyUpdate);
0 ignored issues
show
Bug introduced by
It seems like $policy can be null; however, updatePolicy() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::updatePolicy() has been deprecated with message: since 6.0, use {@link updatePolicyByRoleDraft()} 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...
1567
        /* END: Use Case */
1568
1569
        $this->assertInstanceOf(
1570
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1571
            $policy
1572
        );
1573
1574
        return array($roleService->loadRole($role->id), $policy);
1575
    }
1576
1577
    /**
1578
     * Test for the updatePolicy() method.
1579
     *
1580
     * @param array $roleAndPolicy
1581
     *
1582
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1583
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicy
1584
     */
1585
    public function testUpdatePolicyUpdatesLimitations($roleAndPolicy)
1586
    {
1587
        list($role, $policy) = $roleAndPolicy;
1588
1589
        $this->assertEquals(
1590
            array(
1591
                new ContentTypeLimitation(
1592
                    array(
1593
                        'limitationValues' => array(29, 30),
1594
                    )
1595
                ),
1596
            ),
1597
            $policy->getLimitations()
1598
        );
1599
1600
        return $role;
1601
    }
1602
1603
    /**
1604
     * Test for the updatePolicy() method.
1605
     *
1606
     * @param \eZ\Publish\API\Repository\Values\User\Role $role
1607
     *
1608
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1609
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyUpdatesLimitations
1610
     */
1611
    public function testUpdatePolicyUpdatesRole($role)
1612
    {
1613
        $limitations = array();
1614
        foreach ($role->getPolicies() as $policy) {
1615
            foreach ($policy->getLimitations() as $limitation) {
1616
                $limitations[] = $limitation;
1617
            }
1618
        }
1619
1620
        $this->assertCount(1, $limitations);
1621
        $this->assertInstanceOf(
1622
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Limitation',
1623
            $limitations[0]
1624
        );
1625
1626
        $expectedData = array(
1627
            'limitationValues' => array(29, 30),
1628
        );
1629
        $this->assertPropertiesCorrectUnsorted(
1630
            $expectedData,
1631
            $limitations[0]
1632
        );
1633
    }
1634
1635
    /**
1636
     * Test for the updatePolicy() method.
1637
     *
1638
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1639
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
1640
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1641
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1642
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1643
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
1644
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
1645
     */
1646
    public function testUpdatePolicyThrowsLimitationValidationException()
1647
    {
1648
        $repository = $this->getRepository();
1649
1650
        /* BEGIN: Use Case */
1651
        $roleService = $repository->getRoleService();
1652
1653
        // Instantiate new policy create
1654
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
1655
1656
        // Add some limitations for the new policy
1657
        $policyCreate->addLimitation(
1658
            new SubtreeLimitation(
1659
                array(
1660
                    'limitationValues' => array('/1/2/'),
1661
                )
1662
            )
1663
        );
1664
1665
        // Instantiate a role create and add the policy create
1666
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1667
1668
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1669
        // $roleCreate->mainLanguageCode = 'eng-US';
1670
1671
        $roleCreate->addPolicy($policyCreate);
1672
1673
        // Create a new role instance.
1674
        $roleDraft = $roleService->createRole($roleCreate);
1675
        $roleService->publishRoleDraft($roleDraft);
1676
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1677
1678
        // Search for the new policy instance
1679
        $policy = null;
1680
        foreach ($role->getPolicies() as $policy) {
1681
            if ($policy->module === 'content' && $policy->function === 'remove') {
1682
                break;
1683
            }
1684
        }
1685
1686
        // Create an update struct and set a modified limitation
1687
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1688
        $policyUpdate->addLimitation(
1689
            new SubtreeLimitation(
1690
                array(
1691
                    'limitationValues' => array('/mountain/forest/tree/42/'),
1692
                )
1693
            )
1694
        );
1695
1696
        // This call will fail with an LimitationValidationException, because subtree
1697
        // "/mountain/forest/tree/42/" does not exist
1698
        $policy = $roleService->updatePolicy($policy, $policyUpdate);
0 ignored issues
show
Unused Code introduced by
$policy is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Bug introduced by
It seems like $policy can be null; however, updatePolicy() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::updatePolicy() has been deprecated with message: since 6.0, use {@link updatePolicyByRoleDraft()} 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...
1699
        /* END: Use Case */
1700
    }
1701
1702
    /**
1703
     * Test for the removePolicy() method.
1704
     *
1705
     * @see \eZ\Publish\API\Repository\RoleService::removePolicy()
1706
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1707
     */
1708 View Code Duplication
    public function testRemovePolicy()
1709
    {
1710
        $repository = $this->getRepository();
1711
1712
        /* BEGIN: Use Case */
1713
        $roleService = $repository->getRoleService();
1714
1715
        // Instantiate a new role create
1716
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1717
1718
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1719
        // $roleCreate->mainLanguageCode = 'eng-US';
1720
1721
        // Create a new role with two policies
1722
        $roleDraft = $roleService->createRole($roleCreate);
1723
        $roleService->addPolicyByRoleDraft(
1724
            $roleDraft,
1725
            $roleService->newPolicyCreateStruct('content', 'create')
1726
        );
1727
        $roleService->addPolicyByRoleDraft(
1728
            $roleDraft,
1729
            $roleService->newPolicyCreateStruct('content', 'delete')
1730
        );
1731
        $roleService->publishRoleDraft($roleDraft);
1732
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1733
1734
        // Delete all policies from the new role
1735
        foreach ($role->getPolicies() as $policy) {
1736
            $role = $roleService->removePolicy($role, $policy);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::removePolicy() has been deprecated with message: since 5.3, use {@link removePolicyByRoleDraft()} 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...
1737
        }
1738
        /* END: Use Case */
1739
1740
        $this->assertSame(array(), $role->getPolicies());
1741
    }
1742
1743
    /**
1744
     * Test for the removePolicyByRoleDraft() method.
1745
     *
1746
     * @see \eZ\Publish\API\Repository\RoleService::removePolicyByRoleDraft()
1747
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1748
     */
1749 View Code Duplication
    public function testRemovePolicyByRoleDraft()
1750
    {
1751
        $repository = $this->getRepository();
1752
1753
        /* BEGIN: Use Case */
1754
        $roleService = $repository->getRoleService();
1755
1756
        // Instantiate a new role create
1757
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1758
1759
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1760
        // $roleCreate->mainLanguageCode = 'eng-US';
1761
1762
        // Create a new role with two policies
1763
        $roleDraft = $roleService->createRole($roleCreate);
1764
        $roleService->addPolicyByRoleDraft(
1765
            $roleDraft,
1766
            $roleService->newPolicyCreateStruct('content', 'create')
1767
        );
1768
        $roleService->addPolicyByRoleDraft(
1769
            $roleDraft,
1770
            $roleService->newPolicyCreateStruct('content', 'delete')
1771
        );
1772
1773
        // Delete all policies from the new role
1774
        foreach ($roleDraft->getPolicies() as $policy) {
1775
            $roleDraft = $roleService->removePolicyByRoleDraft($roleDraft, $policy);
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1776
        }
1777
        /* END: Use Case */
1778
1779
        $this->assertSame(array(), $roleDraft->getPolicies());
1780
    }
1781
1782
    /**
1783
     * Test for the deletePolicy() method.
1784
     *
1785
     * @see \eZ\Publish\API\Repository\RoleService::deletePolicy()
1786
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
1787
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1788
     */
1789 View Code Duplication
    public function testDeletePolicy()
1790
    {
1791
        $repository = $this->getRepository();
1792
1793
        /* BEGIN: Use Case */
1794
        $roleService = $repository->getRoleService();
1795
1796
        // Instantiate a new role create
1797
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1798
1799
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1800
        // $roleCreate->mainLanguageCode = 'eng-US';
1801
1802
        // Create a new role with two policies
1803
        $roleDraft = $roleService->createRole($roleCreate);
1804
        $roleService->addPolicyByRoleDraft(
1805
            $roleDraft,
1806
            $roleService->newPolicyCreateStruct('content', 'create')
1807
        );
1808
        $roleService->addPolicyByRoleDraft(
1809
            $roleDraft,
1810
            $roleService->newPolicyCreateStruct('content', 'delete')
1811
        );
1812
        $roleService->publishRoleDraft($roleDraft);
1813
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1814
1815
        // Delete all policies from the new role
1816
        foreach ($role->getPolicies() as $policy) {
1817
            $roleService->deletePolicy($policy);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::deletePolicy() has been deprecated with message: since 6.0, use {@link removePolicyByRoleDraft()} 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...
1818
        }
1819
        /* END: Use Case */
1820
1821
        $role = $roleService->loadRole($role->id);
1822
        $this->assertSame(array(), $role->getPolicies());
1823
    }
1824
1825
    /**
1826
     * Test loading user/group role assignments.
1827
     *
1828
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment
1829
     *
1830
     * @covers \eZ\Publish\API\Repository\RoleService::loadRoleAssignment
1831
     */
1832
    public function testLoadRoleAssignment()
1833
    {
1834
        $repository = $this->getRepository();
1835
1836
        /* BEGIN: Use Case */
1837
        $roleService = $repository->getRoleService();
1838
1839
        // Assignment to user group
1840
        $groupRoleAssignment = $roleService->loadRoleAssignment(25);
1841
1842
        // Assignment to user
1843
        $role = $roleService->loadRole(2);
1844
        $user = $repository->getUserService()->loadUser(14);
1845
        $roleService->assignRoleToUser($role, $user);
1846
        $userRoleAssignments = $roleService->getRoleAssignmentsForUser($user);
1847
1848
        $userRoleAssignment = $roleService->loadRoleAssignment($userRoleAssignments[0]->id);
1849
        /* END: Use Case */
1850
1851
        $this->assertInstanceOf(
1852
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1853
            $groupRoleAssignment
1854
        );
1855
1856
        $this->assertEquals(
1857
            [
1858
                12,
1859
                2,
1860
                25,
1861
            ],
1862
            [
1863
                $groupRoleAssignment->userGroup->id,
0 ignored issues
show
Documentation introduced by
The property userGroup does not exist on object<eZ\Publish\API\Re...es\User\RoleAssignment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1864
                $groupRoleAssignment->role->id,
1865
                $groupRoleAssignment->id,
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repositor...ues\User\RoleAssignment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1866
            ]
1867
        );
1868
1869
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment', $userRoleAssignment);
1870
        self::assertEquals(14, $userRoleAssignment->user->id);
0 ignored issues
show
Documentation introduced by
The property user does not exist on object<eZ\Publish\API\Re...es\User\RoleAssignment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1871
1872
        return $groupRoleAssignment;
1873
    }
1874
1875
    /**
1876
     * Test for the getRoleAssignments() method.
1877
     *
1878
     * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[]
1879
     *
1880
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1881
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1882
     */
1883
    public function testGetRoleAssignments()
1884
    {
1885
        $repository = $this->getRepository();
1886
1887
        /* BEGIN: Use Case */
1888
        $roleService = $repository->getRoleService();
1889
1890
        // Load the editor role
1891
        $role = $roleService->loadRoleByIdentifier('Editor');
1892
1893
        // Load all assigned users and user groups
1894
        $roleAssignments = $roleService->getRoleAssignments($role);
1895
1896
        /* END: Use Case */
1897
1898
        $this->assertEquals(2, count($roleAssignments));
1899
        $this->assertInstanceOf(
1900
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1901
            $roleAssignments[0]
1902
        );
1903
        $this->assertInstanceOf(
1904
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1905
            $roleAssignments[1]
1906
        );
1907
1908
        return $roleAssignments;
1909
    }
1910
1911
    /**
1912
     * Test for the getRoleAssignments() method.
1913
     *
1914
     * @param \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $roleAssignments
1915
     *
1916
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1917
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1918
     */
1919
    public function testGetRoleAssignmentsContainExpectedLimitation(array $roleAssignments)
1920
    {
1921
        $this->assertEquals(
1922
            'Subtree',
1923
            reset($roleAssignments)->limitation->getIdentifier()
1924
        );
1925
    }
1926
1927
    /**
1928
     * Test for the assignRoleToUser() method.
1929
     *
1930
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser()
1931
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1932
     */
1933 View Code Duplication
    public function testAssignRoleToUser()
1934
    {
1935
        $repository = $this->getRepository();
1936
        $roleService = $repository->getRoleService();
1937
1938
        /* BEGIN: Use Case */
1939
        $user = $this->createUserVersion1();
1940
1941
        // Load the existing "Administrator" role
1942
        $role = $roleService->loadRoleByIdentifier('Administrator');
1943
1944
        // Assign the "Administrator" role to the newly created user
1945
        $roleService->assignRoleToUser($role, $user);
1946
1947
        // The assignments array will contain the new role<->user assignment
1948
        $roleAssignments = $roleService->getRoleAssignments($role);
1949
        /* END: Use Case */
1950
1951
        // Administrator + Example User
1952
        $this->assertEquals(2, count($roleAssignments));
1953
    }
1954
1955
    /**
1956
     * Test for the assignRoleToUser() method.
1957
     *
1958
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1959
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1960
     */
1961 View Code Duplication
    public function testAssignRoleToUserWithRoleLimitation()
1962
    {
1963
        $repository = $this->getRepository();
1964
        $roleService = $repository->getRoleService();
1965
1966
        /* BEGIN: Use Case */
1967
        $user = $this->createUserVersion1();
1968
1969
        // Load the existing "Anonymous" role
1970
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1971
1972
        // Assign the "Anonymous" role to the newly created user
1973
        $roleService->assignRoleToUser(
1974
            $role,
1975
            $user,
1976
            new SubtreeLimitation(
1977
                array(
1978
                    'limitationValues' => array('/1/43/'),
1979
                )
1980
            )
1981
        );
1982
1983
        // The assignments array will contain the new role<->user assignment
1984
        $roleAssignments = $roleService->getRoleAssignments($role);
1985
        /* END: Use Case */
1986
1987
        // Members + Partners + Anonymous + Example User
1988
        $this->assertEquals(4, count($roleAssignments));
1989
1990
        // Get the role limitation
1991
        $roleLimitation = null;
1992
        foreach ($roleAssignments as $roleAssignment) {
1993
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1994
            if ($roleLimitation) {
1995
                $this->assertInstanceOf(
1996
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1997
                    $roleAssignment
1998
                );
1999
                break;
2000
            }
2001
        }
2002
2003
        $this->assertEquals(
2004
            new SubtreeLimitation(
2005
                array(
2006
                    'limitationValues' => array('/1/43/'),
2007
                )
2008
            ),
2009
            $roleLimitation
2010
        );
2011
2012
        // Test again to see values being merged
2013
        $roleService->assignRoleToUser(
2014
            $role,
2015
            $user,
2016
            new SubtreeLimitation(
2017
                array(
2018
                    'limitationValues' => array('/1/43/', '/1/2/'),
2019
                )
2020
            )
2021
        );
2022
2023
        // The assignments array will contain the new role<->user assignment
2024
        $roleAssignments = $roleService->getRoleAssignments($role);
2025
2026
        // Members + Partners + Anonymous + Example User
2027
        $this->assertEquals(5, count($roleAssignments));
2028
2029
        // Get the role limitation
2030
        $roleLimitations = [];
2031
        foreach ($roleAssignments as $roleAssignment) {
2032
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2033
            if ($roleLimitation) {
2034
                $this->assertInstanceOf(
2035
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2036
                    $roleAssignment
2037
                );
2038
                $roleLimitations[] = $roleLimitation;
2039
            }
2040
        }
2041
        array_multisort($roleLimitations);
2042
2043
        $this->assertEquals(
2044
            [
2045
                new SubtreeLimitation(
2046
                    array(
2047
                        'limitationValues' => array('/1/2/'),
2048
                    )
2049
                ),
2050
                new SubtreeLimitation(
2051
                    array(
2052
                        'limitationValues' => array('/1/43/'),
2053
                    )
2054
                ),
2055
            ],
2056
            $roleLimitations
2057
        );
2058
    }
2059
2060
    /**
2061
     * Test for the assignRoleToUser() method.
2062
     *
2063
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2064
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
2065
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2066
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2067
     */
2068
    public function testAssignRoleToUserWithRoleLimitationThrowsLimitationValidationException()
2069
    {
2070
        $repository = $this->getRepository();
2071
2072
        /* BEGIN: Use Case */
2073
        $roleService = $repository->getRoleService();
2074
2075
        // Load the existing "Anonymous" role
2076
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2077
2078
        // Get current user
2079
        $currentUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
2080
2081
        // Assign the "Anonymous" role to the current user
2082
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
2083
        // does not exists
2084
        $roleService->assignRoleToUser(
2085
            $role,
2086
            $currentUser,
2087
            new SubtreeLimitation(
2088
                array(
2089
                    'limitationValues' => array('/lorem/ipsum/42/'),
2090
                )
2091
            )
2092
        );
2093
        /* END: Use Case */
2094
    }
2095
2096
    /**
2097
     * Test for the assignRoleToUser() method.
2098
     *
2099
     * Makes sure assigning role several times throws.
2100
     *
2101
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2102
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2103
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2104
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2105
     */
2106 View Code Duplication
    public function testAssignRoleToUserThrowsInvalidArgumentException()
2107
    {
2108
        $repository = $this->getRepository();
2109
2110
        /* BEGIN: Use Case */
2111
        $roleService = $repository->getRoleService();
2112
2113
        // Load the existing "Anonymous" role
2114
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2115
2116
        // Get current user
2117
        $currentUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
2118
2119
        // Assign the "Anonymous" role to the current user
2120
        try {
2121
            $roleService->assignRoleToUser(
2122
                $role,
2123
                $currentUser
2124
            );
2125
        } catch (Exception $e) {
2126
            $this->fail('Got exception at first valid attempt to assign role');
2127
        }
2128
2129
        // Re-Assign the "Anonymous" role to the current user
2130
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2131
        $roleService->assignRoleToUser(
2132
            $role,
2133
            $currentUser
2134
        );
2135
        /* END: Use Case */
2136
    }
2137
2138
    /**
2139
     * Test for the assignRoleToUser() method.
2140
     *
2141
     * Makes sure assigning role several times with same limitations throws.
2142
     *
2143
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2144
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2145
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2146
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2147
     */
2148
    public function testAssignRoleToUserWithRoleLimitationThrowsInvalidArgumentException()
2149
    {
2150
        $repository = $this->getRepository();
2151
2152
        /* BEGIN: Use Case */
2153
        $roleService = $repository->getRoleService();
2154
2155
        // Load the existing "Anonymous" role
2156
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2157
2158
        // Get current user
2159
        $currentUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
2160
2161
        // Assign the "Anonymous" role to the current user
2162
        try {
2163
            $roleService->assignRoleToUser(
2164
                $role,
2165
                $currentUser,
2166
                new SubtreeLimitation(
2167
                    array(
2168
                        'limitationValues' => array('/1/43/', '/1/2/'),
2169
                    )
2170
                )
2171
            );
2172
        } catch (Exception $e) {
2173
            $this->fail('Got exception at first valid attempt to assign role');
2174
        }
2175
2176
        // Re-Assign the "Anonymous" role to the current user
2177
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2178
        $roleService->assignRoleToUser(
2179
            $role,
2180
            $currentUser,
2181
            new SubtreeLimitation(
2182
                array(
2183
                    'limitationValues' => array('/1/43/'),
2184
                )
2185
            )
2186
        );
2187
        /* END: Use Case */
2188
    }
2189
2190
    /**
2191
     * Test for the unassignRoleFromUser() method.
2192
     *
2193
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUser()
2194
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2195
     */
2196 View Code Duplication
    public function testUnassignRoleFromUser()
2197
    {
2198
        $repository = $this->getRepository();
2199
        $roleService = $repository->getRoleService();
2200
2201
        /* BEGIN: Use Case */
2202
        $user = $this->createUserVersion1();
2203
2204
        // Load the existing "Member" role
2205
        $role = $roleService->loadRoleByIdentifier('Member');
2206
2207
        // Assign the "Member" role to the newly created user
2208
        $roleService->assignRoleToUser($role, $user);
2209
2210
        // Unassign user from role
2211
        $roleService->unassignRoleFromUser($role, $user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...:unassignRoleFromUser() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} 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...
2212
2213
        // The assignments array will not contain the new role<->user assignment
2214
        $roleAssignments = $roleService->getRoleAssignments($role);
2215
        /* END: Use Case */
2216
2217
        // Members + Editors + Partners
2218
        $this->assertEquals(3, count($roleAssignments));
2219
    }
2220
2221
    /**
2222
     * Test for the unassignRoleFromUser() method.
2223
     *
2224
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUser()
2225
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2226
     */
2227 View Code Duplication
    public function testUnassignRoleFromUserThrowsInvalidArgumentException()
2228
    {
2229
        $repository = $this->getRepository();
2230
        $roleService = $repository->getRoleService();
2231
2232
        /* BEGIN: Use Case */
2233
        $user = $this->createUserVersion1();
2234
2235
        // Load the existing "Member" role
2236
        $role = $roleService->loadRoleByIdentifier('Member');
2237
2238
        // This call will fail with a "InvalidArgumentException", because the
2239
        // user does not have the "Member" role.
2240
        $roleService->unassignRoleFromUser($role, $user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...:unassignRoleFromUser() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} 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...
2241
        /* END: Use Case */
2242
    }
2243
2244
    /**
2245
     * Test for the getRoleAssignmentsForUser() method.
2246
     *
2247
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2248
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2249
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2250
     */
2251 View Code Duplication
    public function testGetRoleAssignmentsForUserDirect()
2252
    {
2253
        $repository = $this->getRepository();
2254
        $roleService = $repository->getRoleService();
2255
2256
        /* BEGIN: Use Case */
2257
        $user = $this->createUserVersion1();
2258
2259
        // Instantiate a role create and add some policies
2260
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2261
2262
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2263
        // $roleCreate->mainLanguageCode = 'eng-US';
2264
2265
        $roleCreate->addPolicy(
2266
            $roleService->newPolicyCreateStruct('user', 'login')
2267
        );
2268
        $roleCreate->addPolicy(
2269
            $roleService->newPolicyCreateStruct('content', 'read')
2270
        );
2271
        $roleCreate->addPolicy(
2272
            $roleService->newPolicyCreateStruct('content', 'edit')
2273
        );
2274
2275
        // Create the new role instance
2276
        $roleDraft = $roleService->createRole($roleCreate);
2277
        $roleService->publishRoleDraft($roleDraft);
2278
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
2279
2280
        // Assign role to new user
2281
        $roleService->assignRoleToUser($role, $user);
2282
2283
        // Load the currently assigned role
2284
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user);
2285
        /* END: Use Case */
2286
2287
        $this->assertEquals(1, count($roleAssignments));
2288
        $this->assertInstanceOf(
2289
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2290
            reset($roleAssignments)
2291
        );
2292
    }
2293
2294
    /**
2295
     * Test for the getRoleAssignmentsForUser() method.
2296
     *
2297
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2298
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2299
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2300
     */
2301
    public function testGetRoleAssignmentsForUserEmpty()
2302
    {
2303
        $repository = $this->getRepository();
2304
        $roleService = $repository->getRoleService();
2305
2306
        /* BEGIN: Use Case */
2307
        $adminUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
2308
2309
        // Load the currently assigned role
2310
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser);
2311
        /* END: Use Case */
2312
2313
        $this->assertEquals(0, count($roleAssignments));
2314
    }
2315
2316
    /**
2317
     * Test for the getRoleAssignmentsForUser() method.
2318
     *
2319
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2320
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2321
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2322
     */
2323
    public function testGetRoleAssignmentsForUserInherited()
2324
    {
2325
        $repository = $this->getRepository();
2326
        $roleService = $repository->getRoleService();
2327
2328
        /* BEGIN: Use Case */
2329
        $adminUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
2330
2331
        // Load the currently assigned role + inherited role assignments
2332
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser, true);
2333
        /* END: Use Case */
2334
2335
        $this->assertEquals(1, count($roleAssignments));
2336
        $this->assertInstanceOf(
2337
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2338
            reset($roleAssignments)
2339
        );
2340
    }
2341
2342
    /**
2343
     * Test for the assignRoleToUserGroup() method.
2344
     *
2345
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
2346
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
2347
     */
2348 View Code Duplication
    public function testAssignRoleToUserGroup()
2349
    {
2350
        $repository = $this->getRepository();
2351
        $roleService = $repository->getRoleService();
2352
2353
        /* BEGIN: Use Case */
2354
        $userGroup = $this->createUserGroupVersion1();
2355
2356
        // Load the existing "Administrator" role
2357
        $role = $roleService->loadRoleByIdentifier('Administrator');
2358
2359
        // Assign the "Administrator" role to the newly created user group
2360
        $roleService->assignRoleToUserGroup($role, $userGroup);
2361
2362
        // The assignments array will contain the new role<->group assignment
2363
        $roleAssignments = $roleService->getRoleAssignments($role);
2364
        /* END: Use Case */
2365
2366
        // Administrator + Example Group
2367
        $this->assertEquals(2, count($roleAssignments));
2368
    }
2369
2370
    /**
2371
     * Test for the assignRoleToUserGroup() method.
2372
     *
2373
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2374
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2375
     */
2376 View Code Duplication
    public function testAssignRoleToUserGroupWithRoleLimitation()
2377
    {
2378
        $repository = $this->getRepository();
2379
        $roleService = $repository->getRoleService();
2380
2381
        /* BEGIN: Use Case */
2382
        $userGroup = $this->createUserGroupVersion1();
2383
2384
        // Load the existing "Anonymous" role
2385
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2386
2387
        // Assign the "Anonymous" role to the newly created user group
2388
        $roleService->assignRoleToUserGroup(
2389
            $role,
2390
            $userGroup,
2391
            new SubtreeLimitation(
2392
                array(
2393
                    'limitationValues' => array('/1/43/'),
2394
                )
2395
            )
2396
        );
2397
2398
        // The assignments array will contain the new role<->group assignment
2399
        $roleAssignments = $roleService->getRoleAssignments($role);
2400
        /* END: Use Case */
2401
2402
        // Members + Partners + Anonymous + Example Group
2403
        $this->assertEquals(4, count($roleAssignments));
2404
2405
        // Get the role limitation
2406
        $roleLimitation = null;
2407
        foreach ($roleAssignments as $roleAssignment) {
2408
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2409
            if ($roleLimitation) {
2410
                break;
2411
            }
2412
        }
2413
2414
        $this->assertEquals(
2415
            new SubtreeLimitation(
2416
                array(
2417
                    'limitationValues' => array('/1/43/'),
2418
                )
2419
            ),
2420
            $roleLimitation
2421
        );
2422
2423
        // Test again to see values being merged
2424
        $roleService->assignRoleToUserGroup(
2425
            $role,
2426
            $userGroup,
2427
            new SubtreeLimitation(
2428
                array(
2429
                    'limitationValues' => array('/1/43/', '/1/2/'),
2430
                )
2431
            )
2432
        );
2433
2434
        // The assignments array will contain the new role<->user assignment
2435
        $roleAssignments = $roleService->getRoleAssignments($role);
2436
2437
        // Members + Partners + Anonymous + Example User
2438
        $this->assertEquals(5, count($roleAssignments));
2439
2440
        // Get the role limitation
2441
        $roleLimitations = [];
2442
        foreach ($roleAssignments as $roleAssignment) {
2443
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2444
            if ($roleLimitation) {
2445
                $this->assertInstanceOf(
2446
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2447
                    $roleAssignment
2448
                );
2449
                $roleLimitations[] = $roleLimitation;
2450
            }
2451
        }
2452
        array_multisort($roleLimitations);
2453
2454
        $this->assertEquals(
2455
            [
2456
                new SubtreeLimitation(
2457
                    array(
2458
                        'limitationValues' => array('/1/2/'),
2459
                    )
2460
                ),
2461
                new SubtreeLimitation(
2462
                    array(
2463
                        'limitationValues' => array('/1/43/'),
2464
                    )
2465
                ),
2466
            ],
2467
            $roleLimitations
2468
        );
2469
    }
2470
2471
    /**
2472
     * Test for the assignRoleToUserGroup() method.
2473
     *
2474
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2475
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
2476
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2477
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2478
     */
2479
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsLimitationValidationException()
2480
    {
2481
        $repository = $this->getRepository();
2482
2483
        $mainGroupId = $this->generateId('group', 4);
2484
        /* BEGIN: Use Case */
2485
        // $mainGroupId is the ID of the main "Users" group
2486
2487
        $userService = $repository->getUserService();
2488
        $roleService = $repository->getRoleService();
2489
2490
        $userGroup = $userService->loadUserGroup($mainGroupId);
2491
2492
        // Load the existing "Anonymous" role
2493
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2494
2495
        // Assign the "Anonymous" role to the newly created user group
2496
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
2497
        // does not exists
2498
        $roleService->assignRoleToUserGroup(
2499
            $role,
2500
            $userGroup,
2501
            new SubtreeLimitation(
2502
                array(
2503
                    'limitationValues' => array('/lorem/ipsum/42/'),
2504
                )
2505
            )
2506
        );
2507
        /* END: Use Case */
2508
    }
2509
2510
    /**
2511
     * Test for the assignRoleToUserGroup() method.
2512
     *
2513
     * Makes sure assigning role several times throws.
2514
     *
2515
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2516
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2517
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2518
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2519
     */
2520
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2521
    {
2522
        $repository = $this->getRepository();
2523
2524
        $mainGroupId = $this->generateId('group', 4);
2525
        /* BEGIN: Use Case */
2526
        // $mainGroupId is the ID of the main "Users" group
2527
2528
        $userService = $repository->getUserService();
2529
        $roleService = $repository->getRoleService();
2530
2531
        $userGroup = $userService->loadUserGroup($mainGroupId);
2532
2533
        // Load the existing "Anonymous" role
2534
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2535
2536
        // Assign the "Anonymous" role to the newly created user group
2537
        try {
2538
            $roleService->assignRoleToUserGroup(
2539
                $role,
2540
                $userGroup
2541
            );
2542
        } catch (Exception $e) {
2543
            $this->fail('Got exception at first valid attempt to assign role');
2544
        }
2545
2546
        // Re-Assign the "Anonymous" role to the newly created user group
2547
        // This call will fail with an InvalidArgumentException, because role is already assigned
2548
        $roleService->assignRoleToUserGroup(
2549
            $role,
2550
            $userGroup
2551
        );
2552
        /* END: Use Case */
2553
    }
2554
2555
    /**
2556
     * Test for the assignRoleToUserGroup() method.
2557
     *
2558
     * Makes sure assigning role several times with same limitations throws.
2559
     *
2560
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2561
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2562
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2563
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2564
     */
2565
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsInvalidArgumentException()
2566
    {
2567
        $repository = $this->getRepository();
2568
2569
        $mainGroupId = $this->generateId('group', 4);
2570
        /* BEGIN: Use Case */
2571
        // $mainGroupId is the ID of the main "Users" group
2572
2573
        $userService = $repository->getUserService();
2574
        $roleService = $repository->getRoleService();
2575
2576
        $userGroup = $userService->loadUserGroup($mainGroupId);
2577
2578
        // Load the existing "Anonymous" role
2579
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2580
2581
        // Assign the "Anonymous" role to the newly created user group
2582
        try {
2583
            $roleService->assignRoleToUserGroup(
2584
                $role,
2585
                $userGroup,
2586
                new SubtreeLimitation(
2587
                    array(
2588
                        'limitationValues' => array('/1/43/', '/1/2/'),
2589
                    )
2590
                )
2591
            );
2592
        } catch (Exception $e) {
2593
            $this->fail('Got exception at first valid attempt to assign role');
2594
        }
2595
2596
        // Re-Assign the "Anonymous" role to the newly created user group
2597
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2598
        $roleService->assignRoleToUserGroup(
2599
            $role,
2600
            $userGroup,
2601
            new SubtreeLimitation(
2602
                array(
2603
                    'limitationValues' => array('/1/43/'),
2604
                )
2605
            )
2606
        );
2607
        /* END: Use Case */
2608
    }
2609
2610
    /**
2611
     * Test for the unassignRoleFromUserGroup() method.
2612
     *
2613
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUserGroup()
2614
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2615
     */
2616 View Code Duplication
    public function testUnassignRoleFromUserGroup()
2617
    {
2618
        $repository = $this->getRepository();
2619
        $roleService = $repository->getRoleService();
2620
2621
        /* BEGIN: Use Case */
2622
        $userGroup = $this->createUserGroupVersion1();
2623
2624
        // Load the existing "Member" role
2625
        $role = $roleService->loadRoleByIdentifier('Member');
2626
2627
        // Assign the "Member" role to the newly created user group
2628
        $roleService->assignRoleToUserGroup($role, $userGroup);
2629
2630
        // Unassign group from role
2631
        $roleService->unassignRoleFromUserGroup($role, $userGroup);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...signRoleFromUserGroup() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} 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...
2632
2633
        // The assignments array will not contain the new role<->group assignment
2634
        $roleAssignments = $roleService->getRoleAssignments($role);
2635
        /* END: Use Case */
2636
2637
        // Members + Editors + Partners
2638
        $this->assertEquals(3, count($roleAssignments));
2639
    }
2640
2641
    /**
2642
     * Test for the unassignRoleFromUserGroup() method.
2643
     *
2644
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUserGroup()
2645
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2646
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUserGroup
2647
     */
2648 View Code Duplication
    public function testUnassignRoleFromUserGroupThrowsInvalidArgumentException()
2649
    {
2650
        $repository = $this->getRepository();
2651
        $roleService = $repository->getRoleService();
2652
2653
        /* BEGIN: Use Case */
2654
        $userGroup = $this->createUserGroupVersion1();
2655
2656
        // Load the existing "Member" role
2657
        $role = $roleService->loadRoleByIdentifier('Member');
2658
2659
        // This call will fail with a "InvalidArgumentException", because the
2660
        // user group does not have the "Member" role.
2661
        $roleService->unassignRoleFromUserGroup($role, $userGroup);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...signRoleFromUserGroup() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} 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...
2662
        /* END: Use Case */
2663
    }
2664
2665
    /**
2666
     * Test unassigning role by assignment.
2667
     *
2668
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2669
     */
2670
    public function testUnassignRoleByAssignment()
2671
    {
2672
        $repository = $this->getRepository();
2673
        $roleService = $repository->getRoleService();
2674
2675
        $role = $roleService->loadRole(2);
2676
        $user = $repository->getUserService()->loadUser(14);
2677
2678
        $originalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2679
2680
        $roleService->assignRoleToUser($role, $user);
2681
        $newAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2682
        self::assertEquals($originalAssignmentCount + 1, $newAssignmentCount);
2683
2684
        $assignments = $roleService->getRoleAssignmentsForUser($user);
2685
        $roleService->removeRoleAssignment($assignments[0]);
2686
        $finalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2687
        self::assertEquals($newAssignmentCount - 1, $finalAssignmentCount);
2688
    }
2689
2690
    /**
2691
     * Test unassigning role by assignment.
2692
     *
2693
     * But on current admin user so he lacks access to read roles.
2694
     *
2695
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2696
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2697
     */
2698 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsUnauthorizedException()
2699
    {
2700
        $repository = $this->getRepository();
2701
        $roleService = $repository->getRoleService();
2702
2703
        try {
2704
            $adminUserGroup = $repository->getUserService()->loadUserGroup(12);
2705
            $assignments = $roleService->getRoleAssignmentsForUserGroup($adminUserGroup);
2706
            $roleService->removeRoleAssignment($assignments[0]);
2707
        } catch (Exception $e) {
2708
            self::fail(
2709
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2710
            );
2711
        }
2712
2713
        $roleService->removeRoleAssignment($assignments[0]);
0 ignored issues
show
Bug introduced by
The variable $assignments 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...
2714
    }
2715
2716
    /**
2717
     * Test unassigning role by non-existing assignment.
2718
     *
2719
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2720
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2721
     */
2722 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsNotFoundException()
2723
    {
2724
        $repository = $this->getRepository();
2725
        $roleService = $repository->getRoleService();
2726
2727
        try {
2728
            $editorsUserGroup = $repository->getUserService()->loadUserGroup(13);
2729
            $assignments = $roleService->getRoleAssignmentsForUserGroup($editorsUserGroup);
2730
            $roleService->removeRoleAssignment($assignments[0]);
2731
        } catch (Exception $e) {
2732
            self::fail(
2733
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2734
            );
2735
        }
2736
2737
        $roleService->removeRoleAssignment($assignments[0]);
0 ignored issues
show
Bug introduced by
The variable $assignments 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...
2738
    }
2739
2740
    /**
2741
     * Test for the getRoleAssignmentsForUserGroup() method.
2742
     *
2743
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup()
2744
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2745
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2746
     */
2747 View Code Duplication
    public function testGetRoleAssignmentsForUserGroup()
2748
    {
2749
        $repository = $this->getRepository();
2750
        $roleService = $repository->getRoleService();
2751
2752
        /* BEGIN: Use Case */
2753
        $userGroup = $this->createUserGroupVersion1();
2754
2755
        // Instantiate a role create and add some policies
2756
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2757
2758
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2759
        // $roleCreate->mainLanguageCode = 'eng-US';
2760
2761
        $roleCreate->addPolicy(
2762
            $roleService->newPolicyCreateStruct('user', 'login')
2763
        );
2764
        $roleCreate->addPolicy(
2765
            $roleService->newPolicyCreateStruct('content', 'read')
2766
        );
2767
        $roleCreate->addPolicy(
2768
            $roleService->newPolicyCreateStruct('content', 'edit')
2769
        );
2770
2771
        // Create the new role instance
2772
        $roleDraft = $roleService->createRole($roleCreate);
2773
        $roleService->publishRoleDraft($roleDraft);
2774
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
2775
2776
        // Assign role to new user group
2777
        $roleService->assignRoleToUserGroup($role, $userGroup);
2778
2779
        // Load the currently assigned role
2780
        $roleAssignments = $roleService->getRoleAssignmentsForUserGroup($userGroup);
2781
        /* END: Use Case */
2782
2783
        $this->assertEquals(1, count($roleAssignments));
2784
        $this->assertInstanceOf(
2785
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2786
            reset($roleAssignments)
2787
        );
2788
    }
2789
2790
    /**
2791
     * Test for the loadPoliciesByUserId() method.
2792
     *
2793
     * @see \eZ\Publish\API\Repository\RoleService::loadPoliciesByUserId()
2794
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2795
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2796
     */
2797
    public function testLoadPoliciesByUserId()
2798
    {
2799
        $repository = $this->getRepository();
2800
2801
        $anonUserId = $this->generateId('user', 10);
2802
        /* BEGIN: Use Case */
2803
        // $anonUserId is the ID of the "Anonymous" user.
2804
2805
        $userService = $repository->getUserService();
2806
        $roleService = $repository->getRoleService();
2807
2808
        // Load "Anonymous" user
2809
        $user = $userService->loadUser($anonUserId);
2810
2811
        // Instantiate a role create and add some policies
2812
        $roleCreate = $roleService->newRoleCreateStruct('User Role');
2813
2814
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2815
        // $roleCreate->mainLanguageCode = 'eng-US';
2816
2817
        $roleCreate->addPolicy(
2818
            $roleService->newPolicyCreateStruct('notification', 'use')
2819
        );
2820
        $roleCreate->addPolicy(
2821
            $roleService->newPolicyCreateStruct('user', 'password')
2822
        );
2823
        $roleCreate->addPolicy(
2824
            $roleService->newPolicyCreateStruct('user', 'selfedit')
2825
        );
2826
2827
        // Create the new role instance
2828
        $roleDraft = $roleService->createRole($roleCreate);
2829
        $roleService->publishRoleDraft($roleDraft);
2830
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
2831
2832
        // Assign role to anon user
2833
        $roleService->assignRoleToUser($role, $user);
2834
2835
        // Load the currently assigned role
2836
        $policies = array();
2837
        foreach ($roleService->loadPoliciesByUserId($user->id) as $policy) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...:loadPoliciesByUserId() has been deprecated with message: Since 6.8, not currently in use as permission system needs to know about role assignment limitations.

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...
2838
            $policies[] = array($policy->roleId, $policy->module, $policy->function);
2839
        }
2840
        /* END: Use Case */
2841
        array_multisort($policies);
2842
2843
        $this->assertEquals(
2844
            array(
2845
                array(1, 'content', 'pdf'),
2846
                array(1, 'content', 'read'),
2847
                array(1, 'content', 'read'),
2848
                array(1, 'rss', 'feed'),
2849
                array(1, 'user', 'login'),
2850
                array(1, 'user', 'login'),
2851
                array(1, 'user', 'login'),
2852
                array(1, 'user', 'login'),
2853
                array($role->id, 'notification', 'use'),
2854
                array($role->id, 'user', 'password'),
2855
                array($role->id, 'user', 'selfedit'),
2856
            ),
2857
            $policies
2858
        );
2859
    }
2860
2861
    /**
2862
     * Test for the loadPoliciesByUserId() method.
2863
     *
2864
     * @see \eZ\Publish\API\Repository\RoleService::loadPoliciesByUserId()
2865
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2866
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadPoliciesByUserId
2867
     */
2868
    public function testLoadPoliciesByUserIdThrowsNotFoundException()
2869
    {
2870
        $repository = $this->getRepository();
2871
2872
        $nonExistingUserId = $this->generateId('user', self::DB_INT_MAX);
2873
        /* BEGIN: Use Case */
2874
        $roleService = $repository->getRoleService();
2875
2876
        // This call will fail with a "NotFoundException", because hopefully no
2877
        // user with an ID equal to self::DB_INT_MAX exists.
2878
        $roleService->loadPoliciesByUserId($nonExistingUserId);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...:loadPoliciesByUserId() has been deprecated with message: Since 6.8, not currently in use as permission system needs to know about role assignment limitations.

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...
2879
        /* END: Use Case */
2880
    }
2881
2882
    /**
2883
     * Test for the publishRoleDraft() method.
2884
     *
2885
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2886
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2887
     */
2888 View Code Duplication
    public function testPublishRoleDraft()
2889
    {
2890
        $repository = $this->getRepository();
2891
2892
        /* BEGIN: Use Case */
2893
        $roleService = $repository->getRoleService();
2894
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2895
2896
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2897
        // $roleCreate->mainLanguageCode = 'eng-US';
2898
2899
        $roleDraft = $roleService->createRole($roleCreate);
2900
2901
        $roleDraft = $roleService->addPolicyByRoleDraft(
2902
            $roleDraft,
2903
            $roleService->newPolicyCreateStruct('content', 'delete')
2904
        );
2905
        $roleDraft = $roleService->addPolicyByRoleDraft(
2906
            $roleDraft,
2907
            $roleService->newPolicyCreateStruct('content', 'create')
2908
        );
2909
2910
        $roleService->publishRoleDraft($roleDraft);
2911
        /* END: Use Case */
2912
2913
        $this->assertInstanceOf(
2914
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Role',
2915
            $roleService->loadRoleByIdentifier($roleCreate->identifier)
2916
        );
2917
    }
2918
2919
    /**
2920
     * Test for the publishRoleDraft() method.
2921
     *
2922
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2923
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2924
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
2925
     */
2926 View Code Duplication
    public function testPublishRoleDraftAddPolicies()
2927
    {
2928
        $repository = $this->getRepository();
2929
2930
        /* BEGIN: Use Case */
2931
        $roleService = $repository->getRoleService();
2932
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2933
2934
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2935
        // $roleCreate->mainLanguageCode = 'eng-US';
2936
2937
        $roleDraft = $roleService->createRole($roleCreate);
2938
2939
        $roleDraft = $roleService->addPolicyByRoleDraft(
2940
            $roleDraft,
2941
            $roleService->newPolicyCreateStruct('content', 'delete')
2942
        );
2943
        $roleDraft = $roleService->addPolicyByRoleDraft(
2944
            $roleDraft,
2945
            $roleService->newPolicyCreateStruct('content', 'create')
2946
        );
2947
2948
        $roleService->publishRoleDraft($roleDraft);
2949
        $role = $roleService->loadRoleByIdentifier($roleCreate->identifier);
2950
        /* END: Use Case */
2951
2952
        $actual = array();
2953
        foreach ($role->getPolicies() as $policy) {
2954
            $actual[] = array(
2955
                'module' => $policy->module,
2956
                'function' => $policy->function,
2957
            );
2958
        }
2959
        usort(
2960
            $actual,
2961
            function ($p1, $p2) {
2962
                return strcasecmp($p1['function'], $p2['function']);
2963
            }
2964
        );
2965
2966
        $this->assertEquals(
2967
            array(
2968
                array(
2969
                    'module' => 'content',
2970
                    'function' => 'create',
2971
                ),
2972
                array(
2973
                    'module' => 'content',
2974
                    'function' => 'delete',
2975
                ),
2976
            ),
2977
            $actual
2978
        );
2979
    }
2980
2981
    /**
2982
     * Create a user group fixture in a variable named <b>$userGroup</b>,.
2983
     *
2984
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
2985
     */
2986
    private function createUserGroupVersion1()
2987
    {
2988
        $repository = $this->getRepository();
2989
2990
        $mainGroupId = $this->generateId('group', 4);
2991
        /* BEGIN: Inline */
2992
        // $mainGroupId is the ID of the main "Users" group
2993
2994
        $roleService = $repository->getRoleService();
0 ignored issues
show
Unused Code introduced by
$roleService is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2995
        $userService = $repository->getUserService();
2996
2997
        // Load main group
2998
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
2999
3000
        // Instantiate a new create struct
3001
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
3002
        $userGroupCreate->setField('name', 'Example Group');
3003
3004
        // Create the new user group
3005
        $userGroup = $userService->createUserGroup(
3006
            $userGroupCreate,
3007
            $parentUserGroup
3008
        );
3009
        /* END: Inline */
3010
3011
        return $userGroup;
3012
    }
3013
}
3014