Clean up the test data in tearDownAfterClass()

The result of the listener should then be empty and can be removed
This commit is contained in:
Joas Schilling 2014-11-19 14:40:15 +01:00
parent cbb9caf030
commit 84d358a761
1 changed files with 112 additions and 6 deletions

View File

@ -23,21 +23,127 @@
namespace Test;
abstract class TestCase extends \PHPUnit_Framework_TestCase {
/**
* Returns a unique identifier as uniqid() is not reliable sometimes
*
* @param string $prefix
* @param int $length
* @return string
*/
protected function getUniqueID($prefix = '', $length = 13) {
// Do not use dots and slashes as we use the value for file names
return $prefix . \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(
$length,
// Do not use dots and slashes as we use the value for file names
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
);
}
public static function tearDownAfterClass() {
if (\OC_Util::runningOnWindows()) {
$rootDirectory = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');
$mapper = new \OC\Files\Mapper($rootDirectory);
$mapper->removePath($rootDirectory, true, true);
}
$dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');
self::tearDownAfterClassCleanFileMapper($dataDir);
self::tearDownAfterClassCleanStorages();
self::tearDownAfterClassCleanFileCache();
self::tearDownAfterClassCleanStrayDataFiles($dataDir);
self::tearDownAfterClassCleanStrayHooks();
self::tearDownAfterClassCleanProxies();
parent::tearDownAfterClass();
}
/**
* Remove all entries from the files map table
* @param string $dataDir
*/
static protected function tearDownAfterClassCleanFileMapper($dataDir) {
if (\OC_Util::runningOnWindows()) {
$mapper = new \OC\Files\Mapper($dataDir);
$mapper->removePath($dataDir, true, true);
}
}
/**
* Remove all entries from the storages table
* @throws \DatabaseException
*/
static protected function tearDownAfterClassCleanStorages() {
$sql = 'DELETE FROM `*PREFIX*storages`';
$query = \OC_DB::prepare($sql);
$query->execute();
}
/**
* Remove all entries from the filecache table
* @throws \DatabaseException
*/
static protected function tearDownAfterClassCleanFileCache() {
$sql = 'DELETE FROM `*PREFIX*filecache`';
$query = \OC_DB::prepare($sql);
$query->execute();
}
/**
* Remove all unused files from the data dir
*
* @param string $dataDir
*/
static protected function tearDownAfterClassCleanStrayDataFiles($dataDir) {
$knownEntries = array(
'owncloud.log' => true,
'owncloud.db' => true,
'.ocdata' => true,
'..' => true,
'.' => true,
);
if ($dh = opendir($dataDir)) {
while (($file = readdir($dh)) !== false) {
if (!isset($knownEntries[$file])) {
self::tearDownAfterClassCleanStrayDataUnlinkDir($dataDir . '/' . $file);
}
}
closedir($dh);
}
}
/**
* Recursive delete files and folders from a given directory
*
* @param string $dir
*/
static protected function tearDownAfterClassCleanStrayDataUnlinkDir($dir) {
if ($dh = @opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file === '..' || $file === '.') {
continue;
}
$path = $dir . '/' . $file;
if (is_dir($path)) {
self::tearDownAfterClassCleanStrayDataUnlinkDir($path);
}
else {
@unlink($path);
}
}
closedir($dh);
}
@rmdir($dir);
}
/**
* Clean up the list of hooks
*/
static protected function tearDownAfterClassCleanStrayHooks() {
\OC_Hook::clear();
}
/**
* Clean up the list of file proxies
*
* Also reenables file proxies, in case a test disabled them
*/
static protected function tearDownAfterClassCleanProxies() {
\OC_FileProxy::$enabled = true;
\OC_FileProxy::clearProxies();
}
}