Proyectos de Subversion Moodle

Rev

| 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_backup;
18
 
19
use core_backup_backup_restore_base_testcase;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
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');
27
 
28
/**
29
 * Backup restore permission tests.
30
 *
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
34
 */
35
class backup_restore_permission_test extends core_backup_backup_restore_base_testcase {
36
 
37
    /** @var stdClass A test course which is restored/imported from. */
38
    protected $course1;
39
 
40
    /** @var stdClass A test course which is restored/imported to. */
41
    protected $course2;
42
 
43
    /** @var stdClass A user for using in this test. */
44
    protected $user;
45
 
46
    /** @var string Capability name for using in this test. */
47
    protected $capabilityname;
48
 
49
    /** @var context_course Context instance for course1. */
50
    protected $course1context;
51
 
52
    /** @var context_course Context instance for course2. */
53
    protected $course2context;
54
 
55
    /**
56
     * Setup test data.
57
     */
58
    protected function setUp(): void {
59
        global $DB;
60
 
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();
70
 
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);
74
 
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);
78
    }
79
 
80
    /**
81
     * Test having settings.
82
     */
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'));
86
    }
87
 
88
    /**
89
     * Test for restore with permission.
90
     */
91
    public function test_backup_restore_with_permission(): void {
92
 
93
        // Set default setting to restore with permission.
94
        set_config('restore_general_permissions', 1, 'restore');
95
 
96
        // Confirm course1 has the capability for the user.
97
        $this->assertTrue(has_capability($this->capabilityname, $this->course1context, $this->user));
98
 
99
        // Confirm course2 does not have the capability for the user.
100
        $this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
101
 
102
        // Perform backup and restore.
103
        $backupid = $this->perform_backup($this->course1);
104
        $this->perform_restore($backupid, $this->course2);
105
 
106
        // Confirm course2 has the capability for the user.
107
        $this->assertTrue(has_capability($this->capabilityname, $this->course2context, $this->user));
108
    }
109
 
110
    /**
111
     * Test for backup / restore without restore permission.
112
     */
113
    public function test_backup_restore_without_permission(): void {
114
 
115
        // Set default setting to restore without permission.
116
        set_config('restore_general_permissions', 0, 'restore');
117
 
118
        // Perform backup and restore.
119
        $backupid = $this->perform_backup($this->course1);
120
        $this->perform_restore($backupid, $this->course2);
121
 
122
        // Confirm course2 does not have the capability for the user.
123
        $this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
124
    }
125
 
126
    /**
127
     * Test for import with permission.
128
     */
129
    public function test_backup_import_with_permission(): void {
130
 
131
        // Set default setting to restore with permission.
132
        set_config('backup_import_permissions', 1, 'backup');
133
 
134
        // Perform import.
135
        $this->perform_import($this->course1, $this->course2);
136
 
137
        // Confirm course2 does not have the capability for the user.
138
        $this->assertTrue(has_capability($this->capabilityname, $this->course2context, $this->user));
139
    }
140
 
141
    /**
142
     * Test for import without permission.
143
     */
144
    public function test_backup_import_without_permission(): void {
145
 
146
        // Set default setting to restore without permission.
147
        set_config('backup_import_permissions', 0, 'backup');
148
 
149
        // Perform import.
150
        $this->perform_import($this->course1, $this->course2);
151
 
152
        // Confirm course2 does not have the capability for the user.
153
        $this->assertFalse(has_capability($this->capabilityname, $this->course2context, $this->user));
154
    }
155
 
156
}