Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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   * Tests for Moodle 2 format backup operation.
      19   *
      20   * @package core_backup
      21   * @copyright 2014 Russell Smith
      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 . '/backup/util/includes/backup_includes.php');
      29  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
      30  require_once($CFG->dirroot . '/course/format/topics/lib.php');
      31  require_once($CFG->libdir . '/completionlib.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 core_backup_moodle2_course_format_testcase extends advanced_testcase {
      41  
      42      /**
      43       * Tidy up open files that may be left open.
      44       */
      45      protected function tearDown() {
      46          gc_collect_cycles();
      47      }
      48  
      49      /**
      50       * Tests a backup and restore adds the required section option data
      51       * when the same course format is used.
      52       */
      53      public function test_course_format_options_restore() {
      54          global $DB, $CFG;
      55  
      56          $this->resetAfterTest(true);
      57          $this->setAdminUser();
      58          $CFG->enableavailability = true;
      59          $CFG->enablecompletion = true;
      60  
      61          // Create a course with some availability data set.
      62          $generator = $this->getDataGenerator();
      63          $course = $generator->create_course(
      64              array('format' => 'test_cs_options', 'numsections' => 3,
      65                  'enablecompletion' => COMPLETION_ENABLED),
      66              array('createsections' => true));
      67  
      68          $courseobject = format_base::instance($course->id);
      69          $section = $DB->get_record('course_sections',
      70              array('course' => $course->id, 'section' => 1), '*', MUST_EXIST);
      71          $data = array('id' => $section->id,
      72              'numdaystocomplete' => 2);
      73          $courseobject->update_section_format_options($data);
      74          // Backup and restore it.
      75          $this->backup_and_restore($course);
      76  
      77          $sectionoptions = $courseobject->get_format_options(1);
      78          $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
      79          $this->assertEquals(2, $sectionoptions['numdaystocomplete']);
      80      }
      81  
      82      /**
      83       * Tests an import into the same subject successfully
      84       * restores the options without error.
      85       */
      86      public function test_course_format_options_import_myself() {
      87          global $DB, $CFG;
      88  
      89          $this->resetAfterTest(true);
      90          $this->setAdminUser();
      91          $CFG->enableavailability = true;
      92          $CFG->enablecompletion = true;
      93  
      94          // Create a course with some availability data set.
      95          $generator = $this->getDataGenerator();
      96          $course = $generator->create_course(
      97                  array('format' => 'test_cs_options', 'numsections' => 3,
      98                      'enablecompletion' => COMPLETION_ENABLED),
      99                  array('createsections' => true));
     100  
     101          $courseobject = format_base::instance($course->id);
     102          $section = $DB->get_record('course_sections',
     103              array('course' => $course->id, 'section' => 1), '*', MUST_EXIST);
     104          $data = array('id' => $section->id,
     105                        'numdaystocomplete' => 2);
     106          $courseobject->update_section_format_options($data);
     107  
     108          $this->backup_and_restore($course, $course, backup::TARGET_EXISTING_ADDING);
     109  
     110          $sectionoptions = $courseobject->get_format_options(1);
     111          $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
     112          $this->assertArrayNotHasKey('secondparameter', $sectionoptions);
     113          $this->assertEquals(2, $sectionoptions['numdaystocomplete']);
     114      }
     115  
     116      /**
     117       * Tests that all section options are copied when the course format is changed.
     118       * None of the data is copied.
     119       *
     120       * It is a future enhancement to copy;
     121       * 1. Only the relevant options.
     122       * 2. Only the data associated with relevant options.
     123       */
     124      public function test_course_format_options_restore_new_format() {
     125          global $DB, $CFG;
     126  
     127          $this->resetAfterTest(true);
     128          $this->setAdminUser();
     129  
     130          // Create a source course using the test_cs2_options format.
     131          $generator = $this->getDataGenerator();
     132          $course = $generator->create_course(
     133              array('format' => 'test_cs2_options', 'numsections' => 3,
     134                  'enablecompletion' => COMPLETION_ENABLED),
     135              array('createsections' => true));
     136  
     137          // Create a target course using test_cs_options format.
     138          $newcourse = $generator->create_course(
     139              array('format' => 'test_cs_options', 'numsections' => 3,
     140                  'enablecompletion' => COMPLETION_ENABLED),
     141              array('createsections' => true));
     142  
     143          // Set section 2 to have both options, and a name.
     144          $courseobject = format_base::instance($course->id);
     145          $section = $DB->get_record('course_sections',
     146              array('course' => $course->id, 'section' => 2), '*', MUST_EXIST);
     147          $data = array('id' => $section->id,
     148              'numdaystocomplete' => 2,
     149              'secondparameter' => 8
     150          );
     151          $courseobject->update_section_format_options($data);
     152          $DB->set_field('course_sections', 'name', 'Frogs', array('id' => $section->id));
     153  
     154          // Backup and restore to the new course using 'add to existing' so it
     155          // keeps the current (test_cs_options) format.
     156          $this->backup_and_restore($course, $newcourse, backup::TARGET_EXISTING_ADDING);
     157  
     158          // Check that the section contains the options suitable for the new
     159          // format and that even the one with the same name as from the old format
     160          // has NOT been set.
     161          $newcourseobject = format_base::instance($newcourse->id);
     162          $sectionoptions = $newcourseobject->get_format_options(2);
     163          $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
     164          $this->assertArrayNotHasKey('secondparameter', $sectionoptions);
     165          $this->assertEquals(0, $sectionoptions['numdaystocomplete']);
     166  
     167          // However, the name should have been changed, as this does not depend
     168          // on the format.
     169          $modinfo = get_fast_modinfo($newcourse->id);
     170          $section = $modinfo->get_section_info(2);
     171          $this->assertEquals('Frogs', $section->name);
     172      }
     173  
     174      /**
     175       * Backs a course up and restores it.
     176       *
     177       * @param stdClass $srccourse Course object to backup
     178       * @param stdClass $dstcourse Course object to restore into
     179       * @param int $target Target course mode (backup::TARGET_xx)
     180       * @return int ID of newly restored course
     181       */
     182      protected function backup_and_restore($srccourse, $dstcourse = null,
     183              $target = backup::TARGET_NEW_COURSE) {
     184          global $USER, $CFG;
     185  
     186          // Turn off file logging, otherwise it can't delete the file (Windows).
     187          $CFG->backup_file_logger_level = backup::LOG_NONE;
     188  
     189          // Do backup with default settings. MODE_IMPORT means it will just
     190          // create the directory and not zip it.
     191          $bc = new backup_controller(backup::TYPE_1COURSE, $srccourse->id,
     192                  backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
     193                  $USER->id);
     194          $backupid = $bc->get_backupid();
     195          $bc->execute_plan();
     196          $bc->destroy();
     197  
     198          // Do restore to new course with default settings.
     199          if ($dstcourse !== null) {
     200              $newcourseid = $dstcourse->id;
     201          } else {
     202              $newcourseid = restore_dbops::create_new_course(
     203                  $srccourse->fullname, $srccourse->shortname . '_2', $srccourse->category);
     204          }
     205          $rc = new restore_controller($backupid, $newcourseid,
     206                  backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
     207                  $target);
     208  
     209          $this->assertTrue($rc->execute_precheck());
     210          $rc->execute_plan();
     211          $rc->destroy();
     212  
     213          return $newcourseid;
     214      }
     215  }
     216  
     217  /**
     218   * Class format_test_cs_options
     219   *
     220   * Test course format that has 1 option.
     221   */
     222  class format_test_cs_options extends format_topics {
     223      public function section_format_options($foreditform = false) {
     224          return array(
     225              'numdaystocomplete' => array(
     226                   'type' => PARAM_INT,
     227                   'label' => 'Test days',
     228                   'element_type' => 'text',
     229                   'default' => 0,
     230               ),
     231           );
     232      }
     233  }
     234  
     235  /**
     236   * Class format_test_cs2_options
     237   *
     238   * Test course format that has 2 options, 1 inherited.
     239   */
     240  class format_test_cs2_options extends format_test_cs_options {
     241      public function section_format_options($foreditform = false) {
     242          return array(
     243              'numdaystocomplete' => array(
     244                   'type' => PARAM_INT,
     245                   'label' => 'Test days',
     246                   'element_type' => 'text',
     247                   'default' => 0,
     248               ),
     249              'secondparameter' => array(
     250                  'type' => PARAM_INT,
     251                  'label' => 'Test Parmater',
     252                  'element_type' => 'text',
     253                  'default' => 0,
     254              ),
     255          ) + parent::section_format_options($foreditform);
     256      }
     257  }
    

    Search This Site: