|
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 * check tests (all) 00035 */ 00036 class backup_check_test extends UnitTestCase { 00037 00038 public static $includecoverage = array('backup/util/checks'); 00039 public static $excludecoverage = array('backup/util/checks/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 $userid; // user record id 00045 00046 protected $errorlogloggerlevel; // To store $CFG->backup_error_log_logger_level 00047 protected $fileloggerlevel; // To store level $CFG->backup_file_logger_level 00048 protected $databaseloggerlevel; // To store $CFG->backup_database_logger_level 00049 protected $outputindentedloggerlevel; // To store $CFG->backup_output_indented_logger_level 00050 protected $fileloggerextra; // To store $CFG->backup_file_logger_extra 00051 protected $fileloggerlevelextra; // To store level $CFG->backup_file_logger_level_extra 00052 protected $debugging; // To store $CFG->debug 00053 protected $debugdisplay; // To store $CFG->debugdisplay 00054 00055 function __construct() { 00056 global $DB, $USER, $CFG; 00057 00058 $this->moduleid = 0; 00059 $this->sectionid = 0; 00060 $this->courseid = 0; 00061 $this->userid = $USER->id; 00062 00063 // Check we have (at least) one course_module 00064 if ($coursemodule = $DB->get_record('course_modules', array(), '*', IGNORE_MULTIPLE)) { 00065 $this->moduleid = $coursemodule->id; 00066 $this->sectionid = $coursemodule->section; 00067 $this->courseid = $coursemodule->course; 00068 } 00069 parent::__construct(); 00070 } 00071 00072 function skip() { 00073 $this->skipIf(empty($this->moduleid), 'backup_check_test require at least one course module to exist'); 00074 $this->skipIf(empty($this->sectionid),'backup_check_test require at least one course section to exist'); 00075 $this->skipIf(empty($this->courseid), 'backup_check_test require at least one course to exist'); 00076 $this->skipIf(empty($this->userid),'backup_check_test require one valid user to exist'); 00077 } 00078 00079 function setUp() { 00080 global $CFG; 00081 parent::setUp(); 00082 // Avoid any file logger to be created, we'll restore original settings on tearDown() 00083 // Fetch the rest of CFG variables to be able to restore them after tests 00084 // and normalize default values 00085 $this->errorlogloggerlevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : null; 00086 $CFG->backup_error_log_logger_level = backup::LOG_NONE; 00087 00088 $this->outputindentedloggerlevel = isset($CFG->backup_output_indented_logger_level) ? $CFG->backup_output_indented_logger_level : null; 00089 $CFG->backup_output_indented_logger_level = backup::LOG_NONE; 00090 00091 $this->fileloggerlevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : null; 00092 $CFG->backup_file_logger_level = backup::LOG_NONE; 00093 00094 $this->databaseloggerlevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : null; 00095 $CFG->backup_database_logger_level = backup::LOG_NONE; 00096 00097 $this->fileloggerextra = isset($CFG->backup_file_logger_extra) ? $CFG->backup_file_logger_extra : null; 00098 unset($CFG->backup_file_logger_extra); 00099 $this->fileloggerlevelextra = isset($CFG->backup_file_logger_level_extra) ? $CFG->backup_file_logger_level_extra : null; 00100 $CFG->backup_file_logger_level_extra = backup::LOG_NONE; 00101 00102 $this->debugging = isset($CFG->debug) ? $CFG->debug : null; 00103 $this->debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; 00104 } 00105 00106 function tearDown() { 00107 global $CFG; 00108 // Restore original file_logger levels 00109 if ($this->errorlogloggerlevel !== null) { 00110 $CFG->backup_error_log_logger_level = $this->errorlogloggerlevel; 00111 } else { 00112 unset($CFG->backup_error_log_logger_level); 00113 } 00114 00115 if ($this->outputindentedloggerlevel !== null) { 00116 $CFG->backup_output_indented_logger_level = $this->outputindentedloggerlevel; 00117 } else { 00118 unset($CFG->backup_output_indented_logger_level); 00119 } 00120 00121 if ($this->fileloggerlevel !== null) { 00122 $CFG->backup_file_logger_level = $this->fileloggerlevel; 00123 } else { 00124 unset($CFG->backup_file_logger_level); 00125 } 00126 00127 if ($this->databaseloggerlevel !== null) { 00128 $CFG->backup_database_logger_level = $this->databaseloggerlevel; 00129 } else { 00130 unset($CFG->backup_database_logger_level); 00131 } 00132 00133 if ($this->fileloggerextra !== null) { 00134 $CFG->backup_file_logger_extra = $this->fileloggerextra; 00135 } else { 00136 unset($CFG->backup_file_logger_extra); 00137 } 00138 if ($this->fileloggerlevelextra !== null) { 00139 $CFG->backup_file_logger_level_extra = $this->fileloggerlevelextra; 00140 } else { 00141 unset($CFG->backup_file_logger_level_extra); 00142 } 00143 // Restore the rest of $CFG settings 00144 if ($this->debugging !== null) { 00145 $CFG->debug = $this->debugging; 00146 } else { 00147 unset($CFG->debug); 00148 } 00149 if ($this->debugdisplay !== null) { 00150 $CFG->debugdisplay = $this->debugdisplay; 00151 } else { 00152 unset($CFG->debugdisplay); 00153 } 00154 parent::tearDown(); 00155 } 00156 00157 /* 00158 * test backup_check class 00159 */ 00160 function test_backup_check() { 00161 00162 // Check against existing course module/section course or fail 00163 $this->assertTrue(backup_check::check_id(backup::TYPE_1ACTIVITY, $this->moduleid)); 00164 $this->assertTrue(backup_check::check_id(backup::TYPE_1SECTION, $this->sectionid)); 00165 $this->assertTrue(backup_check::check_id(backup::TYPE_1COURSE, $this->courseid)); 00166 $this->assertTrue(backup_check::check_user($this->userid)); 00167 00168 // Check agains non-existing course module/section/course (0) 00169 try { 00170 backup_check::check_id(backup::TYPE_1ACTIVITY, 0); 00171 $this->assertTrue(false, 'backup_controller_exception expected'); 00172 } catch (exception $e) { 00173 $this->assertTrue($e instanceof backup_controller_exception); 00174 $this->assertEqual($e->errorcode, 'backup_check_module_not_exists'); 00175 } 00176 try { 00177 backup_check::check_id(backup::TYPE_1SECTION, 0); 00178 $this->assertTrue(false, 'backup_controller_exception expected'); 00179 } catch (exception $e) { 00180 $this->assertTrue($e instanceof backup_controller_exception); 00181 $this->assertEqual($e->errorcode, 'backup_check_section_not_exists'); 00182 } 00183 try { 00184 backup_check::check_id(backup::TYPE_1COURSE, 0); 00185 $this->assertTrue(false, 'backup_controller_exception expected'); 00186 } catch (exception $e) { 00187 $this->assertTrue($e instanceof backup_controller_exception); 00188 $this->assertEqual($e->errorcode, 'backup_check_course_not_exists'); 00189 } 00190 00191 // Try wrong type 00192 try { 00193 backup_check::check_id(12345678,0); 00194 $this->assertTrue(false, 'backup_controller_exception expected'); 00195 } catch (exception $e) { 00196 $this->assertTrue($e instanceof backup_controller_exception); 00197 $this->assertEqual($e->errorcode, 'backup_check_incorrect_type'); 00198 } 00199 00200 // Test non-existing user 00201 $userid = 0; 00202 try { 00203 backup_check::check_user($userid); 00204 $this->assertTrue(false, 'backup_controller_exception expected'); 00205 } catch (exception $e) { 00206 $this->assertTrue($e instanceof backup_controller_exception); 00207 $this->assertEqual($e->errorcode, 'backup_check_user_not_exists'); 00208 } 00209 00210 // Security check tests 00211 // Try to pass wrong controller 00212 try { 00213 backup_check::check_security(new stdclass(), true); 00214 $this->assertTrue(false, 'backup_controller_exception expected'); 00215 } catch (exception $e) { 00216 $this->assertTrue($e instanceof backup_controller_exception); 00217 $this->assertEqual($e->errorcode, 'backup_check_security_requires_backup_controller'); 00218 } 00219 00220 // Pass correct controller, check must return true in any case with $apply enabled 00221 // and $bc must continue being mock_backup_controller 00222 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, 00223 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); 00224 $this->assertTrue(backup_check::check_security($bc, true)); 00225 $this->assertTrue($bc instanceof backup_controller); 00226 00227 } 00228 }