From f151b073936118a2ff6aced31ba4f9eb15f9aa2e Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Mon, 7 May 2007 07:33:11 +0000 Subject: [PATCH] MDL-9506 Added category's associated grade_item object, and changed unit tests database data so that it creates its own test tables. --- lib/grade/grade_category.php | 45 ++++- .../grade/simpletest/testgradecategory.php | 9 +- .../grade/simpletest/testgradeoutcome.php | 1 - lib/simpletest/testgradelib.php | 162 ++++++++++++------ 4 files changed, 155 insertions(+), 62 deletions(-) diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 341bc3f718..d3458dd9d0 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -106,13 +106,28 @@ class grade_category extends grade_object { */ var $all_children; + /** + * An associated grade_item object, with itemtype=category, used to calculate and cache a set of grade values + * for this category. + * @var object $grade_item + */ + var $grade_item; + /** * Constructor. Extends the basic functionality defined in grade_object. * @param array $params Can also be a standard object. - * @param boolean $fetch Wether or not to fetch the corresponding row from the DB. + * @param boolean $fetch Whether or not to fetch the corresponding row from the DB. + * @param object $grade_item The associated grade_item object can be passed during construction. */ - function grade_category($params=NULL, $fetch=true) { + function grade_category($params=NULL, $fetch=true, $grade_item=NULL) { $this->grade_object($params, $fetch); + if (!empty($grade_item) && $grade_item->itemtype == 'category') { + $this->grade_item = $grade_item; + if (empty($this->grade_item->iteminstance)) { + $this->grade_item->iteminstance = $this->id; + $this->grade_item->update(); + } + } } @@ -169,6 +184,7 @@ class grade_category extends grade_object { * and path for this object, and update the record accordingly. The reason why this must * be done here instead of in the constructor, is that they both need to know the record's * id number, which only gets created at insertion time. + * This method also creates an associated grade_item if this wasn't done during construction. */ function insert() { $result = parent::insert(); @@ -183,6 +199,15 @@ class grade_category extends grade_object { } $this->update(); + + if (empty($this->grade_item)) { + $grade_item = new grade_item(); + $grade_item->iteminstance = $this->id; + $grade_item->itemtype = 'category'; + $result = $result & $grade_item->insert(); + $this->grade_item = $grade_item; + } + return $result; } @@ -220,14 +245,15 @@ class grade_category extends grade_object { } $childrentype = $this->get_childrentype(); - + if ($childrentype == 'grade_item') { - $children = get_records('grade_items', 'categoryid', $this->id, 'id'); + $children = get_records('grade_items', 'categoryid', $this->id); // No need to proceed with recursion $children_array = $this->children_to_array($children, $arraytype, 'grade_item'); $this->children = $this->children_to_array($children, 'flat', 'grade_item'); } elseif ($childrentype == 'grade_category') { $children = get_records('grade_categories', 'parent', $this->id, 'id'); + if ($depth == 1) { $children_array = $this->children_to_array($children, $arraytype, 'grade_category'); $this->children = $this->children_to_array($children, 'flat', 'grade_category'); @@ -321,6 +347,7 @@ class grade_category extends grade_object { if (empty($this->children)) { $count_item_children = count_records('grade_items', 'categoryid', $this->id); $count_cat_children = count_records('grade_categories', 'parent', $this->id); + if ($count_item_children > 0) { return 'grade_item'; } elseif ($count_cat_children > 0) { @@ -331,6 +358,16 @@ class grade_category extends grade_object { } return get_class($children[0]); } + + /** + * Retrieves from DB, instantiates and saves the associated grade_item object. + * @return object Grade_item + */ + function load_grade_item() { + $params = get_record('grade_items', 'categoryid', $this->id, 'itemtype', 'category'); + $this->grade_item = new grade_item($params); + return $this->grade_item; + } } ?> diff --git a/lib/simpletest/grade/simpletest/testgradecategory.php b/lib/simpletest/grade/simpletest/testgradecategory.php index 838c06dfff..65ab826980 100755 --- a/lib/simpletest/grade/simpletest/testgradecategory.php +++ b/lib/simpletest/grade/simpletest/testgradecategory.php @@ -40,12 +40,13 @@ class grade_category_test extends gradelib_test { $params = new stdClass(); $params->courseid = $this->courseid; - $params->fullname = 'unittestcategory4'; + $params->fullname = 'unittestcategory8'; $grade_category = new grade_category($params, false); $grade_category->insert(); - $this->grade_categories[] = $grade_category; + $this->grade_categories[] = $grade_category; + $this->grade_items[] = $grade_category->grade_item; $this->assertEqual($params->courseid, $grade_category->courseid); $this->assertEqual($params->fullname, $grade_category->fullname); $this->assertEqual(1, $grade_category->depth); @@ -58,6 +59,7 @@ class grade_category_test extends gradelib_test { $grade_category = new grade_category($params, false); $grade_category->insert(); $this->grade_categories[] = $grade_category; + $this->grade_items[] = $grade_category->grade_item; $this->assertEqual(2, $grade_category->depth); $this->assertEqual("$parentpath/$grade_category->id", $grade_category->path); $parentpath = $grade_category->path; @@ -68,6 +70,7 @@ class grade_category_test extends gradelib_test { $grade_category = new grade_category($params, false); $grade_category->insert(); $this->grade_categories[] = $grade_category; + $this->grade_items[] = $grade_category->grade_item; $this->assertEqual(3, $grade_category->depth); $this->assertEqual("$parentpath/$grade_category->id", $grade_category->path); } @@ -92,7 +95,7 @@ class grade_category_test extends gradelib_test { $this->assertTrue(!empty($grade_category->timecreated)); $this->assertTrue(!empty($grade_category->timemodified)); $this->grade_categories[] = $grade_category; - + $this->grade_items[] = $grade_category->grade_item; } function test_grade_category_update() { diff --git a/lib/simpletest/grade/simpletest/testgradeoutcome.php b/lib/simpletest/grade/simpletest/testgradeoutcome.php index af75394a4b..c5d113fbf8 100644 --- a/lib/simpletest/grade/simpletest/testgradeoutcome.php +++ b/lib/simpletest/grade/simpletest/testgradeoutcome.php @@ -62,7 +62,6 @@ class grade_outcome_test extends gradelib_test { $this->assertFalse(empty($grade_outcome->timecreated)); $this->assertFalse(empty($grade_outcome->timemodified)); $this->grade_outcomes[] = $grade_outcome; - } function test_grade_outcome_update() { diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 139291c80e..a065957f53 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -38,17 +38,7 @@ global $CFG; require_once($CFG->libdir . '/simpletestlib.php'); require_once($CFG->libdir . '/gradelib.php'); require_once($CFG->libdir . '/dmllib.php'); - -/** - * A cleanup of the tables is a good idea before we start, in case the last unit test - * crashed before running its tearDown method. Be careful because ANY record matching - * this search (%unittest%) will be deleted! Maybe a good idea to switch this off in - * production environment. - */ -delete_records_select('grade_categories', 'fullname LIKE "%unittest%"'); -delete_records_select('grade_items', 'itemname LIKE "%unittest%"'); -delete_records_select('grade_calculation', 'calculation LIKE "%unittest%"'); -delete_records_select('scale', 'name LIKE "%unittest%"'); +require_once($CFG->libdir . '/ddllib.php'); /** * Here is a brief explanation of the test data set up in these unit tests. @@ -87,32 +77,37 @@ class gradelib_test extends UnitTestCase { var $userid = 1; /** - * Create temporary entries in the database for these tests. - * These tests have to work no matter the data currently in the database - * (meaning they should run on a brand new site). This means several items of - * data have to be artificially inseminated (:-) in the DB. + * Create temporary test tables and entries in the database for these tests. + * These tests have to work on a brand new site. + * Override $CFG->prefix while these tests run. */ function setUp() { + global $CFG; + global $db; + // $db->debug=true; + $CFG->old_prefix = $CFG->prefix; + $CFG->prefix .= 'unittest_'; foreach ($this->tables as $table) { + execute_sql("CREATE TABLE IF NOT EXISTS {$CFG->prefix}$table LIKE {$CFG->old_prefix}$table", false); $function = "load_$table"; $this->$function(); } } /** - * Delete temporary entries from the database + * Drop test tables from DB. + * Restore original $CFG->prefix. */ function tearDown() { + global $CFG; foreach ($this->tables as $table) { - foreach ($this->$table as $object) { - delete_records($table, 'id', $object->id); - } - + execute_sql("TRUNCATE TABLE {$CFG->prefix}$table", false); // If data has been entered in DB for any table, unset corresponding array if (count($this->$table) > 0) { unset ($this->$table); } } + $CFG->prefix = $CFG->old_prefix; } /** @@ -222,7 +217,51 @@ class gradelib_test extends UnitTestCase { if ($grade_item->id = insert_record('grade_items', $grade_item)) { $this->grade_items[] = $grade_item; - } + } + + // Load grade_items associated with the 3 categories + $grade_item = new stdClass(); + + $grade_item->courseid = $this->courseid; + $grade_item->iteminstance = $this->grade_categories[0]->id; + $grade_item->itemname = 'unittestgradeitemcategory1'; + $grade_item->itemtype = 'category'; + $grade_item->iteminfo = 'Grade item used for unit testing'; + $grade_item->timecreated = mktime(); + $grade_item->timemodified = mktime(); + + if ($grade_item->id = insert_record('grade_items', $grade_item)) { + $this->grade_items[] = $grade_item; + } + + $grade_item = new stdClass(); + + $grade_item->courseid = $this->courseid; + $grade_item->iteminstance = $this->grade_categories[1]->id; + $grade_item->itemname = 'unittestgradeitemcategory2'; + $grade_item->itemtype = 'category'; + $grade_item->iteminfo = 'Grade item used for unit testing'; + $grade_item->timecreated = mktime(); + $grade_item->timemodified = mktime(); + + if ($grade_item->id = insert_record('grade_items', $grade_item)) { + $this->grade_items[] = $grade_item; + } + + $grade_item = new stdClass(); + + $grade_item->courseid = $this->courseid; + $grade_item->iteminstance = $this->grade_categories[2]->id; + $grade_item->itemname = 'unittestgradeitemcategory3'; + $grade_item->itemtype = 'category'; + $grade_item->iteminfo = 'Grade item used for unit testing'; + $grade_item->timecreated = mktime(); + $grade_item->timemodified = mktime(); + + if ($grade_item->id = insert_record('grade_items', $grade_item)) { + $this->grade_items[] = $grade_item; + } + } /** @@ -644,49 +683,64 @@ class gradelib_test extends UnitTestCase { // API FUNCTIONS function test_grade_get_items() { - $grade_items = grade_get_items($this->courseid); + if (get_class($this) == 'gradelib_test') { + $grade_items = grade_get_items($this->courseid); - $this->assertTrue(is_array($grade_items)); - $this->assertEqual(count($grade_items), 3); + $this->assertTrue(is_array($grade_items)); + $this->assertEqual(count($grade_items), 6); + } } function test_grade_create_item() { - $params = new stdClass(); - - $params->courseid = $this->courseid; - $params->categoryid = $this->grade_categories[0]->id; - $params->itemname = 'unittestgradeitem4'; - $params->itemtype = 'mod'; - $params->itemmodule = 'database'; - $params->iteminstance = 4; - $params->iteminfo = 'Grade item used for unit testing'; - $params->timecreated = mktime(); - $params->timemodified = mktime(); - - $params->id = grade_create_item($params); - $last_grade_item = end($this->grade_items); - - $this->assertEqual($params->id, $last_grade_item->id + 1); - $this->grade_items[] = $params; + if (get_class($this) == 'gradelib_test') { + $params = new stdClass(); + + $params->courseid = $this->courseid; + $params->categoryid = $this->grade_categories[0]->id; + $params->itemname = 'unittestgradeitem4'; + $params->itemtype = 'mod'; + $params->itemmodule = 'database'; + $params->iteminstance = 4; + $params->iteminfo = 'Grade item used for unit testing'; + $params->timecreated = mktime(); + $params->timemodified = mktime(); + + $params->id = grade_create_item($params); + $last_grade_item = end($this->grade_items); + + $this->assertEqual($params->id, $last_grade_item->id + 1); + $this->grade_items[] = $params; + } } function test_grade_create_category() { - $grade_category = new stdClass(); - $grade_category->timecreated = mktime(); - $grade_category->timemodified = mktime(); - - $grade_category->id = grade_create_category($this->courseid, 'unittestcategory4', $this->grade_items, GRADE_AGGREGATE_MEAN); - $last_grade_category = end($this->grade_categories); - - $this->assertEqual($grade_category->id, $last_grade_category->id + 1); - $this->grade_categories[] = $grade_category; + if (get_class($this) == 'gradelib_test') { + $grade_category = new stdClass(); + $grade_category->timecreated = mktime(); + $grade_category->timemodified = mktime(); + + $items = array(new grade_item(), new grade_item()); + + $grade_category->id = grade_create_category($this->courseid, 'unittestcategory4', $items, GRADE_AGGREGATE_MEAN); + + $last_grade_category = end($this->grade_categories); + $this->assertEqual($grade_category->id, $last_grade_category->id + 1); + + $db_grade_category = get_record('grade_categories', 'id', $grade_category->id); + $db_grade_category = new grade_category($db_grade_category); + $db_grade_category->load_grade_item(); + $this->grade_categories[] = $db_grade_category; + $this->grade_items[] = $db_grade_category->grade_item; + } } function test_grade_is_locked() { - $grade_item = $this->grade_items[0]; - $this->assertFalse(grade_is_locked($grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance, $grade_item->itemnumber)); - $grade_item = $this->grade_items[1]; - $this->assertTrue(grade_is_locked($grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance, $grade_item->itemnumber)); + if (get_class($this) == 'gradelib_test') { + $grade_item = $this->grade_items[0]; + $this->assertFalse(grade_is_locked($grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance, $grade_item->itemnumber)); + $grade_item = $this->grade_items[1]; + $this->assertTrue(grade_is_locked($grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance, $grade_item->itemnumber)); + } } } -- 2.39.5