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\scale\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' => 5, 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 grading with scales"); 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 $options = [ 107 'A', 108 'B', 109 'C' 110 ]; 111 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 112 113 $forum = $this->get_forum_instance([ 114 // Negative numbers mean a scale. 115 'grade_forum' => -1 * $scale->id 116 ]); 117 $course = $forum->get_course_record(); 118 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 119 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 120 $this->setUser($teacher); 121 122 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 123 124 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); 125 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 126 127 $this->assertIsArray($result); 128 $this->assertArrayHasKey('templatename', $result); 129 130 $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']); 131 132 $this->assertArrayHasKey('warnings', $result); 133 $this->assertIsArray($result['warnings']); 134 $this->assertEmpty($result['warnings']); 135 136 // Test the grade array items. 137 $this->assertArrayHasKey('grade', $result); 138 $this->assertIsArray($result['grade']); 139 140 $this->assertIsInt($result['grade']['timecreated']); 141 $this->assertArrayHasKey('timemodified', $result['grade']); 142 $this->assertIsInt($result['grade']['timemodified']); 143 144 $this->assertArrayHasKey('usergrade', $result['grade']); 145 $this->assertEquals('-', $result['grade']['usergrade']); 146 147 $this->assertArrayHasKey('maxgrade', $result['grade']); 148 $this->assertIsInt($result['grade']['maxgrade']); 149 $this->assertEquals(3, $result['grade']['maxgrade']); 150 151 $this->assertArrayHasKey('gradedby', $result['grade']); 152 $this->assertEquals(null, $result['grade']['gradedby']); 153 154 $this->assertArrayHasKey('options', $result['grade']); 155 $this->assertCount(count($options), $result['grade']['options']); 156 rsort($options); 157 foreach ($options as $index => $option) { 158 $this->assertArrayHasKey($index, $result['grade']['options']); 159 160 $returnedoption = $result['grade']['options'][$index]; 161 $this->assertArrayHasKey('value', $returnedoption); 162 $this->assertEquals(3 - $index, $returnedoption['value']); 163 164 $this->assertArrayHasKey('title', $returnedoption); 165 $this->assertEquals($option, $returnedoption['title']); 166 167 $this->assertArrayHasKey('selected', $returnedoption); 168 $this->assertFalse($returnedoption['selected']); 169 } 170 } 171 172 /** 173 * Ensure that an execute against the correct grading method returns the current state of the user. 174 */ 175 public function test_execute_fetch_graded(): void { 176 $this->resetAfterTest(); 177 178 $options = [ 179 'A', 180 'B', 181 'C' 182 ]; 183 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 184 185 $forum = $this->get_forum_instance([ 186 // Negative numbers mean a scale. 187 'grade_forum' => -1 * $scale->id 188 ]); 189 $course = $forum->get_course_record(); 190 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 191 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 192 193 $this->execute_and_assert_fetch($forum, $options, $scale, $teacher, $teacher, $student); 194 } 195 196 /** 197 * Class mates should not get other's grades. 198 */ 199 public function test_execute_fetch_does_not_return_data_to_other_students(): void { 200 $this->resetAfterTest(); 201 202 $options = [ 203 'A', 204 'B', 205 'C' 206 ]; 207 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 208 209 $forum = $this->get_forum_instance([ 210 // Negative numbers mean a scale. 211 'grade_forum' => -1 * $scale->id 212 ]); 213 $course = $forum->get_course_record(); 214 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 215 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 216 $evilstudent = $this->getDataGenerator()->create_and_enrol($course, 'student'); 217 218 $this->expectException(\required_capability_exception::class); 219 $this->execute_and_assert_fetch($forum, $options, $scale, $evilstudent, $teacher, $student); 220 } 221 222 /** 223 * Grades can be returned to graded user. 224 */ 225 public function test_execute_fetch_return_data_to_graded_user(): void { 226 $this->resetAfterTest(); 227 228 $options = [ 229 'A', 230 'B', 231 'C' 232 ]; 233 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 234 235 $forum = $this->get_forum_instance([ 236 // Negative numbers mean a scale. 237 'grade_forum' => -1 * $scale->id 238 ]); 239 $course = $forum->get_course_record(); 240 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 241 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 242 243 $this->execute_and_assert_fetch($forum, $options, $scale, $student, $teacher, $student); 244 } 245 246 /** 247 * Executes the fetch method with the given users and returns the result. 248 */ 249 private function execute_and_assert_fetch ($forum, $options, $scale, $fetcheruser, $grader, $gradeduser) { 250 251 $this->setUser($grader); 252 253 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 254 $gradeitem->store_grade_from_formdata($gradeduser, $grader, (object) [ 255 'grade' => 2, 256 ]); 257 258 $this->setUser($fetcheruser); 259 260 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id); 261 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 262 263 $this->assertIsArray($result); 264 $this->assertArrayHasKey('templatename', $result); 265 266 $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']); 267 268 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id); 269 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 270 271 $this->assertIsArray($result); 272 $this->assertArrayHasKey('templatename', $result); 273 274 $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']); 275 276 $this->assertArrayHasKey('warnings', $result); 277 $this->assertIsArray($result['warnings']); 278 $this->assertEmpty($result['warnings']); 279 280 // Test the grade array items. 281 $this->assertArrayHasKey('grade', $result); 282 $this->assertIsArray($result['grade']); 283 284 $this->assertIsInt($result['grade']['timecreated']); 285 $this->assertArrayHasKey('timemodified', $result['grade']); 286 $this->assertIsInt($result['grade']['timemodified']); 287 288 $this->assertArrayHasKey('usergrade', $result['grade']); 289 $this->assertEquals('B', $result['grade']['usergrade']); 290 291 $this->assertArrayHasKey('maxgrade', $result['grade']); 292 $this->assertIsInt($result['grade']['maxgrade']); 293 $this->assertEquals(3, $result['grade']['maxgrade']); 294 295 $this->assertArrayHasKey('gradedby', $result['grade']); 296 $this->assertEquals(fullname($grader), $result['grade']['gradedby']); 297 298 $this->assertArrayHasKey('options', $result['grade']); 299 $this->assertCount(count($options), $result['grade']['options']); 300 rsort($options); 301 foreach ($options as $index => $option) { 302 $this->assertArrayHasKey($index, $result['grade']['options']); 303 304 $returnedoption = $result['grade']['options'][$index]; 305 $this->assertArrayHasKey('value', $returnedoption); 306 $this->assertEquals(3 - $index, $returnedoption['value']); 307 308 $this->assertArrayHasKey('title', $returnedoption); 309 $this->assertEquals($option, $returnedoption['title']); 310 311 $this->assertArrayHasKey('selected', $returnedoption); 312 } 313 314 // The grade was 2, which relates to the middle option. 315 $this->assertFalse($result['grade']['options'][0]['selected']); 316 $this->assertTrue($result['grade']['options'][1]['selected']); 317 $this->assertFalse($result['grade']['options'][2]['selected']); 318 } 319 320 /** 321 * Get a forum instance. 322 * 323 * @param array $config 324 * @return forum_entity 325 */ 326 protected function get_forum_instance(array $config = []): forum_entity { 327 $this->resetAfterTest(); 328 329 $datagenerator = $this->getDataGenerator(); 330 $course = $datagenerator->create_course(); 331 $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id])); 332 333 $vaultfactory = \mod_forum\local\container::get_vault_factory(); 334 $vault = $vaultfactory->get_forum_vault(); 335 336 return $vault->get_from_id((int) $forum->id); 337 } 338 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body