Merge branch 'MDL-79937_fixlessonmatching' of https://github.com/catalystfd/moodle
[moodle.git] / course / tests / backup_restore_activity_test.php
blob0fc1d9ebbb255abaea41662ac8409c1162d01d4a
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 core_course;
18 use backup;
20 /**
21 * Restore date tests.
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
29 class backup_restore_activity_test extends \advanced_testcase {
31 /**
32 * Test that duplicating a page preserves the lang setting.
34 public function test_duplicating_page_preserves_lang() {
35 $this->resetAfterTest();
36 $this->setAdminUser();
38 // Make a test course.
39 $generator = $this->getDataGenerator();
40 $course = $generator->create_course();
42 // Create a page with forced language set.
43 $page = $generator->create_module('page', ['course' => $course->id, 'lang' => 'en']);
45 // Duplicate the page.
46 $newpagecm = duplicate_module($course, get_fast_modinfo($course)->get_cm($page->cmid));
48 // Verify the settings of the duplicated activity.
49 $this->assertEquals('en', $newpagecm->lang);
52 public function test_activity_forced_lang_not_restored_without_capability() {
53 global $DB;
54 $this->resetAfterTest();
55 $this->setAdminUser();
57 // Make a test course.
58 $generator = $this->getDataGenerator();
59 $course = $generator->create_course();
61 // Create a page with forced language set.
62 $generator->create_module('page', ['course' => $course->id, 'lang' => 'en']);
64 // Backup the course.
65 $backupid = $this->backup_course($course);
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);
74 // Restore the course.
75 $newcourseid = $this->restore_course($backupid);
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);
84 /**
85 * Makes a backup of the course.
87 * @param \stdClass $course The course object.
88 * @return string Unique identifier for this backup.
90 protected function backup_course(\stdClass $course): string {
91 global $CFG, $USER;
93 // Turn off file logging, otherwise it can't delete the file (Windows).
94 $CFG->backup_file_logger_level = backup::LOG_NONE;
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();
105 return $backupid;
109 * Restores a backup that has been made earlier.
111 * @param string $backupid The unique identifier of the backup.
112 * @return int The new course id.
114 protected function restore_course(string $backupid): int {
115 global $CFG, $DB, $USER;
117 // Turn off file logging, otherwise it can't delete the file (Windows).
118 $CFG->backup_file_logger_level = backup::LOG_NONE;
120 $defaultcategoryid = $DB->get_field('course_categories', 'id',
121 ['parent' => 0], IGNORE_MULTIPLE);
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);
129 $precheck = $rc->execute_precheck();
130 $this->assertTrue($precheck);
132 $rc->execute_plan();
133 $rc->destroy();
135 return $newcourseid;