Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-42882 upgrade: unit tests for root folders upgrade

Just verifying the original behaviour
  • Loading branch information...
commit 5f38edeefd5a50840d41ba0c2b8e11c5b7d17421 1 parent 4f6e4f8
@danpoltawski danpoltawski authored
View
25 lib/db/upgrade.php
@@ -2748,30 +2748,7 @@ function xmldb_main_upgrade($oldversion) {
if ($oldversion < 2013102500.01) {
// Find all fileareas that have missing root folder entry and add the root folder entry.
if (empty($CFG->filesrootrecordsfixed)) {
- $sql = "SELECT distinct f1.contextid, f1.component, f1.filearea, f1.itemid
- FROM {files} f1 left JOIN {files} f2
- ON f1.contextid = f2.contextid
- AND f1.component = f2.component
- AND f1.filearea = f2.filearea
- AND f1.itemid = f2.itemid
- AND f2.filename = '.'
- AND f2.filepath = '/'
- WHERE (f1.component <> 'user' or f1.filearea <> 'draft')
- and f2.id is null";
- $rs = $DB->get_recordset_sql($sql);
- $defaults = array('filepath' => '/',
- 'filename' => '.',
- 'userid' => $USER->id,
- 'filesize' => 0,
- 'timecreated' => time(),
- 'timemodified' => time(),
- 'contenthash' => sha1(''));
- foreach ($rs as $r) {
- $pathhash = sha1("/$r->contextid/$r->component/$r->filearea/$r->itemid".'/.');
- $DB->insert_record('files', (array)$r + $defaults +
- array('pathnamehash' => $pathhash));
- }
- $rs->close();
+ upgrade_fix_missing_root_folders();
// To skip running the same script on the upgrade to the next major release.
set_config('filesrootrecordsfixed', 1);
}
View
46 lib/tests/upgradelib_test.php
@@ -149,4 +149,50 @@ private function insert_fake_grade_item_sortorder($courseid, $sortorder) {
return $DB->get_record('grade_items', array('id' => $item->id));
}
+
+ public function test_upgrade_fix_missing_root_folders() {
+ global $DB, $SITE;
+
+ $this->resetAfterTest(true);
+
+ // Setup some broken data...
+ // Create two resources (and associated file areas).
+ $this->setAdminUser();
+ $resource1 = $this->getDataGenerator()->get_plugin_generator('mod_resource')
+ ->create_instance(array('course' => $SITE->id));
+ $resource2 = $this->getDataGenerator()->get_plugin_generator('mod_resource')
+ ->create_instance(array('course' => $SITE->id));
+
+ // Delete the folder record of resource1 to simulate broken data.
+ $context = context_module::instance($resource1->cmid);
+ $selectargs = array('contextid' => $context->id,
+ 'component' => 'mod_resource',
+ 'filearea' => 'content',
+ 'itemid' => 0);
+
+ // Verify file records exist.
+ $areafilecount = $DB->count_records('files', $selectargs);
+ $this->assertNotEmpty($areafilecount);
+
+ // Delete the folder record.
+ $folderrecord = $selectargs;
+ $folderrecord['filepath'] = '/';
+ $folderrecord['filename'] = '.';
+ $DB->delete_records('files', $folderrecord);
+
+ // Verify the folder record has been removed.
+ $newareafilecount = $DB->count_records('files', $selectargs);
+ $this->assertSame($newareafilecount, $areafilecount - 1);
+
+ $this->assertFalse($DB->record_exists('files', $folderrecord));
+
+ // Run the upgrade step!
+ upgrade_fix_missing_root_folders();
+
+ // Verify the folder record has been restored.
+ $newareafilecount = $DB->count_records('files', $selectargs);
+ $this->assertSame($newareafilecount, $areafilecount);
+
+ $this->assertTrue($DB->record_exists('files', $folderrecord));
+ }
}
View
34 lib/upgradelib.php
@@ -2088,3 +2088,37 @@ function upgrade_grade_item_fix_sortorder() {
$transaction->allow_commit();
}
+
+/**
+ * Detect file areas with missing root directory records and add them.
+ */
+function upgrade_fix_missing_root_folders() {
+ global $DB, $USER;
+
+ $transaction = $DB->start_delegated_transaction();
+ $sql = "SELECT distinct f1.contextid, f1.component, f1.filearea, f1.itemid
+ FROM {files} f1 left JOIN {files} f2
+ ON f1.contextid = f2.contextid
+ AND f1.component = f2.component
+ AND f1.filearea = f2.filearea
+ AND f1.itemid = f2.itemid
+ AND f2.filename = '.'
+ AND f2.filepath = '/'
+ WHERE (f1.component <> 'user' or f1.filearea <> 'draft')
+ and f2.id is null";
+ $rs = $DB->get_recordset_sql($sql);
+ $defaults = array('filepath' => '/',
+ 'filename' => '.',
+ 'userid' => $USER->id,
+ 'filesize' => 0,
+ 'timecreated' => time(),
+ 'timemodified' => time(),
+ 'contenthash' => sha1(''));
+ foreach ($rs as $r) {
+ $pathhash = sha1("/$r->contextid/$r->component/$r->filearea/$r->itemid".'/.');
+ $DB->insert_record('files', (array)$r + $defaults +
+ array('pathnamehash' => $pathhash));
+ }
+ $rs->close();
+ $transaction->allow_commit();
+}
Please sign in to comment.
Something went wrong with that request. Please try again.