PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/util/dbops/tests/dbops_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 257 lines | 150 code | 36 blank | 71 comment | 2 complexity | 135a1fb0c5212093c6ee717c87965068 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package core_backup
  18. * @category phpunit
  19. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. defined('MOODLE_INTERNAL') || die();
  23. // Include all the needed stuff
  24. global $CFG;
  25. require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  26. require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  27. /**
  28. * Restore dbops tests (all).
  29. */
  30. class restore_dbops_testcase extends advanced_testcase {
  31. /**
  32. * Verify the xxx_ids_cached (in-memory backup_ids cache) stuff works as expected.
  33. *
  34. * Note that those private implementations are tested here by using the public
  35. * backup_ids API and later performing low-level tests.
  36. */
  37. public function test_backup_ids_cached() {
  38. global $DB;
  39. $dbman = $DB->get_manager(); // We are going to use database_manager services.
  40. $this->resetAfterTest(true); // Playing with temp tables, better reset once finished.
  41. // Some variables and objects for testing.
  42. $restoreid = 'testrestoreid';
  43. $mapping = new stdClass();
  44. $mapping->itemname = 'user';
  45. $mapping->itemid = 1;
  46. $mapping->newitemid = 2;
  47. $mapping->parentitemid = 3;
  48. $mapping->info = 'info';
  49. // Create the backup_ids temp tables used by restore.
  50. restore_controller_dbops::create_restore_temp_tables($restoreid);
  51. // Send one mapping using the public api with defaults.
  52. restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  53. // Get that mapping and verify everything is returned as expected.
  54. $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  55. $this->assertSame($mapping->itemname, $result->itemname);
  56. $this->assertSame($mapping->itemid, $result->itemid);
  57. $this->assertSame(0, $result->newitemid);
  58. $this->assertSame(null, $result->parentitemid);
  59. $this->assertSame(null, $result->info);
  60. // Drop the backup_xxx_temp temptables manually, so memory cache won't be invalidated.
  61. $dbman->drop_table(new xmldb_table('backup_ids_temp'));
  62. $dbman->drop_table(new xmldb_table('backup_files_temp'));
  63. // Verify the mapping continues returning the same info,
  64. // now from cache (the table does not exist).
  65. $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  66. $this->assertSame($mapping->itemname, $result->itemname);
  67. $this->assertSame($mapping->itemid, $result->itemid);
  68. $this->assertSame(0, $result->newitemid);
  69. $this->assertSame(null, $result->parentitemid);
  70. $this->assertSame(null, $result->info);
  71. // Recreate the temp table, just to drop it using the restore API in
  72. // order to check that, then, the cache becomes invalid for the same request.
  73. restore_controller_dbops::create_restore_temp_tables($restoreid);
  74. restore_controller_dbops::drop_restore_temp_tables($restoreid);
  75. // No cached info anymore, so the mapping request will arrive to
  76. // DB leading to error (temp table does not exist).
  77. try {
  78. $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  79. $this->fail('Expecting an exception, none occurred');
  80. } catch (Exception $e) {
  81. $this->assertTrue($e instanceof dml_exception);
  82. $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
  83. }
  84. // Create the backup_ids temp tables once more.
  85. restore_controller_dbops::create_restore_temp_tables($restoreid);
  86. // Send one mapping using the public api with complete values.
  87. restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid,
  88. $mapping->newitemid, $mapping->parentitemid, $mapping->info);
  89. // Get that mapping and verify everything is returned as expected.
  90. $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  91. $this->assertSame($mapping->itemname, $result->itemname);
  92. $this->assertSame($mapping->itemid, $result->itemid);
  93. $this->assertSame($mapping->newitemid, $result->newitemid);
  94. $this->assertSame($mapping->parentitemid, $result->parentitemid);
  95. $this->assertSame($mapping->info, $result->info);
  96. // Finally, drop the temp tables properly and get the DB error again (memory caches empty).
  97. restore_controller_dbops::drop_restore_temp_tables($restoreid);
  98. try {
  99. $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  100. $this->fail('Expecting an exception, none occurred');
  101. } catch (Exception $e) {
  102. $this->assertTrue($e instanceof dml_exception);
  103. $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
  104. }
  105. }
  106. }
  107. /**
  108. * Backup dbops tests (all).
  109. */
  110. class backup_dbops_testcase extends advanced_testcase {
  111. protected $moduleid; // course_modules id used for testing
  112. protected $sectionid; // course_sections id used for testing
  113. protected $courseid; // course id used for testing
  114. protected $userid; // user record used for testing
  115. protected function setUp() {
  116. global $DB, $CFG;
  117. parent::setUp();
  118. $this->resetAfterTest(true);
  119. $course = $this->getDataGenerator()->create_course();
  120. $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  121. $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  122. $this->moduleid = $coursemodule->id;
  123. $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
  124. $this->courseid = $coursemodule->course;
  125. $this->userid = 2; // admin
  126. $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  127. $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
  128. $CFG->backup_file_logger_level = backup::LOG_NONE;
  129. $CFG->backup_database_logger_level = backup::LOG_NONE;
  130. unset($CFG->backup_file_logger_extra);
  131. $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  132. }
  133. /*
  134. * test backup_ops class
  135. */
  136. function test_backup_dbops() {
  137. // Nothing to do here, abstract class + exception, will be tested by the rest
  138. }
  139. /*
  140. * test backup_controller_dbops class
  141. */
  142. function test_backup_controller_dbops() {
  143. global $DB;
  144. $dbman = $DB->get_manager(); // Going to use some database_manager services for testing
  145. // Instantiate non interactive backup_controller
  146. $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  147. backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  148. $this->assertTrue($bc instanceof backup_controller);
  149. // Calculate checksum
  150. $checksum = $bc->calculate_checksum();
  151. $this->assertEquals(strlen($checksum), 32); // is one md5
  152. // save controller
  153. $recid = backup_controller_dbops::save_controller($bc, $checksum);
  154. $this->assertNotEmpty($recid);
  155. // save it again (should cause update to happen)
  156. $recid2 = backup_controller_dbops::save_controller($bc, $checksum);
  157. $this->assertNotEmpty($recid2);
  158. $this->assertEquals($recid, $recid2); // Same record in both save operations
  159. // Try incorrect checksum
  160. $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  161. backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  162. $checksum = $bc->calculate_checksum();
  163. try {
  164. $recid = backup_controller_dbops::save_controller($bc, 'lalala');
  165. $this->assertTrue(false, 'backup_dbops_exception expected');
  166. } catch (exception $e) {
  167. $this->assertTrue($e instanceof backup_dbops_exception);
  168. $this->assertEquals($e->errorcode, 'backup_controller_dbops_saving_checksum_mismatch');
  169. }
  170. // Try to save non backup_controller object
  171. $bc = new stdclass();
  172. try {
  173. $recid = backup_controller_dbops::save_controller($bc, 'lalala');
  174. $this->assertTrue(false, 'backup_controller_exception expected');
  175. } catch (exception $e) {
  176. $this->assertTrue($e instanceof backup_controller_exception);
  177. $this->assertEquals($e->errorcode, 'backup_controller_expected');
  178. }
  179. // save and load controller (by backupid). Then compare
  180. $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  181. backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  182. $checksum = $bc->calculate_checksum(); // Calculate checksum
  183. $backupid = $bc->get_backupid();
  184. $this->assertEquals(strlen($backupid), 32); // is one md5
  185. $recid = backup_controller_dbops::save_controller($bc, $checksum); // save controller
  186. $newbc = backup_controller_dbops::load_controller($backupid); // load controller
  187. $this->assertTrue($newbc instanceof backup_controller);
  188. $newchecksum = $newbc->calculate_checksum();
  189. $this->assertEquals($newchecksum, $checksum);
  190. // try to load non-existing controller
  191. try {
  192. $bc = backup_controller_dbops::load_controller('1234567890');
  193. $this->assertTrue(false, 'backup_dbops_exception expected');
  194. } catch (exception $e) {
  195. $this->assertTrue($e instanceof backup_dbops_exception);
  196. $this->assertEquals($e->errorcode, 'backup_controller_dbops_nonexisting');
  197. }
  198. // backup_ids_temp table tests
  199. // If, for any reason table exists, drop it
  200. if ($dbman->table_exists('backup_ids_temp')) {
  201. $dbman->drop_table(new xmldb_table('backup_ids_temp'));
  202. }
  203. // Check backup_ids_temp table doesn't exist
  204. $this->assertFalse($dbman->table_exists('backup_ids_temp'));
  205. // Create and check it exists
  206. backup_controller_dbops::create_backup_ids_temp_table('testingid');
  207. $this->assertTrue($dbman->table_exists('backup_ids_temp'));
  208. // Drop and check it doesn't exists anymore
  209. backup_controller_dbops::drop_backup_ids_temp_table('testingid');
  210. $this->assertFalse($dbman->table_exists('backup_ids_temp'));
  211. }
  212. }
  213. class mock_backup_controller4dbops extends backup_controller {
  214. /**
  215. * Change standard behavior so the checksum is also stored and not onlt calculated
  216. */
  217. public function calculate_checksum() {
  218. $this->checksum = parent::calculate_checksum();
  219. return $this->checksum;
  220. }
  221. }