Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/plan/simpletest/testtask.php
Go to the documentation of this file.
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/interfaces/checksumable.class.php');
00032 require_once($CFG->dirroot . '/backup/util/interfaces/executable.class.php');
00033 require_once($CFG->dirroot . '/backup/backup.class.php');
00034 require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php');
00035 require_once($CFG->dirroot . '/backup/util/dbops/backup_dbops.class.php');
00036 require_once($CFG->dirroot . '/backup/util/dbops/backup_controller_dbops.class.php');
00037 require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php');
00038 require_once($CFG->dirroot . '/backup/util/helper/backup_general_helper.class.php');
00039 require_once($CFG->dirroot . '/backup/util/checks/backup_check.class.php');
00040 require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php');
00041 require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php');
00042 require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php');
00043 require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php');
00044 require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php');
00045 require_once($CFG->dirroot . '/backup/controller/backup_controller.class.php');
00046 require_once($CFG->dirroot . '/backup/util/plan/base_plan.class.php');
00047 require_once($CFG->dirroot . '/backup/util/plan/backup_plan.class.php');
00048 require_once($CFG->dirroot . '/backup/util/plan/base_task.class.php');
00049 require_once($CFG->dirroot . '/backup/util/plan/backup_task.class.php');
00050 
00051 /*
00052  * task tests (all)
00053  */
00054 class backup_task_test extends UnitTestCase {
00055 
00056     public static $includecoverage = array('backup/util/plan');
00057     public static $excludecoverage = array('backup/util/plan/simpletest');
00058 
00059     protected $moduleid;  // course_modules id used for testing
00060     protected $sectionid; // course_sections id used for testing
00061     protected $courseid;  // course id used for testing
00062     protected $user;      // user record used for testing
00063 
00064     function __construct() {
00065         global $DB, $USER, $CFG;
00066 
00067         $this->moduleid  = 0;
00068         $this->sectionid = 0;
00069         $this->courseid  = 0;
00070         $this->userid = $USER->id;
00071         $this->todelete = array();
00072 
00073         // Check we have (at least) one course_module
00074         if ($coursemodule = $DB->get_record('course_modules', array(), '*', IGNORE_MULTIPLE)) {
00075             $this->moduleid  = $coursemodule->id;
00076             $this->sectionid = $coursemodule->section;
00077             $this->courseid  = $coursemodule->course;
00078         }
00079 
00080         // Avoid any logger to be created, we'll restore original settings on tearDown()
00081         $this->errorlogloggerlevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : null;
00082         $this->fileloggerlevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : null;
00083         $this->databaseloggerlevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : null;
00084         $this->fileloggerlevelextra = isset($CFG->backup_file_logger_level_extra) ? $CFG->backup_file_logger_level_extra : null;
00085 
00086         parent::__construct();
00087     }
00088 
00089     function skip() {
00090         $this->skipIf(empty($this->moduleid), 'backup_task_test require at least one course module to exist');
00091         $this->skipIf(empty($this->sectionid),'backup_task_test require at least one course section to exist');
00092         $this->skipIf(empty($this->courseid), 'backup_task_test require at least one course to exist');
00093         $this->skipIf(empty($this->userid),'backup_task_test require one valid user to exist');
00094     }
00095 
00096     function setUp() {
00097         global $CFG;
00098 
00099         // Disable all loggers
00100         $CFG->backup_error_log_logger_level = backup::LOG_NONE;
00101         $CFG->backup_file_logger_level = backup::LOG_NONE;
00102         $CFG->backup_database_logger_level = backup::LOG_NONE;
00103         $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
00104     }
00105 
00106     function tearDown() {
00107         global $DB, $CFG;
00108         // Delete all the records marked to
00109         foreach ($this->todelete as $todelete) {
00110             $DB->delete_records($todelete[0], array('id' => $todelete[1]));
00111         }
00112         // Restore original file_logger levels
00113         if ($this->errorlogloggerlevel !== null) {
00114             $CFG->backup_error_log_logger_level = $this->errorlogloggerlevel;
00115         } else {
00116             unset($CFG->backup_error_log_logger_level);
00117         }
00118         if ($this->fileloggerlevel !== null) {
00119             $CFG->backup_file_logger_level = $this->fileloggerlevel;
00120         } else {
00121             unset($CFG->backup_file_logger_level);
00122         }
00123         if ($this->databaseloggerlevel !== null) {
00124             $CFG->backup_database_logger_level = $this->databaseloggerlevel;
00125         } else {
00126             unset($CFG->backup_database_logger_level);
00127         }
00128         if ($this->fileloggerlevelextra !== null) {
00129             $CFG->backup_file_logger_level_extra = $this->fileloggerlevelextra;
00130         } else {
00131             unset($CFG->backup_file_logger_level_extra);
00132         }
00133     }
00134 
00138     function test_base_task() {
00139 
00140         $bp = new mock_base_plan('planname'); // We need one plan
00141         // Instantiate
00142         $bt = new mock_base_task('taskname', $bp);
00143         $this->assertTrue($bt instanceof base_task);
00144         $this->assertEqual($bt->get_name(), 'taskname');
00145         $this->assertTrue(is_array($bt->get_settings()));
00146         $this->assertEqual(count($bt->get_settings()), 0);
00147         $this->assertTrue(is_array($bt->get_steps()));
00148         $this->assertEqual(count($bt->get_steps()), 0);
00149     }
00150 
00151     /*
00152      * test backup_task class
00153      */
00154     function test_backup_task() {
00155 
00156         // We need one (non interactive) controller for instatiating plan
00157         $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
00158                                     backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
00159         // We need one plan
00160         $bp = new backup_plan($bc);
00161         // Instantiate task
00162         $bt = new mock_backup_task('taskname', $bp);
00163         $this->assertTrue($bt instanceof backup_task);
00164         $this->assertEqual($bt->get_name(), 'taskname');
00165 
00166         // Calculate checksum and check it
00167         $checksum = $bt->calculate_checksum();
00168         $this->assertTrue($bt->is_checksum_correct($checksum));
00169 
00170     }
00171 
00175     function test_base_task_wrong() {
00176 
00177         // Try to pass one wrong plan
00178         try {
00179             $bt = new mock_base_task('tasktest', new stdclass());
00180             $this->assertTrue(false, 'base_task_exception expected');
00181         } catch (exception $e) {
00182             $this->assertTrue($e instanceof base_task_exception);
00183             $this->assertEqual($e->errorcode, 'wrong_base_plan_specified');
00184         }
00185 
00186         // Add wrong step to task
00187         $bp = new mock_base_plan('planname'); // We need one plan
00188         // Instantiate
00189         $bt = new mock_base_task('taskname', $bp);
00190         try {
00191             $bt->add_step(new stdclass());
00192             $this->assertTrue(false, 'base_task_exception expected');
00193         } catch (exception $e) {
00194             $this->assertTrue($e instanceof base_task_exception);
00195             $this->assertEqual($e->errorcode, 'wrong_base_step_specified');
00196         }
00197 
00198     }
00199 
00203     function test_backup_task_wrong() {
00204 
00205         // Try to pass one wrong plan
00206         try {
00207             $bt = new mock_backup_task('tasktest', new stdclass());
00208             $this->assertTrue(false, 'backup_task_exception expected');
00209         } catch (exception $e) {
00210             $this->assertTrue($e instanceof backup_task_exception);
00211             $this->assertEqual($e->errorcode, 'wrong_backup_plan_specified');
00212         }
00213     }
00214 }
00215 
00219 class mock_base_task extends base_task {
00220     public function build() {
00221     }
00222 
00223     public function define_settings() {
00224     }
00225 }
00226 
00230 class mock_backup_task extends backup_task {
00231     public function build() {
00232     }
00233 
00234     public function define_settings() {
00235     }
00236 }
 All Data Structures Namespaces Files Functions Variables Enumerations