Code Duplication    Length = 94-98 lines in 2 locations

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

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