diff --git a/classes/sort_module/user_info_field.php b/classes/sort_module/user_info_field.php index f6f7a63..02003cc 100644 --- a/classes/sort_module/user_info_field.php +++ b/classes/sort_module/user_info_field.php @@ -96,10 +96,22 @@ public function eligible_groups_for_user(stdClass $user) { $field = $this->field; $data = $DB->get_record('user_info_data', ['fieldid' => $field, 'userid' => $user->id]); - if ($data && !empty($data->data)) { - return [$data->data]; + if ($data && !empty($data->data)){ + // Check if the field has comma separated values. + if ($fieldvalues = explode(',', $data->data)) { + $fields = []; + foreach ($fieldvalues as $fieldvalue) { + if ($fieldvalue = trim($fieldvalue)) { + $fields[] = $fieldvalue; + } + } + return $fields; + } else { + return [$data->data]; + } + } else { + return []; } - return []; } /** diff --git a/classes/usecase/verify_course_group_membership.php b/classes/usecase/verify_course_group_membership.php index c5ceacd..4495356 100644 --- a/classes/usecase/verify_course_group_membership.php +++ b/classes/usecase/verify_course_group_membership.php @@ -67,7 +67,7 @@ public function invoke() { return false; } - set_time_limit(500); + !PHPUNIT_TEST ? set_time_limit(500) : null; return $this->course->verify_all_group_membership($this->db); } } diff --git a/tests/local_autogroup_lib_test.php b/tests/local_autogroup_lib_test.php new file mode 100644 index 0000000..559bf0f --- /dev/null +++ b/tests/local_autogroup_lib_test.php @@ -0,0 +1,183 @@ +. +/** + * Tests for local_autogroup. + * + * @package local_autogroup + * @category test + * @copyright 2021 My Learning Consultants + * @author David Saylor + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot.'/user/profile/lib.php'); +require_once($CFG->dirroot.'/user/profile/definelib.php'); + +/** + * Test class for local_autogroup_lib. + * + * @package local_autogroup + * @category test + * @copyright 2021 My Learning Consultants + * @author David Saylor + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class local_autogroup_lib_test extends advanced_testcase { + /** + * Test setup. + */ + public function setUp(): void { + $this->resetAfterTest(); + } + + /** + * A completed course with no equivalents being complete and triggering recompletion notifications + * and also expiration.. + */ + public function test_multiple_profile_live_values() { + global $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $fieldid = $this->create_profile_field(); + + set_config('enabled', true, 'local_autogroup'); + set_config('addtonewcourses', true, 'local_autogroup'); + set_config('filter', $fieldid, 'local_autogroup'); + set_config('adhoceventhandler', false, 'local_autogroup'); + + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $user = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student'); + profile_save_custom_fields($user->id, array('test' => 'Test 1, Test 2, Test 3')); + user_update_user($user, false, true); + + $groups = groups_get_all_groups($course->id, $user->id); + $this->assertCount(3, $groups); + $count = 1; + foreach ($groups as $group) { + $this->assertEquals('Test ' . $count, $group->name); + $count++; + } + } + + /** + * Same as above but with adhoc event handler. + */ + public function test_multiple_profile_adhoc_values() { + global $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $fieldid = $this->create_profile_field(); + + set_config('enabled', true, 'local_autogroup'); + set_config('addtonewcourses', true, 'local_autogroup'); + set_config('filter', $fieldid, 'local_autogroup'); + set_config('adhoceventhandler', true, 'local_autogroup'); + + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $user = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student'); + profile_save_custom_fields($user->id, array('test' => 'Test 1, Test 2, Test 3')); + user_update_user($user, false, true); + + $groups = groups_get_all_groups($course->id, $user->id); + $this->assertCount(0, $groups); + + $this->execute_adhoc(); + $groups = groups_get_all_groups($course->id, $user->id); + $this->assertCount(3, $groups); + $count = 1; + foreach ($groups as $group) { + $this->assertEquals('Test ' . $count, $group->name); + $count++; + } + } + + /** + * Create a profile field. + * + * @return int + */ + private function create_profile_field() { + global $CFG, $DB, $PAGE; + + $PAGE->set_context(context_system::instance()); + + $data = (object) [ + 'name' => 'Test' + ]; + $data->sortorder = $DB->count_records('user_info_category') + 1; + $data->id = $DB->insert_record('user_info_category', $data, true); + $createdcategory = $DB->get_record('user_info_category', array('id' => $data->id)); + \core\event\user_info_category_created::create_from_category($createdcategory)->trigger(); + + $field = (object) [ + 'shortname' => 'test', + 'name' => 'Test', + 'datatype' => 'text', + 'description' => 'A test field', + 'descriptionformat' => 1, + 'categoryid' => 6, + 'sortorder' => 2, + 'required' => 0, + 'locked' => 1, + 'visible' => 0, + 'forceunique' => 0, + 'signup' => 1, + 'defaultdata' => '', + 'defaultdataformat' => 0, + 'param1' => '30', + 'param2' => '100', + 'param3' => '0', + 'param4' => '', + 'param5' => '', + ]; + + $field->id = $DB->get_field('user_info_field', 'id', ['shortname' => $field->shortname]); + require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/define.class.php'); + $newfield = 'profile_define_'.$field->datatype; + $formfield = new $newfield(); + $field->categoryid = $createdcategory->id; + $formfield->define_save($field); + + profile_reorder_fields(); + profile_reorder_categories(); + + return $field->id; + } + + /** + * Execute adhoc tasks. + */ + public function execute_adhoc() { + global $DB; + $tasks = $DB->get_records('task_adhoc', ['component' => 'local_autogroup']); + foreach ($tasks as $taskinfo) { + $task = new \local_autogroup\task\process_event(); + $task->set_custom_data(json_decode($taskinfo->customdata)); + $task->execute(); + $DB->delete_records('task_adhoc', ['id' => $taskinfo->id]); + } + } +} \ No newline at end of file