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;
|
|
|
|
|
2016-04-12 13:49:11 +03:00
|
|
|
use DOMDocument;
|
|
|
|
use DOMNode;
|
2015-03-02 17:25:31 +03:00
|
|
|
use OC\Command\QueueBus;
|
2015-03-24 12:02:48 +03:00
|
|
|
use OC\Files\Filesystem;
|
2016-04-12 13:49:11 +03:00
|
|
|
use OC\Template\Base;
|
2020-10-12 22:03:58 +03:00
|
|
|
use OCP\Command\IBus;
|
2015-10-05 11:13:45 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
2015-11-03 02:23:05 +03:00
|
|
|
use OCP\IDBConnection;
|
2016-04-12 13:49:11 +03:00
|
|
|
use OCP\IL10N;
|
2014-12-02 15:51:36 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
2018-01-24 19:23:59 +03:00
|
|
|
abstract class TestCase extends \PHPUnit\Framework\TestCase {
|
2015-11-03 02:23:05 +03:00
|
|
|
/** @var \OC\Command\QueueBus */
|
2015-03-02 17:25:31 +03:00
|
|
|
private $commandBus;
|
|
|
|
|
2015-11-03 02:23:05 +03:00
|
|
|
/** @var IDBConnection */
|
2020-04-10 17:48:31 +03:00
|
|
|
protected static $realDatabase = null;
|
2016-04-12 13:49:11 +03:00
|
|
|
|
|
|
|
/** @var bool */
|
2020-04-10 17:48:31 +03:00
|
|
|
private static $wasDatabaseAllowed = false;
|
2015-11-03 02:23:05 +03:00
|
|
|
|
2016-01-18 11:53:25 +03:00
|
|
|
/** @var array */
|
|
|
|
protected $services = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $newService
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-07-28 10:38:43 +03:00
|
|
|
public function overwriteService(string $name, $newService): bool {
|
2016-01-18 11:53:25 +03:00
|
|
|
if (isset($this->services[$name])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->services[$name] = \OC::$server->query($name);
|
2020-07-28 10:38:43 +03:00
|
|
|
$container = \OC::$server->getAppContainerForService($name);
|
|
|
|
$container = $container ?? \OC::$server;
|
|
|
|
|
|
|
|
$container->registerService($name, function () use ($newService) {
|
2016-01-18 11:53:25 +03:00
|
|
|
return $newService;
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-07-28 10:38:43 +03:00
|
|
|
public function restoreService(string $name): bool {
|
2016-01-18 11:53:25 +03:00
|
|
|
if (isset($this->services[$name])) {
|
|
|
|
$oldService = $this->services[$name];
|
2020-07-28 10:38:43 +03:00
|
|
|
|
|
|
|
$container = \OC::$server->getAppContainerForService($name);
|
|
|
|
$container = $container ?? \OC::$server;
|
|
|
|
|
|
|
|
$container->registerService($name, function () use ($oldService) {
|
2016-01-18 11:53:25 +03:00
|
|
|
return $oldService;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
unset($this->services[$name]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:15:54 +03:00
|
|
|
public function restoreAllServices() {
|
|
|
|
if (!empty($this->services)) {
|
|
|
|
if (!empty($this->services)) {
|
|
|
|
foreach ($this->services as $name => $service) {
|
|
|
|
$this->restoreService($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:38:51 +03: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\\';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-11-08 18:32:41 +03:00
|
|
|
// overwrite the command bus with one we can run ourselves
|
|
|
|
$this->commandBus = new QueueBus();
|
|
|
|
$this->overwriteService('AsyncCommandBus', $this->commandBus);
|
2020-10-12 22:03:58 +03:00
|
|
|
$this->overwriteService(IBus::class, $this->commandBus);
|
2016-11-08 18:32:41 +03:00
|
|
|
|
2015-11-03 02:23:05 +03:00
|
|
|
// detect database access
|
2015-12-21 20:08:08 +03:00
|
|
|
self::$wasDatabaseAllowed = true;
|
2015-11-03 02:23:05 +03:00
|
|
|
if (!$this->IsDatabaseAccessAllowed()) {
|
2015-12-21 20:08:08 +03:00
|
|
|
self::$wasDatabaseAllowed = false;
|
2015-11-03 02:23:05 +03:00
|
|
|
if (is_null(self::$realDatabase)) {
|
|
|
|
self::$realDatabase = \OC::$server->getDatabaseConnection();
|
|
|
|
}
|
2017-03-20 16:53:57 +03:00
|
|
|
\OC::$server->registerService(IDBConnection::class, function () {
|
2015-11-03 02:23:05 +03:00
|
|
|
$this->fail('Your test case is not allowed to access the database.');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:38:51 +03: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 17:25:31 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function onNotSuccessfulTest(\Throwable $t): void {
|
2016-10-20 16:15:54 +03:00
|
|
|
$this->restoreAllServices();
|
|
|
|
|
|
|
|
// restore database connection
|
|
|
|
if (!$this->IsDatabaseAccessAllowed()) {
|
2017-03-20 16:53:57 +03:00
|
|
|
\OC::$server->registerService(IDBConnection::class, function () {
|
2016-10-20 16:15:54 +03:00
|
|
|
return self::$realDatabase;
|
|
|
|
});
|
|
|
|
}
|
2017-03-20 15:38:52 +03:00
|
|
|
|
2018-01-24 19:23:59 +03:00
|
|
|
parent::onNotSuccessfulTest($t);
|
2016-10-20 16:15:54 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2016-10-20 16:15:54 +03:00
|
|
|
$this->restoreAllServices();
|
|
|
|
|
2015-11-03 02:23:05 +03:00
|
|
|
// restore database connection
|
|
|
|
if (!$this->IsDatabaseAccessAllowed()) {
|
2017-03-20 16:53:57 +03:00
|
|
|
\OC::$server->registerService(IDBConnection::class, function () {
|
2015-11-03 02:23:05 +03:00
|
|
|
return self::$realDatabase;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// further cleanup
|
2015-03-24 12:19:30 +03:00
|
|
|
$hookExceptions = \OC_Hook::$thrownExceptions;
|
|
|
|
\OC_Hook::$thrownExceptions = [];
|
2015-05-20 14:08:56 +03:00
|
|
|
\OC::$server->getLockingProvider()->releaseAll();
|
2015-08-20 15:38:51 +03:00
|
|
|
if (!empty($hookExceptions)) {
|
2015-03-24 12:19:30 +03:00
|
|
|
throw $hookExceptions[0];
|
|
|
|
}
|
2015-08-20 15:38:51 +03:00
|
|
|
|
2015-12-17 19:15:04 +03:00
|
|
|
// fail hard if xml errors have not been cleaned up
|
|
|
|
$errors = libxml_get_errors();
|
|
|
|
libxml_clear_errors();
|
2016-12-06 15:51:57 +03:00
|
|
|
if (!empty($errors)) {
|
|
|
|
self::assertEquals([], $errors, "There have been xml parsing errors");
|
|
|
|
}
|
2015-12-17 19:15:04 +03:00
|
|
|
|
2017-08-24 17:06:37 +03:00
|
|
|
if ($this->IsDatabaseAccessAllowed()) {
|
|
|
|
\OC\Files\Cache\Storage::getGlobalCache()->clearCache();
|
|
|
|
}
|
2016-09-02 16:41:59 +03:00
|
|
|
|
2015-12-17 19:15:04 +03:00
|
|
|
// tearDown the traits
|
2015-08-20 15:38:51 +03: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 12:19:30 +03:00
|
|
|
}
|
|
|
|
|
2015-06-03 13:03:02 +03:00
|
|
|
/**
|
|
|
|
* Allows us to test private methods/properties
|
|
|
|
*
|
|
|
|
* @param $object
|
|
|
|
* @param $methodName
|
|
|
|
* @param array $parameters
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
protected static function invokePrivate($object, $methodName, array $parameters = []) {
|
2016-06-01 14:59:37 +03:00
|
|
|
if (is_string($object)) {
|
|
|
|
$className = $object;
|
|
|
|
} else {
|
|
|
|
$className = get_class($object);
|
|
|
|
}
|
|
|
|
$reflection = new \ReflectionClass($className);
|
2015-06-03 13:03:02 +03:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-12-03 18:18:01 +03:00
|
|
|
if (is_object($object)) {
|
|
|
|
return $property->getValue($object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $property->getValue();
|
2015-06-03 13:03:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-01-11 21:59:15 +03:00
|
|
|
return $prefix . \OC::$server->getSecureRandom()->generate(
|
2014-11-05 15:20:43 +03:00
|
|
|
$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
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
public static function tearDownAfterClass(): void {
|
2015-12-21 20:08:08 +03: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
|
2017-03-20 16:53:57 +03:00
|
|
|
\OC::$server->registerService(IDBConnection::class, function () {
|
2015-12-21 20:08:08 +03:00
|
|
|
return self::$realDatabase;
|
|
|
|
});
|
|
|
|
}
|
2014-11-19 16:40:15 +03:00
|
|
|
$dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');
|
2015-12-21 20:08:08 +03:00
|
|
|
if (self::$wasDatabaseAllowed && \OC::$server->getDatabaseConnection()) {
|
2021-01-08 13:45:02 +03:00
|
|
|
$db = \OC::$server->getDatabaseConnection();
|
|
|
|
if ($db->inTransaction()) {
|
|
|
|
$db->rollBack();
|
|
|
|
throw new \Exception('There was a transaction still in progress and needed to be rolled back. Please fix this in your test.');
|
|
|
|
}
|
|
|
|
$queryBuilder = $db->getQueryBuilder();
|
2014-11-19 16:40:15 +03:00
|
|
|
|
2015-12-17 19:29:50 +03:00
|
|
|
self::tearDownAfterClassCleanShares($queryBuilder);
|
|
|
|
self::tearDownAfterClassCleanStorages($queryBuilder);
|
|
|
|
self::tearDownAfterClassCleanFileCache($queryBuilder);
|
|
|
|
}
|
2014-11-19 16:40:15 +03:00
|
|
|
self::tearDownAfterClassCleanStrayDataFiles($dataDir);
|
|
|
|
self::tearDownAfterClassCleanStrayHooks();
|
2015-06-22 13:10:06 +03:00
|
|
|
self::tearDownAfterClassCleanStrayLocks();
|
2014-11-19 16:40:15 +03:00
|
|
|
|
|
|
|
parent::tearDownAfterClass();
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:13:45 +03:00
|
|
|
/**
|
|
|
|
* Remove all entries from the share table
|
|
|
|
*
|
|
|
|
* @param IQueryBuilder $queryBuilder
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function tearDownAfterClassCleanShares(IQueryBuilder $queryBuilder) {
|
2015-10-05 11:13:45 +03:00
|
|
|
$queryBuilder->delete('share')
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
2014-11-19 16:40:15 +03:00
|
|
|
/**
|
|
|
|
* Remove all entries from the storages table
|
2015-03-02 17:25:31 +03:00
|
|
|
*
|
2015-10-05 11:13:45 +03:00
|
|
|
* @param IQueryBuilder $queryBuilder
|
2014-11-19 16:40:15 +03:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function tearDownAfterClassCleanStorages(IQueryBuilder $queryBuilder) {
|
2015-10-05 11:13:45 +03:00
|
|
|
$queryBuilder->delete('storages')
|
|
|
|
->execute();
|
2014-11-19 16:40:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all entries from the filecache table
|
2015-03-02 17:25:31 +03:00
|
|
|
*
|
2015-10-05 11:13:45 +03:00
|
|
|
* @param IQueryBuilder $queryBuilder
|
2014-11-19 16:40:15 +03:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function tearDownAfterClassCleanFileCache(IQueryBuilder $queryBuilder) {
|
2015-10-05 11:13:45 +03:00
|
|
|
$queryBuilder->delete('filecache')
|
|
|
|
->execute();
|
2014-11-19 16:40:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all unused files from the data dir
|
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function tearDownAfterClassCleanStrayDataFiles($dataDir) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$knownEntries = [
|
2016-07-04 12:50:32 +03:00
|
|
|
'nextcloud.log' => true,
|
2018-06-29 12:03:13 +03:00
|
|
|
'audit.log' => true,
|
2015-03-02 17:25:31 +03:00
|
|
|
'owncloud.db' => true,
|
|
|
|
'.ocdata' => true,
|
|
|
|
'..' => true,
|
|
|
|
'.' => true,
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
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
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function tearDownAfterClassCleanStrayDataUnlinkDir($dir) {
|
2014-11-19 16:40:15 +03:00
|
|
|
if ($dh = @opendir($dir)) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
2015-09-21 15:09:28 +03:00
|
|
|
if (\OC\Files\Filesystem::isIgnoredDir($file)) {
|
2014-11-19 16:40:15 +03:00
|
|
|
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
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function tearDownAfterClassCleanStrayHooks() {
|
2014-11-19 16:40:15 +03:00
|
|
|
\OC_Hook::clear();
|
|
|
|
}
|
|
|
|
|
2015-06-22 13:10:06 +03:00
|
|
|
/**
|
|
|
|
* Clean up the list of locks
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function tearDownAfterClassCleanStrayLocks() {
|
2015-06-22 13:10:06 +03:00
|
|
|
\OC::$server->getLockingProvider()->releaseAll();
|
|
|
|
}
|
|
|
|
|
2014-12-04 16:01:23 +03:00
|
|
|
/**
|
|
|
|
* Login and setup FS as a given user,
|
|
|
|
* sets the given user as the current user.
|
|
|
|
*
|
2015-04-08 13:03:55 +03:00
|
|
|
* @param string $user user id or empty for a generic FS
|
2014-12-04 16:01:23 +03:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function loginAsUser($user = '') {
|
2014-12-04 16:01:23 +03:00
|
|
|
self::logout();
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
\OC_User::setUserId($user);
|
2016-09-23 18:21:07 +03:00
|
|
|
$userObject = \OC::$server->getUserManager()->get($user);
|
|
|
|
if (!is_null($userObject)) {
|
|
|
|
$userObject->updateLastLoginTimestamp();
|
|
|
|
}
|
2014-12-04 16:01:23 +03:00
|
|
|
\OC_Util::setupFS($user);
|
2018-01-16 15:27:45 +03:00
|
|
|
if (\OC::$server->getUserManager()->userExists($user)) {
|
2015-05-11 16:55:23 +03:00
|
|
|
\OC::$server->getUserFolder($user);
|
|
|
|
}
|
2014-12-04 16:01:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout the current user and tear down the filesystem.
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
protected static function logout() {
|
2014-12-04 16:01:23 +03:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
2015-05-15 13:29:47 +03:00
|
|
|
// needed for fully logout
|
|
|
|
\OC::$server->getUserSession()->setUser(null);
|
2014-12-04 16:01:23 +03:00
|
|
|
}
|
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) {
|
2021-01-12 12:15:48 +03:00
|
|
|
[, $user] = explode('/', $view->getRoot());
|
2015-03-24 12:02:48 +03:00
|
|
|
} else {
|
|
|
|
$user = null;
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:14:52 +03:00
|
|
|
\OC_Util::tearDownFS(); // command can't 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
|
|
|
}
|
2015-06-12 19:50:49 +03: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 14:54:00 +03:00
|
|
|
* @param bool $onMountPoint true to check the mount point instead of the
|
|
|
|
* mounted storage
|
2015-06-12 19:50:49 +03:00
|
|
|
*
|
|
|
|
* @return boolean true if the file is locked with the
|
|
|
|
* given type, false otherwise
|
|
|
|
*/
|
2015-06-19 14:54:00 +03:00
|
|
|
protected function isFileLocked($view, $path, $type, $onMountPoint = false) {
|
2015-06-12 19:50:49 +03: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 14:54:00 +03:00
|
|
|
$view->lockFile($path, $checkType, $onMountPoint);
|
2015-06-12 19:50:49 +03:00
|
|
|
// no exception, which means the lock of $type is not set
|
|
|
|
// clean up
|
2015-06-19 14:54:00 +03:00
|
|
|
$view->unlockFile($path, $checkType, $onMountPoint);
|
2015-06-12 19:50:49 +03: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-03 02:23:05 +03:00
|
|
|
|
2021-03-29 10:41:32 +03:00
|
|
|
protected function getGroupAnnotations(): array {
|
|
|
|
if (method_exists($this, 'getAnnotations')) {
|
|
|
|
$annotations = $this->getAnnotations();
|
|
|
|
return $annotations['class']['group'] ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = new \ReflectionClass($this);
|
|
|
|
$doc = $r->getDocComment();
|
|
|
|
preg_match_all('#@group\s+(.*?)\n#s', $doc, $annotations);
|
|
|
|
return $annotations[1] ?? [];
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:58:04 +03:00
|
|
|
protected function IsDatabaseAccessAllowed() {
|
2015-11-30 12:52:52 +03: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;
|
|
|
|
}
|
2021-03-29 10:41:32 +03:00
|
|
|
$annotations = $this->getGroupAnnotations();
|
|
|
|
if (isset($annotations)) {
|
|
|
|
if (in_array('DB', $annotations) || in_array('SLOWDB', $annotations)) {
|
2016-09-29 01:53:53 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-03 02:23:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-12 13:49:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $expectedHtml
|
|
|
|
* @param string $template
|
|
|
|
* @param array $vars
|
|
|
|
*/
|
|
|
|
protected function assertTemplate($expectedHtml, $template, $vars = []) {
|
2016-05-12 14:32:02 +03:00
|
|
|
require_once __DIR__.'/../../lib/private/legacy/template/functions.php';
|
2016-04-12 13:49:11 +03:00
|
|
|
|
|
|
|
$requestToken = 12345;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Defaults|\PHPUnit\Framework\MockObject\MockObject $l10n */
|
2017-04-07 23:42:43 +03:00
|
|
|
$theme = $this->getMockBuilder('\OCP\Defaults')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$theme->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn('Nextcloud');
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l10n */
|
2017-10-24 16:26:53 +03:00
|
|
|
$l10n = $this->getMockBuilder(IL10N::class)
|
2016-04-12 13:49:11 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$l10n
|
|
|
|
->expects($this->any())
|
|
|
|
->method('t')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($text, $parameters = []) {
|
2016-04-12 13:49:11 +03:00
|
|
|
return vsprintf($text, $parameters);
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-04-12 13:49:11 +03:00
|
|
|
|
|
|
|
$t = new Base($template, $requestToken, $l10n, $theme);
|
|
|
|
$buf = $t->fetchPage($vars);
|
|
|
|
$this->assertHtmlStringEqualsHtmlString($expectedHtml, $buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $expectedHtml
|
|
|
|
* @param string $actualHtml
|
|
|
|
* @param string $message
|
|
|
|
*/
|
|
|
|
protected function assertHtmlStringEqualsHtmlString($expectedHtml, $actualHtml, $message = '') {
|
|
|
|
$expected = new DOMDocument();
|
|
|
|
$expected->preserveWhiteSpace = false;
|
|
|
|
$expected->formatOutput = true;
|
|
|
|
$expected->loadHTML($expectedHtml);
|
|
|
|
|
|
|
|
$actual = new DOMDocument();
|
|
|
|
$actual->preserveWhiteSpace = false;
|
|
|
|
$actual->formatOutput = true;
|
|
|
|
$actual->loadHTML($actualHtml);
|
|
|
|
$this->removeWhitespaces($actual);
|
|
|
|
|
|
|
|
$expectedHtml1 = $expected->saveHTML();
|
|
|
|
$actualHtml1 = $actual->saveHTML();
|
|
|
|
self::assertEquals($expectedHtml1, $actualHtml1, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function removeWhitespaces(DOMNode $domNode) {
|
|
|
|
foreach ($domNode->childNodes as $node) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($node->hasChildNodes()) {
|
2016-04-12 13:49:11 +03:00
|
|
|
$this->removeWhitespaces($node);
|
|
|
|
} else {
|
2020-04-09 17:07:47 +03:00
|
|
|
if ($node instanceof \DOMText && $node->isWhitespaceInElementContent()) {
|
2016-04-12 13:49:11 +03:00
|
|
|
$domNode->removeChild($node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-05 15:20:43 +03:00
|
|
|
}
|