Unit test refactoring using mock objects. NOT FINISHED: testgradecategory and testgradeitem are incomplete. The other tests work great however, and no longer a need for DB data

This commit is contained in:
nicolasconnault
2007-11-07 17:59:33 +00:00
parent 120a18f051
commit 1f0e492171
11 changed files with 666 additions and 330 deletions
+2
View File
@@ -44,6 +44,7 @@ $string['delete_xml_file'] = 'Delete XML File';
$string['down'] = 'Down';
$string['duplicate'] = 'Duplicate';
$string['duplicatefieldname'] = 'Another field with that name exists';
$string['duplicatekeyname'] = 'Another key with that name exists';
$string['edit'] = 'Edit';
$string['edit_field'] = 'Edit Field';
$string['edit_index'] = 'Edit Index';
@@ -56,6 +57,7 @@ $string['enumvaluesincorrect'] = 'Incorrect values for enum field';
$string['field'] = 'Field';
$string['fieldnameempty'] = 'Name field empty';
$string['fields'] = 'Fields';
$string['fieldsusedinkey'] = '[[incomplete lang string at line ' . __LINE__ . ' in ' . __FILE__ . ']]';
$string['filenotwriteable'] = 'File not writeable';
$string['floatincorrectdecimals'] = 'Incorrect number of decimals for float field';
$string['floatincorrectlength'] = 'Incorrect length for float field';
+1 -1
View File
@@ -7,7 +7,7 @@
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
// //
// This program is free software; you can redistribute it and/or modify //
+7 -6
View File
@@ -7,7 +7,7 @@
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
@@ -283,6 +283,7 @@ class grade_category extends grade_object {
if (empty($this->courseid)) {
error('Can not insert grade category without course id!');
return false;
}
if (empty($this->parent)) {
@@ -314,11 +315,11 @@ class grade_category extends grade_object {
* @return bool success
*/
function insert_course_category($courseid) {
$this->courseid = $courseid;
$this->fullname = get_string('coursegradecategory', 'grades');
$this->path = null;
$this->parent = null;
$this->aggregate = GRADE_AGGREGATE_MEAN;
$this->courseid = $courseid;
$this->fullname = get_string('coursegradecategory', 'grades');
$this->path = null;
$this->parent = null;
$this->aggregation = GRADE_AGGREGATE_MEAN;
$this->apply_forced_settings();
+10 -4
View File
@@ -240,8 +240,11 @@ class grade_grade extends grade_object {
*/
function is_locked() {
$this->load_grade_item();
return !empty($this->locked) or $this->grade_item->is_locked();
if (empty($this->grade_item)) {
return !empty($this->locked);
} else {
return !empty($this->locked) or $this->grade_item->is_locked();
}
}
/**
@@ -429,8 +432,11 @@ class grade_grade extends grade_object {
*/
function is_hidden() {
$this->load_grade_item();
return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time()) or $this->grade_item->is_hidden();
if (empty($this->grade_item)) {
return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time());
} else {
return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time()) or $this->grade_item->is_hidden();
}
}
/**
+2 -6
View File
@@ -7,7 +7,7 @@
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
@@ -127,11 +127,9 @@ class grade_object {
/**
* Factory method - uses the parameters to retrieve matching instance from the DB.
* @static final protected
* @return mixed object insatnce or false if not found
* @return mixed object instance or false if not found
*/
function fetch_helper($table, $classname, $params) {
// we have to do use this hack because of the incomplete OOP implementation in PHP4 :-(
// in PHP5 we could do it much better
if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) {
if (count($instances) > 1) {
// we should not tolerate any errors here - problems might appear later
@@ -149,8 +147,6 @@ class grade_object {
* @return mixed array of object instances or false if not found
*/
function fetch_all_helper($table, $classname, $params) {
// we have to do use this hack because of the incomplete OOP implementation in PHP4 :-(
// in PHP5 we could do it much better
$instance = new $classname();
$classvars = (array)$instance;
+2 -1
View File
@@ -7,7 +7,7 @@
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
@@ -104,6 +104,7 @@ class grade_scale extends grade_object {
* @return int PK ID if successful, false otherwise
*/
function insert($source=null) {
$this->timecreated = time();
$this->timemodified = time();
return parent::insert($source);
}
+34 -10
View File
@@ -26,7 +26,7 @@
/**
* Unit tests for grade_category object.
*
* @author nicolas@moodle.com
* @author nicolasconnault@gmail.com
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
@@ -39,15 +39,34 @@ require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
class grade_category_test extends grade_test {
function setUp() {
parent::setUp();
$this->load_grade_items();
}
function test_grade_category_construct() {
$course_category = grade_category::fetch_course_category($this->courseid);
global $db;
$course_category = $this->grade_categories[0];
$params = new stdClass();
$params->courseid = $this->courseid;
$params->fullname = 'unittestcategory4';
// Mock Insertion of category
$db->setReturnValue('GetInsertSQL', true);
$db->setReturnValue('Insert_ID', 1);
// Mock update of category
$grade_category = new grade_category($params, false);
$grade_category->parent = $course_category->id;
$this->rs->setReturnValue('RecordCount', 1);
$this->rs->fields = array(1);
$column = new stdClass();
$column->name = 'path';
$db->setReturnValue('MetaColumns', array($column));
$grade_category->insert();
$this->assertEqual($params->courseid, $grade_category->courseid);
@@ -55,11 +74,15 @@ class grade_category_test extends grade_test {
$this->assertEqual(2, $grade_category->depth);
$this->assertEqual("/$course_category->id/$grade_category->id/", $grade_category->path);
$parentpath = $grade_category->path;
/*
// Test a child category
$params->parent = $grade_category->id;
$params->fullname = 'unittestcategory5';
$grade_category = new grade_category($params, false);
$this->reset_mocks();
$this->rs->setReturnValue('RecordCount', 1);
$this->rs->fields = array(1);
$grade_category->insert();
$this->assertEqual(3, $grade_category->depth);
@@ -70,11 +93,16 @@ class grade_category_test extends grade_test {
$params->parent = $grade_category->id;
$params->fullname = 'unittestcategory6';
$grade_category = new grade_category($params, false);
$this->reset_mocks();
$this->rs->setReturnValue('RecordCount', 1);
$this->rs->fields = array(1);
$grade_category->insert();
$this->assertEqual(4, $grade_category->depth);
$this->assertEqual($parentpath.$grade_category->id."/", $grade_category->path);
*/
}
/*
function test_grade_category_build_path() {
$grade_category = new grade_category($this->grade_categories[1]);
$this->assertTrue(method_exists($grade_category, 'build_path'));
@@ -260,9 +288,6 @@ class grade_category_test extends grade_test {
$this->assertEqual(9.4743, $grade);
}
/**
* TODO implement
*/
function test_grade_category_is_aggregationcoef_used() {
}
@@ -378,9 +403,7 @@ class grade_category_test extends grade_test {
$category = grade_category::fetch_course_category($this->courseid);
$this->assertTrue(empty($category->parent));
}
/**
* TODO implement
*/
function test_grade_category_is_editable() {
}
@@ -424,5 +447,6 @@ class grade_category_test extends grade_test {
$grade->insert();
return $grade->rawgrade;
}
*/
}
?>
+103 -36
View File
@@ -38,6 +38,15 @@ if (!defined('MOODLE_INTERNAL')) {
require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
class grade_grade_test extends grade_test {
function setUp() {
parent::setUp();
$this->load_grade_grades();
}
function tearDown() {
parent::tearDown();
}
function test_grade_grade_construct() {
$params = new stdClass();
@@ -54,6 +63,7 @@ class grade_grade_test extends grade_test {
}
function test_grade_grade_insert() {
global $db;
$grade_grade = new grade_grade();
$this->assertTrue(method_exists($grade_grade, 'insert'));
@@ -62,89 +72,137 @@ class grade_grade_test extends grade_test {
$grade_grade->rawgrade = 88;
$grade_grade->rawgrademax = 110;
$grade_grade->rawgrademin = 18;
$grade_item = new grade_item($this->grade_items[0], false);
$grade_grade->grade_item = $grade_item;
// Check the grade_item's needsupdate variable first
$grade_grade->load_grade_item();
$this->assertFalse($grade_grade->grade_item->needsupdate);
// Mock insert of data in history table
$this->rs->setReturnValue('RecordCount', 1);
$this->rs->fields = array(1);
// Mock insert of outcome object
$db->setReturnValue('GetInsertSQL', true);
$db->setReturnValue('Insert_ID', 1);
$grade_grade->insert();
$last_grade_grade = end($this->grade_grades);
$this->assertEqual($grade_grade->id, 1);
$this->assertEqual($grade_grade->id, $last_grade_grade->id + 1);
$this->assertFalse(empty($grade_grade->timecreated));
$this->assertFalse(empty($grade_grade->timemodified));
// timecreated doesn't refer to creation in the DB, but to time of submission. Timemodified refers to date of grading
$this->assertTrue(empty($grade_grade->timecreated));
$this->assertTrue(empty($grade_grade->timemodified));
}
function test_grade_grade_update() {
$grade_grade = new grade_grade($this->grade_grades[0]);
$grade_grade = new grade_grade($this->grade_grades[0], false);
$this->assertTrue(method_exists($grade_grade, 'update'));
}
function test_grade_grade_fetch() {
$grade_grade = new grade_grade();
global $db;
$grade_grade = new grade_grade($this->grade_grades[0], false);
$this->assertTrue(method_exists($grade_grade, 'fetch'));
$grades = grade_grade::fetch(array('id'=>$this->grade_grades[0]->id));
$this->assertEqual($this->grade_grades[0]->id, $grades->id);
$this->assertEqual($this->grade_grades[0]->rawgrade, $grades->rawgrade);
// Mock fetch
$column = new stdClass();
$column->name = 'id';
$this->rs->setReturnValue('FetchField', $column); // Fetching the name of the first column
$this->rs->setReturnValue('GetAssoc', array($grade_grade->id => (array) $grade_grade));
$grades = grade_grade::fetch(array('id'=>$grade_grade->id));
$this->assertEqual($grade_grade->id, $grades->id);
$this->assertEqual($grade_grade->rawgrade, $grades->rawgrade);
}
function test_grade_grade_fetch_all() {
$grade_grade = new grade_grade();
$this->assertTrue(method_exists($grade_grade, 'fetch_all'));
// Mock fetch_all
$return_array = array();
foreach ($this->grade_grades as $gg) {
$return_array[$gg->id] = (array) $gg;
}
$column = new stdClass();
$column->name = 'id';
$this->rs->setReturnValue('FetchField', $column); // Fetching the name of the first column
$this->rs->setReturnValue('GetAssoc', $return_array);
$grades = grade_grade::fetch_all(array());
$this->assertEqual(count($this->grade_grades), count($grades));
$this->assertEqual(count($this->grade_grades), count($grades));
}
function test_grade_grade_load_grade_item() {
$grade_grade = new grade_grade($this->grade_grades[0]);
$grade_grade = new grade_grade($this->grade_grades[0], false);
$this->assertTrue(method_exists($grade_grade, 'load_grade_item'));
$this->assertNull($grade_grade->grade_item);
$this->assertTrue($grade_grade->itemid);
// Mock fetch
$grade_item = $this->grade_items[0];
$column = new stdClass();
$column->name = 'id';
$this->rs->setReturnValue('FetchField', $column); // Fetching the name of the first column
$this->rs->setReturnValue('GetAssoc', array($grade_item->id => (array) $grade_item));
$this->assertNotNull($grade_grade->load_grade_item());
$this->assertNotNull($grade_grade->grade_item);
$this->assertEqual($this->grade_items[0]->id, $grade_grade->grade_item->id);
}
function test_grade_grade_standardise_score() {
$this->assertEqual(4, round(grade_grade::standardise_score(6, 0, 7, 0, 5)));
$this->assertEqual(40, grade_grade::standardise_score(50, 30, 80, 0, 100));
}
/**
* In this test we always set the 2nd param of set_locked() to false, because it
* would otherwise trigger the refresh_grades method, which is not being tested here.
*/
function test_grade_grade_set_locked() {
$grade_item = new grade_item($this->grade_items[0]);
$grade = new grade_grade($grade_item->get_final(1));
global $db;
$grade_item = new grade_item($this->grade_items[0], false);
$grade = new grade_grade($grade_item->get_final(1), false);
$grade->grade_item = $grade_item;
$grade->itemid = $grade_item->id;
$this->assertTrue(method_exists($grade, 'set_locked'));
$this->assertTrue(empty($grade_item->locked));
$this->assertTrue(empty($grade->locked));
// Test locking the grade
$this->assertTrue($grade->set_locked(true));
// Test locking the grade when needsupdate is true
$grade->grade_item->needsupdate = true;
$this->assertFalse($grade->set_locked(true, false, false));
// Test locking the grade when needsupdate is false
$grade->grade_item->needsupdate = false;
$column = new stdClass();
$column->name = 'locked';
$db->setReturnValue('MetaColumns', array($column));
$this->assertTrue($grade->set_locked(true, false, false));
$this->assertFalse(empty($grade->locked));
$this->assertTrue($grade->set_locked(false));
$this->assertTrue($grade->set_locked(false, false, false));
$this->assertTrue(empty($grade->locked));
// Test locking the grade item with cascading on
$this->assertTrue($grade_item->set_locked(true, true, false));
$grade = new grade_grade($grade_item->get_final(1));
$grade = new grade_grade($grade_item->get_final(1), false);
$grade->grade_item = $grade_item;
$grade->itemid = $grade_item->id;
// Grade should already be locked
$this->assertFalse(empty($grade->locked));
$this->assertTrue($grade->set_locked(false));
$this->assertTrue($grade_item->set_locked(false));
$grade = new grade_grade($grade_item->get_final(1));
$this->assertTrue($grade->set_locked(false));
$this->assertTrue($grade->set_locked(false, false, false));
}
function test_grade_grade_is_locked() {
$grade = new grade_grade($this->grade_grades[0]);
$grade = new grade_grade($this->grade_grades[0], false);
$grade->grade_item = new grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade, 'is_locked'));
$this->assertFalse($grade->is_locked());
@@ -153,22 +211,33 @@ class grade_grade_test extends grade_test {
}
function test_grade_grade_set_hidden() {
$grade_item = new grade_item($this->grade_items[0]);
$grade = new grade_grade($grade_item->get_final(1));
global $db;
$grade_item = new grade_item($this->grade_items[0], false);
$grade = new grade_grade($grade_item->get_final(1), false);
$grade->grade_item = $grade_item;
$grade->itemid = $grade_item->id;
$this->assertTrue(method_exists($grade, 'set_hidden'));
$this->assertEqual(0, $grade_item->hidden);
$this->assertEqual(0, $grade->hidden);
$column = new stdClass();
$column->name = 'hidden';
$db->setReturnValue('MetaColumns', array($column));
$grade->set_hidden(0);
$this->assertEqual(0, $grade->hidden);
$grade->set_hidden(1);
$this->assertEqual(1, $grade->hidden);
// @TODO test with cascading on (2nd param set to true)
}
function test_grade_grade_is_hidden() {
$grade = new grade_grade($this->grade_grades[0]);
$grade = new grade_grade($this->grade_grades[0], false);
$grade->grade_item = new grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade, 'is_hidden'));
$this->assertFalse($grade->is_hidden());
@@ -181,7 +250,5 @@ class grade_grade_test extends grade_test {
$grade->hidden = time()+666;
$this->assertTrue($grade->is_hidden());
}
}
?>
+76 -10
View File
@@ -38,6 +38,14 @@ if (!defined('MOODLE_INTERNAL')) {
require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
class grade_outcome_test extends grade_test {
function setUp() {
parent::setUp();
$this->load_grade_outcomes();
}
function tearDown() {
parent::tearDown();
}
function test_grade_outcome_construct() {
$params = new stdClass();
@@ -51,46 +59,93 @@ class grade_outcome_test extends grade_test {
}
function test_grade_outcome_insert() {
global $db;
$grade_outcome = new grade_outcome();
$this->assertTrue(method_exists($grade_outcome, 'insert'));
$grade_outcome->courseid = $this->courseid;
$grade_outcome->shortname = 'tw';
$grade_outcome->fullname = 'Team work';
// Mock insert of data in history table
$this->rs->setReturnValue('RecordCount', 1);
$this->rs->fields = array(1);
// Mock insert of outcome object
$db->setReturnValue('GetInsertSQL', true);
$db->setReturnValue('Insert_ID', 1);
$grade_outcome->insert();
$last_grade_outcome = end($this->grade_outcomes);
$this->assertEqual($grade_outcome->id, $last_grade_outcome->id + 1);
$this->assertEqual($grade_outcome->id, 1);
$this->assertFalse(empty($grade_outcome->timecreated));
$this->assertFalse(empty($grade_outcome->timemodified));
}
function test_grade_outcome_update() {
$grade_outcome = new grade_outcome($this->grade_outcomes[0]);
global $db;
$grade_outcome = new grade_outcome($this->grade_outcomes[0], false);
$grade_outcome->timecreated = time() - 200000;
$grade_outcome->timemodified = $grade_outcome->timecreated;
$timemodified = $grade_outcome->timemodified;
$timecreated = $grade_outcome->timecreated;
$grade_outcome->courseid = null;
$this->assertTrue(method_exists($grade_outcome, 'update'));
$grade_outcome->shortname = 'Team work';
// Mock update: MetaColumns is first returned to compare existing data with new
$column = new stdClass();
$column->name = 'shortname';
$db->setReturnValue('MetaColumns', array($column));
$this->assertTrue($grade_outcome->update());
$shortname = get_field('grade_outcomes', 'shortname', 'id', $this->grade_outcomes[0]->id);
$this->assertEqual($grade_outcome->shortname, $shortname);
// We expect timecreated to be unchanged, and timemodified to be updated
$this->assertTrue($grade_outcome->timemodified > $timemodified);
$this->assertTrue($grade_outcome->timemodified > $grade_outcome->timecreated);
$this->assertTrue($grade_outcome->timecreated == $timecreated);
// @TODO When the grade_outcome has a courseid but no match in the grade_outcomes_courses table, the update method should insert a new record in that table
// @TODO If history switch is on, an insert should be performed in the grade_outcomes_history table
}
function test_grade_outcome_delete() {
$grade_outcome = new grade_outcome($this->grade_outcomes[0]);
$this->assertTrue(method_exists($grade_outcome, 'delete'));
global $db;
$grade_outcome = new grade_outcome($this->grade_outcomes[0], false);
// Mock delete
$this->assertTrue(method_exists($grade_outcome, 'delete'));
$this->assertTrue($grade_outcome->delete());
$this->assertFalse(get_record('grade_outcomes', 'id', $grade_outcome->id));
// @TODO If history switch is on, an insert should be performed in the grade_outcomes_history table
// @TODO If grade_outcome has a courseid, associated records from grade_outcomes_courses should be deleted also
}
function test_grade_outcome_fetch() {
global $db;
$grade_outcome = new grade_outcome();
$this->assertTrue(method_exists($grade_outcome, 'fetch'));
// Mock fetch
$column = new stdClass();
$column->name = 'id';
$this->rs->setReturnValue('FetchField', $column); // Fetching the name of the first column
$this->rs->setReturnValue('GetAssoc', array($this->grade_outcomes[0]->id => (array) $this->grade_outcomes[0]));
$grade_outcome = grade_outcome::fetch(array('id'=>$this->grade_outcomes[0]->id));
$this->assertEqual($this->grade_outcomes[0]->id, $grade_outcome->id);
$this->assertEqual($this->grade_outcomes[0]->shortname, $grade_outcome->shortname);
// Mock fetching of scale object
$this->reset_mocks();
$this->rs->setReturnValue('FetchField', $column); // Fetching the name of the first column
$this->rs->setReturnValue('GetAssoc', array($this->scale[2]->id => (array) $this->scale[2]));
$grade_outcome->load_scale();
$this->assertEqual($this->scale[2]->id, $grade_outcome->scale->id);
}
@@ -98,6 +153,17 @@ class grade_outcome_test extends grade_test {
function test_grade_outcome_fetch_all() {
$grade_outcome = new grade_outcome();
$this->assertTrue(method_exists($grade_outcome, 'fetch_all'));
// Mock fetch_all
$return_array = array();
foreach ($this->grade_outcomes as $go) {
$return_array[$go->id] = (array) $go;
}
$column = new stdClass();
$column->name = 'id';
$this->rs->setReturnValue('FetchField', $column); // Fetching the name of the first column
$this->rs->setReturnValue('GetAssoc', $return_array);
$grade_outcomes = grade_outcome::fetch_all(array());
$this->assertEqual(count($this->grade_outcomes), count($grade_outcomes));
+54 -15
View File
@@ -7,7 +7,7 @@
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
@@ -39,6 +39,15 @@ require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
class grade_scale_test extends grade_test {
function setUp() {
parent::setUp();
$this->load_scale();
}
function tearDown() {
parent::tearDown();
}
function test_scale_construct() {
$params = new stdClass();
@@ -58,6 +67,7 @@ class grade_scale_test extends grade_test {
}
function test_grade_scale_insert() {
global $db;
$grade_scale = new grade_scale();
$this->assertTrue(method_exists($grade_scale, 'insert'));
@@ -67,53 +77,83 @@ class grade_scale_test extends grade_test {
$grade_scale->scale = 'Distinction, Very Good, Good, Pass, Fail';
$grade_scale->description = 'This scale is used to mark standard assignments.';
// Mock insert of data in history table
$this->rs->setReturnValue('RecordCount', 1);
$this->rs->fields = array(1);
// Mock insert of outcome object
$db->setReturnValue('GetInsertSQL', true);
$db->setReturnValue('Insert_ID', 1);
$grade_scale->insert();
$last_grade_scale = end($this->scale);
$this->assertEqual($grade_scale->id, $last_grade_scale->id + 1);
$this->assertTrue(!empty($grade_scale->timecreated));
$this->assertTrue(!empty($grade_scale->timemodified));
$this->assertEqual($grade_scale->id, 1);
$this->assertFalse(empty($grade_scale->timecreated));
$this->assertFalse(empty($grade_scale->timemodified));
}
function test_grade_scale_update() {
$grade_scale = new grade_scale($this->scale[0]);
global $db;
$grade_scale = new grade_scale($this->scale[0], false);
$this->assertTrue(method_exists($grade_scale, 'update'));
$grade_scale->timecreated = time() - 200000;
$grade_scale->timemodified = $grade_scale->timecreated;
$timemodified = $grade_scale->timemodified;
$timecreated = $grade_scale->timecreated;
// Mock update: MetaColumns is first returned to compare existing data with new
$column = new stdClass();
$column->name = 'name';
$db->setReturnValue('MetaColumns', array($column));
$grade_scale->name = 'Updated info for this unittest grade_scale';
$this->assertTrue($grade_scale->update());
$name = get_field('scale', 'name', 'id', $this->scale[0]->id);
$this->assertEqual($grade_scale->name, $name);
// We expect timecreated to be unchanged, and timemodified to be updated
$this->assertTrue($grade_scale->timemodified > $timemodified);
$this->assertTrue($grade_scale->timemodified > $grade_scale->timecreated);
$this->assertTrue($grade_scale->timecreated == $timecreated);
}
function test_grade_scale_delete() {
$grade_scale = new grade_scale($this->scale[0]);
$grade_scale = new grade_scale($this->scale[0], false);
$this->assertTrue(method_exists($grade_scale, 'delete'));
$this->assertTrue($grade_scale->delete());
$this->assertFalse(get_record('scale', 'id', $grade_scale->id));
}
function test_grade_scale_fetch() {
global $db;
$grade_scale = new grade_scale();
$this->assertTrue(method_exists($grade_scale, 'fetch'));
// Mock fetch
$column = new stdClass();
$column->name = 'id';
$this->rs->setReturnValue('FetchField', $column); // Fetching the name of the first column
$this->rs->setReturnValue('GetAssoc', array($this->scale[0]->id => (array) $this->scale[0]));
$grade_scale = grade_scale::fetch(array('id'=>$this->scale[0]->id));
$this->assertEqual($this->scale[0]->id, $grade_scale->id);
$this->assertEqual($this->scale[0]->name, $grade_scale->name);
}
function test_scale_load_items() {
$scale = new grade_scale($this->scale[0]);
$scale = new grade_scale($this->scale[0], false);
$this->assertTrue(method_exists($scale, 'load_items'));
$scale->load_items();
$this->assertEqual(7, count($scale->scale_items));
$this->assertEqual('Fairly neutral', $scale->scale_items[2]);
$newscale = 'Item1, Item2, Item3, Item4';
$this->assertEqual(4, count($scale->load_items($newscale)));
}
function test_scale_compact_items() {
$scale = new grade_scale($this->scale[0]);
$scale = new grade_scale($this->scale[0], false);
$this->assertTrue(method_exists($scale, 'compact_items'));
$scale->load_items();
@@ -121,8 +161,7 @@ class grade_scale_test extends grade_test {
$scale->compact_items();
// The original string and the new string may have differences in whitespace around the delimiter, and that's OK
$this->assertEqual(preg_replace('/\s*,\s*/', ',', $this->scale[0]->scale), $scale->scale);
$this->assertEqual(preg_replace('/\s*,\s*' . '/', ',', $this->scale[0]->scale), $scale->scale);
}
}
?>
File diff suppressed because it is too large Load Diff