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
25 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->dirroot
. '/group/lib.php');
34 * @copyright 2013 Frédéric Massart
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_group_lib_testcase
extends advanced_testcase
{
39 public function test_member_added_event() {
40 $this->resetAfterTest();
42 $this->setAdminUser();
43 $course = $this->getDataGenerator()->create_course();
44 $user = $this->getDataGenerator()->create_user();
45 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
46 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
48 $sink = $this->redirectEvents();
49 groups_add_member($group->id
, $user->id
, 'mod_workshop', '123');
50 $events = $sink->get_events();
51 $this->assertCount(1, $events);
52 $event = reset($events);
54 $expected = new stdClass();
55 $expected->groupid
= $group->id
;
56 $expected->userid
= $user->id
;
57 $expected->component
= 'mod_workshop';
58 $expected->itemid
= '123';
59 $this->assertEventLegacyData($expected, $event);
60 $this->assertSame('groups_member_added', $event->get_legacy_eventname());
61 $this->assertInstanceOf('\core\event\group_member_added', $event);
62 $this->assertEquals($user->id
, $event->relateduserid
);
63 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
64 $this->assertEquals($group->id
, $event->objectid
);
65 $url = new moodle_url('/group/members.php', array('group' => $event->objectid
));
66 $this->assertEquals($url, $event->get_url());
69 public function test_member_removed_event() {
70 $this->resetAfterTest();
72 $this->setAdminUser();
73 $course = $this->getDataGenerator()->create_course();
74 $user = $this->getDataGenerator()->create_user();
75 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
76 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
77 $this->getDataGenerator()->create_group_member(array('userid' => $user->id
, 'groupid' => $group->id
));
79 $sink = $this->redirectEvents();
80 groups_remove_member($group->id
, $user->id
);
81 $events = $sink->get_events();
82 $this->assertCount(1, $events);
83 $event = reset($events);
85 $expected = new stdClass();
86 $expected->groupid
= $group->id
;
87 $expected->userid
= $user->id
;
88 $this->assertEventLegacyData($expected, $event);
89 $this->assertSame('groups_member_removed', $event->get_legacy_eventname());
90 $this->assertInstanceOf('\core\event\group_member_removed', $event);
91 $this->assertEquals($user->id
, $event->relateduserid
);
92 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
93 $this->assertEquals($group->id
, $event->objectid
);
94 $url = new moodle_url('/group/members.php', array('group' => $event->objectid
));
95 $this->assertEquals($url, $event->get_url());
98 public function test_group_created_event() {
99 $this->resetAfterTest();
101 $this->setAdminUser();
102 $course = $this->getDataGenerator()->create_course();
104 $sink = $this->redirectEvents();
105 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
106 $events = $sink->get_events();
107 $this->assertCount(1, $events);
108 $event = reset($events);
110 $this->assertInstanceOf('\core\event\group_created', $event);
111 $this->assertEventLegacyData($group, $event);
112 $this->assertSame('groups_group_created', $event->get_legacy_eventname());
113 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
114 $this->assertEquals($group->id
, $event->objectid
);
115 $url = new moodle_url('/group/index.php', array('id' => $event->courseid
));
116 $this->assertEquals($url, $event->get_url());
119 public function test_grouping_created_event() {
120 $this->resetAfterTest();
122 $this->setAdminUser();
123 $course = $this->getDataGenerator()->create_course();
125 $sink = $this->redirectEvents();
126 $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
127 $events = $sink->get_events();
128 $this->assertCount(1, $events);
129 $event = reset($events);
131 $this->assertInstanceOf('\core\event\grouping_created', $event);
133 $this->assertEventLegacyData($group, $event);
134 $this->assertSame('groups_grouping_created', $event->get_legacy_eventname());
136 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
137 $this->assertEquals($group->id
, $event->objectid
);
138 $url = new moodle_url('/group/groupings.php', array('id' => $event->courseid
));
139 $this->assertEquals($url, $event->get_url());
142 public function test_group_updated_event() {
145 $this->resetAfterTest();
147 $course = $this->getDataGenerator()->create_course();
148 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
150 $sink = $this->redirectEvents();
151 $data = new stdClass();
152 $data->id
= $group->id
;
153 $data->courseid
= $course->id
;
154 $data->name
= 'Backend team';
155 $this->setCurrentTimeStart();
156 groups_update_group($data);
157 $group = $DB->get_record('groups', array('id'=>$group->id
)); // Fetch record with modified timestamp.
158 $events = $sink->get_events();
159 $this->assertCount(1, $events);
160 $event = reset($events);
161 $this->assertTimeCurrent($group->timemodified
);
163 $this->assertInstanceOf('\core\event\group_updated', $event);
164 $group->name
= $data->name
;
165 $this->assertEventLegacyData($group, $event);
166 $this->assertSame('groups_group_updated', $event->get_legacy_eventname());
167 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
168 $this->assertEquals($group->id
, $event->objectid
);
169 $url = new moodle_url('/group/group.php', array('id' => $event->objectid
));
170 $this->assertEquals($url, $event->get_url());
173 public function test_group_updated_event_does_not_require_names() {
176 $this->resetAfterTest();
178 $course = $this->getDataGenerator()->create_course();
179 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
181 $sink = $this->redirectEvents();
182 $data = new stdClass();
183 $data->id
= $group->id
;
184 $data->courseid
= $course->id
;
185 $this->setCurrentTimeStart();
186 groups_update_group($data);
187 $group = $DB->get_record('groups', array('id'=>$group->id
)); // Fetch record with modified timestamp.
188 $events = $sink->get_events();
189 $this->assertCount(1, $events);
190 $event = reset($events);
191 $this->assertTimeCurrent($group->timemodified
);
193 $this->assertInstanceOf('\core\event\group_updated', $event);
194 $this->assertEventLegacyData($group, $event);
195 $this->assertSame('groups_group_updated', $event->get_legacy_eventname());
196 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
197 $this->assertEquals($group->id
, $event->objectid
);
198 $url = new moodle_url('/group/group.php', array('id' => $event->objectid
));
199 $this->assertEquals($url, $event->get_url());
202 public function test_grouping_updated_event() {
205 $this->resetAfterTest();
207 $course = $this->getDataGenerator()->create_course();
208 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
210 $sink = $this->redirectEvents();
211 $data = new stdClass();
212 $data->id
= $grouping->id
;
213 $data->courseid
= $course->id
;
214 $data->name
= 'Backend team';
215 $this->setCurrentTimeStart();
216 groups_update_grouping($data);
217 $events = $sink->get_events();
218 $this->assertCount(1, $events);
219 $event = reset($events);
221 $this->assertInstanceOf('\core\event\grouping_updated', $event);
223 // Get the timemodified from DB for comparison with snapshot.
224 $data->timemodified
= $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id
));
225 $this->assertTimeCurrent($data->timemodified
);
226 // Following fields were not updated so the snapshot should have them the same as in original group.
227 $data->description
= $grouping->description
;
228 $data->descriptionformat
= $grouping->descriptionformat
;
229 $data->configdata
= $grouping->configdata
;
230 $data->idnumber
= $grouping->idnumber
;
231 $data->timecreated
= $grouping->timecreated
;
232 // Assert legacy event data.
233 $this->assertEventLegacyData($data, $event);
234 $this->assertSame('groups_grouping_updated', $event->get_legacy_eventname());
236 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
237 $this->assertEquals($grouping->id
, $event->objectid
);
238 $url = new moodle_url('/group/grouping.php', array('id' => $event->objectid
));
239 $this->assertEquals($url, $event->get_url());
242 public function test_grouping_updated_event_does_not_require_names() {
245 $this->resetAfterTest();
247 $course = $this->getDataGenerator()->create_course();
248 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
250 $sink = $this->redirectEvents();
251 $data = new stdClass();
252 $data->id
= $grouping->id
;
253 $data->courseid
= $course->id
;
254 $this->setCurrentTimeStart();
255 groups_update_grouping($data);
256 $events = $sink->get_events();
257 $this->assertCount(1, $events);
258 $event = reset($events);
260 $this->assertInstanceOf('\core\event\grouping_updated', $event);
262 // Get the timemodified from DB for comparison with snapshot.
263 $data->timemodified
= $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id
));
264 $this->assertTimeCurrent($data->timemodified
);
265 // Following fields were not updated so the snapshot should have them the same as in original group.
266 $data->description
= $grouping->description
;
267 $data->descriptionformat
= $grouping->descriptionformat
;
268 $data->configdata
= $grouping->configdata
;
269 $data->idnumber
= $grouping->idnumber
;
270 $data->name
= $grouping->name
;
271 $data->timecreated
= $grouping->timecreated
;
272 // Assert legacy event data.
273 $this->assertEventLegacyData($data, $event);
274 $this->assertSame('groups_grouping_updated', $event->get_legacy_eventname());
276 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
277 $this->assertEquals($grouping->id
, $event->objectid
);
278 $url = new moodle_url('/group/grouping.php', array('id' => $event->objectid
));
279 $this->assertEquals($url, $event->get_url());
282 public function test_group_deleted_event() {
283 $this->resetAfterTest();
285 $course = $this->getDataGenerator()->create_course();
286 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
288 $sink = $this->redirectEvents();
289 groups_delete_group($group->id
);
290 $events = $sink->get_events();
291 $this->assertCount(1, $events);
292 $event = reset($events);
294 $this->assertInstanceOf('\core\event\group_deleted', $event);
295 $this->assertEventLegacyData($group, $event);
296 $this->assertSame('groups_group_deleted', $event->get_legacy_eventname());
297 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
298 $this->assertEquals($group->id
, $event->objectid
);
299 $url = new moodle_url('/group/index.php', array('id' => $event->courseid
));
300 $this->assertEquals($url, $event->get_url());
303 public function test_grouping_deleted_event() {
304 $this->resetAfterTest();
306 $course = $this->getDataGenerator()->create_course();
307 $group = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
309 $sink = $this->redirectEvents();
310 groups_delete_grouping($group->id
);
311 $events = $sink->get_events();
312 $this->assertCount(1, $events);
313 $event = reset($events);
315 $this->assertInstanceOf('\core\event\grouping_deleted', $event);
316 $this->assertEventLegacyData($group, $event);
317 $this->assertSame('groups_grouping_deleted', $event->get_legacy_eventname());
318 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
319 $this->assertEquals($group->id
, $event->objectid
);
320 $url = new moodle_url('/group/groupings.php', array('id' => $event->courseid
));
321 $this->assertEquals($url, $event->get_url());
324 public function test_groups_delete_group_members() {
326 $this->resetAfterTest();
328 $course = $this->getDataGenerator()->create_course();
329 $user1 = $this->getDataGenerator()->create_user();
330 $user2 = $this->getDataGenerator()->create_user();
331 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
332 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
);
333 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
334 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
336 // Test deletion of all the users.
337 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
338 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
));
339 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
341 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
342 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
343 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
344 groups_delete_group_members($course->id
);
345 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
346 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
347 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
349 // Test deletion of a specific user.
350 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
351 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
));
352 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
354 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
355 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
356 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
357 groups_delete_group_members($course->id
, $user2->id
);
358 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
359 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
360 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
363 public function test_groups_remove_member() {
365 $this->resetAfterTest();
367 $course = $this->getDataGenerator()->create_course();
368 $user1 = $this->getDataGenerator()->create_user();
369 $user2 = $this->getDataGenerator()->create_user();
370 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
371 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
);
372 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
373 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
375 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
376 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user1->id
));
377 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
379 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
380 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
381 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
382 groups_remove_member($group1->id
, $user1->id
);
383 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
384 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
385 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
386 groups_remove_member($group1->id
, $user2->id
);
387 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
388 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user2->id
)));
389 groups_remove_member($group2->id
, $user1->id
);
390 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id
, 'userid' => $user1->id
)));
393 public function test_groups_delete_groupings_groups() {
395 $this->resetAfterTest();
397 $course = $this->getDataGenerator()->create_course();
398 $course2 = $this->getDataGenerator()->create_course();
399 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
400 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
401 $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id
));
402 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
403 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
404 $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id
));
406 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id
, 'groupid' => $group1->id
));
407 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1->id
, 'groupid' => $group2->id
));
408 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping2->id
, 'groupid' => $group1->id
));
409 $this->getDataGenerator()->create_grouping_group(array('groupingid' => $grouping1c2->id
, 'groupid' => $group1c2->id
));
410 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
411 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group2->id
, 'groupingid' => $grouping1->id
)));
412 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping2->id
)));
413 $this->assertTrue($DB->record_exists('groupings_groups',
414 array('groupid' => $group1c2->id
, 'groupingid' => $grouping1c2->id
)));
415 groups_delete_groupings_groups($course->id
);
416 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
417 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group2->id
, 'groupingid' => $grouping1->id
)));
418 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping2->id
)));
419 $this->assertTrue($DB->record_exists('groupings_groups',
420 array('groupid' => $group1c2->id
, 'groupingid' => $grouping1c2->id
)));
423 public function test_groups_delete_groups() {
425 $this->resetAfterTest();
427 $course = $this->getDataGenerator()->create_course();
428 $course2 = $this->getDataGenerator()->create_course();
429 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
430 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
431 $group1c2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id
));
432 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
433 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
434 $user1 = $this->getDataGenerator()->create_user();
435 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
436 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
437 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
));
439 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
440 $this->assertTrue($DB->record_exists('groups', array('id' => $group2->id
, 'courseid' => $course->id
)));
441 $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id
, 'courseid' => $course2->id
)));
442 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
443 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
444 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
445 groups_delete_groups($course->id
);
446 $this->assertFalse($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
447 $this->assertFalse($DB->record_exists('groups', array('id' => $group2->id
, 'courseid' => $course->id
)));
448 $this->assertTrue($DB->record_exists('groups', array('id' => $group1c2->id
, 'courseid' => $course2->id
)));
449 $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id
, 'userid' => $user1->id
)));
450 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
451 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
454 public function test_groups_delete_groupings() {
456 $this->resetAfterTest();
458 $course = $this->getDataGenerator()->create_course();
459 $course2 = $this->getDataGenerator()->create_course();
460 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
461 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
462 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
463 $grouping1c2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course2->id
));
464 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
));
466 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
467 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
468 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping2->id
, 'courseid' => $course->id
)));
469 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id
, 'courseid' => $course2->id
)));
470 $this->assertTrue($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
471 groups_delete_groupings($course->id
);
472 $this->assertTrue($DB->record_exists('groups', array('id' => $group1->id
, 'courseid' => $course->id
)));
473 $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping1->id
, 'courseid' => $course->id
)));
474 $this->assertFalse($DB->record_exists('groupings', array('id' => $grouping2->id
, 'courseid' => $course->id
)));
475 $this->assertTrue($DB->record_exists('groupings', array('id' => $grouping1c2->id
, 'courseid' => $course2->id
)));
476 $this->assertFalse($DB->record_exists('groupings_groups', array('groupid' => $group1->id
, 'groupingid' => $grouping1->id
)));
479 public function test_groups_create_autogroups () {
481 $this->resetAfterTest();
483 $course = $this->getDataGenerator()->create_course();
484 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
485 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
486 $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
487 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
488 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group2->id
, 'groupingid' => $grouping1->id
));
489 $this->getDataGenerator()->create_grouping_group(array('groupid' => $group3->id
, 'groupingid' => $grouping1->id
));
490 $user1 = $this->getDataGenerator()->create_user();
491 $user2 = $this->getDataGenerator()->create_user();
492 $user3 = $this->getDataGenerator()->create_user();
493 $user4 = $this->getDataGenerator()->create_user();
494 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
495 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
);
496 $this->getDataGenerator()->enrol_user($user3->id
, $course->id
);
497 $this->getDataGenerator()->enrol_user($user4->id
, $course->id
);
498 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user1->id
));
499 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id
, 'userid' => $user2->id
));
500 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id
, 'userid' => $user3->id
));
501 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id
, 'userid' => $user4->id
));
503 // Test autocreate group based on all course users.
504 $users = groups_get_potential_members($course->id
);
505 $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
506 foreach ($users as $user) {
507 $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id
, 'userid' => $user->id
));
509 $this->assertEquals(4, $DB->count_records('groups_members', array('groupid' => $group4->id
)));
511 // Test autocreate group based on existing group.
513 $source['groupid'] = $group1->id
;
514 $users = groups_get_potential_members($course->id
, 0, $source);
515 $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
516 foreach ($users as $user) {
517 $this->getDataGenerator()->create_group_member(array('groupid' => $group5->id
, 'userid' => $user->id
));
519 $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group5->id
)));
521 // Test autocreate group based on existing grouping.
523 $source['groupingid'] = $grouping1->id
;
524 $users = groups_get_potential_members($course->id
, 0, $source);
525 $group6 = $this->getDataGenerator()->create_group(array('courseid' => $course->id
));
526 foreach ($users as $user) {
527 $this->getDataGenerator()->create_group_member(array('groupid' => $group6->id
, 'userid' => $user->id
));
529 $this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id
)));