Code Duplication    Length = 94-98 lines in 2 locations

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

@@ 1723-1820 (lines=98) @@
1720
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1721
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1722
     */
1723
    public function testAssignRoleToUserWithRoleLimitation()
1724
    {
1725
        $repository = $this->getRepository();
1726
        $roleService = $repository->getRoleService();
1727
1728
        /* BEGIN: Use Case */
1729
        $user = $this->createUserVersion1();
1730
1731
        // Load the existing "Anonymous" role
1732
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1733
1734
        // Assign the "Anonymous" role to the newly created user
1735
        $roleService->assignRoleToUser(
1736
            $role,
1737
            $user,
1738
            new SubtreeLimitation(
1739
                [
1740
                    'limitationValues' => ['/1/43/'],
1741
                ]
1742
            )
1743
        );
1744
1745
        // The assignments array will contain the new role<->user assignment
1746
        $roleAssignments = $roleService->getRoleAssignments($role);
1747
        /* END: Use Case */
1748
1749
        // Members + Partners + Anonymous + Example User
1750
        $this->assertCount(4, $roleAssignments);
1751
1752
        // Get the role limitation
1753
        $roleLimitation = null;
1754
        foreach ($roleAssignments as $roleAssignment) {
1755
            $roleLimitation = $roleAssignment->getRoleLimitation();
1756
            if ($roleLimitation) {
1757
                $this->assertInstanceOf(
1758
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1759
                    $roleAssignment
1760
                );
1761
                break;
1762
            }
1763
        }
1764
1765
        $this->assertEquals(
1766
            new SubtreeLimitation(
1767
                [
1768
                    'limitationValues' => ['/1/43/'],
1769
                ]
1770
            ),
1771
            $roleLimitation
1772
        );
1773
1774
        // Test again to see values being merged
1775
        $roleService->assignRoleToUser(
1776
            $role,
1777
            $user,
1778
            new SubtreeLimitation(
1779
                [
1780
                    'limitationValues' => ['/1/43/', '/1/2/'],
1781
                ]
1782
            )
1783
        );
1784
1785
        // The assignments array will contain the new role<->user assignment
1786
        $roleAssignments = $roleService->getRoleAssignments($role);
1787
1788
        // Members + Partners + Anonymous + Example User
1789
        $this->assertCount(5, $roleAssignments);
1790
1791
        // Get the role limitation
1792
        $roleLimitations = [];
1793
        foreach ($roleAssignments as $roleAssignment) {
1794
            $roleLimitation = $roleAssignment->getRoleLimitation();
1795
            if ($roleLimitation) {
1796
                $this->assertInstanceOf(
1797
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1798
                    $roleAssignment
1799
                );
1800
                $roleLimitations[] = $roleLimitation;
1801
            }
1802
        }
1803
        array_multisort($roleLimitations);
1804
1805
        $this->assertEquals(
1806
            [
1807
                new SubtreeLimitation(
1808
                    [
1809
                        'limitationValues' => ['/1/2/'],
1810
                    ]
1811
                ),
1812
                new SubtreeLimitation(
1813
                    [
1814
                        'limitationValues' => ['/1/43/'],
1815
                    ]
1816
                ),
1817
            ],
1818
            $roleLimitations
1819
        );
1820
    }
1821
1822
    /**
1823
     * Test for the assignRoleToUser() method.
@@ 2167-2260 (lines=94) @@
2164
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2165
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2166
     */
2167
    public function testAssignRoleToUserGroupWithRoleLimitation()
2168
    {
2169
        $repository = $this->getRepository();
2170
        $roleService = $repository->getRoleService();
2171
2172
        /* BEGIN: Use Case */
2173
        $userGroup = $this->createUserGroupVersion1();
2174
2175
        // Load the existing "Anonymous" role
2176
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2177
2178
        // Assign the "Anonymous" role to the newly created user group
2179
        $roleService->assignRoleToUserGroup(
2180
            $role,
2181
            $userGroup,
2182
            new SubtreeLimitation(
2183
                [
2184
                    'limitationValues' => ['/1/43/'],
2185
                ]
2186
            )
2187
        );
2188
2189
        // The assignments array will contain the new role<->group assignment
2190
        $roleAssignments = $roleService->getRoleAssignments($role);
2191
        /* END: Use Case */
2192
2193
        // Members + Partners + Anonymous + Example Group
2194
        $this->assertCount(4, $roleAssignments);
2195
2196
        // Get the role limitation
2197
        $roleLimitation = null;
2198
        foreach ($roleAssignments as $roleAssignment) {
2199
            $roleLimitation = $roleAssignment->getRoleLimitation();
2200
            if ($roleLimitation) {
2201
                break;
2202
            }
2203
        }
2204
2205
        $this->assertEquals(
2206
            new SubtreeLimitation(
2207
                [
2208
                    'limitationValues' => ['/1/43/'],
2209
                ]
2210
            ),
2211
            $roleLimitation
2212
        );
2213
2214
        // Test again to see values being merged
2215
        $roleService->assignRoleToUserGroup(
2216
            $role,
2217
            $userGroup,
2218
            new SubtreeLimitation(
2219
                [
2220
                    'limitationValues' => ['/1/43/', '/1/2/'],
2221
                ]
2222
            )
2223
        );
2224
2225
        // The assignments array will contain the new role<->user assignment
2226
        $roleAssignments = $roleService->getRoleAssignments($role);
2227
2228
        // Members + Partners + Anonymous + Example User
2229
        $this->assertCount(5, $roleAssignments);
2230
2231
        // Get the role limitation
2232
        $roleLimitations = [];
2233
        foreach ($roleAssignments as $roleAssignment) {
2234
            $roleLimitation = $roleAssignment->getRoleLimitation();
2235
            if ($roleLimitation) {
2236
                $this->assertInstanceOf(
2237
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2238
                    $roleAssignment
2239
                );
2240
                $roleLimitations[] = $roleLimitation;
2241
            }
2242
        }
2243
        array_multisort($roleLimitations);
2244
2245
        $this->assertEquals(
2246
            [
2247
                new SubtreeLimitation(
2248
                    [
2249
                        'limitationValues' => ['/1/2/'],
2250
                    ]
2251
                ),
2252
                new SubtreeLimitation(
2253
                    [
2254
                        'limitationValues' => ['/1/43/'],
2255
                    ]
2256
                ),
2257
            ],
2258
            $roleLimitations
2259
        );
2260
    }
2261
2262
    /**
2263
     * Test for the assignRoleToUserGroup() method.