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