Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
     * Load the backup and restore classes.
32
     */
33
    public static function setUpBeforeClass(): void {
34
        global $CFG;
35
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
36
        require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
37
    }
38
 
39
    /**
40
     * Test that duplicating a page preserves the lang setting.
41
     */
11 efrain 42
    public function test_duplicating_page_preserves_lang(): void {
1 efrain 43
        $this->resetAfterTest();
44
        $this->setAdminUser();
45
 
46
        // Make a test course.
47
        $generator = $this->getDataGenerator();
48
        $course = $generator->create_course();
49
 
50
        // Create a page with forced language set.
51
        $page = $generator->create_module('page', ['course' => $course->id, 'lang' => 'en']);
52
 
53
        // Duplicate the page.
54
        $newpagecm = duplicate_module($course, get_fast_modinfo($course)->get_cm($page->cmid));
55
 
56
        // Verify the settings of the duplicated activity.
57
        $this->assertEquals('en', $newpagecm->lang);
58
    }
59
 
11 efrain 60
    public function test_activity_forced_lang_not_restored_without_capability(): void {
1 efrain 61
        global $DB;
62
        $this->resetAfterTest();
63
        $this->setAdminUser();
64
 
65
        // Make a test course.
66
        $generator = $this->getDataGenerator();
67
        $course = $generator->create_course();
68
 
69
        // Create a page with forced language set.
70
        $generator->create_module('page', ['course' => $course->id, 'lang' => 'en']);
71
 
72
        // Backup the course.
73
        $backupid = $this->backup_course($course);
74
 
75
        // Create a manger user without 'moodle/course:setforcedlanguage' to do the restore.
76
        $manager = $generator->create_user();
77
        $generator->role_assign('manager', $manager->id);
78
        role_change_permission($DB->get_field('role', 'id', ['shortname' => 'manager'], MUST_EXIST),
79
                \context_system::instance(), 'moodle/course:setforcedlanguage', CAP_INHERIT);
80
        $this->setUser($manager);
81
 
82
        // Restore the course.
83
        $newcourseid = $this->restore_course($backupid);
84
 
85
        // Verify the settings of the duplicated activity.
86
        $newmodinfo = get_fast_modinfo($newcourseid);
87
        $newcms = $newmodinfo->instances['page'];
88
        $newpagecm = reset($newcms);
89
        $this->assertNull($newpagecm->lang);
90
    }
91
 
92
    /**
93
     * Makes a backup of the course.
94
     *
95
     * @param \stdClass $course The course object.
96
     * @return string Unique identifier for this backup.
97
     */
98
    protected function backup_course(\stdClass $course): string {
99
        global $CFG, $USER;
100
 
101
        // Turn off file logging, otherwise it can't delete the file (Windows).
102
        $CFG->backup_file_logger_level = backup::LOG_NONE;
103
 
104
        // Do backup with default settings. MODE_IMPORT means it will just
105
        // create the directory and not zip it.
106
        $bc = new \backup_controller(backup::TYPE_1COURSE, $course->id,
107
                backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
108
                $USER->id);
109
        $backupid = $bc->get_backupid();
110
        $bc->execute_plan();
111
        $bc->destroy();
112
 
113
        return $backupid;
114
    }
115
 
116
    /**
117
     * Restores a backup that has been made earlier.
118
     *
119
     * @param string $backupid The unique identifier of the backup.
120
     * @return int The new course id.
121
     */
122
    protected function restore_course(string $backupid): int {
123
        global $CFG, $DB, $USER;
124
 
125
        // Turn off file logging, otherwise it can't delete the file (Windows).
126
        $CFG->backup_file_logger_level = backup::LOG_NONE;
127
 
128
        $defaultcategoryid = $DB->get_field('course_categories', 'id',
129
                ['parent' => 0], IGNORE_MULTIPLE);
130
 
131
        // Do restore to new course with default settings.
132
        $newcourseid = \restore_dbops::create_new_course('Restored course', 'R1', $defaultcategoryid);
133
        $rc = new \restore_controller($backupid, $newcourseid,
134
                backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
135
                backup::TARGET_NEW_COURSE);
136
 
137
        $precheck = $rc->execute_precheck();
138
        $this->assertTrue($precheck);
139
 
140
        $rc->execute_plan();
141
        $rc->destroy();
142
 
143
        return $newcourseid;
144
    }
145
}