Code Duplication    Length = 94-98 lines in 2 locations

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

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