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