From 32b97a03f3cd20bb351b04d7cbc35168cfea8f17 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 6 Apr 2015 10:47:54 +0800 Subject: [PATCH] MDL-49380 enrol_cohort: Fixing matches Tiny fix as the group name was not properly generated when incrementing the captured generated number. Also adding unit test for this case. --- enrol/cohort/lib.php | 4 ++-- enrol/cohort/tests/cohortlib_test.php | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index 571d01dbd4f..55e8a04c08b 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -350,7 +350,7 @@ function enrol_cohort_create_new_group($courseid, $cohortid) { if (!preg_match('/(.*?)\(([0-9]+)\)$/', $groupname, $matches)) { $a->increment = '(2)'; } else { - $a->increment = '(' . $matches[2]+1 . ')'; + $a->increment = '(' . ($matches[2] + 1) . ')'; } $newshortname = get_string('defaultgroupnametext', 'enrol_cohort', $a); $groupname = $newshortname; @@ -362,4 +362,4 @@ function enrol_cohort_create_new_group($courseid, $cohortid) { $groupid = groups_create_group($groupdata); return $groupid; -} \ No newline at end of file +} diff --git a/enrol/cohort/tests/cohortlib_test.php b/enrol/cohort/tests/cohortlib_test.php index 4850eaa2021..781c5e6c0eb 100644 --- a/enrol/cohort/tests/cohortlib_test.php +++ b/enrol/cohort/tests/cohortlib_test.php @@ -18,7 +18,7 @@ * Cohort enrolment sync functional test. * * @package enrol_cohort - * @category phpunit + * @category test * @copyright 2015 Adrian Greeve * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -60,7 +60,7 @@ class enrol_cohort_lib_testcase extends advanced_testcase { // Group course id should match the course id. $this->assertEquals($course->id, $group->courseid); - // Create a group that will have the same name as the cohort + // Create a group that will have the same name as the cohort. $groupdata = new stdClass(); $groupdata->courseid = $course2->id; $groupdata->name = $cohort->name . ' cohort'; @@ -70,5 +70,17 @@ class enrol_cohort_lib_testcase extends advanced_testcase { $groupinfo = $DB->get_record('groups', array('id' => $groupid)); // Check that the group name has been changed. $this->assertEquals($cohort->name . ' cohort(2)', $groupinfo->name); + + // Create another group that will have the same name as a generated cohort. + $groupdata = new stdClass(); + $groupdata->courseid = $course2->id; + $groupdata->name = $cohort->name . ' cohort(2)'; + groups_create_group($groupdata); + // Create a group for the cohort in course 2. + $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id); + $groupinfo = $DB->get_record('groups', array('id' => $groupid)); + // Check that the group name has been changed. + $this->assertEquals($cohort->name . ' cohort(3)', $groupinfo->name); + } -} \ No newline at end of file +}