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 gradingform_guide 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 gradingform_guide\grades\grader\gradingpanel\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 gradingform_guide 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 store_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 store::execute('mod_invalid', 1, 'foo', 2, false, 'formdata'); 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 store::execute('mod_forum', 1, 'foo', 2, false, 'formdata'); 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 'grade_forum' => 5, 86 ]); 87 $course = $forum->get_course_record(); 88 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 89 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 90 $this->setUser($teacher); 91 92 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 93 94 $this->expectException(moodle_exception::class); 95 $this->expectExceptionMessage("not configured for advanced grading with a marking guide"); 96 store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, 'formdata'); 97 } 98 99 /** 100 * Ensure that an execute against a different grading method is rejected. 101 */ 102 public function test_execute_disabled(): void { 103 $this->resetAfterTest(); 104 105 $forum = $this->get_forum_instance(); 106 $course = $forum->get_course_record(); 107 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 108 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 109 $this->setUser($teacher); 110 111 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 112 113 $this->expectException(moodle_exception::class); 114 $this->expectExceptionMessage("Grading is not enabled"); 115 store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, 'formdata'); 116 } 117 118 /** 119 * Ensure that an execute against the correct grading method returns the current state of the user. 120 */ 121 public function test_execute_store_graded(): void { 122 $this->resetAfterTest(); 123 $generator = \testing_util::get_data_generator(); 124 $guidegenerator = $generator->get_plugin_generator('gradingform_guide'); 125 126 [ 127 'forum' => $forum, 128 'controller' => $controller, 129 'definition' => $definition, 130 'student' => $student, 131 'teacher' => $teacher, 132 ] = $this->get_test_data(); 133 134 $this->setUser($teacher); 135 136 $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); 137 $grade = $gradeitem->get_grade_for_user($student, $teacher); 138 $instance = $gradeitem->get_advanced_grading_instance($teacher, $grade); 139 140 $submissiondata = $guidegenerator->get_test_form_data($controller, (int) $student->id, 141 10, 'Propper good speling', 142 0, 'ASCII art is not a picture' 143 ); 144 145 $formdata = http_build_query((object) [ 146 'instanceid' => $instance->get_id(), 147 'advancedgrading' => $submissiondata, 148 ], '', '&'); 149 150 $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, $formdata); 151 $result = external_api::clean_returnvalue(store::execute_returns(), $result); 152 153 $this->assertIsArray($result); 154 $this->assertArrayHasKey('templatename', $result); 155 156 $this->assertEquals('gradingform_guide/grades/grader/gradingpanel', $result['templatename']); 157 158 $this->assertArrayHasKey('warnings', $result); 159 $this->assertIsArray($result['warnings']); 160 $this->assertEmpty($result['warnings']); 161 162 // Test the grade array items. 163 $this->assertArrayHasKey('grade', $result); 164 $this->assertIsArray($result['grade']); 165 $this->assertIsInt($result['grade']['timecreated']); 166 167 $this->assertArrayHasKey('timemodified', $result['grade']); 168 $this->assertIsInt($result['grade']['timemodified']); 169 170 $this->assertArrayHasKey('usergrade', $result['grade']); 171 $this->assertEquals('0.50 / 2.00', $result['grade']['usergrade']); 172 173 $this->assertArrayHasKey('maxgrade', $result['grade']); 174 $this->assertIsInt($result['grade']['maxgrade']); 175 $this->assertEquals(2, $result['grade']['maxgrade']); 176 177 $this->assertArrayHasKey('gradedby', $result['grade']); 178 $this->assertEquals(fullname($teacher), $result['grade']['gradedby']); 179 180 $this->assertArrayHasKey('criterion', $result['grade']); 181 $criteria = $result['grade']['criterion']; 182 $this->assertCount(count($definition->guide_criteria), $criteria); 183 foreach ($criteria as $criterion) { 184 $this->assertArrayHasKey('id', $criterion); 185 $criterionid = $criterion['id']; 186 $sourcecriterion = $definition->guide_criteria[$criterionid]; 187 188 $this->assertArrayHasKey('name', $criterion); 189 $this->assertEquals($sourcecriterion['shortname'], $criterion['name']); 190 191 $this->assertArrayHasKey('maxscore', $criterion); 192 $this->assertEquals($sourcecriterion['maxscore'], $criterion['maxscore']); 193 194 $this->assertArrayHasKey('description', $criterion); 195 $this->assertEquals($sourcecriterion['description'], $criterion['description']); 196 197 $this->assertArrayHasKey('descriptionmarkers', $criterion); 198 $this->assertEquals($sourcecriterion['descriptionmarkers'], $criterion['descriptionmarkers']); 199 200 $this->assertArrayHasKey('score', $criterion); 201 $this->assertArrayHasKey('remark', $criterion); 202 } 203 204 $this->assertEquals(10, $criteria[0]['score']); 205 $this->assertEquals('Propper good speling', $criteria[0]['remark']); 206 $this->assertEquals(0, $criteria[1]['score']); 207 $this->assertEquals('ASCII art is not a picture', $criteria[1]['remark']); 208 } 209 210 /** 211 * Get a forum instance. 212 * 213 * @param array $config 214 * @return forum_entity 215 */ 216 protected function get_forum_instance(array $config = []): forum_entity { 217 $this->resetAfterTest(); 218 219 $datagenerator = $this->getDataGenerator(); 220 $course = $datagenerator->create_course(); 221 $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id])); 222 223 $vaultfactory = \mod_forum\local\container::get_vault_factory(); 224 $vault = $vaultfactory->get_forum_vault(); 225 226 return $vault->get_from_id((int) $forum->id); 227 } 228 229 /** 230 * Get test data for forums graded using a marking guide. 231 * 232 * @return array 233 */ 234 protected function get_test_data(): array { 235 global $DB; 236 237 $this->resetAfterTest(); 238 239 $generator = \testing_util::get_data_generator(); 240 $guidegenerator = $generator->get_plugin_generator('gradingform_guide'); 241 242 $forum = $this->get_forum_instance(); 243 $course = $forum->get_course_record(); 244 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 245 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 246 247 $this->setUser($teacher); 248 $controller = $guidegenerator->get_test_guide($forum->get_context(), 'forum', 'forum'); 249 $definition = $controller->get_definition(); 250 251 $DB->set_field('forum', 'grade_forum', count($definition->guide_criteria), ['id' => $forum->get_id()]); 252 return [ 253 'forum' => $forum, 254 'controller' => $controller, 255 'definition' => $definition, 256 'student' => $student, 257 'teacher' => $teacher, 258 ]; 259 } 260 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body