2014-11-05 12:20:43 +00: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 14:25:31 +00:00
|
|
|
use OC\Command\QueueBus;
|
2015-03-24 09:02:48 +00:00
|
|
|
use OC\Files\Filesystem;
|
2015-10-05 08:13:45 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2015-11-02 23:23:05 +00:00
|
|
|
use OCP\IDBConnection;
|
2014-12-02 12:51:36 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
2014-11-05 12:20:43 +00:00
|
|
|
abstract class TestCase extends \PHPUnit_Framework_TestCase {
|
2015-11-02 23:23:05 +00:00
|
|
|
/** @var \OC\Command\QueueBus */
|
2015-03-02 14:25:31 +00:00
|
|
|
private $commandBus;
|
|
|
|
|
2015-11-02 23:23:05 +00:00
|
|
|
/** @var IDBConnection */
|
2015-12-21 17:08:08 +00:00
|
|
|
static protected $realDatabase = null;
|
|
|
|
static private $wasDatabaseAllowed = false;
|
2015-11-02 23:23:05 +00:00
|
|
|
|
2015-08-20 12:38:51 +00:00
|
|
|
protected function getTestTraits() {
|
|
|
|
$traits = [];
|
|
|
|
$class = $this;
|
|
|
|
do {
|
|
|
|
$traits = array_merge(class_uses($class), $traits);
|
|
|
|
} while ($class = get_parent_class($class));
|
|
|
|
foreach ($traits as $trait => $same) {
|
|
|
|
$traits = array_merge(class_uses($trait), $traits);
|
|
|
|
}
|
|
|
|
$traits = array_unique($traits);
|
|
|
|
return array_filter($traits, function ($trait) {
|
|
|
|
return substr($trait, 0, 5) === 'Test\\';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-02 14:25:31 +00:00
|
|
|
protected function setUp() {
|
2015-11-02 23:23:05 +00:00
|
|
|
// detect database access
|
2015-12-21 17:08:08 +00:00
|
|
|
self::$wasDatabaseAllowed = true;
|
2015-11-02 23:23:05 +00:00
|
|
|
if (!$this->IsDatabaseAccessAllowed()) {
|
2015-12-21 17:08:08 +00:00
|
|
|
self::$wasDatabaseAllowed = false;
|
2015-11-02 23:23:05 +00:00
|
|
|
if (is_null(self::$realDatabase)) {
|
|
|
|
self::$realDatabase = \OC::$server->getDatabaseConnection();
|
|
|
|
}
|
|
|
|
\OC::$server->registerService('DatabaseConnection', function () {
|
|
|
|
$this->fail('Your test case is not allowed to access the database.');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-02 14:25:31 +00:00
|
|
|
// overwrite the command bus with one we can run ourselves
|
|
|
|
$this->commandBus = new QueueBus();
|
2015-03-24 09:02:48 +00:00
|
|
|
\OC::$server->registerService('AsyncCommandBus', function () {
|
2015-03-02 14:25:31 +00:00
|
|
|
return $this->commandBus;
|
|
|
|
});
|
2015-08-20 12:38:51 +00:00
|
|
|
|
|
|
|
$traits = $this->getTestTraits();
|
|
|
|
foreach ($traits as $trait) {
|
|
|
|
$methodName = 'setUp' . basename(str_replace('\\', '/', $trait));
|
|
|
|
if (method_exists($this, $methodName)) {
|
|
|
|
call_user_func([$this, $methodName]);
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 14:25:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 09:19:30 +00:00
|
|
|
protected function tearDown() {
|
2015-11-02 23:23:05 +00:00
|
|
|
// restore database connection
|
|
|
|
if (!$this->IsDatabaseAccessAllowed()) {
|
|
|
|
\OC::$server->registerService('DatabaseConnection', function () {
|
|
|
|
return self::$realDatabase;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// further cleanup
|
2015-03-24 09:19:30 +00:00
|
|
|
$hookExceptions = \OC_Hook::$thrownExceptions;
|
|
|
|
\OC_Hook::$thrownExceptions = [];
|
2015-05-20 11:08:56 +00:00
|
|
|
\OC::$server->getLockingProvider()->releaseAll();
|
2015-08-20 12:38:51 +00:00
|
|
|
if (!empty($hookExceptions)) {
|
2015-03-24 09:19:30 +00:00
|
|
|
throw $hookExceptions[0];
|
|
|
|
}
|
2015-08-20 12:38:51 +00:00
|
|
|
|
2015-12-17 16:15:04 +00:00
|
|
|
// fail hard if xml errors have not been cleaned up
|
|
|
|
$errors = libxml_get_errors();
|
|
|
|
libxml_clear_errors();
|
|
|
|
$this->assertEquals([], $errors);
|
|
|
|
|
|
|
|
// tearDown the traits
|
2015-08-20 12:38:51 +00:00
|
|
|
$traits = $this->getTestTraits();
|
|
|
|
foreach ($traits as $trait) {
|
|
|
|
$methodName = 'tearDown' . basename(str_replace('\\', '/', $trait));
|
|
|
|
if (method_exists($this, $methodName)) {
|
|
|
|
call_user_func([$this, $methodName]);
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 09:19:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 10:03:02 +00:00
|
|
|
/**
|
|
|
|
* Allows us to test private methods/properties
|
|
|
|
*
|
|
|
|
* @param $object
|
|
|
|
* @param $methodName
|
|
|
|
* @param array $parameters
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected static function invokePrivate($object, $methodName, array $parameters = array()) {
|
|
|
|
$reflection = new \ReflectionClass(get_class($object));
|
|
|
|
|
|
|
|
if ($reflection->hasMethod($methodName)) {
|
|
|
|
$method = $reflection->getMethod($methodName);
|
|
|
|
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
return $method->invokeArgs($object, $parameters);
|
|
|
|
} elseif ($reflection->hasProperty($methodName)) {
|
|
|
|
$property = $reflection->getProperty($methodName);
|
|
|
|
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
|
|
|
if (!empty($parameters)) {
|
|
|
|
$property->setValue($object, array_pop($parameters));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $property->getValue($object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-19 13:40:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a unique identifier as uniqid() is not reliable sometimes
|
|
|
|
*
|
|
|
|
* @param string $prefix
|
|
|
|
* @param int $length
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-02 12:51:36 +00:00
|
|
|
protected static function getUniqueID($prefix = '', $length = 13) {
|
2016-01-11 18:59:15 +00:00
|
|
|
return $prefix . \OC::$server->getSecureRandom()->generate(
|
2014-11-05 12:20:43 +00:00
|
|
|
$length,
|
2014-11-19 13:40:15 +00:00
|
|
|
// Do not use dots and slashes as we use the value for file names
|
2014-12-02 12:51:36 +00:00
|
|
|
ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
|
2014-11-05 12:20:43 +00:00
|
|
|
);
|
|
|
|
}
|
2014-11-07 14:23:15 +00:00
|
|
|
|
|
|
|
public static function tearDownAfterClass() {
|
2015-12-21 17:08:08 +00:00
|
|
|
if (!self::$wasDatabaseAllowed && self::$realDatabase !== null) {
|
|
|
|
// in case an error is thrown in a test, PHPUnit jumps straight to tearDownAfterClass,
|
|
|
|
// so we need the database again
|
|
|
|
\OC::$server->registerService('DatabaseConnection', function () {
|
|
|
|
return self::$realDatabase;
|
|
|
|
});
|
|
|
|
}
|
2014-11-19 13:40:15 +00:00
|
|
|
$dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');
|
2015-12-21 17:08:08 +00:00
|
|
|
if (self::$wasDatabaseAllowed && \OC::$server->getDatabaseConnection()) {
|
2015-12-17 16:29:50 +00:00
|
|
|
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
|
2014-11-19 13:40:15 +00:00
|
|
|
|
2015-12-17 16:29:50 +00:00
|
|
|
self::tearDownAfterClassCleanShares($queryBuilder);
|
|
|
|
self::tearDownAfterClassCleanStorages($queryBuilder);
|
|
|
|
self::tearDownAfterClassCleanFileCache($queryBuilder);
|
|
|
|
}
|
2014-11-19 13:40:15 +00:00
|
|
|
self::tearDownAfterClassCleanStrayDataFiles($dataDir);
|
|
|
|
self::tearDownAfterClassCleanStrayHooks();
|
2015-06-22 10:10:06 +00:00
|
|
|
self::tearDownAfterClassCleanStrayLocks();
|
2014-11-19 13:40:15 +00:00
|
|
|
|
|
|
|
parent::tearDownAfterClass();
|
|
|
|
}
|
|
|
|
|
2015-10-05 08:13:45 +00:00
|
|
|
/**
|
|
|
|
* Remove all entries from the share table
|
|
|
|
*
|
|
|
|
* @param IQueryBuilder $queryBuilder
|
|
|
|
*/
|
|
|
|
static protected function tearDownAfterClassCleanShares(IQueryBuilder $queryBuilder) {
|
|
|
|
$queryBuilder->delete('share')
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
2014-11-19 13:40:15 +00:00
|
|
|
/**
|
|
|
|
* Remove all entries from the storages table
|
2015-03-02 14:25:31 +00:00
|
|
|
*
|
2015-10-05 08:13:45 +00:00
|
|
|
* @param IQueryBuilder $queryBuilder
|
2014-11-19 13:40:15 +00:00
|
|
|
*/
|
2015-10-05 08:13:45 +00:00
|
|
|
static protected function tearDownAfterClassCleanStorages(IQueryBuilder $queryBuilder) {
|
|
|
|
$queryBuilder->delete('storages')
|
|
|
|
->execute();
|
2014-11-19 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all entries from the filecache table
|
2015-03-02 14:25:31 +00:00
|
|
|
*
|
2015-10-05 08:13:45 +00:00
|
|
|
* @param IQueryBuilder $queryBuilder
|
2014-11-19 13:40:15 +00:00
|
|
|
*/
|
2015-10-05 08:13:45 +00:00
|
|
|
static protected function tearDownAfterClassCleanFileCache(IQueryBuilder $queryBuilder) {
|
|
|
|
$queryBuilder->delete('filecache')
|
|
|
|
->execute();
|
2014-11-19 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all unused files from the data dir
|
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
*/
|
|
|
|
static protected function tearDownAfterClassCleanStrayDataFiles($dataDir) {
|
|
|
|
$knownEntries = array(
|
2015-03-02 14:25:31 +00:00
|
|
|
'owncloud.log' => true,
|
|
|
|
'owncloud.db' => true,
|
|
|
|
'.ocdata' => true,
|
|
|
|
'..' => true,
|
|
|
|
'.' => true,
|
2014-11-19 13:40:15 +00: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) {
|
2015-09-21 12:09:28 +00:00
|
|
|
if (\OC\Files\Filesystem::isIgnoredDir($file)) {
|
2014-11-19 13:40:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$path = $dir . '/' . $file;
|
|
|
|
if (is_dir($path)) {
|
|
|
|
self::tearDownAfterClassCleanStrayDataUnlinkDir($path);
|
2015-03-02 14:25:31 +00:00
|
|
|
} else {
|
2014-11-19 13:40:15 +00:00
|
|
|
@unlink($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
|
|
|
@rmdir($dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up the list of hooks
|
|
|
|
*/
|
|
|
|
static protected function tearDownAfterClassCleanStrayHooks() {
|
|
|
|
\OC_Hook::clear();
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:10:06 +00:00
|
|
|
/**
|
|
|
|
* Clean up the list of locks
|
|
|
|
*/
|
|
|
|
static protected function tearDownAfterClassCleanStrayLocks() {
|
|
|
|
\OC::$server->getLockingProvider()->releaseAll();
|
|
|
|
}
|
|
|
|
|
2014-12-04 13:01:23 +00:00
|
|
|
/**
|
|
|
|
* Login and setup FS as a given user,
|
|
|
|
* sets the given user as the current user.
|
|
|
|
*
|
2015-04-08 10:03:55 +00:00
|
|
|
* @param string $user user id or empty for a generic FS
|
2014-12-04 13:01:23 +00:00
|
|
|
*/
|
2015-04-08 10:03:55 +00:00
|
|
|
static protected function loginAsUser($user = '') {
|
2014-12-04 13:01:23 +00:00
|
|
|
self::logout();
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
\OC_User::setUserId($user);
|
|
|
|
\OC_Util::setupFS($user);
|
2015-05-11 13:55:23 +00:00
|
|
|
if (\OC_User::userExists($user)) {
|
|
|
|
\OC::$server->getUserFolder($user);
|
|
|
|
}
|
2014-12-04 13:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout the current user and tear down the filesystem.
|
|
|
|
*/
|
|
|
|
static protected function logout() {
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
2015-05-15 10:29:47 +00:00
|
|
|
// needed for fully logout
|
|
|
|
\OC::$server->getUserSession()->setUser(null);
|
2014-12-04 13:01:23 +00:00
|
|
|
}
|
2015-03-02 14:25:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run all commands pushed to the bus
|
|
|
|
*/
|
|
|
|
protected function runCommands() {
|
2015-03-24 09:02:48 +00: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 14:25:31 +00:00
|
|
|
$this->commandBus->run();
|
2015-03-24 09:02:48 +00:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
\OC_Util::setupFS($user);
|
|
|
|
}
|
2015-03-02 14:25:31 +00:00
|
|
|
}
|
2015-06-12 16:50:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given path is locked with a given type
|
|
|
|
*
|
|
|
|
* @param \OC\Files\View $view view
|
|
|
|
* @param string $path path to check
|
|
|
|
* @param int $type lock type
|
2015-06-19 11:54:00 +00:00
|
|
|
* @param bool $onMountPoint true to check the mount point instead of the
|
|
|
|
* mounted storage
|
2015-06-12 16:50:49 +00:00
|
|
|
*
|
|
|
|
* @return boolean true if the file is locked with the
|
|
|
|
* given type, false otherwise
|
|
|
|
*/
|
2015-06-19 11:54:00 +00:00
|
|
|
protected function isFileLocked($view, $path, $type, $onMountPoint = false) {
|
2015-06-12 16:50:49 +00:00
|
|
|
// Note: this seems convoluted but is necessary because
|
|
|
|
// the format of the lock key depends on the storage implementation
|
|
|
|
// (in our case mostly md5)
|
|
|
|
|
|
|
|
if ($type === \OCP\Lock\ILockingProvider::LOCK_SHARED) {
|
|
|
|
// to check if the file has a shared lock, try acquiring an exclusive lock
|
|
|
|
$checkType = \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE;
|
|
|
|
} else {
|
|
|
|
// a shared lock cannot be set if exclusive lock is in place
|
|
|
|
$checkType = \OCP\Lock\ILockingProvider::LOCK_SHARED;
|
|
|
|
}
|
|
|
|
try {
|
2015-06-19 11:54:00 +00:00
|
|
|
$view->lockFile($path, $checkType, $onMountPoint);
|
2015-06-12 16:50:49 +00:00
|
|
|
// no exception, which means the lock of $type is not set
|
|
|
|
// clean up
|
2015-06-19 11:54:00 +00:00
|
|
|
$view->unlockFile($path, $checkType, $onMountPoint);
|
2015-06-12 16:50:49 +00:00
|
|
|
return false;
|
|
|
|
} catch (\OCP\Lock\LockedException $e) {
|
|
|
|
// we could not acquire the counter-lock, which means
|
|
|
|
// the lock of $type was in place
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 23:23:05 +00:00
|
|
|
|
|
|
|
private function IsDatabaseAccessAllowed() {
|
2015-11-30 09:52:52 +00:00
|
|
|
// on travis-ci.org we allow database access in any case - otherwise
|
|
|
|
// this will break all apps right away
|
|
|
|
if (true == getenv('TRAVIS')) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-02 23:23:05 +00:00
|
|
|
$annotations = $this->getAnnotations();
|
|
|
|
if (isset($annotations['class']['group']) && in_array('DB', $annotations['class']['group'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-05 12:20:43 +00:00
|
|
|
}
|