Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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          // TODO: Remove this once MDL-72950 is fixed.
  51          if ($DB->get_dbfamily() == 'oracle') {
  52              $this->markTestSkipped("Skipping for Oracle until MDL-72950 is fixed.");
  53          }
  54  
  55          $this->resetAfterTest();
  56          $this->setAdminUser();
  57  
  58          // Contains a quiz with four attempts from different users. The users are as follows (user ID -> user):
  59          // 3 -> User 01, 4 -> User 02, 5 -> User 03, 6 -> User 04.
  60          // The user details for User 02 and User 03 have been removed from the backup file.
  61          $testfixture = __DIR__ . '/fixtures/question_attempts_missing_users.mbz';
  62  
  63          // Extract our test fixture, ready to be restored.
  64          $backuptempdir = 'aaa';
  65          $backuppath = make_backup_temp_directory($backuptempdir);
  66          get_file_packer('application/vnd.moodle.backup')->extract_to_pathname($testfixture, $backuppath);
  67  
  68          // Do the restore to new course with default settings.
  69          $categoryid = $DB->get_field('course_categories', 'MIN(id)', []);
  70          $courseid = restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid);
  71  
  72          $controller = new restore_controller($backuptempdir, $courseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
  73              backup::TARGET_NEW_COURSE);
  74  
  75          $this->assertTrue($controller->execute_precheck());
  76          $controller->execute_plan();
  77          $controller->destroy();
  78  
  79          // Grade restore also generates some debugging.
  80          $this->assertDebuggingCalledCount(2);
  81  
  82          $restoredquiz = $DB->get_record('quiz', []);
  83  
  84          // Assert logs were added for User 02 and User 03, due to missing mapping lookup.
  85          $loginfomessages = $DB->get_fieldset_select('backup_logs', 'message', 'backupid = ? AND loglevel = ?', [
  86              $controller->get_restoreid(),
  87              backup::LOG_INFO,
  88          ]);
  89  
  90          $this->assertContains("Mapped user ID not found for user 4, quiz {$restoredquiz->id}, attempt 1. Skipping quiz attempt",
  91              $loginfomessages);
  92          $this->assertContains("Mapped user ID not found for user 5, quiz {$restoredquiz->id}, attempt 1. Skipping quiz attempt",
  93              $loginfomessages);
  94  
  95          // User 01 has supplied the wrong answer, assert dates match the backup file too.
  96          $user01attempt = $DB->get_record('quiz_attempts', [
  97              'quiz' => $restoredquiz->id,
  98              'userid' => core_user::get_user_by_username('user01')->id,
  99          ]);
 100  
 101          $this->assertEquals(1634751274, $user01attempt->timestart);
 102          $this->assertEquals(1634751290, $user01attempt->timefinish);
 103          $this->assertEquals(0.0, (float) $user01attempt->sumgrades);
 104  
 105          // User 04 has supplied the correct answer, assert dates match the backup file too.
 106          $user04attempt = $DB->get_record('quiz_attempts', [
 107              'quiz' => $restoredquiz->id,
 108              'userid' => core_user::get_user_by_username('user04')->id,
 109          ]);
 110  
 111          $this->assertEquals(1634751341, $user04attempt->timestart);
 112          $this->assertEquals(1634751347, $user04attempt->timefinish);
 113          $this->assertEquals(1.0, (float) $user04attempt->sumgrades);
 114      }
 115  }