3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
10 // Copyright (C) 1999-2004 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_scale_construct() {
43 $params = new stdClass();
45 $params->name
= 'unittestscale3';
46 $params->courseid
= $this->courseid
;
47 $params->userid
= $this->userid
;
48 $params->scale
= 'Distinction, Very Good, Good, Pass, Fail';
49 $params->description
= 'This scale is used to mark standard assignments.';
50 $params->timemodified
= mktime();
52 $scale = new grade_scale($params, false);
54 $this->assertEqual($params->name
, $scale->name
);
55 $this->assertEqual($params->scale
, $scale->scale
);
56 $this->assertEqual($params->description
, $scale->description
);
60 function test_grade_scale_insert() {
61 $grade_scale = new grade_scale();
62 $this->assertTrue(method_exists($grade_scale, 'insert'));
64 $grade_scale->name
= 'unittestscale3';
65 $grade_scale->courseid
= $this->courseid
;
66 $grade_scale->userid
= $this->userid
;
67 $grade_scale->scale
= 'Distinction, Very Good, Good, Pass, Fail';
68 $grade_scale->description
= 'This scale is used to mark standard assignments.';
70 $grade_scale->insert();
72 $last_grade_scale = end($this->scale
);
74 $this->assertEqual($grade_scale->id
, $last_grade_scale->id +
1);
75 $this->assertTrue(!empty($grade_scale->timecreated
));
76 $this->assertTrue(!empty($grade_scale->timemodified
));
79 function test_grade_scale_update() {
80 $grade_scale = new grade_scale($this->scale
[0]);
81 $this->assertTrue(method_exists($grade_scale, 'update'));
83 $grade_scale->name
= 'Updated info for this unittest grade_scale';
84 $this->assertTrue($grade_scale->update());
85 $name = get_field('scale', 'name', 'id', $this->scale
[0]->id
);
86 $this->assertEqual($grade_scale->name
, $name);
89 function test_grade_scale_delete() {
90 $grade_scale = new grade_scale($this->scale
[0]);
91 $this->assertTrue(method_exists($grade_scale, 'delete'));
93 $this->assertTrue($grade_scale->delete());
94 $this->assertFalse(get_record('scale', 'id', $grade_scale->id
));
97 function test_grade_scale_fetch() {
98 $grade_scale = new grade_scale();
99 $this->assertTrue(method_exists($grade_scale, 'fetch'));
101 $grade_scale = grade_scale
::fetch(array('id'=>$this->scale
[0]->id
));
102 $this->assertEqual($this->scale
[0]->id
, $grade_scale->id
);
103 $this->assertEqual($this->scale
[0]->name
, $grade_scale->name
);
106 function test_scale_load_items() {
107 $scale = new grade_scale($this->scale
[0]);
108 $this->assertTrue(method_exists($scale, 'load_items'));
110 $scale->load_items();
111 $this->assertEqual(7, count($scale->scale_items
));
112 $this->assertEqual('Fairly neutral', $scale->scale_items
[2]);
115 function test_scale_compact_items() {
116 $scale = new grade_scale($this->scale
[0]);
117 $this->assertTrue(method_exists($scale, 'compact_items'));
119 $scale->load_items();
120 $scale->scale
= null;
121 $scale->compact_items();
123 // The original string and the new string may have differences in whitespace around the delimiter, and that's OK
124 $this->assertEqual(preg_replace('/\s*,\s*/', ',', $this->scale
[0]->scale
), $scale->scale
);