MDL-73975 course: Remove course_search_form template
[moodle.git] / group / tests / lib_test.php
blob0b6645ddd61744ed4e8545a74add8ddb32ffc735
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.
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/>.
17 /**
18 * Unit tests for group lib.
20 * @package core_group
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_group;
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/group/lib.php');
30 require_once($CFG->dirroot . '/lib/grouplib.php');
32 /**
33 * Group lib testcase.
35 * @package core_group
36 * @copyright 2013 Frédéric Massart
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class lib_test extends \advanced_testcase {
41 public function test_member_added_event() {
42 $this->resetAfterTest();
44 $this->setAdminUser();
45 $course = $this->getDataGenerator()->create_course();
46 $user = $this->getDataGenerator()->create_user();
47 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
48 $this->getDataGenerator()->enrol_user($user->id, $course->id);
50 $sink = $this->redirectEvents();
51 groups_add_member($group->id, $user->id, 'mod_workshop', '123');
52 $events = $sink->get_events();
53 $this->assertCount(1, $events);
54 $event = reset($events);
56 $this->assertInstanceOf('\core\event\group_member_added', $event);
57 $this->assertEquals($user->id, $event->relateduserid);
58 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
59 $this->assertEquals($group->id, $event->objectid);
60 $url = new \moodle_url('/group/members.php', array('group' => $event->objectid));
61 $this->assertEquals($url, $event->get_url());
64 public function test_member_removed_event() {
65 $this->resetAfterTest();
67 $this->setAdminUser();
68 $course = $this->getDataGenerator()->create_course();
69 $user = $this->getDataGenerator()->create_user();
70 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
71 $this->getDataGenerator()->enrol_user($user->id, $course->id);
72 $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group->id));
74 $sink = $this->redirectEvents();
75 groups_remove_member($group->id, $user->id);
76 $events = $sink->get_events();
77 $this->assertCount(1, $events);
78 $event = reset($events);
80 $this->assertInstanceOf('\core\event\group_member_removed', $event);
81 $this->assertEquals($user->id, $event->relateduserid);
82 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
83 $this->assertEquals($group->id, $event->objectid);
84 $url = new \moodle_url('/group/members.php', array('group' => $event->objectid));
85 $this->assertEquals($url, $event->get_url());
88 public function test_group_created_event() {
89 $this->resetAfterTest();
91 $this->setAdminUser();
92 $course = $this->getDataGenerator()->create_course();
94 $sink = $this->redirectEvents();
95 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
96 $events = $sink->get_events();
97 $this->assertCount(1, $events);
98 $event = reset($events);
100 $this->assertInstanceOf('\core\event\group_created', $event);
101 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
102 $this->assertEquals($group->id, $event->objectid);
103 $url = new \moodle_url('/group/index.php', array('id' => $event->courseid));
104 $this->assertEquals($url, $event->get_url());
107 public function test_grouping_created_event() {
108 $this->resetAfterTest();
110 $this->setAdminUser();
111 $course = $this->getDataGenerator()->create_course();
113 $sink = $this->redirectEvents();
114 $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
115 $events = $sink->get_events();
116 $this->assertCount(1, $events);
117 $event = reset($events);
119 $this->assertInstanceOf('\core\event\grouping_created', $event);
121 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
122 $this->assertEquals($group->id, $event->objectid);
123 $url = new \moodle_url('/group/groupings.php', array('id' => $event->courseid));
124 $this->assertEquals($url, $event->get_url());
127 public function test_group_updated_event() {
128 global $DB;
130 $this->resetAfterTest();
132 $course = $this->getDataGenerator()->create_course();
133 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
135 $sink = $this->redirectEvents();
136 $data = new \stdClass();
137 $data->id = $group->id;
138 $data->courseid = $course->id;
139 $data->name = 'Backend team';
140 $this->setCurrentTimeStart();
141 groups_update_group($data);
142 $group = $DB->get_record('groups', array('id'=>$group->id)); // Fetch record with modified timestamp.
143 $events = $sink->get_events();
144 $this->assertCount(1, $events);
145 $event = reset($events);
146 $this->assertTimeCurrent($group->timemodified);
148 $this->assertInstanceOf('\core\event\group_updated', $event);
149 $group->name = $data->name;
150 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
151 $this->assertEquals($group->id, $event->objectid);
152 $url = new \moodle_url('/group/group.php', array('id' => $event->objectid));
153 $this->assertEquals($url, $event->get_url());
156 public function test_group_updated_event_does_not_require_names() {
157 global $DB;
159 $this->resetAfterTest();
161 $course = $this->getDataGenerator()->create_course();
162 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
164 $sink = $this->redirectEvents();
165 $data = new \stdClass();
166 $data->id = $group->id;
167 $data->courseid = $course->id;
168 $this->setCurrentTimeStart();
169 groups_update_group($data);
170 $group = $DB->get_record('groups', array('id'=>$group->id)); // Fetch record with modified timestamp.
171 $events = $sink->get_events();
172 $this->assertCount(1, $events);
173 $event = reset($events);
174 $this->assertTimeCurrent($group->timemodified);
176 $this->assertInstanceOf('\core\event\group_updated', $event);
177 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
178 $this->assertEquals($group->id, $event->objectid);
179 $url = new \moodle_url('/group/group.php', array('id' => $event->objectid));
180 $this->assertEquals($url, $event->get_url());
183 public function test_grouping_updated_event() {
184 global $DB;
186 $this->resetAfterTest();
188 $course = $this->getDataGenerator()->create_course();
189 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
191 $sink = $this->redirectEvents();
192 $data = new \stdClass();
193 $data->id = $grouping->id;
194 $data->courseid = $course->id;
195 $data->name = 'Backend team';
196 $this->setCurrentTimeStart();
197 groups_update_grouping($data);
198 $events = $sink->get_events();
199 $this->assertCount(1, $events);
200 $event = reset($events);
202 $this->assertInstanceOf('\core\event\grouping_updated', $event);
204 // Get the timemodified from DB for comparison with snapshot.
205 $data->timemodified = $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id));
206 $this->assertTimeCurrent($data->timemodified);
208 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
209 $this->assertEquals($grouping->id, $event->objectid);
210 $url = new \moodle_url('/group/grouping.php', array('id' => $event->objectid));
211 $this->assertEquals($url, $event->get_url());
214 public function test_grouping_updated_event_does_not_require_names() {
215 global $DB;
217 $this->resetAfterTest();
219 $course = $this->getDataGenerator()->create_course();
220 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
222 $sink = $this->redirectEvents();
223 $data = new \stdClass();
224 $data->id = $grouping->id;
225 $data->courseid = $course->id;
226 $this->setCurrentTimeStart();
227 groups_update_grouping($data);
228 $events = $sink->get_events();
229 $this->assertCount(1, $events);
230 $event = reset($events);
232 $this->assertInstanceOf('\core\event\grouping_updated', $event);
234 // Get the timemodified from DB for comparison with snapshot.
235 $data->timemodified = $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id));
236 $this->assertTimeCurrent($data->timemodified);
237 // Following fields were not updated so the snapshot should have them the same as in original group.
238 $data->description = $grouping->description;
239 $data->descriptionformat = $grouping->descriptionformat;
240 $data->configdata = $grouping->configdata;
241 $data->idnumber = $grouping->idnumber;
242 $data->name = $grouping->name;
243 $data->timecreated = $grouping->timecreated;
245 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
246 $this->assertEquals($grouping->id, $event->objectid);
247 $url = new \moodle_url('/group/grouping.php', array('id' => $event->objectid));
248 $this->assertEquals($url, $event->get_url());
251 public function test_group_deleted_event() {
252 $this->resetAfterTest();
254 $course = $this->getDataGenerator()->create_course();
255 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
257 $sink = $this->redirectEvents();
258 groups_delete_group($group->id);
259 $events = $sink->get_events();
260 $this->assertCount(1, $events);
261 $event = reset($events);
263 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
264 $this->assertEquals($group->id, $event->objectid);
265 $url = new \moodle_url('/group/index.php', array('id' => $event->courseid));
266 $this->assertEquals($url, $event->get_url());
269 public function test_grouping_deleted_event() {
270 $this->resetAfterTest();
272 $course = $this->getDataGenerator()->create_course();
273 $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
275 $sink = $this->redirectEvents();
276 groups_delete_grouping($group->id);
277 $events = $sink->get_events();
278 $this->assertCount(1, $events);
279 $event = reset($events);
281 $this->assertInstanceOf('\core\event\grouping_deleted', $event);
282 $this->assertEquals(\context_course::instance($course->id), $event->get_context());
283 $this->assertEquals($group->id, $event->objectid);
284 $url = new \moodle_url('/group/groupings.php', array('id' => $event->courseid));
285 $this->assertEquals($url, $event->get_url());
288 public function test_groups_delete_group_members() {
289 global $DB;
290 $this->resetAfterTest();
292 $course = $this->getDataGenerator()->create_course();
293 $user1 = $this->getDataGenerator()->create_user();
294 $user2 = $this->getDataGenerator()->create_user();
295 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
296 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
297 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
298 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
300 // Test deletion of all the users.
301 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
302 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
303 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
305 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
306 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
307 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
308 groups_delete_group_members($course->id);
309 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
310 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
311 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
313 // Test deletion of a specific user.
314 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
315 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
316 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
318 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
319 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
320 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
321 groups_delete_group_members($course->id, $user2->id);
322 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
323 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
324 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
327 public function test_groups_remove_member() {
328 global $DB;
329 $this->resetAfterTest();
331 $course = $this->getDataGenerator()->create_course();
332 $user1 = $this->getDataGenerator()->create_user();
333 $user2 = $this->getDataGenerator()->create_user();
334 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
335 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
336 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
337 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
339 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
340 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id));
341 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
343 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
344 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
345 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
346 groups_remove_member($group1->id, $user1->id);
347 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
348 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
349 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
350 groups_remove_member($group1->id, $user2->id);
351 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
352 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user2->id)));
353 groups_remove_member($group2->id, $user1->id);
354 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id)));
357 public function test_groups_delete_groupings_groups() {
358 global $DB;
359 $this->resetAfterTest();
361 $course = $this->getDataGenerator()->create_course();
362 $course2 = $this->getDataGenerator()->create_course();
363 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
364 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
365 $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
366 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
367 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
368 $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id));
370 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id, 'groupid' => $group1->id));
371 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id, 'groupid' => $group2->id));
372 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping2->id, 'groupid' => $group1->id));
373 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1c2->id, 'groupid' => $group1c2->id));
374 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
375 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group2->id, 'groupingid' => $grouping1->id)));
376 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping2->id)));
377 $this->assertTrue($DB->record_exists('groupings_groups',
378 array('groupid' => $group1c2->id, 'groupingid' => $grouping1c2->id)));
379 groups_delete_groupings_groups($course->id);
380 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
381 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group2->id, 'groupingid' => $grouping1->id)));
382 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping2->id)));
383 $this->assertTrue($DB->record_exists('groupings_groups',
384 array('groupid' => $group1c2->id, 'groupingid' => $grouping1c2->id)));
387 public function test_groups_delete_groups() {
388 global $DB;
389 $this->resetAfterTest();
391 $course = $this->getDataGenerator()->create_course();
392 $course2 = $this->getDataGenerator()->create_course();
393 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
394 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
395 $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
396 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
397 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
398 $user1 = $this->getDataGenerator()->create_user();
399 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
400 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
401 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id, 'groupingid' => $grouping1->id));
403 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
404 $this->assertTrue($DB->record_exists('groups', array('id' => $group2->id, 'courseid' => $course->id)));
405 $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id, 'courseid' => $course2->id)));
406 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
407 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
408 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
409 groups_delete_groups($course->id);
410 $this->assertFalse($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
411 $this->assertFalse($DB->record_exists('groups', array('id' => $group2->id, 'courseid' => $course->id)));
412 $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id, 'courseid' => $course2->id)));
413 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id)));
414 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
415 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
418 public function test_groups_delete_groupings() {
419 global $DB;
420 $this->resetAfterTest();
422 $course = $this->getDataGenerator()->create_course();
423 $course2 = $this->getDataGenerator()->create_course();
424 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
425 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
426 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
427 $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id));
428 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id, 'groupingid' => $grouping1->id));
430 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
431 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
432 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping2->id, 'courseid' => $course->id)));
433 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id, 'courseid' => $course2->id)));
434 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
435 groups_delete_groupings($course->id);
436 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id, 'courseid' => $course->id)));
437 $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping1->id, 'courseid' => $course->id)));
438 $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping2->id, 'courseid' => $course->id)));
439 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id, 'courseid' => $course2->id)));
440 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id, 'groupingid' => $grouping1->id)));
444 * Test custom field for group.
445 * @covers ::groups_create_group
446 * @covers ::groups_get_group
448 public function test_groups_with_customfield() {
449 $this->resetAfterTest();
450 $this->setAdminUser();
452 $course1 = self::getDataGenerator()->create_course();
453 $course2 = self::getDataGenerator()->create_course();
455 $groupfieldcategory = self::getDataGenerator()->create_custom_field_category([
456 'component' => 'core_group',
457 'area' => 'group',
459 $groupcustomfield = self::getDataGenerator()->create_custom_field([
460 'shortname' => 'testgroupcustomfield1',
461 'type' => 'text',
462 'categoryid' => $groupfieldcategory->get('id'),
464 $groupingfieldcategory = self::getDataGenerator()->create_custom_field_category([
465 'component' => 'core_group',
466 'area' => 'grouping',
468 $groupingcustomfield = self::getDataGenerator()->create_custom_field([
469 'shortname' => 'testgroupingcustomfield1',
470 'type' => 'text',
471 'categoryid' => $groupingfieldcategory->get('id'),
474 $group1 = self::getDataGenerator()->create_group([
475 'courseid' => $course1->id,
476 'customfield_testgroupcustomfield1' => 'Custom input for group1',
478 $group2 = self::getDataGenerator()->create_group([
479 'courseid' => $course2->id,
480 'customfield_testgroupcustomfield1' => 'Custom input for group2',
482 $grouping1 = self::getDataGenerator()->create_grouping([
483 'courseid' => $course1->id,
484 'customfield_testgroupingcustomfield1' => 'Custom input for grouping1',
486 $grouping2 = self::getDataGenerator()->create_grouping([
487 'courseid' => $course2->id,
488 'customfield_testgroupingcustomfield1' => 'Custom input for grouping2',
491 $grouphandler = \core_group\customfield\group_handler::create();
492 $data = $grouphandler->export_instance_data_object($group1->id);
493 $this->assertSame('Custom input for group1', $data->testgroupcustomfield1);
494 $data = $grouphandler->export_instance_data_object($group2->id);
495 $this->assertSame('Custom input for group2', $data->testgroupcustomfield1);
497 $groupinghandler = \core_group\customfield\grouping_handler::create();
498 $data = $groupinghandler->export_instance_data_object($grouping1->id);
499 $this->assertSame('Custom input for grouping1', $data->testgroupingcustomfield1);
500 $data = $groupinghandler->export_instance_data_object($grouping2->id);
501 $this->assertSame('Custom input for grouping2', $data->testgroupingcustomfield1);
503 $group1->customfield_testgroupcustomfield1 = 'Updated input for group1';
504 $group2->customfield_testgroupcustomfield1 = 'Updated input for group2';
505 groups_update_group($group1);
506 groups_update_group($group2);
507 $data = $grouphandler->export_instance_data_object($group1->id);
508 $this->assertSame('Updated input for group1', $data->testgroupcustomfield1);
509 $data = $grouphandler->export_instance_data_object($group2->id);
510 $this->assertSame('Updated input for group2', $data->testgroupcustomfield1);
512 $group = groups_get_group($group1->id, '*', IGNORE_MISSING, true);
513 $this->assertCount(1, $group->customfields);
514 $customfield = reset($group->customfields);
515 $this->assertSame('Updated input for group1', $customfield['value']);
517 $grouping1->customfield_testgroupingcustomfield1 = 'Updated input for grouping1';
518 $grouping2->customfield_testgroupingcustomfield1 = 'Updated input for grouping2';
519 groups_update_grouping($grouping1);
520 groups_update_grouping($grouping2);
521 $data = $groupinghandler->export_instance_data_object($grouping1->id);
522 $this->assertSame('Updated input for grouping1', $data->testgroupingcustomfield1);
523 $data = $groupinghandler->export_instance_data_object($grouping2->id);
524 $this->assertSame('Updated input for grouping2', $data->testgroupingcustomfield1);
526 $grouping = groups_get_grouping($grouping1->id, '*', IGNORE_MISSING, true);
527 $this->assertCount(1, $grouping->customfields);
528 $customfield = reset($grouping->customfields);
529 $this->assertSame('Updated input for grouping1', $customfield['value']);
532 public function test_groups_create_autogroups() {
533 global $DB;
534 $this->resetAfterTest();
536 $course = $this->getDataGenerator()->create_course();
537 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
538 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
539 $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
540 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
541 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group2->id, 'groupingid' => $grouping1->id));
542 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group3->id, 'groupingid' => $grouping1->id));
543 $user1 = $this->getDataGenerator()->create_user();
544 $user2 = $this->getDataGenerator()->create_user();
545 $user3 = $this->getDataGenerator()->create_user();
546 $user4 = $this->getDataGenerator()->create_user();
547 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
548 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
549 $this->getDataGenerator()->enrol_user($user3->id, $course->id);
550 $this->getDataGenerator()->enrol_user($user4->id, $course->id);
551 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
552 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
553 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user3->id));
554 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user4->id));
556 // Test autocreate group based on all course users.
557 $users = groups_get_potential_members($course->id);
558 $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
559 foreach ($users as $user) {
560 $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id, 'userid' => $user->id));
562 $this->assertEquals(4, $DB->count_records('groups_members', array('groupid' => $group4->id)));
564 // Test autocreate group based on existing group.
565 $source = array();
566 $source['groupid'] = $group1->id;
567 $users = groups_get_potential_members($course->id, 0, $source);
568 $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
569 foreach ($users as $user) {
570 $this->getDataGenerator()->create_group_member(array('groupid' => $group5->id, 'userid' => $user->id));
572 $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group5->id)));
574 // Test autocreate group based on existing grouping.
575 $source = array();
576 $source['groupingid'] = $grouping1->id;
577 $users = groups_get_potential_members($course->id, 0, $source);
578 $group6 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
579 foreach ($users as $user) {
580 $this->getDataGenerator()->create_group_member(array('groupid' => $group6->id, 'userid' => $user->id));
582 $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id)));
586 * Test groups_create_group enabling a group conversation.
588 public function test_groups_create_group_with_conversation() {
589 global $DB;
591 $this->resetAfterTest();
592 $this->setAdminUser();
593 $course1 = $this->getDataGenerator()->create_course();
594 $coursecontext1 = \context_course::instance($course1->id);
596 // Create two groups and only one group with enablemessaging = 1.
597 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
598 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
600 $conversations = $DB->get_records('message_conversations',
602 'contextid' => $coursecontext1->id,
603 'component' => 'core_group',
604 'itemtype' => 'groups',
605 'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
608 $this->assertCount(1, $conversations);
610 $conversation = reset($conversations);
611 // Check groupid was stored in itemid on conversation area.
612 $this->assertEquals($group1a->id, $conversation->itemid);
614 $conversations = $DB->get_records('message_conversations', ['id' => $conversation->id]);
615 $this->assertCount(1, $conversations);
617 $conversation = reset($conversations);
619 // Check group name was stored in conversation.
620 $this->assertEquals($group1a->name, $conversation->name);
624 * Test groups_update_group enabling and disabling a group conversation.
626 public function test_groups_update_group_conversation() {
627 global $DB;
629 $this->resetAfterTest();
630 $this->setAdminUser();
631 $course1 = $this->getDataGenerator()->create_course();
632 $coursecontext1 = \context_course::instance($course1->id);
634 // Create two groups and only one group with enablemessaging = 1.
635 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
636 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
638 $conversations = $DB->get_records('message_conversations',
640 'contextid' => $coursecontext1->id,
641 'component' => 'core_group',
642 'itemtype' => 'groups',
643 'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
646 $this->assertCount(1, $conversations);
648 // Check that the conversation area is created when group messaging is enabled in the course group.
649 $group1b->enablemessaging = 1;
650 groups_update_group($group1b);
652 $conversations = $DB->get_records('message_conversations',
654 'contextid' => $coursecontext1->id,
655 'component' => 'core_group',
656 'itemtype' => 'groups',
657 'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED
659 'id ASC');
660 $this->assertCount(2, $conversations);
662 $conversation1a = array_shift($conversations);
663 $conversation1b = array_shift($conversations);
665 $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]);
667 // Check for group1b that group name was stored in conversation.
668 $this->assertEquals($group1b->name, $conversation1b->name);
670 $group1b->enablemessaging = 0;
671 groups_update_group($group1b);
672 $this->assertEquals(0, $DB->get_field("message_conversations", "enabled", ['id' => $conversation1b->id]));
674 // Check that the name of the conversation is changed when the name of the course group is updated.
675 $group1b->name = 'New group name';
676 groups_update_group($group1b);
677 $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]);
678 $this->assertEquals($group1b->name, $conversation1b->name);
682 * Test groups_add_member to conversation.
684 public function test_groups_add_member_conversation() {
685 global $DB;
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' => 1));
703 // Add users to group1.
704 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
705 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
707 $conversation = \core_message\api::get_conversation_by_area(
708 'core_group',
709 'groups',
710 $group1->id,
711 $coursecontext1->id
714 // Check if the users has been added to the conversation.
715 $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
717 // Check if the user has been added to the conversation when the conversation is disabled.
718 \core_message\api::disable_conversation($conversation->id);
719 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
720 $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
724 * Test groups_remove_member to conversation.
726 public function test_groups_remove_member_conversation() {
727 global $DB;
729 $this->resetAfterTest();
731 $this->setAdminUser();
733 $course1 = $this->getDataGenerator()->create_course();
734 $coursecontext1 = \context_course::instance($course1->id);
736 $user1 = $this->getDataGenerator()->create_user();
737 $user2 = $this->getDataGenerator()->create_user();
738 $user3 = $this->getDataGenerator()->create_user();
740 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
741 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
742 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
744 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
746 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
747 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
748 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
750 $conversation = \core_message\api::get_conversation_by_area(
751 'core_group',
752 'groups',
753 $group1->id,
754 $coursecontext1->id
757 // Check if there are three users in the conversation.
758 $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
760 // Check if after removing one member in the conversation there are two members.
761 groups_remove_member($group1->id, $user1->id);
762 $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
764 // Check if the user has been removed from the conversation when the conversation is disabled.
765 \core_message\api::disable_conversation($conversation->id);
766 groups_remove_member($group1->id, $user2->id);
767 $this->assertEquals(1, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
771 * Test if you enable group messaging in a group with members these are added to the conversation.
773 public function test_add_members_group_updated_conversation_enabled() {
774 global $DB;
776 $this->resetAfterTest();
778 $this->setAdminUser();
780 $course1 = $this->getDataGenerator()->create_course();
781 $coursecontext1 = \context_course::instance($course1->id);
783 $user1 = $this->getDataGenerator()->create_user();
784 $user2 = $this->getDataGenerator()->create_user();
785 $user3 = $this->getDataGenerator()->create_user();
787 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
788 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
789 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
791 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
793 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
794 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
795 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
797 $conversation = \core_message\api::get_conversation_by_area(
798 'core_group',
799 'groups',
800 $group1->id,
801 $coursecontext1->id
804 // No conversation should exist as 'enablemessaging' was set to 0.
805 $this->assertFalse($conversation);
807 // Check that the three users are in the conversation when group messaging is enabled in the course group.
808 $group1->enablemessaging = 1;
809 groups_update_group($group1);
811 $conversation = \core_message\api::get_conversation_by_area(
812 'core_group',
813 'groups',
814 $group1->id,
815 $coursecontext1->id
818 $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
821 public function test_groups_get_members_by_role(): void {
822 $this->resetAfterTest();
824 $this->setAdminUser();
826 $course1 = $this->getDataGenerator()->create_course();
828 $user1 = $this->getDataGenerator()->create_user(['username' => 'user1', 'idnumber' => 1]);
829 $user2 = $this->getDataGenerator()->create_user(['username' => 'user2', 'idnumber' => 2]);
830 $user3 = $this->getDataGenerator()->create_user(['username' => 'user3', 'idnumber' => 3]);
832 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0);
833 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 1);
834 $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 1);
836 $group1 = $this->getDataGenerator()->create_group(['courseid' => $course1->id]);
838 $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user1->id]);
839 $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user2->id]);
840 $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $user3->id]);
842 // Test basic usage.
843 $result = groups_get_members_by_role($group1->id, $course1->id);
844 $this->assertEquals(1, count($result[0]->users));
845 $this->assertEquals(2, count($result[1]->users));
846 $this->assertEquals($user1->firstname, reset($result[0]->users)->firstname);
847 $this->assertEquals($user1->username, reset($result[0]->users)->username);
849 // Test with specified fields.
850 $result = groups_get_members_by_role($group1->id, $course1->id, 'u.firstname, u.lastname');
851 $this->assertEquals(1, count($result[0]->users));
852 $this->assertEquals($user1->firstname, reset($result[0]->users)->firstname);
853 $this->assertEquals($user1->lastname, reset($result[0]->users)->lastname);
854 $this->assertEquals(false, isset(reset($result[0]->users)->username));
856 // Test with sorting.
857 $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username', 'u.username DESC');
858 $this->assertEquals(1, count($result[0]->users));
859 $this->assertEquals($user3->username, reset($result[1]->users)->username);
860 $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username', 'u.username ASC');
861 $this->assertEquals(1, count($result[0]->users));
862 $this->assertEquals($user2->username, reset($result[1]->users)->username);
864 // Test with extra WHERE.
865 $result = groups_get_members_by_role(
866 $group1->id,
867 $course1->id,
868 'u.username',
869 null,
870 'u.idnumber > :number',
871 ['number' => 2]);
872 $this->assertEquals(1, count($result));
873 $this->assertEquals(1, count($result[1]->users));
874 $this->assertEquals($user3->username, reset($result[1]->users)->username);
876 // Test with join.
877 set_user_preference('reptile', 'snake', $user1);
878 $result = groups_get_members_by_role($group1->id, $course1->id, 'u.username, up.value', null, 'up.name = :prefname',
879 ['prefname' => 'reptile'], 'JOIN {user_preferences} up ON up.userid = u.id');
880 $this->assertEquals('snake', reset($result[0]->users)->value);
884 * Tests set_groups_messaging
886 * @covers \core_group::set_groups_messaging
888 public function test_set_groups_messaging() {
889 $this->resetAfterTest();
890 $this->setAdminUser();
891 $dg = $this->getDataGenerator();
892 $course = $dg->create_course();
894 // Create some groups in the course.
895 $groupids = [];
896 for ($i = 0; $i < 5; $i++) {
897 $group = new \stdClass();
898 $group->courseid = $course->id;
899 $group->name = 'group-'.$i;
900 $group->enablemessaging = 0;
901 $groupids[] = groups_create_group($group);
904 // They should all initially be disabled.
905 $alldisabledinitially = $this->check_groups_messaging_status_is($groupids, $course->id, false);
906 $this->assertTrue($alldisabledinitially);
908 // Enable messaging for all the groups.
909 set_groups_messaging($groupids, true);
911 // Check they were all enabled.
912 $allenabled = $this->check_groups_messaging_status_is($groupids, $course->id, true);
913 $this->assertTrue($allenabled);
915 // Disable messaging for all the groups.
916 set_groups_messaging($groupids, false);
918 // Check they were all disabled.
919 $alldisabled = $this->check_groups_messaging_status_is($groupids, $course->id, false);
920 $this->assertTrue($alldisabled);
924 * Tests set group messaging where it doesn't exist
926 * @covers \core_group::set_groups_messaging
928 public function test_set_groups_messaging_doesnt_exist() {
929 $this->resetAfterTest();
930 $this->setAdminUser();
932 $groupids = [-1];
934 $this->expectException('dml_exception');
935 set_groups_messaging($groupids, false);
939 * Checks the given list of groups to verify their messaging settings.
941 * @param array $groupids array of group ids
942 * @param int $courseid the course the groups are in
943 * @param bool $desired the desired setting value
944 * @return bool true if all groups $enablemessaging setting matches the given $desired value, else false
946 private function check_groups_messaging_status_is(array $groupids, int $courseid, bool $desired) {
947 $context = \context_course::instance($courseid);
949 foreach ($groupids as $groupid) {
950 $conversation = \core_message\api::get_conversation_by_area(
951 'core_group',
952 'groups',
953 $groupid,
954 $context->id
957 // An empty conversation means it has not been enabled yet.
958 if (empty($conversation)) {
959 $conversation = (object) [
960 'enabled' => 0
964 if ($desired !== boolval($conversation->enabled)) {
965 return false;
969 return true;