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