MDL-41022 cleanup core_grade_scale_testcase
[moodle.git] / lib / grade / tests / grade_scale_test.php
CommitLineData
948d46da
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 * @package core_grades
19 * @category phpunit
20 * @copyright nicolas@moodle.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
26require_once(__DIR__.'/fixtures/lib.php');
27
28
8252b7c2 29class core_grade_scale_testcase extends grade_base_testcase {
948d46da
PS
30
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();
39 }
40
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();
49
50 $scale = new grade_scale($params, false);
51
52 $this->assertEquals($params->name, $scale->name);
53 $this->assertEquals($params->scale, $scale->scale);
54 $this->assertEquals($params->description, $scale->description);
55
56 }
57
58 protected function sub_test_grade_scale_insert() {
59 $grade_scale = new grade_scale();
60 $this->assertTrue(method_exists($grade_scale, 'insert'));
61
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.';
67
68 $grade_scale->insert();
69
70 $last_grade_scale = end($this->scale);
71
72 $this->assertEquals($grade_scale->id, $last_grade_scale->id + 1);
686d4ec9
PS
73 $this->assertNotEmpty($grade_scale->timecreated);
74 $this->assertNotEmpty($grade_scale->timemodified);
948d46da
PS
75 }
76
77 protected function sub_test_grade_scale_update() {
78 global $DB;
bd42a265 79 $grade_scale = new grade_scale($this->scale[1], false);
948d46da
PS
80 $this->assertTrue(method_exists($grade_scale, 'update'));
81
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);
86 }
87
88 protected function sub_test_grade_scale_delete() {
89 global $DB;
686d4ec9 90 $grade_scale = new grade_scale($this->scale[4], false); // Choose one we're not using elsewhere.
948d46da
PS
91 $this->assertTrue(method_exists($grade_scale, 'delete'));
92
93 $this->assertTrue($grade_scale->delete());
94 $this->assertFalse($DB->get_record('scale', array('id' => $grade_scale->id)));
95
686d4ec9 96 // Keep the reference collection the same as what is in the database.
948d46da
PS
97 unset($this->scale[4]);
98 }
99
100 protected function sub_test_grade_scale_fetch() {
101 $grade_scale = new grade_scale();
102 $this->assertTrue(method_exists($grade_scale, 'fetch'));
103
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);
107 }
108
109 protected function sub_test_scale_load_items() {
bd42a265 110 $scale = new grade_scale($this->scale[0], false);
948d46da
PS
111 $this->assertTrue(method_exists($scale, 'load_items'));
112
113 $scale->load_items();
686d4ec9 114 $this->assertCount(7, $scale->scale_items);
948d46da
PS
115 $this->assertEquals('Fairly neutral', $scale->scale_items[2]);
116
117 }
118
119 protected function sub_test_scale_compact_items() {
bd42a265 120 $scale = new grade_scale($this->scale[0], false);
948d46da
PS
121 $this->assertTrue(method_exists($scale, 'compact_items'));
122
123 $scale->load_items();
124 $scale->scale = null;
125 $scale->compact_items();
126
686d4ec9 127 // The original string and the new string may have differences in whitespace around the delimiter, and that's OK.
948d46da
PS
128 $this->assertEquals(preg_replace('/\s*,\s*/', ',', $this->scale[0]->scale), $scale->scale);
129 }
130}