2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for grade/edit/tree/lib.php.
22 * @author Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
.'/grade/edit/tree/lib.php');
33 * Tests grade_edit_tree (deals with the data on the categories and items page in the gradebook)
35 class core_grade_edittreelib_testcase
extends advanced_testcase
{
36 public function test_format_number() {
37 $numinput = array(0, 1, 1.01, '1.010', 1.2345);
38 $numoutput = array(0.0, 1.0, 1.01, 1.01, 1.2345);
40 for ($i = 0; $i < count($numinput); $i++
) {
41 $msg = 'format_number() testing '.$numinput[$i].' %s';
42 $this->assertEquals(grade_edit_tree
::format_number($numinput[$i]), $numoutput[$i], $msg);
46 public function test_grade_edit_tree_column_range_get_item_cell() {
49 $this->resetAfterTest(true);
51 // Make some things we need.
52 $scale = $this->getDataGenerator()->create_scale();
53 $course = $this->getDataGenerator()->create_course();
54 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id
));
55 $modulecontext = context_module
::instance($assign->cmid
);
56 // The generator returns a dummy object, lets get the real assign object.
57 $assign = new assign($modulecontext, false, false);
58 $cm = $assign->get_course_module();
61 $column = grade_edit_tree_column
::factory('range');
63 $gradeitemparams = array(
65 'itemmodule' => $cm->modname
,
66 'iteminstance' => $cm->instance
,
67 'courseid' => $cm->course
,
71 // Lets set the grade to something we know.
72 $instance = $assign->get_instance();
73 $instance->grade
= 70;
74 $instance->instance
= $instance->id
;
75 $assign->update_instance($instance);
77 $gradeitem = grade_item
::fetch($gradeitemparams);
78 $cell = $column->get_item_cell($gradeitem, array());
80 $this->assertEquals(GRADE_TYPE_VALUE
, $gradeitem->gradetype
);
81 $this->assertEquals(null, $gradeitem->scaleid
);
82 $this->assertEquals(70.0, (float) $cell->text
, "Grade text is 70", 0.01);
84 // Now change it to a scale.
85 $instance = $assign->get_instance();
86 $instance->grade
= -($scale->id
);
87 $instance->instance
= $instance->id
;
88 $assign->update_instance($instance);
90 $gradeitem = grade_item
::fetch($gradeitemparams);
91 $cell = $column->get_item_cell($gradeitem, array());
93 // Make the expected scale text.
95 $scaleitems = explode(',', $scale->scale
);
96 $scalestring = end($scaleitems) . ' (' . count($scaleitems) . ')';
98 $this->assertEquals(GRADE_TYPE_SCALE
, $gradeitem->gradetype
);
99 $this->assertEquals($scale->id
, $gradeitem->scaleid
);
100 $this->assertEquals($scalestring, $cell->text
, "Grade text matches scale");
102 // Now change it to no grade with gradebook feedback enabled.
103 $adminconfig = $assign->get_admin_config();
104 $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook
;
105 $gradebookplugin .= '_enabled';
107 $instance = $assign->get_instance();
108 $instance->grade
= 0;
109 $instance->$gradebookplugin = 1;
110 $instance->instance
= $instance->id
;
111 $assign->update_instance($instance);
113 $gradeitem = grade_item
::fetch($gradeitemparams);
114 $cell = $column->get_item_cell($gradeitem, array());
116 $this->assertEquals(GRADE_TYPE_TEXT
, $gradeitem->gradetype
);
117 $this->assertEquals(null, $gradeitem->scaleid
);
118 $this->assertEquals(' - ', $cell->text
, 'Grade text matches empty value of " - "');
120 // Now change it to no grade with gradebook feedback disabled.
121 $instance = $assign->get_instance();
122 $instance->grade
= 0;
123 $instance->$gradebookplugin = 0;
124 $instance->instance
= $instance->id
;
125 $assign->update_instance($instance);
127 $gradeitem = grade_item
::fetch($gradeitemparams);
128 $cell = $column->get_item_cell($gradeitem, array());
130 $this->assertEquals(GRADE_TYPE_NONE
, $gradeitem->gradetype
);
131 $this->assertEquals(null, $gradeitem->scaleid
);