1441 |
ariadna |
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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace mod_subsection;
|
|
|
20 |
|
|
|
21 |
use advanced_testcase;
|
|
|
22 |
use context_course;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Unit tests for the subsection permission class
|
|
|
26 |
*
|
|
|
27 |
* @package mod_subsection
|
|
|
28 |
* @covers \mod_subsection\permission
|
|
|
29 |
* @copyright 2024 Mikel Martín <mikel@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
final class permission_test extends advanced_testcase {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Test that viewing reports list observes capability to do so
|
|
|
36 |
*
|
|
|
37 |
* @param bool $ismoddisabled
|
|
|
38 |
* @param bool $missingcapability
|
|
|
39 |
* @param bool $isdelegated
|
|
|
40 |
* @param bool $maxsectionsreached
|
|
|
41 |
* @param string $format
|
|
|
42 |
* @param bool $expected
|
|
|
43 |
*
|
|
|
44 |
* @dataProvider can_add_subsection_provider
|
|
|
45 |
*/
|
|
|
46 |
public function test_can_add_subsection(
|
|
|
47 |
bool $ismoddisabled,
|
|
|
48 |
bool $missingcapability,
|
|
|
49 |
bool $isdelegated,
|
|
|
50 |
bool $maxsectionsreached,
|
|
|
51 |
string $format,
|
|
|
52 |
bool $expected
|
|
|
53 |
): void {
|
|
|
54 |
global $DB;
|
|
|
55 |
|
|
|
56 |
$this->resetAfterTest();
|
|
|
57 |
|
|
|
58 |
$course = $this->getDataGenerator()->create_course(['format' => $format, 'numsections' => 5]);
|
|
|
59 |
$user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
|
|
|
60 |
$courseformat = course_get_format($course->id);
|
|
|
61 |
$targetsection = $courseformat->get_modinfo()->get_section_info(5);
|
|
|
62 |
|
|
|
63 |
$manager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
|
|
64 |
$manager::enable_plugin('subsection', (int)!$ismoddisabled);
|
|
|
65 |
|
|
|
66 |
if ($missingcapability) {
|
|
|
67 |
$userrole = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
|
|
|
68 |
assign_capability('mod/subsection:addinstance', CAP_PROHIBIT, $userrole, context_course::instance($course->id));
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if ($maxsectionsreached) {
|
|
|
72 |
set_config('maxsections', 5, 'moodlecourse');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
if ($isdelegated) {
|
|
|
76 |
$this->getDataGenerator()->create_module('subsection', ['course' => $course->id, 'section' => 1]);
|
|
|
77 |
$targetsection = $courseformat->get_modinfo()->get_section_info(6);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$this->setUser($user);
|
|
|
81 |
$this->assertEquals($expected, permission::can_add_subsection($targetsection, (int)$user->id));
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Data provider for {@see self::test_can_add_subsection}
|
|
|
86 |
*
|
|
|
87 |
* @return array[]
|
|
|
88 |
*/
|
|
|
89 |
public static function can_add_subsection_provider(): array {
|
|
|
90 |
return [
|
|
|
91 |
'Plugin disabled' => [
|
|
|
92 |
'ismoddisabled' => true,
|
|
|
93 |
'missingcapability' => false,
|
|
|
94 |
'isdelegated' => false,
|
|
|
95 |
'maxsectionsreached' => false,
|
|
|
96 |
'format' => 'topics',
|
|
|
97 |
'expected' => false,
|
|
|
98 |
],
|
|
|
99 |
'User without capability' => [
|
|
|
100 |
'ismoddisabled' => false,
|
|
|
101 |
'missingcapability' => true,
|
|
|
102 |
'isdelegated' => false,
|
|
|
103 |
'maxsectionsreached' => false,
|
|
|
104 |
'format' => 'topics',
|
|
|
105 |
'expected' => false,
|
|
|
106 |
],
|
|
|
107 |
'Max sections reached' => [
|
|
|
108 |
'ismoddisabled' => false,
|
|
|
109 |
'missingcapability' => false,
|
|
|
110 |
'isdelegated' => false,
|
|
|
111 |
'maxsectionsreached' => true,
|
|
|
112 |
'format' => 'topics',
|
|
|
113 |
'expected' => false,
|
|
|
114 |
],
|
|
|
115 |
'Target section is a delegated section' => [
|
|
|
116 |
'ismoddisabled' => false,
|
|
|
117 |
'missingcapability' => false,
|
|
|
118 |
'isdelegated' => true,
|
|
|
119 |
'maxsectionsreached' => false,
|
|
|
120 |
'format' => 'topics',
|
|
|
121 |
'expected' => false,
|
|
|
122 |
],
|
|
|
123 |
'Format does not support components' => [
|
|
|
124 |
'ismoddisabled' => false,
|
|
|
125 |
'missingcapability' => false,
|
|
|
126 |
'isdelegated' => false,
|
|
|
127 |
'maxsectionsreached' => false,
|
|
|
128 |
'format' => 'singleactivity',
|
|
|
129 |
'expected' => false,
|
|
|
130 |
],
|
|
|
131 |
'Plugin enabled, with capability, max sections not reached, not inside a delegated section' => [
|
|
|
132 |
'ismoddisabled' => false,
|
|
|
133 |
'missingcapability' => false,
|
|
|
134 |
'isdelegated' => false,
|
|
|
135 |
'maxsectionsreached' => false,
|
|
|
136 |
'format' => 'topics',
|
|
|
137 |
'expected' => true,
|
|
|
138 |
],
|
|
|
139 |
];
|
|
|
140 |
}
|
|
|
141 |
}
|