Differences Between: [Versions 310 and 402] [Versions 39 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 /** 18 * Generator testcase for the gradingforum_rubric generator. 19 * 20 * @package gradingform_rubric 21 * @category test 22 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace gradingform_rubric; 27 28 use advanced_testcase; 29 use context_module; 30 use gradingform_rubric_controller; 31 use gradingform_controller; 32 33 /** 34 * Generator testcase for the gradingforum_rubric generator. 35 * 36 * @package gradingform_rubric 37 * @category test 38 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class generator_test extends advanced_testcase { 42 43 /** 44 * Test rubric creation. 45 */ 46 public function test_rubric_creation(): void { 47 $this->resetAfterTest(true); 48 49 // Fetch generators. 50 $generator = \testing_util::get_data_generator(); 51 $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric'); 52 53 // Create items required for testing. 54 $course = $generator->create_course(); 55 $module = $generator->create_module('assign', ['course' => $course]); 56 $user = $generator->create_user(); 57 $context = context_module::instance($module->cmid); 58 59 // Data for testing. 60 $name = 'myfirstrubric'; 61 $description = 'My first rubric'; 62 $criteria = [ 63 'Alphabet' => [ 64 'Not known' => 0, 65 'Letters known but out of order' => 1, 66 'Letters known in order ascending' => 2, 67 'Letters known and can recite forwards and backwards' => 4, 68 ], 69 'Times tables' => [ 70 'Not known' => 0, 71 '2 times table known' => 2, 72 '2 and 5 times table known' => 4, 73 '2, 5, and 10 times table known' => 8, 74 ], 75 ]; 76 77 // Unit under test. 78 $this->setUser($user); 79 $controller = $rubricgenerator->create_instance($context, 'mod_assign', 'submission', $name, $description, $criteria); 80 81 $this->assertInstanceOf(gradingform_rubric_controller::class, $controller); 82 83 $definition = $controller->get_definition(); 84 $this->assertNotEmpty($definition->id); 85 $this->assertEquals($name, $definition->name); 86 $this->assertEquals($description, $definition->description); 87 $this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status); 88 $this->assertNotEmpty($definition->timecreated); 89 $this->assertNotEmpty($definition->timemodified); 90 $this->assertEquals($user->id, $definition->usercreated); 91 92 $this->assertNotEmpty($definition->rubric_criteria); 93 $this->assertCount(2, $definition->rubric_criteria); 94 95 // Check the alphabet criteria. 96 $criteriaids = array_keys($definition->rubric_criteria); 97 98 $alphabet = $definition->rubric_criteria[$criteriaids[0]]; 99 $this->assertNotEmpty($alphabet['id']); 100 $this->assertEquals(1, $alphabet['sortorder']); 101 $this->assertEquals('Alphabet', $alphabet['description']); 102 103 $this->assertNotEmpty($alphabet['levels']); 104 $levels = $alphabet['levels']; 105 $levelids = array_keys($levels); 106 107 $level = $levels[$levelids[0]]; 108 $this->assertEquals(0, $level['score']); 109 $this->assertEquals('Not known', $level['definition']); 110 111 $level = $levels[$levelids[1]]; 112 $this->assertEquals(1, $level['score']); 113 $this->assertEquals('Letters known but out of order', $level['definition']); 114 115 $level = $levels[$levelids[2]]; 116 $this->assertEquals(2, $level['score']); 117 $this->assertEquals('Letters known in order ascending', $level['definition']); 118 119 $level = $levels[$levelids[3]]; 120 $this->assertEquals(4, $level['score']); 121 $this->assertEquals('Letters known and can recite forwards and backwards', $level['definition']); 122 123 // Check the times tables criteria. 124 $tables = $definition->rubric_criteria[$criteriaids[1]]; 125 $this->assertNotEmpty($tables['id']); 126 $this->assertEquals(2, $tables['sortorder']); 127 $this->assertEquals('Times tables', $tables['description']); 128 129 $this->assertNotEmpty($tables['levels']); 130 $levels = $tables['levels']; 131 $levelids = array_keys($levels); 132 133 $level = $levels[$levelids[0]]; 134 $this->assertEquals(0, $level['score']); 135 $this->assertEquals('Not known', $level['definition']); 136 137 $level = $levels[$levelids[1]]; 138 $this->assertEquals(2, $level['score']); 139 $this->assertEquals('2 times table known', $level['definition']); 140 141 $level = $levels[$levelids[2]]; 142 $this->assertEquals(4, $level['score']); 143 $this->assertEquals('2 and 5 times table known', $level['definition']); 144 145 $level = $levels[$levelids[3]]; 146 $this->assertEquals(8, $level['score']); 147 $this->assertEquals('2, 5, and 10 times table known', $level['definition']); 148 } 149 150 /** 151 * Test the get_level_and_criterion_for_values function. 152 * This is used for finding criterion and level information within a rubric. 153 */ 154 public function test_get_level_and_criterion_for_values(): void { 155 $this->resetAfterTest(true); 156 157 // Fetch generators. 158 $generator = \testing_util::get_data_generator(); 159 $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric'); 160 161 // Create items required for testing. 162 $course = $generator->create_course(); 163 $module = $generator->create_module('assign', ['course' => $course]); 164 $user = $generator->create_user(); 165 $context = context_module::instance($module->cmid); 166 167 // Data for testing. 168 $description = 'My first rubric'; 169 $criteria = [ 170 'Alphabet' => [ 171 'Not known' => 0, 172 'Letters known but out of order' => 1, 173 'Letters known in order ascending' => 2, 174 'Letters known and can recite forwards and backwards' => 4, 175 ], 176 'Times tables' => [ 177 'Not known' => 0, 178 '2 times table known' => 2, 179 '2 and 5 times table known' => 4, 180 '2, 5, and 10 times table known' => 8, 181 ], 182 ]; 183 184 $this->setUser($user); 185 $controller = $rubricgenerator->create_instance($context, 'mod_assign', 'submission', 'rubric', $description, $criteria); 186 187 // Valid criterion and level. 188 $result = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Alphabet', 2); 189 $this->assertEquals('Alphabet', $result['criterion']->description); 190 $this->assertEquals('2', $result['level']->score); 191 $this->assertEquals('Letters known in order ascending', $result['level']->definition); 192 193 // Valid criterion. Invalid level. 194 $result = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Alphabet', 3); 195 $this->assertEquals('Alphabet', $result['criterion']->description); 196 $this->assertNull($result['level']); 197 198 // Invalid criterion. 199 $result = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Foo', 0); 200 $this->assertNull($result['criterion']); 201 } 202 203 /** 204 * Tests for the get_test_rubric function. 205 */ 206 public function test_get_test_rubric(): void { 207 $this->resetAfterTest(true); 208 209 // Fetch generators. 210 $generator = \testing_util::get_data_generator(); 211 $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric'); 212 213 // Create items required for testing. 214 $course = $generator->create_course(); 215 $module = $generator->create_module('assign', ['course' => $course]); 216 $user = $generator->create_user(); 217 $context = context_module::instance($module->cmid); 218 219 $this->setUser($user); 220 $rubric = $rubricgenerator->get_test_rubric($context, 'assign', 'submissions'); 221 $definition = $rubric->get_definition(); 222 223 $this->assertEquals('testrubric', $definition->name); 224 $this->assertEquals('Description text', $definition->description); 225 $this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status); 226 227 // Should create a rubric with 2 criterion. 228 $this->assertCount(2, $definition->rubric_criteria); 229 } 230 231 /** 232 * Test the get_submitted_form_data function. 233 */ 234 public function test_get_submitted_form_data(): void { 235 $this->resetAfterTest(true); 236 237 // Fetch generators. 238 $generator = \testing_util::get_data_generator(); 239 $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric'); 240 241 // Create items required for testing. 242 $course = $generator->create_course(); 243 $module = $generator->create_module('assign', ['course' => $course]); 244 $user = $generator->create_user(); 245 $context = context_module::instance($module->cmid); 246 247 $this->setUser($user); 248 $controller = $rubricgenerator->get_test_rubric($context, 'assign', 'submissions'); 249 250 $result = $rubricgenerator->get_submitted_form_data($controller, 93, [ 251 'Spelling is important' => [ 252 'score' => 1, 253 'remark' => 'Good speeling', 254 ], 255 'Pictures' => [ 256 'score' => 2, 257 'remark' => 'Lots of nice pictures!', 258 ] 259 ]); 260 261 $this->assertIsArray($result); 262 $this->assertEquals(93, $result['itemid']); 263 $this->assertIsArray($result['criteria']); 264 $this->assertCount(2, $result['criteria']); 265 266 $spelling = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Spelling is important', 1); 267 $this->assertIsArray($result['criteria'][$spelling['criterion']->id]); 268 $this->assertEquals($spelling['level']->id, $result['criteria'][$spelling['criterion']->id]['levelid']); 269 $this->assertEquals('Good speeling', $result['criteria'][$spelling['criterion']->id]['remark']); 270 271 $pictures = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Pictures', 2); 272 $this->assertIsArray($result['criteria'][$pictures['criterion']->id]); 273 $this->assertEquals($pictures['level']->id, $result['criteria'][$pictures['criterion']->id]['levelid']); 274 $this->assertEquals('Lots of nice pictures!', $result['criteria'][$pictures['criterion']->id]['remark']); 275 } 276 277 /** 278 * Test the get_test_form_data function. 279 */ 280 public function test_get_test_form_data(): void { 281 $this->resetAfterTest(true); 282 283 // Fetch generators. 284 $generator = \testing_util::get_data_generator(); 285 $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric'); 286 287 // Create items required for testing. 288 $course = $generator->create_course(); 289 $module = $generator->create_module('assign', ['course' => $course]); 290 $user = $generator->create_user(); 291 $context = context_module::instance($module->cmid); 292 293 $this->setUser($user); 294 $controller = $rubricgenerator->get_test_rubric($context, 'assign', 'submissions'); 295 296 // Unit under test. 297 $result = $rubricgenerator->get_test_form_data( 298 $controller, 299 1839, 300 1, 'Propper good speling', 301 0, 'ASCII art is not a picture' 302 ); 303 304 $this->assertIsArray($result); 305 $this->assertEquals(1839, $result['itemid']); 306 $this->assertIsArray($result['criteria']); 307 $this->assertCount(2, $result['criteria']); 308 309 $spelling = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Spelling is important', 1); 310 $this->assertIsArray($result['criteria'][$spelling['criterion']->id]); 311 $this->assertEquals($spelling['level']->id, $result['criteria'][$spelling['criterion']->id]['levelid']); 312 $this->assertEquals('Propper good speling', $result['criteria'][$spelling['criterion']->id]['remark']); 313 314 $pictures = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Pictures', 0); 315 $this->assertIsArray($result['criteria'][$pictures['criterion']->id]); 316 $this->assertEquals($pictures['level']->id, $result['criteria'][$pictures['criterion']->id]['levelid']); 317 $this->assertEquals('ASCII art is not a picture', $result['criteria'][$pictures['criterion']->id]['remark']); 318 } 319 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body