Code Duplication    Length = 94-98 lines in 2 locations

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

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