Code Duplication    Length = 94-98 lines in 2 locations

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

@@ 1859-1956 (lines=98) @@
1856
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1857
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1858
     */
1859
    public function testAssignRoleToUserWithRoleLimitation()
1860
    {
1861
        $repository = $this->getRepository();
1862
        $roleService = $repository->getRoleService();
1863
1864
        /* BEGIN: Use Case */
1865
        $user = $this->createUserVersion1();
1866
1867
        // Load the existing "Anonymous" role
1868
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1869
1870
        // Assign the "Anonymous" role to the newly created user
1871
        $roleService->assignRoleToUser(
1872
            $role,
1873
            $user,
1874
            new SubtreeLimitation(
1875
                [
1876
                    'limitationValues' => ['/1/43/'],
1877
                ]
1878
            )
1879
        );
1880
1881
        // The assignments array will contain the new role<->user assignment
1882
        $roleAssignments = $roleService->getRoleAssignments($role);
1883
        /* END: Use Case */
1884
1885
        // Members + Partners + Anonymous + Example User
1886
        $this->assertCount(4, $roleAssignments);
1887
1888
        // Get the role limitation
1889
        $roleLimitation = null;
1890
        foreach ($roleAssignments as $roleAssignment) {
1891
            $roleLimitation = $roleAssignment->getRoleLimitation();
1892
            if ($roleLimitation) {
1893
                $this->assertInstanceOf(
1894
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1895
                    $roleAssignment
1896
                );
1897
                break;
1898
            }
1899
        }
1900
1901
        $this->assertEquals(
1902
            new SubtreeLimitation(
1903
                [
1904
                    'limitationValues' => ['/1/43/'],
1905
                ]
1906
            ),
1907
            $roleLimitation
1908
        );
1909
1910
        // Test again to see values being merged
1911
        $roleService->assignRoleToUser(
1912
            $role,
1913
            $user,
1914
            new SubtreeLimitation(
1915
                [
1916
                    'limitationValues' => ['/1/43/', '/1/2/'],
1917
                ]
1918
            )
1919
        );
1920
1921
        // The assignments array will contain the new role<->user assignment
1922
        $roleAssignments = $roleService->getRoleAssignments($role);
1923
1924
        // Members + Partners + Anonymous + Example User
1925
        $this->assertCount(5, $roleAssignments);
1926
1927
        // Get the role limitation
1928
        $roleLimitations = [];
1929
        foreach ($roleAssignments as $roleAssignment) {
1930
            $roleLimitation = $roleAssignment->getRoleLimitation();
1931
            if ($roleLimitation) {
1932
                $this->assertInstanceOf(
1933
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1934
                    $roleAssignment
1935
                );
1936
                $roleLimitations[] = $roleLimitation;
1937
            }
1938
        }
1939
        array_multisort($roleLimitations);
1940
1941
        $this->assertEquals(
1942
            [
1943
                new SubtreeLimitation(
1944
                    [
1945
                        'limitationValues' => ['/1/2/'],
1946
                    ]
1947
                ),
1948
                new SubtreeLimitation(
1949
                    [
1950
                        'limitationValues' => ['/1/43/'],
1951
                    ]
1952
                ),
1953
            ],
1954
            $roleLimitations
1955
        );
1956
    }
1957
1958
    /**
1959
     * Test for the assignRoleToUser() method.
@@ 2303-2396 (lines=94) @@
2300
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2301
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2302
     */
2303
    public function testAssignRoleToUserGroupWithRoleLimitation()
2304
    {
2305
        $repository = $this->getRepository();
2306
        $roleService = $repository->getRoleService();
2307
2308
        /* BEGIN: Use Case */
2309
        $userGroup = $this->createUserGroupVersion1();
2310
2311
        // Load the existing "Anonymous" role
2312
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2313
2314
        // Assign the "Anonymous" role to the newly created user group
2315
        $roleService->assignRoleToUserGroup(
2316
            $role,
2317
            $userGroup,
2318
            new SubtreeLimitation(
2319
                [
2320
                    'limitationValues' => ['/1/43/'],
2321
                ]
2322
            )
2323
        );
2324
2325
        // The assignments array will contain the new role<->group assignment
2326
        $roleAssignments = $roleService->getRoleAssignments($role);
2327
        /* END: Use Case */
2328
2329
        // Members + Partners + Anonymous + Example Group
2330
        $this->assertCount(4, $roleAssignments);
2331
2332
        // Get the role limitation
2333
        $roleLimitation = null;
2334
        foreach ($roleAssignments as $roleAssignment) {
2335
            $roleLimitation = $roleAssignment->getRoleLimitation();
2336
            if ($roleLimitation) {
2337
                break;
2338
            }
2339
        }
2340
2341
        $this->assertEquals(
2342
            new SubtreeLimitation(
2343
                [
2344
                    'limitationValues' => ['/1/43/'],
2345
                ]
2346
            ),
2347
            $roleLimitation
2348
        );
2349
2350
        // Test again to see values being merged
2351
        $roleService->assignRoleToUserGroup(
2352
            $role,
2353
            $userGroup,
2354
            new SubtreeLimitation(
2355
                [
2356
                    'limitationValues' => ['/1/43/', '/1/2/'],
2357
                ]
2358
            )
2359
        );
2360
2361
        // The assignments array will contain the new role<->user assignment
2362
        $roleAssignments = $roleService->getRoleAssignments($role);
2363
2364
        // Members + Partners + Anonymous + Example User
2365
        $this->assertCount(5, $roleAssignments);
2366
2367
        // Get the role limitation
2368
        $roleLimitations = [];
2369
        foreach ($roleAssignments as $roleAssignment) {
2370
            $roleLimitation = $roleAssignment->getRoleLimitation();
2371
            if ($roleLimitation) {
2372
                $this->assertInstanceOf(
2373
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2374
                    $roleAssignment
2375
                );
2376
                $roleLimitations[] = $roleLimitation;
2377
            }
2378
        }
2379
        array_multisort($roleLimitations);
2380
2381
        $this->assertEquals(
2382
            [
2383
                new SubtreeLimitation(
2384
                    [
2385
                        'limitationValues' => ['/1/2/'],
2386
                    ]
2387
                ),
2388
                new SubtreeLimitation(
2389
                    [
2390
                        'limitationValues' => ['/1/43/'],
2391
                    ]
2392
                ),
2393
            ],
2394
            $roleLimitations
2395
        );
2396
    }
2397
2398
    /**
2399
     * Test for the assignRoleToUserGroup() method.