Merge branch 'MDL-63381_master_v3' of https://github.com/TomoTsuyuki/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2021-04-20 23:06:42 +02:00
commit 7c75781518
15 changed files with 395 additions and 11 deletions

View file

@ -0,0 +1,123 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Backup restore base tests.
*
* @package core_backup
* @copyright Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Basic testcase class for backup / restore functionality.
*/
abstract class core_backup_backup_restore_base_testcase extends advanced_testcase {
/**
* Setup test data.
*/
protected function setUp(): void {
$this->resetAfterTest();
$this->setAdminUser();
}
/**
* Backup the course by general mode.
*
* @param stdClass $course Course for backup.
* @return string Hash string ID from the backup.
* @throws coding_exception
* @throws moodle_exception
*/
protected function perform_backup($course): string {
global $CFG, $USER;
$coursecontext = context_course::instance($course->id);
// Start backup process.
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id);
$bc->execute_plan();
$backupid = $bc->get_backupid();
$bc->destroy();
// Get the backup file.
$fs = get_file_storage();
$files = $fs->get_area_files($coursecontext->id, 'backup', 'course', false, 'id ASC');
$backupfile = reset($files);
// Extract backup file.
$path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupid;
$fp = get_file_packer('application/vnd.moodle.backup');
$fp->extract_to_pathname($backupfile, $path);
return $backupid;
}
/**
* Restore from backupid to course.
*
* @param string $backupid Hash string ID from backup.
* @param stdClass $course Course which is restored for.
* @throws restore_controller_exception
*/
protected function perform_restore($backupid, $course): void {
global $USER;
// Set up restore.
$rc = new restore_controller($backupid, $course->id,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, backup::TARGET_EXISTING_ADDING);
// Execute restore.
$rc->execute_precheck();
$rc->execute_plan();
$rc->destroy();
}
/**
* Import course from course1 to course2.
*
* @param stdClass $course1 Course to be backuped up.
* @param stdClass $course2 Course to be restored.
* @throws restore_controller_exception
*/
protected function perform_import($course1, $course2): void {
global $USER;
// Start backup process.
$bc = new backup_controller(backup::TYPE_1COURSE, $course1->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
$backupid = $bc->get_backupid();
$bc->execute_plan();
$bc->destroy();
// Set up restore.
$rc = new restore_controller($backupid, $course2->id,
backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $USER->id, backup::TARGET_EXISTING_ADDING);
// Execute restore.
$rc->execute_precheck();
$rc->execute_plan();
$rc->destroy();
}
}

View file

@ -0,0 +1,156 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Backup restore permission tests.
*
* @package core_backup
* @copyright Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once('backup_restore_base_testcase.php');
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Testcase class for permission backup / restore functionality.
*/
class core_backup_backup_restore_permission_testcase extends core_backup_backup_restore_base_testcase {
/** @var stdClass A test course which is restored/imported from. */
protected $course1;
/** @var stdClass A test course which is restored/imported to. */
protected $course2;
/** @var stdClass A user for using in this test. */
protected $user;
/** @var string Capability name for using in this test. */
protected $capabilityname;
/** @var context_course Context instance for course1. */
protected $course1context;
/** @var context_course Context instance for course2. */
protected $course2context;
/**
* Setup test data.
*/
protected function setUp(): void {
global $DB;
parent::setUp();
// Create a course with some availability data set.
$generator = $this->getDataGenerator();
$this->course1 = $generator->create_course();
$this->course1context = context_course::instance($this->course1->id);
$this->course2 = $generator->create_course();
$this->course2context = context_course::instance($this->course2->id);
$this->capabilityname = 'enrol/manual:enrol';
$this->user = $generator->create_user();
// Set additional permission for course 1.
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], '*', MUST_EXIST);
role_change_permission($teacherrole->id, $this->course1context, $this->capabilityname, CAP_ALLOW);
// Enrol to the courses.
$generator->enrol_user($this->user->id, $this->course1->id, $teacherrole->id);
$generator->enrol_user($this->user->id, $this->course2->id, $teacherrole->id);
}
/**
* Test having settings.
*/
public function test_having_settings(): void {
$this->assertEquals(0, get_config('backup', 'backup_import_permissions'));
$this->assertEquals(1, get_config('restore', 'restore_general_permissions'));
}
/**
* Test for restore with permission.
*/
public function test_backup_restore_with_permission(): void {
// Set default setting to restore with permission.
set_config('restore_general_permissions', 1, 'restore');
// Confirm course1 has the capability for the user.
$this->assertTrue(has_capability($this->capabilityname, $this->course1context, $this->user));
// Confirm course2 does not have the capability for the user.
$this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
// Perform backup and restore.
$backupid = $this->perform_backup($this->course1);
$this->perform_restore($backupid, $this->course2);
// Confirm course2 has the capability for the user.
$this->assertTrue(has_capability($this->capabilityname, $this->course2context, $this->user));
}
/**
* Test for backup / restore without restore permission.
*/
public function test_backup_restore_without_permission(): void {
// Set default setting to restore without permission.
set_config('restore_general_permissions', 0, 'restore');
// Perform backup and restore.
$backupid = $this->perform_backup($this->course1);
$this->perform_restore($backupid, $this->course2);
// Confirm course2 does not have the capability for the user.
$this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
}
/**
* Test for import with permission.
*/
public function test_backup_import_with_permission(): void {
// Set default setting to restore with permission.
set_config('backup_import_permissions', 1, 'backup');
// Perform import.
$this->perform_import($this->course1, $this->course2);
// Confirm course2 does not have the capability for the user.
$this->assertTrue(has_capability($this->capabilityname, $this->course2context, $this->user));
}
/**
* Test for import without permission.
*/
public function test_backup_import_without_permission(): void {
// Set default setting to restore without permission.
set_config('backup_import_permissions', 0, 'backup');
// Perform import.
$this->perform_import($this->course1, $this->course2);
// Confirm course2 does not have the capability for the user.
$this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
}
}