Skip to content

Commit

Permalink
Merge branch 'wip-MDL-47398-m27' of git://github.com/marinaglancy/moo…
Browse files Browse the repository at this point in the history
…dle into MOODLE_27_STABLE
  • Loading branch information
Sam Hemelryk committed Sep 29, 2014
2 parents 3d9daae + 3c30123 commit 1fcb39f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
14 changes: 9 additions & 5 deletions lib/testing/generator/data_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,24 +896,28 @@ public function create_grade_category($record = null) {
$this->gradecategorycounter++;
$i = $this->gradecategorycounter;

$record = (array)$record;

if (empty($record['courseid'])) {
throw new coding_exception('courseid must be present in testing::create_grade_category() $record');
}

if (!isset($record['fullname'])) {
$record['fullname'] = 'Grade category ' . $i;
}

// For gradelib classes.
require_once($CFG->libdir . '/gradelib.php');
// Create new grading category in this course.
$gradecategory = new grade_category($record, false);
$gradecategory = new grade_category(array('courseid' => $record['courseid']), false);
$gradecategory->apply_default_settings();
grade_category::set_properties($gradecategory, $record);
$gradecategory->apply_forced_settings();
$gradecategory->insert();

// This creates a default grade item for the category
$gradeitem = $gradecategory->load_grade_item();

if (isset($record->parentcategory)) {
$gradecategory->set_parent($data->parentcategory);
}

$gradecategory->update_from_db();
return $gradecategory->get_record_data();
}
Expand Down
39 changes: 30 additions & 9 deletions lib/testing/tests/generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ public function test_create() {

$scale = $generator->create_scale();
$this->assertNotEmpty($scale);

// Note we only count grade cats with depth > 1 because the course grade category
// is lazily created.
$count = $DB->count_records_select('grade_categories', 'depth <> 1');
$gradecategory = $generator->create_grade_category(array('courseid'=>$course->id));
$this->assertEquals($count+1, $DB->count_records_select('grade_categories', 'depth <> 1'));
$this->assertEquals(2, $gradecategory->depth);
$this->assertEquals($course->id, $gradecategory->courseid);
$this->assertEquals('Grade category 1', $gradecategory->fullname);
}

public function test_create_module() {
Expand Down Expand Up @@ -360,4 +351,34 @@ public function test_enrol_user() {
$result = $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
$this->assertFalse($result);
}

public function test_create_grade_category() {
global $DB, $CFG;
require_once $CFG->libdir . '/grade/constants.php';

$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course();

// Generate category and make sure number of records in DB table increases.
// Note we only count grade cats with depth > 1 because the course grade category
// is lazily created.
$count = $DB->count_records_select('grade_categories', 'depth <> 1');
$gradecategory = $generator->create_grade_category(array('courseid'=>$course->id));
$this->assertEquals($count+1, $DB->count_records_select('grade_categories', 'depth <> 1'));
$this->assertEquals(2, $gradecategory->depth);
$this->assertEquals($course->id, $gradecategory->courseid);
$this->assertEquals('Grade category 1', $gradecategory->fullname);

// Generate category and make sure aggregation is set.
$gradecategory = $generator->create_grade_category(
array('courseid' => $course->id, 'aggregation' => GRADE_AGGREGATE_MEDIAN));
$this->assertEquals(GRADE_AGGREGATE_MEDIAN, $gradecategory->aggregation);

// Generate category and make sure parent is set.
$gradecategory2 = $generator->create_grade_category(
array('courseid' => $course->id,
'parent' => $gradecategory->id));
$this->assertEquals($gradecategory->id, $gradecategory2->parent);
}
}

0 comments on commit 1fcb39f

Please sign in to comment.