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