|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00025 // Prevent direct access to this file 00026 if (!defined('MOODLE_INTERNAL')) { 00027 die('Direct access to this script is forbidden.'); 00028 } 00029 00030 // Include all the needed stuff 00031 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); 00032 00033 /* 00034 * plan tests (all) 00035 */ 00036 class backup_plan_test extends UnitTestCase { 00037 00038 public static $includecoverage = array('backup/util/plan'); 00039 public static $excludecoverage = array('backup/util/plan/simpletest'); 00040 00041 protected $moduleid; // course_modules id used for testing 00042 protected $sectionid; // course_sections id used for testing 00043 protected $courseid; // course id used for testing 00044 protected $user; // user record used for testing 00045 00046 function __construct() { 00047 global $DB, $USER, $CFG; 00048 00049 $this->moduleid = 0; 00050 $this->sectionid = 0; 00051 $this->courseid = 0; 00052 $this->userid = $USER->id; 00053 $this->todelete = array(); 00054 00055 // Check we have (at least) one course_module 00056 if ($coursemodule = $DB->get_record('course_modules', array(), '*', IGNORE_MULTIPLE)) { 00057 $this->moduleid = $coursemodule->id; 00058 $this->sectionid = $coursemodule->section; 00059 $this->courseid = $coursemodule->course; 00060 } 00061 00062 // Avoid any logger to be created, we'll restore original settings on tearDown() 00063 $this->errorlogloggerlevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : null; 00064 $this->fileloggerlevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : null; 00065 $this->databaseloggerlevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : null; 00066 $this->fileloggerlevelextra = isset($CFG->backup_file_logger_level_extra) ? $CFG->backup_file_logger_level_extra : null; 00067 00068 parent::__construct(); 00069 } 00070 00071 function skip() { 00072 $this->skipIf(empty($this->moduleid), 'backup_plan_test require at least one course module to exist'); 00073 $this->skipIf(empty($this->sectionid),'backup_plan_test require at least one course section to exist'); 00074 $this->skipIf(empty($this->courseid), 'backup_plan_test require at least one course to exist'); 00075 $this->skipIf(empty($this->userid),'backup_plan_test require one valid user to exist'); 00076 } 00077 00078 function setUp() { 00079 global $CFG; 00080 00081 // Disable all loggers 00082 $CFG->backup_error_log_logger_level = backup::LOG_NONE; 00083 $CFG->backup_file_logger_level = backup::LOG_NONE; 00084 $CFG->backup_database_logger_level = backup::LOG_NONE; 00085 $CFG->backup_file_logger_level_extra = backup::LOG_NONE; 00086 } 00087 00088 function tearDown() { 00089 global $DB, $CFG; 00090 // Delete all the records marked to 00091 foreach ($this->todelete as $todelete) { 00092 $DB->delete_records($todelete[0], array('id' => $todelete[1])); 00093 } 00094 // Restore original file_logger levels 00095 if ($this->errorlogloggerlevel !== null) { 00096 $CFG->backup_error_log_logger_level = $this->errorlogloggerlevel; 00097 } else { 00098 unset($CFG->backup_error_log_logger_level); 00099 } 00100 if ($this->fileloggerlevel !== null) { 00101 $CFG->backup_file_logger_level = $this->fileloggerlevel; 00102 } else { 00103 unset($CFG->backup_file_logger_level); 00104 } 00105 if ($this->databaseloggerlevel !== null) { 00106 $CFG->backup_database_logger_level = $this->databaseloggerlevel; 00107 } else { 00108 unset($CFG->backup_database_logger_level); 00109 } 00110 if ($this->fileloggerlevelextra !== null) { 00111 $CFG->backup_file_logger_level_extra = $this->fileloggerlevelextra; 00112 } else { 00113 unset($CFG->backup_file_logger_level_extra); 00114 } 00115 } 00116 00120 function test_base_plan() { 00121 00122 // Instantiate 00123 $bp = new mock_base_plan('name'); 00124 $this->assertTrue($bp instanceof base_plan); 00125 $this->assertEqual($bp->get_name(), 'name'); 00126 $this->assertTrue(is_array($bp->get_settings())); 00127 $this->assertEqual(count($bp->get_settings()), 0); 00128 $this->assertTrue(is_array($bp->get_tasks())); 00129 $this->assertEqual(count($bp->get_tasks()), 0); 00130 } 00131 00132 /* 00133 * test backup_plan class 00134 */ 00135 function test_backup_plan() { 00136 00137 // We need one (non interactive) controller for instatiating plan 00138 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, 00139 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); 00140 // Instantiate one backup plan 00141 $bp = new backup_plan($bc); 00142 $this->assertTrue($bp instanceof backup_plan); 00143 $this->assertEqual($bp->get_name(), 'backup_plan'); 00144 00145 // Calculate checksum and check it 00146 $checksum = $bp->calculate_checksum(); 00147 $this->assertTrue($bp->is_checksum_correct($checksum)); 00148 } 00149 00153 function test_base_plan_wrong() { 00154 00155 // We need one (non interactive) controller for instatiating plan 00156 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, 00157 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); 00158 // Instantiate one backup plan 00159 $bp = new backup_plan($bc); 00160 // Add wrong task 00161 try { 00162 $bp->add_task(new stdclass()); 00163 $this->assertTrue(false, 'base_plan_exception expected'); 00164 } catch (exception $e) { 00165 $this->assertTrue($e instanceof base_plan_exception); 00166 $this->assertEqual($e->errorcode, 'wrong_base_task_specified'); 00167 } 00168 } 00169 00173 function test_backup_plan_wrong() { 00174 00175 // Try to pass one wrong controller 00176 try { 00177 $bp = new backup_plan(new stdclass()); 00178 $this->assertTrue(false, 'backup_plan_exception expected'); 00179 } catch (exception $e) { 00180 $this->assertTrue($e instanceof backup_plan_exception); 00181 $this->assertEqual($e->errorcode, 'wrong_backup_controller_specified'); 00182 } 00183 try { 00184 $bp = new backup_plan(null); 00185 $this->assertTrue(false, 'backup_plan_exception expected'); 00186 } catch (exception $e) { 00187 $this->assertTrue($e instanceof backup_plan_exception); 00188 $this->assertEqual($e->errorcode, 'wrong_backup_controller_specified'); 00189 } 00190 00191 // Try to build one non-existent format plan (when creating the controller) 00192 // We need one (non interactive) controller for instatiating plan 00193 try { 00194 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, 'non_existing_format', 00195 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); 00196 $this->assertTrue(false, 'backup_controller_exception expected'); 00197 } catch (exception $e) { 00198 $this->assertTrue($e instanceof backup_controller_exception); 00199 $this->assertEqual($e->errorcode, 'backup_check_unsupported_format'); 00200 $this->assertEqual($e->a, 'non_existing_format'); 00201 } 00202 } 00203 } 00204 00208 class mock_base_plan extends base_plan { 00209 public function build() { 00210 } 00211 }