Merge branch 'wip-MDL-47398-master' of git://github.com/marinaglancy/moodle
authorSam Hemelryk <sam@moodle.com>
Mon, 29 Sep 2014 01:35:06 +0000 (14:35 +1300)
committerSam Hemelryk <sam@moodle.com>
Mon, 29 Sep 2014 01:35:06 +0000 (14:35 +1300)
lib/testing/generator/data_generator.php
lib/testing/tests/generator_test.php

index 4f361e9..9b2ef27 100644 (file)
@@ -912,6 +912,12 @@ EOD;
         $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;
         }
@@ -919,17 +925,15 @@ EOD;
         // 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();
     }
index c301fb3..911f519 100644 (file)
@@ -105,15 +105,6 @@ class core_test_generator_testcase extends advanced_testcase {
 
         $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() {
@@ -356,4 +347,34 @@ class core_test_generator_testcase extends advanced_testcase {
         $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);
+    }
 }