2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
21 use restore_controller
;
25 * Unit tests restoring quiz attempts
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
{
34 * Load required libraries
36 public static function setUpBeforeClass(): void
{
39 require_once("{$CFG->dirroot}/backup/util/includes/restore_includes.php");
45 * @covers \restore_quiz_activity_structure_step
47 public function test_restore_question_attempts_missing_users(): void
{
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(),
85 $this->assertContains("Mapped user ID not found for user 4, quiz {$restoredquiz->id}, attempt 1. Skipping quiz attempt",
87 $this->assertContains("Mapped user ID not found for user 5, quiz {$restoredquiz->id}, attempt 1. Skipping quiz attempt",
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
,
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
);