Code Duplication    Length = 94-98 lines in 2 locations

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

@@ 1907-2004 (lines=98) @@
1904
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1905
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1906
     */
1907
    public function testAssignRoleToUserWithRoleLimitation()
1908
    {
1909
        $repository = $this->getRepository();
1910
        $roleService = $repository->getRoleService();
1911
1912
        /* BEGIN: Use Case */
1913
        $user = $this->createUserVersion1();
1914
1915
        // Load the existing "Anonymous" role
1916
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1917
1918
        // Assign the "Anonymous" role to the newly created user
1919
        $roleService->assignRoleToUser(
1920
            $role,
1921
            $user,
1922
            new SubtreeLimitation(
1923
                [
1924
                    'limitationValues' => ['/1/43/'],
1925
                ]
1926
            )
1927
        );
1928
1929
        // The assignments array will contain the new role<->user assignment
1930
        $roleAssignments = $roleService->getRoleAssignments($role);
1931
        /* END: Use Case */
1932
1933
        // Members + Partners + Anonymous + Example User
1934
        $this->assertCount(4, $roleAssignments);
1935
1936
        // Get the role limitation
1937
        $roleLimitation = null;
1938
        foreach ($roleAssignments as $roleAssignment) {
1939
            $roleLimitation = $roleAssignment->getRoleLimitation();
1940
            if ($roleLimitation) {
1941
                $this->assertInstanceOf(
1942
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1943
                    $roleAssignment
1944
                );
1945
                break;
1946
            }
1947
        }
1948
1949
        $this->assertEquals(
1950
            new SubtreeLimitation(
1951
                [
1952
                    'limitationValues' => ['/1/43/'],
1953
                ]
1954
            ),
1955
            $roleLimitation
1956
        );
1957
1958
        // Test again to see values being merged
1959
        $roleService->assignRoleToUser(
1960
            $role,
1961
            $user,
1962
            new SubtreeLimitation(
1963
                [
1964
                    'limitationValues' => ['/1/43/', '/1/2/'],
1965
                ]
1966
            )
1967
        );
1968
1969
        // The assignments array will contain the new role<->user assignment
1970
        $roleAssignments = $roleService->getRoleAssignments($role);
1971
1972
        // Members + Partners + Anonymous + Example User
1973
        $this->assertCount(5, $roleAssignments);
1974
1975
        // Get the role limitation
1976
        $roleLimitations = [];
1977
        foreach ($roleAssignments as $roleAssignment) {
1978
            $roleLimitation = $roleAssignment->getRoleLimitation();
1979
            if ($roleLimitation) {
1980
                $this->assertInstanceOf(
1981
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1982
                    $roleAssignment
1983
                );
1984
                $roleLimitations[] = $roleLimitation;
1985
            }
1986
        }
1987
        array_multisort($roleLimitations);
1988
1989
        $this->assertEquals(
1990
            [
1991
                new SubtreeLimitation(
1992
                    [
1993
                        'limitationValues' => ['/1/2/'],
1994
                    ]
1995
                ),
1996
                new SubtreeLimitation(
1997
                    [
1998
                        'limitationValues' => ['/1/43/'],
1999
                    ]
2000
                ),
2001
            ],
2002
            $roleLimitations
2003
        );
2004
    }
2005
2006
    /**
2007
     * 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
                [
2368
                    'limitationValues' => ['/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->assertCount(4, $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
                [
2392
                    'limitationValues' => ['/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
                [
2404
                    'limitationValues' => ['/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->assertCount(5, $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
                    [
2433
                        'limitationValues' => ['/1/2/'],
2434
                    ]
2435
                ),
2436
                new SubtreeLimitation(
2437
                    [
2438
                        'limitationValues' => ['/1/43/'],
2439
                    ]
2440
                ),
2441
            ],
2442
            $roleLimitations
2443
        );
2444
    }
2445
2446
    /**
2447
     * Test for the assignRoleToUserGroup() method.