Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 * External function test for get_results. 19 * 20 * @package mod_h5pactivity 21 * @category external 22 * @since Moodle 3.9 23 * @copyright 2020 Ferran Recio <ferran@moodle.com> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 namespace mod_h5pactivity\external; 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 global $CFG; 32 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 33 34 use mod_h5pactivity\local\manager; 35 use external_api; 36 use externallib_advanced_testcase; 37 use dml_missing_record_exception; 38 39 /** 40 * External function test for get_results. 41 * 42 * @package mod_h5pactivity 43 * @copyright 2020 Ferran Recio <ferran@moodle.com> 44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 45 */ 46 class get_results_test extends externallib_advanced_testcase { 47 48 /** 49 * Test the behaviour of get_results. 50 * 51 * @dataProvider execute_data 52 * @param int $enabletracking the activity tracking enable 53 * @param int $reviewmode the activity review mode 54 * @param string $loginuser the user which calls the webservice 55 * @param string|null $participant the user to get the data 56 * @param bool $createattempts if the student user has attempts created 57 * @param int|null $count the expected number of attempts returned (null for exception) 58 */ 59 public function test_execute(int $enabletracking, int $reviewmode, string $loginuser, 60 ?string $participant, bool $createattempts, ?int $count): void { 61 62 $this->resetAfterTest(); 63 $this->setAdminUser(); 64 65 $course = $this->getDataGenerator()->create_course(); 66 $activity = $this->getDataGenerator()->create_module('h5pactivity', 67 ['course' => $course, 'enabletracking' => $enabletracking, 'reviewmode' => $reviewmode]); 68 69 $manager = manager::create_from_instance($activity); 70 $cm = $manager->get_coursemodule(); 71 72 // Prepare users: 1 teacher, 1 student and 1 unenroled user. 73 $users = [ 74 'editingteacher' => $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'), 75 'student' => $this->getDataGenerator()->create_and_enrol($course, 'student'), 76 'other' => $this->getDataGenerator()->create_and_enrol($course, 'student'), 77 ]; 78 79 $attempts = []; 80 81 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); 82 83 if ($createattempts) { 84 $user = $users['student']; 85 $params = ['cmid' => $cm->id, 'userid' => $user->id]; 86 $attempts['student'] = $generator->create_content($activity, $params); 87 } 88 89 // Create another 2 attempts for the user "other" to validate no cross attempts are returned. 90 $user = $users['other']; 91 $params = ['cmid' => $cm->id, 'userid' => $user->id]; 92 $attempts['other'] = $generator->create_content($activity, $params); 93 94 // Execute external method. 95 $this->setUser($users[$loginuser]); 96 97 $attemptid = $attempts[$participant]->id ?? 0; 98 99 $result = get_results::execute($activity->id, [$attemptid]); 100 $result = external_api::clean_returnvalue( 101 get_results::execute_returns(), 102 $result 103 ); 104 105 // Validate general structure. 106 $this->assertArrayHasKey('activityid', $result); 107 $this->assertArrayHasKey('attempts', $result); 108 $this->assertArrayHasKey('warnings', $result); 109 110 $this->assertEquals($activity->id, $result['activityid']); 111 112 if ($count === null) { 113 $this->assertCount(1, $result['warnings']); 114 $this->assertCount(0, $result['attempts']); 115 return; 116 } 117 118 $this->assertCount(0, $result['warnings']); 119 $this->assertCount(1, $result['attempts']); 120 121 // Validate attempt. 122 $attempt = $result['attempts'][0]; 123 $this->assertEquals($attemptid, $attempt['id']); 124 125 // Validate results. 126 $this->assertArrayHasKey('results', $attempt); 127 $this->assertCount($count, $attempt['results']); 128 foreach ($attempt['results'] as $value) { 129 $this->assertEquals($attemptid, $value['attemptid']); 130 $this->assertArrayHasKey('subcontent', $value); 131 $this->assertArrayHasKey('rawscore', $value); 132 $this->assertArrayHasKey('maxscore', $value); 133 $this->assertArrayHasKey('duration', $value); 134 $this->assertArrayHasKey('track', $value); 135 if (isset($value['options'])) { 136 foreach ($value['options'] as $option) { 137 $this->assertArrayHasKey('description', $option); 138 $this->assertArrayHasKey('id', $option); 139 } 140 } 141 } 142 } 143 144 /** 145 * Data provider for the test_execute tests. 146 * 147 * @return array 148 */ 149 public function execute_data(): array { 150 return [ 151 'Teacher reviewing an attempt' => [ 152 1, manager::REVIEWCOMPLETION, 'editingteacher', 'student', true, 1 153 ], 154 'Teacher try to review an inexistent attempt' => [ 155 1, manager::REVIEWCOMPLETION, 'editingteacher', 'student', false, null 156 ], 157 'Teacher reviewing attempt with student review mode off' => [ 158 1, manager::REVIEWNONE, 'editingteacher', 'student', true, 1 159 ], 160 'Student reviewing own attempt' => [ 161 1, manager::REVIEWCOMPLETION, 'student', 'student', true, 1 162 ], 163 'Student reviewing an inexistent attempt' => [ 164 1, manager::REVIEWCOMPLETION, 'student', 'student', false, null 165 ], 166 'Student reviewing own attempt with review mode off' => [ 167 1, manager::REVIEWNONE, 'student', 'student', true, null 168 ], 169 'Student try to stalk other student attempt' => [ 170 1, manager::REVIEWCOMPLETION, 'student', 'other', false, null 171 ], 172 'Teacher trying to review an attempt without tracking enabled' => [ 173 0, manager::REVIEWNONE, 'editingteacher', 'student', true, null 174 ], 175 'Student trying to review an attempt without tracking enabled' => [ 176 0, manager::REVIEWNONE, 'editingteacher', 'student', true, null 177 ], 178 'Student trying to stalk another student attempt without tracking enabled' => [ 179 0, manager::REVIEWNONE, 'editingteacher', 'student', true, null 180 ], 181 ]; 182 } 183 184 /** 185 * Test the behaviour of get_results. 186 * 187 * @dataProvider execute_multipleattempts_data 188 * @param string $loginuser the user which calls the webservice 189 * @param array $getattempts the attempts to get the data 190 * @param array $warnings warnigns expected 191 * @param array $reports data expected 192 * 193 */ 194 public function test_execute_multipleattempts(string $loginuser, 195 array $getattempts, array $warnings, array $reports): void { 196 197 $this->resetAfterTest(); 198 $this->setAdminUser(); 199 200 $course = $this->getDataGenerator()->create_course(); 201 $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); 202 203 $manager = manager::create_from_instance($activity); 204 $cm = $manager->get_coursemodule(); 205 206 // Prepare users: 1 teacher, 2 student. 207 $users = [ 208 'editingteacher' => $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'), 209 'student1' => $this->getDataGenerator()->create_and_enrol($course, 'student'), 210 'student2' => $this->getDataGenerator()->create_and_enrol($course, 'student'), 211 ]; 212 213 $attempts = []; 214 215 // Generate attempts for student 1 and 2. 216 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); 217 218 $user = $users['student1']; 219 $params = ['cmid' => $cm->id, 'userid' => $user->id]; 220 $attempts['student1_1'] = $generator->create_content($activity, $params); 221 $attempts['student1_2'] = $generator->create_content($activity, $params); 222 223 $user = $users['student2']; 224 $params = ['cmid' => $cm->id, 'userid' => $user->id]; 225 $attempts['student2_1'] = $generator->create_content($activity, $params); 226 $attempts['student2_2'] = $generator->create_content($activity, $params); 227 228 // Execute external method. 229 $this->setUser($users[$loginuser]); 230 231 $attemptids = []; 232 foreach ($getattempts as $getattempt) { 233 $attemptids[] = $attempts[$getattempt]->id ?? 0; 234 } 235 236 $result = get_results::execute($activity->id, $attemptids); 237 $result = external_api::clean_returnvalue( 238 get_results::execute_returns(), 239 $result 240 ); 241 242 // Validate general structure. 243 $this->assertArrayHasKey('activityid', $result); 244 $this->assertArrayHasKey('attempts', $result); 245 $this->assertArrayHasKey('warnings', $result); 246 247 $this->assertEquals($activity->id, $result['activityid']); 248 249 $this->assertCount(count($warnings), $result['warnings']); 250 $this->assertCount(count($reports), $result['attempts']); 251 252 // Validate warnings. 253 $expectedwarnings = []; 254 foreach ($warnings as $warningattempt) { 255 $id = $attempts[$warningattempt]->id ?? 0; 256 $expectedwarnings[$id] = $warningattempt; 257 } 258 foreach ($result['warnings'] as $warning) { 259 $this->assertEquals('h5pactivity_attempts', $warning['item']); 260 $this->assertEquals(1, $warning['warningcode']); 261 $this->assertArrayHasKey($warning['itemid'], $expectedwarnings); 262 } 263 264 // Validate attempts. 265 $expectedattempts = []; 266 foreach ($reports as $expectedattempt) { 267 $id = $attempts[$expectedattempt]->id; 268 $expectedattempts[$id] = $expectedattempt; 269 } 270 foreach ($result['attempts'] as $value) { 271 $this->assertArrayHasKey($value['id'], $expectedattempts); 272 } 273 } 274 275 /** 276 * Data provider for the test_execute_multipleattempts tests. 277 * 278 * @return array 279 */ 280 public function execute_multipleattempts_data(): array { 281 return [ 282 // Teacher cases. 283 'Teacher reviewing students attempts' => [ 284 'editingteacher', ['student1_1', 'student2_1'], [], ['student1_1', 'student2_1'] 285 ], 286 'Teacher reviewing invalid attempt' => [ 287 'editingteacher', ['student1_1', 'invalid'], ['invalid'], ['student1_1'] 288 ], 289 'Teacher reviewing empty attempts list' => [ 290 'editingteacher', [], [], [] 291 ], 292 // Student cases. 293 'Student reviewing own students attempts' => [ 294 'student1', ['student1_1', 'student1_2'], [], ['student1_1', 'student1_2'] 295 ], 296 'Student reviewing invalid attempt' => [ 297 'student1', ['student1_1', 'invalid'], ['invalid'], ['student1_1'] 298 ], 299 'Student reviewing trying to access another user attempts' => [ 300 'student1', ['student1_1', 'student2_1'], ['student2_1'], ['student1_1'] 301 ], 302 'Student reviewing empty attempts list' => [ 303 'student1', [], [], ['student1_1', 'student1_2'] 304 ], 305 ]; 306 } 307 308 /** 309 * Test the behaviour of get_results using mixed activityid. 310 * 311 * @dataProvider execute_mixactivities_data 312 * @param string $activityname the activity name to use 313 * @param string $attemptname the attempt name to use 314 * @param string $expectedwarnings expected warning attempt 315 * @param string $expectedattempt expected result attempt 316 * 317 */ 318 public function test_execute_mixactivities(string $activityname, string $attemptname, 319 string $expectedwarnings, string $expectedattempt): void { 320 321 $this->resetAfterTest(); 322 $this->setAdminUser(); 323 324 // Create 2 courses. 325 $course1 = $this->getDataGenerator()->create_course(); 326 $course2 = $this->getDataGenerator()->create_course(); 327 328 // Prepare users: 1 teacher, 1 student. 329 $user = $this->getDataGenerator()->create_and_enrol($course1, 'student'); 330 $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student'); 331 332 // Create our base activity. 333 $activity11 = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course1]); 334 $manager11 = manager::create_from_instance($activity11); 335 $cm11 = $manager11->get_coursemodule(); 336 337 // Create a second activity in the same course to check if the retuned attempt is the correct one. 338 $activity12 = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course1]); 339 $manager12 = manager::create_from_instance($activity12); 340 $cm12 = $manager12->get_coursemodule(); 341 342 // Create a second activity on a different course. 343 $activity21 = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course2]); 344 $manager21 = manager::create_from_instance($activity21); 345 $cm21 = $manager21->get_coursemodule(); 346 347 $activities = [ 348 '11' => $activity11->id, 349 '12' => $activity12->id, 350 '21' => $activity21->id, 351 'inexistent' => 0, 352 ]; 353 354 // Generate attempts. 355 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); 356 357 $params = ['cmid' => $cm11->id, 'userid' => $user->id]; 358 $attempt11 = $generator->create_content($activity11, $params); 359 $params = ['cmid' => $cm12->id, 'userid' => $user->id]; 360 $attempt12 = $generator->create_content($activity12, $params); 361 $params = ['cmid' => $cm21->id, 'userid' => $user->id]; 362 $attempt21 = $generator->create_content($activity21, $params); 363 364 $attempts = [ 365 '11' => $attempt11->id, 366 '12' => $attempt12->id, 367 '21' => $attempt21->id, 368 'inexistent' => 0, 369 ]; 370 371 if ($activityname == 'inexistent') { 372 $this->expectException(dml_missing_record_exception::class); 373 } 374 375 // Execute external method. 376 $this->setUser($user); 377 378 $attemptid = $attempts[$attemptname]; 379 380 $result = get_results::execute($activities[$activityname], [$attemptid]); 381 $result = external_api::clean_returnvalue( 382 get_results::execute_returns(), 383 $result 384 ); 385 386 // Validate general structure. 387 $this->assertArrayHasKey('activityid', $result); 388 $this->assertArrayHasKey('attempts', $result); 389 $this->assertArrayHasKey('warnings', $result); 390 391 if (empty($expectedwarnings)) { 392 $this->assertEmpty($result['warnings']); 393 } else { 394 $this->assertEquals('h5pactivity_attempts', $result['warnings'][0]['item']); 395 $this->assertEquals(1, $result['warnings'][0]['warningcode']); 396 $this->assertEquals($attempts[$expectedwarnings], $result['warnings'][0]['itemid']); 397 } 398 399 if (empty($expectedattempt)) { 400 $this->assertEmpty($result['attempts']); 401 } else { 402 $this->assertEquals($attempts[$expectedattempt], $result['attempts'][0]['id']); 403 } 404 } 405 406 /** 407 * Data provider for the test_execute_multipleattempts tests. 408 * 409 * @return array 410 */ 411 public function execute_mixactivities_data(): array { 412 return [ 413 // Teacher cases. 414 'Correct activity id' => [ 415 '11', '11', '', '11' 416 ], 417 'Wrong activity id' => [ 418 '21', '11', '11', '' 419 ], 420 'Inexistent activity id' => [ 421 'inexistent', '11', '', '' 422 ], 423 'Inexistent attempt id' => [ 424 '11', 'inexistent', 'inexistent', '' 425 ], 426 ]; 427 } 428 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body