2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * @package core_backup
20 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 // Include all the needed stuff
28 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
31 * Restore dbops tests (all).
33 class restore_dbops_testcase extends advanced_testcase {
36 * Verify the xxx_ids_cached (in-memory backup_ids cache) stuff works as expected.
38 * Note that those private implementations are tested here by using the public
39 * backup_ids API and later performing low-level tests.
41 public function test_backup_ids_cached() {
43 $dbman = $DB->get_manager(); // We are going to use database_manager services.
45 $this->resetAfterTest(true); // Playing with temp tables, better reset once finished.
47 // Some variables and objects for testing.
48 $restoreid = 'testrestoreid';
50 $mapping = new stdClass();
51 $mapping->itemname = 'user';
53 $mapping->newitemid = 2;
54 $mapping->parentitemid = 3;
55 $mapping->info = 'info';
57 // Create the backup_ids temp tables used by restore.
58 restore_controller_dbops::create_restore_temp_tables($restoreid);
60 // Send one mapping using the public api with defaults.
61 restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
62 // Get that mapping and verify everything is returned as expected.
63 $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
64 $this->assertSame($mapping->itemname, $result->itemname);
65 $this->assertSame($mapping->itemid, $result->itemid);
66 $this->assertSame(0, $result->newitemid);
67 $this->assertSame(null, $result->parentitemid);
68 $this->assertSame(null, $result->info);
70 // Drop the backup_xxx_temp temptables manually, so memory cache won't be invalidated.
71 $dbman->drop_table(new xmldb_table('backup_ids_temp'));
72 $dbman->drop_table(new xmldb_table('backup_files_temp'));
74 // Verify the mapping continues returning the same info,
75 // now from cache (the table does not exist).
76 $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
77 $this->assertSame($mapping->itemname, $result->itemname);
78 $this->assertSame($mapping->itemid, $result->itemid);
79 $this->assertSame(0, $result->newitemid);
80 $this->assertSame(null, $result->parentitemid);
81 $this->assertSame(null, $result->info);
83 // Recreate the temp table, just to drop it using the restore API in
84 // order to check that, then, the cache becomes invalid for the same request.
85 restore_controller_dbops::create_restore_temp_tables($restoreid);
86 restore_controller_dbops::drop_restore_temp_tables($restoreid);
88 // No cached info anymore, so the mapping request will arrive to
89 // DB leading to error (temp table does not exist).
91 $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
92 $this->fail('Expecting an exception, none occurred');
93 } catch (Exception $e) {
94 $this->assertTrue($e instanceof dml_exception);
95 $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
98 // Create the backup_ids temp tables once more.
99 restore_controller_dbops::create_restore_temp_tables($restoreid);
101 // Send one mapping using the public api with complete values.
102 restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid,
103 $mapping->newitemid, $mapping->parentitemid, $mapping->info);
104 // Get that mapping and verify everything is returned as expected.
105 $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
106 $this->assertSame($mapping->itemname, $result->itemname);
107 $this->assertSame($mapping->itemid, $result->itemid);
108 $this->assertSame($mapping->newitemid, $result->newitemid);
109 $this->assertSame($mapping->parentitemid, $result->parentitemid);
110 $this->assertSame($mapping->info, $result->info);
112 // Finally, drop the temp tables properly and get the DB error again (memory caches empty).
113 restore_controller_dbops::drop_restore_temp_tables($restoreid);
115 $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
116 $this->fail('Expecting an exception, none occurred');
117 } catch (Exception $e) {
118 $this->assertTrue($e instanceof dml_exception);
119 $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());