Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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