Code Duplication    Length = 24-34 lines in 6 locations

eZ/Publish/API/Repository/Tests/UserServiceAuthorizationTest.php 2 locations

@@ 77-100 (lines=24) @@
74
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
75
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
76
     */
77
    public function testCreateUserGroupThrowsUnauthorizedException()
78
    {
79
        $repository = $this->getRepository();
80
        $userService = $repository->getUserService();
81
82
        $editorsGroupId = $this->generateId('group', 13);
83
84
        /* BEGIN: Use Case */
85
        $user = $this->createUserVersion1();
86
87
        // Load the parent group
88
        $parentUserGroup = $userService->loadUserGroup($editorsGroupId);
89
90
        // Now set the currently created "Editor" as current user
91
        $repository->setCurrentUser($user);
92
93
        // Instantiate a new group create struct
94
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-GB');
95
        $userGroupCreate->setField('name', 'Example Group');
96
97
        // This call will fail with an "UnauthorizedException"
98
        $userService->createUserGroup($userGroupCreate, $parentUserGroup);
99
        /* END: Use Case */
100
    }
101
102
    /**
103
     * Test for the deleteUserGroup() method.
@@ 321-350 (lines=30) @@
318
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
319
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUnAssignUserFromUserGroup
320
     */
321
    public function testUnAssignUserFromUserGroupThrowsUnauthorizedException()
322
    {
323
        $repository = $this->getRepository();
324
        $userService = $repository->getUserService();
325
326
        $editorsGroupId = $this->generateId('group', 13);
327
        $memberGroupId = $this->generateId('group', 11);
328
329
        /* BEGIN: Use Case */
330
        // $memberGroupId is the ID of the "Members" group in an eZ Publish
331
        // demo installation
332
333
        $user = $this->createUserVersion1();
334
335
        // Assign group to newly created user
336
        $userService->assignUserToUserGroup(
337
            $user,
338
            $userService->loadUserGroup($memberGroupId)
339
        );
340
341
        // Now set the currently created "Editor" as current user
342
        $repository->setCurrentUser($user);
343
344
        // This call will fail with an "UnauthorizedException"
345
        $userService->unAssignUserFromUserGroup(
346
            $user,
347
            $userService->loadUserGroup($editorsGroupId)
348
        );
349
        /* END: Use Case */
350
    }
351
352
    /**
353
     * Test for the loadUserGroupsOfUser() method.

eZ/Publish/API/Repository/Tests/RoleServiceTest.php 2 locations

@@ 2426-2459 (lines=34) @@
2423
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2424
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2425
     */
2426
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2427
    {
2428
        $repository = $this->getRepository();
2429
2430
        $mainGroupId = $this->generateId('group', 4);
2431
        /* BEGIN: Use Case */
2432
        // $mainGroupId is the ID of the main "Users" group
2433
2434
        $userService = $repository->getUserService();
2435
        $roleService = $repository->getRoleService();
2436
2437
        $userGroup = $userService->loadUserGroup($mainGroupId);
2438
2439
        // Load the existing "Anonymous" role
2440
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2441
2442
        // Assign the "Anonymous" role to the newly created user group
2443
        try {
2444
            $roleService->assignRoleToUserGroup(
2445
                $role,
2446
                $userGroup
2447
            );
2448
        } catch (Exception $e) {
2449
            $this->fail('Got exception at first valid attempt to assign role');
2450
        }
2451
2452
        // Re-Assign the "Anonymous" role to the newly created user group
2453
        // This call will fail with an InvalidArgumentException, because role is already assigned
2454
        $roleService->assignRoleToUserGroup(
2455
            $role,
2456
            $userGroup
2457
        );
2458
        /* END: Use Case */
2459
    }
2460
2461
    /**
2462
     * Test for the assignRoleToUserGroup() method.
@@ 2892-2918 (lines=27) @@
2889
     *
2890
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
2891
     */
2892
    private function createUserGroupVersion1()
2893
    {
2894
        $repository = $this->getRepository();
2895
2896
        $mainGroupId = $this->generateId('group', 4);
2897
        /* BEGIN: Inline */
2898
        // $mainGroupId is the ID of the main "Users" group
2899
2900
        $roleService = $repository->getRoleService();
2901
        $userService = $repository->getUserService();
2902
2903
        // Load main group
2904
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
2905
2906
        // Instantiate a new create struct
2907
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
2908
        $userGroupCreate->setField('name', 'Example Group');
2909
2910
        // Create the new user group
2911
        $userGroup = $userService->createUserGroup(
2912
            $userGroupCreate,
2913
            $parentUserGroup
2914
        );
2915
        /* END: Inline */
2916
2917
        return $userGroup;
2918
    }
2919
}
2920

eZ/Publish/API/Repository/Tests/UserServiceTest.php 2 locations

@@ 485-510 (lines=26) @@
482
     * @see \eZ\Publish\API\Repository\UserService::moveUserGroup()
483
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testMoveUserGroup
484
     */
485
    public function testMoveUserGroupIncrementsSubGroupCountOnNewParent()
486
    {
487
        $repository = $this->getRepository();
488
        $userService = $repository->getUserService();
489
490
        $membersGroupId = $this->generateId('group', 13);
491
        /* BEGIN: Use Case */
492
        // $membersGroupId is the ID of the "Members" user group in an eZ
493
        // Publish demo installation
494
495
        $userGroup = $this->createUserGroupVersion1();
496
497
        // Load the new parent group
498
        $membersUserGroup = $userService->loadUserGroup($membersGroupId);
499
500
        // Move user group from "Users" to "Members"
501
        $userService->moveUserGroup($userGroup, $membersUserGroup);
502
503
        $this->refreshSearch($repository);
504
505
        // Reload the user group to get an updated $subGroupCount
506
        $membersUserGroupUpdated = $userService->loadUserGroup($membersGroupId);
507
        /* END: Use Case */
508
509
        $this->assertEquals(1, $membersUserGroupUpdated->subGroupCount);
510
    }
511
512
    /**
513
     * Test for the moveUserGroup() method.
@@ 518-542 (lines=25) @@
515
     * @see \eZ\Publish\API\Repository\UserService::moveUserGroup()
516
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testMoveUserGroup
517
     */
518
    public function testMoveUserGroupDecrementsSubGroupCountOnOldParent()
519
    {
520
        $repository = $this->getRepository();
521
        $userService = $repository->getUserService();
522
523
        $membersGroupId = $this->generateId('group', 13);
524
        /* BEGIN: Use Case */
525
        // $membersGroupId is the ID of the "Members" user group in an eZ
526
        // Publish demo installation
527
528
        $userGroup = $this->createUserGroupVersion1();
529
530
        // Load the new parent group
531
        $membersUserGroup = $userService->loadUserGroup($membersGroupId);
532
533
        // Move user group from "Users" to "Members"
534
        $userService->moveUserGroup($userGroup, $membersUserGroup);
535
        /* END: Use Case */
536
537
        $this->refreshSearch($repository);
538
539
        $mainUserGroup = $userService->loadUserGroup($this->generateId('group', 4));
540
541
        $this->assertEquals(5, $mainUserGroup->subGroupCount);
542
    }
543
544
    /**
545
     * Test for the newUserGroupUpdateStruct() method.