Completed
Push — ezp28158_test_updated_user_can... ( 6c9da0...dbade0 )
by
unknown
12:45
created

UserServiceTest::testUpdateUserNoPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 41
nc 1
nop 0
dl 0
loc 65
rs 9.3571
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the UserServiceTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
14
use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct;
15
use eZ\Publish\API\Repository\Values\User\UserUpdateStruct;
16
use eZ\Publish\API\Repository\Values\User\User as ApiUser;
17
use eZ\Publish\Core\Repository\Values\Content\Content;
18
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
19
use eZ\Publish\Core\Repository\Values\User\User;
20
use eZ\Publish\Core\Repository\Values\User\UserGroup;
21
use Exception;
22
use ReflectionClass;
23
24
/**
25
 * Test case for operations in the UserService using in memory storage.
26
 *
27
 * @see eZ\Publish\API\Repository\UserService
28
 * @group integration
29
 * @group user
30
 */
31
class UserServiceTest extends BaseTest
32
{
33
    /**
34
     * Test for the loadUserGroup() method.
35
     *
36
     * @see \eZ\Publish\API\Repository\UserService::loadUserGroup()
37
     */
38
    public function testLoadUserGroup()
39
    {
40
        $repository = $this->getRepository();
41
42
        $mainGroupId = $this->generateId('group', 4);
43
        /* BEGIN: Use Case */
44
        // $mainGroupId is the ID of the main "Users" group
45
46
        $userService = $repository->getUserService();
47
48
        $userGroup = $userService->loadUserGroup($mainGroupId);
49
        /* END: Use Case */
50
51
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup', $userGroup);
52
    }
53
54
    /**
55
     * Test for the loadUserGroup() method.
56
     *
57
     * @see \eZ\Publish\API\Repository\UserService::loadUserGroup()
58
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
59
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup
60
     */
61
    public function testLoadUserGroupThrowsNotFoundException()
62
    {
63
        $repository = $this->getRepository();
64
65
        $nonExistingGroupId = $this->generateId('group', self::DB_INT_MAX);
66
        /* BEGIN: Use Case */
67
        $userService = $repository->getUserService();
68
69
        // This call will fail with a NotFoundException
70
        $userService->loadUserGroup($nonExistingGroupId);
71
        /* END: Use Case */
72
    }
73
74
    /**
75
     * Test for the loadSubUserGroups() method.
76
     *
77
     * @see \eZ\Publish\API\Repository\UserService::loadSubUserGroups()
78
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup
79
     */
80
    public function testLoadSubUserGroups()
81
    {
82
        $repository = $this->getRepository();
83
84
        $mainGroupId = $this->generateId('group', 4);
85
        /* BEGIN: Use Case */
86
        // $mainGroupId is the ID of the main "Users" group
87
88
        $userService = $repository->getUserService();
89
90
        $userGroup = $userService->loadUserGroup($mainGroupId);
91
92
        $subUserGroups = $userService->loadSubUserGroups($userGroup);
93
        foreach ($subUserGroups as $subUserGroup) {
94
            // Do something with the $subUserGroup
95
            $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup', $subUserGroup);
96
        }
97
        /* END: Use Case */
98
    }
99
100
    /**
101
     * Test loading sub groups throwing NotFoundException.
102
     *
103
     * @covers \eZ\Publish\API\Repository\UserService::loadSubUserGroups
104
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
105
     */
106 View Code Duplication
    public function testLoadSubUserGroupsThrowsNotFoundException()
107
    {
108
        $repository = $this->getRepository();
109
        $userService = $repository->getUserService();
110
111
        $parentGroup = new UserGroup(
112
            [
113
                'content' => new Content(
114
                    [
115
                        'versionInfo' => new VersionInfo(
116
                            [
117
                                'contentInfo' => new ContentInfo(
118
                                ['id' => 123456]
119
                            ),
120
                            ]
121
                        ),
122
                        'internalFields' => [],
123
                    ]
124
                ),
125
            ]
126
        );
127
        $userService->loadSubUserGroups($parentGroup);
128
    }
129
130
    /**
131
     * Test for the newUserGroupCreateStruct() method.
132
     *
133
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct
134
     *
135
     * @see \eZ\Publish\API\Repository\UserService::newUserGroupCreateStruct()
136
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
137
     */
138
    public function testNewUserGroupCreateStruct()
139
    {
140
        $repository = $this->getRepository();
141
142
        /* BEGIN: Use Case */
143
        $userService = $repository->getUserService();
144
145
        $groupCreate = $userService->newUserGroupCreateStruct('eng-US');
146
        /* END: Use Case */
147
148
        $this->assertInstanceOf(
149
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupCreateStruct',
150
            $groupCreate
151
        );
152
153
        return $groupCreate;
154
    }
155
156
    /**
157
     * Test for the newUserGroupCreateStruct() method.
158
     *
159
     * @param \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct $groupCreate
160
     *
161
     * @see \eZ\Publish\API\Repository\UserService::newUserGroupCreateStruct()
162
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct
163
     */
164
    public function testNewUserGroupCreateStructSetsMainLanguageCode($groupCreate)
165
    {
166
        $this->assertEquals('eng-US', $groupCreate->mainLanguageCode);
167
    }
168
169
    /**
170
     * Test for the newUserGroupCreateStruct() method.
171
     *
172
     * @param \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct $groupCreate
173
     *
174
     * @see \eZ\Publish\API\Repository\UserService::newUserGroupCreateStruct()
175
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct
176
     */
177
    public function testNewUserGroupCreateStructSetsContentType($groupCreate)
178
    {
179
        $this->assertInstanceOf(
180
            '\\eZ\\Publish\\API\\Repository\\Values\\ContentType\\ContentType',
181
            $groupCreate->contentType
182
        );
183
    }
184
185
    /**
186
     * Test for the newUserGroupCreateStruct() method.
187
     *
188
     * @see \eZ\Publish\API\Repository\UserService::newUserGroupCreateStruct($mainLanguageCode, $contentType)
189
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct
190
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
191
     */
192 View Code Duplication
    public function testNewUserGroupCreateStructWithSecondParameter()
193
    {
194
        if ($this->isVersion4()) {
195
            $this->markTestSkipped('This test is only relevant for eZ Publish versions > 4');
196
        }
197
198
        $repository = $this->getRepository();
199
200
        /* BEGIN: Use Case */
201
        $contentTypeService = $repository->getContentTypeService();
202
        $userService = $repository->getUserService();
203
204
        // Load the default ContentType for user groups
205
        $groupType = $contentTypeService->loadContentTypeByIdentifier('user_group');
206
207
        // Instantiate a new group create struct
208
        $groupCreate = $userService->newUserGroupCreateStruct(
209
            'eng-US',
210
            $groupType
211
        );
212
        /* END: Use Case */
213
214
        $this->assertSame($groupType, $groupCreate->contentType);
215
    }
216
217
    /**
218
     * Test for the createUserGroup() method.
219
     *
220
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
221
     *
222
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
223
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct
224
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup
225
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
226
     */
227
    public function testCreateUserGroup()
228
    {
229
        /* BEGIN: Use Case */
230
        $userGroup = $this->createUserGroupVersion1();
231
        /* END: Use Case */
232
233
        $this->assertInstanceOf(
234
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup',
235
            $userGroup
236
        );
237
238
        $versionInfo = $userGroup->getVersionInfo();
239
240
        $this->assertEquals(APIVersionInfo::STATUS_PUBLISHED, $versionInfo->status);
241
        $this->assertEquals(1, $versionInfo->versionNo);
242
243
        return $userGroup;
244
    }
245
246
    /**
247
     * Test for the createUserGroup() method.
248
     *
249
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
250
     *
251
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
252
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
253
     */
254
    public function testCreateUserGroupSetsExpectedProperties($userGroup)
255
    {
256
        $this->assertEquals(
257
            array(
258
                'parentId' => $this->generateId('group', 4),
259
                'subGroupCount' => 0,
260
            ),
261
            array(
262
                'parentId' => $userGroup->parentId,
263
                'subGroupCount' => $userGroup->subGroupCount,
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\API\Repositor...erGroup::$subGroupCount has been deprecated with message: As of eZ Publish 5.3.3, count can be obtained on demand using location service.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
264
            )
265
        );
266
    }
267
268
    /**
269
     * Test for the createUserGroup() method.
270
     *
271
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
272
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
273
     */
274 View Code Duplication
    public function testCreateUserGroupIncrementsParentSubGroupCount()
275
    {
276
        $repository = $this->getRepository();
277
        $userService = $repository->getUserService();
278
        $mainGroupId = $this->generateId('group', 4);
279
280
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
281
        $parentGroupCount = $parentUserGroup->subGroupCount;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\API\Repositor...erGroup::$subGroupCount has been deprecated with message: As of eZ Publish 5.3.3, count can be obtained on demand using location service.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
282
283
        /* BEGIN: Use Case */
284
        $this->createUserGroupVersion1();
285
286
        $this->refreshSearch($repository);
287
288
        // This should be one greater than before
289
        $subGroupCount = $userService->loadUserGroup($mainGroupId)->subGroupCount;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\API\Repositor...erGroup::$subGroupCount has been deprecated with message: As of eZ Publish 5.3.3, count can be obtained on demand using location service.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
290
        /* END: Use Case */
291
292
        $this->assertEquals($parentGroupCount + 1, $subGroupCount);
293
    }
294
295
    /**
296
     * Test for the createUserGroup() method.
297
     *
298
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
299
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
300
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
301
     */
302 View Code Duplication
    public function testCreateUserGroupThrowsInvalidArgumentException()
303
    {
304
        $repository = $this->getRepository();
305
306
        $mainGroupId = $this->generateId('group', 4);
307
        /* BEGIN: Use Case */
308
        // $mainGroupId is the ID of the main "Users" group
309
310
        $userService = $repository->getUserService();
311
312
        // Load main group
313
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
314
315
        // Instantiate a new create struct
316
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
317
        $userGroupCreate->setField('name', 'Example Group');
318
        $userGroupCreate->remoteId = '5f7f0bdb3381d6a461d8c29ff53d908f';
319
320
        // This call will fail with an "InvalidArgumentException", because the
321
        // specified remoteId is already used for the "Members" user group.
322
        $userService->createUserGroup(
323
            $userGroupCreate,
324
            $parentUserGroup
325
        );
326
        /* END: Use Case */
327
    }
328
329
    /**
330
     * Test for the createUserGroup() method.
331
     *
332
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
333
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
334
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
335
     */
336
    public function testCreateUserGroupThrowsInvalidArgumentExceptionFieldTypeNotAccept()
337
    {
338
        $repository = $this->getRepository();
339
340
        $mainGroupId = $this->generateId('group', 4);
341
        /* BEGIN: Use Case */
342
        // $mainGroupId is the ID of the main "Users" group
343
344
        $userService = $repository->getUserService();
345
346
        // Load main group
347
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
348
349
        // Instantiate a new create struct
350
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
351
        $userGroupCreate->setField('name', new \stdClass());
352
353
        // This call will fail with an "InvalidArgumentException", because the
354
        // specified remoteId is already used for the "Members" user group.
355
        $userService->createUserGroup(
356
            $userGroupCreate,
357
            $parentUserGroup
358
        );
359
        /* END: Use Case */
360
    }
361
362
    /**
363
     * Test for the createUserGroup() method.
364
     *
365
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
366
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
367
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
368
     */
369
    public function testCreateUserGroupWhenMissingField()
370
    {
371
        $repository = $this->getRepository();
372
373
        $mainGroupId = $this->generateId('group', 4);
374
        /* BEGIN: Use Case */
375
        // $mainGroupId is the ID of the main "Users" group
376
377
        $userService = $repository->getUserService();
378
379
        // Load main group
380
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
381
382
        // Instantiate a new create struct
383
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
384
385
        // This call will fail with a "ContentFieldValidationException", because the
386
        // only mandatory field "name" is not set.
387
        $userService->createUserGroup($userGroupCreate, $parentUserGroup);
388
        /* END: Use Case */
389
    }
390
391
    /**
392
     * Test for the createUserGroup() method.
393
     *
394
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
395
     *
396
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
397
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct
398
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup
399
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
400
     */
401
    public function testCreateUserGroupInTransactionWithRollback()
402
    {
403
        $repository = $this->getRepository();
404
405
        $mainGroupId = $this->generateId('group', 4);
406
        /* BEGIN: Use Case */
407
        // $mainGroupId is the ID of the main "Users" group
408
409
        $userService = $repository->getUserService();
410
411
        $repository->beginTransaction();
412
413
        try {
414
            // Load main group
415
            $parentUserGroup = $userService->loadUserGroup($mainGroupId);
416
417
            // Instantiate a new create struct
418
            $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
419
            $userGroupCreate->setField('name', 'Example Group');
420
421
            // Create the new user group
422
            $createdUserGroupId = $userService->createUserGroup(
423
                $userGroupCreate,
424
                $parentUserGroup
425
            )->id;
426
        } catch (Exception $e) {
427
            // Cleanup hanging transaction on error
428
            $repository->rollback();
429
            throw $e;
430
        }
431
432
        $repository->rollback();
433
434
        try {
435
            // Throws exception since creation of user group was rolled back
436
            $loadedGroup = $userService->loadUserGroup($createdUserGroupId);
0 ignored issues
show
Unused Code introduced by
$loadedGroup 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...
437
        } catch (NotFoundException $e) {
438
            return;
439
        }
440
        /* END: Use Case */
441
442
        $this->fail('User group object still exists after rollback.');
443
    }
444
445
    /**
446
     * Test for the deleteUserGroup() method.
447
     *
448
     * @see \eZ\Publish\API\Repository\UserService::deleteUserGroup()
449
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
450
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
451
     */
452
    public function testDeleteUserGroup()
453
    {
454
        $repository = $this->getRepository();
455
        $userService = $repository->getUserService();
456
457
        /* BEGIN: Use Case */
458
        $userGroup = $this->createUserGroupVersion1();
459
460
        // Delete the currently created user group again
461
        $userService->deleteUserGroup($userGroup);
462
        /* END: Use Case */
463
464
        // We use the NotFoundException here for verification
465
        $userService->loadUserGroup($userGroup->id);
466
    }
467
468
    /**
469
     * Test deleting user group throwing NotFoundException.
470
     *
471
     * @covers \eZ\Publish\API\Repository\UserService::deleteUserGroup
472
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
473
     */
474 View Code Duplication
    public function testDeleteUserGroupThrowsNotFoundException()
475
    {
476
        $repository = $this->getRepository();
477
        $userService = $repository->getUserService();
478
479
        $userGroup = new UserGroup(
480
            [
481
                'content' => new Content(
482
                    [
483
                        'versionInfo' => new VersionInfo(
484
                            ['contentInfo' => new ContentInfo(['id' => 123456])]
485
                        ),
486
                        'internalFields' => [],
487
                    ]
488
                ),
489
            ]
490
        );
491
        $userService->deleteUserGroup($userGroup);
492
    }
493
494
    /**
495
     * Test for the moveUserGroup() method.
496
     *
497
     * @see \eZ\Publish\API\Repository\UserService::moveUserGroup()
498
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
499
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadSubUserGroups
500
     */
501
    public function testMoveUserGroup()
502
    {
503
        $repository = $this->getRepository();
504
        $userService = $repository->getUserService();
505
506
        $membersGroupId = $this->generateId('group', 13);
507
        /* BEGIN: Use Case */
508
        // $membersGroupId is the ID of the "Members" user group in an eZ
509
        // Publish demo installation
510
511
        $userGroup = $this->createUserGroupVersion1();
512
513
        // Load the new parent group
514
        $membersUserGroup = $userService->loadUserGroup($membersGroupId);
515
516
        // Move user group from "Users" to "Members"
517
        $userService->moveUserGroup($userGroup, $membersUserGroup);
518
519
        // Reload the user group to get an updated $parentId
520
        $userGroup = $userService->loadUserGroup($userGroup->id);
521
522
        $this->refreshSearch($repository);
523
524
        // The returned array will no contain $userGroup
525
        $subUserGroups = $userService->loadSubUserGroups(
526
            $membersUserGroup
527
        );
528
        /* END: Use Case */
529
530
        $subUserGroupIds = array_map(
531
            function ($content) {
532
                return $content->id;
533
            },
534
            $subUserGroups
535
        );
536
537
        $this->assertEquals($membersGroupId, $userGroup->parentId);
538
        $this->assertEquals(array($userGroup->id), $subUserGroupIds);
539
    }
540
541
    /**
542
     * Test for the moveUserGroup() method.
543
     *
544
     * @see \eZ\Publish\API\Repository\UserService::moveUserGroup()
545
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testMoveUserGroup
546
     */
547 View Code Duplication
    public function testMoveUserGroupIncrementsSubGroupCountOnNewParent()
548
    {
549
        $repository = $this->getRepository();
550
        $userService = $repository->getUserService();
551
552
        $membersGroupId = $this->generateId('group', 13);
553
        /* BEGIN: Use Case */
554
        // $membersGroupId is the ID of the "Members" user group in an eZ
555
        // Publish demo installation
556
557
        $userGroup = $this->createUserGroupVersion1();
558
559
        // Load the new parent group
560
        $membersUserGroup = $userService->loadUserGroup($membersGroupId);
561
562
        // Move user group from "Users" to "Members"
563
        $userService->moveUserGroup($userGroup, $membersUserGroup);
564
565
        $this->refreshSearch($repository);
566
567
        // Reload the user group to get an updated $subGroupCount
568
        $membersUserGroupUpdated = $userService->loadUserGroup($membersGroupId);
569
        /* END: Use Case */
570
571
        $this->assertEquals(1, $membersUserGroupUpdated->subGroupCount);
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\API\Repositor...erGroup::$subGroupCount has been deprecated with message: As of eZ Publish 5.3.3, count can be obtained on demand using location service.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
572
    }
573
574
    /**
575
     * Test for the moveUserGroup() method.
576
     *
577
     * @see \eZ\Publish\API\Repository\UserService::moveUserGroup()
578
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testMoveUserGroup
579
     */
580 View Code Duplication
    public function testMoveUserGroupDecrementsSubGroupCountOnOldParent()
581
    {
582
        $repository = $this->getRepository();
583
        $userService = $repository->getUserService();
584
585
        $membersGroupId = $this->generateId('group', 13);
586
        /* BEGIN: Use Case */
587
        // $membersGroupId is the ID of the "Members" user group in an eZ
588
        // Publish demo installation
589
590
        $userGroup = $this->createUserGroupVersion1();
591
592
        // Load the new parent group
593
        $membersUserGroup = $userService->loadUserGroup($membersGroupId);
594
595
        // Move user group from "Users" to "Members"
596
        $userService->moveUserGroup($userGroup, $membersUserGroup);
597
        /* END: Use Case */
598
599
        $mainUserGroup = $userService->loadUserGroup($this->generateId('group', 4));
600
601
        $this->assertEquals(5, $mainUserGroup->subGroupCount);
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\API\Repositor...erGroup::$subGroupCount has been deprecated with message: As of eZ Publish 5.3.3, count can be obtained on demand using location service.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
602
    }
603
604
    /**
605
     * Test moving a user group below another group throws NotFoundException.
606
     *
607
     * @covers \eZ\Publish\API\Repository\UserService::moveUserGroup
608
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
609
     */
610
    public function testMoveUserGroupThrowsNotFoundException()
611
    {
612
        $repository = $this->getRepository();
613
        $userService = $repository->getUserService();
614
615
        $userGroupToMove = new UserGroup(
616
            [
617
                'content' => new Content(
618
                    [
619
                        'versionInfo' => new VersionInfo(
620
                            ['contentInfo' => new ContentInfo(['id' => 123456])]
621
                        ),
622
                        'internalFields' => [],
623
                    ]
624
                ),
625
            ]
626
        );
627
        $parentUserGroup = new UserGroup(
628
            [
629
                'content' => new Content(
630
                    [
631
                        'versionInfo' => new VersionInfo(
632
                            ['contentInfo' => new ContentInfo(['id' => 123455])]
633
                        ),
634
                        'internalFields' => [],
635
                    ]
636
                ),
637
            ]
638
        );
639
        $userService->moveUserGroup($userGroupToMove, $parentUserGroup);
640
    }
641
642
    /**
643
     * Test for the newUserGroupUpdateStruct() method.
644
     *
645
     * @covers \eZ\Publish\API\Repository\UserService::newUserGroupUpdateStruct
646
     */
647
    public function testNewUserGroupUpdateStruct()
648
    {
649
        $repository = $this->getRepository();
650
651
        /* BEGIN: Use Case */
652
        $userService = $repository->getUserService();
653
654
        $groupUpdate = $userService->newUserGroupUpdateStruct();
655
        /* END: Use Case */
656
657
        $this->assertInstanceOf(
658
            UserGroupUpdateStruct::class,
659
            $groupUpdate
660
        );
661
662
        $this->assertNull($groupUpdate->contentUpdateStruct);
663
        $this->assertNull($groupUpdate->contentMetadataUpdateStruct);
664
    }
665
666
    /**
667
     * Test for the updateUserGroup() method.
668
     *
669
     * @see \eZ\Publish\API\Repository\UserService::updateUserGroup()
670
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
671
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupUpdateStruct
672
     */
673
    public function testUpdateUserGroup()
674
    {
675
        $repository = $this->getRepository();
676
        $userService = $repository->getUserService();
677
678
        /* BEGIN: Use Case */
679
        $userGroup = $this->createUserGroupVersion1();
680
681
        // Create a group update struct and change nothing
682
        $groupUpdate = $userService->newUserGroupUpdateStruct();
683
684
        // This update will do nothing
685
        $userGroup = $userService->updateUserGroup(
686
            $userGroup,
687
            $groupUpdate
688
        );
689
        /* END: Use Case */
690
691
        $this->assertInstanceOf(
692
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroup',
693
            $userGroup
694
        );
695
696
        $this->assertEquals(1, $userGroup->getVersionInfo()->versionNo);
697
    }
698
699
    /**
700
     * Test for the updateUserGroup() method.
701
     *
702
     * @see \eZ\Publish\API\Repository\UserService::updateUserGroup()
703
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUserGroup
704
     */
705 View Code Duplication
    public function testUpdateUserGroupWithSubContentUpdateStruct()
706
    {
707
        $repository = $this->getRepository();
708
        $userService = $repository->getUserService();
709
710
        /* BEGIN: Use Case */
711
        $userGroup = $this->createUserGroupVersion1();
712
713
        // Load the content service
714
        $contentService = $repository->getContentService();
715
716
        // Create a content update struct and update the group name
717
        $contentUpdate = $contentService->newContentUpdateStruct();
718
        $contentUpdate->setField('name', 'Sindelfingen', 'eng-US');
719
720
        // Create a group update struct and set content update struct
721
        $groupUpdate = $userService->newUserGroupUpdateStruct();
722
        $groupUpdate->contentUpdateStruct = $contentUpdate;
723
724
        // This will update the name and the increment the group version number
725
        $userGroup = $userService->updateUserGroup(
726
            $userGroup,
727
            $groupUpdate
728
        );
729
        /* END: Use Case */
730
731
        $this->assertEquals('Sindelfingen', $userGroup->getFieldValue('name', 'eng-US'));
732
733
        $versionInfo = $userGroup->getVersionInfo();
734
735
        $this->assertEquals(APIVersionInfo::STATUS_PUBLISHED, $versionInfo->status);
736
        $this->assertEquals(2, $versionInfo->versionNo);
737
    }
738
739
    /**
740
     * Test for the updateUserGroup() method.
741
     *
742
     * @see \eZ\Publish\API\Repository\UserService::updateUserGroup()
743
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUserGroup
744
     */
745
    public function testUpdateUserGroupWithSubContentMetadataUpdateStruct()
746
    {
747
        $repository = $this->getRepository();
748
        $userService = $repository->getUserService();
749
750
        /* BEGIN: Use Case */
751
        $userGroup = $this->createUserGroupVersion1();
752
753
        // Load the content service
754
        $contentService = $repository->getContentService();
755
756
        // Create a metadata update struct and change the remoteId
757
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
758
        $metadataUpdate->remoteId = '3c61299780663bafa3af2101e52125da';
759
760
        // Create a group update struct and set content update struct
761
        $groupUpdate = $userService->newUserGroupUpdateStruct();
762
        $groupUpdate->contentMetadataUpdateStruct = $metadataUpdate;
763
764
        // This will update the name and the increment the group version number
765
        $userGroup = $userService->updateUserGroup(
766
            $userGroup,
767
            $groupUpdate
768
        );
769
        /* END: Use Case */
770
771
        $this->assertEquals(
772
            '3c61299780663bafa3af2101e52125da',
773
            $userGroup->contentInfo->remoteId
774
        );
775
776
        $versionInfo = $userGroup->getVersionInfo();
777
778
        $this->assertEquals(APIVersionInfo::STATUS_PUBLISHED, $versionInfo->status);
779
        $this->assertEquals(1, $versionInfo->versionNo);
780
    }
781
782
    /**
783
     * Test for the updateUserGroup() method.
784
     *
785
     * @see \eZ\Publish\API\Repository\UserService::updateUserGroup()
786
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
787
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUserGroup
788
     */
789 View Code Duplication
    public function testUpdateUserGroupThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
790
    {
791
        $repository = $this->getRepository();
792
        $userService = $repository->getUserService();
793
794
        /* BEGIN: Use Case */
795
        $userGroup = $this->createUserGroupVersion1();
796
797
        // Load the content service
798
        $contentService = $repository->getContentService();
799
800
        // Create a content update struct and update the group name
801
        $contentUpdate = $contentService->newContentUpdateStruct();
802
        // An object of stdClass is not accepted as a value by the field "name"
803
        $contentUpdate->setField('name', new \stdClass(), 'eng-US');
804
805
        // Create a group update struct and set content update struct
806
        $groupUpdate = $userService->newUserGroupUpdateStruct();
807
        $groupUpdate->contentUpdateStruct = $contentUpdate;
808
809
        // This call will fail with an InvalidArgumentException, because the
810
        // field "name" does not accept the given value
811
        $userService->updateUserGroup($userGroup, $groupUpdate);
812
        /* END: Use Case */
813
    }
814
815
    /**
816
     * Test for the newUserCreateStruct() method.
817
     *
818
     * @see \eZ\Publish\API\Repository\UserService::newUserCreateStruct()
819
     */
820
    public function testNewUserCreateStruct()
821
    {
822
        $repository = $this->getRepository();
823
824
        /* BEGIN: Use Case */
825
        $userService = $repository->getUserService();
826
827
        $userCreate = $userService->newUserCreateStruct(
828
            'user',
829
            '[email protected]',
830
            'secret',
831
            'eng-US'
832
        );
833
        /* END: Use Case */
834
835
        $this->assertInstanceOf(
836
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserCreateStruct',
837
            $userCreate
838
        );
839
840
        return $userCreate;
841
    }
842
843
    /**
844
     * Test updating a user group throws ContentFieldValidationException.
845
     *
846
     * @covers \eZ\Publish\API\Repository\UserService::updateUserGroup
847
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
848
     */
849
    public function testUpdateUserGroupThrowsContentFieldValidationExceptionOnRequiredFieldEmpty()
850
    {
851
        $repository = $this->getRepository();
852
        $userService = $repository->getUserService();
853
        $contentService = $repository->getContentService();
854
855
        $userGroup = $userService->loadUserGroup(42);
856
        $userGroupUpdateStruct = $userService->newUserGroupUpdateStruct();
857
        $userGroupUpdateStruct->contentUpdateStruct = $contentService->newContentUpdateStruct();
858
        $userGroupUpdateStruct->contentUpdateStruct->setField('name', '', 'eng-US');
859
860
        $userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
861
    }
862
863
    /**
864
     * Test for the newUserCreateStruct() method.
865
     *
866
     * @param \eZ\Publish\API\Repository\Values\User\UserCreateStruct $userCreate
867
     *
868
     * @see \eZ\Publish\API\Repository\UserService::newUserCreateStruct()
869
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserCreateStruct
870
     */
871 View Code Duplication
    public function testNewUserCreateStructSetsExpectedProperties($userCreate)
872
    {
873
        $this->assertEquals(
874
            array(
875
                'login' => 'user',
876
                'email' => '[email protected]',
877
                'password' => 'secret',
878
                'mainLanguageCode' => 'eng-US',
879
            ),
880
            array(
881
                'login' => $userCreate->login,
882
                'email' => $userCreate->email,
883
                'password' => $userCreate->password,
884
                'mainLanguageCode' => $userCreate->mainLanguageCode,
885
            )
886
        );
887
    }
888
889
    /**
890
     * Test for the newUserCreateStruct() method.
891
     *
892
     * @see \eZ\Publish\API\Repository\UserService::newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType)
893
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserCreateStruct
894
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
895
     */
896 View Code Duplication
    public function testNewUserCreateStructWithFifthParameter()
897
    {
898
        if ($this->isVersion4()) {
899
            $this->markTestSkipped('This test is only relevant for eZ Publish versions > 4');
900
        }
901
902
        $repository = $this->getRepository();
903
904
        /* BEGIN: Use Case */
905
        $contentTypeService = $repository->getContentTypeService();
906
        $userService = $repository->getUserService();
907
908
        $userType = $contentTypeService->loadContentTypeByIdentifier('user');
909
910
        $userCreate = $userService->newUserCreateStruct(
911
            'user',
912
            '[email protected]',
913
            'secret',
914
            'eng-US',
915
            $userType
916
        );
917
        /* END: Use Case */
918
919
        $this->assertSame($userType, $userCreate->contentType);
920
    }
921
922
    /**
923
     * Test for the createUser() method.
924
     *
925
     * @return \eZ\Publish\API\Repository\Values\User\User
926
     *
927
     * @see \eZ\Publish\API\Repository\UserService::createUser()
928
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup
929
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserCreateStruct
930
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
931
     */
932
    public function testCreateUser()
933
    {
934
        /* BEGIN: Use Case */
935
        $user = $this->createUserVersion1();
936
        /* END: Use Case */
937
938
        $this->assertInstanceOf(
939
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\User',
940
            $user
941
        );
942
943
        return $user;
944
    }
945
946
    /**
947
     * Test for the createUser() method.
948
     *
949
     * @param \eZ\Publish\API\Repository\Values\User\User $user
950
     *
951
     * @see \eZ\Publish\API\Repository\UserService::createUser()
952
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
953
     */
954
    public function testCreateUserSetsExpectedProperties(User $user)
955
    {
956
        $this->assertEquals(
957
            array(
958
                'login' => 'user',
959
                'email' => '[email protected]',
960
                'mainLanguageCode' => 'eng-US',
961
            ),
962
            array(
963
                'login' => $user->login,
0 ignored issues
show
Documentation introduced by
The property $login is declared protected in eZ\Publish\API\Repository\Values\User\User. 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...
964
                'email' => $user->email,
0 ignored issues
show
Documentation introduced by
The property $email is declared protected in eZ\Publish\API\Repository\Values\User\User. 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...
965
                'mainLanguageCode' => $user->contentInfo->mainLanguageCode,
966
            )
967
        );
968
    }
969
970
    /**
971
     * Test for the createUser() method.
972
     *
973
     * @see \eZ\Publish\API\Repository\UserService::createUser()
974
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
975
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
976
     */
977 View Code Duplication
    public function testCreateUserWhenMissingField()
978
    {
979
        $repository = $this->getRepository();
980
981
        $editorsGroupId = $this->generateId('group', 13);
982
        /* BEGIN: Use Case */
983
        // $editorsGroupId is the ID of the "Editors" user group in an eZ
984
        // Publish demo installation
985
986
        $userService = $repository->getUserService();
987
988
        // Instantiate a create struct with mandatory properties
989
        $userCreate = $userService->newUserCreateStruct(
990
            'user',
991
            '[email protected]',
992
            'secret',
993
            'eng-US'
994
        );
995
996
        // Do not set the mandatory fields "first_name" and "last_name"
997
        //$userCreate->setField( 'first_name', 'Example' );
998
        //$userCreate->setField( 'last_name', 'User' );
999
1000
        // Load parent group for the user
1001
        $group = $userService->loadUserGroup($editorsGroupId);
1002
1003
        // This call will fail with a "ContentFieldValidationException", because the
1004
        // mandatory fields "first_name" and "last_name" are not set.
1005
        $userService->createUser($userCreate, array($group));
1006
        /* END: Use Case */
1007
    }
1008
1009
    /**
1010
     * Test for the createUser() method.
1011
     *
1012
     * @see \eZ\Publish\API\Repository\UserService::createUser()
1013
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1014
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1015
     */
1016 View Code Duplication
    public function testCreateUserThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
1017
    {
1018
        $repository = $this->getRepository();
1019
1020
        $editorsGroupId = $this->generateId('group', 13);
1021
        /* BEGIN: Use Case */
1022
        // $editorsGroupId is the ID of the "Editors" user group in an eZ
1023
        // Publish demo installation
1024
1025
        $userService = $repository->getUserService();
1026
1027
        // Instantiate a create struct with mandatory properties
1028
        $userCreate = $userService->newUserCreateStruct(
1029
            'user',
1030
            '[email protected]',
1031
            'secret',
1032
            'eng-US'
1033
        );
1034
1035
        // An object of stdClass is not a valid value for the field first_name
1036
        $userCreate->setField('first_name', new \stdClass());
1037
        $userCreate->setField('last_name', 'User');
1038
1039
        // Load parent group for the user
1040
        $group = $userService->loadUserGroup($editorsGroupId);
1041
1042
        // This call will fail with an "InvalidArgumentException", because the
1043
        // value for the firled "first_name" is not accepted by the field type.
1044
        $userService->createUser($userCreate, array($group));
1045
        /* END: Use Case */
1046
    }
1047
1048
    /**
1049
     * Test for the createUser() method.
1050
     *
1051
     * @covers \eZ\Publish\API\Repository\UserService::createUser
1052
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1053
     * @expectedExceptionMessage Argument 'userCreateStruct' is invalid: User with provided login already exists
1054
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1055
     */
1056 View Code Duplication
    public function testCreateUserThrowsInvalidArgumentException()
1057
    {
1058
        $repository = $this->getRepository();
1059
1060
        $editorsGroupId = $this->generateId('group', 13);
1061
        /* BEGIN: Use Case */
1062
        // $editorsGroupId is the ID of the "Editors" user group in an eZ
1063
        // Publish demo installation
1064
1065
        $userService = $repository->getUserService();
1066
1067
        // Instantiate a create struct with mandatory properties
1068
        $userCreate = $userService->newUserCreateStruct(
1069
            // admin is an existing login
1070
            'admin',
1071
            '[email protected]',
1072
            'secret',
1073
            'eng-US'
1074
        );
1075
1076
        $userCreate->setField('first_name', 'Example');
1077
        $userCreate->setField('last_name', 'User');
1078
1079
        // Load parent group for the user
1080
        $group = $userService->loadUserGroup($editorsGroupId);
1081
1082
        // This call will fail with a "InvalidArgumentException", because the
1083
        // user with "admin" login already exists.
1084
        $userService->createUser($userCreate, array($group));
1085
        /* END: Use Case */
1086
    }
1087
1088
    /**
1089
     * Test for the createUser() method.
1090
     *
1091
     * @return \eZ\Publish\API\Repository\Values\User\User
1092
     *
1093
     * @see \eZ\Publish\API\Repository\UserService::createUser()
1094
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup
1095
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserCreateStruct
1096
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
1097
     */
1098
    public function testCreateUserInTransactionWithRollback()
1099
    {
1100
        $repository = $this->getRepository();
1101
        $userService = $repository->getUserService();
1102
1103
        /* BEGIN: Use Case */
1104
        $repository->beginTransaction();
1105
1106
        try {
1107
            $user = $this->createUserVersion1();
1108
        } catch (Exception $e) {
1109
            // Cleanup hanging transaction on error
1110
            $repository->rollback();
1111
            throw $e;
1112
        }
1113
1114
        $repository->rollback();
1115
1116
        try {
1117
            // Throws exception since creation of user was rolled back
1118
            $loadedUser = $userService->loadUser($user->id);
0 ignored issues
show
Unused Code introduced by
$loadedUser 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...
1119
        } catch (NotFoundException $e) {
1120
            return;
1121
        }
1122
        /* END: Use Case */
1123
1124
        $this->fail('User object still exists after rollback.');
1125
    }
1126
1127
    /**
1128
     * Test creating a user throwing NotFoundException.
1129
     *
1130
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1131
     * @covers \eZ\Publish\API\Repository\UserService::createUser
1132
     */
1133
    public function testCreateUserThrowsNotFoundException()
1134
    {
1135
        $repository = $this->getRepository();
1136
        $userService = $repository->getUserService();
1137
1138
        $userCreateStruct = $userService->newUserCreateStruct('new_user', '[email protected]', 'password', 'eng-GB');
1139
        $userCreateStruct->setField('first_name', 'New');
1140
        $userCreateStruct->setField('last_name', 'User');
1141
1142
        $parentGroup = new UserGroup(
1143
            [
1144
                'content' => new Content(
1145
                    [
1146
                        'versionInfo' => new VersionInfo(
1147
                            [
1148
                                'contentInfo' => new ContentInfo(['id' => 123456]),
1149
                            ]
1150
                        ),
1151
                        'internalFields' => [],
1152
                    ]
1153
                ),
1154
            ]
1155
        );
1156
        $userService->createUser($userCreateStruct, [$parentGroup]);
1157
    }
1158
1159
    /**
1160
     * Test for the loadUser() method.
1161
     *
1162
     * @see \eZ\Publish\API\Repository\UserService::loadUser()
1163
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1164
     */
1165
    public function testLoadUser()
1166
    {
1167
        $repository = $this->getRepository();
1168
1169
        $userService = $repository->getUserService();
1170
1171
        /* BEGIN: Use Case */
1172
        $user = $this->createUserVersion1();
1173
1174
        // Load the newly created user
1175
        $userReloaded = $userService->loadUser($user->id);
1176
        /* END: Use Case */
1177
1178
        $this->assertEquals($user, $userReloaded);
1179
    }
1180
1181
    /**
1182
     * Test for the loadUser() method.
1183
     *
1184
     * @see \eZ\Publish\API\Repository\UserService::loadUser()
1185
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1186
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUser
1187
     */
1188
    public function testLoadUserThrowsNotFoundException()
1189
    {
1190
        $repository = $this->getRepository();
1191
1192
        $nonExistingUserId = $this->generateId('user', self::DB_INT_MAX);
1193
        /* BEGIN: Use Case */
1194
        $userService = $repository->getUserService();
1195
1196
        // This call will fail with a "NotFoundException", because no user with
1197
        // an id equal to self::DB_INT_MAX should exist.
1198
        $userService->loadUser($nonExistingUserId);
1199
        /* END: Use Case */
1200
    }
1201
1202
    /**
1203
     * Test for the loadAnonymousUser() method.
1204
     *
1205
     * @see \eZ\Publish\API\Repository\UserService::loadAnonymousUser()
1206
     */
1207
    public function testLoadAnonymousUser()
1208
    {
1209
        $repository = $this->getRepository();
1210
1211
        $anonymousUserId = $this->generateId('user', 10);
1212
        /* BEGIN: Use Case */
1213
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
1214
        // Publish demo installation.
1215
        $userService = $repository->getUserService();
1216
1217
        // Load default anonymous user available in each eZ Publish installation
1218
        $anonymousUser = $userService->loadUser($anonymousUserId);
1219
        /* END: Use Case */
1220
1221
        $this->assertInstanceOf(
1222
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\User',
1223
            $anonymousUser
1224
        );
1225
1226
        $this->assertEquals('anonymous', $anonymousUser->login);
1227
    }
1228
1229
    /**
1230
     * Test for the loadUserByCredentials() method.
1231
     *
1232
     * @see \eZ\Publish\API\Repository\UserService::loadUserByCredentials()
1233
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1234
     */
1235
    public function testLoadUserByCredentials()
1236
    {
1237
        $repository = $this->getRepository();
1238
1239
        $userService = $repository->getUserService();
1240
1241
        /* BEGIN: Use Case */
1242
        $user = $this->createUserVersion1();
1243
1244
        // Load the newly created user
1245
        $userReloaded = $userService->loadUserByCredentials('user', 'secret');
1246
        /* END: Use Case */
1247
1248
        $this->assertEquals($user, $userReloaded);
1249
    }
1250
1251
    /**
1252
     * Test for the loadUserByCredentials() method.
1253
     *
1254
     * @see \eZ\Publish\API\Repository\UserService::loadUserByCredentials()
1255
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1256
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserByCredentials
1257
     */
1258
    public function testLoadUserByCredentialsThrowsNotFoundExceptionForUnknownPassword()
1259
    {
1260
        $repository = $this->getRepository();
1261
1262
        $userService = $repository->getUserService();
1263
1264
        /* BEGIN: Use Case */
1265
        $this->createUserVersion1();
1266
1267
        // This call will fail with a "NotFoundException", because the given
1268
        // login/password combination does not exist.
1269
        $userService->loadUserByCredentials('user', 'SeCrEt');
1270
        /* END: Use Case */
1271
    }
1272
1273
    /**
1274
     * Test for the loadUserByCredentials() method.
1275
     *
1276
     * @see \eZ\Publish\API\Repository\UserService::loadUserByCredentials()
1277
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1278
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserByCredentials
1279
     */
1280
    public function testLoadUserByCredentialsThrowsNotFoundExceptionForUnknownPasswordEmtpy()
1281
    {
1282
        $repository = $this->getRepository();
1283
1284
        $userService = $repository->getUserService();
1285
1286
        /* BEGIN: Use Case */
1287
        $this->createUserVersion1();
1288
1289
        // This call will fail with a "NotFoundException", because the given
1290
        // login/password combination does not exist.
1291
        $userService->loadUserByCredentials('user', '');
1292
        /* END: Use Case */
1293
    }
1294
1295
    /**
1296
     * Test for the loadUserByCredentials() method.
1297
     *
1298
     * @see \eZ\Publish\API\Repository\UserService::loadUserByCredentials()
1299
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1300
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserByCredentials
1301
     */
1302
    public function testLoadUserByCredentialsThrowsNotFoundExceptionForUnknownLogin()
1303
    {
1304
        $repository = $this->getRepository();
1305
1306
        $userService = $repository->getUserService();
1307
1308
        /* BEGIN: Use Case */
1309
        $this->createUserVersion1();
1310
1311
        // This call will fail with a "NotFoundException", because the given
1312
        // login/password combination does not exist.
1313
        $userService->loadUserByCredentials('üser', 'secret');
1314
        /* END: Use Case */
1315
    }
1316
1317
    /**
1318
     * Test for the loadUserByCredentials() method.
1319
     *
1320
     * @see \eZ\Publish\API\Repository\UserService::loadUserByCredentials()
1321
     * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
1322
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserByCredentials
1323
     */
1324
    public function testLoadUserByCredentialsThrowsInvalidArgumentValueForEmptyLogin()
1325
    {
1326
        $repository = $this->getRepository();
1327
1328
        $userService = $repository->getUserService();
1329
1330
        /* BEGIN: Use Case */
1331
        $this->createUserVersion1();
1332
1333
        // This call will fail with a "InvalidArgumentValue", because the given
1334
        // login is empty.
1335
        $userService->loadUserByCredentials('', 'secret');
1336
        /* END: Use Case */
1337
    }
1338
1339
    /**
1340
     * Test for the loadUserByLogin() method.
1341
     *
1342
     * @see \eZ\Publish\API\Repository\UserService::loadUserByLogin()
1343
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1344
     */
1345 View Code Duplication
    public function testLoadUserByLogin()
1346
    {
1347
        $repository = $this->getRepository();
1348
1349
        $userService = $repository->getUserService();
1350
1351
        /* BEGIN: Use Case */
1352
        $user = $this->createUserVersion1('User');
1353
1354
        // Load the newly created user
1355
        $userReloaded = $userService->loadUserByLogin('User');
1356
        /* END: Use Case */
1357
1358
        $this->assertPropertiesCorrect(
1359
            array(
1360
                'login' => $user->login,
1361
                'email' => $user->email,
1362
                'passwordHash' => $user->passwordHash,
1363
                'hashAlgorithm' => $user->hashAlgorithm,
1364
                'enabled' => $user->enabled,
1365
                'maxLogin' => $user->maxLogin,
1366
                'id' => $user->id,
1367
                'contentInfo' => $user->contentInfo,
1368
                'versionInfo' => $user->versionInfo,
1369
                'fields' => $user->fields,
1370
            ),
1371
            $userReloaded
1372
        );
1373
    }
1374
1375
    /**
1376
     * Test for the loadUserByLogin() method.
1377
     *
1378
     * @see \eZ\Publish\API\Repository\UserService::loadUserByLogin()
1379
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1380
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserByLogin
1381
     */
1382
    public function testLoadUserByLoginThrowsNotFoundExceptionForUnknownLogin()
1383
    {
1384
        $repository = $this->getRepository();
1385
1386
        $userService = $repository->getUserService();
1387
1388
        /* BEGIN: Use Case */
1389
        $this->createUserVersion1();
1390
1391
        // This call will fail with a "NotFoundException", because the given
1392
        // login/password combination does not exist.
1393
        $userService->loadUserByLogin('user42');
1394
        /* END: Use Case */
1395
    }
1396
1397
    /**
1398
     * Test for the loadUserByLogin() method.
1399
     *
1400
     * @see \eZ\Publish\API\Repository\UserService::loadUserByLogin()
1401
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserByLogin
1402
     */
1403 View Code Duplication
    public function testLoadUserByLoginWorksForLoginWithWrongCase()
1404
    {
1405
        $repository = $this->getRepository();
1406
1407
        $userService = $repository->getUserService();
1408
1409
        /* BEGIN: Use Case */
1410
        $user = $this->createUserVersion1();
1411
1412
        // Lookup by user login should ignore casing
1413
        $userReloaded = $userService->loadUserByLogin('USER');
1414
        /* END: Use Case */
1415
1416
        $this->assertPropertiesCorrect(
1417
            array(
1418
                'login' => $user->login,
1419
                'email' => $user->email,
1420
                'passwordHash' => $user->passwordHash,
1421
                'hashAlgorithm' => $user->hashAlgorithm,
1422
                'enabled' => $user->enabled,
1423
                'maxLogin' => $user->maxLogin,
1424
                'id' => $user->id,
1425
                'contentInfo' => $user->contentInfo,
1426
                'versionInfo' => $user->versionInfo,
1427
                'fields' => $user->fields,
1428
            ),
1429
            $userReloaded
1430
        );
1431
    }
1432
1433
    /**
1434
     * Test for the loadUsersByEmail() method.
1435
     *
1436
     * @see \eZ\Publish\API\Repository\UserService::loadUsersByEmail()
1437
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1438
     */
1439 View Code Duplication
    public function testLoadUserByEmail()
1440
    {
1441
        $repository = $this->getRepository();
1442
1443
        $userService = $repository->getUserService();
1444
1445
        /* BEGIN: Use Case */
1446
        $user = $this->createUserVersion1();
1447
1448
        // Load the newly created user
1449
        $usersReloaded = $userService->loadUsersByEmail('[email protected]');
1450
        /* END: Use Case */
1451
1452
        $this->assertEquals(array($user), $usersReloaded);
1453
    }
1454
1455
    /**
1456
     * Test for the loadUsersByEmail() method.
1457
     *
1458
     * @see \eZ\Publish\API\Repository\UserService::loadUsersByEmail()
1459
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserByEmail
1460
     */
1461 View Code Duplication
    public function testLoadUserByEmailReturnsEmptyInUnknownEmail()
1462
    {
1463
        $repository = $this->getRepository();
1464
1465
        $userService = $repository->getUserService();
1466
1467
        /* BEGIN: Use Case */
1468
        $this->createUserVersion1();
1469
1470
        // This call will return empty array, because the given
1471
        // login/password combination does not exist.
1472
        $emptyUserList = $userService->loadUsersByEmail('[email protected]');
1473
        /* END: Use Case */
1474
1475
        $this->assertEquals(array(), $emptyUserList);
1476
    }
1477
1478
    /**
1479
     * Test for the deleteUser() method.
1480
     *
1481
     * @see \eZ\Publish\API\Repository\UserService::deleteUser()
1482
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1483
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1484
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUser
1485
     */
1486 View Code Duplication
    public function testDeleteUser()
1487
    {
1488
        $repository = $this->getRepository();
1489
1490
        $userService = $repository->getUserService();
1491
1492
        /* BEGIN: Use Case */
1493
        $user = $this->createUserVersion1();
1494
1495
        // Delete the currently created user
1496
        $userService->deleteUser($user);
1497
        /* END: Use Case */
1498
1499
        // We use the NotFoundException here to verify that the user not exists
1500
        $userService->loadUser($user->id);
1501
    }
1502
1503
    /**
1504
     * Test for the newUserUpdateStruct() method.
1505
     *
1506
     * @see \eZ\Publish\API\Repository\UserService::newUserUpdateStruct()
1507
     */
1508
    public function testNewUserUpdateStruct()
1509
    {
1510
        $repository = $this->getRepository();
1511
1512
        /* BEGIN: Use Case */
1513
        $userService = $repository->getUserService();
1514
1515
        // Create a new update struct instance
1516
        $userUpdate = $userService->newUserUpdateStruct();
1517
        /* END: Use Case */
1518
1519
        $this->assertInstanceOf(
1520
            UserUpdateStruct::class,
1521
            $userUpdate
1522
        );
1523
1524
        $this->assertNull($userUpdate->contentUpdateStruct);
1525
        $this->assertNull($userUpdate->contentMetadataUpdateStruct);
1526
1527
        $this->assertPropertiesCorrect(
1528
            [
1529
                'email' => null,
1530
                'password' => null,
1531
                'enabled' => null,
1532
                'maxLogin' => null,
1533
            ],
1534
            $userUpdate
1535
        );
1536
    }
1537
1538
    /**
1539
     * Test for the updateUser() method.
1540
     *
1541
     * @return \eZ\Publish\API\Repository\Values\User\User
1542
     *
1543
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1544
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1545
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserUpdateStruct
1546
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1547
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1548
     */
1549
    public function testUpdateUser()
1550
    {
1551
        $repository = $this->getRepository();
1552
1553
        $userService = $repository->getUserService();
1554
1555
        /* BEGIN: Use Case */
1556
        $user = $this->createUserVersion1();
1557
1558
        // Create a new update struct instance
1559
        $userUpdate = $userService->newUserUpdateStruct();
1560
1561
        // Set new values for password and maxLogin
1562
        $userUpdate->password = 'my-new-password';
1563
        $userUpdate->maxLogin = 42;
1564
        $userUpdate->enabled = false;
1565
1566
        // Updated the user record.
1567
        $userVersion2 = $userService->updateUser($user, $userUpdate);
1568
        /* END: Use Case */
1569
1570
        $this->assertInstanceOf(
1571
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\User',
1572
            $user
1573
        );
1574
1575
        return $userVersion2;
1576
    }
1577
1578
    /**
1579
     * Test for the updateUser() method.
1580
     *
1581
     * @return \eZ\Publish\API\Repository\Values\User\User
1582
     *
1583
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1584
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1585
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserUpdateStruct
1586
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1587
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1588
     */
1589
    public function testUpdateUserNoPassword()
1590
    {
1591
        $repository = $this->getRepository();
1592
        $signalSlotUserService = $repository->getUserService();
1593
1594
        $signalSlotUserServiceReflection = new ReflectionClass($signalSlotUserService);
1595
        $userServiceProperty = $signalSlotUserServiceReflection->getProperty('service');
1596
        $userServiceProperty->setAccessible(true);
1597
        $userService = $userServiceProperty->getValue($signalSlotUserService);
1598
1599
        $userServiceReflection = new ReflectionClass($userService);
1600
        $settingsProperty = $userServiceReflection->getProperty('settings');
1601
        $settingsProperty->setAccessible(true);
1602
        $settingsProperty->setValue(
1603
            $userService,
1604
            [
1605
                'hashType' => User::PASSWORD_HASH_MD5_USER,
1606
            ] + $settingsProperty->getValue($userService)
1607
        );
1608
1609
        /* BEGIN: Use Case */
1610
        $user = $this->createUserVersion1();
1611
1612
        $settingsProperty->setValue(
1613
            $userService,
1614
            [
1615
                'hashType' => User::PASSWORD_HASH_PHP_DEFAULT,
1616
            ] + $settingsProperty->getValue($userService)
1617
        );
1618
1619
        // Create a new update struct instance
1620
        $userUpdate = $userService->newUserUpdateStruct();
1621
1622
        // Set new values for maxLogin, don't change password
1623
        $userUpdate->maxLogin = 43;
1624
        $userUpdate->enabled = false;
1625
1626
        // Updated the user record.
1627
        $userVersion2 = $userService->updateUser($user, $userUpdate);
1628
        /* END: Use Case */
1629
1630
        $this->assertInstanceOf(
1631
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\User',
1632
            $user
1633
        );
1634
1635
        $this->assertEquals(
1636
            array(
1637
                'login' => $user->login,
1638
                'email' => $user->email,
1639
                'passwordHash' => $user->passwordHash,
1640
                'hashAlgorithm' => $user->hashAlgorithm,
1641
                'maxLogin' => 43,
1642
                'enabled' => false,
1643
            ),
1644
            array(
1645
                'login' => $userVersion2->login,
1646
                'email' => $userVersion2->email,
1647
                'passwordHash' => $userVersion2->passwordHash,
1648
                'hashAlgorithm' => $userVersion2->hashAlgorithm,
1649
                'maxLogin' => $userVersion2->maxLogin,
1650
                'enabled' => $userVersion2->enabled,
1651
            )
1652
        );
1653
    }
1654
1655
    /**
1656
     * Test for the updateUser() method.
1657
     *
1658
     * @param \eZ\Publish\API\Repository\Values\User\User $user
1659
     *
1660
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1661
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUser
1662
     */
1663 View Code Duplication
    public function testUpdateUserUpdatesExpectedProperties(User $user)
1664
    {
1665
        $this->assertEquals(
1666
            array(
1667
                'login' => 'user',
1668
                'email' => '[email protected]',
1669
                'maxLogin' => 42,
1670
                'enabled' => false,
1671
            ),
1672
            array(
1673
                'login' => $user->login,
0 ignored issues
show
Documentation introduced by
The property $login is declared protected in eZ\Publish\API\Repository\Values\User\User. 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...
1674
                'email' => $user->email,
0 ignored issues
show
Documentation introduced by
The property $email is declared protected in eZ\Publish\API\Repository\Values\User\User. 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...
1675
                'maxLogin' => $user->maxLogin,
0 ignored issues
show
Documentation introduced by
The property $maxLogin is declared protected in eZ\Publish\API\Repository\Values\User\User. 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...
1676
                'enabled' => $user->enabled,
0 ignored issues
show
Documentation introduced by
The property $enabled is declared protected in eZ\Publish\API\Repository\Values\User\User. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

Loading history...
1677
            )
1678
        );
1679
    }
1680
1681
    /**
1682
     * Test for the updateUser() method.
1683
     *
1684
     * @param \eZ\Publish\API\Repository\Values\User\User $user
1685
     *
1686
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1687
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUser
1688
     */
1689
    public function testUpdateUserReturnsPublishedVersion($user)
1690
    {
1691
        $this->assertEquals(
1692
            APIVersionInfo::STATUS_PUBLISHED,
1693
            $user->getVersionInfo()->status
1694
        );
1695
    }
1696
1697
    /**
1698
     * Test for the updateUser() method.
1699
     *
1700
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1701
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUser
1702
     */
1703
    public function testUpdateUserWithContentMetadataUpdateStruct()
1704
    {
1705
        $repository = $this->getRepository();
1706
1707
        $userService = $repository->getUserService();
1708
1709
        /* BEGIN: Use Case */
1710
        $user = $this->createUserVersion1();
1711
1712
        // Get the ContentService implementation
1713
        $contentService = $repository->getContentService();
1714
1715
        // Create a metadata update struct and change the remote id.
1716
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1717
        $metadataUpdate->remoteId = '85e10037d1ac0a00aa75443ced483e08';
1718
1719
        // Create a new update struct instance
1720
        $userUpdate = $userService->newUserUpdateStruct();
1721
1722
        // Set the metadata update struct.
1723
        $userUpdate->contentMetadataUpdateStruct = $metadataUpdate;
1724
1725
        // Updated the user record.
1726
        $userVersion2 = $userService->updateUser($user, $userUpdate);
1727
1728
        // The contentInfo->remoteId will be changed now.
1729
        $remoteId = $userVersion2->contentInfo->remoteId;
1730
        /* END: Use Case */
1731
1732
        $this->assertEquals('85e10037d1ac0a00aa75443ced483e08', $remoteId);
1733
    }
1734
1735
    /**
1736
     * Test for the updateUser() method.
1737
     *
1738
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1739
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUser
1740
     */
1741
    public function testUpdateUserWithContentUpdateStruct()
1742
    {
1743
        $repository = $this->getRepository();
1744
1745
        $userService = $repository->getUserService();
1746
1747
        /* BEGIN: Use Case */
1748
        $user = $this->createUserVersion1();
1749
1750
        // Get the ContentService implementation
1751
        $contentService = $repository->getContentService();
1752
1753
        // Create a content update struct and change the remote id.
1754
        $contentUpdate = $contentService->newContentUpdateStruct();
1755
        $contentUpdate->setField('first_name', 'Hello', 'eng-US');
1756
        $contentUpdate->setField('last_name', 'World', 'eng-US');
1757
1758
        // Create a new update struct instance
1759
        $userUpdate = $userService->newUserUpdateStruct();
1760
1761
        // Set the content update struct.
1762
        $userUpdate->contentUpdateStruct = $contentUpdate;
1763
1764
        // Updated the user record.
1765
        $userVersion2 = $userService->updateUser($user, $userUpdate);
1766
1767
        $name = sprintf(
1768
            '%s %s',
1769
            $userVersion2->getFieldValue('first_name'),
1770
            $userVersion2->getFieldValue('last_name')
1771
        );
1772
        /* END: Use Case */
1773
1774
        $this->assertEquals('Hello World', $name);
1775
    }
1776
1777
    /**
1778
     * Test for the updateUser() method.
1779
     *
1780
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1781
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1782
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUser
1783
     */
1784 View Code Duplication
    public function testUpdateUserWhenMissingField()
1785
    {
1786
        $repository = $this->getRepository();
1787
1788
        $userService = $repository->getUserService();
1789
1790
        /* BEGIN: Use Case */
1791
        $user = $this->createUserVersion1();
1792
1793
        // Get the ContentService implementation
1794
        $contentService = $repository->getContentService();
1795
1796
        // Create a content update struct and change the remote id.
1797
        $contentUpdate = $contentService->newContentUpdateStruct();
1798
        $contentUpdate->setField('first_name', null, 'eng-US');
1799
1800
        // Create a new update struct instance
1801
        $userUpdate = $userService->newUserUpdateStruct();
1802
1803
        // Set the content update struct.
1804
        $userUpdate->contentUpdateStruct = $contentUpdate;
1805
1806
        // This call will fail with a "ContentFieldValidationException" because the
1807
        // mandatory field "first_name" is set to an empty value.
1808
        $userService->updateUser($user, $userUpdate);
1809
1810
        /* END: Use Case */
1811
    }
1812
1813
    /**
1814
     * Test for the updateUser() method.
1815
     *
1816
     * @see \eZ\Publish\API\Repository\UserService::updateUser()
1817
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1818
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUpdateUser
1819
     */
1820 View Code Duplication
    public function testUpdateUserThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
1821
    {
1822
        $repository = $this->getRepository();
1823
1824
        $userService = $repository->getUserService();
1825
1826
        /* BEGIN: Use Case */
1827
        $user = $this->createUserVersion1();
1828
1829
        // Get the ContentService implementation
1830
        $contentService = $repository->getContentService();
1831
1832
        $contentUpdate = $contentService->newContentUpdateStruct();
1833
        // An object of stdClass is not valid for the field first_name
1834
        $contentUpdate->setField('first_name', new \stdClass(), 'eng-US');
1835
1836
        // Create a new update struct instance
1837
        $userUpdate = $userService->newUserUpdateStruct();
1838
1839
        // Set the content update struct.
1840
        $userUpdate->contentUpdateStruct = $contentUpdate;
1841
1842
        // This call will fail with a "InvalidArgumentException" because the
1843
        // the field "first_name" does not accept the given value.
1844
        $userService->updateUser($user, $userUpdate);
1845
1846
        /* END: Use Case */
1847
    }
1848
1849
    /**
1850
     * Test for the loadUserGroupsOfUser() method.
1851
     *
1852
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroupsOfUser
1853
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1854
     */
1855
    public function testLoadUserGroupsOfUser()
1856
    {
1857
        $repository = $this->getRepository();
1858
1859
        $userService = $repository->getUserService();
1860
1861
        /* BEGIN: Use Case */
1862
        $user = $this->createUserVersion1();
1863
1864
        // This array will contain the "Editors" user group name
1865
        $userGroupNames = [];
1866
        foreach ($userService->loadUserGroupsOfUser($user) as $userGroup) {
1867
            $this->assertInstanceOf(UserGroup::class, $userGroup);
1868
            $userGroupNames[] = $userGroup->getFieldValue('name');
1869
        }
1870
        /* END: Use Case */
1871
1872
        $this->assertEquals(['Editors'], $userGroupNames);
1873
    }
1874
1875
    /**
1876
     * Test for the loadUsersOfUserGroup() method.
1877
     *
1878
     * @covers \eZ\Publish\API\Repository\UserService::loadUsersOfUserGroup
1879
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
1880
     */
1881
    public function testLoadUsersOfUserGroup()
1882
    {
1883
        $repository = $this->getRepository();
1884
        $userService = $repository->getUserService();
1885
1886
        $group = $userService->loadUserGroup($this->generateId('group', 13));
1887
1888
        /* BEGIN: Use Case */
1889
        $this->createUserVersion1();
1890
1891
        $this->refreshSearch($repository);
1892
1893
        // This array will contain the email of the newly created "Editor" user
1894
        $email = array();
1895
        foreach ($userService->loadUsersOfUserGroup($group) as $user) {
1896
            $this->assertInstanceOf(ApiUser::class, $user);
1897
            $email[] = $user->email;
1898
        }
1899
        /* END: Use Case */
1900
        $this->assertEquals(array('[email protected]'), $email);
1901
    }
1902
1903
    /**
1904
     * Test for the assignUserToUserGroup() method.
1905
     *
1906
     * @see \eZ\Publish\API\Repository\UserService::assignUserToUserGroup()
1907
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroupsOfUser
1908
     */
1909
    public function testAssignUserToUserGroup()
1910
    {
1911
        $repository = $this->getRepository();
1912
        $userService = $repository->getUserService();
1913
1914
        $administratorGroupId = $this->generateId('group', 12);
1915
        /* BEGIN: Use Case */
1916
        // $administratorGroupId is the ID of the "Administrator" group in an
1917
        // eZ Publish demo installation
1918
1919
        $user = $this->createUserVersion1();
1920
1921
        // Assign group to newly created user
1922
        $userService->assignUserToUserGroup(
1923
            $user,
1924
            $userService->loadUserGroup($administratorGroupId)
1925
        );
1926
1927
        // This array will contain "Editors" and "Administrator users"
1928
        $userGroupNames = array();
1929
        foreach ($userService->loadUserGroupsOfUser($user) as $userGroup) {
1930
            $userGroupNames[] = $userGroup->getFieldValue('name');
1931
        }
1932
        /* END: Use Case */
1933
1934
        sort($userGroupNames, SORT_STRING);
1935
1936
        $this->assertEquals(
1937
            array(
1938
                'Administrator users',
1939
                'Editors',
1940
            ),
1941
            $userGroupNames
1942
        );
1943
    }
1944
1945
    /**
1946
     * Test for the assignUserToUserGroup() method.
1947
     *
1948
     * @covers \eZ\Publish\API\Repository\UserService::assignUserToUserGroup
1949
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1950
     * @expectedExceptionMessage Argument 'user' is invalid: user is already in the given user group
1951
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testAssignUserToUserGroup
1952
     */
1953 View Code Duplication
    public function testAssignUserToUserGroupThrowsInvalidArgumentException()
1954
    {
1955
        $repository = $this->getRepository();
1956
        $userService = $repository->getUserService();
1957
1958
        $editorsGroupId = $this->generateId('group', 13);
1959
        /* BEGIN: Use Case */
1960
        $user = $this->createUserVersion1();
1961
        // $editorsGroupId is the ID of the "Editors" group in an
1962
        // eZ Publish demo installation
1963
1964
        // This call will fail with an "InvalidArgumentException", because the
1965
        // user is already assigned to the "Editors" group
1966
        $userService->assignUserToUserGroup(
1967
            $user,
1968
            $userService->loadUserGroup($editorsGroupId)
1969
        );
1970
        /* END: Use Case */
1971
    }
1972
1973
    /**
1974
     * Test for the unAssignUssrFromUserGroup() method.
1975
     *
1976
     * @see \eZ\Publish\API\Repository\UserService::unAssignUssrFromUserGroup()
1977
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroupsOfUser
1978
     */
1979
    public function testUnAssignUserFromUserGroup()
1980
    {
1981
        $repository = $this->getRepository();
1982
        $userService = $repository->getUserService();
1983
1984
        $editorsGroupId = $this->generateId('group', 13);
1985
        $anonymousGroupId = $this->generateId('group', 42);
1986
1987
        /* BEGIN: Use Case */
1988
        // $anonymousGroupId is the ID of the "Anonymous Users" group in an eZ
1989
        // Publish demo installation
1990
1991
        $user = $this->createUserVersion1();
1992
1993
        // Assign group to newly created user
1994
        $userService->assignUserToUserGroup(
1995
            $user,
1996
            $userService->loadUserGroup($anonymousGroupId)
1997
        );
1998
1999
        // Unassign user from "Editors" group
2000
        $userService->unAssignUserFromUserGroup(
2001
            $user,
2002
            $userService->loadUserGroup($editorsGroupId)
2003
        );
2004
2005
        // This array will contain "Anonymous Users"
2006
        $userGroupNames = array();
2007
        foreach ($userService->loadUserGroupsOfUser($user) as $userGroup) {
2008
            $userGroupNames[] = $userGroup->getFieldValue('name');
2009
        }
2010
        /* END: Use Case */
2011
2012
        $this->assertEquals(array('Anonymous Users'), $userGroupNames);
2013
    }
2014
2015
    /**
2016
     * Test for the unAssignUserFromUserGroup() method.
2017
     *
2018
     * @see \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup()
2019
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2020
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUnAssignUserFromUserGroup
2021
     */
2022
    public function testUnAssignUserFromUserGroupThrowsInvalidArgumentException()
2023
    {
2024
        $repository = $this->getRepository();
2025
        $userService = $repository->getUserService();
2026
2027
        $administratorGroupId = $this->generateId('group', 12);
2028
        /* BEGIN: Use Case */
2029
        $user = $this->createUserVersion1();
2030
        // $administratorGroupId is the ID of the "Administrator" group in an
2031
        // eZ Publish demo installation
2032
2033
        // This call will fail with an "InvalidArgumentException", because the
2034
        // user is not assigned to the "Administrator" group
2035
        $userService->unAssignUserFromUserGroup(
2036
            $user,
2037
            $userService->loadUserGroup($administratorGroupId)
2038
        );
2039
        /* END: Use Case */
2040
    }
2041
2042
    /**
2043
     * Test for the unAssignUserFromUserGroup() method removing user from the last group.
2044
     *
2045
     * @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup
2046
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2047
     * @expectedExceptionMessage Argument 'user' has a bad state: user only has one user group, cannot unassign from last group
2048
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUnAssignUserFromUserGroup
2049
     */
2050
    public function testUnAssignUserFromUserGroupThrowsBadStateArgumentException()
2051
    {
2052
        $repository = $this->getRepository();
2053
        $userService = $repository->getUserService();
2054
2055
        $editorsGroupId = $this->generateId('group', 13);
2056
        /* BEGIN: Use Case */
2057
        $user = $this->createUserVersion1();
2058
2059
        // This call will fail with an "BadStateException", because the
2060
        // user has to be assigned to at least one group
2061
        $userService->unAssignUserFromUserGroup(
2062
            $user,
2063
            $userService->loadUserGroup($editorsGroupId)
2064
        );
2065
        /* END: Use Case */
2066
    }
2067
2068
    /**
2069
     * Test that multi-language logic for the loadUserGroup method respects prioritized language list.
2070
     *
2071
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroup
2072
     * @dataProvider getPrioritizedLanguageList
2073
     * @param string[] $prioritizedLanguages
2074
     * @param string|null $expectedLanguageCode language code of expected translation
2075
     */
2076
    public function testLoadUserGroupWithPrioritizedLanguagesList(
2077
        array $prioritizedLanguages,
2078
        $expectedLanguageCode
2079
    ) {
2080
        $repository = $this->getRepository();
2081
        $userService = $repository->getUserService();
2082
2083
        $userGroup = $this->createMultiLanguageUserGroup();
2084
        if ($expectedLanguageCode === null) {
2085
            $expectedLanguageCode = $userGroup->contentInfo->mainLanguageCode;
2086
        }
2087
2088
        $loadedUserGroup = $userService->loadUserGroup($userGroup->id, $prioritizedLanguages);
2089
2090
        self::assertEquals(
2091
            $loadedUserGroup->getName($expectedLanguageCode),
2092
            $loadedUserGroup->getName()
2093
        );
2094
        self::assertEquals(
2095
            $loadedUserGroup->getFieldValue('description', $expectedLanguageCode),
2096
            $loadedUserGroup->getFieldValue('description')
2097
        );
2098
    }
2099
2100
    /**
2101
     * Test that multi-language logic works correctly after updating user group main language.
2102
     *
2103
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroup
2104
     * @dataProvider getPrioritizedLanguageList
2105
     * @param string[] $prioritizedLanguages
2106
     * @param string|null $expectedLanguageCode language code of expected translation
2107
     */
2108
    public function testLoadUserGroupWithPrioritizedLanguagesListAfterMainLanguageUpdate(
2109
        array $prioritizedLanguages,
2110
        $expectedLanguageCode
2111
    ) {
2112
        $repository = $this->getRepository();
2113
        $userService = $repository->getUserService();
2114
        $contentService = $repository->getContentService();
2115
2116
        $userGroup = $this->createMultiLanguageUserGroup();
2117
2118
        $userGroupUpdateStruct = $userService->newUserGroupUpdateStruct();
2119
        $userGroupUpdateStruct->contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2120
        $userGroupUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
2121
        $userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
2122
2123
        if ($expectedLanguageCode === null) {
2124
            $expectedLanguageCode = 'eng-GB';
2125
        }
2126
2127
        $loadedUserGroup = $userService->loadUserGroup($userGroup->id, $prioritizedLanguages);
2128
2129
        self::assertEquals(
2130
            $loadedUserGroup->getName($expectedLanguageCode),
2131
            $loadedUserGroup->getName()
2132
        );
2133
        self::assertEquals(
2134
            $loadedUserGroup->getFieldValue('description', $expectedLanguageCode),
2135
            $loadedUserGroup->getFieldValue('description')
2136
        );
2137
    }
2138
2139
    /**
2140
     * Test that multi-language logic for the loadSubUserGroups method respects prioritized language list.
2141
     *
2142
     * @covers \eZ\Publish\API\Repository\UserService::loadSubUserGroups
2143
     * @dataProvider getPrioritizedLanguageList
2144
     * @param string[] $prioritizedLanguages
2145
     * @param string|null $expectedLanguageCode language code of expected translation
2146
     */
2147
    public function testLoadSubUserGroupsWithPrioritizedLanguagesList(
2148
        array $prioritizedLanguages,
2149
        $expectedLanguageCode
2150
    ) {
2151
        $repository = $this->getRepository();
2152
        $userService = $repository->getUserService();
2153
2154
        // create main group for subgroups
2155
        $userGroup = $this->createMultiLanguageUserGroup(4);
2156
        if ($expectedLanguageCode === null) {
2157
            $expectedLanguageCode = $userGroup->contentInfo->mainLanguageCode;
2158
        }
2159
2160
        // create subgroups
2161
        $this->createMultiLanguageUserGroup($userGroup->id);
2162
        $this->createMultiLanguageUserGroup($userGroup->id);
2163
2164
        $userGroup = $userService->loadUserGroup($userGroup->id, $prioritizedLanguages);
2165
2166
        $subUserGroups = $userService->loadSubUserGroups($userGroup, 0, 2, $prioritizedLanguages);
2167
        foreach ($subUserGroups as $subUserGroup) {
2168
            self::assertEquals(
2169
                $subUserGroup->getName($expectedLanguageCode),
2170
                $subUserGroup->getName()
2171
            );
2172
            self::assertEquals(
2173
                $subUserGroup->getFieldValue('description', $expectedLanguageCode),
2174
                $subUserGroup->getFieldValue('description')
2175
            );
2176
        }
2177
    }
2178
2179
    /**
2180
     * Test that multi-language logic for the loadUser method respects prioritized language list.
2181
     *
2182
     * @covers \eZ\Publish\API\Repository\UserService::loadUser
2183
     * @dataProvider getPrioritizedLanguageList
2184
     * @param string[] $prioritizedLanguages
2185
     * @param string|null $expectedLanguageCode language code of expected translation
2186
     */
2187 View Code Duplication
    public function testLoadUserWithPrioritizedLanguagesList(
2188
        array $prioritizedLanguages,
2189
        $expectedLanguageCode
2190
    ) {
2191
        $repository = $this->getRepository();
2192
        $userService = $repository->getUserService();
2193
2194
        $user = $this->createMultiLanguageUser();
2195
        if ($expectedLanguageCode === null) {
2196
            $expectedLanguageCode = $user->contentInfo->mainLanguageCode;
2197
        }
2198
2199
        $loadedUser = $userService->loadUser($user->id, $prioritizedLanguages);
2200
2201
        self::assertEquals(
2202
            $loadedUser->getName($expectedLanguageCode),
2203
            $loadedUser->getName()
2204
        );
2205
2206
        foreach (['fist_name', 'last_name', 'signature'] as $fieldIdentifier) {
2207
            self::assertEquals(
2208
                $loadedUser->getFieldValue($fieldIdentifier, $expectedLanguageCode),
2209
                $loadedUser->getFieldValue($fieldIdentifier)
2210
            );
2211
        }
2212
    }
2213
2214
    /**
2215
     * Test that multi-language logic for the loadUser method works correctly after updating
2216
     * user content main language.
2217
     *
2218
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroup
2219
     * @dataProvider getPrioritizedLanguageList
2220
     * @param string[] $prioritizedLanguages
2221
     * @param string|null $expectedLanguageCode language code of expected translation
2222
     */
2223
    public function testLoadUserWithPrioritizedLanguagesListAfterMainLanguageUpdate(
2224
        array $prioritizedLanguages,
2225
        $expectedLanguageCode
2226
    ) {
2227
        $repository = $this->getRepository();
2228
        $userService = $repository->getUserService();
2229
        $contentService = $repository->getContentService();
2230
2231
        $user = $this->createMultiLanguageUser();
2232
        // sanity check
2233
        self::assertEquals($user->contentInfo->mainLanguageCode, 'eng-US');
2234
2235
        $userUpdateStruct = $userService->newUserUpdateStruct();
2236
        $userUpdateStruct->contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2237
        $userUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
2238
        $userService->updateUser($user, $userUpdateStruct);
2239
        if ($expectedLanguageCode === null) {
2240
            $expectedLanguageCode = 'eng-GB';
2241
        }
2242
2243
        $loadedUser = $userService->loadUser($user->id, $prioritizedLanguages);
2244
2245
        self::assertEquals(
2246
            $loadedUser->getName($expectedLanguageCode),
2247
            $loadedUser->getName()
2248
        );
2249
2250
        foreach (['fist_name', 'last_name', 'signature'] as $fieldIdentifier) {
2251
            self::assertEquals(
2252
                $loadedUser->getFieldValue($fieldIdentifier, $expectedLanguageCode),
2253
                $loadedUser->getFieldValue($fieldIdentifier)
2254
            );
2255
        }
2256
    }
2257
2258
    /**
2259
     * Test that multi-language logic for the loadUserByLogin method respects prioritized language list.
2260
     *
2261
     * @covers \eZ\Publish\API\Repository\UserService::loadUserByLogin
2262
     * @dataProvider getPrioritizedLanguageList
2263
     * @param string[] $prioritizedLanguages
2264
     * @param string|null $expectedLanguageCode language code of expected translation
2265
     */
2266 View Code Duplication
    public function testLoadUserByLoginWithPrioritizedLanguagesList(
2267
        array $prioritizedLanguages,
2268
        $expectedLanguageCode
2269
    ) {
2270
        $repository = $this->getRepository();
2271
        $userService = $repository->getUserService();
2272
        $user = $this->createMultiLanguageUser();
2273
2274
        // load, with prioritized languages, the newly created user
2275
        $loadedUser = $userService->loadUserByLogin($user->login, $prioritizedLanguages);
2276
        if ($expectedLanguageCode === null) {
2277
            $expectedLanguageCode = $loadedUser->contentInfo->mainLanguageCode;
2278
        }
2279
2280
        self::assertEquals(
2281
            $loadedUser->getName($expectedLanguageCode),
2282
            $loadedUser->getName()
2283
        );
2284
2285
        foreach (['first_name', 'last_name', 'signature'] as $fieldIdentifier) {
2286
            self::assertEquals(
2287
                $loadedUser->getFieldValue($fieldIdentifier, $expectedLanguageCode),
2288
                $loadedUser->getFieldValue($fieldIdentifier)
2289
            );
2290
        }
2291
    }
2292
2293
    /**
2294
     * Test that multi-language logic for the loadUserByCredentials method respects
2295
     * prioritized language list.
2296
     *
2297
     * @covers \eZ\Publish\API\Repository\UserService::loadUserByCredentials
2298
     * @dataProvider getPrioritizedLanguageList
2299
     * @param string[] $prioritizedLanguages
2300
     * @param string|null $expectedLanguageCode language code of expected translation
2301
     */
2302 View Code Duplication
    public function testLoadUserByCredentialsWithPrioritizedLanguagesList(
2303
        array $prioritizedLanguages,
2304
        $expectedLanguageCode
2305
    ) {
2306
        $repository = $this->getRepository();
2307
        $userService = $repository->getUserService();
2308
        $user = $this->createMultiLanguageUser();
2309
2310
        // load, with prioritized languages, the newly created user
2311
        $loadedUser = $userService->loadUserByCredentials(
2312
            $user->login,
2313
            'secret',
2314
            $prioritizedLanguages
2315
        );
2316
        if ($expectedLanguageCode === null) {
2317
            $expectedLanguageCode = $loadedUser->contentInfo->mainLanguageCode;
2318
        }
2319
2320
        self::assertEquals(
2321
            $loadedUser->getName($expectedLanguageCode),
2322
            $loadedUser->getName()
2323
        );
2324
2325
        foreach (['first_name', 'last_name', 'signature'] as $fieldIdentifier) {
2326
            self::assertEquals(
2327
                $loadedUser->getFieldValue($fieldIdentifier, $expectedLanguageCode),
2328
                $loadedUser->getFieldValue($fieldIdentifier)
2329
            );
2330
        }
2331
    }
2332
2333
    /**
2334
     * Test that multi-language logic for the loadUsersByEmail method respects
2335
     * prioritized language list.
2336
     *
2337
     * @covers \eZ\Publish\API\Repository\UserService::loadUsersByEmail
2338
     * @dataProvider getPrioritizedLanguageList
2339
     * @param string[] $prioritizedLanguages
2340
     * @param string|null $expectedLanguageCode language code of expected translation
2341
     */
2342 View Code Duplication
    public function testLoadUsersByEmailWithPrioritizedLanguagesList(
2343
        array $prioritizedLanguages,
2344
        $expectedLanguageCode
2345
    ) {
2346
        $repository = $this->getRepository();
2347
        $userService = $repository->getUserService();
2348
        $user = $this->createMultiLanguageUser();
2349
2350
        // load, with prioritized languages, users by email
2351
        $loadedUsers = $userService->loadUsersByEmail($user->email, $prioritizedLanguages);
2352
2353
        foreach ($loadedUsers as $loadedUser) {
2354
            if ($expectedLanguageCode === null) {
2355
                $expectedLanguageCode = $loadedUser->contentInfo->mainLanguageCode;
2356
            }
2357
            self::assertEquals(
2358
                $loadedUser->getName($expectedLanguageCode),
2359
                $loadedUser->getName()
2360
            );
2361
2362
            foreach (['first_name', 'last_name', 'signature'] as $fieldIdentifier) {
2363
                self::assertEquals(
2364
                    $loadedUser->getFieldValue($fieldIdentifier, $expectedLanguageCode),
2365
                    $loadedUser->getFieldValue($fieldIdentifier)
2366
                );
2367
            }
2368
        }
2369
    }
2370
2371
    /**
2372
     * Test that multi-language logic for the loadUserGroupsOfUser method respects
2373
     * prioritized language list.
2374
     *
2375
     * @covers \eZ\Publish\API\Repository\UserService::loadUserGroupsOfUser
2376
     * @dataProvider getPrioritizedLanguageList
2377
     * @param string[] $prioritizedLanguages
2378
     * @param string|null $expectedLanguageCode language code of expected translation
2379
     */
2380
    public function testLoadUserGroupsOfUserWithPrioritizedLanguagesList(
2381
        array $prioritizedLanguages,
2382
        $expectedLanguageCode
2383
    ) {
2384
        $repository = $this->getRepository();
2385
        $userService = $repository->getUserService();
2386
        $userGroup = $this->createMultiLanguageUserGroup();
2387
        $user = $this->createMultiLanguageUser($userGroup->id);
2388
2389
        $userGroups = $userService->loadUserGroupsOfUser($user, 0, 25, $prioritizedLanguages);
2390
        foreach ($userGroups as $userGroup) {
2391
            self::assertEquals(
2392
                $userGroup->getName($expectedLanguageCode),
2393
                $userGroup->getName()
2394
            );
2395
            self::assertEquals(
2396
                $userGroup->getFieldValue('description', $expectedLanguageCode),
2397
                $userGroup->getFieldValue('description')
2398
            );
2399
        }
2400
    }
2401
2402
    /**
2403
     * Test that multi-language logic for the loadUsersOfUserGroup method respects
2404
     * prioritized language list.
2405
     *
2406
     * @covers \eZ\Publish\API\Repository\UserService::loadUsersOfUserGroup
2407
     * @dataProvider getPrioritizedLanguageList
2408
     * @param string[] $prioritizedLanguages
2409
     * @param string|null $expectedLanguageCode language code of expected translation
2410
     */
2411
    public function testLoadUsersOfUserGroupWithPrioritizedLanguagesList(
2412
        array $prioritizedLanguages,
2413
        $expectedLanguageCode
2414
    ) {
2415
        $repository = $this->getRepository();
2416
        $userService = $repository->getUserService();
2417
2418
        // create parent user group
2419
        $userGroup = $this->createMultiLanguageUserGroup();
2420
        // add two users to the created parent user group
2421
        $this->createMultiLanguageUser($userGroup->id);
2422
        $this->createMultiLanguageUser($userGroup->id);
2423
2424
        // test loading of users via user group with prioritized languages list
2425
        $users = $userService->loadUsersOfUserGroup($userGroup, 0, 25, $prioritizedLanguages);
2426
        foreach ($users as $user) {
2427
            if ($expectedLanguageCode === null) {
2428
                $expectedLanguageCode = $user->contentInfo->mainLanguageCode;
2429
            }
2430
            self::assertEquals(
2431
                $user->getName($expectedLanguageCode),
2432
                $user->getName()
2433
            );
2434
2435
            foreach (['first_name', 'last_name', 'signature'] as $fieldIdentifier) {
2436
                self::assertEquals(
2437
                    $user->getFieldValue($fieldIdentifier, $expectedLanguageCode),
2438
                    $user->getFieldValue($fieldIdentifier)
2439
                );
2440
            }
2441
        }
2442
    }
2443
2444
    /**
2445
     * Get prioritized languages list data.
2446
     *
2447
     * Test cases using this data provider should expect the following arguments:
2448
     * <code>
2449
     *   array $prioritizedLanguagesList
2450
     *   string $expectedLanguage (if null - use main language)
2451
     * </code>
2452
     *
2453
     * @return array
2454
     */
2455 View Code Duplication
    public function getPrioritizedLanguageList()
2456
    {
2457
        return [
2458
            [[], null],
2459
            [['eng-US'], 'eng-US'],
2460
            [['eng-GB'], 'eng-GB'],
2461
            [['eng-US', 'eng-GB'], 'eng-US'],
2462
            [['eng-GB', 'eng-US'], 'eng-GB'],
2463
            // use non-existent group as the first one
2464
            [['ger-DE'], null],
2465
            [['ger-DE', 'eng-GB'], 'eng-GB'],
2466
        ];
2467
    }
2468
2469
    /**
2470
     * @param int $parentGroupId
2471
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
2472
     */
2473
    private function createMultiLanguageUserGroup($parentGroupId = 4)
2474
    {
2475
        $repository = $this->getRepository();
2476
        $userService = $repository->getUserService();
2477
2478
        // create user group with multiple translations
2479
        $parentGroupId = $this->generateId('group', $parentGroupId);
2480
        $parentGroup = $userService->loadUserGroup($parentGroupId);
2481
2482
        $userGroupCreateStruct = $userService->newUserGroupCreateStruct('eng-US');
2483
        $userGroupCreateStruct->setField('name', 'US user group', 'eng-US');
2484
        $userGroupCreateStruct->setField('name', 'GB user group', 'eng-GB');
2485
        $userGroupCreateStruct->setField('description', 'US user group description', 'eng-US');
2486
        $userGroupCreateStruct->setField('description', 'GB user group description', 'eng-GB');
2487
        $userGroupCreateStruct->alwaysAvailable = true;
2488
2489
        return $userService->createUserGroup($userGroupCreateStruct, $parentGroup);
2490
    }
2491
2492
    /**
2493
     * Create a user group fixture in a variable named <b>$userGroup</b>,.
2494
     *
2495
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
2496
     */
2497
    private function createUserGroupVersion1()
2498
    {
2499
        $repository = $this->getRepository();
2500
2501
        $mainGroupId = $this->generateId('group', 4);
2502
        /* BEGIN: Inline */
2503
        // $mainGroupId is the ID of the main "Users" group
2504
2505
        $userService = $repository->getUserService();
2506
2507
        // Load main group
2508
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
2509
2510
        // Instantiate a new create struct
2511
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
2512
        $userGroupCreate->setField('name', 'Example Group');
2513
2514
        // Create the new user group
2515
        $userGroup = $userService->createUserGroup(
2516
            $userGroupCreate,
2517
            $parentUserGroup
2518
        );
2519
        /* END: Inline */
2520
2521
        return $userGroup;
2522
    }
2523
2524
    /**
2525
     * Create user with multiple translations of User Content fields.
2526
     *
2527
     * @param int $userGroupId User group ID (default 13 - Editors)
2528
     *
2529
     * @return \eZ\Publish\API\Repository\Values\User\User
2530
     */
2531
    private function createMultiLanguageUser($userGroupId = 13)
2532
    {
2533
        $repository = $this->getRepository();
2534
        $userService = $repository->getUserService();
2535
2536
        // Instantiate a create struct with mandatory properties
2537
        $randomLogin = md5(rand() . time());
2538
        $userCreateStruct = $userService->newUserCreateStruct(
2539
            $randomLogin,
2540
            "{$randomLogin}@example.com",
2541
            'secret',
2542
            'eng-US'
2543
        );
2544
        $userCreateStruct->enabled = true;
2545
        $userCreateStruct->alwaysAvailable = true;
2546
2547
        // set field for each language
2548
        foreach (['eng-US', 'eng-GB'] as $languageCode) {
2549
            $userCreateStruct->setField('first_name', "{$languageCode} Example", $languageCode);
2550
            $userCreateStruct->setField('last_name', "{$languageCode} User", $languageCode);
2551
            $userCreateStruct->setField('signature', "{$languageCode} signature", $languageCode);
2552
        }
2553
2554
        // Load parent group for the user
2555
        $group = $userService->loadUserGroup($userGroupId);
2556
2557
        // Create a new user
2558
        return $userService->createUser($userCreateStruct, [$group]);
2559
    }
2560
}
2561