Skip to content

Commit

Permalink
Merge branch 'wip-MDL-48252-27' of git://github.com/abgreeve/moodle i…
Browse files Browse the repository at this point in the history
…nto MOODLE_27_STABLE
  • Loading branch information
danpoltawski committed Nov 24, 2014
2 parents 688b5dc + f45fad6 commit a054294
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/classes/task/file_temp_cleanup_task.php
Expand Up @@ -52,13 +52,28 @@ public function execute() {
// Show all child nodes prior to their parent.
$iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);

// An array of the full path (key) and date last modified.
$modifieddateobject = array();

// Get the time modified for each directory node. Nodes will be updated
// once a file is deleted, so we need a list of the original values.
for ($iter->rewind(); $iter->valid(); $iter->next()) {
$node = $iter->getRealPath();
if (!is_readable($node)) {
continue;
}
$modifieddateobject[$node] = $iter->getMTime();
}

// Now loop through again and remove old files and directories.
for ($iter->rewind(); $iter->valid(); $iter->next()) {
$node = $iter->getRealPath();
if (!is_readable($node)) {
continue;
}

// Check if file or directory is older than the given time.
if ($iter->getMTime() < $time) {
if ($modifieddateobject[$node] < $time) {
if ($iter->isDir() && !$iter->isDot()) {
// Don't attempt to delete the directory if it isn't empty.
if (!glob($node. DIRECTORY_SEPARATOR . '*')) {
Expand All @@ -72,9 +87,11 @@ public function execute() {
mtrace("Failed removing file '$node'.");
}
}
} else {
// Return the time modified to the original date.
touch($node, $modifieddateobject[$node]);
}
}

}

}
57 changes: 57 additions & 0 deletions lib/tests/scheduled_task_test.php
Expand Up @@ -281,4 +281,61 @@ public function test_get_broken_scheduled_task() {
$this->assertDebuggingCalled();
$this->assertNull($task);
}

/**
* Test that the file_temp_cleanup_task removes directories and
* files as expected.
*/
public function test_file_temp_cleanup_task() {
global $CFG;

// Create directories.
$dir = $CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR . 'courses';
mkdir($dir, 0777, true);

// Create files to be checked and then deleted.
$file01 = $dir . DIRECTORY_SEPARATOR . 'sections.xml';
file_put_contents($file01, 'test data 001');
$file02 = $dir . DIRECTORY_SEPARATOR . 'modules.xml';
file_put_contents($file02, 'test data 002');
// Change the time modified for the first file, to a time that will be deleted by the task (greater than seven days).
touch($file01, time() - (8 * 24 * 3600));

$task = \core\task\manager::get_scheduled_task('\\core\\task\\file_temp_cleanup_task');
$this->assertInstanceOf('\core\task\file_temp_cleanup_task', $task);
$task->execute();

// Scan the directory. Only modules.xml should be left.
$filesarray = scandir($dir);
$this->assertEquals('modules.xml', $filesarray[2]);
$this->assertEquals(3, count($filesarray));

// Change the time modified on modules.xml.
touch($file02, time() - (8 * 24 * 3600));
// Change the time modified on the courses directory.
touch($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR .
'courses', time() - (8 * 24 * 3600));
// Run the scheduled task to remove the file and directory.
$task->execute();
$filesarray = scandir($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01');
// There should only be two items in the array, '.' and '..'.
$this->assertEquals(2, count($filesarray));

// Change the time modified on all of the files and directories.
$dir = new \RecursiveDirectoryIterator($CFG->tempdir);
// Show all child nodes prior to their parent.
$iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);

for ($iter->rewind(); $iter->valid(); $iter->next()) {
$node = $iter->getRealPath();
touch($node, time() - (8 * 24 * 3600));
}

// Run the scheduled task again to remove all of the files and directories.
$task->execute();
$filesarray = scandir($CFG->tempdir);
// All of the files and directories should be deleted.
// There should only be two items in the array, '.' and '..'.
$this->assertEquals(2, count($filesarray));
}
}

0 comments on commit a054294

Please sign in to comment.