2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for group lib.
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
. '/group/lib.php');
35 * @copyright 2013 Frédéric Massart
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class lib_test
extends \advanced_testcase
{
40 public function test_member_added_event() {
41 $this->resetAfterTest();
43 $this->setAdminUser();
44 $course = $this->getDataGenerator()->create_course();
45 $user = $this->getDataGenerator()->create_user();
46 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
47 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
49 $sink = $this->redirectEvents();
50 groups_add_member($group->id
, $user->id
, 'mod_workshop', '123');
51 $events = $sink->get_events();
52 $this->assertCount(1, $events);
53 $event = reset($events);
55 $this->assertInstanceOf('\core\event\group_member_added', $event);
56 $this->assertEquals($user->id
, $event->relateduserid
);
57 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
58 $this->assertEquals($group->id
, $event->objectid
);
59 $url = new \
moodle_url('/group/members.php', array('group' => $event->objectid
));
60 $this->assertEquals($url, $event->get_url());
63 public function test_member_removed_event() {
64 $this->resetAfterTest();
66 $this->setAdminUser();
67 $course = $this->getDataGenerator()->create_course();
68 $user = $this->getDataGenerator()->create_user();
69 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
70 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
71 $this->getDataGenerator()->create_group_member(array('userid' => $user->id
, 'groupid' => $group->id
));
73 $sink = $this->redirectEvents();
74 groups_remove_member($group->id
, $user->id
);
75 $events = $sink->get_events();
76 $this->assertCount(1, $events);
77 $event = reset($events);
79 $this->assertInstanceOf('\core\event\group_member_removed', $event);
80 $this->assertEquals($user->id
, $event->relateduserid
);
81 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
82 $this->assertEquals($group->id
, $event->objectid
);
83 $url = new \
moodle_url('/group/members.php', array('group' => $event->objectid
));
84 $this->assertEquals($url, $event->get_url());
87 public function test_group_created_event() {
88 $this->resetAfterTest();
90 $this->setAdminUser();
91 $course = $this->getDataGenerator()->create_course();
93 $sink = $this->redirectEvents();
94 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
95 $events = $sink->get_events();
96 $this->assertCount(1, $events);
97 $event = reset($events);
99 $this->assertInstanceOf('\core\event\group_created', $event);
100 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
101 $this->assertEquals($group->id
, $event->objectid
);
102 $url = new \
moodle_url('/group/index.php', array('id' => $event->courseid
));
103 $this->assertEquals($url, $event->get_url());
106 public function test_grouping_created_event() {
107 $this->resetAfterTest();
109 $this->setAdminUser();
110 $course = $this->getDataGenerator()->create_course();
112 $sink = $this->redirectEvents();
113 $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
114 $events = $sink->get_events();
115 $this->assertCount(1, $events);
116 $event = reset($events);
118 $this->assertInstanceOf('\core\event\grouping_created', $event);
120 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
121 $this->assertEquals($group->id
, $event->objectid
);
122 $url = new \
moodle_url('/group/groupings.php', array('id' => $event->courseid
));
123 $this->assertEquals($url, $event->get_url());
126 public function test_group_updated_event() {
129 $this->resetAfterTest();
131 $course = $this->getDataGenerator()->create_course();
132 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
134 $sink = $this->redirectEvents();
135 $data = new \
stdClass();
136 $data->id
= $group->id
;
137 $data->courseid
= $course->id
;
138 $data->name
= 'Backend team';
139 $this->setCurrentTimeStart();
140 groups_update_group($data);
141 $group = $DB->get_record('groups', array('id'=>$group->id
)); // Fetch record with modified timestamp.
142 $events = $sink->get_events();
143 $this->assertCount(1, $events);
144 $event = reset($events);
145 $this->assertTimeCurrent($group->timemodified
);
147 $this->assertInstanceOf('\core\event\group_updated', $event);
148 $group->name
= $data->name
;
149 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
150 $this->assertEquals($group->id
, $event->objectid
);
151 $url = new \
moodle_url('/group/group.php', array('id' => $event->objectid
));
152 $this->assertEquals($url, $event->get_url());
155 public function test_group_updated_event_does_not_require_names() {
158 $this->resetAfterTest();
160 $course = $this->getDataGenerator()->create_course();
161 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
163 $sink = $this->redirectEvents();
164 $data = new \
stdClass();
165 $data->id
= $group->id
;
166 $data->courseid
= $course->id
;
167 $this->setCurrentTimeStart();
168 groups_update_group($data);
169 $group = $DB->get_record('groups', array('id'=>$group->id
)); // Fetch record with modified timestamp.
170 $events = $sink->get_events();
171 $this->assertCount(1, $events);
172 $event = reset($events);
173 $this->assertTimeCurrent($group->timemodified
);
175 $this->assertInstanceOf('\core\event\group_updated', $event);
176 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
177 $this->assertEquals($group->id
, $event->objectid
);
178 $url = new \
moodle_url('/group/group.php', array('id' => $event->objectid
));
179 $this->assertEquals($url, $event->get_url());
182 public function test_grouping_updated_event() {
185 $this->resetAfterTest();
187 $course = $this->getDataGenerator()->create_course();
188 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
190 $sink = $this->redirectEvents();
191 $data = new \
stdClass();
192 $data->id
= $grouping->id
;
193 $data->courseid
= $course->id
;
194 $data->name
= 'Backend team';
195 $this->setCurrentTimeStart();
196 groups_update_grouping($data);
197 $events = $sink->get_events();
198 $this->assertCount(1, $events);
199 $event = reset($events);
201 $this->assertInstanceOf('\core\event\grouping_updated', $event);
203 // Get the timemodified from DB for comparison with snapshot.
204 $data->timemodified
= $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id
));
205 $this->assertTimeCurrent($data->timemodified
);
207 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
208 $this->assertEquals($grouping->id
, $event->objectid
);
209 $url = new \
moodle_url('/group/grouping.php', array('id' => $event->objectid
));
210 $this->assertEquals($url, $event->get_url());
213 public function test_grouping_updated_event_does_not_require_names() {
216 $this->resetAfterTest();
218 $course = $this->getDataGenerator()->create_course();
219 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
221 $sink = $this->redirectEvents();
222 $data = new \
stdClass();
223 $data->id
= $grouping->id
;
224 $data->courseid
= $course->id
;
225 $this->setCurrentTimeStart();
226 groups_update_grouping($data);
227 $events = $sink->get_events();
228 $this->assertCount(1, $events);
229 $event = reset($events);
231 $this->assertInstanceOf('\core\event\grouping_updated', $event);
233 // Get the timemodified from DB for comparison with snapshot.
234 $data->timemodified
= $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id
));
235 $this->assertTimeCurrent($data->timemodified
);
236 // Following fields were not updated so the snapshot should have them the same as in original group.
237 $data->description
= $grouping->description
;
238 $data->descriptionformat
= $grouping->descriptionformat
;
239 $data->configdata
= $grouping->configdata
;
240 $data->idnumber
= $grouping->idnumber
;
241 $data->name
= $grouping->name
;
242 $data->timecreated
= $grouping->timecreated
;
244 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
245 $this->assertEquals($grouping->id
, $event->objectid
);
246 $url = new \
moodle_url('/group/grouping.php', array('id' => $event->objectid
));
247 $this->assertEquals($url, $event->get_url());
250 public function test_group_deleted_event() {
251 $this->resetAfterTest();
253 $course = $this->getDataGenerator()->create_course();
254 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
256 $sink = $this->redirectEvents();
257 groups_delete_group($group->id
);
258 $events = $sink->get_events();
259 $this->assertCount(1, $events);
260 $event = reset($events);
262 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
263 $this->assertEquals($group->id
, $event->objectid
);
264 $url = new \
moodle_url('/group/index.php', array('id' => $event->courseid
));
265 $this->assertEquals($url, $event->get_url());
268 public function test_grouping_deleted_event() {
269 $this->resetAfterTest();
271 $course = $this->getDataGenerator()->create_course();
272 $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
274 $sink = $this->redirectEvents();
275 groups_delete_grouping($group->id
);
276 $events = $sink->get_events();
277 $this->assertCount(1, $events);
278 $event = reset($events);
280 $this->assertInstanceOf('\core\event\grouping_deleted', $event);
281 $this->assertEquals(\context_course
::instance($course->id
), $event->get_context());
282 $this->assertEquals($group->id
, $event->objectid
);
283 $url = new \
moodle_url('/group/groupings.php', array('id' => $event->courseid
));
284 $this->assertEquals($url, $event->get_url());
287 public function test_groups_delete_group_members() {
289 $this->resetAfterTest();
291 $course = $this->getDataGenerator()->create_course();
292 $user1 = $this->getDataGenerator()->create_user();
293 $user2 = $this->getDataGenerator()->create_user();
294 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
295 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
);
296 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
297 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
299 // Test deletion of all the users.
300 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
301 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
));
302 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
304 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
305 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
306 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
307 groups_delete_group_members($course->id
);
308 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
309 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
310 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
312 // Test deletion of a specific user.
313 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
314 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
));
315 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
317 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
318 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
319 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
320 groups_delete_group_members($course->id
, $user2->id
);
321 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
322 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
323 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
326 public function test_groups_remove_member() {
328 $this->resetAfterTest();
330 $course = $this->getDataGenerator()->create_course();
331 $user1 = $this->getDataGenerator()->create_user();
332 $user2 = $this->getDataGenerator()->create_user();
333 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
334 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
);
335 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
336 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
338 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
339 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
));
340 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
342 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
343 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
344 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
345 groups_remove_member($group1->id
, $user1->id
);
346 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
347 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
348 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
349 groups_remove_member($group1->id
, $user2->id
);
350 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
351 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
352 groups_remove_member($group2->id
, $user1->id
);
353 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
356 public function test_groups_delete_groupings_groups() {
358 $this->resetAfterTest();
360 $course = $this->getDataGenerator()->create_course();
361 $course2 = $this->getDataGenerator()->create_course();
362 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
363 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
364 $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id
));
365 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
366 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
367 $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id
));
369 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id
, 'groupid' => $group1->id
));
370 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id
, 'groupid' => $group2->id
));
371 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping2->id
, 'groupid' => $group1->id
));
372 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1c2->id
, 'groupid' => $group1c2->id
));
373 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
374 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group2->id
, 'groupingid' => $grouping1->id
)));
375 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping2->id
)));
376 $this->assertTrue($DB->record_exists('groupings_groups',
377 array('groupid' => $group1c2->id
, 'groupingid' => $grouping1c2->id
)));
378 groups_delete_groupings_groups($course->id
);
379 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
380 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group2->id
, 'groupingid' => $grouping1->id
)));
381 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping2->id
)));
382 $this->assertTrue($DB->record_exists('groupings_groups',
383 array('groupid' => $group1c2->id
, 'groupingid' => $grouping1c2->id
)));
386 public function test_groups_delete_groups() {
388 $this->resetAfterTest();
390 $course = $this->getDataGenerator()->create_course();
391 $course2 = $this->getDataGenerator()->create_course();
392 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
393 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
394 $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id
));
395 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
396 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
397 $user1 = $this->getDataGenerator()->create_user();
398 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
399 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
400 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
));
402 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
403 $this->assertTrue($DB->record_exists('groups', array('id' => $group2->id
, 'courseid' => $course->id
)));
404 $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id
, 'courseid' => $course2->id
)));
405 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
406 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
407 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
408 groups_delete_groups($course->id
);
409 $this->assertFalse($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
410 $this->assertFalse($DB->record_exists('groups', array('id' => $group2->id
, 'courseid' => $course->id
)));
411 $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id
, 'courseid' => $course2->id
)));
412 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
413 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
414 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
417 public function test_groups_delete_groupings() {
419 $this->resetAfterTest();
421 $course = $this->getDataGenerator()->create_course();
422 $course2 = $this->getDataGenerator()->create_course();
423 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
424 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
425 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
426 $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id
));
427 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
));
429 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
430 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
431 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping2->id
, 'courseid' => $course->id
)));
432 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id
, 'courseid' => $course2->id
)));
433 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
434 groups_delete_groupings($course->id
);
435 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
436 $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
437 $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping2->id
, 'courseid' => $course->id
)));
438 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id
, 'courseid' => $course2->id
)));
439 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
442 public function test_groups_create_autogroups () {
444 $this->resetAfterTest();
446 $course = $this->getDataGenerator()->create_course();
447 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
448 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
449 $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
450 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
451 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group2->id
, 'groupingid' => $grouping1->id
));
452 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group3->id
, 'groupingid' => $grouping1->id
));
453 $user1 = $this->getDataGenerator()->create_user();
454 $user2 = $this->getDataGenerator()->create_user();
455 $user3 = $this->getDataGenerator()->create_user();
456 $user4 = $this->getDataGenerator()->create_user();
457 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
458 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
);
459 $this->getDataGenerator()->enrol_user($user3->id
, $course->id
);
460 $this->getDataGenerator()->enrol_user($user4->id
, $course->id
);
461 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
462 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
463 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user3->id
));
464 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id
, 'userid' => $user4->id
));
466 // Test autocreate group based on all course users.
467 $users = groups_get_potential_members($course->id
);
468 $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
469 foreach ($users as $user) {
470 $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id
, 'userid' => $user->id
));
472 $this->assertEquals(4, $DB->count_records('groups_members', array('groupid' => $group4->id
)));
474 // Test autocreate group based on existing group.
476 $source['groupid'] = $group1->id
;
477 $users = groups_get_potential_members($course->id
, 0, $source);
478 $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
479 foreach ($users as $user) {
480 $this->getDataGenerator()->create_group_member(array('groupid' => $group5->id
, 'userid' => $user->id
));
482 $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group5->id
)));
484 // Test autocreate group based on existing grouping.
486 $source['groupingid'] = $grouping1->id
;
487 $users = groups_get_potential_members($course->id
, 0, $source);
488 $group6 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
489 foreach ($users as $user) {
490 $this->getDataGenerator()->create_group_member(array('groupid' => $group6->id
, 'userid' => $user->id
));
492 $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id
)));
496 * Test groups_create_group enabling a group conversation.
498 public function test_groups_create_group_with_conversation() {
501 $this->resetAfterTest();
502 $this->setAdminUser();
503 $course1 = $this->getDataGenerator()->create_course();
504 $coursecontext1 = \context_course
::instance($course1->id
);
506 // Create two groups and only one group with enablemessaging = 1.
507 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id
, 'enablemessaging' => 1));
508 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id
, 'enablemessaging' => 0));
510 $conversations = $DB->get_records('message_conversations',
512 'contextid' => $coursecontext1->id
,
513 'component' => 'core_group',
514 'itemtype' => 'groups',
515 'enabled' => \core_message\api
::MESSAGE_CONVERSATION_ENABLED
518 $this->assertCount(1, $conversations);
520 $conversation = reset($conversations);
521 // Check groupid was stored in itemid on conversation area.
522 $this->assertEquals($group1a->id
, $conversation->itemid
);
524 $conversations = $DB->get_records('message_conversations', ['id' => $conversation->id
]);
525 $this->assertCount(1, $conversations);
527 $conversation = reset($conversations);
529 // Check group name was stored in conversation.
530 $this->assertEquals($group1a->name
, $conversation->name
);
534 * Test groups_update_group enabling and disabling a group conversation.
536 public function test_groups_update_group_conversation() {
539 $this->resetAfterTest();
540 $this->setAdminUser();
541 $course1 = $this->getDataGenerator()->create_course();
542 $coursecontext1 = \context_course
::instance($course1->id
);
544 // Create two groups and only one group with enablemessaging = 1.
545 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id
, 'enablemessaging' => 1));
546 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id
, 'enablemessaging' => 0));
548 $conversations = $DB->get_records('message_conversations',
550 'contextid' => $coursecontext1->id
,
551 'component' => 'core_group',
552 'itemtype' => 'groups',
553 'enabled' => \core_message\api
::MESSAGE_CONVERSATION_ENABLED
556 $this->assertCount(1, $conversations);
558 // Check that the conversation area is created when group messaging is enabled in the course group.
559 $group1b->enablemessaging
= 1;
560 groups_update_group($group1b);
562 $conversations = $DB->get_records('message_conversations',
564 'contextid' => $coursecontext1->id
,
565 'component' => 'core_group',
566 'itemtype' => 'groups',
567 'enabled' => \core_message\api
::MESSAGE_CONVERSATION_ENABLED
570 $this->assertCount(2, $conversations);
572 $conversation1a = array_shift($conversations);
573 $conversation1b = array_shift($conversations);
575 $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id
]);
577 // Check for group1b that group name was stored in conversation.
578 $this->assertEquals($group1b->name
, $conversation1b->name
);
580 $group1b->enablemessaging
= 0;
581 groups_update_group($group1b);
582 $this->assertEquals(0, $DB->get_field("message_conversations", "enabled", ['id' => $conversation1b->id
]));
584 // Check that the name of the conversation is changed when the name of the course group is updated.
585 $group1b->name
= 'New group name';
586 groups_update_group($group1b);
587 $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id
]);
588 $this->assertEquals($group1b->name
, $conversation1b->name
);
592 * Test groups_add_member to conversation.
594 public function test_groups_add_member_conversation() {
596 $this->resetAfterTest();
598 $this->setAdminUser();
600 $course1 = $this->getDataGenerator()->create_course();
601 $coursecontext1 = \context_course
::instance($course1->id
);
603 $user1 = $this->getDataGenerator()->create_user();
604 $user2 = $this->getDataGenerator()->create_user();
605 $user3 = $this->getDataGenerator()->create_user();
607 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
608 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
609 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
611 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id
, 'enablemessaging' => 1));
613 // Add users to group1.
614 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
615 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
617 $conversation = \core_message\api
::get_conversation_by_area(
624 // Check if the users has been added to the conversation.
625 $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id
]));
627 // Check if the user has been added to the conversation when the conversation is disabled.
628 \core_message\api
::disable_conversation($conversation->id
);
629 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user3->id
));
630 $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id
]));
634 * Test groups_remove_member to conversation.
636 public function test_groups_remove_member_conversation() {
639 $this->resetAfterTest();
641 $this->setAdminUser();
643 $course1 = $this->getDataGenerator()->create_course();
644 $coursecontext1 = \context_course
::instance($course1->id
);
646 $user1 = $this->getDataGenerator()->create_user();
647 $user2 = $this->getDataGenerator()->create_user();
648 $user3 = $this->getDataGenerator()->create_user();
650 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
651 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
652 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
654 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id
, 'enablemessaging' => 1));
656 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
657 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
658 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user3->id
));
660 $conversation = \core_message\api
::get_conversation_by_area(
667 // Check if there are three users in the conversation.
668 $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id
]));
670 // Check if after removing one member in the conversation there are two members.
671 groups_remove_member($group1->id
, $user1->id
);
672 $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id
]));
674 // Check if the user has been removed from the conversation when the conversation is disabled.
675 \core_message\api
::disable_conversation($conversation->id
);
676 groups_remove_member($group1->id
, $user2->id
);
677 $this->assertEquals(1, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id
]));
681 * Test if you enable group messaging in a group with members these are added to the conversation.
683 public function test_add_members_group_updated_conversation_enabled() {
686 $this->resetAfterTest();
688 $this->setAdminUser();
690 $course1 = $this->getDataGenerator()->create_course();
691 $coursecontext1 = \context_course
::instance($course1->id
);
693 $user1 = $this->getDataGenerator()->create_user();
694 $user2 = $this->getDataGenerator()->create_user();
695 $user3 = $this->getDataGenerator()->create_user();
697 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
);
698 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
);
699 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
);
701 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id
, 'enablemessaging' => 0));
703 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
704 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
705 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user3->id
));
707 $conversation = \core_message\api
::get_conversation_by_area(
714 // No conversation should exist as 'enablemessaging' was set to 0.
715 $this->assertFalse($conversation);
717 // Check that the three users are in the conversation when group messaging is enabled in the course group.
718 $group1->enablemessaging
= 1;
719 groups_update_group($group1);
721 $conversation = \core_message\api
::get_conversation_by_area(
728 $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id
]));
731 public function test_groups_get_members_by_role(): void
{
732 $this->resetAfterTest();
734 $this->setAdminUser();
736 $course1 = $this->getDataGenerator()->create_course();
738 $user1 = $this->getDataGenerator()->create_user(['username' => 'user1', 'idnumber' => 1]);
739 $user2 = $this->getDataGenerator()->create_user(['username' => 'user2', 'idnumber' => 2]);
740 $user3 = $this->getDataGenerator()->create_user(['username' => 'user3', 'idnumber' => 3]);
742 $this->getDataGenerator()->enrol_user($user1->id
, $course1->id
, 0);
743 $this->getDataGenerator()->enrol_user($user2->id
, $course1->id
, 1);
744 $this->getDataGenerator()->enrol_user($user3->id
, $course1->id
, 1);
746 $group1 = $this->getDataGenerator()->create_group(['courseid' => $course1->id
]);
748 $this->getDataGenerator()->create_group_member(['groupid' => $group1->id
, 'userid' => $user1->id
]);
749 $this->getDataGenerator()->create_group_member(['groupid' => $group1->id
, 'userid' => $user2->id
]);
750 $this->getDataGenerator()->create_group_member(['groupid' => $group1->id
, 'userid' => $user3->id
]);
753 $result = groups_get_members_by_role($group1->id
, $course1->id
);
754 $this->assertEquals(1, count($result[0]->users
));
755 $this->assertEquals(2, count($result[1]->users
));
756 $this->assertEquals($user1->firstname
, reset($result[0]->users
)->firstname
);
757 $this->assertEquals($user1->username
, reset($result[0]->users
)->username
);
759 // Test with specified fields.
760 $result = groups_get_members_by_role($group1->id
, $course1->id
, 'u.firstname, u.lastname');
761 $this->assertEquals(1, count($result[0]->users
));
762 $this->assertEquals($user1->firstname
, reset($result[0]->users
)->firstname
);
763 $this->assertEquals($user1->lastname
, reset($result[0]->users
)->lastname
);
764 $this->assertEquals(false, isset(reset($result[0]->users
)->username
));
766 // Test with sorting.
767 $result = groups_get_members_by_role($group1->id
, $course1->id
, 'u.username', 'u.username DESC');
768 $this->assertEquals(1, count($result[0]->users
));
769 $this->assertEquals($user3->username
, reset($result[1]->users
)->username
);
770 $result = groups_get_members_by_role($group1->id
, $course1->id
, 'u.username', 'u.username ASC');
771 $this->assertEquals(1, count($result[0]->users
));
772 $this->assertEquals($user2->username
, reset($result[1]->users
)->username
);
774 // Test with extra WHERE.
775 $result = groups_get_members_by_role(
780 'u.idnumber > :number',
782 $this->assertEquals(1, count($result));
783 $this->assertEquals(1, count($result[1]->users
));
784 $this->assertEquals($user3->username
, reset($result[1]->users
)->username
);
787 set_user_preference('reptile', 'snake', $user1);
788 $result = groups_get_members_by_role($group1->id
, $course1->id
, 'u.username, up.value', null, 'up.name = :prefname',
789 ['prefname' => 'reptile'], 'JOIN {user_preferences} up ON up.userid = u.id');
790 $this->assertEquals('snake', reset($result[0]->users
)->value
);
794 * Tests set_groups_messaging
796 * @covers \core_group::set_groups_messaging
798 public function test_set_groups_messaging() {
799 $this->resetAfterTest();
800 $this->setAdminUser();
801 $dg = $this->getDataGenerator();
802 $course = $dg->create_course();
804 // Create some groups in the course.
806 for ($i = 0; $i < 5; $i++
) {
807 $group = new \
stdClass();
808 $group->courseid
= $course->id
;
809 $group->name
= 'group-'.$i;
810 $group->enablemessaging
= 0;
811 $groupids[] = groups_create_group($group);
814 // They should all initially be disabled.
815 $alldisabledinitially = $this->check_groups_messaging_status_is($groupids, $course->id
, false);
816 $this->assertTrue($alldisabledinitially);
818 // Enable messaging for all the groups.
819 set_groups_messaging($groupids, true);
821 // Check they were all enabled.
822 $allenabled = $this->check_groups_messaging_status_is($groupids, $course->id
, true);
823 $this->assertTrue($allenabled);
825 // Disable messaging for all the groups.
826 set_groups_messaging($groupids, false);
828 // Check they were all disabled.
829 $alldisabled = $this->check_groups_messaging_status_is($groupids, $course->id
, false);
830 $this->assertTrue($alldisabled);
834 * Tests set group messaging where it doesn't exist
836 * @covers \core_group::set_groups_messaging
838 public function test_set_groups_messaging_doesnt_exist() {
839 $this->resetAfterTest();
840 $this->setAdminUser();
844 $this->expectException('dml_exception');
845 set_groups_messaging($groupids, false);
849 * Checks the given list of groups to verify their messaging settings.
851 * @param array $groupids array of group ids
852 * @param int $courseid the course the groups are in
853 * @param bool $desired the desired setting value
854 * @return bool true if all groups $enablemessaging setting matches the given $desired value, else false
856 private function check_groups_messaging_status_is(array $groupids, int $courseid, bool $desired) {
857 $context = \context_course
::instance($courseid);
859 foreach ($groupids as $groupid) {
860 $conversation = \core_message\api
::get_conversation_by_area(
867 // An empty conversation means it has not been enabled yet.
868 if (empty($conversation)) {
869 $conversation = (object) [
874 if ($desired !== boolval($conversation->enabled
)) {