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

@@ 2311-2346 (lines=36) @@
2308
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2309
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2310
     */
2311
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2312
    {
2313
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2314
2315
        $repository = $this->getRepository();
2316
2317
        $mainGroupId = $this->generateId('group', 4);
2318
        /* BEGIN: Use Case */
2319
        // $mainGroupId is the ID of the main "Users" group
2320
2321
        $userService = $repository->getUserService();
2322
        $roleService = $repository->getRoleService();
2323
2324
        $userGroup = $userService->loadUserGroup($mainGroupId);
2325
2326
        // Load the existing "Anonymous" role
2327
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2328
2329
        // Assign the "Anonymous" role to the newly created user group
2330
        try {
2331
            $roleService->assignRoleToUserGroup(
2332
                $role,
2333
                $userGroup
2334
            );
2335
        } catch (Exception $e) {
2336
            $this->fail('Got exception at first valid attempt to assign role');
2337
        }
2338
2339
        // Re-Assign the "Anonymous" role to the newly created user group
2340
        // This call will fail with an InvalidArgumentException, because role is already assigned
2341
        $roleService->assignRoleToUserGroup(
2342
            $role,
2343
            $userGroup
2344
        );
2345
        /* END: Use Case */
2346
    }
2347
2348
    /**
2349
     * Test for the assignRoleToUserGroup() method.