Skip to content

Commit

Permalink
Unit test refactoring using mock objects. NOT FINISHED: testgradecate…
Browse files Browse the repository at this point in the history
…gory and testgradeitem are incomplete. The other tests work great however, and no longer a need for DB data
  • Loading branch information
nicolasconnault committed Nov 7, 2007
1 parent 120a18f commit 1f0e492
Show file tree
Hide file tree
Showing 11 changed files with 667 additions and 331 deletions.
2 changes: 2 additions & 0 deletions lang/en_utf8/xmldb.php
Expand Up @@ -44,6 +44,7 @@
$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';
Expand All @@ -56,6 +57,7 @@
$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';
Expand Down
2 changes: 1 addition & 1 deletion lib/ddllib.php
Expand Up @@ -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 //
Expand Down
13 changes: 7 additions & 6 deletions lib/grade/grade_category.php
Expand Up @@ -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 //
Expand Down Expand Up @@ -283,6 +283,7 @@ function insert($source=null) {

if (empty($this->courseid)) {
error('Can not insert grade category without course id!');
return false;
}

if (empty($this->parent)) {
Expand Down Expand Up @@ -314,11 +315,11 @@ function insert($source=null) {
* @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();

Expand Down
14 changes: 10 additions & 4 deletions lib/grade/grade_grade.php
Expand Up @@ -240,8 +240,11 @@ function is_editable() {
*/
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();
}
}

/**
Expand Down Expand Up @@ -429,8 +432,11 @@ function get_locktime() {
*/
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();
}
}

/**
Expand Down
8 changes: 2 additions & 6 deletions lib/grade/grade_object.php
Expand Up @@ -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 //
Expand Down Expand Up @@ -127,11 +127,9 @@ function fetch_all($params) {
/**
* 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
Expand All @@ -149,8 +147,6 @@ function fetch_helper($table, $classname, $params) {
* @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;
Expand Down
3 changes: 2 additions & 1 deletion lib/grade/grade_scale.php
Expand Up @@ -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 //
Expand Down Expand Up @@ -104,6 +104,7 @@ function fetch_all($params) {
* @return int PK ID if successful, false otherwise
*/
function insert($source=null) {
$this->timecreated = time();
$this->timemodified = time();
return parent::insert($source);
}
Expand Down
44 changes: 34 additions & 10 deletions lib/grade/simpletest/testgradecategory.php
Expand Up @@ -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
*/
Expand All @@ -39,27 +39,50 @@

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);
$this->assertEqual($params->fullname, $grade_category->fullname);
$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);
Expand All @@ -70,11 +93,16 @@ function test_grade_category_construct() {
$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'));
Expand Down Expand Up @@ -260,9 +288,6 @@ function test_grade_category_apply_limit_rules() {
$this->assertEqual(9.4743, $grade);
}
/**
* TODO implement
*/
function test_grade_category_is_aggregationcoef_used() {
}
Expand Down Expand Up @@ -378,9 +403,7 @@ function test_grade_category_fetch_course_category() {
$category = grade_category::fetch_course_category($this->courseid);
$this->assertTrue(empty($category->parent));
}
/**
* TODO implement
*/
function test_grade_category_is_editable() {
}
Expand Down Expand Up @@ -424,5 +447,6 @@ function generate_random_raw_grade($item, $userid) {
$grade->insert();
return $grade->rawgrade;
}
*/
}
?>

0 comments on commit 1f0e492

Please sign in to comment.