Code Duplication    Length = 94-98 lines in 2 locations

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

@@ 2104-2201 (lines=98) @@
2101
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
2102
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2103
     */
2104
    public function testAssignRoleToUserWithRoleLimitation()
2105
    {
2106
        $repository = $this->getRepository();
2107
        $roleService = $repository->getRoleService();
2108
2109
        /* BEGIN: Use Case */
2110
        $user = $this->createUserVersion1();
2111
2112
        // Load the existing "Anonymous" role
2113
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2114
2115
        // Assign the "Anonymous" role to the newly created user
2116
        $roleService->assignRoleToUser(
2117
            $role,
2118
            $user,
2119
            new SubtreeLimitation(
2120
                [
2121
                    'limitationValues' => ['/1/43/'],
2122
                ]
2123
            )
2124
        );
2125
2126
        // The assignments array will contain the new role<->user assignment
2127
        $roleAssignments = $roleService->getRoleAssignments($role);
2128
        /* END: Use Case */
2129
2130
        // Members + Partners + Anonymous + Example User
2131
        $this->assertEquals(4, count($roleAssignments));
2132
2133
        // Get the role limitation
2134
        $roleLimitation = null;
2135
        foreach ($roleAssignments as $roleAssignment) {
2136
            $roleLimitation = $roleAssignment->getRoleLimitation();
2137
            if ($roleLimitation) {
2138
                $this->assertInstanceOf(
2139
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2140
                    $roleAssignment
2141
                );
2142
                break;
2143
            }
2144
        }
2145
2146
        $this->assertEquals(
2147
            new SubtreeLimitation(
2148
                [
2149
                    'limitationValues' => ['/1/43/'],
2150
                ]
2151
            ),
2152
            $roleLimitation
2153
        );
2154
2155
        // Test again to see values being merged
2156
        $roleService->assignRoleToUser(
2157
            $role,
2158
            $user,
2159
            new SubtreeLimitation(
2160
                [
2161
                    'limitationValues' => ['/1/43/', '/1/2/'],
2162
                ]
2163
            )
2164
        );
2165
2166
        // The assignments array will contain the new role<->user assignment
2167
        $roleAssignments = $roleService->getRoleAssignments($role);
2168
2169
        // Members + Partners + Anonymous + Example User
2170
        $this->assertEquals(5, count($roleAssignments));
2171
2172
        // Get the role limitation
2173
        $roleLimitations = [];
2174
        foreach ($roleAssignments as $roleAssignment) {
2175
            $roleLimitation = $roleAssignment->getRoleLimitation();
2176
            if ($roleLimitation) {
2177
                $this->assertInstanceOf(
2178
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2179
                    $roleAssignment
2180
                );
2181
                $roleLimitations[] = $roleLimitation;
2182
            }
2183
        }
2184
        array_multisort($roleLimitations);
2185
2186
        $this->assertEquals(
2187
            [
2188
                new SubtreeLimitation(
2189
                    [
2190
                        'limitationValues' => ['/1/2/'],
2191
                    ]
2192
                ),
2193
                new SubtreeLimitation(
2194
                    [
2195
                        'limitationValues' => ['/1/43/'],
2196
                    ]
2197
                ),
2198
            ],
2199
            $roleLimitations
2200
        );
2201
    }
2202
2203
    /**
2204
     * Test for the assignRoleToUser() method.
@@ 2554-2647 (lines=94) @@
2551
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2552
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2553
     */
2554
    public function testAssignRoleToUserGroupWithRoleLimitation()
2555
    {
2556
        $repository = $this->getRepository();
2557
        $roleService = $repository->getRoleService();
2558
2559
        /* BEGIN: Use Case */
2560
        $userGroup = $this->createUserGroupVersion1();
2561
2562
        // Load the existing "Anonymous" role
2563
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2564
2565
        // Assign the "Anonymous" role to the newly created user group
2566
        $roleService->assignRoleToUserGroup(
2567
            $role,
2568
            $userGroup,
2569
            new SubtreeLimitation(
2570
                [
2571
                    'limitationValues' => ['/1/43/'],
2572
                ]
2573
            )
2574
        );
2575
2576
        // The assignments array will contain the new role<->group assignment
2577
        $roleAssignments = $roleService->getRoleAssignments($role);
2578
        /* END: Use Case */
2579
2580
        // Members + Partners + Anonymous + Example Group
2581
        $this->assertEquals(4, count($roleAssignments));
2582
2583
        // Get the role limitation
2584
        $roleLimitation = null;
2585
        foreach ($roleAssignments as $roleAssignment) {
2586
            $roleLimitation = $roleAssignment->getRoleLimitation();
2587
            if ($roleLimitation) {
2588
                break;
2589
            }
2590
        }
2591
2592
        $this->assertEquals(
2593
            new SubtreeLimitation(
2594
                [
2595
                    'limitationValues' => ['/1/43/'],
2596
                ]
2597
            ),
2598
            $roleLimitation
2599
        );
2600
2601
        // Test again to see values being merged
2602
        $roleService->assignRoleToUserGroup(
2603
            $role,
2604
            $userGroup,
2605
            new SubtreeLimitation(
2606
                [
2607
                    'limitationValues' => ['/1/43/', '/1/2/'],
2608
                ]
2609
            )
2610
        );
2611
2612
        // The assignments array will contain the new role<->user assignment
2613
        $roleAssignments = $roleService->getRoleAssignments($role);
2614
2615
        // Members + Partners + Anonymous + Example User
2616
        $this->assertEquals(5, count($roleAssignments));
2617
2618
        // Get the role limitation
2619
        $roleLimitations = [];
2620
        foreach ($roleAssignments as $roleAssignment) {
2621
            $roleLimitation = $roleAssignment->getRoleLimitation();
2622
            if ($roleLimitation) {
2623
                $this->assertInstanceOf(
2624
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2625
                    $roleAssignment
2626
                );
2627
                $roleLimitations[] = $roleLimitation;
2628
            }
2629
        }
2630
        array_multisort($roleLimitations);
2631
2632
        $this->assertEquals(
2633
            [
2634
                new SubtreeLimitation(
2635
                    [
2636
                        'limitationValues' => ['/1/2/'],
2637
                    ]
2638
                ),
2639
                new SubtreeLimitation(
2640
                    [
2641
                        'limitationValues' => ['/1/43/'],
2642
                    ]
2643
                ),
2644
            ],
2645
            $roleLimitations
2646
        );
2647
    }
2648
2649
    /**
2650
     * Test for the assignRoleToUserGroup() method.