Completed
Push — ezp25533-http_cache_anonymous_... ( b956b0...914558 )
by
unknown
25:20
created

testLoadRoleByIdentifierThrowsNotFoundException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 13
rs 9.4285
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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\API\Repository\Tests;
12
13
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
14
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
15
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation;
16
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
17
use Exception;
18
19
/**
20
 * Test case for operations in the RoleService using in memory storage.
21
 *
22
 * The following IDs from the default eZ community edition database are used in
23
 * this test:
24
 *
25
 * <ul>
26
 *   <li>
27
 *     ContentType
28
 *     <ul>
29
 *       <li><strong>28</strong>: File</li>
30
 *       <li><strong>29</strong>: Flash</li>
31
 *       <li><strong>30</strong>: Image</li>
32
 *     </ul>
33
 *   </li>
34
 * <ul>
35
 *
36
 * @see eZ\Publish\API\Repository\RoleService
37
 * @group role
38
 */
39
class RoleServiceTest extends BaseTest
40
{
41
    /**
42
     * Test for the newRoleCreateStruct() method.
43
     *
44
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
45
     */
46
    public function testNewRoleCreateStruct()
47
    {
48
        $repository = $this->getRepository();
49
50
        $roleService = $repository->getRoleService();
51
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
52
53
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleCreateStruct', $roleCreate);
54
    }
55
56
    /**
57
     * Test for the newRoleCreateStruct() method.
58
     *
59
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
60
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
61
     */
62
    public function testNewRoleCreateStructSetsNamePropertyOnStruct()
63
    {
64
        $repository = $this->getRepository();
65
66
        /* BEGIN: Use Case */
67
68
        $roleService = $repository->getRoleService();
69
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
70
71
        /* END: Use Case */
72
73
        $this->assertEquals('roleName', $roleCreate->identifier);
74
    }
75
76
    /**
77
     * Test for the createRole() method.
78
     *
79
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
80
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
81
     */
82
    public function testCreateRole()
83
    {
84
        $repository = $this->getRepository();
85
86
        /* BEGIN: Use Case */
87
88
        $roleService = $repository->getRoleService();
89
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
90
91
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
92
        // $roleCreate->mainLanguageCode = 'eng-US';
93
94
        $role = $roleService->createRole($roleCreate);
95
96
        /* END: Use Case */
97
98
        $this->assertInstanceOf(
99
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
100
            $role
101
        );
102
103
        return [
104
            'createStruct' => $roleCreate,
105
            'role' => $role,
106
        ];
107
    }
108
109
    /**
110
     * Test for the createRole() method.
111
     *
112
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
113
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
114
     */
115
    public function testRoleCreateStructValues(array $data)
116
    {
117
        $createStruct = $data['createStruct'];
118
        $role = $data['role'];
119
120
        $this->assertEquals(
121
            [
122
                'identifier' => $createStruct->identifier,
123
                'policies' => $createStruct->policies,
124
            ],
125
            [
126
                'identifier' => $role->identifier,
127
                'policies' => $role->policies,
128
            ]
129
        );
130
        $this->assertNotNull($role->id);
131
132
        return $data;
133
    }
134
135
    /**
136
     * Test for the createRole() method.
137
     *
138
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
139
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
140
     */
141
    public function testCreateRoleWithPolicy()
142
    {
143
        $repository = $this->getRepository();
144
145
        /* BEGIN: Use Case */
146
147
        $roleService = $repository->getRoleService();
148
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
149
150
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
151
        // $roleCreate->mainLanguageCode = 'eng-US';
152
153
        // Create new subtree limitation
154
        $limitation = new SubtreeLimitation(
155
            array(
156
                'limitationValues' => array('/1/2/'),
157
            )
158
        );
159
160
        // Create policy create struct and add limitation to it
161
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'read');
162
        $policyCreate->addLimitation($limitation);
163
164
        // Add policy create struct to role create struct
165
        $roleCreate->addPolicy($policyCreate);
166
167
        $role = $roleService->createRole($roleCreate);
168
169
        /* END: Use Case */
170
171
        $this->assertInstanceOf(
172
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
173
            $role
174
        );
175
176
        return [
177
            'createStruct' => $roleCreate,
178
            'role' => $role,
179
        ];
180
    }
181
182
    /**
183
     * Test for the createRole() method.
184
     *
185
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
186
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithPolicy
187
     */
188
    public function testRoleCreateStructValuesWithPolicy(array $data)
189
    {
190
        $createStruct = $data['createStruct'];
191
        $role = $data['role'];
192
193
        $this->assertEquals(
194
            [
195
                'identifier' => $createStruct->identifier,
196
                'policy_module' => $createStruct->policies[0]->module,
197
                'policy_function' => $createStruct->policies[0]->function,
198
                'policy_limitation' => array_values($createStruct->policies[0]->limitations),
199
            ],
200
            [
201
                'identifier' => $role->identifier,
202
                'policy_module' => $role->policies[0]->module,
203
                'policy_function' => $role->policies[0]->function,
204
                'policy_limitation' => array_values($role->policies[0]->limitations),
205
            ]
206
        );
207
        $this->assertNotNull($role->id);
208
209
        return $data;
210
    }
211
212
    /**
213
     * Test for the createRoleDraft() method.
214
     *
215
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
216
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
217
     */
218 View Code Duplication
    public function testCreateRoleDraft()
219
    {
220
        $repository = $this->getRepository();
221
222
        /* BEGIN: Use Case */
223
224
        $roleService = $repository->getRoleService();
225
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
226
227
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
228
        // $roleCreate->mainLanguageCode = 'eng-US';
229
230
        $roleDraft = $roleService->createRole($roleCreate);
231
        $roleService->publishRoleDraft($roleDraft);
232
        $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...
233
        $newRoleDraft = $roleService->createRoleDraft($role);
234
235
        /* END: Use Case */
236
237
        $this->assertInstanceOf(
238
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
239
            $newRoleDraft
240
        );
241
    }
242
243
    /**
244
     * Test for the createRole() method.
245
     *
246
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
247
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
248
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
249
     */
250
    public function testCreateRoleThrowsInvalidArgumentException()
251
    {
252
        $repository = $this->getRepository();
253
254
        /* BEGIN: Use Case */
255
256
        $roleService = $repository->getRoleService();
257
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
258
259
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
260
        // $roleCreate->mainLanguageCode = 'eng-US';
261
262
        // This call will fail with an InvalidArgumentException, because Editor exists
263
        $roleService->createRole($roleCreate);
264
265
        /* END: Use Case */
266
    }
267
268
    /**
269
     * Test for the createRoleDraft() method.
270
     *
271
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
272
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
273
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
274
     */
275 View Code Duplication
    public function testCreateRoleDraftThrowsInvalidArgumentException()
276
    {
277
        $repository = $this->getRepository();
278
279
        /* BEGIN: Use Case */
280
281
        $roleService = $repository->getRoleService();
282
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
283
284
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
285
        // $roleCreate->mainLanguageCode = 'eng-US';
286
287
        $roleDraft = $roleService->createRole($roleCreate);
288
        $roleService->publishRoleDraft($roleDraft);
289
        $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...
290
        $roleService->createRoleDraft($role); // First role draft
291
292
        // This call will fail with an InvalidArgumentException, because there is already a draft
293
        $roleService->createRoleDraft($role);
294
295
        /* END: Use Case */
296
    }
297
298
    /**
299
     * Test for the createRole() method.
300
     *
301
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
302
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
303
     */
304 View Code Duplication
    public function testCreateRoleThrowsLimitationValidationException()
305
    {
306
        $repository = $this->getRepository();
307
308
        /* BEGIN: Use Case */
309
        $roleService = $repository->getRoleService();
310
311
        // Create new role create struct
312
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
313
314
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
315
        // $roleCreate->mainLanguageCode = 'eng-US';
316
317
        // Create new subtree limitation
318
        $limitation = new SubtreeLimitation(
319
            array(
320
                'limitationValues' => array('/mountain/forest/tree/42/'),
321
            )
322
        );
323
324
        // Create policy create struct and add limitation to it
325
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
326
        $policyCreate->addLimitation($limitation);
327
328
        // Add policy create struct to role create struct
329
        $roleCreate->addPolicy($policyCreate);
330
331
        // This call will fail with an LimitationValidationException, because subtree
332
        // "/mountain/forest/tree/42/" does not exist
333
        $roleService->createRole($roleCreate);
334
        /* END: Use Case */
335
    }
336
337
    /**
338
     * Test for the createRole() method.
339
     *
340
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
341
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
342
     */
343
    public function testCreateRoleInTransactionWithRollback()
344
    {
345
        $repository = $this->getRepository();
346
347
        /* BEGIN: Use Case */
348
349
        $roleService = $repository->getRoleService();
350
351
        $repository->beginTransaction();
352
353
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
354
355
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
356
        // $roleCreate->mainLanguageCode = 'eng-US';
357
358
        $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...
359
360
        $repository->rollback();
361
362
        try {
363
            // This call will fail with a "NotFoundException"
364
            $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...
365
        } catch (NotFoundException $e) {
366
            return;
367
        }
368
        /* END: Use Case */
369
370
        $this->fail('Role object still exists after rollback.');
371
    }
372
373
    /**
374
     * Test for the createRoleDraft() method.
375
     *
376
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
377
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
378
     */
379
    public function testCreateRoleDraftInTransactionWithRollback()
380
    {
381
        $repository = $this->getRepository();
382
383
        /* BEGIN: Use Case */
384
385
        $roleService = $repository->getRoleService();
386
387
        $repository->beginTransaction();
388
389
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
390
391
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
392
        // $roleCreate->mainLanguageCode = 'eng-US';
393
394
        $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...
395
396
        $repository->rollback();
397
398
        try {
399
            // This call will fail with a "NotFoundException"
400
            $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...
401
        } catch (NotFoundException $e) {
402
            return;
403
        }
404
        /* END: Use Case */
405
406
        $this->fail('Role draft object still exists after rollback.');
407
    }
408
409
    /**
410
     * Test for the loadRole() method.
411
     *
412
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
413
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
414
     */
415
    public function testLoadRole()
416
    {
417
        $repository = $this->getRepository();
418
419
        /* BEGIN: Use Case */
420
421
        $roleService = $repository->getRoleService();
422
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
423
424
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
425
        // $roleCreate->mainLanguageCode = 'eng-US';
426
427
        $roleDraft = $roleService->createRole($roleCreate);
428
        $roleService->publishRoleDraft($roleDraft);
429
430
        // Load the newly created role by its ID
431
        $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...
432
433
        /* END: Use Case */
434
435
        $this->assertEquals('roleName', $role->identifier);
436
    }
