MDL-49380 enrol_cohort: Unit tests and formatting update.

This commit is contained in:
Adrian Greeve 2015-03-27 09:17:23 +08:00
parent 73985b1425
commit df9dbc9f42
2 changed files with 72 additions and 9 deletions

View File

@ -324,16 +324,17 @@ function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
return false;
}
/**
* If group has been created in course returns id group, if not, create a group and return id group.
* @param int $courseid
* @param int $cohortid
* @return int $groupid
*/
/**
* Create a new group with the cohorts name.
*
* @param int $courseid
* @param int $cohortid
* @return int $groupid Group ID for this cohort.
*/
function enrol_cohort_create_new_group($courseid, $cohortid) {
global $DB;
$cohort = $DB->get_record('cohort', array('id' => $cohortid));
$groupid = $DB->get_record('groups', array('name' => $cohort->name, 'courseid' => $courseid));
global $DB;
$cohort = $DB->get_record('cohort', array('id' => $cohortid));
$groupid = $DB->get_record('groups', array('name' => $cohort->name, 'courseid' => $courseid));
if (isset($groupid->id)) {
$groupid = $groupid->id;
} else {

View File

@ -0,0 +1,62 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort enrolment sync functional test.
*
* @package enrol_cohort
* @category phpunit
* @copyright 2015 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/cohort/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
/**
* Contains tests for the cohort library.
*
* @package enrol_cohort
* @copyright 2015 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_cohort_lib_testcase extends advanced_testcase {
/**
* Test that a new group with the name of the cohort is created.
*/
public function test_enrol_cohort_create_new_group() {
global $DB;
$this->resetAfterTest();
// Create a category.
$category = $this->getDataGenerator()->create_category();
// Create a course.
$course = $this->getDataGenerator()->create_course(array('category' => $category->id));
// Create a cohort.
$cohort = $this->getDataGenerator()->create_cohort(array('context' => context_coursecat::instance($category->id)->id));
// Run the function.
$groupid = enrol_cohort_create_new_group($course->id, $cohort->id);
// Check the results.
$group = $DB->get_record('groups', array('id' => $groupid));
// The group name should match the cohort name.
$this->assertEquals($cohort->name, $group->name);
// Group course id should match the course id.
$this->assertEquals($course->id, $group->courseid);
}
}