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