437
438
    /**
439
     * Test for the loadRoleDraft() method.
440
     *
441
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
442
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
443
     */
444
    public function testLoadRoleDraft()
445
    {
446
        $repository = $this->getRepository();
447
448
        /* BEGIN: Use Case */
449
450
        $roleService = $repository->getRoleService();
451
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
452
453
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
454
        // $roleCreate->mainLanguageCode = 'eng-US';
455
456
        $roleDraft = $roleService->createRole($roleCreate);
457
458
        // Load the newly created role by its ID
459
        $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...
460
461
        /* END: Use Case */
462
463
        $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...
464
    }
465
466
    public function testLoadRoleDraftByRoleId()
467
    {
468
        $repository = $this->getRepository();
469
470
        /* BEGIN: Use Case */
471
472
        $roleService = $repository->getRoleService();
473
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
474
475
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
476
        // $roleCreate->mainLanguageCode = 'eng-US';
477
478
        $role = $roleService->createRole($roleCreate);
479
        $roleService->publishRoleDraft($role);
480
481
        // Now create a new draft based on the role
482
        $newDraft = $roleService->createRoleDraft($role);
483
        $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...
484
485
        /* END: Use Case */
486
487
        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...
488
        self::assertInstanceOf('eZ\Publish\API\Repository\Values\User\RoleDraft', $loadedRoleDraft);
489
        self::assertEquals($newDraft, $loadedRoleDraft);
490
    }
491
492
    /**
493
     * Test for the loadRole() method.
494
     *
495
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
496
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
497
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
498
     */
499 View Code Duplication
    public function testLoadRoleThrowsNotFoundException()
500
    {
501
        $repository = $this->getRepository();
502
503
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
504
        /* BEGIN: Use Case */
505
506
        $roleService = $repository->getRoleService();
507
508
        // This call will fail with a NotFoundException, because no such role exists.
509
        $roleService->loadRole($nonExistingRoleId);
510
511
        /* END: Use Case */
512
    }
513
514
    /**
515
     * Test for the loadRoleDraft() method.
516
     *
517
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
518
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
519
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
520
     */
521 View Code Duplication
    public function testLoadRoleDraftThrowsNotFoundException()
522
    {
523
        $repository = $this->getRepository();
524
525
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
526
        /* BEGIN: Use Case */
527
528
        $roleService = $repository->getRoleService();
529
530
        // This call will fail with a NotFoundException, because no such role exists.
531
        $roleService->loadRoleDraft($nonExistingRoleId);
532
533
        /* END: Use Case */
534
    }
535
536
    /**
537
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
538
     */
539 View Code Duplication
    public function testLoadRoleDraftByRoleIdThrowsNotFoundException()
540
    {
541
        $repository = $this->getRepository();
542
543
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
544
        /* BEGIN: Use Case */
545
546
        $roleService = $repository->getRoleService();
547
548
        // This call will fail with a NotFoundException, because no such role exists.
549
        $roleService->loadRoleDraftByRoleId($nonExistingRoleId);
550
551
        /* END: Use Case */
552
    }
553
554
    /**
555
     * Test for the loadRoleByIdentifier() method.
556
     *
557
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
558
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
559
     */
560 View Code Duplication
    public function testLoadRoleByIdentifier()
561
    {
562
        $repository = $this->getRepository();
563
564
        /* BEGIN: Use Case */
565
566
        $roleService = $repository->getRoleService();
567
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
568
569
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
570
        // $roleCreate->mainLanguageCode = 'eng-US';
571
572
        $roleDraft = $roleService->createRole($roleCreate);
573
        $roleService->publishRoleDraft($roleDraft);
574
575
        // Load the newly created role by its identifier
576
        $role = $roleService->loadRoleByIdentifier('roleName');
577
578
        /* END: Use Case */
579
580
        $this->assertEquals('roleName', $role->identifier);
581
    }
582
583
    /**
584
     * Test for the loadRoleByIdentifier() method.
585
     *
586
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
587
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
588
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
589
     */
590
    public function testLoadRoleByIdentifierThrowsNotFoundException()
591
    {
592
        $repository = $this->getRepository();
593
594
        /* BEGIN: Use Case */
595
596
        $roleService = $repository->getRoleService();
597
598
        // This call will fail with a NotFoundException, because no such role exists.
599
        $roleService->loadRoleByIdentifier('MissingRole');
600
601
        /* END: Use Case */
602
    }
603
604
    /**
605
     * Test for the loadRoles() method.
606
     *
607
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
608
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
609
     */
610
    public function testLoadRoles()
611
    {
612
        $repository = $this->getRepository();
613
614
        /* BEGIN: Use Case */
615
616
        // First create a custom role
617
        $roleService = $repository->getRoleService();
618
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
619
620
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
621
        // $roleCreate->mainLanguageCode = 'eng-US';
622
623
        $roleDraft = $roleService->createRole($roleCreate);
624
        $roleService->publishRoleDraft($roleDraft);
625
626
        // Now load all available roles
627
        $roles = $roleService->loadRoles();
628
629
        foreach ($roles as $role) {
630
            if ($role->identifier === 'roleName') {
631
                break;
632
            }
633
        }
634
635
        /* END: Use Case */
636
637
        $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 629. 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...
638
    }
639
640
    /**
641
     * Test for the loadRoles() method.
642
     *
643
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
644
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
645
     */
646
    public function testLoadRolesReturnsExpectedSetOfDefaultRoles()
647
    {
648
        $repository = $this->getRepository();
649
650
        /* BEGIN: Use Case */
651
        $roleService = $repository->getRoleService();
652
653
        $roles = $roleService->loadRoles();
654
655
        $roleNames = array();
656
        foreach ($roles as $role) {
657
            $roleNames[] = $role->identifier;
658
        }
659
        /* END: Use Case */
660
661
        sort($roleNames);
662
663
        $this->assertEquals(
664
            array(
665
                'Administrator',
666
                'Anonymous',
667
                'Editor',
668
                'Member',
669
                'Partner',
670
            ),
671
            $roleNames
672
        );
673
    }
674
675
    /**
676
     * Test for the newRoleUpdateStruct() method.
677
     *
678
     * @see \eZ\Publish\API\Repository\RoleService::newRoleUpdateStruct()
679
     */
680
    public function testNewRoleUpdateStruct()
681
    {
682
        $repository = $this->getRepository();
683
684
        /* BEGIN: Use Case */
685
        $roleService = $repository->getRoleService();
686
        $roleUpdate = $roleService->newRoleUpdateStruct('newRole');
687
        /* END: Use Case */
688
689
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleUpdateStruct', $roleUpdate);
690
    }
691
692
    /**
693
     * Test for the updateRole() method.
694
     *
695
     * @see \eZ\Publish\API\Repository\RoleService::updateRole()
696
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleUpdateStruct
697
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
698
     */
699
    public function testUpdateRole()
700
    {
701
        $repository = $this->getRepository();
702
703
        /* BEGIN: Use Case */
704
        $roleService = $repository->getRoleService();
705
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
706
707
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
708
        // $roleCreate->mainLanguageCode = 'eng-US';
709
710
        $roleDraft = $roleService->createRole($roleCreate);
711
        $roleService->publishRoleDraft($roleDraft);
712
        $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...
713
714
        $roleUpdate = $roleService->newRoleUpdateStruct();
715
        $roleUpdate->identifier = 'updatedRole';
716
717
        $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...
718
        /* END: Use Case */
719
720
        // Now verify that our change was saved
721
        $role = $roleService->loadRoleByIdentifier('updatedRole');
722
723
        $this->assertEquals($role->id, $updatedRole->id);
724
    }
725
726
    /**
727
     * Test for the updateRoleDraft() method.
728
     *
729
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
730
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleUpdateStruct
731
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
732
     */
733 View Code Duplication
    public function testUpdateRoleDraft()
734
    {
735
        $repository = $this->getRepository();
736
737
        /* BEGIN: Use Case */
738
        $roleService = $repository->getRoleService();
739
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
740
741
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
742
        // $roleCreate->mainLanguageCode = 'eng-US';
743
744
        $roleDraft = $roleService->createRole($roleCreate);
745
746
        $roleUpdate = $roleService->newRoleUpdateStruct();
747
        $roleUpdate->identifier = 'updatedRole';
748
749
        $updatedRole = $roleService->updateRoleDraft($roleDraft, $roleUpdate);
750
        /* END: Use Case */
751
752
        // Now verify that our change was saved
753
        $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...
754
755
        $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...
756
    }
757
758
    /**
759
     * Test for the updateRole() method.
760
     *
761
     * @see \eZ\Publish\API\Repository\RoleService::updateRole()
762
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
763
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRole
764
     */
765 View Code Duplication
    public function testUpdateRoleThrowsInvalidArgumentException()
766
    {
767
        $repository = $this->getRepository();
768
769
        /* BEGIN: Use Case */
770
        $roleService = $repository->getRoleService();
771
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
772
773
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
774
        // $roleCreate->mainLanguageCode = 'eng-US';
775
776
        $roleDraft = $roleService->createRole($roleCreate);
777
        $roleService->publishRoleDraft($roleDraft);
778
        $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...
779
780
        $roleUpdate = $roleService->newRoleUpdateStruct();
781
        $roleUpdate->identifier = 'Editor';
782
783
        // This call will fail with an InvalidArgumentException, because Editor is a predefined role
784
        $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...
785
        /* END: Use Case */
786
    }
787
788
    /**
789
     * Test for the updateRoleDraft() method.
790
     *
791
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
792
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
793
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRoleDraft
794
     */
795
    public function testUpdateRoleDraftThrowsInvalidArgumentException()
796
    {
797
        $repository = $this->getRepository();
798
799
        /* BEGIN: Use Case */
800
        $roleService = $repository->getRoleService();
801
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
802
803
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
804
        // $roleCreate->mainLanguageCode = 'eng-US';
805
806
        $roleDraft = $roleService->createRole($roleCreate);
807
808
        $roleUpdate = $roleService->newRoleUpdateStruct();
809
        $roleUpdate->identifier = 'Editor';
810
811
        // This call will fail with an InvalidArgumentException, because Editor is a predefined role
812
        $roleService->updateRoleDraft($roleDraft, $roleUpdate);
813
        /* END: Use Case */
814
    }
