2014-11-05 15:20:43 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Joas Schilling
|
|
|
|
* @copyright 2014 Joas Schilling nickvergessen@owncloud.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
2015-03-02 17:25:31 +03:00
|
|
|
use OC\Command\QueueBus;
|
2015-03-24 12:02:48 +03:00
|
|
|
use OC\Files\Filesystem;
|
2014-12-02 15:51:36 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
2014-11-05 15:20:43 +03:00
|
|
|
abstract class TestCase extends \PHPUnit_Framework_TestCase {
|
2015-03-02 17:25:31 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Command\QueueBus
|
|
|
|
*/
|
|
|
|
private $commandBus;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
// overwrite the command bus with one we can run ourselves
|
|
|
|
$this->commandBus = new QueueBus();
|
2015-03-24 12:02:48 +03:00
|
|
|
\OC::$server->registerService('AsyncCommandBus', function () {
|
2015-03-02 17:25:31 +03:00
|
|
|
return $this->commandBus;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:19:30 +03:00
|
|
|
protected function tearDown() {
|
|
|
|
$hookExceptions = \OC_Hook::$thrownExceptions;
|
|
|
|
\OC_Hook::$thrownExceptions = [];
|
|
|
|
if(!empty($hookExceptions)) {
|
|
|
|
throw $hookExceptions[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 16:40:15 +03:00
|
|
|
/**
|
|
|
|
* Returns a unique identifier as uniqid() is not reliable sometimes
|
|
|
|
*
|
|
|
|
* @param string $prefix
|
|
|
|
* @param int $length
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-02 15:51:36 +03:00
|
|
|
protected static function getUniqueID($prefix = '', $length = 13) {
|
2014-11-05 15:20:43 +03:00
|
|
|
return $prefix . \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(
|
|
|
|
$length,
|
2014-11-19 16:40:15 +03:00
|
|
|
// Do not use dots and slashes as we use the value for file names
|
2014-12-02 15:51:36 +03:00
|
|
|
ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
|
2014-11-05 15:20:43 +03:00
|
|
|
);
|
|
|
|
}
|
2014-11-07 17:23:15 +03:00
|
|
|
|
|
|
|
public static function tearDownAfterClass() {
|
2014-11-19 16:40:15 +03:00
|
|
|
$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
|
2015-03-02 17:25:31 +03:00
|
|
|
*
|
2014-11-19 16:40:15 +03:00
|
|
|
* @param string $dataDir
|
|
|
|
*/
|
|
|
|
static protected function tearDownAfterClassCleanFileMapper($dataDir) {
|
2014-11-07 17:23:15 +03:00
|
|
|
if (\OC_Util::runningOnWindows()) {
|
2014-11-19 16:40:15 +03:00
|
|
|
$mapper = new \OC\Files\Mapper($dataDir);
|
|
|
|
$mapper->removePath($dataDir, true, true);
|
2014-11-07 17:23:15 +03:00
|
|
|
}
|
2014-11-19 16:40:15 +03:00
|
|
|
}
|
2014-11-07 17:23:15 +03:00
|
|
|
|
2014-11-19 16:40:15 +03:00
|
|
|
/**
|
|
|
|
* Remove all entries from the storages table
|
2015-03-02 17:25:31 +03:00
|
|
|
*
|
2014-11-26 14:38:24 +03:00
|
|
|
* @throws \OC\DatabaseException
|
2014-11-19 16:40:15 +03:00
|
|
|
*/
|
|
|
|
static protected function tearDownAfterClassCleanStorages() {
|
|
|
|
$sql = 'DELETE FROM `*PREFIX*storages`';
|
|
|
|
$query = \OC_DB::prepare($sql);
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all entries from the filecache table
|
2015-03-02 17:25:31 +03:00
|
|
|
*
|
2014-11-26 14:38:24 +03:00
|
|
|
* @throws \OC\DatabaseException
|
2014-11-19 16:40:15 +03:00
|
|
|
*/
|
|
|
|
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(
|
2015-03-02 17:25:31 +03:00
|
|
|
'owncloud.log' => true,
|
|
|
|
'owncloud.db' => true,
|
|
|
|
'.ocdata' => true,
|
|
|
|
'..' => true,
|
|
|
|
'.' => true,
|
2014-11-19 16:40:15 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
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);
|
2015-03-02 17:25:31 +03:00
|
|
|
} else {
|
2014-11-19 16:40:15 +03:00
|
|
|
@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();
|
2014-11-07 17:23:15 +03:00
|
|
|
}
|
2014-12-04 16:01:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Login and setup FS as a given user,
|
|
|
|
* sets the given user as the current user.
|
|
|
|
*
|
|
|
|
* @param string $user user id
|
|
|
|
*/
|
|
|
|
static protected function loginAsUser($user) {
|
|
|
|
self::logout();
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
\OC_User::setUserId($user);
|
|
|
|
\OC_Util::setupFS($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout the current user and tear down the filesystem.
|
|
|
|
*/
|
|
|
|
static protected function logout() {
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
}
|
2015-03-02 17:25:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run all commands pushed to the bus
|
|
|
|
*/
|
|
|
|
protected function runCommands() {
|
2015-03-24 12:02:48 +03:00
|
|
|
// get the user for which the fs is setup
|
|
|
|
$view = Filesystem::getView();
|
|
|
|
if ($view) {
|
|
|
|
list(, $user) = explode('/', $view->getRoot());
|
|
|
|
} else {
|
|
|
|
$user = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
\OC_Util::tearDownFS(); // command cant reply on the fs being setup
|
2015-03-02 17:25:31 +03:00
|
|
|
$this->commandBus->run();
|
2015-03-24 12:02:48 +03:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
\OC_Util::setupFS($user);
|
|
|
|
}
|
2015-03-02 17:25:31 +03:00
|
|
|
}
|
2014-11-05 15:20:43 +03:00
|
|
|
}
|