Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace tool_cohortroles;
18
 
19
/**
20
 * API tests.
21
 *
22
 * @package    tool_cohortroles
23
 * @copyright  2015 Damyon Wiese
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
1441 ariadna 26
final class api_test extends \advanced_testcase {
1 efrain 27
    /** @var \stdClass $cohort */
28
    protected $cohort = null;
29
 
30
    /** @var \stdClass $userassignto */
31
    protected $userassignto = null;
32
 
33
    /** @var \stdClass $userassignover */
34
    protected $userassignover = null;
35
 
36
    /** @var \stdClass $role */
37
    protected $role = null;
38
 
39
    /** @var int $roleid */
40
    protected $roleid;
41
 
42
    /**
43
     * Setup function- we will create a course and add an assign instance to it.
44
     */
45
    protected function setUp(): void {
1441 ariadna 46
        parent::setUp();
1 efrain 47
        $this->resetAfterTest(true);
48
 
49
        // Create some users.
50
        $this->cohort = $this->getDataGenerator()->create_cohort();
51
        $this->userassignto = $this->getDataGenerator()->create_user();
52
        $this->userassignover = $this->getDataGenerator()->create_user();
53
        $this->roleid = create_role('Sausage Roll', 'sausageroll', 'mmmm');
54
        cohort_add_member($this->cohort->id, $this->userassignover->id);
55
    }
56
 
11 efrain 57
    public function test_create_cohort_role_assignment_without_permission(): void {
1 efrain 58
        $this->setUser($this->userassignto);
59
        $params = (object) array(
60
            'userid' => $this->userassignto->id,
61
            'roleid' => $this->roleid,
62
            'cohortid' => $this->cohort->id
63
        );
64
        $this->expectException(\required_capability_exception::class);
65
        api::create_cohort_role_assignment($params);
66
    }
67
 
11 efrain 68
    public function test_create_cohort_role_assignment_with_invalid_data(): void {
1 efrain 69
        $this->setAdminUser();
70
        $params = (object) array(
71
            'userid' => $this->userassignto->id,
72
            'roleid' => -8,
73
            'cohortid' => $this->cohort->id
74
        );
1441 ariadna 75
        $this->expectException(\core\invalid_persistent_exception::class);
1 efrain 76
        api::create_cohort_role_assignment($params);
77
    }
78
 
11 efrain 79
    public function test_create_cohort_role_assignment(): void {
1 efrain 80
        $this->setAdminUser();
81
        $params = (object) array(
82
            'userid' => $this->userassignto->id,
83
            'roleid' => $this->roleid,
84
            'cohortid' => $this->cohort->id
85
        );
86
        $result = api::create_cohort_role_assignment($params);
87
        $this->assertNotEmpty($result->get('id'));
88
        $this->assertEquals($result->get('userid'), $this->userassignto->id);
89
        $this->assertEquals($result->get('roleid'), $this->roleid);
90
        $this->assertEquals($result->get('cohortid'), $this->cohort->id);
91
    }
92
 
11 efrain 93
    public function test_delete_cohort_role_assignment_without_permission(): void {
1 efrain 94
        $this->setAdminUser();
95
        $params = (object) array(
96
            'userid' => $this->userassignto->id,
97
            'roleid' => $this->roleid,
98
            'cohortid' => $this->cohort->id
99
        );
100
        $result = api::create_cohort_role_assignment($params);
101
        $this->setUser($this->userassignto);
102
        $this->expectException(\required_capability_exception::class);
103
        api::delete_cohort_role_assignment($result->get('id'));
104
    }
105
 
11 efrain 106
    public function test_delete_cohort_role_assignment_with_invalid_data(): void {
1 efrain 107
        $this->setAdminUser();
108
        $params = (object) array(
109
            'userid' => $this->userassignto->id,
110
            'roleid' => $this->roleid,
111
            'cohortid' => $this->cohort->id
112
        );
113
        $result = api::create_cohort_role_assignment($params);
114
        $this->expectException(\dml_missing_record_exception::class);
115
        api::delete_cohort_role_assignment($result->get('id') + 1);
116
    }
117
 