815
816
    /**
817
     * Test for the deleteRole() method.
818
     *
819
     * @see \eZ\Publish\API\Repository\RoleService::deleteRole()
820
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
821
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
822
     */
823 View Code Duplication
    public function testDeleteRole()
824
    {
825
        $repository = $this->getRepository();
826
827
        /* BEGIN: Use Case */
828
        $roleService = $repository->getRoleService();
829
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
830
831
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
832
        // $roleCreate->mainLanguageCode = 'eng-US';
833
834
        $roleDraft = $roleService->createRole($roleCreate);
835
        $roleService->publishRoleDraft($roleDraft);
836
        $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...
837
838
        $roleService->deleteRole($role);
839
        /* END: Use Case */
840
841
        $this->assertEquals(5, count($roleService->loadRoles()));
842
    }
843
844
    /**
845
     * Test for the deleteRoleDraft() method.
846
     *
847
     * @see \eZ\Publish\API\Repository\RoleService::deleteRoleDraft()
848
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
849
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
850
     */
851
    public function testDeleteRoleDraft()
852
    {
853
        $repository = $this->getRepository();
854
855
        /* BEGIN: Use Case */
856
        $roleService = $repository->getRoleService();
857
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
858
859
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
860
        // $roleCreate->mainLanguageCode = 'eng-US';
861
862
        $roleDraft = $roleService->createRole($roleCreate);
863
        $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...
864
        $roleService->deleteRoleDraft($roleDraft);
865
866
        // This call will fail with a NotFoundException, because the draft no longer exists
867
        $roleService->loadRoleDraft($roleID);
868
        /* END: Use Case */
869
    }
870
871
    /**
872
     * Test for the newPolicyCreateStruct() method.
873
     *
874
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
875
     */
876
    public function testNewPolicyCreateStruct()
877
    {
878
        $repository = $this->getRepository();
879
880
        /* BEGIN: Use Case */
881
        $roleService = $repository->getRoleService();
882
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
883
        /* END: Use Case */
884
885
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyCreateStruct', $policyCreate);
886
    }
887
888
    /**
889
     * Test for the newPolicyCreateStruct() method.
890
     *
891
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
892
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
893
     */
894
    public function testNewPolicyCreateStructSetsStructProperties()
895
    {
896
        $repository = $this->getRepository();
897
898
        /* BEGIN: Use Case */
899
        $roleService = $repository->getRoleService();
900
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
901
        /* END: Use Case */
902
903
        $this->assertEquals(
904
            array('content', 'create'),
905
            array($policyCreate->module, $policyCreate->function)
906
        );
907
    }
908
909
    /**
910
     * Test for the addPolicy() method.
911
     *
912
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
913
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
914
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
915
     */
916 View Code Duplication
    public function testAddPolicy()
917
    {
918
        $repository = $this->getRepository();
919
920
        /* BEGIN: Use Case */
921
        $roleService = $repository->getRoleService();
922
923
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
924
925
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
926
        // $roleCreate->mainLanguageCode = 'eng-US';
927
928
        $roleDraft = $roleService->createRole($roleCreate);
929
        $roleService->publishRoleDraft($roleDraft);
930
        $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...
931
932
        $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...
933
            $role,
934
            $roleService->newPolicyCreateStruct('content', 'delete')
935
        );
936
        $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...
937
            $role,
938
            $roleService->newPolicyCreateStruct('content', 'create')
939
        );
940
        /* END: Use Case */
941
942
        $actual = array();
943
        foreach ($role->getPolicies() as $policy) {
944
            $actual[] = array(
945
                'module' => $policy->module,
946
                'function' => $policy->function,
947
            );
948
        }
949
        usort(
950
            $actual,
951
            function ($p1, $p2) {
952
                return strcasecmp($p1['function'], $p2['function']);
953
            }
954
        );
955
956
        $this->assertEquals(
957
            array(
958
                array(
959
                    'module' => 'content',
960
                    'function' => 'create',
961
                ),
962
                array(
963
                    'module' => 'content',
964
                    'function' => 'delete',
965
                ),
966
            ),
967
            $actual
968
        );
969
    }
970
971
    /**
972
     * Test for the addPolicyByRoleDraft() method.
973
     *
974
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
975
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
976
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
977
     */
978
    public function testAddPolicyByRoleDraft()
979
    {
980
        $repository = $this->getRepository();
981
982
        /* BEGIN: Use Case */
983
        $roleService = $repository->getRoleService();
984
985
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
986
987
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
988
        // $roleCreate->mainLanguageCode = 'eng-US';
989
990
        $roleDraft = $roleService->createRole($roleCreate);
991
992
        $roleDraft = $roleService->addPolicyByRoleDraft(
993
            $roleDraft,
994
            $roleService->newPolicyCreateStruct('content', 'delete')
995
        );
996
        $roleDraft = $roleService->addPolicyByRoleDraft(
997
            $roleDraft,
998
            $roleService->newPolicyCreateStruct('content', 'create')
999
        );
1000
        /* END: Use Case */
1001
1002
        $actual = array();
1003 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...
1004
            $actual[] = array(
1005
                'module' => $policy->module,
1006
                'function' => $policy->function,
1007
            );
1008
        }
1009
        usort(
1010
            $actual,
1011
            function ($p1, $p2) {
1012
                return strcasecmp($p1['function'], $p2['function']);
1013
            }
1014
        );
1015
1016
        $this->assertEquals(
1017
            array(
1018
                array(
1019
                    'module' => 'content',
1020
                    'function' => 'create',
1021
                ),
1022
                array(
1023
                    'module' => 'content',
1024
                    'function' => 'delete',
1025
                ),
1026
            ),
1027
            $actual
1028
        );
1029
    }
1030
1031
    /**
1032
     * Test for the addPolicy() method.
1033
     *
1034
     * @return array [\eZ\Publish\API\Repository\Values\User\Role, \eZ\Publish\API\Repository\Values\User\Policy]
1035
     *
1036
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
1037
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1038
     */
1039
    public function testAddPolicyUpdatesRole()
1040
    {
1041
        $repository = $this->getRepository();
1042
1043
        /* BEGIN: Use Case */
1044
        $roleService = $repository->getRoleService();
1045
1046
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1047
1048
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1049
        // $roleCreate->mainLanguageCode = 'eng-US';
1050
1051
        $roleDraft = $roleService->createRole($roleCreate);
1052
        $roleService->publishRoleDraft($roleDraft);
1053
        $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...
1054
1055
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1056
        $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...
1057
1058
        $policy = null;
1059
        foreach ($role->getPolicies() as $policy) {
1060
            if ($policy->module === 'content' && $policy->function === 'create') {
1061
                break;
1062
            }
1063
        }
1064
        /* END: Use Case */
1065
1066
        $this->assertInstanceOf(
1067
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1068
            $policy
1069
        );
1070
1071
        return array($role, $policy);
1072
    }
1073
1074
    /**
1075
     * Test for the addPolicyByRoleDraft() method.
1076
     *
1077
     * @return array [\eZ\Publish\API\Repository\Values\User\RoleDraft, \eZ\Publish\API\Repository\Values\User\Policy]
1078
     *
1079
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1080
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1081
     */
1082
    public function testAddPolicyByRoleDraftUpdatesRole()
1083
    {
1084
        $repository = $this->getRepository();
1085
1086
        /* BEGIN: Use Case */
1087
        $roleService = $repository->getRoleService();
1088
1089
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1090
1091
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1092
        // $roleCreate->mainLanguageCode = 'eng-US';
1093
1094
        $roleDraft = $roleService->createRole($roleCreate);
1095
1096
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1097
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreate);
1098
1099
        $policy = null;
1100
        foreach ($roleDraft->getPolicies() as $policy) {
1101
            if ($policy->module === 'content' && $policy->function === 'create') {
1102
                break;
1103
            }
1104
        }
1105
        /* END: Use Case */
1106
1107
        $this->assertInstanceOf(
1108
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1109
            $policy
1110
        );
1111
1112
        return array($roleDraft, $policy);
1113
    }
1114
1115
    /**
1116
     * Test for the addPolicy() method.
1117
     *
1118
     * @param array $roleAndPolicy
1119
     *
1120
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
1121
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyUpdatesRole
1122
     */
1123 View Code Duplication
    public function testAddPolicySetsPolicyProperties($roleAndPolicy)
1124
    {
1125
        list($role, $policy) = $roleAndPolicy;
1126
1127
        $this->assertEquals(
1128
            array($role->id, 'content', 'create'),
1129
            array($policy->roleId, $policy->module, $policy->function)
1130
        );
1131
    }
1132
1133
    /**
1134
     * Test for the addPolicyByRoleDraft() method.
1135
     *
1136
     * @param array $roleAndPolicy
1137
     *
1138
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1139
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1140
     */
1141 View Code Duplication
    public function testAddPolicyByRoleDraftSetsPolicyProperties($roleAndPolicy)
1142
    {
1143
        list($role, $policy) = $roleAndPolicy;
1144
1145
        $this->assertEquals(
1146
            array($role->id, 'content', 'create'),
1147
            array($policy->roleId, $policy->module, $policy->function)
1148
        );
1149
    }
1150
1151
    /**
1152
     * Test for the addPolicy() method.
1153
     *
1154
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
1155
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
1156
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1157
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
1158
     */
1159
    public function testAddPolicyThrowsLimitationValidationException()
1160
    {
1161
        $repository = $this->getRepository();
1162
1163
        /* BEGIN: Use Case */
1164
        $roleService = $repository->getRoleService();
1165
1166
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
1167
1168
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1169
        // $roleCreate->mainLanguageCode = 'eng-US';
1170
1171
        $roleDraft = $roleService->createRole($roleCreate);
1172
        $roleService->publishRoleDraft($roleDraft);
1173
        $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...
1174
1175
        // Create new subtree limitation
1176
        $limitation = new SubtreeLimitation(
1177
            array(
1178
                'limitationValues' => array('/mountain/forest/tree/42/'),
1179
            )
1180
        );
1181
1182
        // Create policy create struct and add limitation to it
1183
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'remove');
1184
        $policyCreateStruct->addLimitation($limitation);
1185
1186
        // This call will fail with an LimitationValidationException, because subtree
1187
        // "/mountain/forest/tree/42/" does not exist
1188
        $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...
1189
        /* END: Use Case */
1190
    }
1191
1192
    /**
1193
     * Test for the addPolicyByRoleDraft() method.
1194
     *
1195
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1196
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
1197
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1198
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
1199
     */
