2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * @package core_backup
20 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 require_once(__DIR__.'/fixtures/plan_fixtures.php');
32 class backup_step_testcase extends advanced_testcase {
34 protected $moduleid; // course_modules id used for testing
35 protected $sectionid; // course_sections id used for testing
36 protected $courseid; // course id used for testing
37 protected $userid; // user record used for testing
39 protected function setUp() {
43 $this->resetAfterTest(true);
45 $course = $this->getDataGenerator()->create_course();
46 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
47 $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
49 $this->moduleid = $coursemodule->id;
50 $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
51 $this->courseid = $coursemodule->course;
52 $this->userid = 2; // admin
54 // Disable all loggers
55 $CFG->backup_error_log_logger_level = backup::LOG_NONE;
56 $CFG->backup_file_logger_level = backup::LOG_NONE;
57 $CFG->backup_database_logger_level = backup::LOG_NONE;
58 $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
62 * test base_step class
64 function test_base_step() {
66 $bp = new mock_base_plan('planname'); // We need one plan
67 $bt = new mock_base_task('taskname', $bp); // We need one task
69 $bs = new mock_base_step('stepname', $bt);
70 $this->assertTrue($bs instanceof base_step);
71 $this->assertEquals($bs->get_name(), 'stepname');
75 * test backup_step class
77 function test_backup_step() {
79 // We need one (non interactive) controller for instatiating plan
80 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
81 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
83 $bp = new backup_plan($bc);
85 $bt = new mock_backup_task('taskname', $bp);
87 $bs = new mock_backup_step('stepname', $bt);
88 $this->assertTrue($bs instanceof backup_step);
89 $this->assertEquals($bs->get_name(), 'stepname');
94 * test backup_structure_step class
96 function test_backup_structure_step() {
99 $file = $CFG->tempdir . '/test/test_backup_structure_step.txt';
100 // Remove the test dir and any content
101 @remove_dir(dirname($file));
103 if (!check_dir_exists(dirname($file), true, true)) {
104 throw new moodle_exception('error_creating_temp_dir', 'error', dirname($file));
107 // We need one (non interactive) controller for instatiating plan
108 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
109 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
111 $bp = new backup_plan($bc);
112 // We need one task with mocked basepath
113 $bt = new mock_backup_task_basepath('taskname');
115 // Instantiate backup_structure_step (and add it to task)
116 $bs = new mock_backup_structure_step('steptest', basename($file), $bt);
117 // Execute backup_structure_step
120 // Test file has been created
121 $this->assertTrue(file_exists($file));
123 // Some simple tests with contents
124 $contents = file_get_contents($file);
125 $this->assertTrue(strpos($contents, '<?xml version="1.0"') !== false);
126 $this->assertTrue(strpos($contents, '<test id="1">') !== false);
127 $this->assertTrue(strpos($contents, '<field1>value1</field1>') !== false);
128 $this->assertTrue(strpos($contents, '<field2>value2</field2>') !== false);
129 $this->assertTrue(strpos($contents, '</test>') !== false);
131 unlink($file); // delete file
133 // Remove the test dir and any content
134 @remove_dir(dirname($file));
138 * wrong base_step class tests
140 function test_base_step_wrong() {
142 // Try to pass one wrong task
144 $bt = new mock_base_step('teststep', new stdclass());
145 $this->assertTrue(false, 'base_step_exception expected');
146 } catch (exception $e) {
147 $this->assertTrue($e instanceof base_step_exception);
148 $this->assertEquals($e->errorcode, 'wrong_base_task_specified');
153 * wrong backup_step class tests
155 function test_backup_test_wrong() {
157 // Try to pass one wrong task
159 $bt = new mock_backup_step('teststep', new stdclass());
160 $this->assertTrue(false, 'backup_step_exception expected');
161 } catch (exception $e) {
162 $this->assertTrue($e instanceof backup_step_exception);
163 $this->assertEquals($e->errorcode, 'wrong_backup_task_specified');