Differences Between: [Versions 39 and 310]
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 core_grades\component_gradeitems; 19 * 20 * @package core_grades 21 * @category test 22 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 24 */ 25 26 declare(strict_types = 1); 27 28 namespace core_grades\grades\grader\gradingpanel\point\external; 29 30 use advanced_testcase; 31 use coding_exception; 32 use core_grades\component_gradeitem; 33 use external_api; 34 use mod_forum\local\entities\forum as forum_entity; 35 use moodle_exception; 36 37 /** 38 * Unit tests for core_grades\component_gradeitems; 39 * 40 * @package core_grades 41 * @category test 42 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 44 */ 45 class fetch_test extends advanced_testcase { 46 47 public static function setupBeforeClass(): void { 48 global $CFG; 49 require_once("{$CFG->libdir}/externallib.php"); 50 } 51 52 /** 53 * Ensure that an execute with an invalid component is rejected. 54 */ 55 public function test_execute_invalid_component(): void { 56 $this->resetAfterTest(); 57 $user = $this->getDataGenerator()->create_user(); 58 $this->setUser($user); 59 60 $this->expectException(coding_exception::class); 61 $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component"); 62 fetch::execute('mod_invalid', 1, 'foo', 2); 63 } 64 65 /** 66 * Ensure that an execute with an invalid itemname on a valid component is rejected. 67 */ 68 public function test_execute_invalid_itemname(): void { 69 $this->resetAfterTest(); 70 $user = $this->getDataGenerator()->create_user(); 71 $this->setUser($user); 72 73 $this->expectException(coding_exception::class); 74 $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component"); 75 fetch::execute('mod_forum', 1, 'foo', 2); 76 } 77 78 /** 79 * Ensure that an execute against a different grading method is rejected. 80 */ 81 public function test_execute_incorrect_type(): void { 82 $this->resetAfterTest(); 83 84 $forum = $this->get_forum_instance([ 85 // Negative numbers mean a scale. 86 'grade_forum' => -1, 87 ]); 88 $course = $forum->get_course_record(); 89 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 90 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 91 $this->setUser($teacher); 92 93 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 94 95 $this->expectException(moodle_exception::class); 96 $this->expectExceptionMessage("not configured for direct grading"); 97 fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); 98 } 99 100 /** 101 * Ensure that an execute against the correct grading method returns the current state of the user. 102 */ 103 public function test_execute_fetch_empty(): void { 104 $this->resetAfterTest(); 105 106 $forum = $this->get_forum_instance([ 107 // Negative numbers mean a scale. 108 'grade_forum' => 5, 109 ]); 110 $course = $forum->get_course_record(); 111 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 112 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 113 $this->setUser($teacher); 114 115 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 116 117 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); 118 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 119 120 $this->assertIsArray($result); 121 $this->assertArrayHasKey('templatename', $result); 122 123 $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']); 124 125 $this->assertArrayHasKey('warnings', $result); 126 $this->assertIsArray($result['warnings']); 127 $this->assertEmpty($result['warnings']); 128 129 // Test the grade array items. 130 $this->assertArrayHasKey('grade', $result); 131 $this->assertIsArray($result['grade']); 132 133 $this->assertArrayHasKey('grade', $result['grade']); 134 $this->assertEmpty($result['grade']['grade']); 135 136 $this->assertIsInt($result['grade']['timecreated']); 137 $this->assertArrayHasKey('timemodified', $result['grade']); 138 $this->assertIsInt($result['grade']['timemodified']); 139 140 $this->assertArrayHasKey('usergrade', $result['grade']); 141 $this->assertEquals('- / 5.00', $result['grade']['usergrade']); 142 143 $this->assertArrayHasKey('maxgrade', $result['grade']); 144 $this->assertIsInt($result['grade']['maxgrade']); 145 $this->assertEquals(5, $result['grade']['maxgrade']); 146 147 $this->assertArrayHasKey('gradedby', $result['grade']); 148 $this->assertEquals(null, $result['grade']['gradedby']); 149 } 150 151 /** 152 * Ensure that an execute against the correct grading method returns the current state of the user. 153 */ 154 public function test_execute_fetch_graded(): void { 155 $this->resetAfterTest(); 156 157 $forum = $this->get_forum_instance([ 158 // Negative numbers mean a scale. 159 'grade_forum' => 5, 160 ]); 161 $course = $forum->get_course_record(); 162 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 163 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 164 165 $this->execute_and_assert_fetch($forum, $teacher, $teacher, $student); 166 } 167 168 /** 169 * Class mates should not get other's grades. 170 */ 171 public function test_execute_fetch_does_not_return_data_to_other_students(): void { 172 $this->resetAfterTest(); 173 174 $forum = $this->get_forum_instance([ 175 // Negative numbers mean a scale. 176 'grade_forum' => 5, 177 ]); 178 $course = $forum->get_course_record(); 179 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 180 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 181 $evilstudent = $this->getDataGenerator()->create_and_enrol($course, 'student'); 182 183 $this->expectException(\required_capability_exception::class); 184 $this->execute_and_assert_fetch($forum, $evilstudent, $teacher, $student); 185 } 186 187 /** 188 * Grades can be returned to graded user. 189 */ 190 public function test_execute_fetch_return_data_to_graded_user(): void { 191 $this->resetAfterTest(); 192 193 $forum = $this->get_forum_instance([ 194 // Negative numbers mean a scale. 195 'grade_forum' => 5, 196 ]); 197 $course = $forum->get_course_record(); 198 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 199 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 200 201 $this->execute_and_assert_fetch($forum, $student, $teacher, $student); 202 } 203 204 /** 205 * Executes the fetch method with the given users and returns the result. 206 */ 207 private function execute_and_assert_fetch ($forum, $fetcheruser, $grader, $gradeduser) { 208 $this->setUser($grader); 209 210 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 211 $gradeitem->store_grade_from_formdata($gradeduser, $grader, (object) [ 212 'grade' => 4, 213 ]); 214 215 $this->setUser($fetcheruser); 216 217 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id); 218 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 219 220 $this->assertIsArray($result); 221 $this->assertArrayHasKey('templatename', $result); 222 223 $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']); 224 225 $this->assertArrayHasKey('warnings', $result); 226 $this->assertIsArray($result['warnings']); 227 $this->assertEmpty($result['warnings']); 228 229 // Test the grade array items. 230 $this->assertArrayHasKey('grade', $result); 231 $this->assertIsArray($result['grade']); 232 233 $this->assertArrayHasKey('grade', $result['grade']); 234 $this->assertIsFloat($result['grade']['grade']); 235 $this->assertEquals(grade_floatval(unformat_float(4)), $result['grade']['grade']); 236 237 $this->assertIsInt($result['grade']['timecreated']); 238 $this->assertArrayHasKey('timemodified', $result['grade']); 239 $this->assertIsInt($result['grade']['timemodified']); 240 241 $this->assertArrayHasKey('usergrade', $result['grade']); 242 $this->assertEquals('4.00 / 5.00', $result['grade']['usergrade']); 243 244 $this->assertArrayHasKey('maxgrade', $result['grade']); 245 $this->assertIsInt($result['grade']['maxgrade']); 246 $this->assertEquals(5, $result['grade']['maxgrade']); 247 248 $this->assertArrayHasKey('gradedby', $result['grade']); 249 $this->assertEquals(fullname($grader), $result['grade']['gradedby']); 250 251 return $result; 252 } 253 254 /** 255 * Get a forum instance. 256 * 257 * @param array $config 258 * @return forum_entity 259 */ 260 protected function get_forum_instance(array $config = []): forum_entity { 261 $this->resetAfterTest(); 262 263 $datagenerator = $this->getDataGenerator(); 264 $course = $datagenerator->create_course(); 265 $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id])); 266 267 $vaultfactory = \mod_forum\local\container::get_vault_factory(); 268 $vault = $vaultfactory->get_forum_vault(); 269 270 return $vault->get_from_id((int) $forum->id); 271 } 272 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body