3
///////////////////////////////////////////////////////////////////////////
5
// NOTICE OF COPYRIGHT //
7
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8
// http://moodle.org //
10
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
12
// This program is free software; you can redistribute it and/or modify //
13
// it under the terms of the GNU General Public License as published by //
14
// the Free Software Foundation; either version 2 of the License, or //
15
// (at your option) any later version. //
17
// This program is distributed in the hope that it will be useful, //
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20
// GNU General Public License for more details: //
22
// http://www.gnu.org/copyleft/gpl.html //
24
///////////////////////////////////////////////////////////////////////////
27
* Unit tests for grade_scale object.
29
* @author nicolas@moodle.com
30
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
34
if (!defined('MOODLE_INTERNAL')) {
35
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
38
require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
40
class grade_scale_test extends grade_test {
42
function test_grade_scale() {
43
$this->sub_test_scale_construct();
44
$this->sub_test_grade_scale_insert();
45
$this->sub_test_grade_scale_update();
46
$this->sub_test_grade_scale_delete();
47
$this->sub_test_grade_scale_fetch();
48
$this->sub_test_scale_load_items();
49
$this->sub_test_scale_compact_items();
52
function sub_test_scale_construct() {
53
$params = new stdClass();
55
$params->name = 'unittestscale3';
56
$params->courseid = $this->courseid;
57
$params->userid = $this->userid;
58
$params->scale = 'Distinction, Very Good, Good, Pass, Fail';
59
$params->description = 'This scale is used to mark standard assignments.';
60
$params->timemodified = mktime();
62
$scale = new grade_scale($params, false);
64
$this->assertEqual($params->name, $scale->name);
65
$this->assertEqual($params->scale, $scale->scale);
66
$this->assertEqual($params->description, $scale->description);
70
function sub_test_grade_scale_insert() {
71
$grade_scale = new grade_scale();
72
$this->assertTrue(method_exists($grade_scale, 'insert'));
74
$grade_scale->name = 'unittestscale3';
75
$grade_scale->courseid = $this->courseid;
76
$grade_scale->userid = $this->userid;
77
$grade_scale->scale = 'Distinction, Very Good, Good, Pass, Fail';
78
$grade_scale->description = 'This scale is used to mark standard assignments.';
80
$grade_scale->insert();
82
$last_grade_scale = end($this->scale);
84
$this->assertEqual($grade_scale->id, $last_grade_scale->id + 1);
85
$this->assertTrue(!empty($grade_scale->timecreated));
86
$this->assertTrue(!empty($grade_scale->timemodified));
89
function sub_test_grade_scale_update() {
91
$grade_scale = new grade_scale($this->scale[1]);
92
$this->assertTrue(method_exists($grade_scale, 'update'));
94
$grade_scale->name = 'Updated info for this unittest grade_scale';
95
$this->assertTrue($grade_scale->update());
96
$name = $DB->get_field('scale', 'name', array('id' => $this->scale[1]->id));
97
$this->assertEqual($grade_scale->name, $name);
100
function sub_test_grade_scale_delete() {
102
$grade_scale = new grade_scale($this->scale[4]);//choose one we're not using elsewhere
103
$this->assertTrue(method_exists($grade_scale, 'delete'));
105
$this->assertTrue($grade_scale->delete());
106
$this->assertFalse($DB->get_record('scale', array('id' => $grade_scale->id)));
108
//keep the reference collection the same as what is in the database
109
unset($this->scale[4]);
112
function sub_test_grade_scale_fetch() {
113
$grade_scale = new grade_scale();
114
$this->assertTrue(method_exists($grade_scale, 'fetch'));
116
$grade_scale = grade_scale::fetch(array('id'=>$this->scale[0]->id));
117
$this->assertEqual($this->scale[0]->id, $grade_scale->id);
118
$this->assertEqual($this->scale[0]->name, $grade_scale->name);
121
function sub_test_scale_load_items() {
122
$scale = new grade_scale($this->scale[0]);
123
$this->assertTrue(method_exists($scale, 'load_items'));
125
$scale->load_items();
126
$this->assertEqual(7, count($scale->scale_items));
127
$this->assertEqual('Fairly neutral', $scale->scale_items[2]);
131
function sub_test_scale_compact_items() {
132
$scale = new grade_scale($this->scale[0]);
133
$this->assertTrue(method_exists($scale, 'compact_items'));
135
$scale->load_items();
136
$scale->scale = null;
137
$scale->compact_items();
139
// The original string and the new string may have differences in whitespace around the delimiter, and that's OK
140
$this->assertEqual(preg_replace('/\s*,\s*/', ',', $this->scale[0]->scale), $scale->scale);