MDL-35465 add basic cohort lib tests and generator

This commit is contained in:
Petr Škoda
2012-09-16 20:23:19 +02:00
parent 88a119bb48
commit 4729332b15
4 changed files with 243 additions and 2 deletions
+47 -2
View File
@@ -36,6 +36,7 @@
class phpunit_data_generator {
protected $usercounter = 0;
protected $categorycount = 0;
protected $cohortcount = 0;
protected $coursecount = 0;
protected $scalecount = 0;
protected $groupcount = 0;
@@ -218,7 +219,7 @@ EOD;
* @param array $options
* @return stdClass course category record
*/
function create_category($record=null, array $options=null) {
public function create_category($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
@@ -270,6 +271,50 @@ EOD;
return $DB->get_record('course_categories', array('id'=>$catid), '*', MUST_EXIST);
}
/**
* Create test cohort.
* @param array|stdClass $record
* @param array $options
* @return stdClass cohort record
*/
public function create_cohort($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/cohort/lib.php");
$this->cohortcount++;
$i = $this->cohortcount;
$record = (array)$record;
if (!isset($record['contextid'])) {
$record['contextid'] = context_system::instance()->id;
}
if (!isset($record['name'])) {
$record['name'] = 'Cohort '.$i;
}
if (!isset($record['idnumber'])) {
$record['idnumber'] = '';
}
if (!isset($record['description'])) {
$record['description'] = "Test cohort $i\n$this->loremipsum";
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
if (!isset($record['component'])) {
$record['component'] = '';
}
$id = cohort_add_cohort((object)$record);
return $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
}
/**
* Create a test course
* @param array|stdClass $record
@@ -277,7 +322,7 @@ EOD;
* 'createsections'=>bool precreate all sections
* @return stdClass course record
*/
function create_course($record=null, array $options=null) {
public function create_course($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
+12
View File
@@ -53,6 +53,18 @@ class core_phpunit_generator_testcase extends advanced_testcase {
$this->assertRegExp('/^Test course category \d/', $category->description);
$this->assertSame(FORMAT_MOODLE, $category->descriptionformat);
$count = $DB->count_records('cohort');
$cohort = $generator->create_cohort();
$this->assertEquals($count+1, $DB->count_records('cohort'));
$this->assertEquals(context_system::instance()->id, $cohort->contextid);
$this->assertRegExp('/^Cohort \d/', $cohort->name);
$this->assertSame('', $cohort->idnumber);
$this->assertRegExp('/^Test cohort \d/', $cohort->description);
$this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat);
$this->assertSame('', $cohort->component);
$this->assertLessThanOrEqual(time(), $cohort->timecreated);
$this->assertSame($cohort->timecreated, $cohort->timemodified);
$count = $DB->count_records('course');
$course = $generator->create_course();
$this->assertEquals($count+1, $DB->count_records('course'));