weekly release 5.0dev
[moodle.git] / backup / tests / backup_restore_permission_test.php
blob4d15cbe8c2783aeb5fb83fd6397bf2b64a1a17b0
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_backup;
19 use core_backup_backup_restore_base_testcase;
21 defined('MOODLE_INTERNAL') || die();
23 global $CFG;
24 require_once('backup_restore_base_testcase.php');
25 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
26 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
28 /**
29 * Backup restore permission tests.
31 * @package core_backup
32 * @copyright Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class backup_restore_permission_test extends core_backup_backup_restore_base_testcase {
37 /** @var stdClass A test course which is restored/imported from. */
38 protected $course1;
40 /** @var stdClass A test course which is restored/imported to. */
41 protected $course2;
43 /** @var stdClass A user for using in this test. */
44 protected $user;
46 /** @var string Capability name for using in this test. */
47 protected $capabilityname;
49 /** @var context_course Context instance for course1. */
50 protected $course1context;
52 /** @var context_course Context instance for course2. */
53 protected $course2context;
55 /**
56 * Setup test data.
58 protected function setUp(): void {
59 global $DB;
61 parent::setUp();
62 // Create a course with some availability data set.
63 $generator = $this->getDataGenerator();
64 $this->course1 = $generator->create_course();
65 $this->course1context = \context_course::instance($this->course1->id);
66 $this->course2 = $generator->create_course();
67 $this->course2context = \context_course::instance($this->course2->id);
68 $this->capabilityname = 'enrol/manual:enrol';
69 $this->user = $generator->create_user();
71 // Set additional permission for course 1.
72 $teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], '*', MUST_EXIST);
73 role_change_permission($teacherrole->id, $this->course1context, $this->capabilityname, CAP_ALLOW);
75 // Enrol to the courses.
76 $generator->enrol_user($this->user->id, $this->course1->id, $teacherrole->id);
77 $generator->enrol_user($this->user->id, $this->course2->id, $teacherrole->id);
80 /**
81 * Test having settings.
83 public function test_having_settings(): void {
84 $this->assertEquals(0, get_config('backup', 'backup_import_permissions'));
85 $this->assertEquals(1, get_config('restore', 'restore_general_permissions'));
88 /**
89 * Test for restore with permission.
91 public function test_backup_restore_with_permission(): void {
93 // Set default setting to restore with permission.
94 set_config('restore_general_permissions', 1, 'restore');
96 // Confirm course1 has the capability for the user.
97 $this->assertTrue(has_capability($this->capabilityname, $this->course1context, $this->user));
99 // Confirm course2 does not have the capability for the user.
100 $this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
102 // Perform backup and restore.
103 $backupid = $this->perform_backup($this->course1);
104 $this->perform_restore($backupid, $this->course2);
106 // Confirm course2 has the capability for the user.
107 $this->assertTrue(has_capability($this->capabilityname, $this->course2context, $this->user));
111 * Test for backup / restore without restore permission.
113 public function test_backup_restore_without_permission(): void {
115 // Set default setting to restore without permission.
116 set_config('restore_general_permissions', 0, 'restore');
118 // Perform backup and restore.
119 $backupid = $this->perform_backup($this->course1);
120 $this->perform_restore($backupid, $this->course2);
122 // Confirm course2 does not have the capability for the user.
123 $this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
127 * Test for import with permission.
129 public function test_backup_import_with_permission(): void {
131 // Set default setting to restore with permission.
132 set_config('backup_import_permissions', 1, 'backup');
134 // Perform import.
135 $this->perform_import($this->course1, $this->course2);
137 // Confirm course2 does not have the capability for the user.
138 $this->assertTrue(has_capability($this->capabilityname, $this->course2context, $this->user));
142 * Test for import without permission.
144 public function test_backup_import_without_permission(): void {
146 // Set default setting to restore without permission.
147 set_config('backup_import_permissions', 0, 'backup');
149 // Perform import.
150 $this->perform_import($this->course1, $this->course2);
152 // Confirm course2 does not have the capability for the user.
153 $this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));