Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/plan/simpletest/teststep.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/processable.class.php');
00033 require_once($CFG->dirroot . '/backup/util/interfaces/annotable.class.php');
00034 require_once($CFG->dirroot . '/backup/util/interfaces/executable.class.php');
00035 require_once($CFG->dirroot . '/backup/backup.class.php');
00036 require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php');
00037 require_once($CFG->dirroot . '/backup/util/helper/backup_array_iterator.class.php');
00038 require_once($CFG->dirroot . '/backup/util/dbops/backup_dbops.class.php');
00039 require_once($CFG->dirroot . '/backup/util/dbops/backup_controller_dbops.class.php');
00040 require_once($CFG->dirroot . '/backup/util/dbops/backup_structure_dbops.class.php');
00041 require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php');
00042 require_once($CFG->dirroot . '/backup/util/helper/backup_general_helper.class.php');
00043 require_once($CFG->dirroot . '/backup/util/checks/backup_check.class.php');
00044 require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php');
00045 require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php');
00046 require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php');
00047 require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php');
00048 require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php');
00049 require_once($CFG->dirroot . '/backup/util/xml/output/xml_output.class.php');
00050 require_once($CFG->dirroot . '/backup/util/xml/output/file_xml_output.class.php');
00051 require_once($CFG->dirroot . '/backup/util/xml/xml_writer.class.php');
00052 require_once($CFG->dirroot . '/backup/util/structure/base_atom.class.php');
00053 require_once($CFG->dirroot . '/backup/util/structure/base_attribute.class.php');
00054 require_once($CFG->dirroot . '/backup/util/structure/base_final_element.class.php');
00055 require_once($CFG->dirroot . '/backup/util/structure/base_nested_element.class.php');
00056 require_once($CFG->dirroot . '/backup/util/structure/base_optigroup.class.php');
00057 require_once($CFG->dirroot . '/backup/util/structure/base_processor.class.php');
00058 require_once($CFG->dirroot . '/backup/util/structure/backup_attribute.class.php');
00059 require_once($CFG->dirroot . '/backup/util/structure/backup_final_element.class.php');
00060 require_once($CFG->dirroot . '/backup/util/structure/backup_nested_element.class.php');
00061 require_once($CFG->dirroot . '/backup/util/structure/backup_optigroup.class.php');
00062 require_once($CFG->dirroot . '/backup/util/structure/backup_optigroup_element.class.php');
00063 require_once($CFG->dirroot . '/backup/util/structure/backup_structure_processor.class.php');
00064 require_once($CFG->dirroot . '/backup/controller/backup_controller.class.php');
00065 require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php');
00066 require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php');
00067 require_once($CFG->dirroot . '/backup/util/settings/activity/activity_backup_setting.class.php');
00068 require_once($CFG->dirroot . '/backup/util/plan/base_plan.class.php');
00069 require_once($CFG->dirroot . '/backup/util/plan/backup_plan.class.php');
00070 require_once($CFG->dirroot . '/backup/util/plan/base_task.class.php');
00071 require_once($CFG->dirroot . '/backup/util/plan/backup_task.class.php');
00072 require_once($CFG->dirroot . '/backup/util/plan/base_step.class.php');
00073 require_once($CFG->dirroot . '/backup/util/plan/backup_step.class.php');
00074 require_once($CFG->dirroot . '/backup/util/plan/backup_structure_step.class.php');
00075 
00076 /*
00077  * step tests (all)
00078  */
00079 class backup_step_test extends UnitTestCase {
00080 
00081     public static $includecoverage = array('backup/util/plan');
00082     public static $excludecoverage = array('backup/util/plan/simpletest');
00083 
00084     protected $moduleid;  // course_modules id used for testing
00085     protected $sectionid; // course_sections id used for testing
00086     protected $courseid;  // course id used for testing
00087     protected $user;      // user record used for testing
00088 
00089     function __construct() {
00090         global $DB, $USER, $CFG;
00091 
00092         $this->moduleid  = 0;
00093         $this->sectionid = 0;
00094         $this->courseid  = 0;
00095         $this->userid = $USER->id;
00096         $this->todelete = array();
00097 
00098         // Check we have (at least) one course_module
00099         if ($coursemodule = $DB->get_record('course_modules', array(), '*', IGNORE_MULTIPLE)) {
00100             $this->moduleid  = $coursemodule->id;
00101             $this->sectionid = $coursemodule->section;
00102             $this->courseid  = $coursemodule->course;
00103         }
00104 
00105         // Avoid any logger to be created, we'll restore original settings on tearDown()
00106         $this->errorlogloggerlevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : null;
00107         $this->fileloggerlevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : null;
00108         $this->databaseloggerlevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : null;
00109         $this->fileloggerlevelextra = isset($CFG->backup_file_logger_level_extra) ? $CFG->backup_file_logger_level_extra : null;
00110 
00111         parent::__construct();
00112     }
00113 
00114     function skip() {
00115         $this->skipIf(empty($this->moduleid), 'backup_step_test require at least one course module to exist');
00116         $this->skipIf(empty($this->sectionid),'backup_step_test require at least one course section to exist');
00117         $this->skipIf(empty($this->courseid), 'backup_step_test require at least one course to exist');
00118         $this->skipIf(empty($this->userid),'backup_step_test require one valid user to exist');
00119     }
00120 
00121     function setUp() {
00122         global $CFG;
00123 
00124         // Disable all loggers
00125         $CFG->backup_error_log_logger_level = backup::LOG_NONE;
00126         $CFG->backup_file_logger_level = backup::LOG_NONE;
00127         $CFG->backup_database_logger_level = backup::LOG_NONE;
00128         $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
00129     }
00130 
00131     function tearDown() {
00132         global $DB, $CFG;
00133         // Delete all the records marked to
00134         foreach ($this->todelete as $todelete) {
00135             $DB->delete_records($todelete[0], array('id' => $todelete[1]));
00136         }
00137         // Restore original file_logger levels
00138         if ($this->errorlogloggerlevel !== null) {
00139             $CFG->backup_error_log_logger_level = $this->errorlogloggerlevel;
00140         } else {
00141             unset($CFG->backup_error_log_logger_level);
00142         }
00143         if ($this->fileloggerlevel !== null) {
00144             $CFG->backup_file_logger_level = $this->fileloggerlevel;
00145         } else {
00146             unset($CFG->backup_file_logger_level);
00147         }
00148         if ($this->databaseloggerlevel !== null) {
00149             $CFG->backup_database_logger_level = $this->databaseloggerlevel;
00150         } else {
00151             unset($CFG->backup_database_logger_level);
00152         }
00153         if ($this->fileloggerlevelextra !== null) {
00154             $CFG->backup_file_logger_level_extra = $this->fileloggerlevelextra;
00155         } else {
00156             unset($CFG->backup_file_logger_level_extra);
00157         }
00158     }
00159 
00163     function test_base_step() {
00164 
00165         $bp = new mock_base_plan('planname'); // We need one plan
00166         $bt = new mock_base_task('taskname', $bp); // We need one task
00167         // Instantiate
00168         $bs = new mock_base_step('stepname', $bt);
00169         $this->assertTrue($bs instanceof base_step);
00170         $this->assertEqual($bs->get_name(), 'stepname');
00171     }
00172 
00173     /*
00174      * test backup_step class
00175      */
00176     function test_backup_step() {
00177 
00178         // We need one (non interactive) controller for instatiating plan
00179         $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
00180                                     backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
00181         // We need one plan
00182         $bp = new backup_plan($bc);
00183         // We need one task
00184         $bt = new mock_backup_task('taskname', $bp);
00185         // Instantiate step
00186         $bs = new mock_backup_step('stepname', $bt);
00187         $this->assertTrue($bs instanceof backup_step);
00188         $this->assertEqual($bs->get_name(), 'stepname');
00189 
00190     }
00191 
00195     function test_backup_structure_step() {
00196         global $CFG;
00197 
00198         $file = $CFG->tempdir . '/test/test_backup_structure_step.txt';
00199         // Remove the test dir and any content
00200         @remove_dir(dirname($file));
00201         // Recreate test dir
00202         if (!check_dir_exists(dirname($file), true, true)) {
00203             throw new moodle_exception('error_creating_temp_dir', 'error', dirname($file));
00204         }
00205 
00206         // We need one (non interactive) controller for instatiating plan
00207         $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
00208                                     backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
00209         // We need one plan
00210         $bp = new backup_plan($bc);
00211         // We need one task with mocked basepath
00212         $bt = new mock_backup_task_basepath('taskname');
00213         $bp->add_task($bt);
00214         // Instantiate backup_structure_step (and add it to task)
00215         $bs = new mock_backup_structure_step('steptest', basename($file), $bt);
00216         // Execute backup_structure_step
00217         $bs->execute();
00218 
00219         // Test file has been created
00220         $this->assertTrue(file_exists($file));
00221 
00222         // Some simple tests with contents
00223         $contents = file_get_contents($file);
00224         $this->assertTrue(strpos($contents, '<?xml version="1.0"') !== false);
00225         $this->assertTrue(strpos($contents, '<test id="1">') !== false);
00226         $this->assertTrue(strpos($contents, '<field1>value1</field1>') !== false);
00227         $this->assertTrue(strpos($contents, '<field2>value2</field2>') !== false);
00228         $this->assertTrue(strpos($contents, '</test>') !== false);
00229 
00230         unlink($file); // delete file
00231 
00232         // Remove the test dir and any content
00233         @remove_dir(dirname($file));
00234     }
00235 
00239     function test_base_step_wrong() {
00240 
00241         // Try to pass one wrong task
00242         try {
00243             $bt = new mock_base_step('teststep', new stdclass());
00244             $this->assertTrue(false, 'base_step_exception expected');
00245         } catch (exception $e) {
00246             $this->assertTrue($e instanceof base_step_exception);
00247             $this->assertEqual($e->errorcode, 'wrong_base_task_specified');
00248         }
00249     }
00250 
00254     function test_backup_test_wrong() {
00255 
00256         // Try to pass one wrong task
00257         try {
00258             $bt = new mock_backup_step('teststep', new stdclass());
00259             $this->assertTrue(false, 'backup_step_exception expected');
00260         } catch (exception $e) {
00261             $this->assertTrue($e instanceof backup_step_exception);
00262             $this->assertEqual($e->errorcode, 'wrong_backup_task_specified');
00263         }
00264     }
00265 }
00266 
00270 class mock_base_step extends base_step {
00271     public function execute() {
00272     }
00273 }
00274 
00278 class mock_backup_step extends backup_step {
00279     public function execute() {
00280     }
00281 }
00282 
00286 class mock_backup_task_basepath extends backup_task {
00287 
00288     public function build() {
00289         // Nothing to do
00290     }
00291 
00292     public function define_settings() {
00293         // Nothing to do
00294     }
00295 
00296     public function get_taskbasepath() {
00297         global $CFG;
00298         return $CFG->tempdir . '/test';
00299     }
00300 }
00301 
00305 class mock_backup_structure_step extends backup_structure_step {
00306 
00307     protected function define_structure() {
00308 
00309         // Create really simple structure (1 nested with 1 attr and 2 fields)
00310         $test = new backup_nested_element('test',
00311                                           array('id'),
00312                                           array('field1', 'field2')
00313                                          );
00314         $test->set_source_array(array(array('id' => 1, 'field1' => 'value1', 'field2' => 'value2')));
00315 
00316         return $test;
00317     }
00318 }
00319 
00323 class mock_fullpath_activity_setting extends activity_backup_setting {
00324     public function process_change($setting, $ctype, $oldv) {
00325         // Nothing to do
00326     }
00327 }
00328 
00332 class mock_backupid_activity_setting extends activity_backup_setting {
00333     public function process_change($setting, $ctype, $oldv) {
00334         // Nothing to do
00335     }
00336 }
 All Data Structures Namespaces Files Functions Variables Enumerations