Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]
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\scale\external; 20 21 use advanced_testcase; 22 use coding_exception; 23 use core_grades\component_gradeitem; 24 use core_external\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 /** 39 * Ensure that an execute with an invalid component is rejected. 40 */ 41 public function test_execute_invalid_component(): void { 42 $this->resetAfterTest(); 43 $user = $this->getDataGenerator()->create_user(); 44 $this->setUser($user); 45 46 $this->expectException(coding_exception::class); 47 $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component"); 48 fetch::execute('mod_invalid', 1, 'foo', 2); 49 } 50 51 /** 52 * Ensure that an execute with an invalid itemname on a valid component is rejected. 53 */ 54 public function test_execute_invalid_itemname(): void { 55 $this->resetAfterTest(); 56 $user = $this->getDataGenerator()->create_user(); 57 $this->setUser($user); 58 59 $this->expectException(coding_exception::class); 60 $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component"); 61 fetch::execute('mod_forum', 1, 'foo', 2); 62 } 63 64 /** 65 * Ensure that an execute against a different grading method is rejected. 66 */ 67 public function test_execute_incorrect_type(): void { 68 $this->resetAfterTest(); 69 70 $forum = $this->get_forum_instance([ 71 // Negative numbers mean a scale. 72 'grade_forum' => 5, 73 ]); 74 $course = $forum->get_course_record(); 75 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 76 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 77 $this->setUser($teacher); 78 79 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 80 81 $this->expectException(moodle_exception::class); 82 $this->expectExceptionMessage("not configured for grading with scales"); 83 fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); 84 } 85 86 /** 87 * Ensure that an execute against the correct grading method returns the current state of the user. 88 */ 89 public function test_execute_fetch_empty(): void { 90 $this->resetAfterTest(); 91 92 $options = [ 93 'A', 94 'B', 95 'C' 96 ]; 97 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 98 99 $forum = $this->get_forum_instance([ 100 // Negative numbers mean a scale. 101 'grade_forum' => -1 * $scale->id 102 ]); 103 $course = $forum->get_course_record(); 104 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 105 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 106 $this->setUser($teacher); 107 108 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 109 110 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); 111 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 112 113 $this->assertIsArray($result); 114 $this->assertArrayHasKey('templatename', $result); 115 116 $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']); 117 118 $this->assertArrayHasKey('warnings', $result); 119 $this->assertIsArray($result['warnings']); 120 $this->assertEmpty($result['warnings']); 121 122 // Test the grade array items. 123 $this->assertArrayHasKey('grade', $result); 124 $this->assertIsArray($result['grade']); 125 126 $this->assertIsInt($result['grade']['timecreated']); 127 $this->assertArrayHasKey('timemodified', $result['grade']); 128 $this->assertIsInt($result['grade']['timemodified']); 129 130 $this->assertArrayHasKey('usergrade', $result['grade']); 131 $this->assertEquals('-', $result['grade']['usergrade']); 132 133 $this->assertArrayHasKey('maxgrade', $result['grade']); 134 $this->assertIsInt($result['grade']['maxgrade']); 135 $this->assertEquals(3, $result['grade']['maxgrade']); 136 137 $this->assertArrayHasKey('gradedby', $result['grade']); 138 $this->assertEquals(null, $result['grade']['gradedby']); 139 140 $this->assertArrayHasKey('options', $result['grade']); 141 $this->assertCount(count($options), $result['grade']['options']); 142 rsort($options); 143 foreach ($options as $index => $option) { 144 $this->assertArrayHasKey($index, $result['grade']['options']); 145 146 $returnedoption = $result['grade']['options'][$index]; 147 $this->assertArrayHasKey('value', $returnedoption); 148 $this->assertEquals(3 - $index, $returnedoption['value']); 149 150 $this->assertArrayHasKey('title', $returnedoption); 151 $this->assertEquals($option, $returnedoption['title']); 152 153 $this->assertArrayHasKey('selected', $returnedoption); 154 $this->assertFalse($returnedoption['selected']); 155 } 156 } 157 158 /** 159 * Ensure that an execute against the correct grading method returns the current state of the user. 160 */ 161 public function test_execute_fetch_graded(): void { 162 $this->resetAfterTest(); 163 164 $options = [ 165 'A', 166 'B', 167 'C' 168 ]; 169 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 170 171 $forum = $this->get_forum_instance([ 172 // Negative numbers mean a scale. 173 'grade_forum' => -1 * $scale->id 174 ]); 175 $course = $forum->get_course_record(); 176 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 177 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 178 179 $this->execute_and_assert_fetch($forum, $options, $scale, $teacher, $teacher, $student); 180 } 181 182 /** 183 * Class mates should not get other's grades. 184 */ 185 public function test_execute_fetch_does_not_return_data_to_other_students(): void { 186 $this->resetAfterTest(); 187 188 $options = [ 189 'A', 190 'B', 191 'C' 192 ]; 193 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 194 195 $forum = $this->get_forum_instance([ 196 // Negative numbers mean a scale. 197 'grade_forum' => -1 * $scale->id 198 ]); 199 $course = $forum->get_course_record(); 200 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 201 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 202 $evilstudent = $this->getDataGenerator()->create_and_enrol($course, 'student'); 203 204 $this->expectException(\required_capability_exception::class); 205 $this->execute_and_assert_fetch($forum, $options, $scale, $evilstudent, $teacher, $student); 206 } 207 208 /** 209 * Grades can be returned to graded user. 210 */ 211 public function test_execute_fetch_return_data_to_graded_user(): void { 212 $this->resetAfterTest(); 213 214 $options = [ 215 'A', 216 'B', 217 'C' 218 ]; 219 $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]); 220 221 $forum = $this->get_forum_instance([ 222 // Negative numbers mean a scale. 223 'grade_forum' => -1 * $scale->id 224 ]); 225 $course = $forum->get_course_record(); 226 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 227 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 228 229 $this->execute_and_assert_fetch($forum, $options, $scale, $student, $teacher, $student); 230 } 231 232 /** 233 * Executes the fetch method with the given users and returns the result. 234 */ 235 private function execute_and_assert_fetch ($forum, $options, $scale, $fetcheruser, $grader, $gradeduser) { 236 237 $this->setUser($grader); 238 239 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 240 $gradeitem->store_grade_from_formdata($gradeduser, $grader, (object) [ 241 'grade' => 2, 242 ]); 243 244 $this->setUser($fetcheruser); 245 246 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id); 247 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 248 249 $this->assertIsArray($result); 250 $this->assertArrayHasKey('templatename', $result); 251 252 $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']); 253 254 $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id); 255 $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); 256 257 $this->assertIsArray($result); 258 $this->assertArrayHasKey('templatename', $result); 259 260 $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']); 261 262 $this->assertArrayHasKey('warnings', $result); 263 $this->assertIsArray($result['warnings']); 264 $this->assertEmpty($result['warnings']); 265 266 // Test the grade array items. 267 $this->assertArrayHasKey('grade', $result); 268 $this->assertIsArray($result['grade']); 269 270 $this->assertIsInt($result['grade']['timecreated']); 271 $this->assertArrayHasKey('timemodified', $result['grade']); 272 $this->assertIsInt($result['grade']['timemodified']); 273 274 $this->assertArrayHasKey('usergrade', $result['grade']); 275 $this->assertEquals('B', $result['grade']['usergrade']); 276 277 $this->assertArrayHasKey('maxgrade', $result['grade']); 278 $this->assertIsInt($result['grade']['maxgrade']); 279 $this->assertEquals(3, $result['grade']['maxgrade']); 280 281 $this->assertArrayHasKey('gradedby', $result['grade']); 282 $this->assertEquals(fullname($grader), $result['grade']['gradedby']); 283 284 $this->assertArrayHasKey('options', $result['grade']); 285 $this->assertCount(count($options), $result['grade']['options']); 286 rsort($options); 287 foreach ($options as $index => $option) { 288 $this->assertArrayHasKey($index, $result['grade']['options']); 289 290 $returnedoption = $result['grade']['options'][$index]; 291 $this->assertArrayHasKey('value', $returnedoption); 292 $this->assertEquals(3 - $index, $returnedoption['value']); 293 294 $this->assertArrayHasKey('title', $returnedoption); 295 $this->assertEquals($option, $returnedoption['title']); 296 297 $this->assertArrayHasKey('selected', $returnedoption); 298 } 299 300 // The grade was 2, which relates to the middle option. 301 $this->assertFalse($result['grade']['options'][0]['selected']); 302 $this->assertTrue($result['grade']['options'][1]['selected']); 303 $this->assertFalse($result['grade']['options'][2]['selected']); 304 } 305 306 /** 307 * Get a forum instance. 308 * 309 * @param array $config 310 * @return forum_entity 311 */ 312 protected function get_forum_instance(array $config = []): forum_entity { 313 $this->resetAfterTest(); 314 315 $datagenerator = $this->getDataGenerator(); 316 $course = $datagenerator->create_course(); 317 $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id])); 318 319 $vaultfactory = \mod_forum\local\container::get_vault_factory(); 320 $vault = $vaultfactory->get_forum_vault(); 321 322 return $vault->get_from_id((int) $forum->id); 323 } 324 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body