Code Duplication    Length = 94-98 lines in 2 locations

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

@@ 1968-2065 (lines=98) @@
1965
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1966
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1967
     */
1968
    public function testAssignRoleToUserWithRoleLimitation()
1969
    {
1970
        $repository = $this->getRepository();
1971
        $roleService = $repository->getRoleService();
1972
1973
        /* BEGIN: Use Case */
1974
        $user = $this->createUserVersion1();
1975
1976
        // Load the existing "Anonymous" role
1977
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1978
1979
        // Assign the "Anonymous" role to the newly created user
1980
        $roleService->assignRoleToUser(
1981
            $role,
1982
            $user,
1983
            new SubtreeLimitation(
1984
                [
1985
                    'limitationValues' => ['/1/43/'],
1986
                ]
1987
            )
1988
        );
1989
1990
        // The assignments array will contain the new role<->user assignment
1991
        $roleAssignments = $roleService->getRoleAssignments($role);
1992
        /* END: Use Case */
1993
1994
        // Members + Partners + Anonymous + Example User
1995
        $this->assertEquals(4, count($roleAssignments));
1996
1997
        // Get the role limitation
1998
        $roleLimitation = null;
1999
        foreach ($roleAssignments as $roleAssignment) {
2000
            $roleLimitation = $roleAssignment->getRoleLimitation();
2001
            if ($roleLimitation) {
2002
                $this->assertInstanceOf(
2003
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2004
                    $roleAssignment
2005
                );
2006
                break;
2007
            }
2008
        }
2009
2010
        $this->assertEquals(
2011
            new SubtreeLimitation(
2012
                [
2013
                    'limitationValues' => ['/1/43/'],
2014
                ]
2015
            ),
2016
            $roleLimitation
2017
        );
2018
2019
        // Test again to see values being merged
2020
        $roleService->assignRoleToUser(
2021
            $role,
2022
            $user,
2023
            new SubtreeLimitation(
2024
                [
2025
                    'limitationValues' => ['/1/43/', '/1/2/'],
2026
                ]
2027
            )
2028
        );
2029
2030
        // The assignments array will contain the new role<->user assignment
2031
        $roleAssignments = $roleService->getRoleAssignments($role);
2032
2033
        // Members + Partners + Anonymous + Example User
2034
        $this->assertEquals(5, count($roleAssignments));
2035
2036
        // Get the role limitation
2037
        $roleLimitations = [];
2038
        foreach ($roleAssignments as $roleAssignment) {
2039
            $roleLimitation = $roleAssignment->getRoleLimitation();
2040
            if ($roleLimitation) {
2041
                $this->assertInstanceOf(
2042
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2043
                    $roleAssignment
2044
                );
2045
                $roleLimitations[] = $roleLimitation;
2046
            }
2047
        }
2048
        array_multisort($roleLimitations);
2049
2050
        $this->assertEquals(
2051
            [
2052
                new SubtreeLimitation(
2053
                    [
2054
                        'limitationValues' => ['/1/2/'],
2055
                    ]
2056
                ),
2057
                new SubtreeLimitation(
2058
                    [
2059
                        'limitationValues' => ['/1/43/'],
2060
                    ]
2061
                ),
2062
            ],
2063
            $roleLimitations
2064
        );
2065
    }
2066
2067
    /**
2068
     * Test for the assignRoleToUser() method.
@@ 2418-2511 (lines=94) @@
2415
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2416
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2417
     */
2418
    public function testAssignRoleToUserGroupWithRoleLimitation()
2419
    {
2420
        $repository = $this->getRepository();
2421
        $roleService = $repository->getRoleService();
2422
2423
        /* BEGIN: Use Case */
2424
        $userGroup = $this->createUserGroupVersion1();
2425
2426
        // Load the existing "Anonymous" role
2427
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2428
2429
        // Assign the "Anonymous" role to the newly created user group
2430
        $roleService->assignRoleToUserGroup(
2431
            $role,
2432
            $userGroup,
2433
            new SubtreeLimitation(
2434
                [
2435
                    'limitationValues' => ['/1/43/'],
2436
                ]
2437
            )
2438
        );
2439
2440
        // The assignments array will contain the new role<->group assignment
2441
        $roleAssignments = $roleService->getRoleAssignments($role);
2442
        /* END: Use Case */
2443
2444
        // Members + Partners + Anonymous + Example Group
2445
        $this->assertEquals(4, count($roleAssignments));
2446
2447
        // Get the role limitation
2448
        $roleLimitation = null;
2449
        foreach ($roleAssignments as $roleAssignment) {
2450
            $roleLimitation = $roleAssignment->getRoleLimitation();
2451
            if ($roleLimitation) {
2452
                break;
2453
            }
2454
        }
2455
2456
        $this->assertEquals(
2457
            new SubtreeLimitation(
2458
                [
2459
                    'limitationValues' => ['/1/43/'],
2460
                ]
2461
            ),
2462
            $roleLimitation
2463
        );
2464
2465
        // Test again to see values being merged
2466
        $roleService->assignRoleToUserGroup(
2467
            $role,
2468
            $userGroup,
2469
            new SubtreeLimitation(
2470
                [
2471
                    'limitationValues' => ['/1/43/', '/1/2/'],
2472
                ]
2473
            )
2474
        );
2475
2476
        // The assignments array will contain the new role<->user assignment
2477
        $roleAssignments = $roleService->getRoleAssignments($role);
2478
2479
        // Members + Partners + Anonymous + Example User
2480
        $this->assertEquals(5, count($roleAssignments));
2481
2482
        // Get the role limitation
2483
        $roleLimitations = [];
2484
        foreach ($roleAssignments as $roleAssignment) {
2485
            $roleLimitation = $roleAssignment->getRoleLimitation();
2486
            if ($roleLimitation) {
2487
                $this->assertInstanceOf(
2488
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2489
                    $roleAssignment
2490
                );
2491
                $roleLimitations[] = $roleLimitation;
2492
            }
2493
        }
2494
        array_multisort($roleLimitations);
2495
2496
        $this->assertEquals(
2497
            [
2498
                new SubtreeLimitation(
2499
                    [
2500
                        'limitationValues' => ['/1/2/'],
2501
                    ]
2502
                ),
2503
                new SubtreeLimitation(
2504
                    [
2505
                        'limitationValues' => ['/1/43/'],
2506
                    ]
2507
                ),
2508
            ],
2509
            $roleLimitations
2510
        );
2511
    }
2512
2513
    /**
2514
     * Test for the assignRoleToUserGroup() method.