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