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 400] [Versions 311 and 401] [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 core_backup;
  18  
  19  use backup;
  20  use backup_controller;
  21  use restore_dbops;
  22  use restore_controller;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  28  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  29  require_once($CFG->dirroot . '/course/format/topics/lib.php');
  30  require_once($CFG->libdir . '/completionlib.php');
  31  require_once($CFG->dirroot . '/backup/moodle2/tests/fixtures/format_test_cs_options.php');
  32  
  33  /**
  34   * Tests for Moodle 2 course format section_options backup operation.
  35   *
  36   * @package core_backup
  37   * @copyright 2014 Russell Smith
  38   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class moodle2_course_format_test extends \advanced_testcase {
  41  
  42      /**
  43       * Tests a backup and restore adds the required section option data
  44       * when the same course format is used.
  45       */
  46      public function test_course_format_options_restore() {
  47          global $DB, $CFG;
  48  
  49          $this->resetAfterTest(true);
  50          $this->setAdminUser();
  51          $CFG->enableavailability = true;
  52          $CFG->enablecompletion = true;
  53  
  54          // Create a course with some availability data set.
  55          $generator = $this->getDataGenerator();
  56          $course = $generator->create_course(
  57              array('format' => 'test_cs_options', 'numsections' => 3,
  58                  'enablecompletion' => COMPLETION_ENABLED),
  59              array('createsections' => true));
  60  
  61          $courseobject = \format_base::instance($course->id);
  62          $section = $DB->get_record('course_sections',
  63              array('course' => $course->id, 'section' => 1), '*', MUST_EXIST);
  64          $data = array('id' => $section->id,
  65              'numdaystocomplete' => 2);
  66          $courseobject->update_section_format_options($data);
  67          // Backup and restore it.
  68          $this->backup_and_restore($course);
  69  
  70          $sectionoptions = $courseobject->get_format_options(1);
  71          $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
  72          $this->assertEquals(2, $sectionoptions['numdaystocomplete']);
  73      }
  74  
  75      /**
  76       * Tests an import into the same subject successfully
  77       * restores the options without error.
  78       */
  79      public function test_course_format_options_import_myself() {
  80          global $DB, $CFG;
  81  
  82          $this->resetAfterTest(true);
  83          $this->setAdminUser();
  84          $CFG->enableavailability = true;
  85          $CFG->enablecompletion = true;
  86  
  87          // Create a course with some availability data set.
  88          $generator = $this->getDataGenerator();
  89          $course = $generator->create_course(
  90                  array('format' => 'test_cs_options', 'numsections' => 3,
  91                      'enablecompletion' => COMPLETION_ENABLED),
  92                  array('createsections' => true));
  93  
  94          $courseobject = \format_base::instance($course->id);
  95          $section = $DB->get_record('course_sections',
  96              array('course' => $course->id, 'section' => 1), '*', MUST_EXIST);
  97          $data = array('id' => $section->id,
  98                        'numdaystocomplete' => 2);
  99          $courseobject->update_section_format_options($data);
 100  
 101          $this->backup_and_restore($course, $course, backup::TARGET_EXISTING_ADDING);
 102  
 103          $sectionoptions = $courseobject->get_format_options(1);
 104          $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
 105          $this->assertArrayNotHasKey('secondparameter', $sectionoptions);
 106          $this->assertEquals(2, $sectionoptions['numdaystocomplete']);
 107      }
 108  
 109      /**
 110       * Tests that all section options are copied when the course format is changed.
 111       * None of the data is copied.
 112       *
 113       * It is a future enhancement to copy;
 114       * 1. Only the relevant options.
 115       * 2. Only the data associated with relevant options.
 116       */
 117      public function test_course_format_options_restore_new_format() {
 118          global $DB, $CFG;
 119  
 120          $this->resetAfterTest(true);
 121          $this->setAdminUser();
 122  
 123          // Create a source course using the test_cs2_options format.
 124          $generator = $this->getDataGenerator();
 125          $course = $generator->create_course(
 126              array('format' => 'test_cs2_options', 'numsections' => 3,
 127                  'enablecompletion' => COMPLETION_ENABLED),
 128              array('createsections' => true));
 129  
 130          // Create a target course using test_cs_options format.
 131          $newcourse = $generator->create_course(
 132              array('format' => 'test_cs_options', 'numsections' => 3,
 133                  'enablecompletion' => COMPLETION_ENABLED),
 134              array('createsections' => true));
 135  
 136          // Set section 2 to have both options, and a name.
 137          $courseobject = \format_base::instance($course->id);
 138          $section = $DB->get_record('course_sections',
 139              array('course' => $course->id, 'section' => 2), '*', MUST_EXIST);
 140          $data = array('id' => $section->id,
 141              'numdaystocomplete' => 2,
 142              'secondparameter' => 8
 143          );
 144          $courseobject->update_section_format_options($data);
 145          $DB->set_field('course_sections', 'name', 'Frogs', array('id' => $section->id));
 146  
 147          // Backup and restore to the new course using 'add to existing' so it
 148          // keeps the current (test_cs_options) format.
 149          $this->backup_and_restore($course, $newcourse, backup::TARGET_EXISTING_ADDING);
 150  
 151          // Check that the section contains the options suitable for the new
 152          // format and that even the one with the same name as from the old format
 153          // has NOT been set.
 154          $newcourseobject = \format_base::instance($newcourse->id);
 155          $sectionoptions = $newcourseobject->get_format_options(2);
 156          $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
 157          $this->assertArrayNotHasKey('secondparameter', $sectionoptions);
 158          $this->assertEquals(0, $sectionoptions['numdaystocomplete']);
 159  
 160          // However, the name should have been changed, as this does not depend
 161          // on the format.
 162          $modinfo = get_fast_modinfo($newcourse->id);
 163          $section = $modinfo->get_section_info(2);
 164          $this->assertEquals('Frogs', $section->name);
 165      }
 166  
 167      /**
 168       * Backs a course up and restores it.
 169       *
 170       * @param \stdClass $srccourse Course object to backup
 171       * @param \stdClass $dstcourse Course object to restore into
 172       * @param int $target Target course mode (backup::TARGET_xx)
 173       * @return int ID of newly restored course
 174       */
 175      protected function backup_and_restore($srccourse, $dstcourse = null,
 176              $target = backup::TARGET_NEW_COURSE) {
 177          global $USER, $CFG;
 178  
 179          // Turn off file logging, otherwise it can't delete the file (Windows).
 180          $CFG->backup_file_logger_level = backup::LOG_NONE;
 181  
 182          // Do backup with default settings. MODE_IMPORT means it will just
 183          // create the directory and not zip it.
 184          $bc = new backup_controller(backup::TYPE_1COURSE, $srccourse->id,
 185                  backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
 186                  $USER->id);
 187          $backupid = $bc->get_backupid();
 188          $bc->execute_plan();
 189          $bc->destroy();
 190  
 191          // Do restore to new course with default settings.
 192          if ($dstcourse !== null) {
 193              $newcourseid = $dstcourse->id;
 194          } else {
 195              $newcourseid = restore_dbops::create_new_course(
 196                  $srccourse->fullname, $srccourse->shortname . '_2', $srccourse->category);
 197          }
 198          $rc = new restore_controller($backupid, $newcourseid,
 199                  backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
 200                  $target);
 201  
 202          $this->assertTrue($rc->execute_precheck());
 203          $rc->execute_plan();
 204          $rc->destroy();
 205  
 206          return $newcourseid;
 207      }
 208  }