1200 View Code Duplication
    public function testAddPolicyByRoleDraftThrowsLimitationValidationException()
1201
    {
1202
        $repository = $this->getRepository();
1203
1204
        /* BEGIN: Use Case */
1205
        $roleService = $repository->getRoleService();
1206
1207
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
1208
1209
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1210
        // $roleCreate->mainLanguageCode = 'eng-US';
1211
1212
        $roleDraft = $roleService->createRole($roleCreate);
1213
1214
        // Create new subtree limitation
1215
        $limitation = new SubtreeLimitation(
1216
            array(
1217
                'limitationValues' => array('/mountain/forest/tree/42/'),
1218
            )
1219
        );
1220
1221
        // Create policy create struct and add limitation to it
1222
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'remove');
1223
        $policyCreateStruct->addLimitation($limitation);
1224
1225
        // This call will fail with an LimitationValidationException, because subtree
1226
        // "/mountain/forest/tree/42/" does not exist
1227
        $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
1228
        /* END: Use Case */
1229
    }
1230
1231
    /**
1232
     * Test for the createRole() method.
1233
     *
1234
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
1235
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyUpdatesRole
1236
     */
1237
    public function testCreateRoleWithAddPolicy()
1238
    {
1239
        $repository = $this->getRepository();
1240
1241
        /* BEGIN: Use Case */
1242
        $roleService = $repository->getRoleService();
1243
1244
        // Instantiate a new create struct
1245
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1246
1247
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1248
        // $roleCreate->mainLanguageCode = 'eng-US';
1249
1250
        // Add some role policies
1251
        $roleCreate->addPolicy(
1252
            $roleService->newPolicyCreateStruct(
1253
                'content',
1254
                'read'
1255
            )
1256
        );
1257
        $roleCreate->addPolicy(
1258
            $roleService->newPolicyCreateStruct(
1259
                'content',
1260
                'translate'
1261
            )
1262
        );
1263
1264
        // Create new role instance
1265
        $roleDraft = $roleService->createRole($roleCreate);
1266
        $roleService->publishRoleDraft($roleDraft);
1267
        $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...
1268
1269
        $policies = array();
1270 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...
1271
            $policies[] = array('module' => $policy->module, 'function' => $policy->function);
1272
        }
1273
        /* END: Use Case */
1274
        array_multisort($policies);
1275
1276
        $this->assertEquals(
1277
            array(
1278
                array(
1279
                    'module' => 'content',
1280
                    'function' => 'read',
1281
                ),
1282
                array(
1283
                    'module' => 'content',
1284
                    'function' => 'translate',
1285
                ),
1286
            ),
1287
            $policies
1288
        );
1289
    }
1290
1291
    /**
1292
     * Test for the createRoleDraft() method.
1293
     *
1294
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
1295
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1296
     */
1297
    public function testCreateRoleDraftWithAddPolicy()
1298
    {
1299
        $repository = $this->getRepository();
1300
1301
        /* BEGIN: Use Case */
1302
        $roleService = $repository->getRoleService();
1303
1304
        // Instantiate a new create struct
1305
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1306
1307
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1308
        // $roleCreate->mainLanguageCode = 'eng-US';
1309
1310
        // Add some role policies
1311
        $roleCreate->addPolicy(
1312
            $roleService->newPolicyCreateStruct(
1313
                'content',
1314
                'read'
1315
            )
1316
        );
1317
        $roleCreate->addPolicy(
1318
            $roleService->newPolicyCreateStruct(
1319
                'content',
1320
                'translate'
1321
            )
1322
        );
1323
1324
        // Create new role instance
1325
        $roleDraft = $roleService->createRole($roleCreate);
1326
1327
        $policies = array();
1328 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...
1329
            $policies[] = array('module' => $policy->module, 'function' => $policy->function);
1330
        }
1331
        /* END: Use Case */
1332
1333
        $this->assertEquals(
1334
            array(
1335
                array(
1336
                    'module' => 'content',
1337
                    'function' => 'read',
1338
                ),
1339
                array(
1340
                    'module' => 'content',
1341
                    'function' => 'translate',
1342
                ),
1343
            ),
1344
            $policies
1345
        );
1346
    }
1347
1348
    /**
1349
     * Test for the newPolicyUpdateStruct() method.
1350
     *
1351
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyUpdateStruct()
1352
     */
1353
    public function testNewPolicyUpdateStruct()
1354
    {
1355
        $repository = $this->getRepository();
1356
1357
        /* BEGIN: Use Case */
1358
        $roleService = $repository->getRoleService();
1359
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1360
        /* END: Use Case */
1361
1362
        $this->assertInstanceOf(
1363
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyUpdateStruct',
1364
            $policyUpdate
1365
        );
1366
    }
1367
1368
    public function testUpdatePolicyNoLimitation()
1369
    {
1370
        $repository = $this->getRepository();
1371
1372
        /* BEGIN: Use Case */
1373
        $roleService = $repository->getRoleService();
1374
1375
        // Instantiate new policy create
1376
        $policyCreate = $roleService->newPolicyCreateStruct('foo', 'bar');
1377
1378
        // Instantiate a role create and add the policy create
1379
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1380
1381
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1382
        // $roleCreate->mainLanguageCode = 'eng-US';
1383
1384
        $roleCreate->addPolicy($policyCreate);
1385
1386
        // Create a new role instance.
1387
        $roleDraft = $roleService->createRole($roleCreate);
1388
        $roleService->publishRoleDraft($roleDraft);
1389
        $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...
1390
1391
        // Search for the new policy instance
1392
        $policy = null;
1393
        foreach ($role->getPolicies() as $policy) {
1394
            if ($policy->module === 'foo' && $policy->function === 'bar') {
1395
                break;
1396
            }
1397
        }
1398
1399
        // Create an update struct
1400
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1401
1402
        // Update the the policy
1403
        $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...
1404
        /* END: Use Case */
1405
1406
        $this->assertInstanceOf(
1407
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1408
            $policy
1409
        );
1410
1411
        self::assertEquals(array(), $policy->getLimitations());
1412
    }
1413
1414
    /**
1415
     * Test for the updatePolicy() method.
1416
     *
1417
     * @return array
1418
     *
1419
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1420
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1421
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1422
     */
1423
    public function testUpdatePolicy()
1424
    {
1425
        $repository = $this->getRepository();
1426
1427
        /* BEGIN: Use Case */
1428
        $roleService = $repository->getRoleService();
1429
1430
        // Instantiate new policy create
1431
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'translate');
1432
1433
        // Add some limitations for the new policy
1434
        $policyCreate->addLimitation(
1435
            new LanguageLimitation(
1436
                array(
1437
                    'limitationValues' => array('eng-US', 'eng-GB'),
1438
                )
1439
            )
1440
        );
1441
1442
        // Instantiate a role create and add the policy create
1443
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1444
1445
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1446
        // $roleCreate->mainLanguageCode = 'eng-US';
1447
1448
        $roleCreate->addPolicy($policyCreate);
1449
1450
        // Create a new role instance.
1451
        $roleDraft = $roleService->createRole($roleCreate);
1452
        $roleService->publishRoleDraft($roleDraft);
1453
        $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...
1454
1455
        // Search for the new policy instance
1456
        $policy = null;
1457
        foreach ($role->getPolicies() as $policy) {
1458
            if ($policy->module === 'content' && $policy->function === 'translate') {
1459
                break;
1460
            }
1461
        }
1462
1463
        // Create an update struct and set a modified limitation
1464
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1465
        $policyUpdate->addLimitation(
1466
            new ContentTypeLimitation(
1467
                array(
1468
                    'limitationValues' => array(29, 30),
1469
                )
1470
            )
1471
        );
1472
1473
        // Update the the policy
1474
        $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...
1475
        /* END: Use Case */
1476
1477
        $this->assertInstanceOf(
1478
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1479
            $policy
1480
        );
1481
1482
        return array($roleService->loadRole($role->id), $policy);
1483
    }
1484
1485
    /**
1486
     * Test for the updatePolicy() method.
1487
     *
1488
     * @param array $roleAndPolicy
1489
     *
1490
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1491
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicy
1492
     */
1493
    public function testUpdatePolicyUpdatesLimitations($roleAndPolicy)
1494
    {
1495
        list($role, $policy) = $roleAndPolicy;
1496
1497
        $this->assertEquals(
1498
            array(
1499
                new ContentTypeLimitation(
1500
                    array(
1501
                        'limitationValues' => array(29, 30),
1502
                    )
1503
                ),
1504
            ),
1505
            $policy->getLimitations()
1506
        );
1507
1508
        return $role;
1509
    }
1510
1511
    /**
1512
     * Test for the updatePolicy() method.
1513
     *
1514
     * @param \eZ\Publish\API\Repository\Values\User\Role $role
1515
     *
1516
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1517
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyUpdatesLimitations
1518
     */
1519
    public function testUpdatePolicyUpdatesRole($role)
1520
    {
1521
        $limitations = array();
1522
        foreach ($role->getPolicies() as $policy) {
1523
            foreach ($policy->getLimitations() as $limitation) {
1524
                $limitations[] = $limitation;
1525
            }
1526
        }
1527
1528
        $this->assertCount(1, $limitations);
1529
        $this->assertInstanceOf(
1530
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Limitation',
1531
            $limitations[0]
1532
        );
1533
1534
        $expectedData = array(
1535
            'limitationValues' => array(29, 30),
1536
        );
1537
        $this->assertPropertiesCorrectUnsorted(
1538
            $expectedData,
1539
            $limitations[0]
1540
        );
1541
    }
1542
1543
    /**
1544
     * Test for the updatePolicy() method.
1545
     *
1546
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
1547
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
1548
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1549
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1550
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1551
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
1552
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
1553
     */
1554
    public function testUpdatePolicyThrowsLimitationValidationException()
