Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_backup;
18
 
19
use restore_controller_dbops;
20
use restore_dbops;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
// Include all the needed stuff
25
global $CFG;
26
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
27
 
28
/**
29
 * @package    core_backup
30
 * @category   test
31
 * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
1441 ariadna 34
final class restore_dbops_test extends \advanced_testcase {
1 efrain 35
 
36
    /**
37
     * Verify the xxx_ids_cached (in-memory backup_ids cache) stuff works as expected.
38
     *
39
     * Note that those private implementations are tested here by using the public
40
     * backup_ids API and later performing low-level tests.
41
     */
11 efrain 42
    public function test_backup_ids_cached(): void {
1 efrain 43
        global $DB;
44
        $dbman = $DB->get_manager(); // We are going to use database_manager services.
45
 
46
        $this->resetAfterTest(true); // Playing with temp tables, better reset once finished.
47
 
48
        // Some variables and objects for testing.
49
        $restoreid = 'testrestoreid';
50
 
51
        $mapping = new \stdClass();
52
        $mapping->itemname = 'user';
53
        $mapping->itemid = 1;
54
        $mapping->newitemid = 2;
55
        $mapping->parentitemid = 3;
56
        $mapping->info = 'info';
57
 
58
        // Create the backup_ids temp tables used by restore.
59
        restore_controller_dbops::create_restore_temp_tables($restoreid);
60
 
61
        // Send one mapping using the public api with defaults.
62
        restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
63
        // Get that mapping and verify everything is returned as expected.
64
        $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
65
        $this->assertSame($mapping->itemname, $result->itemname);
66
        $this->assertSame($mapping->itemid, $result->itemid);
67
        $this->assertSame(0, $result->newitemid);
68
        $this->assertSame(null, $result->parentitemid);
69
        $this->assertSame(null, $result->info);
70
 
71
        // Drop the backup_xxx_temp temptables manually, so memory cache won't be invalidated.
72
        $dbman->drop_table(new \xmldb_table('backup_ids_temp'));
73
        $dbman->drop_table(new \xmldb_table('backup_files_temp'));
74
 
75
        // Verify the mapping continues returning the same info,
76
        // now from cache (the table does not exist).
77
        $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
78
        $this->assertSame($mapping->itemname, $result->itemname);
79
        $this->assertSame($mapping->itemid, $result->itemid);
80
        $this->assertSame(0, $result->newitemid);
81
        $this->assertSame(null, $result->parentitemid);
82
        $this->assertSame(null, $result->info);
83
 
84
        // Recreate the temp table, just to drop it using the restore API in
85
        // order to check that, then, the cache becomes invalid for the same request.
86
        restore_controller_dbops::create_restore_temp_tables($restoreid);
87
        restore_controller_dbops::drop_restore_temp_tables($restoreid);
88
 
89
        // No cached info anymore, so the mapping request will arrive to
90
        // DB leading to error (temp table does not exist).
91
        try {
92
            $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
93
            $this->fail('Expecting an exception, none occurred');
94
        } catch (\Exception $e) {
95
            $this->assertTrue($e instanceof \dml_exception);
96
            $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
97
        }
98
 
99
        // Create the backup_ids temp tables once more.
100
        restore_controller_dbops::create_restore_temp_tables($restoreid);
101
 
102
        // Send one mapping using the public api with complete values.
103
        restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid,
104
                $mapping->newitemid, $mapping->parentitemid, $mapping->info);
105
        // Get that mapping and verify everything is returned as expected.
106
        $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
107
        $this->assertSame($mapping->itemname, $result->itemname);
108
        $this->assertSame($mapping->itemid, $result->itemid);
109
        $this->assertSame($mapping->newitemid, $result->newitemid);
110
        $this->assertSame($mapping->parentitemid, $result->parentitemid);
111
        $this->assertSame($mapping->info, $result->info);
112
 
113
        // Finally, drop the temp tables properly and get the DB error again (memory caches empty).
114
        restore_controller_dbops::drop_restore_temp_tables($restoreid);
115
        try {
116
            $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
117
            $this->fail('Expecting an exception, none occurred');
118
        } catch (\Exception $e) {
119
            $this->assertTrue($e instanceof \dml_exception);
120
            $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
121
        }
122
    }
123
 
124
    /**
125
     * Data provider for {@link test_precheck_user()}
126
     */
1441 ariadna 127
    public static function precheck_user_provider(): array {
1 efrain 128
        $emailmultiplier = [
129
            'shortmail' => 'normalusername@example.com',
130
            'longmail' => str_repeat('a', 100)  // It's not validated, hence any string is ok.
131
        ];
132
 
133
        $providercases = [];
134
 
135
        foreach ($emailmultiplier as $emailk => $email) {
136
            // Get the related cases.
1441 ariadna 137
            $cases = self::precheck_user_cases($email);
1 efrain 138
            // Rename them (keys).
139
            foreach ($cases as $key => $case) {
140
                $providercases[$key . ' - ' . $emailk] = $case;
141
            }
142
        }
143
 
144
        return $providercases;
145
    }
