Code Duplication    Length = 27-36 lines in 3 locations

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

@@ 80-106 (lines=27) @@
77
     * @see \eZ\Publish\API\Repository\UserService::createUserGroup()
78
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUserGroup
79
     */
80
    public function testCreateUserGroupThrowsUnauthorizedException()
81
    {
82
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
83
84
        $repository = $this->getRepository();
85
        $userService = $repository->getUserService();
86
        $permissionResolver = $repository->getPermissionResolver();
87
88
        $editorsGroupId = $this->generateId('group', 13);
89
90
        /* BEGIN: Use Case */
91
        $user = $this->createUserVersion1();
92
93
        // Load the parent group
94
        $parentUserGroup = $userService->loadUserGroup($editorsGroupId);
95
96
        // Now set the currently created "Editor" as current user
97
        $permissionResolver->setCurrentUserReference($user);
98
99
        // Instantiate a new group create struct
100
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-GB');
101
        $userGroupCreate->setField('name', 'Example Group');
102
103
        // This call will fail with an "UnauthorizedException"
104
        $userService->createUserGroup($userGroupCreate, $parentUserGroup);
105
        /* END: Use Case */
106
    }
107
108
    /**
109
     * Test for the deleteUserGroup() method.
@@ 340-372 (lines=33) @@
337
     * @see \eZ\Publish\API\Repository\UserService::unAssignUssrFromUserGroup()
338
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testUnAssignUserFromUserGroup
339
     */
340
    public function testUnAssignUserFromUserGroupThrowsUnauthorizedException()
341
    {
342
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
343
344
        $repository = $this->getRepository();
345
        $userService = $repository->getUserService();
346
        $permissionResolver = $repository->getPermissionResolver();
347
348
        $editorsGroupId = $this->generateId('group', 13);
349
        $memberGroupId = $this->generateId('group', 11);
350
351
        /* BEGIN: Use Case */
352
        // $memberGroupId is the ID of the "Members" group in an eZ Publish
353
        // demo installation
354
355
        $user = $this->createUserVersion1();
356
357
        // Assign group to newly created user
358
        $userService->assignUserToUserGroup(
359
            $user,
360
            $userService->loadUserGroup($memberGroupId)
361
        );
362
363
        // Now set the currently created "Editor" as current user
364
        $permissionResolver->setCurrentUserReference($user);
365
366
        // This call will fail with an "UnauthorizedException"
367
        $userService->unAssignUserFromUserGroup(
368
            $user,
369
            $userService->loadUserGroup($editorsGroupId)
370
        );
371
        /* END: Use Case */
372
    }
373
374
    /**
375
     * Test for the loadUserGroupsOfUser() method.

eZ/Publish/API/Repository/Tests/RoleServiceTest.php 1 location

@@ 2495-2530 (lines=36) @@
2492
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2493
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2494
     */
2495
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2496
    {
2497
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2498
2499
        $repository = $this->getRepository();
2500
2501
        $mainGroupId = $this->generateId('group', 4);
2502
        /* BEGIN: Use Case */
2503
        // $mainGroupId is the ID of the main "Users" group
2504
2505
        $userService = $repository->getUserService();
2506
        $roleService = $repository->getRoleService();
2507
2508
        $userGroup = $userService->loadUserGroup($mainGroupId);
2509
2510
        // Load the existing "Anonymous" role
2511
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2512
2513
        // Assign the "Anonymous" role to the newly created user group
2514
        try {
2515
            $roleService->assignRoleToUserGroup(
2516
                $role,
2517
                $userGroup
2518
            );
2519
        } catch (Exception $e) {
2520
            $this->fail('Got exception at first valid attempt to assign role');
2521
        }
2522
2523
        // Re-Assign the "Anonymous" role to the newly created user group
2524
        // This call will fail with an InvalidArgumentException, because role is already assigned
2525
        $roleService->assignRoleToUserGroup(
2526
            $role,
2527
            $userGroup
2528
        );
2529
        /* END: Use Case */
2530
    }
2531
2532
    /**
2533
     * Test for the assignRoleToUserGroup() method.