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

@@ 2448-2483 (lines=36) @@
2445
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2446
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2447
     */
2448
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2449
    {
2450
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2451
2452
        $repository = $this->getRepository();
2453
2454
        $mainGroupId = $this->generateId('group', 4);
2455
        /* BEGIN: Use Case */
2456
        // $mainGroupId is the ID of the main "Users" group
2457
2458
        $userService = $repository->getUserService();
2459
        $roleService = $repository->getRoleService();
2460
2461
        $userGroup = $userService->loadUserGroup($mainGroupId);
2462
2463
        // Load the existing "Anonymous" role
2464
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2465
2466
        // Assign the "Anonymous" role to the newly created user group
2467
        try {
2468
            $roleService->assignRoleToUserGroup(
2469
                $role,
2470
                $userGroup
2471
            );
2472
        } catch (Exception $e) {
2473
            $this->fail('Got exception at first valid attempt to assign role');
2474
        }
2475
2476
        // Re-Assign the "Anonymous" role to the newly created user group
2477
        // This call will fail with an InvalidArgumentException, because role is already assigned
2478
        $roleService->assignRoleToUserGroup(
2479
            $role,
2480
            $userGroup
2481
        );
2482
        /* END: Use Case */
2483
    }
2484
2485
    /**
2486
     * Test for the assignRoleToUserGroup() method.