1555
    {
1556
        $repository = $this->getRepository();
1557
1558
        /* BEGIN: Use Case */
1559
        $roleService = $repository->getRoleService();
1560
1561
        // Instantiate new policy create
1562
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
1563
1564
        // Add some limitations for the new policy
1565
        $policyCreate->addLimitation(
1566
            new SubtreeLimitation(
1567
                array(
1568
                    'limitationValues' => array('/1/2/'),
1569
                )
1570
            )
1571
        );
1572
1573
        // Instantiate a role create and add the policy create
1574
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1575
1576
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1577
        // $roleCreate->mainLanguageCode = 'eng-US';
1578
1579
        $roleCreate->addPolicy($policyCreate);
1580
1581
        // Create a new role instance.
1582
        $roleDraft = $roleService->createRole($roleCreate);
1583
        $roleService->publishRoleDraft($roleDraft);
1584
        $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...
1585
1586
        // Search for the new policy instance
1587
        $policy = null;
1588
        foreach ($role->getPolicies() as $policy) {
1589
            if ($policy->module === 'content' && $policy->function === 'remove') {
1590
                break;
1591
            }
1592
        }
1593
1594
        // Create an update struct and set a modified limitation
1595
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1596
        $policyUpdate->addLimitation(
1597
            new SubtreeLimitation(
1598
                array(
1599
                    'limitationValues' => array('/mountain/forest/tree/42/'),
1600
                )
1601
            )
1602
        );
1603
1604
        // This call will fail with an LimitationValidationException, because subtree
1605
        // "/mountain/forest/tree/42/" does not exist
1606
        $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...
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...
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...
1607
        /* END: Use Case */
1608
    }
1609
1610
    /**
1611
     * Test for the removePolicy() method.
1612
     *
1613
     * @see \eZ\Publish\API\Repository\RoleService::removePolicy()
1614
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1615
     */
1616 View Code Duplication
    public function testRemovePolicy()
1617
    {
1618
        $repository = $this->getRepository();
1619
1620
        /* BEGIN: Use Case */
1621
        $roleService = $repository->getRoleService();
1622
1623
        // Instantiate a new role create
1624
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1625
1626
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1627
        // $roleCreate->mainLanguageCode = 'eng-US';
1628
1629
        // Create a new role with two policies
1630
        $roleDraft = $roleService->createRole($roleCreate);
1631
        $roleService->addPolicyByRoleDraft(
1632
            $roleDraft,
1633
            $roleService->newPolicyCreateStruct('content', 'create')
1634
        );
1635
        $roleService->addPolicyByRoleDraft(
1636
            $roleDraft,
1637
            $roleService->newPolicyCreateStruct('content', 'delete')
1638
        );
1639
        $roleService->publishRoleDraft($roleDraft);
1640
        $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...
1641
1642
        // Delete all policies from the new role
1643
        foreach ($role->getPolicies() as $policy) {
1644
            $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...
1645
        }
1646
        /* END: Use Case */
1647
1648
        $this->assertSame(array(), $role->getPolicies());
1649
    }
1650
1651
    /**
1652
     * Test for the removePolicyByRoleDraft() method.
1653
     *
1654
     * @see \eZ\Publish\API\Repository\RoleService::removePolicyByRoleDraft()
1655
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1656
     */
1657 View Code Duplication
    public function testRemovePolicyByRoleDraft()
1658
    {
1659
        $repository = $this->getRepository();
1660
1661
        /* BEGIN: Use Case */
1662
        $roleService = $repository->getRoleService();
1663
1664
        // Instantiate a new role create
1665
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1666
1667
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1668
        // $roleCreate->mainLanguageCode = 'eng-US';
1669
1670
        // Create a new role with two policies
1671
        $roleDraft = $roleService->createRole($roleCreate);
1672
        $roleService->addPolicyByRoleDraft(
1673
            $roleDraft,
1674
            $roleService->newPolicyCreateStruct('content', 'create')
1675
        );
1676
        $roleService->addPolicyByRoleDraft(
1677
            $roleDraft,
1678
            $roleService->newPolicyCreateStruct('content', 'delete')
1679
        );
1680
1681
        // Delete all policies from the new role
1682
        foreach ($roleDraft->getPolicies() as $policy) {
1683
            $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...
1684
        }
1685
        /* END: Use Case */
1686
1687
        $this->assertSame(array(), $roleDraft->getPolicies());
1688
    }
1689
1690
    /**
1691
     * Test for the deletePolicy() method.
1692
     *
1693
     * @see \eZ\Publish\API\Repository\RoleService::deletePolicy()
1694
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
1695
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
1696
     */
1697 View Code Duplication
    public function testDeletePolicy()
1698
    {
1699
        $repository = $this->getRepository();
1700
1701
        /* BEGIN: Use Case */
1702
        $roleService = $repository->getRoleService();
1703
1704
        // Instantiate a new role create
1705
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1706
1707
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1708
        // $roleCreate->mainLanguageCode = 'eng-US';
1709
1710
        // Create a new role with two policies
1711
        $roleDraft = $roleService->createRole($roleCreate);
1712
        $roleService->addPolicyByRoleDraft(
1713
            $roleDraft,
1714
            $roleService->newPolicyCreateStruct('content', 'create')
1715
        );
1716
        $roleService->addPolicyByRoleDraft(
1717
            $roleDraft,
1718
            $roleService->newPolicyCreateStruct('content', 'delete')
1719
        );
1720
        $roleService->publishRoleDraft($roleDraft);
1721
        $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...
1722
1723
        // Delete all policies from the new role
1724
        foreach ($role->getPolicies() as $policy) {
1725
            $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...
1726
        }
1727
        /* END: Use Case */
1728
1729
        $role = $roleService->loadRole($role->id);
1730
        $this->assertSame(array(), $role->getPolicies());
1731
    }
1732
1733
    /**
1734
     * Test loading user/group role assignments.
1735
     *
1736
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment
1737
     *
1738
     * @covers \eZ\Publish\API\Repository\RoleService::loadRoleAssignment
1739
     */
1740
    public function testLoadRoleAssignment()
1741
    {
1742
        $repository = $this->getRepository();
1743
1744
        /* BEGIN: Use Case */
1745
        $roleService = $repository->getRoleService();
1746
1747
        // Assignment to user group
1748
        $groupRoleAssignment = $roleService->loadRoleAssignment(25);
1749
1750
        // Assignment to user
1751
        $role = $roleService->loadRole(2);
1752
        $user = $repository->getUserService()->loadUser(14);
1753
        $roleService->assignRoleToUser($role, $user);
1754
        $userRoleAssignments = $roleService->getRoleAssignmentsForUser($user);
1755
1756
        $userRoleAssignment = $roleService->loadRoleAssignment($userRoleAssignments[0]->id);
1757
        /* END: Use Case */
1758
1759
        $this->assertInstanceOf(
1760
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1761
            $groupRoleAssignment
1762
        );
1763
1764
        $this->assertEquals(
1765
            [
1766
                12,
1767
                2,
1768
                25,
1769
            ],
1770
            [
1771
                $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...
1772
                $groupRoleAssignment->role->id,
1773
                $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...
1774
            ]
1775
        );
1776
1777
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment', $userRoleAssignment);
1778
        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...
1779
1780
        return $groupRoleAssignment;
1781
    }
1782
1783
    /**
1784
     * Test for the getRoleAssignments() method.
1785
     *
1786
     * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[]
1787
     *
1788
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1789
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1790
     */
1791
    public function testGetRoleAssignments()
1792
    {
1793
        $repository = $this->getRepository();
1794
1795
        /* BEGIN: Use Case */
1796
        $roleService = $repository->getRoleService();
1797
1798
        // Load the editor role
1799
        $role = $roleService->loadRoleByIdentifier('Editor');
1800
1801
        // Load all assigned users and user groups
1802
        $roleAssignments = $roleService->getRoleAssignments($role);
1803
1804
        /* END: Use Case */
1805
1806
        $this->assertEquals(2, count($roleAssignments));
1807
        $this->assertInstanceOf(
1808
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1809
            $roleAssignments[0]
1810
        );
1811
        $this->assertInstanceOf(
1812
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1813
            $roleAssignments[1]
1814
        );
1815
1816
        return $roleAssignments;
1817
    }
1818
1819
    /**
1820
     * Test for the getRoleAssignments() method.
1821
     *
1822
     * @param \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $roleAssignments
1823
     *
1824
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1825
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1826
     */
1827
    public function testGetRoleAssignmentsContainExpectedLimitation(array $roleAssignments)
1828
    {
1829
        $this->assertEquals(
1830
            'Subtree',
1831
            reset($roleAssignments)->limitation->getIdentifier()
1832
        );
1833
    }
1834
1835
    /**
1836
     * Test for the assignRoleToUser() method.
1837
     *
1838
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser()
1839
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1840
     */
1841 View Code Duplication
    public function testAssignRoleToUser()
1842
    {
1843
        $repository = $this->getRepository();
1844
        $roleService = $repository->getRoleService();
1845
1846
        /* BEGIN: Use Case */
1847
        $user = $this->createUserVersion1();
1848
1849
        // Load the existing "Administrator" role
1850
        $role = $roleService->loadRoleByIdentifier('Administrator');
1851
1852
        // Assign the "Administrator" role to the newly created user
1853
        $roleService->assignRoleToUser($role, $user);
1854
1855
        // The assignments array will contain the new role<->user assignment
1856
        $roleAssignments = $roleService->getRoleAssignments($role);
1857
        /* END: Use Case */
1858
1859
        // Administrator + Example User
1860
        $this->assertEquals(2, count($roleAssignments));
1861
    }
1862
1863
    /**
1864
     * Test for the assignRoleToUser() method.
1865
     *
1866
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1867
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1868
     */
1869 View Code Duplication
    public function testAssignRoleToUserWithRoleLimitation()
