Code Duplication    Length = 94-98 lines in 2 locations

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

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