aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/TestCase.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/TestCase.php')
-rw-r--r--tests/lib/TestCase.php73
1 files changed, 45 insertions, 28 deletions
diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php
index 918edf7150e..cdeefde748c 100644
--- a/tests/lib/TestCase.php
+++ b/tests/lib/TestCase.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -10,22 +11,29 @@ namespace Test;
use DOMDocument;
use DOMNode;
use OC\Command\QueueBus;
+use OC\Files\Cache\Storage;
use OC\Files\Config\MountProviderCollection;
use OC\Files\Filesystem;
use OC\Files\Mount\CacheMountProvider;
use OC\Files\Mount\LocalHomeMountProvider;
use OC\Files\Mount\RootMountProvider;
+use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
use OC\Files\SetupManager;
+use OC\Files\View;
use OC\Template\Base;
+use OCP\AppFramework\QueryException;
use OCP\Command\IBus;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
+use OCP\IUserManager;
+use OCP\IUserSession;
use OCP\Lock\ILockingProvider;
+use OCP\Lock\LockedException;
use OCP\Security\ISecureRandom;
-use Psr\Log\LoggerInterface;
+use OCP\Server;
if (version_compare(\PHPUnit\Runner\Version::id(), 10, '>=')) {
trait OnNotSuccessfulTestTrait {
@@ -84,7 +92,11 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
return false;
}
- $this->services[$name] = \OC::$server->query($name);
+ try {
+ $this->services[$name] = Server::get($name);
+ } catch (QueryException $e) {
+ $this->services[$name] = false;
+ }
$container = \OC::$server->getAppContainerForService($name);
$container = $container ?? \OC::$server;
@@ -106,9 +118,13 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
$container = \OC::$server->getAppContainerForService($name);
$container = $container ?? \OC::$server;
- $container->registerService($name, function () use ($oldService) {
- return $oldService;
- });
+ if ($oldService !== false) {
+ $container->registerService($name, function () use ($oldService) {
+ return $oldService;
+ });
+ } else {
+ unset($container[$oldService]);
+ }
unset($this->services[$name]);
@@ -154,9 +170,9 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
if (!$this->IsDatabaseAccessAllowed()) {
self::$wasDatabaseAllowed = false;
if (is_null(self::$realDatabase)) {
- self::$realDatabase = \OC::$server->getDatabaseConnection();
+ self::$realDatabase = Server::get(IDBConnection::class);
}
- \OC::$server->registerService(IDBConnection::class, function () {
+ \OC::$server->registerService(IDBConnection::class, function (): void {
$this->fail('Your test case is not allowed to access the database.');
});
}
@@ -183,7 +199,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
// further cleanup
$hookExceptions = \OC_Hook::$thrownExceptions;
\OC_Hook::$thrownExceptions = [];
- \OC::$server->get(ILockingProvider::class)->releaseAll();
+ Server::get(ILockingProvider::class)->releaseAll();
if (!empty($hookExceptions)) {
throw $hookExceptions[0];
}
@@ -196,7 +212,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
}
if ($this->IsDatabaseAccessAllowed()) {
- \OC\Files\Cache\Storage::getGlobalCache()->clearCache();
+ Storage::getGlobalCache()->clearCache();
}
// tearDown the traits
@@ -264,7 +280,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
* @return string
*/
protected static function getUniqueID($prefix = '', $length = 13) {
- return $prefix . \OC::$server->get(ISecureRandom::class)->generate(
+ return $prefix . Server::get(ISecureRandom::class)->generate(
$length,
// Do not use dots and slashes as we use the value for file names
ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
@@ -303,9 +319,9 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
return self::$realDatabase;
});
}
- $dataDir = \OC::$server->getConfig()->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data-autotest');
- if (self::$wasDatabaseAllowed && \OC::$server->getDatabaseConnection()) {
- $db = \OC::$server->getDatabaseConnection();
+ $dataDir = Server::get(IConfig::class)->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data-autotest');
+ if (self::$wasDatabaseAllowed && Server::get(IDBConnection::class)) {
+ $db = Server::get(IDBConnection::class);
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.');
@@ -321,18 +337,19 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
self::tearDownAfterClassCleanStrayLocks();
/** @var SetupManager $setupManager */
- $setupManager = \OC::$server->get(SetupManager::class);
+ $setupManager = Server::get(SetupManager::class);
$setupManager->tearDown();
/** @var MountProviderCollection $mountProviderCollection */
- $mountProviderCollection = \OC::$server->get(MountProviderCollection::class);
+ $mountProviderCollection = Server::get(MountProviderCollection::class);
$mountProviderCollection->clearProviders();
/** @var IConfig $config */
- $config = \OC::$server->get(IConfig::class);
+ $config = Server::get(IConfig::class);
$mountProviderCollection->registerProvider(new CacheMountProvider($config));
$mountProviderCollection->registerHomeProvider(new LocalHomeMountProvider());
- $mountProviderCollection->registerRootProvider(new RootMountProvider($config, \OC::$server->get(LoggerInterface::class)));
+ $objectStoreConfig = Server::get(PrimaryObjectStoreConfig::class);
+ $mountProviderCollection->registerRootProvider(new RootMountProvider($objectStoreConfig, $config));
$setupManager->setupRoot();
@@ -403,7 +420,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
protected static function tearDownAfterClassCleanStrayDataUnlinkDir($dir) {
if ($dh = @opendir($dir)) {
while (($file = readdir($dh)) !== false) {
- if (\OC\Files\Filesystem::isIgnoredDir($file)) {
+ if (Filesystem::isIgnoredDir($file)) {
continue;
}
$path = $dir . '/' . $file;
@@ -429,7 +446,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
* Clean up the list of locks
*/
protected static function tearDownAfterClassCleanStrayLocks() {
- \OC::$server->get(ILockingProvider::class)->releaseAll();
+ Server::get(ILockingProvider::class)->releaseAll();
}
/**
@@ -440,14 +457,14 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
*/
protected static function loginAsUser($user = '') {
self::logout();
- \OC\Files\Filesystem::tearDown();
+ Filesystem::tearDown();
\OC_User::setUserId($user);
- $userObject = \OC::$server->getUserManager()->get($user);
+ $userObject = Server::get(IUserManager::class)->get($user);
if (!is_null($userObject)) {
$userObject->updateLastLoginTimestamp();
}
\OC_Util::setupFS($user);
- if (\OC::$server->getUserManager()->userExists($user)) {
+ if (Server::get(IUserManager::class)->userExists($user)) {
\OC::$server->getUserFolder($user);
}
}
@@ -459,7 +476,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
\OC_Util::tearDownFS();
\OC_User::setUserId('');
// needed for fully logout
- \OC::$server->getUserSession()->setUser(null);
+ Server::get(IUserSession::class)->setUser(null);
}
/**
@@ -486,7 +503,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
/**
* Check if the given path is locked with a given type
*
- * @param \OC\Files\View $view view
+ * @param View $view view
* @param string $path path to check
* @param int $type lock type
* @param bool $onMountPoint true to check the mount point instead of the
@@ -500,12 +517,12 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
// the format of the lock key depends on the storage implementation
// (in our case mostly md5)
- if ($type === \OCP\Lock\ILockingProvider::LOCK_SHARED) {
+ if ($type === ILockingProvider::LOCK_SHARED) {
// to check if the file has a shared lock, try acquiring an exclusive lock
- $checkType = \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE;
+ $checkType = ILockingProvider::LOCK_EXCLUSIVE;
} else {
// a shared lock cannot be set if exclusive lock is in place
- $checkType = \OCP\Lock\ILockingProvider::LOCK_SHARED;
+ $checkType = ILockingProvider::LOCK_SHARED;
}
try {
$view->lockFile($path, $checkType, $onMountPoint);
@@ -513,7 +530,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
// clean up
$view->unlockFile($path, $checkType, $onMountPoint);
return false;
- } catch (\OCP\Lock\LockedException $e) {
+ } catch (LockedException $e) {
// we could not acquire the counter-lock, which means
// the lock of $type was in place
return true;