Merge branch 'wip-MDL-45538-m27' of git://github.com/marinaglancy/moodle into MOODLE_27_STABLE

This commit is contained in:
Damyon Wiese
2014-11-03 14:25:45 +08:00
3 changed files with 82 additions and 0 deletions
+8
View File
@@ -3706,5 +3706,13 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2014051201.10);
}
if ($oldversion < 2014051202.10) {
// Run script restoring missing folder records for draft file areas.
upgrade_fix_missing_root_folders_draft();
// Main savepoint reached.
upgrade_main_savepoint(true, 2014051202.10);
}
return true;
}
+44
View File
@@ -201,6 +201,50 @@ class core_upgradelib_testcase extends advanced_testcase {
$this->assertSame($oldrecord->pathnamehash, $newrecord->pathnamehash);
}
public function test_upgrade_fix_missing_root_folders_draft() {
global $DB, $SITE;
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$usercontext = context_user::instance($user->id);
$this->setUser($user);
$resource1 = $this->getDataGenerator()->get_plugin_generator('mod_resource')
->create_instance(array('course' => $SITE->id));
$context = context_module::instance($resource1->cmid);
$draftitemid = 0;
file_prepare_draft_area($draftitemid, $context->id, 'mod_resource', 'content', 0);
$queryparams = array(
'component' => 'user',
'contextid' => $usercontext->id,
'filearea' => 'draft',
'itemid' => $draftitemid,
);
// Make sure there are two records in files for the draft file area and one of them has filename '.'.
$records = $DB->get_records_menu('files', $queryparams, '', 'id, filename');
$this->assertEquals(2, count($records));
$this->assertTrue(in_array('.', $records));
$originalhash = $DB->get_field('files', 'pathnamehash', $queryparams + array('filename' => '.'));
// Delete record with filename '.' and make sure it does not exist any more.
$DB->delete_records('files', $queryparams + array('filename' => '.'));
$records = $DB->get_records_menu('files', $queryparams, '', 'id, filename');
$this->assertEquals(1, count($records));
$this->assertFalse(in_array('.', $records));
// Run upgrade script and make sure the record is restored.
upgrade_fix_missing_root_folders_draft();
$records = $DB->get_records_menu('files', $queryparams, '', 'id, filename');
$this->assertEquals(2, count($records));
$this->assertTrue(in_array('.', $records));
$newhash = $DB->get_field('files', 'pathnamehash', $queryparams + array('filename' => '.'));
$this->assertEquals($originalhash, $newhash);
}
/**
* Tests the upgrade of an individual course-module or section from the
* old to new availability system. (This test does not use the database
+30
View File
@@ -2162,3 +2162,33 @@ function upgrade_fix_missing_root_folders() {
$rs->close();
$transaction->allow_commit();
}
/**
* Detect draft file areas with missing root directory records and add them.
*/
function upgrade_fix_missing_root_folders_draft() {
global $DB;
$transaction = $DB->start_delegated_transaction();
$sql = "SELECT contextid, itemid, MAX(timecreated) AS timecreated, MAX(timemodified) AS timemodified
FROM {files}
WHERE (component = 'user' AND filearea = 'draft')
GROUP BY contextid, itemid
HAVING MAX(CASE WHEN filename = '.' AND filepath = '/' THEN 1 ELSE 0 END) = 0";
$rs = $DB->get_recordset_sql($sql);
$defaults = array('component' => 'user',
'filearea' => 'draft',
'filepath' => '/',
'filename' => '.',
'userid' => 0, // Don't rely on any particular user for these system records.
'filesize' => 0,
'contenthash' => sha1(''));
foreach ($rs as $r) {
$r->pathnamehash = sha1("/$r->contextid/user/draft/$r->itemid/.");
$DB->insert_record('files', (array)$r + $defaults);
}
$rs->close();
$transaction->allow_commit();
}