Code Duplication    Length = 94-98 lines in 2 locations

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

@@ 1961-2058 (lines=98) @@
1958
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1959
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1960
     */
1961
    public function testAssignRoleToUserWithRoleLimitation()
1962
    {
1963
        $repository = $this->getRepository();
1964
        $roleService = $repository->getRoleService();
1965
1966
        /* BEGIN: Use Case */
1967
        $user = $this->createUserVersion1();
1968
1969
        // Load the existing "Anonymous" role
1970
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1971
1972
        // Assign the "Anonymous" role to the newly created user
1973
        $roleService->assignRoleToUser(
1974
            $role,
1975
            $user,
1976
            new SubtreeLimitation(
1977
                array(
1978
                    'limitationValues' => array('/1/43/'),
1979
                )
1980
            )
1981
        );
1982
1983
        // The assignments array will contain the new role<->user assignment
1984
        $roleAssignments = $roleService->getRoleAssignments($role);
1985
        /* END: Use Case */
1986
1987
        // Members + Partners + Anonymous + Example User
1988
        $this->assertEquals(4, count($roleAssignments));
1989
1990
        // Get the role limitation
1991
        $roleLimitation = null;
1992
        foreach ($roleAssignments as $roleAssignment) {
1993
            $roleLimitation = $roleAssignment->getRoleLimitation();
1994
            if ($roleLimitation) {
1995
                $this->assertInstanceOf(
1996
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1997
                    $roleAssignment
1998
                );
1999
                break;
2000
            }
2001
        }
2002
2003
        $this->assertEquals(
2004
            new SubtreeLimitation(
2005
                array(
2006
                    'limitationValues' => array('/1/43/'),
2007
                )
2008
            ),
2009
            $roleLimitation
2010
        );
2011
2012
        // Test again to see values being merged
2013
        $roleService->assignRoleToUser(
2014
            $role,
2015
            $user,
2016
            new SubtreeLimitation(
2017
                array(
2018
                    'limitationValues' => array('/1/43/', '/1/2/'),
2019
                )
2020
            )
2021
        );
2022
2023
        // The assignments array will contain the new role<->user assignment
2024
        $roleAssignments = $roleService->getRoleAssignments($role);
2025
2026
        // Members + Partners + Anonymous + Example User
2027
        $this->assertEquals(5, count($roleAssignments));
2028
2029
        // Get the role limitation
2030
        $roleLimitations = [];
2031
        foreach ($roleAssignments as $roleAssignment) {
2032
            $roleLimitation = $roleAssignment->getRoleLimitation();
2033
            if ($roleLimitation) {
2034
                $this->assertInstanceOf(
2035
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2036
                    $roleAssignment
2037
                );
2038
                $roleLimitations[] = $roleLimitation;
2039
            }
2040
        }
2041
        array_multisort($roleLimitations);
2042
2043
        $this->assertEquals(
2044
            [
2045
                new SubtreeLimitation(
2046
                    array(
2047
                        'limitationValues' => array('/1/2/'),
2048
                    )
2049
                ),
2050
                new SubtreeLimitation(
2051
                    array(
2052
                        'limitationValues' => array('/1/43/'),
2053
                    )
2054
                ),
2055
            ],
2056
            $roleLimitations
2057
        );
2058
    }
2059
2060
    /**
2061
     * Test for the assignRoleToUser() method.
@@ 2376-2469 (lines=94) @@
2373
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2374
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2375
     */
2376
    public function testAssignRoleToUserGroupWithRoleLimitation()
2377
    {
2378
        $repository = $this->getRepository();
2379
        $roleService = $repository->getRoleService();
2380
2381
        /* BEGIN: Use Case */
2382
        $userGroup = $this->createUserGroupVersion1();
2383
2384
        // Load the existing "Anonymous" role
2385
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2386
2387
        // Assign the "Anonymous" role to the newly created user group
2388
        $roleService->assignRoleToUserGroup(
2389
            $role,
2390
            $userGroup,
2391
            new SubtreeLimitation(
2392
                array(
2393
                    'limitationValues' => array('/1/43/'),
2394
                )
2395
            )
2396
        );
2397
2398
        // The assignments array will contain the new role<->group assignment
2399
        $roleAssignments = $roleService->getRoleAssignments($role);
2400
        /* END: Use Case */
2401
2402
        // Members + Partners + Anonymous + Example Group
2403
        $this->assertEquals(4, count($roleAssignments));
2404
2405
        // Get the role limitation
2406
        $roleLimitation = null;
2407
        foreach ($roleAssignments as $roleAssignment) {
2408
            $roleLimitation = $roleAssignment->getRoleLimitation();
2409
            if ($roleLimitation) {
2410
                break;
2411
            }
2412
        }
2413
2414
        $this->assertEquals(
2415
            new SubtreeLimitation(
2416
                array(
2417
                    'limitationValues' => array('/1/43/'),
2418
                )
2419
            ),
2420
            $roleLimitation
2421
        );
2422
2423
        // Test again to see values being merged
2424
        $roleService->assignRoleToUserGroup(
2425
            $role,
2426
            $userGroup,
2427
            new SubtreeLimitation(
2428
                array(
2429
                    'limitationValues' => array('/1/43/', '/1/2/'),
2430
                )
2431
            )
2432
        );
2433
2434
        // The assignments array will contain the new role<->user assignment
2435
        $roleAssignments = $roleService->getRoleAssignments($role);
2436
2437
        // Members + Partners + Anonymous + Example User
2438
        $this->assertEquals(5, count($roleAssignments));
2439
2440
        // Get the role limitation
2441
        $roleLimitations = [];
2442
        foreach ($roleAssignments as $roleAssignment) {
2443
            $roleLimitation = $roleAssignment->getRoleLimitation();
2444
            if ($roleLimitation) {
2445
                $this->assertInstanceOf(
2446
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2447
                    $roleAssignment
2448
                );
2449
                $roleLimitations[] = $roleLimitation;
2450
            }
2451
        }
2452
        array_multisort($roleLimitations);
2453
2454
        $this->assertEquals(
2455
            [
2456
                new SubtreeLimitation(
2457
                    array(
2458
                        'limitationValues' => array('/1/2/'),
2459
                    )
2460
                ),
2461
                new SubtreeLimitation(
2462
                    array(
2463
                        'limitationValues' => array('/1/43/'),
2464
                    )
2465
                ),
2466
            ],
2467
            $roleLimitations
2468
        );
2469
    }
2470
2471
    /**
2472
     * Test for the assignRoleToUserGroup() method.