diff options
Diffstat (limited to 'tests/lib/TestCase.php')
-rw-r--r-- | tests/lib/TestCase.php | 111 |
1 files changed, 73 insertions, 38 deletions
diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php index 8c97c184c6f..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]; } @@ -192,11 +208,11 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { $errors = libxml_get_errors(); libxml_clear_errors(); if (!empty($errors)) { - self::assertEquals([], $errors, "There have been xml parsing errors"); + self::assertEquals([], $errors, 'There have been xml parsing errors'); } if ($this->IsDatabaseAccessAllowed()) { - \OC\Files\Cache\Storage::getGlobalCache()->clearCache(); + Storage::getGlobalCache()->clearCache(); } // tearDown the traits @@ -264,13 +280,37 @@ 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 ); } + /** + * Filter methods + * + * Returns all methods of the given class, + * that are public or abstract and not in the ignoreMethods list, + * to be able to fill onlyMethods() with an inverted list. + * + * @param string $className + * @param string[] $filterMethods + * @return string[] + */ + public function filterClassMethods(string $className, array $filterMethods): array { + $class = new \ReflectionClass($className); + + $methods = []; + foreach ($class->getMethods() as $method) { + if (($method->isPublic() || $method->isAbstract()) && !in_array($method->getName(), $filterMethods, true)) { + $methods[] = $method->getName(); + } + } + + return $methods; + } + public static function tearDownAfterClass(): void { if (!self::$wasDatabaseAllowed && self::$realDatabase !== null) { // in case an error is thrown in a test, PHPUnit jumps straight to tearDownAfterClass, @@ -279,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.'); @@ -297,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(); @@ -342,6 +383,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { */ protected static function tearDownAfterClassCleanFileCache(IQueryBuilder $queryBuilder) { $queryBuilder->delete('filecache') + ->runAcrossAllShards() ->execute(); } @@ -378,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; @@ -404,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(); } /** @@ -415,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); } } @@ -434,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); } /** @@ -461,26 +503,26 @@ 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 - * mounted storage + * mounted storage * * @return boolean true if the file is locked with the - * given type, false otherwise + * given type, false otherwise */ protected function isFileLocked($view, $path, $type, $onMountPoint = false) { // 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) { + 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); @@ -488,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; @@ -508,11 +550,6 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { } protected function IsDatabaseAccessAllowed() { - // on travis-ci.org we allow database access in any case - otherwise - // this will break all apps right away - if (getenv('TRAVIS') == true) { - return true; - } $annotations = $this->getGroupAnnotations(); if (isset($annotations)) { if (in_array('DB', $annotations) || in_array('SLOWDB', $annotations)) { @@ -529,8 +566,6 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { * @param array $vars */ protected function assertTemplate($expectedHtml, $template, $vars = []) { - require_once __DIR__.'/../../lib/private/legacy/template/functions.php'; - $requestToken = 12345; /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject $l10n */ $theme = $this->getMockBuilder('\OCP\Defaults') |