MDL-30085 core_grades functions moved to correct location
[moodle.git] / grade / tests / edittreelib_test.php
CommitLineData
50738805
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Unit tests for grade/edit/tree/lib.php.
19 *
20 * @pacakge core_grade
21 * @category phpunit
22 * @author Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot.'/grade/edit/tree/lib.php');
30
31
32/**
33 * Tests grade_edit_tree (deals with the data on the categories and items page in the gradebook)
34 */
e3a65c9e 35class core_grade_edittreelib_testcase extends advanced_testcase {
50738805 36 public function test_format_number() {
e3a65c9e 37 $numinput = array(0, 1, 1.01, '1.010', 1.2345);
50738805
PS
38 $numoutput = array(0.0, 1.0, 1.01, 1.01, 1.2345);
39
e3a65c9e 40 for ($i = 0; $i < count($numinput); $i++) {
50738805 41 $msg = 'format_number() testing '.$numinput[$i].' %s';
e3a65c9e 42 $this->assertEquals(grade_edit_tree::format_number($numinput[$i]), $numoutput[$i], $msg);
50738805
PS
43 }
44 }
45
e3a65c9e
EM
46 public function test_grade_edit_tree_column_range_get_item_cell() {
47 global $DB, $CFG;
48
49 $this->resetAfterTest(true);
50
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));
780cf0a7 55 $modulecontext = context_module::instance($assign->cmid);
e3a65c9e
EM
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();
59
60 // Get range column.
61 $column = grade_edit_tree_column::factory('range');
62
63 $gradeitemparams = array(
64 'itemtype' => 'mod',
65 'itemmodule' => $cm->modname,
66 'iteminstance' => $cm->instance,
67 'courseid' => $cm->course,
68 'itemnumber' => 0
69 );
70
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);
76
77 $gradeitem = grade_item::fetch($gradeitemparams);
78 $cell = $column->get_item_cell($gradeitem, array());
79
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);
83
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);
89
90 $gradeitem = grade_item::fetch($gradeitemparams);
91 $cell = $column->get_item_cell($gradeitem, array());
92
93 // Make the expected scale text.
94 $scaleitems = null;
95 $scaleitems = explode(',', $scale->scale);
96 $scalestring = end($scaleitems) . ' (' . count($scaleitems) . ')';
97
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");
101
456d7bc7
RT
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';
106
e3a65c9e
EM
107 $instance = $assign->get_instance();
108 $instance->grade = 0;
456d7bc7 109 $instance->$gradebookplugin = 1;
e3a65c9e
EM
110 $instance->instance = $instance->id;
111 $assign->update_instance($instance);
112
113 $gradeitem = grade_item::fetch($gradeitemparams);
114 $cell = $column->get_item_cell($gradeitem, array());
115
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 " - "');
456d7bc7
RT
119
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);
126
127 $gradeitem = grade_item::fetch($gradeitemparams);
128 $cell = $column->get_item_cell($gradeitem, array());
129
130 $this->assertEquals(GRADE_TYPE_NONE, $gradeitem->gradetype);
131 $this->assertEquals(null, $gradeitem->scaleid);
e3a65c9e 132 }
50738805
PS
133}
134
135