1870
    {
1871
        $repository = $this->getRepository();
1872
        $roleService = $repository->getRoleService();
1873
1874
        /* BEGIN: Use Case */
1875
        $user = $this->createUserVersion1();
1876
1877
        // Load the existing "Anonymous" role
1878
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1879
1880
        // Assign the "Anonymous" role to the newly created user
1881
        $roleService->assignRoleToUser(
1882
            $role,
1883
            $user,
1884
            new SubtreeLimitation(
1885
                array(
1886
                    'limitationValues' => array('/1/43/'),
1887
                )
1888
            )
1889
        );
1890
1891
        // The assignments array will contain the new role<->user assignment
1892
        $roleAssignments = $roleService->getRoleAssignments($role);
1893
        /* END: Use Case */
1894
1895
        // Members + Partners + Anonymous + Example User
1896
        $this->assertEquals(4, count($roleAssignments));
1897
1898
        // Get the role limitation
1899
        $roleLimitation = null;
1900
        foreach ($roleAssignments as $roleAssignment) {
1901
            $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...
1902
            if ($roleLimitation) {
1903
                $this->assertInstanceOf(
1904
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1905
                    $roleAssignment
1906
                );
1907
                break;
1908
            }
1909
        }
1910
1911
        $this->assertEquals(
1912
            new SubtreeLimitation(
1913
                array(
1914
                    'limitationValues' => array('/1/43/'),
1915
                )
1916
            ),
1917
            $roleLimitation
1918
        );
1919
1920
        // Test again to see values being merged
1921
        $roleService->assignRoleToUser(
1922
            $role,
1923
            $user,
1924
            new SubtreeLimitation(
1925
                array(
1926
                    'limitationValues' => array('/1/43/', '/1/2/'),
1927
                )
1928
            )
1929
        );
1930
1931
        // The assignments array will contain the new role<->user assignment
1932
        $roleAssignments = $roleService->getRoleAssignments($role);
1933
1934
        // Members + Partners + Anonymous + Example User
1935
        $this->assertEquals(5, count($roleAssignments));
1936
1937
        // Get the role limitation
1938
        $roleLimitations = [];
1939
        foreach ($roleAssignments as $roleAssignment) {
1940
            $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...
1941
            if ($roleLimitation) {
1942
                $this->assertInstanceOf(
1943
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1944
                    $roleAssignment
1945
                );
1946
                $roleLimitations[] = $roleLimitation;
1947
            }
1948
        }
1949
        array_multisort($roleLimitations);
1950
1951
        $this->assertEquals(
1952
            [
1953
                new SubtreeLimitation(
1954
                    array(
1955
                        'limitationValues' => array('/1/2/'),
1956
                    )
1957
                ),
1958
                new SubtreeLimitation(
1959
                    array(
1960
                        'limitationValues' => array('/1/43/'),
1961
                    )
1962
                ),
1963
            ],
1964
            $roleLimitations
1965
        );
1966
    }
1967
1968
    /**
1969
     * Test for the assignRoleToUser() method.
1970
     *
1971
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1972
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
1973
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1974
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1975
     */
1976
    public function testAssignRoleToUserWithRoleLimitationThrowsLimitationValidationException()
1977
    {
1978
        $repository = $this->getRepository();
1979
1980
        /* BEGIN: Use Case */
1981
        $roleService = $repository->getRoleService();
1982
1983
        // Load the existing "Anonymous" role
1984
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1985
1986
        // Get current user
1987
        $currentUser = $repository->getCurrentUser();
1988
1989
        // Assign the "Anonymous" role to the current user
1990
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
1991
        // does not exists
1992
        $roleService->assignRoleToUser(
1993
            $role,
1994
            $currentUser,
1995
            new SubtreeLimitation(
1996
                array(
1997
                    'limitationValues' => array('/lorem/ipsum/42/'),
1998
                )
1999
            )
2000
        );
2001
        /* END: Use Case */
2002
    }
2003
2004
    /**
2005
     * Test for the assignRoleToUser() method.
2006
     *
2007
     * Makes sure assigning role several times throws.
2008
     *
2009
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2010
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2011
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2012
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2013
     */
2014 View Code Duplication
    public function testAssignRoleToUserThrowsInvalidArgumentException()
2015
    {
2016
        $repository = $this->getRepository();
2017
2018
        /* BEGIN: Use Case */
2019
        $roleService = $repository->getRoleService();
2020
2021
        // Load the existing "Anonymous" role
2022
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2023
2024
        // Get current user
2025
        $currentUser = $repository->getCurrentUser();
2026
2027
        // Assign the "Anonymous" role to the current user
2028
        try {
2029
            $roleService->assignRoleToUser(
2030
                $role,
2031
                $currentUser
2032
            );
2033
        } catch (Exception $e) {
2034
            $this->fail('Got exception at first valid attempt to assign role');
2035
        }
2036
2037
        // Re-Assign the "Anonymous" role to the current user
2038
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2039
        $roleService->assignRoleToUser(
2040
            $role,
2041
            $currentUser
2042
        );
2043
        /* END: Use Case */
2044
    }
2045
2046
    /**
2047
     * Test for the assignRoleToUser() method.
2048
     *
2049
     * Makes sure assigning role several times with same limitations throws.
2050
     *
2051
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2052
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2053
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2054
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2055
     */
2056
    public function testAssignRoleToUserWithRoleLimitationThrowsInvalidArgumentException()
2057
    {
2058
        $repository = $this->getRepository();
2059
2060
        /* BEGIN: Use Case */
2061
        $roleService = $repository->getRoleService();
2062
2063
        // Load the existing "Anonymous" role
2064
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2065
2066
        // Get current user
2067
        $currentUser = $repository->getCurrentUser();
2068
2069
        // Assign the "Anonymous" role to the current user
2070
        try {
2071
            $roleService->assignRoleToUser(
2072
                $role,
2073
                $currentUser,
2074
                new SubtreeLimitation(
2075
                    array(
2076
                        'limitationValues' => array('/1/43/', '/1/2/'),
2077
                    )
2078
                )
2079
            );
2080
        } catch (Exception $e) {
2081
            $this->fail('Got exception at first valid attempt to assign role');
2082
        }
2083
2084
        // Re-Assign the "Anonymous" role to the current user
2085
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2086
        $roleService->assignRoleToUser(
2087
            $role,
2088
            $currentUser,
2089
            new SubtreeLimitation(
2090
                array(
2091
                    'limitationValues' => array('/1/43/'),
2092
                )
2093
            )
2094
        );
2095
        /* END: Use Case */
2096
    }
2097
2098
    /**
2099
     * Test for the unassignRoleFromUser() method.
2100
     *
2101
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUser()
2102
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2103
     */
2104 View Code Duplication
    public function testUnassignRoleFromUser()
2105
    {
2106
        $repository = $this->getRepository();
2107
        $roleService = $repository->getRoleService();
2108
2109
        /* BEGIN: Use Case */
2110
        $user = $this->createUserVersion1();
2111
2112
        // Load the existing "Member" role
2113
        $role = $roleService->loadRoleByIdentifier('Member');
2114
2115
        // Assign the "Member" role to the newly created user
2116
        $roleService->assignRoleToUser($role, $user);
2117
2118
        // Unassign user from role
2119
        $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...
2120
2121
        // The assignments array will not contain the new role<->user assignment
2122
        $roleAssignments = $roleService->getRoleAssignments($role);
2123
        /* END: Use Case */
2124
2125
        // Members + Editors + Partners
2126
        $this->assertEquals(3, count($roleAssignments));
2127
    }
2128
2129
    /**
2130
     * Test for the unassignRoleFromUser() method.
2131
     *
2132
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUser()
2133
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2134
     */
2135 View Code Duplication
    public function testUnassignRoleFromUserThrowsInvalidArgumentException()
2136
    {
2137
        $repository = $this->getRepository();
2138
        $roleService = $repository->getRoleService();
2139
2140
        /* BEGIN: Use Case */
2141
        $user = $this->createUserVersion1();
2142
2143
        // Load the existing "Member" role
2144
        $role = $roleService->loadRoleByIdentifier('Member');
2145
2146
        // This call will fail with a "InvalidArgumentException", because the
2147
        // user does not have the "Member" role.
2148
        $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...
2149
        /* END: Use Case */
2150
    }
2151
2152
    /**
2153
     * Test for the getRoleAssignmentsForUser() method.
2154
     *
2155
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2156
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2157
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2158
     */
2159 View Code Duplication
    public function testGetRoleAssignmentsForUserDirect()
2160
    {
2161
        $repository = $this->getRepository();
2162
        $roleService = $repository->getRoleService();
2163
2164
        /* BEGIN: Use Case */
2165
        $user = $this->createUserVersion1();
2166
2167
        // Instantiate a role create and add some policies
2168
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2169
2170
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2171
        // $roleCreate->mainLanguageCode = 'eng-US';
2172
2173
        $roleCreate->addPolicy(
2174
            $roleService->newPolicyCreateStruct('user', 'login')
2175
        );
2176
        $roleCreate->addPolicy(
2177
            $roleService->newPolicyCreateStruct('content', 'read')
2178
        );
2179
        $roleCreate->addPolicy(
2180
            $roleService->newPolicyCreateStruct('content', 'edit')
2181
        );
2182
2183
        // Create the new role instance
2184
        $roleDraft = $roleService->createRole($roleCreate);
2185
        $roleService->publishRoleDraft($roleDraft);
2186
        $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...
2187
2188
        // Assign role to new user
2189
        $roleService->assignRoleToUser($role, $user);
2190
2191
        // Load the currently assigned role
2192
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user);
2193
        /* END: Use Case */
2194
2195
        $this->assertEquals(1, count($roleAssignments));
2196
        $this->assertInstanceOf(
2197
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2198
            reset($roleAssignments)
2199
        );
2200
    }
2201
2202
    /**
2203
     * Test for the getRoleAssignmentsForUser() method.
2204
     *
2205
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2206
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2207
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2208
     */
2209
    public function testGetRoleAssignmentsForUserEmpty()
2210
    {
2211
        $repository = $this->getRepository();
2212
        $roleService = $repository->getRoleService();
2213
2214
        /* BEGIN: Use Case */
2215
        $adminUser = $repository->getCurrentUser();
2216
2217
        // Load the currently assigned role
2218
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser);
2219
        /* END: Use Case */
2220
2221
        $this->assertEquals(0, count($roleAssignments));
2222
    }
2223
2224
    /**
2225
     * Test for the getRoleAssignmentsForUser() method.
2226
     *
2227
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2228
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2229
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2230
     */
2231
    public function testGetRoleAssignmentsForUserInherited()
2232
    {
2233
        $repository = $this->getRepository();
2234
        $roleService = $repository->getRoleService();
2235
2236
        /* BEGIN: Use Case */
2237
        $adminUser = $repository->getCurrentUser();
2238
2239
        // Load the currently assigned role + inherited role assignments
2240
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser, true);
2241
        /* END: Use Case */
2242
2243
        $this->assertEquals(1, count($roleAssignments));
2244
        $this->assertInstanceOf(
2245
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2246
            reset($roleAssignments)
2247
        );
2248
    }
2249
2250
    /**
2251
     * Test for the assignRoleToUserGroup() method.
2252
     *
2253
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
2254
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
2255
     */
2256 View Code Duplication
    public function testAssignRoleToUserGroup()
