From 7ea6574332ed394ea1807dc4a2b4127ae6989898 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Mon, 3 Feb 2014 12:30:23 +0800 Subject: [PATCH] MDL-42882 upgrade: unit tests for root folders upgrade Just verifying the original behaviour --- lib/db/upgrade.php | 25 +------------------ lib/tests/upgradelib_test.php | 46 +++++++++++++++++++++++++++++++++++ lib/upgradelib.php | 34 ++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 24 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 953ab4cefe8..ad2a79f13b6 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2216,30 +2216,7 @@ function xmldb_main_upgrade($oldversion) { if ($oldversion < 2013051402.10) { // 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); } diff --git a/lib/tests/upgradelib_test.php b/lib/tests/upgradelib_test.php index 7b56cb7d9d5..70547536b05 100644 --- a/lib/tests/upgradelib_test.php +++ b/lib/tests/upgradelib_test.php @@ -149,4 +149,50 @@ class upgradelib_test 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)); + } } diff --git a/lib/upgradelib.php b/lib/upgradelib.php index d889e214de1..b69e38a1ed5 100644 --- a/lib/upgradelib.php +++ b/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(); +}