Skip to content

Commit

Permalink
MDL-79692 enrol_meta: check for group validity when adding members.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Nov 13, 2023
1 parent 8ad9114 commit c7b9f29
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions enrol/meta/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ protected static function sync_with_parent_course(stdClass $instance, $userid) {
$ue->userid = $userid;
$ue->enrolid = $instance->id;
$ue->status = $parentstatus;
if ($instance->customint2) {
groups_add_member($instance->customint2, $userid, 'enrol_meta', $instance->id);
if ($instance->customint2 && $group = $DB->get_record('groups', ['id' => $instance->customint2])) {
groups_add_member($group, $userid, 'enrol_meta', $instance->id);
}
}

Expand Down Expand Up @@ -346,8 +346,8 @@ function enrol_meta_sync($courseid = NULL, $verbose = false) {
$ue->timestart = ($ue->timestart == 9999999999) ? 0 : (int)$ue->timestart;

$meta->enrol_user($instance, $ue->userid, null, $ue->timestart, $ue->timeend, $ue->status);
if ($instance->customint2) {
groups_add_member($instance->customint2, $ue->userid, 'enrol_meta', $instance->id);
if ($instance->customint2 && $group = $DB->get_record('groups', ['id' => $instance->customint2])) {
groups_add_member($group, $ue->userid, 'enrol_meta', $instance->id);
}
if ($verbose) {
mtrace(" enrolling: $ue->userid ==> $instance->courseid");
Expand Down
37 changes: 37 additions & 0 deletions enrol/meta/tests/plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

namespace enrol_meta;

use context_course;
use enrol_meta_plugin;

/**
* Meta enrolment sync functional test.
*
Expand Down Expand Up @@ -594,6 +597,40 @@ public function test_add_to_group_with_member() {

}

/**
* Test enrolling users in a course, where the customint2 (group) property of the instance points to an invalid group
*
* @covers \enrol_meta_handler::sync_with_parent_course
* @covers ::enrol_meta_sync
*/
public function test_add_to_group_invalid(): void {
$this->resetAfterTest();

$this->enable_plugin();

$courseone = $this->getDataGenerator()->create_course();
$coursetwo = $this->getDataGenerator()->create_course();

/** @var enrol_meta_plugin $plugin */
$plugin = enrol_get_plugin('meta');
$plugin->add_instance($coursetwo, ['customint1' => $courseone->id, 'customint2' => 42]);

// Ensure the event observer works for invalid groups.
$userone = $this->getDataGenerator()->create_and_enrol($courseone);

// Now disable the plugin, add another enrolment.
$this->disable_plugin();
$usertwo = $this->getDataGenerator()->create_and_enrol($courseone);

// Re-enable the plugin, run sync task - should also work for invalid groups.
$this->enable_plugin();
enrol_meta_sync($coursetwo->id);

$coursetwocontext = context_course::instance($coursetwo->id);
$this->assertTrue(is_enrolled($coursetwocontext, $userone));
$this->assertTrue(is_enrolled($coursetwocontext, $usertwo));
}

/**
* Test user_enrolment_created event.
*/
Expand Down

0 comments on commit c7b9f29

Please sign in to comment.