2257
    {
2258
        $repository = $this->getRepository();
2259
        $roleService = $repository->getRoleService();
2260
2261
        /* BEGIN: Use Case */
2262
        $userGroup = $this->createUserGroupVersion1();
2263
2264
        // Load the existing "Administrator" role
2265
        $role = $roleService->loadRoleByIdentifier('Administrator');
2266
2267
        // Assign the "Administrator" role to the newly created user group
2268
        $roleService->assignRoleToUserGroup($role, $userGroup);
2269
2270
        // The assignments array will contain the new role<->group assignment
2271
        $roleAssignments = $roleService->getRoleAssignments($role);
2272
        /* END: Use Case */
2273
2274
        // Administrator + Example Group
2275
        $this->assertEquals(2, count($roleAssignments));
2276
    }
2277
2278
    /**
2279
     * Test for the assignRoleToUserGroup() method.
2280
     *
2281
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2282
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2283
     */
2284 View Code Duplication
    public function testAssignRoleToUserGroupWithRoleLimitation()
2285
    {
2286
        $repository = $this->getRepository();
2287
        $roleService = $repository->getRoleService();
2288
2289
        /* BEGIN: Use Case */
2290
        $userGroup = $this->createUserGroupVersion1();
2291
2292
        // Load the existing "Anonymous" role
2293
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2294
2295
        // Assign the "Anonymous" role to the newly created user group
2296
        $roleService->assignRoleToUserGroup(
2297
            $role,
2298
            $userGroup,
2299
            new SubtreeLimitation(
2300
                array(
2301
                    'limitationValues' => array('/1/43/'),
2302
                )
2303
            )
2304
        );
2305
2306
        // The assignments array will contain the new role<->group assignment
2307
        $roleAssignments = $roleService->getRoleAssignments($role);
2308
        /* END: Use Case */
2309
2310
        // Members + Partners + Anonymous + Example Group
2311
        $this->assertEquals(4, count($roleAssignments));
2312
2313
        // Get the role limitation
2314
        $roleLimitation = null;
2315
        foreach ($roleAssignments as $roleAssignment) {
2316
            $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...
2317
            if ($roleLimitation) {
2318
                break;
2319
            }
2320
        }
2321
2322
        $this->assertEquals(
2323
            new SubtreeLimitation(
2324
                array(
2325
                    'limitationValues' => array('/1/43/'),
2326
                )
2327
            ),
2328
            $roleLimitation
2329
        );
2330
2331
        // Test again to see values being merged
2332
        $roleService->assignRoleToUserGroup(
2333
            $role,
2334
            $userGroup,
2335
            new SubtreeLimitation(
2336
                array(
2337
                    'limitationValues' => array('/1/43/', '/1/2/'),
2338
                )
2339
            )
2340
        );
2341
2342
        // The assignments array will contain the new role<->user assignment
2343
        $roleAssignments = $roleService->getRoleAssignments($role);
2344
2345
        // Members + Partners + Anonymous + Example User
2346
        $this->assertEquals(5, count($roleAssignments));
2347
2348
        // Get the role limitation
2349
        $roleLimitations = [];
2350
        foreach ($roleAssignments as $roleAssignment) {
2351
            $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...
2352
            if ($roleLimitation) {
2353
                $this->assertInstanceOf(
2354
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2355
                    $roleAssignment
2356
                );
2357
                $roleLimitations[] = $roleLimitation;
2358
            }
2359
        }
2360
        array_multisort($roleLimitations);
2361
2362
        $this->assertEquals(
2363
            [
2364
                new SubtreeLimitation(
2365
                    array(
2366
                        'limitationValues' => array('/1/2/'),
2367
                    )
2368
                ),
2369
                new SubtreeLimitation(
2370
                    array(
2371
                        'limitationValues' => array('/1/43/'),
2372
                    )
2373
                ),
2374
            ],
2375
            $roleLimitations
2376
        );
2377
    }
2378
2379
    /**
2380
     * Test for the assignRoleToUserGroup() method.
2381
     *
2382
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2383
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
2384
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2385
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2386
     */
2387
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsLimitationValidationException()
2388
    {
2389
        $repository = $this->getRepository();
2390
2391
        $mainGroupId = $this->generateId('group', 4);
2392
        /* BEGIN: Use Case */
2393
        // $mainGroupId is the ID of the main "Users" group
2394
2395
        $userService = $repository->getUserService();
2396
        $roleService = $repository->getRoleService();
2397
2398
        $userGroup = $userService->loadUserGroup($mainGroupId);
2399
2400
        // Load the existing "Anonymous" role
2401
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2402
2403
        // Assign the "Anonymous" role to the newly created user group
2404
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
2405
        // does not exists
2406
        $roleService->assignRoleToUserGroup(
2407
            $role,
2408
            $userGroup,
2409
            new SubtreeLimitation(
2410
                array(
2411
                    'limitationValues' => array('/lorem/ipsum/42/'),
2412
                )
2413
            )
2414
        );
2415
        /* END: Use Case */
2416
    }
2417
2418
    /**
2419
     * Test for the assignRoleToUserGroup() method.
2420
     *
2421
     * Makes sure assigning role several times throws.
2422
     *
2423
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2424
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2425
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2426
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2427
     */
2428
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2429
    {
2430
        $repository = $this->getRepository();
2431
2432
        $mainGroupId = $this->generateId('group', 4);
2433
        /* BEGIN: Use Case */
2434
        // $mainGroupId is the ID of the main "Users" group
2435
2436
        $userService = $repository->getUserService();
2437
        $roleService = $repository->getRoleService();
2438
2439
        $userGroup = $userService->loadUserGroup($mainGroupId);
2440
2441
        // Load the existing "Anonymous" role
2442
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2443
2444
        // Assign the "Anonymous" role to the newly created user group
2445
        try {
2446
            $roleService->assignRoleToUserGroup(
2447
                $role,
2448
                $userGroup
2449
            );
2450
        } catch (Exception $e) {
2451
            $this->fail('Got exception at first valid attempt to assign role');
2452
        }
2453
2454
        // Re-Assign the "Anonymous" role to the newly created user group
2455
        // This call will fail with an InvalidArgumentException, because role is already assigned
2456
        $roleService->assignRoleToUserGroup(
2457
            $role,
2458
            $userGroup
2459
        );
2460
        /* END: Use Case */
2461
    }
2462
2463
    /**
2464
     * Test for the assignRoleToUserGroup() method.
2465
     *
2466
     * Makes sure assigning role several times with same limitations throws.
2467
     *
2468
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2469
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2470
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2471
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2472
     */
2473
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsInvalidArgumentException()
2474
    {
2475
        $repository = $this->getRepository();
2476
2477
        $mainGroupId = $this->generateId('group', 4);
2478
        /* BEGIN: Use Case */
2479
        // $mainGroupId is the ID of the main "Users" group
2480
2481
        $userService = $repository->getUserService();
2482
        $roleService = $repository->getRoleService();
2483
2484
        $userGroup = $userService->loadUserGroup($mainGroupId);
2485
2486
        // Load the existing "Anonymous" role
2487
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2488
2489
        // Assign the "Anonymous" role to the newly created user group
2490
        try {
2491
            $roleService->assignRoleToUserGroup(
2492
                $role,
2493
                $userGroup,
2494
                new SubtreeLimitation(
2495
                    array(
2496
                        'limitationValues' => array('/1/43/', '/1/2/'),
2497
                    )
2498
                )
2499
            );
2500
        } catch (Exception $e) {
2501
            $this->fail('Got exception at first valid attempt to assign role');
2502
        }
2503
2504
        // Re-Assign the "Anonymous" role to the newly created user group
2505
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2506
        $roleService->assignRoleToUserGroup(
2507
            $role,
2508
            $userGroup,
2509
            new SubtreeLimitation(
2510
                array(
2511
                    'limitationValues' => array('/1/43/'),
2512
                )
2513
            )
2514
        );
2515
        /* END: Use Case */
2516
    }
2517
2518
    /**
2519
     * Test for the unassignRoleFromUserGroup() method.
2520
     *
2521
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUserGroup()
2522
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2523
     */
2524 View Code Duplication
    public function testUnassignRoleFromUserGroup()
2525
    {
2526
        $repository = $this->getRepository();
2527
        $roleService = $repository->getRoleService();
2528
2529
        /* BEGIN: Use Case */
2530
        $userGroup = $this->createUserGroupVersion1();
2531
2532
        // Load the existing "Member" role
2533
        $role = $roleService->loadRoleByIdentifier('Member');
2534
2535
        // Assign the "Member" role to the newly created user group
2536
        $roleService->assignRoleToUserGroup($role, $userGroup);
2537
2538
        // Unassign group from role
2539
        $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...
2540
2541
        // The assignments array will not contain the new role<->group assignment
2542
        $roleAssignments = $roleService->getRoleAssignments($role);
2543
        /* END: Use Case */
2544
2545
        // Members + Editors + Partners
2546
        $this->assertEquals(3, count($roleAssignments));
2547
    }
2548
2549
    /**
2550
     * Test for the unassignRoleFromUserGroup() method.
2551
     *
2552
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUserGroup()
2553
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2554
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUserGroup
2555
     */
2556 View Code Duplication
    public function testUnassignRoleFromUserGroupThrowsInvalidArgumentException()
2557
    {
2558
        $repository = $this->getRepository();
2559
        $roleService = $repository->getRoleService();
2560
2561
        /* BEGIN: Use Case */
2562
        $userGroup = $this->createUserGroupVersion1();
2563
2564
        // Load the existing "Member" role
2565
        $role = $roleService->loadRoleByIdentifier('Member');
2566
2567
        // This call will fail with a "InvalidArgumentException", because the
2568
        // user group does not have the "Member" role.
2569
        $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...
2570
        /* END: Use Case */
2571
    }
2572
2573
    /**
2574
     * Test unassigning role by assignment.
2575
     *
2576
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2577
     */
2578
    public function testUnassignRoleByAssignment()
2579
    {
2580
        $repository = $this->getRepository();
2581
        $roleService = $repository->getRoleService();
2582
2583
        $role = $roleService->loadRole(2);
2584
        $user = $repository->getUserService()->loadUser(14);
2585
2586
        $originalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2587
2588
        $roleService->assignRoleToUser($role, $user);
2589
        $newAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2590
        self::assertEquals($originalAssignmentCount + 1, $newAssignmentCount);
2591
2592
        $assignments = $roleService->getRoleAssignmentsForUser($user);
2593
        $roleService->removeRoleAssignment($assignments[0]);
2594
        $finalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2595
        self::assertEquals($newAssignmentCount - 1, $finalAssignmentCount);
2596
    }
2597
2598
    /**
2599
     * Test unassigning role by assignment.
2600
     *
2601
     * But on current admin user so he lacks access to read roles.
2602
     *
2603
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2604
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2605
     */
