Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401]

   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  namespace mod_quiz;
  18  
  19  use backup;
  20  use core_user;
  21  use restore_controller;
  22  use restore_dbops;
  23  
  24  /**
  25   * Unit tests restoring quiz attempts
  26   *
  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
  30   */
  31  class restore_attempt_test extends \advanced_testcase {
  32  
  33      /**
  34       * Load required libraries
  35       */
  36      public static function setUpBeforeClass(): void {
  37          global $CFG;
  38  
  39          require_once("{$CFG->dirroot}/backup/util/includes/restore_includes.php");
  40      }
  41  
  42      /**
  43       * Test restore dates.
  44       *
  45       * @covers \restore_quiz_activity_structure_step
  46       */
  47      public function test_restore_question_attempts_missing_users(): void {
  48          global $DB, $USER;
  49  
  50          $this->resetAfterTest();
  51          $this->setAdminUser();
  52  
  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';
  57  
  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);
  62  
  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);
  66  
  67          $controller = new restore_controller($backuptempdir, $courseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
  68              backup::TARGET_NEW_COURSE);
  69  
  70          $this->assertTrue($controller->execute_precheck());
  71          $controller->execute_plan();
  72          $controller->destroy();
  73  
  74          // Grade restore also generates some debugging.
  75          $this->assertDebuggingCalledCount(2);
  76  
  77          $restoredquiz = $DB->get_record('quiz', []);
  78  
  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          ]);
  84  
  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);
  89  
  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          ]);
  95  
  96          $this->assertEquals(1634751274, $user01attempt->timestart);
  97          $this->assertEquals(1634751290, $user01attempt->timefinish);
  98          $this->assertEquals(0.0, (float) $user01attempt->sumgrades);
  99  
 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,
 104          ]);
 105  
 106          $this->assertEquals(1634751341, $user04attempt->timestart);
 107          $this->assertEquals(1634751347, $user04attempt->timefinish);
 108          $this->assertEquals(1.0, (float) $user04attempt->sumgrades);
 109      }
 110  }