|
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 * controller tests (all) 00035 */ 00036 class backup_controller_test extends UnitTestCase { 00037 00038 public static $includecoverage = array('backup/controller'); 00039 public static $excludecoverage = array('backup/controller/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 protected $todelete; // array of records to be deleted after tests 00047 protected $errorlogloggerlevel; // To store level $CFG->backup_error_log_logger_level 00048 protected $outputindentedloggerlevel; // To store level $CFG->backup_output_indented_logger_level 00049 protected $fileloggerlevel; // To store level $CFG->backup_file_logger_level 00050 protected $databaseloggerlevel; // To store level $CFG->backup_database_logger_level 00051 protected $fileloggerlevelextra;// To store level $CFG->backup_file_logger_level_extra 00052 00053 function __construct() { 00054 global $DB, $USER, $CFG; 00055 00056 $this->moduleid = 0; 00057 $this->sectionid = 0; 00058 $this->courseid = 0; 00059 $this->userid = $USER->id; 00060 $this->todelete = array(); 00061 00062 // Check we have (at least) one course_module 00063 if ($coursemodule = $DB->get_record('course_modules', array(), '*', IGNORE_MULTIPLE)) { 00064 $this->moduleid = $coursemodule->id; 00065 $this->sectionid = $coursemodule->section; 00066 $this->courseid = $coursemodule->course; 00067 } 00068 00069 // Avoid any logger to be created, we'll restore original settings on tearDown() 00070 $this->errorlogloggerlevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : null; 00071 $this->outputindentedloggerlevel = isset($CFG->backup_output_indented_logger_level) ? $CFG->backup_output_indented_logger_level : null; 00072 $this->fileloggerlevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : null; 00073 $this->databaseloggerlevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : null; 00074 $this->fileloggerlevelextra = isset($CFG->backup_file_logger_level_extra) ? $CFG->backup_file_logger_level_extra : null; 00075 00076 parent::__construct(); 00077 } 00078 00079 function skip() { 00080 $this->skipIf(empty($this->moduleid), 'backup_controller_test require at least one course module to exist'); 00081 $this->skipIf(empty($this->sectionid),'backup_controller_test require at least one course section to exist'); 00082 $this->skipIf(empty($this->courseid), 'backup_controller_test require at least one course to exist'); 00083 $this->skipIf(empty($this->userid),'backup_controller_test require one valid user to exist'); 00084 } 00085 00086 function setUp() { 00087 global $CFG; 00088 00089 // Disable all loggers 00090 $CFG->backup_error_log_logger_level = backup::LOG_NONE; 00091 $CFG->backup_output_indented_logger_level = backup::LOG_NONE; 00092 $CFG->backup_file_logger_level = backup::LOG_NONE; 00093 $CFG->backup_database_logger_level = backup::LOG_NONE; 00094 $CFG->backup_file_logger_level_extra = backup::LOG_NONE; 00095 } 00096 00097 function tearDown() { 00098 global $DB, $CFG; 00099 // Delete all the records marked to 00100 foreach ($this->todelete as $todelete) { 00101 $DB->delete_records($todelete[0], array('id' => $todelete[1])); 00102 } 00103 // Restore original file_logger levels 00104 if ($this->errorlogloggerlevel !== null) { 00105 $CFG->backup_error_log_logger_level = $this->errorlogloggerlevel; 00106 } else { 00107 unset($CFG->backup_error_log_logger_level); 00108 } 00109 if ($this->outputindentedloggerlevel !== null) { 00110 $CFG->backup_output_indented_logger_level = $this->outputindentedloggerlevel; 00111 } else { 00112 unset($CFG->backup_output_indented_logger_level); 00113 } 00114 if ($this->fileloggerlevel !== null) { 00115 $CFG->backup_file_logger_level = $this->fileloggerlevel; 00116 } else { 00117 unset($CFG->backup_file_logger_level); 00118 } 00119 if ($this->databaseloggerlevel !== null) { 00120 $CFG->backup_database_logger_level = $this->databaseloggerlevel; 00121 } else { 00122 unset($CFG->backup_database_logger_level); 00123 } 00124 if ($this->fileloggerlevelextra !== null) { 00125 $CFG->backup_file_logger_level_extra = $this->fileloggerlevelextra; 00126 } else { 00127 unset($CFG->backup_file_logger_level_extra); 00128 } 00129 } 00130 00131 /* 00132 * test base_setting class 00133 */ 00134 function test_backup_controller() { 00135 00136 // Instantiate non interactive backup_controller 00137 $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, 00138 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); 00139 $this->assertTrue($bc instanceof backup_controller); 00140 $this->assertEqual($bc->get_status(), backup::STATUS_AWAITING); 00141 // Instantiate interactive backup_controller 00142 $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, 00143 backup::INTERACTIVE_YES, backup::MODE_GENERAL, $this->userid); 00144 $this->assertTrue($bc instanceof backup_controller); 00145 $this->assertEqual($bc->get_status(), backup::STATUS_SETTING_UI); 00146 $this->assertEqual(strlen($bc->get_backupid()), 32); // is one md5 00147 00148 // Save and load one backup controller to check everything is in place 00149 $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, 00150 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); 00151 $recid = $bc->save_controller(); 00152 $newbc = mock_backup_controller::load_controller($bc->get_backupid()); 00153 $this->assertTrue($newbc instanceof backup_controller); // This means checksum and load worked ok 00154 00155 $this->todelete[] = array('backup_controllers', $recid); // mark this record for deletion 00156 } 00157 } 00158 00159 /* 00160 * helper extended @backup_controller class that makes some methods public for testing 00161 */ 00162 class mock_backup_controller extends backup_controller { 00163 00164 public function save_controller() { 00165 parent::save_controller(); 00166 } 00167 }