Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * format_topics related unit tests
  19   *
  20   * @package    format_topics
  21   * @copyright  2015 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  
  30  /**
  31   * format_topics related unit tests
  32   *
  33   * @package    format_topics
  34   * @copyright  2015 Marina Glancy
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class format_topics_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Tests for format_topics::get_section_name method with default section names.
  41       */
  42      public function test_get_section_name() {
  43          global $DB;
  44          $this->resetAfterTest(true);
  45  
  46          // Generate a course with 5 sections.
  47          $generator = $this->getDataGenerator();
  48          $numsections = 5;
  49          $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'),
  50              array('createsections' => true));
  51  
  52          // Get section names for course.
  53          $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
  54  
  55          // Test get_section_name with default section names.
  56          $courseformat = course_get_format($course);
  57          foreach ($coursesections as $section) {
  58              // Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name.
  59              $this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section));
  60          }
  61      }
  62  
  63      /**
  64       * Tests for format_topics::get_section_name method with modified section names.
  65       */
  66      public function test_get_section_name_customised() {
  67          global $DB;
  68          $this->resetAfterTest(true);
  69  
  70          // Generate a course with 5 sections.
  71          $generator = $this->getDataGenerator();
  72          $numsections = 5;
  73          $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'),
  74              array('createsections' => true));
  75  
  76          // Get section names for course.
  77          $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
  78  
  79          // Modify section names.
  80          $customname = "Custom Section";
  81          foreach ($coursesections as $section) {
  82              $section->name = "$customname $section->section";
  83              $DB->update_record('course_sections', $section);
  84          }
  85  
  86          // Requery updated section names then test get_section_name.
  87          $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
  88          $courseformat = course_get_format($course);
  89          foreach ($coursesections as $section) {
  90              // Assert that with modified section names, get_section_name returns the modified section name.
  91              $this->assertEquals($section->name, $courseformat->get_section_name($section));
  92          }
  93      }
  94  
  95      /**
  96       * Tests for format_topics::get_default_section_name.
  97       */
  98      public function test_get_default_section_name() {
  99          global $DB;
 100          $this->resetAfterTest(true);
 101  
 102          // Generate a course with 5 sections.
 103          $generator = $this->getDataGenerator();
 104          $numsections = 5;
 105          $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'),
 106              array('createsections' => true));
 107  
 108          // Get section names for course.
 109          $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
 110  
 111          // Test get_default_section_name with default section names.
 112          $courseformat = course_get_format($course);
 113          foreach ($coursesections as $section) {
 114              if ($section->section == 0) {
 115                  $sectionname = get_string('section0name', 'format_topics');
 116                  $this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
 117              } else {
 118                  $sectionname = get_string('sectionname', 'format_topics') . ' ' . $section->section;
 119                  $this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
 120              }
 121          }
 122      }
 123  
 124      /**
 125       * Test web service updating section name
 126       */
 127      public function test_update_inplace_editable() {
 128          global $CFG, $DB, $PAGE;
 129          require_once($CFG->dirroot . '/lib/external/externallib.php');
 130  
 131          $this->resetAfterTest();
 132          $user = $this->getDataGenerator()->create_user();
 133          $this->setUser($user);
 134          $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'topics'),
 135              array('createsections' => true));
 136          $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2));
 137  
 138          // Call webservice without necessary permissions.
 139          try {
 140              core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name');
 141              $this->fail('Exception expected');
 142          } catch (moodle_exception $e) {
 143              $this->assertEquals('Course or activity not accessible. (Not enrolled)',
 144                      $e->getMessage());
 145          }
 146  
 147          // Change to teacher and make sure that section name can be updated using web service update_inplace_editable().
 148          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
 149          $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
 150  
 151          $res = core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name');
 152          $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res);
 153          $this->assertEquals('New section name', $res['value']);
 154          $this->assertEquals('New section name', $DB->get_field('course_sections', 'name', array('id' => $section->id)));
 155      }
 156  
 157      /**
 158       * Test callback updating section name
 159       */
 160      public function test_inplace_editable() {
 161          global $DB, $PAGE;
 162  
 163          $this->resetAfterTest();
 164          $user = $this->getDataGenerator()->create_user();
 165          $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'topics'),
 166              array('createsections' => true));
 167          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
 168          $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
 169          $this->setUser($user);
 170  
 171          $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2));
 172  
 173          // Call callback format_topics_inplace_editable() directly.
 174          $tmpl = component_callback('format_topics', 'inplace_editable', array('sectionname', $section->id, 'Rename me again'));
 175          $this->assertInstanceOf('core\output\inplace_editable', $tmpl);
 176          $res = $tmpl->export_for_template($PAGE->get_renderer('core'));
 177          $this->assertEquals('Rename me again', $res['value']);
 178          $this->assertEquals('Rename me again', $DB->get_field('course_sections', 'name', array('id' => $section->id)));
 179  
 180          // Try updating using callback from mismatching course format.
 181          try {
 182              $tmpl = component_callback('format_weeks', 'inplace_editable', array('sectionname', $section->id, 'New name'));
 183              $this->fail('Exception expected');
 184          } catch (moodle_exception $e) {
 185              $this->assertEquals(1, preg_match('/^Can\'t find data record in database/', $e->getMessage()));
 186          }
 187      }
 188  
 189      /**
 190       * Test get_default_course_enddate.
 191       *
 192       * @return void
 193       */
 194      public function test_default_course_enddate() {
 195          global $CFG, $DB;
 196  
 197          $this->resetAfterTest(true);
 198  
 199          require_once($CFG->dirroot . '/course/tests/fixtures/testable_course_edit_form.php');
 200  
 201          $this->setTimezone('UTC');
 202  
 203          $params = array('format' => 'topics', 'numsections' => 5, 'startdate' => 1445644800);
 204          $course = $this->getDataGenerator()->create_course($params);
 205          $category = $DB->get_record('course_categories', array('id' => $course->category));
 206  
 207          $args = [
 208              'course' => $course,
 209              'category' => $category,
 210              'editoroptions' => [
 211                  'context' => context_course::instance($course->id),
 212                  'subdirs' => 0
 213              ],
 214              'returnto' => new moodle_url('/'),
 215              'returnurl' => new moodle_url('/'),
 216          ];
 217  
 218          $courseform = new testable_course_edit_form(null, $args);
 219          $courseform->definition_after_data();
 220  
 221          $enddate = $params['startdate'] + get_config('moodlecourse', 'courseduration');
 222  
 223          $weeksformat = course_get_format($course->id);
 224          $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form()));
 225  
 226      }
 227  
 228      /**
 229       * Test for get_view_url() to ensure that the url is only given for the correct cases
 230       */
 231      public function test_get_view_url() {
 232          global $CFG;
 233          $this->resetAfterTest();
 234  
 235          $linkcoursesections = $CFG->linkcoursesections;
 236  
 237          // Generate a course with two sections (0 and 1) and two modules.
 238          $generator = $this->getDataGenerator();
 239          $course1 = $generator->create_course(array('format' => 'topics'));
 240          course_create_sections_if_missing($course1, array(0, 1));
 241  
 242          $data = (object)['id' => $course1->id];
 243          $format = course_get_format($course1);
 244          $format->update_course_format_options($data);
 245  
 246          // In page.
 247          $CFG->linkcoursesections = 0;
 248          $this->assertNotEmpty($format->get_view_url(null));
 249          $this->assertNotEmpty($format->get_view_url(0));
 250          $this->assertNotEmpty($format->get_view_url(1));
 251          $CFG->linkcoursesections = 1;
 252          $this->assertNotEmpty($format->get_view_url(null));
 253          $this->assertNotEmpty($format->get_view_url(0));
 254          $this->assertNotEmpty($format->get_view_url(1));
 255  
 256          // Navigation.
 257          $CFG->linkcoursesections = 0;
 258          $this->assertNull($format->get_view_url(1, ['navigation' => 1]));
 259          $this->assertNull($format->get_view_url(0, ['navigation' => 1]));
 260          $CFG->linkcoursesections = 1;
 261          $this->assertNotEmpty($format->get_view_url(1, ['navigation' => 1]));
 262          $this->assertNotEmpty($format->get_view_url(0, ['navigation' => 1]));
 263      }
 264  }