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

UserBase::testReadOnlyProperty()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 9
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Integration\UserBase class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Service\Integration;
10
11
use eZ\Publish\Core\Repository\Tests\Service\Integration\Base as BaseServiceTest;
12
use eZ\Publish\Core\Repository\Values\User\User;
13
use eZ\Publish\Core\Repository\Values\User\UserGroup;
14
use eZ\Publish\Core\Repository\Values\Content\Content;
15
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
16
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
17
use eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException as PropertyNotFound;
18
use eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException;
19
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
20
use eZ\Publish\API\Repository\Tests\BaseTest as APIBaseTest;
21
22
/**
23
 * Test case for User Service.
24
 */
25
abstract class UserBase extends BaseServiceTest
26
{
27
    /**
28
     * Test a new class and default values on properties.
29
     *
30
     * @covers \eZ\Publish\API\Repository\Values\User\User::__construct
31
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__construct
32
     */
33
    public function testNewClass()
34
    {
35
        $user = new User();
36
37
        $this->assertPropertiesCorrect(
38
            [
39
                'login' => null,
40
                'email' => null,
41
                'passwordHash' => null,
42
                'hashAlgorithm' => null,
43
                'maxLogin' => null,
44
                'enabled' => null,
45
            ],
46
            $user
47
        );
48
49
        $group = new UserGroup();
50
        self::assertEquals(null, $group->parentId);
51
        self::assertEquals(null, $group->subGroupCount);
52
53
        $this->assertPropertiesCorrect(
54
            [
55
                'parentId' => null,
56
                'subGroupCount' => null,
57
            ],
58
            $group
59
        );
60
    }
61
62
    /**
63
     * Test retrieving missing property.
64
     *
65
     * @covers \eZ\Publish\API\Repository\Values\User\User::__get
66
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__get
67
     */
68
    public function testMissingProperty()
69
    {
70
        try {
71
            $user = new User();
72
            $value = $user->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\Core\R...itory\Values\User\User>. Since you implemented __set, maybe consider adding a @property annotation.

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...
Unused Code introduced by
$value 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...
73
            self::fail('Succeeded getting non existing property');
74
        } catch (PropertyNotFound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
75
        }
76
77
        try {
78
            $userGroup = new UserGroup();
79
            $value = $userGroup->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\Core\R...\Values\User\UserGroup>. Since you implemented __set, maybe consider adding a @property annotation.

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...
Unused Code introduced by
$value 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...
80
            self::fail('Succeeded getting non existing property');
81
        } catch (PropertyNotFound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
82
        }
83
    }
84
85
    /**
86
     * Test setting read only property.
87
     *
88
     * @covers \eZ\Publish\API\Repository\Values\User\User::__set
89
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__set
90
     */
91
    public function testReadOnlyProperty()
92
    {
93
        try {
94
            $user = new User();
95
            $user->login = 'user';
96
            self::fail('Succeeded setting read only property');
97
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
98
        }
99
100
        try {
101
            $userGroup = new UserGroup();
102
            $userGroup->parentId = 42;
103
            self::fail('Succeeded setting read only property');
104
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
105
        }
106
    }
107
108
    /**
109
     * Test if property exists.
110
     *
111
     * @covers \eZ\Publish\API\Repository\Values\User\User::__isset
112
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__isset
113
     */
114 View Code Duplication
    public function testIsPropertySet()
115
    {
116
        $user = new User();
117
        $value = isset($user->notDefined);
118
        self::assertEquals(false, $value);
119
120
        $value = isset($user->login);
121
        self::assertEquals(true, $value);
122
123
        $userGroup = new UserGroup();
124
        $value = isset($userGroup->notDefined);
125
        self::assertEquals(false, $value);
126
127
        $value = isset($userGroup->parentId);
128
        self::assertEquals(true, $value);
129
    }
130
131
    /**
132
     * Test unsetting a property.
133
     *
134
     * @covers \eZ\Publish\API\Repository\Values\User\User::__unset
135
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__unset
136
     */
137
    public function testUnsetProperty()
138
    {
139
        $user = new User(['login' => 'admin']);
140
        try {
141
            unset($user->login);
142
            self::fail('Unsetting read-only property succeeded');
143
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
144
        }
145
146
        $userGroup = new UserGroup(['parentId' => 1]);
147
        try {
148
            unset($userGroup->parentId);
149
            self::fail('Unsetting read-only property succeeded');
150
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
151
        }
152
    }
153
154
    /**
155
     * Test creating new user group.
156
     *
157
     * @covers \eZ\Publish\API\Repository\UserService::createUserGroup
158
     */
159 View Code Duplication
    public function testCreateUserGroup()
160
    {
161
        $userService = $this->repository->getUserService();
162
163
        $parentGroup = $userService->loadUserGroup(4);
164
        $userGroupCreateStruct = $userService->newUserGroupCreateStruct('eng-GB');
165
        $userGroupCreateStruct->ownerId = 14;
166
        $userGroupCreateStruct->sectionId = 1;
167
        $userGroupCreateStruct->setField('name', 'New group');
168
        $userGroupCreateStruct->setField('description', 'This is a new group');
169
170
        $newGroup = $userService->createUserGroup($userGroupCreateStruct, $parentGroup);
171
172
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup', $newGroup);
173
    }
174
175
    /**
176
     * Test creating new user group throwing ContentFieldValidationException.
177
     *
178
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
179
     * @covers \eZ\Publish\API\Repository\UserService::createUserGroup
180
     */
181
    public function testCreateUserGroupRequiredFieldEmpty()
182
    {
183
        $userService = $this->repository->getUserService();
184
185
        $parentGroup = $userService->loadUserGroup(4);
186
        $userGroupCreateStruct = $userService->newUserGroupCreateStruct('eng-GB');
187
        $userGroupCreateStruct->ownerId = 14;
188
        $userGroupCreateStruct->sectionId = 1;
189
        $userGroupCreateStruct->setField('name', '');
190
191
        $userService->createUserGroup($userGroupCreateStruct, $parentGroup);
192
    }
193
194
    /**
195
     * Test creating new user group throwing ContentFieldValidationException.
196
     *
197
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
198
     * @covers \eZ\Publish\API\Repository\UserService::createUserGroup
199
     */
200
    public function testCreateUserGroupRequiredFieldMissing()
201
    {
202
        $userService = $this->repository->getUserService();
203
204
        $parentGroup = $userService->loadUserGroup(4);
205
        $userGroupCreateStruct = $userService->newUserGroupCreateStruct('eng-GB');
206
207
        $userService->createUserGroup($userGroupCreateStruct, $parentGroup);
208
    }
209
210
    /**
211
     * Test loading a group.
212
     *
213
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroup
214
     */
215
    public function testLoadUserGroup()
216
    {
217
        $userService = $this->repository->getUserService();
218
        $userGroup = $userService->loadUserGroup(4);
219
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup', $userGroup);
220
    }
221
222
    /**
223
     * Test loading a group throwing NotFoundException.
224
     *
225
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
226
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroup
227
     */
228
    public function testLoadUserGroupThrowsNotFoundException()
229
    {
230
        $userService = $this->repository->getUserService();
231
        $userService->loadUserGroup(APIBaseTest::DB_INT_MAX);
232
    }
233
234
    /**
235
     * Test loading sub groups.
236
     *
237
     * @covers \eZ\Publish\API\Repository\UserService::loadSubUserGroups
238
     */
239
    public function testLoadSubUserGroups()
240
    {
241
        $userService = $this->repository->getUserService();
242
243
        $parentGroup = $userService->loadUserGroup(4);
244
        $userGroupCreateStruct = $userService->newUserGroupCreateStruct('eng-GB');
245
        $userGroupCreateStruct->ownerId = 14;
246
        $userGroupCreateStruct->sectionId = 1;
247
        $userGroupCreateStruct->setField('name', 'New group');
248
        $userGroupCreateStruct->setField('description', 'This is a new group');
249
250
        $userService->createUserGroup($userGroupCreateStruct, $parentGroup);
251
252
        $subGroups = $userService->loadSubUserGroups($parentGroup);
253
254
        self::assertInternalType('array', $subGroups);
255
        self::assertNotEmpty($subGroups);
256
    }
257
258
    /**
259
     * Test loading sub groups throwing NotFoundException.
260
     *
261
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
262
     * @covers \eZ\Publish\API\Repository\UserService::loadSubUserGroups
263
     */
264 View Code Duplication
    public function testLoadSubUserGroupsThrowsNotFoundException()
265
    {
266
        $userService = $this->repository->getUserService();
267
268
        $parentGroup = new UserGroup(
269
            [
270
                'content' => new Content(
271
                    [
272
                        'versionInfo' => new VersionInfo(
273
                            ['contentInfo' => new ContentInfo(['id' => APIBaseTest::DB_INT_MAX])]
274
                        ),
275
                        'internalFields' => [],
276
                    ]
277
                ),
278
            ]
279
        );
280
        $userService->loadSubUserGroups($parentGroup);
281
    }
282
283
    /**
284
     * Test deleting user group.
285
     *
286
     * @covers \eZ\Publish\API\Repository\UserService::deleteUserGroup
287
     */
288
    public function testDeleteUserGroup()
289
    {
290
        $userService = $this->repository->getUserService();
291
292
        $userGroup = $userService->loadUserGroup(12);
293
        $userService->deleteUserGroup($userGroup);
294
295
        try {
296
            $userService->loadUserGroup($userGroup->id);
297
            self::fail('Succeeded loading deleted user group');
298
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
299
        }
300
    }
301
302
    /**
303
     * Test deleting user group throwing NotFoundException.
304
     *
305
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
306
     * @covers \eZ\Publish\API\Repository\UserService::deleteUserGroup
307
     */
308 View Code Duplication
    public function testDeleteUserGroupThrowsNotFoundException()
309
    {
310
        $userService = $this->repository->getUserService();
311
312
        $userGroup = new UserGroup(
313
            [
314
                'content' => new Content(
315
                    [
316
                        'versionInfo' => new VersionInfo(
317
                            ['contentInfo' => new ContentInfo(['id' => APIBaseTest::DB_INT_MAX])]
318
                        ),
319
                        'internalFields' => [],
320
                    ]
321
                ),
322
            ]
323
        );
324
        $userService->deleteUserGroup($userGroup);
325
    }
326
327
    /**
328
     * Test moving a user group below another group.
329
     *
330
     * @covers \eZ\Publish\API\Repository\UserService::moveUserGroup
331
     */
332
    public function testMoveUserGroup()
333
    {
334
        $userService = $this->repository->getUserService();
335
        $locationService = $this->repository->getLocationService();
336
337
        $userGroupToMove = $userService->loadUserGroup(42);
338
        $parentUserGroup = $userService->loadUserGroup(12);
339
        $userService->moveUserGroup($userGroupToMove, $parentUserGroup);
340
341
        $movedUserGroup = $userService->loadUserGroup($userGroupToMove->id);
342
343
        $newMainLocation = $locationService->loadLocation($movedUserGroup->getVersionInfo()->getContentInfo()->mainLocationId);
344
        self::assertEquals(
345
            $parentUserGroup->getVersionInfo()->getContentInfo()->mainLocationId,
346
            $newMainLocation->parentLocationId
347
        );
348
    }
349
350
    /**
351
     * Test moving a user group below another group throwing NotFoundException.
352
     *
353
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
354
     * @covers \eZ\Publish\API\Repository\UserService::moveUserGroup
355
     */
356
    public function testMoveUserGroupThrowsNotFoundException()
357
    {
358
        $userService = $this->repository->getUserService();
359
360
        $userGroupToMove = new UserGroup(
361
            [
362
                'content' => new Content(
363
                    [
364
                        'versionInfo' => new VersionInfo(
365
                            ['contentInfo' => new ContentInfo(['id' => APIBaseTest::DB_INT_MAX])]
366
                        ),
367
                        'internalFields' => [],
368
                    ]
369
                ),
370
            ]
371
        );
372
        $parentUserGroup = new UserGroup(
373
            [
374
                'content' => new Content(
375
                    [
376
                        'versionInfo' => new VersionInfo(
377
                            ['contentInfo' => new ContentInfo(['id' => APIBaseTest::DB_INT_MAX])]
378
                        ),
379
                        'internalFields' => [],
380
                    ]
381
                ),
382
            ]
383
        );
384
        $userService->moveUserGroup($userGroupToMove, $parentUserGroup);
385
    }
386
387
    /**
388
     * Test updating a user group.
389
     *
390
     * @covers \eZ\Publish\API\Repository\UserService::updateUserGroup
391
     */
392
    public function testUpdateUserGroup()
393
    {
394
        $userService = $this->repository->getUserService();
395
        $contentService = $this->repository->getContentService();
396
397
        $userGroupUpdateStruct = $userService->newUserGroupUpdateStruct();
398
        $userGroupUpdateStruct->contentUpdateStruct = $contentService->newContentUpdateStruct();
399
        $userGroupUpdateStruct->contentUpdateStruct->setField('name', 'New anonymous group', 'eng-US');
400
401
        $userGroup = $userService->loadUserGroup(42);
402
403
        $updatedUserGroup = $userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
404
        self::assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup', $updatedUserGroup);
405
        self::assertEquals(
406
            $userGroupUpdateStruct->contentUpdateStruct->fields[0]->value,
407
            $updatedUserGroup->getFieldValue('name')
408
        );
409
    }
410
411
    /**
412
     * Test updating a user group throwing ContentFieldValidationException.
413
     *
414
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
415
     * @covers \eZ\Publish\API\Repository\UserService::updateUserGroup
416
     */
417 View Code Duplication
    public function testUpdateUserGroupRequiredFieldEmpty()
418
    {
419
        $userService = $this->repository->getUserService();
420
        $contentService = $this->repository->getContentService();
421
422
        $userGroup = $userService->loadUserGroup(42);
423
        $userGroupUpdateStruct = $userService->newUserGroupUpdateStruct();
424
        $userGroupUpdateStruct->contentUpdateStruct = $contentService->newContentUpdateStruct();
425
        $userGroupUpdateStruct->contentUpdateStruct->setField('name', '', 'eng-US');
426
427
        $userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
428
    }
429
430
    /**
431
     * Test creating a user.
432
     *
433
     * @covers \eZ\Publish\API\Repository\UserService::createUser
434
     */
435
    public function testCreateUser()
436
    {
437
        $userService = $this->repository->getUserService();
438
439
        $userCreateStruct = $userService->newUserCreateStruct('new_user', '[email protected]', 'password', 'eng-GB');
440
        $userCreateStruct->setField('first_name', 'New', 'eng-GB');
441
        $userCreateStruct->setField('last_name', 'User', 'eng-GB');
442
443
        $parentGroup = $userService->loadUserGroup(42);
444
        $createdUser = $userService->createUser($userCreateStruct, [$parentGroup]);
445
446
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\User', $createdUser);
447
        self::assertEquals('New', $createdUser->getFieldValue('first_name'));
448
        self::assertEquals('User', $createdUser->getFieldValue('last_name'));
449
        self::assertEquals($userCreateStruct->login, $createdUser->login);
450
        self::assertEquals($userCreateStruct->email, $createdUser->email);
451
    }
452
453
    /**
454
     * Test creating a user throwing NotFoundException.
455
     *
456
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
457
     * @covers \eZ\Publish\API\Repository\UserService::createUser
458
     */
459
    public function testCreateUserThrowsNotFoundException()
460
    {
461
        $userService = $this->repository->getUserService();
462
463
        $userCreateStruct = $userService->newUserCreateStruct('new_user', '[email protected]', 'password', 'eng-GB');
464
        $userCreateStruct->setField('first_name', 'New');
465
        $userCreateStruct->setField('last_name', 'User');
466
467
        $parentGroup = new UserGroup(
468
            [
469
                'content' => new Content(
470
                    [
471
                        'versionInfo' => new VersionInfo(
472
                            [
473
                                'contentInfo' => new ContentInfo(['id' => APIBaseTest::DB_INT_MAX]),
474
                            ]
475
                        ),
476
                        'internalFields' => [],
477
                    ]
478
                ),
479
            ]
480
        );
481
        $userService->createUser($userCreateStruct, [$parentGroup]);
482
    }
483
484
    /**
485
     * Test creating a user throwing ContentFieldValidationException.
486
     *
487
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
488
     * @covers \eZ\Publish\API\Repository\UserService::createUser
489
     */
490 View Code Duplication
    public function testCreateUserRequiredFieldsEmpty()
491
    {
492
        $userService = $this->repository->getUserService();
493
494
        $userCreateStruct = $userService->newUserCreateStruct('new_user', '[email protected]', 'password', 'eng-GB');
495
        $userCreateStruct->setField('first_name', '', 'eng-GB');
496
        $userCreateStruct->setField('last_name', '', 'eng-GB');
497
498
        $parentGroup = $userService->loadUserGroup(12);
499
        $userService->createUser($userCreateStruct, [$parentGroup]);
500
    }
501
502
    /**
503
     * Test creating a user throwing InvalidArgumentException.
504
     *
505
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
506
     * @covers \eZ\Publish\API\Repository\UserService::createUser
507
     */
508 View Code Duplication
    public function testCreateUserThrowsInvalidArgumentException()
509
    {
510
        $userService = $this->repository->getUserService();
511
512
        $userCreateStruct = $userService->newUserCreateStruct('admin', '[email protected]', 'password', 'eng-GB');
513
        $userCreateStruct->setField('first_name', '', 'eng-GB');
514
        $userCreateStruct->setField('last_name', '', 'eng-GB');
515
516
        $parentGroup = $userService->loadUserGroup(12);
517
        $userService->createUser($userCreateStruct, [$parentGroup]);
518
    }
519
520
    /**
521
     * Test loading a user.
522
     *
523
     * @covers \eZ\Publish\API\Repository\UserService::loadUser
524
     */
525 View Code Duplication
    public function testLoadUser()
526
    {
527
        $userService = $this->repository->getUserService();
528
529
        $loadedUser = $userService->loadUser(14);
530
531
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\User', $loadedUser);
532
        self::assertEquals(14, $loadedUser->id);
533
    }
534
535
    /**
536
     * Test loading a user throwing NotFoundException.
537
     *
538
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
539
     * @covers \eZ\Publish\API\Repository\UserService::loadUser
540
     */
541
    public function testLoadUserThrowsNotFoundException()
542
    {
543
        $userService = $this->repository->getUserService();
544
545
        $userService->loadUser(APIBaseTest::DB_INT_MAX);
546
    }
547
548
    /**
549
     * Test loading anonymous user.
550
     *
551
     * @covers \eZ\Publish\API\Repository\UserService::loadAnonymousUser
552
     */
553 View Code Duplication
    public function testLoadAnonymousUser()
554
    {
555
        $userService = $this->repository->getUserService();
556
557
        $loadedUser = $userService->loadAnonymousUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...ce::loadAnonymousUser() has been deprecated with message: since 5.3, use loadUser( $anonymousUserId ) 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...
558
559
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\User', $loadedUser);
560
        self::assertEquals(10, $loadedUser->id);
561
    }
562
563
    /**
564
     * Test loading a user by credentials.
565
     *
566
     * @covers \eZ\Publish\API\Repository\UserService::loadUserByCredentials
567
     */
568 View Code Duplication
    public function testLoadUserByCredentials()
569
    {
570
        $userService = $this->repository->getUserService();
571
572
        $loadedUser = $userService->loadUserByCredentials('admin', 'publish');
573
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\User', $loadedUser);
574
575
        $this->assertPropertiesCorrect(
576
            [
577
                'id' => 14,
578
                'login' => 'admin',
579
                'email' => '[email protected]',
580
                'passwordHash' => 'c78e3b0f3d9244ed8c6d1c29464bdff9',
581
                'hashAlgorithm' => User::PASSWORD_HASH_MD5_USER,
582
                'enabled' => true,
583
                'maxLogin' => 10,
584
            ],
585
            $loadedUser
586
        );
587
    }
588
589
    /**
590
     * Test loading a user by credentials throwing NotFoundException.
591
     *
592
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
593
     * @covers \eZ\Publish\API\Repository\UserService::loadUser
594
     */
595
    public function testLoadUserByCredentialsThrowsNotFoundException()
596
    {
597
        $userService = $this->repository->getUserService();
598
599
        $userService->loadUserByCredentials('non_existing_user', 'invalid_password');
600
    }
601
602
    /**
603
     * Test loading a user by credentials throwing NotFoundException because of bad password.
604
     *
605
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
606
     * @covers \eZ\Publish\API\Repository\UserService::loadUser
607
     */
608
    public function testLoadUserByCredentialsThrowsNotFoundExceptionBadPassword()
609
    {
610
        $userService = $this->repository->getUserService();
611
612
        $userService->loadUserByCredentials('admin', 'some_password');
613
    }
614
615
    /**
616
     * Test updating a user.
617
     *
618
     * @covers \eZ\Publish\API\Repository\UserService::updateUser
619
     */
620
    public function testUpdateUser()
621
    {
622
        $userService = $this->repository->getUserService();
623
624
        $userUpdateStruct = $userService->newUserUpdateStruct();
625
        $userUpdateStruct->contentUpdateStruct = $this->repository->getContentService()->newContentUpdateStruct();
626
        $userUpdateStruct->contentUpdateStruct->setField('first_name', 'New first name', 'eng-US');
627
        $userUpdateStruct->contentUpdateStruct->setField('last_name', 'New last name', 'eng-US');
628
629
        $user = $userService->loadUser(14);
630
631
        $userService->updateUser($user, $userUpdateStruct);
632
        $updatedUser = $userService->loadUser($user->id);
633
634
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\User', $updatedUser);
635
        self::assertEquals(
636
            'New first name',
637
            $updatedUser->getFieldValue('first_name')
638
        );
639
640
        self::assertEquals(
641
            'New last name',
642
            $updatedUser->getFieldValue('last_name')
643
        );
644
    }
645
646
    /**
647
     * Test updating a user throwing ContentValidationException.
648
     *
649
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentValidationException
650
     * @covers \eZ\Publish\API\Repository\UserService::updateUser
651
     */
652 View Code Duplication
    public function testUpdateUserThrowsContentValidationException()
653
    {
654
        $userService = $this->repository->getUserService();
655
        $contentService = $this->repository->getContentService();
656
657
        $user = $userService->loadUser(14);
658
        $userUpdateStruct = $userService->newUserUpdateStruct();
659
        $userUpdateStruct->contentUpdateStruct = $contentService->newContentUpdateStruct();
660
        $userUpdateStruct->contentUpdateStruct->setField('name', '', 'eng-US');
661
662
        $userService->updateUser($user, $userUpdateStruct);
663
    }
664
665
    /**
666
     * Test assigning a user group to user.
667
     *
668
     * @covers \eZ\Publish\API\Repository\UserService::assignUserToUserGroup
669
     */
670
    public function testAssignUserToUserGroup()
671
    {
672
        $userService = $this->repository->getUserService();
673
        $locationService = $this->repository->getLocationService();
674
675
        $user = $userService->loadUser(14);
676
        $userGroup = $userService->loadUserGroup(42);
677
678
        $userService->assignUserToUserGroup($user, $userGroup);
679
680
        $userLocations = $locationService->loadLocations($user->getVersionInfo()->getContentInfo());
681
682
        if (!is_array($userLocations) || empty($userLocations)) {
683
            self::fail('Failed assigning user to user group');
684
        }
685
686
        $hasAddedLocation = false;
687 View Code Duplication
        foreach ($userLocations as $location) {
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...
688
            if ($location->parentLocationId == $userGroup->getVersionInfo()->getContentInfo()->mainLocationId) {
689
                $hasAddedLocation = true;
690
                break;
691
            }
692
        }
693
694
        if (!$hasAddedLocation) {
695
            self::fail('Failed assigning user to user group');
696
        }
697
    }
698
699
    /**
700
     * Test assigning a user group to user throwing InvalidArgumentException.
701
     *
702
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
703
     * @covers \eZ\Publish\API\Repository\UserService::assignUserToUserGroup
704
     */
705 View Code Duplication
    public function testAssignUserToUserGroupThrowsInvalidArgumentException()
706
    {
707
        $userService = $this->repository->getUserService();
708
709
        $user = $userService->loadUser(14);
710
        $userGroup = $userService->loadUserGroup(12);
711
        $userService->assignUserToUserGroup($user, $userGroup);
712
    }
713
714
    /**
715
     * Test removing a user from user group.
716
     *
717
     * @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup
718
     * @depends testAssignUserToUserGroup
719
     */
720
    public function testUnAssignUserFromUserGroup()
721
    {
722
        $userService = $this->repository->getUserService();
723
        $locationService = $this->repository->getLocationService();
724
725
        $user = $userService->loadUser(14);
726
        $userGroup = $userService->loadUserGroup(12);
0 ignored issues
show
Unused Code introduced by
$userGroup 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...
727
728
        // first assign another user group as we can't remove all users groups
729
        $userGroup = $userService->loadUserGroup(42);
730
        $userService->assignUserToUserGroup($user, $userGroup);
731
732
        // un-assign original group
733
        $userService->unAssignUserFromUserGroup($user, $userGroup);
734
735
        try {
736
            $user = $userService->loadUser(14);
737
        } catch (NotFoundException $e) {
738
            // user was deleted because the group we assigned him from was his last location
739
            return;
740
        }
741
742
        $userLocations = $locationService->loadLocations($user->getVersionInfo()->getContentInfo());
743
744
        if (is_array($userLocations) && !empty($userLocations)) {
745
            $hasRemovedLocation = false;
746 View Code Duplication
            foreach ($userLocations as $location) {
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...
747
                if ($location->parentLocationId == $userGroup->getVersionInfo()->getContentInfo()->mainLocationId) {
748
                    $hasRemovedLocation = true;
749
                    break;
750
                }
751
            }
752
753
            if ($hasRemovedLocation) {
754
                self::fail('Failed removing a user from user group');
755
            }
756
        }
757
    }
758
759
    /**
760
     * Test removing a user from user group throwing BadState for removing last group.
761
     *
762
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
763
     * @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup
764
     */
765 View Code Duplication
    public function testUnAssignUserFromUserGroupThrowsBadStateException()
766
    {
767
        $userService = $this->repository->getUserService();
768
        $user = $userService->loadUser(14);
769
        $userGroup = $userService->loadUserGroup(12);
770
771
        // un-assign original group, should throw BadState
772
        $userService->unAssignUserFromUserGroup($user, $userGroup);
773
    }
774
775
    /**
776
     * Test removing a user from user group throwing InvalidArgumentException.
777
     *
778
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
779
     * @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup
780
     */
781 View Code Duplication
    public function testUnAssignUserFromUserGroupThrowsInvalidArgumentException()
782
    {
783
        $userService = $this->repository->getUserService();
784
785
        $user = $userService->loadUser(14);
786
        $userGroup = $userService->loadUserGroup(42);
787
        $userService->unAssignUserFromUserGroup($user, $userGroup);
788
    }
789
790
    /**
791
     * Test loading user groups the user belongs to.
792
     *
793
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroupsOfUser
794
     */
795
    public function testLoadUserGroupsOfUser()
796
    {
797
        $userService = $this->repository->getUserService();
798
        $locationService = $this->repository->getLocationService();
799
800
        $user = $userService->loadUser(14);
801
        $userLocations = $locationService->loadLocations(
802
            $user->getVersionInfo()->getContentInfo()
803
        );
804
805
        $groupLocationIds = [];
806
        foreach ($userLocations as $userLocation) {
807
            if ($userLocation->parentLocationId !== null) {
808
                $groupLocationIds[] = $userLocation->parentLocationId;
809
            }
810
        }
811
812
        $userGroups = $userService->loadUserGroupsOfUser($user);
813
814
        foreach ($userGroups as $userGroup) {
815
            self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup', $userGroup);
816
            self::assertContains(
817
                $userGroup->getVersionInfo()->getContentInfo()->mainLocationId,
818
                $groupLocationIds
819
            );
820
        }
821
    }
822
823
    /**
824
     * Test loading user groups the user belongs to.
825
     *
826
     * @covers \eZ\Publish\API\Repository\UserService::loadUsersOfUserGroup
827
     */
828
    public function testLoadUsersOfUserGroup()
829
    {
830
        $userService = $this->repository->getUserService();
831
        $locationService = $this->repository->getLocationService();
832
833
        $userGroup = $userService->loadUserGroup(12);
834
        $users = $userService->loadUsersOfUserGroup($userGroup);
835
836
        self::assertInternalType('array', $users);
837
        self::assertNotEmpty($users);
838
839
        foreach ($users as $user) {
840
            self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\User', $user);
841
842
            $userLocation = $locationService->loadLocation($user->getVersionInfo()->getContentInfo()->mainLocationId);
843
844
            self::assertEquals(
845
                $userGroup->getVersionInfo()->getContentInfo()->mainLocationId,
846
                $userLocation->parentLocationId
847
            );
848
        }
849
    }
850
851
    /**
852
     * Test creating new UserCreateStruct.
853
     *
854
     * @covers \eZ\Publish\API\Repository\UserService::newUserCreateStruct
855
     */
856 View Code Duplication
    public function testNewUserCreateStruct()
857
    {
858
        $userService = $this->repository->getUserService();
859
860
        $userCreateStruct = $userService->newUserCreateStruct('admin', '[email protected]', 'password', 'eng-GB');
861
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserCreateStruct', $userCreateStruct);
862
863
        $this->assertPropertiesCorrect(
864
            [
865
                'mainLanguageCode' => 'eng-GB',
866
                'login' => 'admin',
867
                'email' => '[email protected]',
868
                'password' => 'password',
869
                'enabled' => true,
870
                'fields' => [],
871
            ],
872
            $userCreateStruct
873
        );
874
    }
875
876
    /**
877
     * Test creating new UserGroupCreateStruct.
878
     *
879
     * @covers \eZ\Publish\API\Repository\UserService::newUserGroupCreateStruct
880
     */
881
    public function testNewUserGroupCreateStruct()
882
    {
883
        $userService = $this->repository->getUserService();
884
885
        $userGroupCreateStruct = $userService->newUserGroupCreateStruct('eng-GB');
886
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupCreateStruct', $userGroupCreateStruct);
887
        self::assertEquals('eng-GB', $userGroupCreateStruct->mainLanguageCode);
888
    }
889
890
    /**
891
     * Test creating new UserUpdateStruct.
892
     *
893
     * @covers \eZ\Publish\API\Repository\UserService::newUserUpdateStruct
894
     */
895
    public function testNewUserUpdateStruct()
896
    {
897
        $userService = $this->repository->getUserService();
898
899
        $userUpdateStruct = $userService->newUserUpdateStruct();
900
901
        self::assertInstanceOf(
902
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserUpdateStruct',
903
            $userUpdateStruct
904
        );
905
906
        self::assertNull($userUpdateStruct->contentUpdateStruct);
907
        self::assertNull($userUpdateStruct->contentMetadataUpdateStruct);
908
909
        $this->assertPropertiesCorrect(
910
            [
911
                'email' => null,
912
                'password' => null,
913
                'enabled' => null,
914
                'maxLogin' => null,
915
            ],
916
            $userUpdateStruct
917
        );
918
    }
919
920
    /**
921
     * Test creating new UserGroupUpdateStruct.
922
     *
923
     * @covers \eZ\Publish\API\Repository\UserService::newUserGroupUpdateStruct
924
     */
925
    public function testNewUserGroupUpdateStruct()
926
    {
927
        $userService = $this->repository->getUserService();
928
929
        $userGroupUpdateStruct = $userService->newUserGroupUpdateStruct();
930
931
        self::assertInstanceOf(
932
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupUpdateStruct',
933
            $userGroupUpdateStruct
934
        );
935
936
        self::assertNull($userGroupUpdateStruct->contentUpdateStruct);
937
        self::assertNull($userGroupUpdateStruct->contentMetadataUpdateStruct);
938
    }
939
}
940