2606 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsUnauthorizedException()
2607
    {
2608
        $repository = $this->getRepository();
2609
        $roleService = $repository->getRoleService();
2610
2611
        try {
2612
            $adminUserGroup = $repository->getUserService()->loadUserGroup(12);
2613
            $assignments = $roleService->getRoleAssignmentsForUserGroup($adminUserGroup);
2614
            $roleService->removeRoleAssignment($assignments[0]);
2615
        } catch (Exception $e) {
2616
            self::fail(
2617
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2618
            );
2619
        }
2620
2621
        $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...
2622
    }
2623
2624
    /**
2625
     * Test unassigning role by non-existing assignment.
2626
     *
2627
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2628
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2629
     */
2630 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsNotFoundException()
2631
    {
2632
        $repository = $this->getRepository();
2633
        $roleService = $repository->getRoleService();
2634
2635
        try {
2636
            $editorsUserGroup = $repository->getUserService()->loadUserGroup(13);
2637
            $assignments = $roleService->getRoleAssignmentsForUserGroup($editorsUserGroup);
2638
            $roleService->removeRoleAssignment($assignments[0]);
2639
        } catch (Exception $e) {
2640
            self::fail(
2641
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2642
            );
2643
        }
2644
2645
        $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...
2646
    }
2647
2648
    /**
2649
     * Test for the getRoleAssignmentsForUserGroup() method.
2650
     *
2651
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup()
2652
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2653
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2654
     */
2655 View Code Duplication
    public function testGetRoleAssignmentsForUserGroup()
2656
    {
2657
        $repository = $this->getRepository();
2658
        $roleService = $repository->getRoleService();
2659
2660
        /* BEGIN: Use Case */
2661
        $userGroup = $this->createUserGroupVersion1();
2662
2663
        // Instantiate a role create and add some policies
2664
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2665
2666
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2667
        // $roleCreate->mainLanguageCode = 'eng-US';
2668
2669
        $roleCreate->addPolicy(
2670
            $roleService->newPolicyCreateStruct('user', 'login')
2671
        );
2672
        $roleCreate->addPolicy(
2673
            $roleService->newPolicyCreateStruct('content', 'read')
2674
        );
2675
        $roleCreate->addPolicy(
2676
            $roleService->newPolicyCreateStruct('content', 'edit')
2677
        );
2678
2679
        // Create the new role instance
2680
        $roleDraft = $roleService->createRole($roleCreate);
2681
        $roleService->publishRoleDraft($roleDraft);
2682
        $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...
2683
2684
        // Assign role to new user group
2685
        $roleService->assignRoleToUserGroup($role, $userGroup);
2686
2687
        // Load the currently assigned role
2688
        $roleAssignments = $roleService->getRoleAssignmentsForUserGroup($userGroup);
2689
        /* END: Use Case */
2690
2691
        $this->assertEquals(1, count($roleAssignments));
2692
        $this->assertInstanceOf(
2693
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2694
            reset($roleAssignments)
2695
        );
2696
    }
2697
2698
    /**
2699
     * Test for the loadPoliciesByUserId() method.
2700
     *
2701
     * @see \eZ\Publish\API\Repository\RoleService::loadPoliciesByUserId()
2702
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2703
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2704
     */
2705
    public function testLoadPoliciesByUserId()
2706
    {
2707
        $repository = $this->getRepository();
2708
2709
        $anonUserId = $this->generateId('user', 10);
2710
        /* BEGIN: Use Case */
2711
        // $anonUserId is the ID of the "Anonymous" user.
2712
2713
        $userService = $repository->getUserService();
2714
        $roleService = $repository->getRoleService();
2715
2716
        // Load "Anonymous" user
2717
        $user = $userService->loadUser($anonUserId);
2718
2719
        // Instantiate a role create and add some policies
2720
        $roleCreate = $roleService->newRoleCreateStruct('User Role');
2721
2722
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2723
        // $roleCreate->mainLanguageCode = 'eng-US';
2724
2725
        $roleCreate->addPolicy(
2726
            $roleService->newPolicyCreateStruct('notification', 'use')
2727
        );
2728
        $roleCreate->addPolicy(
2729
            $roleService->newPolicyCreateStruct('user', 'password')
2730
        );
2731
        $roleCreate->addPolicy(
2732
            $roleService->newPolicyCreateStruct('user', 'selfedit')
2733
        );
2734
2735
        // Create the new role instance
2736
        $roleDraft = $roleService->createRole($roleCreate);
2737
        $roleService->publishRoleDraft($roleDraft);
2738
        $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...
2739
2740
        // Assign role to anon user
2741
        $roleService->assignRoleToUser($role, $user);
2742
2743
        // Load the currently assigned role
2744
        $policies = array();
2745
        foreach ($roleService->loadPoliciesByUserId($user->id) as $policy) {
2746
            $policies[] = array($policy->roleId, $policy->module, $policy->function);
2747
        }
2748
        /* END: Use Case */
2749
        array_multisort($policies);
2750
2751
        $this->assertEquals(
2752
            array(
2753
                array(1, 'content', 'pdf'),
2754
                array(1, 'content', 'read'),
2755
                array(1, 'content', 'read'),
2756
                array(1, 'rss', 'feed'),
2757
                array(1, 'user', 'login'),
2758
                array(1, 'user', 'login'),
2759
                array(1, 'user', 'login'),
2760
                array(1, 'user', 'login'),
2761
                array($role->id, 'notification', 'use'),
2762
                array($role->id, 'user', 'password'),
2763
                array($role->id, 'user', 'selfedit'),
2764
            ),
2765
            $policies
2766
        );
2767
    }
2768
2769
    /**
2770
     * Test for the loadPoliciesByUserId() method.
2771
     *
2772
     * @see \eZ\Publish\API\Repository\RoleService::loadPoliciesByUserId()
2773
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2774
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadPoliciesByUserId
2775
     */
2776
    public function testLoadPoliciesByUserIdThrowsNotFoundException()
2777
    {
2778
        $repository = $this->getRepository();
2779
2780
        $nonExistingUserId = $this->generateId('user', self::DB_INT_MAX);
2781
        /* BEGIN: Use Case */
2782
        $roleService = $repository->getRoleService();
2783
2784
        // This call will fail with a "NotFoundException", because hopefully no
2785
        // user with an ID equal to self::DB_INT_MAX exists.
2786
        $roleService->loadPoliciesByUserId($nonExistingUserId);
2787
        /* END: Use Case */
2788
    }
2789
2790
    /**
2791
     * Test for the publishRoleDraft() method.
2792
     *
2793
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2794
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2795
     */
2796 View Code Duplication
    public function testPublishRoleDraft()
2797
    {
2798
        $repository = $this->getRepository();
2799
2800
        /* BEGIN: Use Case */
2801
        $roleService = $repository->getRoleService();
2802
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2803
2804
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2805
        // $roleCreate->mainLanguageCode = 'eng-US';
2806
2807
        $roleDraft = $roleService->createRole($roleCreate);
2808
2809
        $roleDraft = $roleService->addPolicyByRoleDraft(
2810
            $roleDraft,
2811
            $roleService->newPolicyCreateStruct('content', 'delete')
2812
        );
2813
        $roleDraft = $roleService->addPolicyByRoleDraft(
2814
            $roleDraft,
2815
            $roleService->newPolicyCreateStruct('content', 'create')
2816
        );
2817
2818
        $roleService->publishRoleDraft($roleDraft);
2819
        /* END: Use Case */
2820
2821
        $this->assertInstanceOf(
2822
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Role',
2823
            $roleService->loadRoleByIdentifier($roleCreate->identifier)
2824
        );
2825
    }
2826
2827
    /**
2828
     * Test for the publishRoleDraft() method.
2829
     *
2830
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2831
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2832
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
2833
     */
2834 View Code Duplication
    public function testPublishRoleDraftAddPolicies()
2835
    {
2836
        $repository = $this->getRepository();
2837
2838
        /* BEGIN: Use Case */
2839
        $roleService = $repository->getRoleService();
2840
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2841
2842
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2843
        // $roleCreate->mainLanguageCode = 'eng-US';
2844
2845
        $roleDraft = $roleService->createRole($roleCreate);
2846
2847
        $roleDraft = $roleService->addPolicyByRoleDraft(
2848
            $roleDraft,
2849
            $roleService->newPolicyCreateStruct('content', 'delete')
2850
        );
2851
        $roleDraft = $roleService->addPolicyByRoleDraft(
2852
            $roleDraft,
2853
            $roleService->newPolicyCreateStruct('content', 'create')
2854
        );
2855
2856
        $roleService->publishRoleDraft($roleDraft);
2857
        $role = $roleService->loadRoleByIdentifier($roleCreate->identifier);
2858
        /* END: Use Case */
2859
2860
        $actual = array();
2861
        foreach ($role->getPolicies() as $policy) {
2862
            $actual[] = array(
2863
                'module' => $policy->module,
2864
                'function' => $policy->function,
2865
            );
2866
        }
2867
        usort(
2868
            $actual,
2869
            function ($p1, $p2) {
2870
                return strcasecmp($p1['function'], $p2['function']);
2871
            }
2872
        );
2873
2874
        $this->assertEquals(
2875
            array(
2876
                array(
2877
                    'module' => 'content',
2878
                    'function' => 'create',
2879
                ),
2880
                array(
2881
                    'module' => 'content',
2882
                    'function' => 'delete',
2883
                ),
2884
            ),
2885
            $actual
2886
        );
2887
    }
2888
2889
    /**
2890
     * Create a user group fixture in a variable named <b>$userGroup</b>,.
2891
     *
2892
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
2893
     */
2894
    private function createUserGroupVersion1()
2895
    {
2896
        $repository = $this->getRepository();
2897
2898
        $mainGroupId = $this->generateId('group', 4);
2899
        /* BEGIN: Inline */
2900
        // $mainGroupId is the ID of the main "Users" group
2901
2902
        $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...
2903
        $userService = $repository->getUserService();
2904
2905
        // Load main group
2906
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
2907
2908
        // Instantiate a new create struct
2909
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
2910
        $userGroupCreate->setField('name', 'Example Group');
2911
2912
        // Create the new user group
2913
        $userGroup = $userService->createUserGroup(
2914
            $userGroupCreate,
2915
            $parentUserGroup
2916
        );
2917
        /* END: Inline */
2918
2919
        return $userGroup;
2920
    }
2921
}
2922