Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  /**
  18   * Course related unit tests
  19   *
  20   * @package    core_course
  21   * @copyright  2014 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/course/lib.php');
  29  require_once($CFG->dirroot . '/course/tests/fixtures/format_theunittest.php');
  30  
  31  class core_course_courseformat_testcase extends advanced_testcase {
  32  
  33      /**
  34       * Tests the save and load functionality.
  35       *
  36       * @author Jason den Dulk
  37       */
  38      public function test_courseformat_saveandload() {
  39          $this->resetAfterTest();
  40  
  41          $courseformatoptiondata = (object) [
  42              "hideoddsections" => 1,
  43              'summary_editor' => [
  44                  'text' => '<p>Somewhere over the rainbow</p><p>The <b>quick</b> brown fox jumpos over the lazy dog.</p>',
  45                  'format' => 1
  46              ]
  47          ];
  48          $generator = $this->getDataGenerator();
  49          $course1 = $generator->create_course(array('format' => 'theunittest'));
  50          $this->assertEquals('theunittest', $course1->format);
  51          course_create_sections_if_missing($course1, array(0, 1));
  52  
  53          $courseformat = course_get_format($course1);
  54          $courseformat->update_course_format_options($courseformatoptiondata);
  55  
  56          $savedcourseformatoptiondata = $courseformat->get_format_options();
  57  
  58          $this->assertEqualsCanonicalizing($courseformatoptiondata, (object) $savedcourseformatoptiondata);
  59      }
  60  
  61      public function test_available_hook() {
  62          global $DB;
  63          $this->resetAfterTest();
  64  
  65          // Generate a course with two sections (0 and 1) and two modules. Course format is set to 'theunittest'.
  66          $generator = $this->getDataGenerator();
  67          $course1 = $generator->create_course(array('format' => 'theunittest'));
  68          $this->assertEquals('theunittest', $course1->format);
  69          course_create_sections_if_missing($course1, array(0, 1));
  70          $assign0 = $generator->create_module('assign', array('course' => $course1, 'section' => 0));
  71          $assign1 = $generator->create_module('assign', array('course' => $course1, 'section' => 1));
  72          $assign2 = $generator->create_module('assign', array('course' => $course1, 'section' => 0, 'visible' => 0));
  73  
  74          // Create a courseoverview role based on the student role.
  75          $roleattr = array('name' => 'courseoverview', 'shortname' => 'courseoverview', 'archetype' => 'student');
  76          $generator->create_role($roleattr);
  77  
  78          // Create user student, editingteacher, teacher and courseoverview.
  79          $student = $generator->create_user();
  80          $teacher = $generator->create_user();
  81          $editingteacher = $generator->create_user();
  82          $courseoverviewuser = $generator->create_user();
  83  
  84          // Enrol users into their roles.
  85          $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
  86          $generator->enrol_user($student->id, $course1->id, $roleids['student']);
  87          $generator->enrol_user($teacher->id, $course1->id, $roleids['teacher']);
  88          $generator->enrol_user($editingteacher->id, $course1->id, $roleids['editingteacher']);
  89          $generator->enrol_user($courseoverviewuser->id, $course1->id, $roleids['courseoverview']);
  90  
  91          // Remove the ignoreavailabilityrestrictions from the teacher role.
  92          role_change_permission($roleids['teacher'], context_system::instance(0),
  93                  'moodle/course:ignoreavailabilityrestrictions', CAP_PREVENT);
  94  
  95          // Allow the courseoverview role to ingore available restriction.
  96          role_change_permission($roleids['courseoverview'], context_system::instance(0),
  97                  'moodle/course:ignoreavailabilityrestrictions', CAP_ALLOW);
  98  
  99          // Make sure that initially both sections and both modules are available and visible for a student.
 100          $modinfostudent = get_fast_modinfo($course1, $student->id);
 101          $this->assertTrue($modinfostudent->get_section_info(1)->available);
 102          $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->available);
 103          $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->uservisible);
 104          $this->assertTrue($modinfostudent->get_cm($assign1->cmid)->available);
 105          $this->assertTrue($modinfostudent->get_cm($assign1->cmid)->uservisible);
 106          $this->assertFalse($modinfostudent->get_cm($assign2->cmid)->uservisible);
 107  
 108          // Set 'hideoddsections' for the course to 1.
 109          // Section1 and assign1 will be unavailable, uservisible will be false for student and true for teacher.
 110          $data = (object)array('id' => $course1->id, 'hideoddsections' => 1);
 111          course_get_format($course1)->update_course_format_options($data);
 112          $modinfostudent = get_fast_modinfo($course1, $student->id);
 113          $this->assertFalse($modinfostudent->get_section_info(1)->available);
 114          $this->assertEmpty($modinfostudent->get_section_info(1)->availableinfo);
 115          $this->assertFalse($modinfostudent->get_section_info(1)->uservisible);
 116          $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->available);
 117          $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->uservisible);
 118          $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->available);
 119          $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->uservisible);
 120          $this->assertFalse($modinfostudent->get_cm($assign2->cmid)->uservisible);
 121  
 122          $modinfoteacher = get_fast_modinfo($course1, $teacher->id);
 123          $this->assertFalse($modinfoteacher->get_section_info(1)->available);
 124          $this->assertEmpty($modinfoteacher->get_section_info(1)->availableinfo);
 125          $this->assertFalse($modinfoteacher->get_section_info(1)->uservisible);
 126          $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->available);
 127          $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->uservisible);
 128          $this->assertFalse($modinfoteacher->get_cm($assign1->cmid)->available);
 129          $this->assertFalse($modinfoteacher->get_cm($assign1->cmid)->uservisible);
 130          $this->assertTrue($modinfoteacher->get_cm($assign2->cmid)->available);
 131          $this->assertTrue($modinfoteacher->get_cm($assign2->cmid)->uservisible);
 132  
 133          $modinfoteacher = get_fast_modinfo($course1, $editingteacher->id);
 134          $this->assertFalse($modinfoteacher->get_section_info(1)->available);
 135          $this->assertEmpty($modinfoteacher->get_section_info(1)->availableinfo);
 136          $this->assertTrue($modinfoteacher->get_section_info(1)->uservisible);
 137          $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->available);
 138          $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->uservisible);
 139          $this->assertFalse($modinfoteacher->get_cm($assign1->cmid)->available);
 140          $this->assertTrue($modinfoteacher->get_cm($assign1->cmid)->uservisible);
 141          $this->assertTrue($modinfoteacher->get_cm($assign2->cmid)->uservisible);
 142  
 143          $modinfocourseoverview = get_fast_modinfo($course1, $courseoverviewuser->id);
 144          $this->assertFalse($modinfocourseoverview->get_section_info(1)->available);
 145          $this->assertEmpty($modinfocourseoverview->get_section_info(1)->availableinfo);
 146          $this->assertTrue($modinfocourseoverview->get_section_info(1)->uservisible);
 147          $this->assertTrue($modinfocourseoverview->get_cm($assign0->cmid)->available);
 148          $this->assertTrue($modinfocourseoverview->get_cm($assign0->cmid)->uservisible);
 149          $this->assertFalse($modinfocourseoverview->get_cm($assign1->cmid)->available);
 150          $this->assertTrue($modinfocourseoverview->get_cm($assign1->cmid)->uservisible);
 151          $this->assertFalse($modinfocourseoverview->get_cm($assign2->cmid)->uservisible);
 152  
 153          // Set 'hideoddsections' for the course to 2.
 154          // Section1 and assign1 will be unavailable, uservisible will be false for student and true for teacher.
 155          // Property availableinfo will be not empty.
 156          $data = (object)array('id' => $course1->id, 'hideoddsections' => 2);
 157          course_get_format($course1)->update_course_format_options($data);
 158          $modinfostudent = get_fast_modinfo($course1, $student->id);
 159          $this->assertFalse($modinfostudent->get_section_info(1)->available);
 160          $this->assertNotEmpty($modinfostudent->get_section_info(1)->availableinfo);
 161          $this->assertFalse($modinfostudent->get_section_info(1)->uservisible);
 162          $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->available);
 163          $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->uservisible);
 164          $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->available);
 165          $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->uservisible);
 166  
 167          $modinfoteacher = get_fast_modinfo($course1, $editingteacher->id);
 168          $this->assertFalse($modinfoteacher->get_section_info(1)->available);
 169          $this->assertNotEmpty($modinfoteacher->get_section_info(1)->availableinfo);
 170          $this->assertTrue($modinfoteacher->get_section_info(1)->uservisible);
 171          $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->available);
 172          $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->uservisible);
 173          $this->assertFalse($modinfoteacher->get_cm($assign1->cmid)->available);
 174          $this->assertTrue($modinfoteacher->get_cm($assign1->cmid)->uservisible);
 175      }
 176  
 177      /**
 178       * Test for supports_news() with a course format plugin that doesn't define 'news_items' in default blocks.
 179       */
 180      public function test_supports_news() {
 181          $this->resetAfterTest();
 182          $format = course_get_format((object)['format' => 'testformat']);
 183          $this->assertFalse($format->supports_news());
 184      }
 185  
 186      /**
 187       * Test for supports_news() for old course format plugins that defines 'news_items' in default blocks.
 188       */
 189      public function test_supports_news_legacy() {
 190          $this->resetAfterTest();
 191          $format = course_get_format((object)['format' => 'testlegacy']);
 192          $this->assertTrue($format->supports_news());
 193      }
 194  
 195      /**
 196       * Test for get_view_url() to ensure that the url is only given for the correct cases
 197       */
 198      public function test_get_view_url() {
 199          global $CFG;
 200          $this->resetAfterTest();
 201  
 202          $linkcoursesections = $CFG->linkcoursesections;
 203  
 204          // Generate a course with two sections (0 and 1) and two modules. Course format is set to 'testformat'.
 205          // This will allow us to test the default implementation of get_view_url.
 206          $generator = $this->getDataGenerator();
 207          $course1 = $generator->create_course(array('format' => 'testformat'));
 208          course_create_sections_if_missing($course1, array(0, 1));
 209  
 210          $data = (object)['id' => $course1->id];
 211          $format = course_get_format($course1);
 212          $format->update_course_format_options($data);
 213  
 214          // In page.
 215          $CFG->linkcoursesections = 0;
 216          $this->assertNotEmpty($format->get_view_url(null));
 217          $this->assertNotEmpty($format->get_view_url(0));
 218          $this->assertNotEmpty($format->get_view_url(1));
 219          $CFG->linkcoursesections = 1;
 220          $this->assertNotEmpty($format->get_view_url(null));
 221          $this->assertNotEmpty($format->get_view_url(0));
 222          $this->assertNotEmpty($format->get_view_url(1));
 223  
 224          // Navigation.
 225          $CFG->linkcoursesections = 0;
 226          $this->assertNull($format->get_view_url(1, ['navigation' => 1]));
 227          $this->assertNull($format->get_view_url(0, ['navigation' => 1]));
 228          $CFG->linkcoursesections = 1;
 229          $this->assertNotEmpty($format->get_view_url(1, ['navigation' => 1]));
 230          $this->assertNotEmpty($format->get_view_url(0, ['navigation' => 1]));
 231      }
 232  }
 233  
 234  /**
 235   * Class format_testformat.
 236   *
 237   * A test class that simulates a course format that doesn't define 'news_items' in default blocks.
 238   *
 239   * @copyright 2016 Jun Pataleta <jun@moodle.com>
 240   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 241   */
 242  class format_testformat extends format_base {
 243      /**
 244       * Returns the list of blocks to be automatically added for the newly created course.
 245       *
 246       * @return array
 247       */
 248      public function get_default_blocks() {
 249          return [
 250              BLOCK_POS_RIGHT => [],
 251              BLOCK_POS_LEFT => []
 252          ];
 253      }
 254  }
 255  
 256  /**
 257   * Class format_testlegacy.
 258   *
 259   * A test class that simulates old course formats that define 'news_items' in default blocks.
 260   *
 261   * @copyright 2016 Jun Pataleta <jun@moodle.com>
 262   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 263   */
 264  class format_testlegacy extends format_base {
 265      /**
 266       * Returns the list of blocks to be automatically added for the newly created course.
 267       *
 268       * @return array
 269       */
 270      public function get_default_blocks() {
 271          return [
 272              BLOCK_POS_RIGHT => ['news_items'],
 273              BLOCK_POS_LEFT => []
 274          ];
 275      }
 276  }