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
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
29require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
30require_once($CFG->dirroot . '/course/format/topics/lib.php');
31require_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 */
40class core_backup_moodle2_course_format_testcase 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}
209
210/**
211 * Class format_test_cs_options
212 *
213 * Test course format that has 1 option.
214 */
215class format_test_cs_options extends format_topics {
216    /**
217     * Override method format_topics::get_default_section_name to prevent PHPUnit errors related to the nonexistent
218     * format_test_cs_options lang file.
219     *
220     * @param stdClass $section The section in question.
221     * @return string The section's name for display.
222     */
223    public function get_default_section_name($section) {
224        if ($section->section == 0) {
225            return parent::get_default_section_name($section);
226        } else {
227            return get_string('sectionname', 'format_topics') . ' ' . $section->section;
228        }
229    }
230
231    public function section_format_options($foreditform = false) {
232        return array(
233            'numdaystocomplete' => array(
234                 'type' => PARAM_INT,
235                 'label' => 'Test days',
236                 'element_type' => 'text',
237                 'default' => 0,
238             ),
239         );
240    }
241}
242
243/**
244 * Class format_test_cs2_options
245 *
246 * Test course format that has 2 options, 1 inherited.
247 */
248class format_test_cs2_options extends format_test_cs_options {
249    public function section_format_options($foreditform = false) {
250        return array(
251            'numdaystocomplete' => array(
252                 'type' => PARAM_INT,
253                 'label' => 'Test days',
254                 'element_type' => 'text',
255                 'default' => 0,
256             ),
257            'secondparameter' => array(
258                'type' => PARAM_INT,
259                'label' => 'Test Parmater',
260                'element_type' => 'text',
261                'default' => 0,
262            ),
263        ) + parent::section_format_options($foreditform);
264    }
265}
266