11 efrain 118
    public function test_delete_cohort_role_assignment(): void {
1 efrain 119
        $this->setAdminUser();
120
        // Create a cohort role assigment.
121
        $params = (object) [
122
            'userid' => $this->userassignto->id,
123
            'roleid' => $this->roleid,
124
            'cohortid' => $this->cohort->id
125
        ];
126
        $cohortroleassignment = api::create_cohort_role_assignment($params);
127
        $sync = api::sync_all_cohort_roles();
128
        $rolesadded = [
129
            [
130
                'useridassignedto' => $this->userassignto->id,
131
                'useridassignedover' => $this->userassignover->id,
132
                'roleid' => $this->roleid
133
            ]
134
        ];
135
        $expected = [
136
            'rolesadded' => $rolesadded,
137
            'rolesremoved' => []
138
        ];
139
        $this->assertEquals($sync, $expected);
140
 
141
        // Delete the cohort role assigment and confirm the roles are removed.
142
        $result = api::delete_cohort_role_assignment($cohortroleassignment->get('id'));
143
        $this->assertTrue($result);
144
        $sync = api::sync_all_cohort_roles();
145
        $expected = [
146
            'rolesadded' => [],
147
            'rolesremoved' => $rolesadded
148
        ];
149
        $this->assertEquals($expected, $sync);
150
    }
151
 
152
    /**
153
     * Test case verifying that syncing won't remove role assignments if they are valid for another cohort role assignment.
154
     */
11 efrain 155
    public function test_delete_cohort_role_assignment_cohorts_having_same_members(): void {
1 efrain 156
        $this->setAdminUser();
157
 
158
        // Create 2 cohorts, with a 1 user (user1) present in both,
159
        // and user2 and user3 members of 1 cohort each.
160
        $cohort1 = $this->getDataGenerator()->create_cohort();
161
        $cohort2 = $this->getDataGenerator()->create_cohort();
162
        $user1 = $this->getDataGenerator()->create_user();
163
        $user2 = $this->getDataGenerator()->create_user();
164
        $user3 = $this->getDataGenerator()->create_user();
165
        cohort_add_member($cohort1->id, $user1->id);
166
        cohort_add_member($cohort1->id, $user2->id);
167
        cohort_add_member($cohort2->id, $user1->id);
168
        cohort_add_member($cohort2->id, $user3->id);
169
 
170
        // And a role and a user to assign that role to.
171
        $user4 = $this->getDataGenerator()->create_user(); // A cohort manager, for example.
172
        $roleid = create_role('Role 1', 'myrole', 'test');
173
 
174
        // Assign the role for user4 in both cohorts.
175
        $params = (object) [
176
            'userid' => $user4->id,
177
            'roleid' => $roleid,
178
            'cohortid' => $cohort1->id
179
        ];
180
        $cohort1roleassignment = api::create_cohort_role_assignment($params);
181
        $params->cohortid = $cohort2->id;
182
        $cohort2roleassignment = api::create_cohort_role_assignment($params);
183
 
184
        $sync = api::sync_all_cohort_roles();
185
 
186
        // There is no guarantee about the order of roles assigned.
187
        // so confirm we have 3 role assignments, and they are for the users 1, 2 and 3.
188
        $this->assertCount(3, $sync['rolesadded']);
189
        $addedusers = array_column($sync['rolesadded'], 'useridassignedover');
190
        $this->assertContains($user1->id, $addedusers);
191
        $this->assertContains($user2->id, $addedusers);
192
        $this->assertContains($user3->id, $addedusers);
193
 
194
        // Remove the role assignment for user4/cohort1.
195
        // Verify only 1 role is unassigned as the others are still valid for the other cohort role assignment.
196
        $result = api::delete_cohort_role_assignment($cohort1roleassignment->get('id'));
197
        $this->assertTrue($result);
198
 
199
        $sync = api::sync_all_cohort_roles();
200
 
201
        $this->assertCount(0, $sync['rolesadded']);
202
        $this->assertCount(1, $sync['rolesremoved']);
203
        $removedusers = array_column($sync['rolesremoved'], 'useridassignedover');
204
        $this->assertContains($user2->id, $removedusers);
205
    }
206
 
11 efrain 207
    public function test_list_cohort_role_assignments(): void {
1 efrain 208
        $this->setAdminUser();
209
        $params = (object) array(
210
            'userid' => $this->userassignto->id,
211
            'roleid' => $this->roleid,
212
            'cohortid' => $this->cohort->id
213
        );
214
        $result = api::create_cohort_role_assignment($params);
215
 
216
        $list = api::list_cohort_role_assignments();
217
        $list[0]->is_valid();
218
        $this->assertEquals($list[0], $result);
219
    }
220
 
11 efrain 221
    public function test_count_cohort_role_assignments(): void {
1 efrain 222
        $this->setAdminUser();
223
        $params = (object) array(
224
            'userid' => $this->userassignto->id,
225
            'roleid' => $this->roleid,
226
            'cohortid' => $this->cohort->id
227
        );
228
        $result = api::create_cohort_role_assignment($params);
229
 
230
        $count = api::count_cohort_role_assignments();
231
        $this->assertEquals($count, 1);
232
    }
233
 
11 efrain 234
    public function test_sync_all_cohort_roles(): void {
1 efrain 235
        $this->setAdminUser();
236
        $params = (object) array(
237
            'userid' => $this->userassignto->id,
238
            'roleid' => $this->roleid,
239
            'cohortid' => $this->cohort->id
240
        );
241
        $result = api::create_cohort_role_assignment($params);
242
 
243
        // Verify roles are assigned when users enter the cohort.
244
        $sync = api::sync_all_cohort_roles();
245
 
246
        $rolesadded = array(array(
247
            'useridassignedto' => $this->userassignto->id,
248
            'useridassignedover' => $this->userassignover->id,
249
            'roleid' => $this->roleid
250
        ));
251
        $rolesremoved = array();
252
        $expected = array('rolesadded' => $rolesadded,
253
                          'rolesremoved' => $rolesremoved);
254
        $this->assertEquals($sync, $expected);
255
 
256
        // Verify roles are removed when users leave the cohort.
257
        cohort_remove_member($this->cohort->id, $this->userassignover->id);
258
        $sync = api::sync_all_cohort_roles();
259
 
260
        $rolesadded = array();
261
        $rolesremoved = array(array(
262
            'useridassignedto' => $this->userassignto->id,
263
            'useridassignedover' => $this->userassignover->id,
264
            'roleid' => $this->roleid
265
        ));
266
        $expected = array('rolesadded' => $rolesadded,
267
                          'rolesremoved' => $rolesremoved);
268
        $this->assertEquals($sync, $expected);
269
 
270
        // Verify roles assigned by any other component are not removed.
271
        $usercontext = \context_user::instance($this->userassignover->id);
272
        role_assign($this->roleid, $this->userassignto->id, $usercontext->id);
273
        $sync = api::sync_all_cohort_roles();
274
 
275
        $rolesadded = array();
276
        $rolesremoved = array();
277
        $expected = array('rolesadded' => $rolesadded,
278
                          'rolesremoved' => $rolesremoved);
279
        $this->assertEquals($sync, $expected);
280
 
281
        // Remove manual role assignment.
282
        role_unassign($this->roleid, $this->userassignto->id, $usercontext->id);
283
        // Add someone to the cohort again...
284
        cohort_add_member($this->cohort->id, $this->userassignover->id);
285
        $sync = api::sync_all_cohort_roles();
286
        $rolesadded = array(array(
287
            'useridassignedto' => $this->userassignto->id,
288
            'useridassignedover' => $this->userassignover->id,
289
            'roleid' => $this->roleid
290
        ));
291
        $rolesremoved = array();
292
        $expected = array('rolesadded' => $rolesadded,
293
                          'rolesremoved' => $rolesremoved);
294
        $this->assertEquals($sync, $expected);
295
 
296
        // Verify no fatal errors when a cohort is deleted.
297
        cohort_delete_cohort($this->cohort);
298
        $sync = api::sync_all_cohort_roles();
299
 
300
        $rolesadded = array();
301
        $rolesremoved = array(array(
302
            'useridassignedto' => $this->userassignto->id,
303
            'useridassignedover' => $this->userassignover->id,
304
            'roleid' => $this->roleid
305
        ));
306
        $expected = array('rolesadded' => $rolesadded,
307
                          'rolesremoved' => $rolesremoved);
308
        $this->assertEquals($sync, $expected);
309
    }
310
 
311
}