MDL-42882 upgrade: unit tests for root folders upgrade
authorDan Poltawski <dan@moodle.com>
Mon, 3 Feb 2014 04:30:23 +0000 (12:30 +0800)
committerDan Poltawski <dan@moodle.com>
Mon, 3 Feb 2014 09:29:51 +0000 (17:29 +0800)
Just verifying the original behaviour

lib/db/upgrade.php
lib/tests/upgradelib_test.php
lib/upgradelib.php

index 0fe0e43..d0d6637 100644 (file)
@@ -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);
         }
index 0ff743f..ddad643 100644 (file)
@@ -149,4 +149,50 @@ class core_upgradelib_testcase extends advanced_testcase {
 
         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));
+    }
 }
index 338a5cc..3bdd006 100644 (file)
@@ -2089,3 +2089,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();
+}