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_courseformat\output\local\state;
|
|
|
18 |
|
|
|
19 |
use availability_date\condition;
|
|
|
20 |
use core_availability\tree;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Tests for cm state class.
|
|
|
25 |
*
|
|
|
26 |
* @package core_courseformat
|
|
|
27 |
* @copyright 2022 Ferran Recio <ferran@moodle.com>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @coversDefaultClass \core_courseformat\output\local\state\cm
|
|
|
30 |
*/
|
|
|
31 |
class cm_test extends \advanced_testcase {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Setup to ensure that fixtures are loaded.
|
|
|
35 |
*/
|
|
|
36 |
public static function setupBeforeClass(): void {
|
|
|
37 |
global $CFG;
|
|
|
38 |
require_once($CFG->dirroot . '/course/lib.php');
|
|
|
39 |
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
|
|
|
40 |
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_output_course_format_state.php');
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Test the behaviour of state\cm hasavailability attribute.
|
|
|
45 |
*
|
|
|
46 |
* @dataProvider hasrestrictions_state_provider
|
|
|
47 |
* @covers ::export_for_template
|
|
|
48 |
*
|
|
|
49 |
* @param string $format the course format
|
|
|
50 |
* @param string $rolename the user role name (editingteacher or student)
|
|
|
51 |
* @param bool $hasavailability if the activity|section has availability
|
|
|
52 |
* @param bool $available if the activity availability condition is available or not to the user
|
|
|
53 |
* @param bool $expected the expected result
|
|
|
54 |
*/
|
|
|
55 |
public function test_cm_hasrestrictions_state(
|
|
|
56 |
string $format = 'topics',
|
|
|
57 |
string $rolename = 'editingteacher',
|
|
|
58 |
bool $hasavailability = false,
|
|
|
59 |
bool $available = false,
|
|
|
60 |
bool $expected = false
|
11 |
efrain |
61 |
): void {
|
1 |
efrain |
62 |
$data = $this->setup_hasrestrictions_scenario($format, $rolename, $hasavailability, $available);
|
|
|
63 |
|
|
|
64 |
// Get the cm state.
|
|
|
65 |
$courseformat = $data->courseformat;
|
|
|
66 |
$renderer = $data->renderer;
|
|
|
67 |
|
|
|
68 |
$cmclass = $courseformat->get_output_classname('state\\cm');
|
|
|
69 |
|
|
|
70 |
$cmstate = new $cmclass(
|
|
|
71 |
$courseformat,
|
|
|
72 |
$data->section,
|
|
|
73 |
$data->cm
|
|
|
74 |
);
|
|
|
75 |
$state = $cmstate->export_for_template($renderer);
|
|
|
76 |
|
|
|
77 |
$this->assertEquals($expected, $state->hascmrestrictions);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Setup section or cm has restrictions scenario.
|
|
|
82 |
*
|
|
|
83 |
* @param string $format the course format
|
|
|
84 |
* @param string $rolename the user role name (editingteacher or student)
|
|
|
85 |
* @param bool $hasavailability if the activity|section has availability
|
|
|
86 |
* @param bool $available if the activity availability condition is available or not to the user
|
|
|
87 |
* @return stdClass the scenario instances.
|
|
|
88 |
*/
|
|
|
89 |
private function setup_hasrestrictions_scenario(
|
|
|
90 |
string $format = 'topics',
|
|
|
91 |
string $rolename = 'editingteacher',
|
|
|
92 |
bool $hasavailability = false,
|
|
|
93 |
bool $available = false
|
|
|
94 |
): stdClass {
|
|
|
95 |
global $PAGE, $DB;
|
|
|
96 |
$this->resetAfterTest();
|
|
|
97 |
|
|
|
98 |
set_config('enableavailability', 1);
|
|
|
99 |
|
|
|
100 |
$course = $this->getDataGenerator()->create_course(['numsections' => 1, 'format' => $format]);
|
|
|
101 |
|
|
|
102 |
// Create and enrol user.
|
|
|
103 |
$user = $this->getDataGenerator()->create_user();
|
|
|
104 |
$this->getDataGenerator()->enrol_user(
|
|
|
105 |
$user->id,
|
|
|
106 |
$course->id,
|
|
|
107 |
$rolename
|
|
|
108 |
);
|
|
|
109 |
$this->setUser($user);
|
|
|
110 |
|
|
|
111 |
// Create an activity.
|
|
|
112 |
$activity = $this->getDataGenerator()->create_module('page', ['course' => $course->id], [
|
|
|
113 |
'section' => 1,
|
|
|
114 |
'visible' => 1
|
|
|
115 |
]);
|
|
|
116 |
|
|
|
117 |
// Set up the availability settings.
|
|
|
118 |
if ($hasavailability) {
|
|
|
119 |
$operation = ($available) ? condition::DIRECTION_UNTIL : condition::DIRECTION_FROM;
|
|
|
120 |
$availabilityjson = json_encode(tree::get_root_json(
|
|
|
121 |
[
|
|
|
122 |
condition::get_json($operation, time() + 3600),
|
|
|
123 |
],
|
|
|
124 |
'&',
|
|
|
125 |
true
|
|
|
126 |
));
|
|
|
127 |
$selector = ['id' => $activity->cmid];
|
|
|
128 |
$DB->set_field('course_modules', 'availability', trim($availabilityjson), $selector);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
// Get the cm state.
|
|
|
132 |
$courseformat = course_get_format($course->id);
|
|
|
133 |
$modinfo = $courseformat->get_modinfo();
|
|
|
134 |
$renderer = $courseformat->get_renderer($PAGE);
|
|
|
135 |
|
|
|
136 |
if ($format == 'theunittest') {
|
|
|
137 |
// These course format's hasn't the renderer file, so a debugging message will be displayed.
|
|
|
138 |
$this->assertDebuggingCalled();
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
return (object)[
|
|
|
142 |
'courseformat' => $courseformat,
|
|
|
143 |
'section' => $modinfo->get_section_info(1),
|
|
|
144 |
'cm' => $modinfo->get_cm($activity->cmid),
|
|
|
145 |
'renderer' => $renderer,
|
|
|
146 |
];
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Data provider for test_state().
|
|
|
151 |
*
|
|
|
152 |
* @return array
|
|
|
153 |
*/
|
|
|
154 |
public function hasrestrictions_state_provider(): array {
|
|
|
155 |
return [
|
|
|
156 |
// Teacher scenarios (topics).
|
|
|
157 |
'Teacher, Topics, can edit, has availability and is available' => [
|
|
|
158 |
'format' => 'topics',
|
|
|
159 |
'rolename' => 'editingteacher',
|
|
|
160 |
'hasavailability' => true,
|
|
|
161 |
'available' => true,
|
|
|
162 |
'expected' => true,
|
|
|
163 |
],
|
|
|
164 |
'Teacher, Topics, can edit, has availability and is not available' => [
|
|
|
165 |
'format' => 'topics',
|
|
|
166 |
'rolename' => 'editingteacher',
|
|
|
167 |
'hasavailability' => true,
|
|
|
168 |
'available' => false,
|
|
|
169 |
'expected' => true,
|
|
|
170 |
],
|
|
|
171 |
'Teacher, Topics, can edit and has not availability' => [
|
|
|
172 |
'format' => 'topics',
|
|
|
173 |
'rolename' => 'editingteacher',
|
|
|
174 |
'hasavailability' => false,
|
|
|
175 |
'available' => true,
|
|
|
176 |
'expected' => false,
|
|
|
177 |
],
|
|
|
178 |
// Teacher scenarios (weeks).
|
|
|
179 |
'Teacher, Weeks, can edit, has availability and is available' => [
|
|
|
180 |
'format' => 'weeks',
|
|
|
181 |
'rolename' => 'editingteacher',
|
|
|
182 |
'hasavailability' => true,
|
|
|
183 |
'available' => true,
|
|
|
184 |
'expected' => true,
|
|
|
185 |
],
|
|
|
186 |
'Teacher, Weeks, can edit, has availability and is not available' => [
|
|
|
187 |
'format' => 'weeks',
|
|
|
188 |
'rolename' => 'editingteacher',
|
|
|
189 |
'hasavailability' => true,
|
|
|
190 |
'available' => false,
|
|
|
191 |
'expected' => true,
|
|
|
192 |
],
|
|
|
193 |
'Teacher, Weeks, can edit and has not availability' => [
|
|
|
194 |
'format' => 'weeks',
|
|
|
195 |
'rolename' => 'editingteacher',
|
|
|
196 |
'hasavailability' => false,
|
|
|
197 |
'available' => true,
|
|
|
198 |
'expected' => false,
|
|
|
199 |
],
|
|
|
200 |
// Teacher scenarios (mock format).
|
|
|
201 |
'Teacher, Mock format, can edit, has availability and is available' => [
|
|
|
202 |
'format' => 'theunittest',
|
|
|
203 |
'rolename' => 'editingteacher',
|
|
|
204 |
'hasavailability' => true,
|
|
|
205 |
'available' => true,
|
|
|
206 |
'expected' => true,
|
|
|
207 |
],
|
|
|
208 |
'Teacher, Mock format, can edit, has availability and is not available' => [
|
|
|
209 |
'format' => 'theunittest',
|
|
|
210 |
'rolename' => 'editingteacher',
|
|
|
211 |
'hasavailability' => true,
|
|
|
212 |
'available' => false,
|
|
|
213 |
'expected' => true,
|
|
|
214 |
],
|
|
|
215 |
'Teacher, Mock format, can edit and has not availability' => [
|
|
|
216 |
'format' => 'theunittest',
|
|
|
217 |
'rolename' => 'editingteacher',
|
|
|
218 |
'hasavailability' => false,
|
|
|
219 |
'available' => true,
|
|
|
220 |
'expected' => false,
|
|
|
221 |
],
|
|
|
222 |
// Non editing teacher scenarios (topics).
|
|
|
223 |
'Non editing teacher, Topics, can edit, has availability and is available' => [
|
|
|
224 |
'format' => 'topics',
|
|
|
225 |
'rolename' => 'teacher',
|
|
|
226 |
'hasavailability' => true,
|
|
|
227 |
'available' => true,
|
|
|
228 |
'expected' => true,
|
|
|
229 |
],
|
|
|
230 |
'Non editing teacher, Topics, can edit, has availability and is not available' => [
|
|
|
231 |
'format' => 'topics',
|
|
|
232 |
'rolename' => 'teacher',
|
|
|
233 |
'hasavailability' => true,
|
|
|
234 |
'available' => false,
|
|
|
235 |
'expected' => true,
|
|
|
236 |
],
|
|
|
237 |
'Non editing teacher, Topics, can edit and has not availability' => [
|
|
|
238 |
'format' => 'topics',
|
|
|
239 |
'rolename' => 'teacher',
|
|
|
240 |
'hasavailability' => false,
|
|
|
241 |
'available' => true,
|
|
|
242 |
'expected' => false,
|
|
|
243 |
],
|
|
|
244 |
// Non editing teacher scenarios (weeks).
|
|
|
245 |
'Non editing teacher, Weeks, can edit, has availability and is available' => [
|
|
|
246 |
'format' => 'weeks',
|
|
|
247 |
'rolename' => 'teacher',
|
|
|
248 |
'hasavailability' => true,
|
|
|
249 |
'available' => true,
|
|
|
250 |
'expected' => true,
|
|
|
251 |
],
|
|
|
252 |
'Non editing teacher, Weeks, can edit, has availability and is not available' => [
|
|
|
253 |
'format' => 'weeks',
|
|
|
254 |
'rolename' => 'teacher',
|
|
|
255 |
'hasavailability' => true,
|
|
|
256 |
'available' => false,
|
|
|
257 |
'expected' => true,
|
|
|
258 |
],
|
|
|
259 |
'Non editing teacher, Weeks, can edit and has not availability' => [
|
|
|
260 |
'format' => 'weeks',
|
|
|
261 |
'rolename' => 'teacher',
|
|
|
262 |
'hasavailability' => false,
|
|
|
263 |
'available' => true,
|
|
|
264 |
'expected' => false,
|
|
|
265 |
],
|
|
|
266 |
// Non editing teacher scenarios (mock format).
|
|
|
267 |
'Non editing teacher, Mock format, can edit, has availability and is available' => [
|
|
|
268 |
'format' => 'theunittest',
|
|
|
269 |
'rolename' => 'teacher',
|
|
|
270 |
'hasavailability' => true,
|
|
|
271 |
'available' => true,
|
|
|
272 |
'expected' => true,
|
|
|
273 |
],
|
|
|
274 |
'Non editing teacher, Mock format, can edit, has availability and is not available' => [
|
|
|
275 |
'format' => 'theunittest',
|
|
|
276 |
'rolename' => 'teacher',
|
|
|
277 |
'hasavailability' => true,
|
|
|
278 |
'available' => false,
|
|
|
279 |
'expected' => true,
|
|
|
280 |
],
|
|
|
281 |
'Non editing teacher, Mock format, can edit and has not availability' => [
|
|
|
282 |
'format' => 'theunittest',
|
|
|
283 |
'rolename' => 'teacher',
|
|
|
284 |
'hasavailability' => false,
|
|
|
285 |
'available' => true,
|
|
|
286 |
'expected' => false,
|
|
|
287 |
],
|
|
|
288 |
// Student scenarios (topics).
|
|
|
289 |
'Student, Topics, cannot edit, has availability and is available' => [
|
|
|
290 |
'format' => 'topics',
|
|
|
291 |
'rolename' => 'student',
|
|
|
292 |
'hasavailability' => true,
|
|
|
293 |
'available' => true,
|
|
|
294 |
'expected' => false,
|
|
|
295 |
],
|
|
|
296 |
'Student, Topics, cannot edit, has availability and is not available' => [
|
|
|
297 |
'format' => 'topics',
|
|
|
298 |
'rolename' => 'student',
|
|
|
299 |
'hasavailability' => true,
|
|
|
300 |
'available' => false,
|
|
|
301 |
'expected' => true,
|
|
|
302 |
],
|
|
|
303 |
'Student, Topics, cannot edit and has not availability' => [
|
|
|
304 |
'format' => 'topics',
|
|
|
305 |
'rolename' => 'student',
|
|
|
306 |
'hasavailability' => false,
|
|
|
307 |
'available' => true,
|
|
|
308 |
'expected' => false,
|
|
|
309 |
],
|
|
|
310 |
// Student scenarios (weeks).
|
|
|
311 |
'Student, Weeks, cannot edit, has availability and is available' => [
|
|
|
312 |
'format' => 'weeks',
|
|
|
313 |
'rolename' => 'student',
|
|
|
314 |
'hasavailability' => true,
|
|
|
315 |
'available' => true,
|
|
|
316 |
'expected' => false,
|
|
|
317 |
],
|
|
|
318 |
'Student, Weeks, cannot edit, has availability and is not available' => [
|
|
|
319 |
'format' => 'weeks',
|
|
|
320 |
'rolename' => 'student',
|
|
|
321 |
'hasavailability' => true,
|
|
|
322 |
'available' => false,
|
|
|
323 |
'expected' => true,
|
|
|
324 |
],
|
|
|
325 |
'Student, Weeks, cannot edit and has not availability' => [
|
|
|
326 |
'format' => 'weeks',
|
|
|
327 |
'rolename' => 'student',
|
|
|
328 |
'hasavailability' => false,
|
|
|
329 |
'available' => true,
|
|
|
330 |
'expected' => false,
|
|
|
331 |
],
|
|
|
332 |
// Student scenarios (mock format).
|
|
|
333 |
'Student, Mock format, cannot edit, has availability and is available' => [
|
|
|
334 |
'format' => 'theunittest',
|
|
|
335 |
'rolename' => 'student',
|
|
|
336 |
'hasavailability' => true,
|
|
|
337 |
'available' => true,
|
|
|
338 |
'expected' => false,
|
|
|
339 |
],
|
|
|
340 |
'Student, Mock format, cannot edit, has availability and is not available' => [
|
|
|
341 |
'format' => 'theunittest',
|
|
|
342 |
'rolename' => 'student',
|
|
|
343 |
'hasavailability' => true,
|
|
|
344 |
'available' => false,
|
|
|
345 |
'expected' => true,
|
|
|
346 |
],
|
|
|
347 |
'Student, Mock format, cannot edit and has not availability' => [
|
|
|
348 |
'format' => 'theunittest',
|
|
|
349 |
'rolename' => 'student',
|
|
|
350 |
'hasavailability' => false,
|
|
|
351 |
'available' => true,
|
|
|
352 |
'expected' => false,
|
|
|
353 |
],
|
|
|
354 |
];
|
|
|
355 |
}
|
|
|
356 |
}
|