Code Duplication    Length = 94-98 lines in 2 locations

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

@@ 1936-2033 (lines=98) @@
1933
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1934
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1935
     */
1936
    public function testAssignRoleToUserWithRoleLimitation()
1937
    {
1938
        $repository = $this->getRepository();
1939
        $roleService = $repository->getRoleService();
1940
1941
        /* BEGIN: Use Case */
1942
        $user = $this->createUserVersion1();
1943
1944
        // Load the existing "Anonymous" role
1945
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1946
1947
        // Assign the "Anonymous" role to the newly created user
1948
        $roleService->assignRoleToUser(
1949
            $role,
1950
            $user,
1951
            new SubtreeLimitation(
1952
                array(
1953
                    'limitationValues' => array('/1/43/'),
1954
                )
1955
            )
1956
        );
1957
1958
        // The assignments array will contain the new role<->user assignment
1959
        $roleAssignments = $roleService->getRoleAssignments($role);
1960
        /* END: Use Case */
1961
1962
        // Members + Partners + Anonymous + Example User
1963
        $this->assertEquals(4, count($roleAssignments));
1964
1965
        // Get the role limitation
1966
        $roleLimitation = null;
1967
        foreach ($roleAssignments as $roleAssignment) {
1968
            $roleLimitation = $roleAssignment->getRoleLimitation();
1969
            if ($roleLimitation) {
1970
                $this->assertInstanceOf(
1971
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1972
                    $roleAssignment
1973
                );
1974
                break;
1975
            }
1976
        }
1977
1978
        $this->assertEquals(
1979
            new SubtreeLimitation(
1980
                array(
1981
                    'limitationValues' => array('/1/43/'),
1982
                )
1983
            ),
1984
            $roleLimitation
1985
        );
1986
1987
        // Test again to see values being merged
1988
        $roleService->assignRoleToUser(
1989
            $role,
1990
            $user,
1991
            new SubtreeLimitation(
1992
                array(
1993
                    'limitationValues' => array('/1/43/', '/1/2/'),
1994
                )
1995
            )
1996
        );
1997
1998
        // The assignments array will contain the new role<->user assignment
1999
        $roleAssignments = $roleService->getRoleAssignments($role);
2000
2001
        // Members + Partners + Anonymous + Example User
2002
        $this->assertEquals(5, count($roleAssignments));
2003
2004
        // Get the role limitation
2005
        $roleLimitations = [];
2006
        foreach ($roleAssignments as $roleAssignment) {
2007
            $roleLimitation = $roleAssignment->getRoleLimitation();
2008
            if ($roleLimitation) {
2009
                $this->assertInstanceOf(
2010
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2011
                    $roleAssignment
2012
                );
2013
                $roleLimitations[] = $roleLimitation;
2014
            }
2015
        }
2016
        array_multisort($roleLimitations);
2017
2018
        $this->assertEquals(
2019
            [
2020
                new SubtreeLimitation(
2021
                    array(
2022
                        'limitationValues' => array('/1/2/'),
2023
                    )
2024
                ),
2025
                new SubtreeLimitation(
2026
                    array(
2027
                        'limitationValues' => array('/1/43/'),
2028
                    )
2029
                ),
2030
            ],
2031
            $roleLimitations
2032
        );
2033
    }
2034
2035
    /**
2036
     * Test for the assignRoleToUser() method.
@@ 2351-2444 (lines=94) @@
2348
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2349
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2350
     */
2351
    public function testAssignRoleToUserGroupWithRoleLimitation()
2352
    {
2353
        $repository = $this->getRepository();
2354
        $roleService = $repository->getRoleService();
2355
2356
        /* BEGIN: Use Case */
2357
        $userGroup = $this->createUserGroupVersion1();
2358
2359
        // Load the existing "Anonymous" role
2360
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2361
2362
        // Assign the "Anonymous" role to the newly created user group
2363
        $roleService->assignRoleToUserGroup(
2364
            $role,
2365
            $userGroup,
2366
            new SubtreeLimitation(
2367
                array(
2368
                    'limitationValues' => array('/1/43/'),
2369
                )
2370
            )
2371
        );
2372
2373
        // The assignments array will contain the new role<->group assignment
2374
        $roleAssignments = $roleService->getRoleAssignments($role);
2375
        /* END: Use Case */
2376
2377
        // Members + Partners + Anonymous + Example Group
2378
        $this->assertEquals(4, count($roleAssignments));
2379
2380
        // Get the role limitation
2381
        $roleLimitation = null;
2382
        foreach ($roleAssignments as $roleAssignment) {
2383
            $roleLimitation = $roleAssignment->getRoleLimitation();
2384
            if ($roleLimitation) {
2385
                break;
2386
            }
2387
        }
2388
2389
        $this->assertEquals(
2390
            new SubtreeLimitation(
2391
                array(
2392
                    'limitationValues' => array('/1/43/'),
2393
                )
2394
            ),
2395
            $roleLimitation
2396
        );
2397
2398
        // Test again to see values being merged
2399
        $roleService->assignRoleToUserGroup(
2400
            $role,
2401
            $userGroup,
2402
            new SubtreeLimitation(
2403
                array(
2404
                    'limitationValues' => array('/1/43/', '/1/2/'),
2405
                )
2406
            )
2407
        );
2408
2409
        // The assignments array will contain the new role<->user assignment
2410
        $roleAssignments = $roleService->getRoleAssignments($role);
2411
2412
        // Members + Partners + Anonymous + Example User
2413
        $this->assertEquals(5, count($roleAssignments));
2414
2415
        // Get the role limitation
2416
        $roleLimitations = [];
2417
        foreach ($roleAssignments as $roleAssignment) {
2418
            $roleLimitation = $roleAssignment->getRoleLimitation();
2419
            if ($roleLimitation) {
2420
                $this->assertInstanceOf(
2421
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2422
                    $roleAssignment
2423
                );
2424
                $roleLimitations[] = $roleLimitation;
2425
            }
2426
        }
2427
        array_multisort($roleLimitations);
2428
2429
        $this->assertEquals(
2430
            [
2431
                new SubtreeLimitation(
2432
                    array(
2433
                        'limitationValues' => array('/1/2/'),
2434
                    )
2435
                ),
2436
                new SubtreeLimitation(
2437
                    array(
2438
                        'limitationValues' => array('/1/43/'),
2439
                    )
2440
                ),
2441
            ],
2442
            $roleLimitations
2443
        );
2444
    }
2445
2446
    /**
2447
     * Test for the assignRoleToUserGroup() method.