Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 core_course;
  18  use backup;
  19  
  20  /**
  21   * Restore date tests.
  22   *
  23   * @package   core_course
  24   * @copyright 2022 The Open University
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @covers \backup_module_structure_step
  27   * @covers \restore_module_structure_step
  28   */
  29  class backup_restore_activity_test extends \advanced_testcase {
  30  
  31      /**
  32       * Test that duplicating a page preserves the lang setting.
  33       */
  34      public function test_duplicating_page_preserves_lang() {
  35          $this->resetAfterTest();
  36          $this->setAdminUser();
  37  
  38          // Make a test course.
  39          $generator = $this->getDataGenerator();
  40          $course = $generator->create_course();
  41  
  42          // Create a page with forced language set.
  43          $page = $generator->create_module('page', ['course' => $course->id, 'lang' => 'en']);
  44  
  45          // Duplicate the page.
  46          $newpagecm = duplicate_module($course, get_fast_modinfo($course)->get_cm($page->cmid));
  47  
  48          // Verify the settings of the duplicated activity.
  49          $this->assertEquals('en', $newpagecm->lang);
  50      }
  51  
  52      public function test_activity_forced_lang_not_restored_without_capability() {
  53          global $DB;
  54          $this->resetAfterTest();
  55          $this->setAdminUser();
  56  
  57          // Make a test course.
  58          $generator = $this->getDataGenerator();
  59          $course = $generator->create_course();
  60  
  61          // Create a page with forced language set.
  62          $generator->create_module('page', ['course' => $course->id, 'lang' => 'en']);
  63  
  64          // Backup the course.
  65          $backupid = $this->backup_course($course);
  66  
  67          // Create a manger user without 'moodle/course:setforcedlanguage' to do the restore.
  68          $manager = $generator->create_user();
  69          $generator->role_assign('manager', $manager->id);
  70          role_change_permission($DB->get_field('role', 'id', ['shortname' => 'manager'], MUST_EXIST),
  71                  \context_system::instance(), 'moodle/course:setforcedlanguage', CAP_INHERIT);
  72          $this->setUser($manager);
  73  
  74          // Restore the course.
  75          $newcourseid = $this->restore_course($backupid);
  76  
  77          // Verify the settings of the duplicated activity.
  78          $newmodinfo = get_fast_modinfo($newcourseid);
  79          $newcms = $newmodinfo->instances['page'];
  80          $newpagecm = reset($newcms);
  81          $this->assertNull($newpagecm->lang);
  82      }
  83  
  84      /**
  85       * Makes a backup of the course.
  86       *
  87       * @param \stdClass $course The course object.
  88       * @return string Unique identifier for this backup.
  89       */
  90      protected function backup_course(\stdClass $course): string {
  91          global $CFG, $USER;
  92  
  93          // Turn off file logging, otherwise it can't delete the file (Windows).
  94          $CFG->backup_file_logger_level = backup::LOG_NONE;
  95  
  96          // Do backup with default settings. MODE_IMPORT means it will just
  97          // create the directory and not zip it.
  98          $bc = new \backup_controller(backup::TYPE_1COURSE, $course->id,
  99                  backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
 100                  $USER->id);
 101          $backupid = $bc->get_backupid();
 102          $bc->execute_plan();
 103          $bc->destroy();
 104  
 105          return $backupid;
 106      }
 107  
 108      /**
 109       * Restores a backup that has been made earlier.
 110       *
 111       * @param string $backupid The unique identifier of the backup.
 112       * @return int The new course id.
 113       */
 114      protected function restore_course(string $backupid): int {
 115          global $CFG, $DB, $USER;
 116  
 117          // Turn off file logging, otherwise it can't delete the file (Windows).
 118          $CFG->backup_file_logger_level = backup::LOG_NONE;
 119  
 120          $defaultcategoryid = $DB->get_field('course_categories', 'id',
 121                  ['parent' => 0], IGNORE_MULTIPLE);
 122  
 123          // Do restore to new course with default settings.
 124          $newcourseid = \restore_dbops::create_new_course('Restored course', 'R1', $defaultcategoryid);
 125          $rc = new \restore_controller($backupid, $newcourseid,
 126                  backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
 127                  backup::TARGET_NEW_COURSE);
 128  
 129          $precheck = $rc->execute_precheck();
 130          $this->assertTrue($precheck);
 131  
 132          $rc->execute_plan();
 133          $rc->destroy();
 134  
 135          return $newcourseid;
 136      }
 137  }