146
 
147
    /**
148
     * Get all the cases implemented in {@link restore_dbops::precheck_users()}
149
     *
150
     * @param string $email
151
     */
1441 ariadna 152
    private static function precheck_user_cases($email) {
1 efrain 153
        global $CFG;
154
 
155
        $baseuserarr = [
156
            'username' => 'normalusername',
157
            'email'    => $email,
158
            'mnethostid' => $CFG->mnet_localhost_id,
159
            'firstaccess' => 123456789,
160
            'deleted'    => 0,
161
            'forceemailcleanup' => false, // Hack to force the DB record to have empty mail.
162
            'forceduplicateadminallowed' => false]; // Hack to enable import_general_duplicate_admin_allowed.
163
 
164
        return [
165
            // Cases with samesite = true.
166
            'samesite match existing (1A)' => [
167
                'dbuser' => $baseuserarr,
168
                'backupuser' => $baseuserarr,
169
                'samesite' => true,
170
                'outcome' => 'match'
171
            ],
172
            'samesite match existing anon (1B)' => [
173
                'dbuser' => array_merge($baseuserarr, [
174
                    'username' => 'anon01']),
175
                'backupuser' => array_merge($baseuserarr, [
176
                    'id' => -1, 'username' => 'anon01', 'firstname' => 'anonfirstname01',
177
                    'lastname' => 'anonlastname01', 'email' => 'anon01@doesntexist.invalid']),
178
                'samesite' => true,
179
                'outcome' => 'match'
180
            ],
181
            'samesite match existing deleted in db, alive in backup, by db username (1C)' => [
182
                'dbuser' => array_merge($baseuserarr, [
183
                    'deleted' => 1]),
184
                'backupuser' => array_merge($baseuserarr, [
185
                    'username' => 'this_wont_match']),
186
                'samesite' => true,
187
                'outcome' => 'match'
188
            ],
189
            'samesite match existing deleted in db, alive in backup, by db email (1C)' => [
190
                'dbuser' => array_merge($baseuserarr, [
191
                    'deleted' => 1]),
192
                'backupuser' => array_merge($baseuserarr, [
193
                    'email' => 'this_wont_match']),
194
                'samesite' => true,
195
                'outcome' => 'match'
196
            ],
197
            'samesite match existing alive in db, deleted in backup (1D)' => [
198
                'dbuser' => $baseuserarr,
199
                'backupuser' => array_merge($baseuserarr, [
200
                    'deleted' => 1]),
201
                'samesite' => true,
202
                'outcome' => 'match'
203
            ],
204
            'samesite conflict (1E)' => [
205
                'dbuser' => $baseuserarr,
206
                'backupuser' => array_merge($baseuserarr, ['id' => -1]),
207
                'samesite' => true,
208
                'outcome' => false
209
            ],
210
            'samesite create user (1F)' => [
211
                'dbuser' => $baseuserarr,
212
                'backupuser' => array_merge($baseuserarr, [
213
                    'username' => 'newusername']),
214
                'samesite' => false,
215
                'outcome' => true
216
            ],
217
 
218
            // Cases with samesite = false.
219
            'no samesite match existing, by db email (2A1)' => [
220
                'dbuser' => $baseuserarr,
221
                'backupuser' => array_merge($baseuserarr, [
222
                    'firstaccess' => 0]),
223
                'samesite' => false,
224
                'outcome' => 'match'
225
            ],
226
            'no samesite match existing, by db firstaccess (2A1)' => [
227
                'dbuser' => $baseuserarr,
228
                'backupuser' => array_merge($baseuserarr, [
229
                    'email' => 'this_wont_match@example.con']),
230
                'samesite' => false,
231
                'outcome' => 'match'
232
            ],
233
            'no samesite match existing anon (2A1 too)' => [
234
                'dbuser' => array_merge($baseuserarr, [
235
                    'username' => 'anon01']),
236
                'backupuser' => array_merge($baseuserarr, [
237
                    'id' => -1, 'username' => 'anon01', 'firstname' => 'anonfirstname01',
238
                    'lastname' => 'anonlastname01', 'email' => 'anon01@doesntexist.invalid']),
239
                'samesite' => false,
240
                'outcome' => 'match'
241
            ],
242
            'no samesite match dupe admin (2A2)' => [
243
                'dbuser' => array_merge($baseuserarr, [
244
                    'username' => 'admin_old_site_id',
245
                    'forceduplicateadminallowed' => true]),
246
                'backupuser' => array_merge($baseuserarr, [
247
                    'username' => 'admin']),
248
                'samesite' => false,
249
                'outcome' => 'match'
250
            ],
251
            'no samesite match existing deleted in db, alive in backup, by db username (2B1)' => [
252
                'dbuser' => array_merge($baseuserarr, [
253
                    'deleted' => 1]),
254
                'backupuser' => array_merge($baseuserarr, [
255
                    'firstaccess' => 0]),
256
                'samesite' => false,
257
                'outcome' => 'match'
258
            ],
259
            'no samesite match existing deleted in db, alive in backup, by db firstaccess (2B1)' => [
260
                'dbuser' => array_merge($baseuserarr, [
261
                    'deleted' => 1]),
262
                'backupuser' => array_merge($baseuserarr, [
263
                    'mail' => 'this_wont_match']),
264
                'samesite' => false,
265
                'outcome' => 'match'
266
            ],
267
            'no samesite match existing deleted in db, alive in backup (2B2)' => [
268
                'dbuser' => array_merge($baseuserarr, [
269
                    'deleted' => 1,
270
                    'forceemailcleanup' => true]),
271
                'backupuser' => $baseuserarr,
272
                'samesite' => false,
273
                'outcome' => 'match'
274
            ],
275
            'no samesite match existing alive in db, deleted in backup (2C)' => [
276
                'dbuser' => $baseuserarr,
277
                'backupuser' => array_merge($baseuserarr, [
278
                    'deleted' => 1]),
279
                'samesite' => false,
280
                'outcome' => 'match'
281
            ],
282
            'no samesite conflict (2D)' => [
283
                'dbuser' => $baseuserarr,
284
                'backupuser' => array_merge($baseuserarr, [
285
                    'email' => 'anotheruser@example.com', 'firstaccess' => 0]),
286
                'samesite' => false,
287
                'outcome' => false
288
            ],
289
            'no samesite create user (2E)' => [
290
                'dbuser' => $baseuserarr,
291
                'backupuser' => array_merge($baseuserarr, [
292
                    'username' => 'newusername']),
293
                'samesite' => false,
294
                'outcome' => true
295
            ],
296
 
297
        ];
298
    }
