Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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