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

Conflicts:
	version.php
This commit is contained in:
Damyon Wiese 2014-11-03 14:10:43 +08:00
commit 219ebf9bb4
4 changed files with 83 additions and 1 deletions

View File

@ -4047,5 +4047,13 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2014102000.00);
}
if ($oldversion < 2014110300.00) {
// Run script restoring missing folder records for draft file areas.
upgrade_fix_missing_root_folders_draft();
// Main savepoint reached.
upgrade_main_savepoint(true, 2014110300.00);
}
return true;
}

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

View File

@ -2173,3 +2173,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();
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2014103100.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2014110300.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.