MDL-77564 Quiz display options: Hide or show the grade information
[moodle.git] / mod / quiz / tests / restore_attempt_test.php
blob5fd21c18c9b079674ae6da1aade08dd2bc65910f
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.
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/>.
17 namespace mod_quiz;
19 use backup;
20 use core_user;
21 use restore_controller;
22 use restore_dbops;
24 /**
25 * Unit tests restoring quiz attempts
27 * @package mod_quiz
28 * @copyright 2021 Paul Holden <paulh@moodle.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class restore_attempt_test extends \advanced_testcase {
33 /**
34 * Load required libraries
36 public static function setUpBeforeClass(): void {
37 global $CFG;
39 require_once("{$CFG->dirroot}/backup/util/includes/restore_includes.php");
42 /**
43 * Test restore dates.
45 * @covers \restore_quiz_activity_structure_step
47 public function test_restore_question_attempts_missing_users(): void {
48 global $DB, $USER;
50 $this->resetAfterTest();
51 $this->setAdminUser();
53 // Contains a quiz with four attempts from different users. The users are as follows (user ID -> user):
54 // 3 -> User 01, 4 -> User 02, 5 -> User 03, 6 -> User 04.
55 // The user details for User 02 and User 03 have been removed from the backup file.
56 $testfixture = __DIR__ . '/fixtures/question_attempts_missing_users.mbz';
58 // Extract our test fixture, ready to be restored.
59 $backuptempdir = 'aaa';
60 $backuppath = make_backup_temp_directory($backuptempdir);
61 get_file_packer('application/vnd.moodle.backup')->extract_to_pathname($testfixture, $backuppath);
63 // Do the restore to new course with default settings.
64 $categoryid = $DB->get_field('course_categories', 'MIN(id)', []);
65 $courseid = restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid);
67 $controller = new restore_controller($backuptempdir, $courseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
68 backup::TARGET_NEW_COURSE);
70 $this->assertTrue($controller->execute_precheck());
71 $controller->execute_plan();
72 $controller->destroy();
74 // Grade restore also generates some debugging.
75 $this->assertDebuggingCalledCount(2);
77 $restoredquiz = $DB->get_record('quiz', []);
79 // Assert logs were added for User 02 and User 03, due to missing mapping lookup.
80 $loginfomessages = $DB->get_fieldset_select('backup_logs', 'message', 'backupid = ? AND loglevel = ?', [
81 $controller->get_restoreid(),
82 backup::LOG_INFO,
83 ]);
85 $this->assertContains("Mapped user ID not found for user 4, quiz {$restoredquiz->id}, attempt 1. Skipping quiz attempt",
86 $loginfomessages);
87 $this->assertContains("Mapped user ID not found for user 5, quiz {$restoredquiz->id}, attempt 1. Skipping quiz attempt",
88 $loginfomessages);
90 // User 01 has supplied the wrong answer, assert dates match the backup file too.
91 $user01attempt = $DB->get_record('quiz_attempts', [
92 'quiz' => $restoredquiz->id,
93 'userid' => core_user::get_user_by_username('user01')->id,
94 ]);
96 $this->assertEquals(1634751274, $user01attempt->timestart);
97 $this->assertEquals(1634751290, $user01attempt->timefinish);
98 $this->assertEquals(0.0, (float) $user01attempt->sumgrades);
100 // User 04 has supplied the correct answer, assert dates match the backup file too.
101 $user04attempt = $DB->get_record('quiz_attempts', [
102 'quiz' => $restoredquiz->id,
103 'userid' => core_user::get_user_by_username('user04')->id,
106 $this->assertEquals(1634751341, $user04attempt->timestart);
107 $this->assertEquals(1634751347, $user04attempt->timefinish);
108 $this->assertEquals(1.0, (float) $user04attempt->sumgrades);