299
 
300
    /**
301
     * Test restore precheck_user method
302
     *
303
     * @dataProvider precheck_user_provider
304
     * @covers \restore_dbops::precheck_user()
305
     *
306
     * @param array $dbuser
307
     * @param array $backupuser
308
     * @param bool $samesite
309
     * @param mixed $outcome
310
     **/
11 efrain 311
    public function test_precheck_user($dbuser, $backupuser, $samesite, $outcome): void {
1 efrain 312
        global $DB;
313
 
314
        $this->resetAfterTest();
315
 
316
        $dbuser = (object)$dbuser;
317
        $backupuser = (object)$backupuser;
318
 
319
        $siteid = null;
320
 
321
        // If the backup user must be deleted, simulate it (by temp inserting to DB, deleting and fetching it back).
322
        if ($backupuser->deleted) {
323
            $backupuser->id = $DB->insert_record('user', array_merge((array)$backupuser, ['deleted' => 0]));
324
            delete_user($backupuser);
325
            $backupuser = $DB->get_record('user', ['id' => $backupuser->id]);
326
            $DB->delete_records('user', ['id' => $backupuser->id]);
327
            unset($backupuser->id);
328
        }
329
 
330
        // Create the db user, normally.
331
        $dbuser->id = $DB->insert_record('user', array_merge((array)$dbuser, ['deleted' => 0]));
332
        $backupuser->id = $backupuser->id ?? $dbuser->id;
333
 
334
        // We may want to enable the import_general_duplicate_admin_allowed setting and look for old admin records.
335
        if ($dbuser->forceduplicateadminallowed) {
336
            set_config('import_general_duplicate_admin_allowed', true, 'backup');
337
            $siteid = 'old_site_id';
338
        }
339
 
340
        // If the DB user must be deleted, do it and fetch it back.
341
        if ($dbuser->deleted) {
342
            delete_user($dbuser);
343
            // We may want to clean the mail field (old behavior, not containing the current md5(username).
344
            if ($dbuser->forceemailcleanup) {
345
                $DB->set_field('user', 'email', '', ['id' => $dbuser->id]);
346
            }
347
        }
348
 
349
        // Get the dbuser  record, because we may have changed it above.
350
        $dbuser = $DB->get_record('user', ['id' => $dbuser->id]);
351
 
352
        $method = (new \ReflectionClass('restore_dbops'))->getMethod('precheck_user');
353
        $result = $method->invoke(null, $backupuser, $samesite, $siteid);
354
 
355
        if (is_bool($result)) {
356
            $this->assertSame($outcome, $result);
357
        } else {
358
            $outcome = $dbuser; // Outcome is not bool, matching found, so it must be the dbuser,
359
            // Just check ids, it means the expected match has been found in database.
360
            $this->assertSame($outcome->id, $result->id);
361
        }
362
    }
363
}