Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/dbops/simpletest/testdbops.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/includes/backup_includes.php');
00032 
00033 /*
00034  * dbops tests (all)
00035  */
00036 class backup_dbops_test extends UnitTestCase {
00037 
00038     public static $includecoverage = array('backup/util/dbops');
00039     public static $excludecoverage = array('backup/util/dbops/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 
00048     protected $errorlogloggerlevel; // To store $CFG->backup_error_log_logger_level
00049     protected $fileloggerlevel; // To store level $CFG->backup_file_logger_level
00050     protected $databaseloggerlevel; // To store $CFG->backup_database_logger_level
00051     protected $outputindentedloggerlevel; // To store $CFG->backup_output_indented_logger_level
00052     protected $fileloggerextra; // To store $CFG->backup_file_logger_extra
00053     protected $fileloggerlevelextra; // To store level $CFG->backup_file_logger_level_extra
00054     protected $debugging; // To store $CFG->debug
00055     protected $debugdisplay; // To store $CFG->debugdisplay
00056 
00057     function __construct() {
00058         global $DB, $USER, $CFG;
00059 
00060         $this->moduleid  = 0;
00061         $this->sectionid = 0;
00062         $this->courseid  = 0;
00063         $this->userid = $USER->id;
00064         $this->todelete = array();
00065 
00066         // Check we have (at least) one course_module
00067         if ($coursemodule = $DB->get_record('course_modules', array(), '*', IGNORE_MULTIPLE)) {
00068             $this->moduleid  = $coursemodule->id;
00069             $this->sectionid = $coursemodule->section;
00070             $this->courseid  = $coursemodule->course;
00071         }
00072         parent::__construct();
00073     }
00074 
00075     function setUp() {
00076         global $CFG;
00077         parent::setUp();
00078         // Avoid any file logger to be created, we'll restore original settings on tearDown()
00079         // Fetch the rest of CFG variables to be able to restore them after tests
00080         // and normalize default values
00081         $this->errorlogloggerlevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : null;
00082         $CFG->backup_error_log_logger_level = backup::LOG_NONE;
00083 
00084         $this->outputindentedloggerlevel = isset($CFG->backup_output_indented_logger_level) ? $CFG->backup_output_indented_logger_level : null;
00085         $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
00086 
00087         $this->fileloggerlevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : null;
00088         $CFG->backup_file_logger_level = backup::LOG_NONE;
00089 
00090         $this->databaseloggerlevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : null;
00091         $CFG->backup_database_logger_level = backup::LOG_NONE;
00092 
00093         $this->fileloggerextra = isset($CFG->backup_file_logger_extra) ? $CFG->backup_file_logger_extra : null;
00094         unset($CFG->backup_file_logger_extra);
00095         $this->fileloggerlevelextra = isset($CFG->backup_file_logger_level_extra) ? $CFG->backup_file_logger_level_extra : null;
00096         $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
00097 
00098         $this->debugging = isset($CFG->debug) ? $CFG->debug : null;
00099         $this->debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null;
00100     }
00101 
00102     function skip() {
00103         $this->skipIf(empty($this->moduleid), 'backup_dbops_test require at least one course module to exist');
00104         $this->skipIf(empty($this->sectionid),'backup_dbops_test require at least one course section to exist');
00105         $this->skipIf(empty($this->courseid), 'backup_dbops_test require at least one course to exist');
00106         $this->skipIf(empty($this->userid),'backup_dbops_test require one valid user to exist');
00107     }
00108 
00109     function tearDown() {
00110         global $DB, $CFG;
00111         // Delete all the records marked to
00112         foreach ($this->todelete as $todelete) {
00113             $DB->delete_records($todelete[0], array('id' => $todelete[1]));
00114         }
00115 
00116         // Restore original file_logger levels
00117         if ($this->errorlogloggerlevel !== null) {
00118             $CFG->backup_error_log_logger_level = $this->errorlogloggerlevel;
00119         } else {
00120             unset($CFG->backup_error_log_logger_level);
00121         }
00122 
00123         if ($this->outputindentedloggerlevel !== null) {
00124             $CFG->backup_output_indented_logger_level = $this->outputindentedloggerlevel;
00125         } else {
00126             unset($CFG->backup_output_indented_logger_level);
00127         }
00128 
00129         if ($this->fileloggerlevel !== null) {
00130             $CFG->backup_file_logger_level = $this->fileloggerlevel;
00131         } else {
00132             unset($CFG->backup_file_logger_level);
00133         }
00134 
00135         if ($this->databaseloggerlevel !== null) {
00136             $CFG->backup_database_logger_level = $this->databaseloggerlevel;
00137         } else {
00138             unset($CFG->backup_database_logger_level);
00139         }
00140 
00141         if ($this->fileloggerextra !== null) {
00142             $CFG->backup_file_logger_extra = $this->fileloggerextra;
00143         } else {
00144             unset($CFG->backup_file_logger_extra);
00145         }
00146         if ($this->fileloggerlevelextra !== null) {
00147             $CFG->backup_file_logger_level_extra = $this->fileloggerlevelextra;
00148         } else {
00149             unset($CFG->backup_file_logger_level_extra);
00150         }
00151         // Restore the rest of $CFG settings
00152         if ($this->debugging !== null) {
00153             $CFG->debug = $this->debugging;
00154         } else {
00155             unset($CFG->debug);
00156         }
00157         if ($this->debugdisplay !== null) {
00158             $CFG->debugdisplay = $this->debugdisplay;
00159         } else {
00160             unset($CFG->debugdisplay);
00161         }
00162         parent::tearDown();
00163     }
00164 
00165     /*
00166      * test backup_ops class
00167      */
00168     function test_backup_dbops() {
00169         // Nothing to do here, abstract class + exception, will be tested by the rest
00170     }
00171 
00172     /*
00173      * test backup_controller_dbops class
00174      */
00175     function test_backup_controller_dbops() {
00176         global $DB;
00177 
00178         $dbman = $DB->get_manager(); // Going to use some database_manager services for testing
00179 
00180         // Instantiate non interactive backup_controller
00181         $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
00182                                     backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
00183         $this->assertTrue($bc instanceof backup_controller);
00184         // Calculate checksum
00185         $checksum = $bc->calculate_checksum();
00186         $this->assertEqual(strlen($checksum), 32); // is one md5
00187 
00188         // save controller
00189         $recid = backup_controller_dbops::save_controller($bc, $checksum);
00190         $this->assertTrue($recid);
00191         $this->todelete[] = array('backup_controllers', $recid); // mark this record for deletion
00192         // save it again (should cause update to happen)
00193         $recid2 = backup_controller_dbops::save_controller($bc, $checksum);
00194         $this->assertTrue($recid2);
00195         $this->todelete[] = array('backup_controllers', $recid2); // mark this record for deletion
00196         $this->assertEqual($recid, $recid2); // Same record in both save operations
00197 
00198         // Try incorrect checksum
00199         $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
00200                                     backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
00201         $checksum = $bc->calculate_checksum();
00202         try {
00203             $recid = backup_controller_dbops::save_controller($bc, 'lalala');
00204             $this->assertTrue(false, 'backup_dbops_exception expected');
00205         } catch (exception $e) {
00206             $this->assertTrue($e instanceof backup_dbops_exception);
00207             $this->assertEqual($e->errorcode, 'backup_controller_dbops_saving_checksum_mismatch');
00208         }
00209 
00210         // Try to save non backup_controller object
00211         $bc = new stdclass();
00212         try {
00213             $recid = backup_controller_dbops::save_controller($bc, 'lalala');
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_controller_expected');
00218         }
00219 
00220         // save and load controller (by backupid). Then compare
00221         $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
00222                                     backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
00223         $checksum = $bc->calculate_checksum(); // Calculate checksum
00224         $backupid = $bc->get_backupid();
00225         $this->assertEqual(strlen($backupid), 32); // is one md5
00226         $recid = backup_controller_dbops::save_controller($bc, $checksum); // save controller
00227         $this->todelete[] = array('backup_controllers', $recid); // mark this record for deletion
00228         $newbc = backup_controller_dbops::load_controller($backupid); // load controller
00229         $this->assertTrue($newbc instanceof backup_controller);
00230         $newchecksum = $newbc->calculate_checksum();
00231         $this->assertEqual($newchecksum, $checksum);
00232 
00233         // try to load non-existing controller
00234         try {
00235             $bc = backup_controller_dbops::load_controller('1234567890');
00236             $this->assertTrue(false, 'backup_dbops_exception expected');
00237         } catch (exception $e) {
00238             $this->assertTrue($e instanceof backup_dbops_exception);
00239             $this->assertEqual($e->errorcode, 'backup_controller_dbops_nonexisting');
00240         }
00241 
00242         // backup_ids_temp table tests
00243         // If, for any reason table exists, drop it
00244         if ($dbman->table_exists('backup_ids_temp')) {
00245             $dbman->drop_temp_table(new xmldb_table('backup_ids_temp'));
00246         }
00247         // Check backup_ids_temp table doesn't exist
00248         $this->assertFalse($dbman->table_exists('backup_ids_temp'));
00249         // Create and check it exists
00250         backup_controller_dbops::create_backup_ids_temp_table('testingid');
00251         $this->assertTrue($dbman->table_exists('backup_ids_temp'));
00252         // Drop and check it doesn't exists anymore
00253         backup_controller_dbops::drop_backup_ids_temp_table('testingid');
00254         $this->assertFalse($dbman->table_exists('backup_ids_temp'));
00255     }
00256 }
00257 
00258 class mock_backup_controller4dbops extends backup_controller {
00259 
00263     public function calculate_checksum() {
00264         $this->checksum = parent::calculate_checksum();
00265         return $this->checksum;
00266     }
00267 }
 All Data Structures Namespaces Files Functions Variables Enumerations