Skip to content

Commit

Permalink
MDL-9506 Added category's associated grade_item object, and changed u…
Browse files Browse the repository at this point in the history
…nit tests database data so that it creates its own test tables.
  • Loading branch information
nicolasconnault committed May 7, 2007
1 parent 64eeaf4 commit f151b07
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 62 deletions.
45 changes: 41 additions & 4 deletions lib/grade/grade_category.php
Expand Up @@ -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();
}
}
}


Expand Down Expand Up @@ -169,6 +184,7 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
* 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();
Expand All @@ -183,6 +199,15 @@ function insert() {
}

$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;
}

Expand Down Expand Up @@ -220,14 +245,15 @@ function get_children($depth=1, $arraytype='nested') {
}

$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');
Expand Down Expand Up @@ -321,6 +347,7 @@ function get_childrentype() {
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) {
Expand All @@ -331,6 +358,16 @@ function get_childrentype() {
}
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;
}
}

?>
9 changes: 6 additions & 3 deletions lib/simpletest/grade/simpletest/testgradecategory.php
Expand Up @@ -40,12 +40,13 @@ function test_grade_category_construct() {
$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);
Expand All @@ -58,6 +59,7 @@ function test_grade_category_construct() {
$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;
Expand All @@ -68,6 +70,7 @@ function test_grade_category_construct() {
$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);
}
Expand All @@ -92,7 +95,7 @@ function test_grade_category_insert() {
$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() {
Expand Down
1 change: 0 additions & 1 deletion lib/simpletest/grade/simpletest/testgradeoutcome.php
Expand Up @@ -62,7 +62,6 @@ function test_grade_outcome_insert() {
$this->assertFalse(empty($grade_outcome->timecreated));
$this->assertFalse(empty($grade_outcome->timemodified));
$this->grade_outcomes[] = $grade_outcome;

}

function test_grade_outcome_update() {
Expand Down
162 changes: 108 additions & 54 deletions lib/simpletest/testgradelib.php
Expand Up @@ -38,17 +38,7 @@
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.
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -222,7 +217,51 @@ function load_grade_items() {

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;
}

}

/**
Expand Down Expand Up @@ -644,49 +683,64 @@ function load_grade_history() {
// 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));
}
}
}

Expand Down

0 comments on commit f151b07

Please sign in to comment.