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 * @package core_grades
20 * @copyright nicolas@moodle.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 require_once(__DIR__.'/fixtures/lib.php');
29 class core_grade_scale_testcase extends grade_base_testcase {
31 public function test_grade_scale() {
32 $this->sub_test_scale_construct();
33 $this->sub_test_grade_scale_insert();
34 $this->sub_test_grade_scale_update();
35 $this->sub_test_grade_scale_delete();
36 $this->sub_test_grade_scale_fetch();
37 $this->sub_test_scale_load_items();
38 $this->sub_test_scale_compact_items();
41 protected function sub_test_scale_construct() {
42 $params = new stdClass();
43 $params->name = 'unittestscale3';
44 $params->courseid = $this->course->id;
45 $params->userid = $this->userid;
46 $params->scale = 'Distinction, Very Good, Good, Pass, Fail';
47 $params->description = 'This scale is used to mark standard assignments.';
48 $params->timemodified = time();
50 $scale = new grade_scale($params, false);
52 $this->assertEquals($params->name, $scale->name);
53 $this->assertEquals($params->scale, $scale->scale);
54 $this->assertEquals($params->description, $scale->description);
58 protected function sub_test_grade_scale_insert() {
59 $grade_scale = new grade_scale();
60 $this->assertTrue(method_exists($grade_scale, 'insert'));
62 $grade_scale->name = 'unittestscale3';
63 $grade_scale->courseid = $this->courseid;
64 $grade_scale->userid = $this->userid;
65 $grade_scale->scale = 'Distinction, Very Good, Good, Pass, Fail';
66 $grade_scale->description = 'This scale is used to mark standard assignments.';
68 $grade_scale->insert();
70 $last_grade_scale = end($this->scale);
72 $this->assertEquals($grade_scale->id, $last_grade_scale->id + 1);
73 $this->assertNotEmpty($grade_scale->timecreated);
74 $this->assertNotEmpty($grade_scale->timemodified);
77 protected function sub_test_grade_scale_update() {
79 $grade_scale = new grade_scale($this->scale[1], false);
80 $this->assertTrue(method_exists($grade_scale, 'update'));
82 $grade_scale->name = 'Updated info for this unittest grade_scale';
83 $this->assertTrue($grade_scale->update());
84 $name = $DB->get_field('scale', 'name', array('id' => $this->scale[1]->id));
85 $this->assertEquals($grade_scale->name, $name);
88 protected function sub_test_grade_scale_delete() {
90 $grade_scale = new grade_scale($this->scale[4], false); // Choose one we're not using elsewhere.
91 $this->assertTrue(method_exists($grade_scale, 'delete'));
93 $this->assertTrue($grade_scale->delete());
94 $this->assertFalse($DB->get_record('scale', array('id' => $grade_scale->id)));
96 // Keep the reference collection the same as what is in the database.
97 unset($this->scale[4]);
100 protected function sub_test_grade_scale_fetch() {
101 $grade_scale = new grade_scale();
102 $this->assertTrue(method_exists($grade_scale, 'fetch'));
104 $grade_scale = grade_scale::fetch(array('id'=>$this->scale[0]->id));
105 $this->assertEquals($this->scale[0]->id, $grade_scale->id);
106 $this->assertEquals($this->scale[0]->name, $grade_scale->name);
109 protected function sub_test_scale_load_items() {
110 $scale = new grade_scale($this->scale[0], false);
111 $this->assertTrue(method_exists($scale, 'load_items'));
113 $scale->load_items();
114 $this->assertCount(7, $scale->scale_items);
115 $this->assertEquals('Fairly neutral', $scale->scale_items[2]);
119 protected function sub_test_scale_compact_items() {
120 $scale = new grade_scale($this->scale[0], false);
121 $this->assertTrue(method_exists($scale, 'compact_items'));
123 $scale->load_items();
124 $scale->scale = null;
125 $scale->compact_items();
127 // The original string and the new string may have differences in whitespace around the delimiter, and that's OK.
128 $this->assertEquals(preg_replace('/\s*,\s*/', ',', $this->scale[0]->scale), $scale->scale);