diff options
Diffstat (limited to 'apps/files_trashbin/tests/TrashbinTest.php')
-rw-r--r-- | apps/files_trashbin/tests/TrashbinTest.php | 302 |
1 files changed, 153 insertions, 149 deletions
diff --git a/apps/files_trashbin/tests/TrashbinTest.php b/apps/files_trashbin/tests/TrashbinTest.php index 6474021b92e..6104a242104 100644 --- a/apps/files_trashbin/tests/TrashbinTest.php +++ b/apps/files_trashbin/tests/TrashbinTest.php @@ -1,13 +1,34 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-License-Identifier: AGPL-3.0-only */ +use OC\AllConfig; use OC\AppFramework\Bootstrap\BootContext; use OC\AppFramework\DependencyInjection\DIContainer; +use OC\Files\Cache\Watcher; +use OC\Files\Filesystem; +use OC\Files\Storage\Local; +use OC\Files\View; +use OC\SystemConfig; +use OC\User\Database; use OCA\Files_Sharing\AppInfo\Application; use OCA\Files_Trashbin\AppInfo\Application as TrashbinApplication; +use OCA\Files_Trashbin\Expiration; +use OCA\Files_Trashbin\Helper; +use OCA\Files_Trashbin\Trashbin; +use OCP\App\IAppManager; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Constants; +use OCP\Files\FileInfo; +use OCP\Files\IRootFolder; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\IUserManager; +use OCP\Server; use OCP\Share\IShare; /** @@ -16,49 +37,41 @@ use OCP\Share\IShare; * @group DB */ class TrashbinTest extends \Test\TestCase { - public const TEST_TRASHBIN_USER1 = "test-trashbin-user1"; - public const TEST_TRASHBIN_USER2 = "test-trashbin-user2"; + public const TEST_TRASHBIN_USER1 = 'test-trashbin-user1'; + public const TEST_TRASHBIN_USER2 = 'test-trashbin-user2'; private $trashRoot1; private $trashRoot2; private static $rememberRetentionObligation; - - /** - * @var bool - */ - private static $trashBinStatus; - - /** - * @var \OC\Files\View - */ - private $rootView; + private static bool $trashBinStatus; + private View $rootView; public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - $appManager = \OC::$server->getAppManager(); + $appManager = Server::get(IAppManager::class); self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin'); // reset backend - \OC_User::clearBackends(); - \OC_User::useBackend('database'); + Server::get(IUserManager::class)->clearBackends(); + Server::get(IUserManager::class)->registerBackend(new Database()); // clear share hooks \OC_Hook::clear('OCP\\Share'); - \OC::registerShareHooks(\OC::$server->getSystemConfig()); + \OC::registerShareHooks(Server::get(SystemConfig::class)); // init files sharing new Application(); //disable encryption - \OC::$server->getAppManager()->disableApp('encryption'); + Server::get(IAppManager::class)->disableApp('encryption'); - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); //configure trashbin - self::$rememberRetentionObligation = $config->getSystemValue('trashbin_retention_obligation', \OCA\Files_Trashbin\Expiration::DEFAULT_RETENTION_OBLIGATION); - /** @var \OCA\Files_Trashbin\Expiration $expiration */ - $expiration = \OC::$server->query(\OCA\Files_Trashbin\Expiration::class); + self::$rememberRetentionObligation = (string)$config->getSystemValue('trashbin_retention_obligation', Expiration::DEFAULT_RETENTION_OBLIGATION); + /** @var Expiration $expiration */ + $expiration = Server::get(Expiration::class); $expiration->setRetentionObligation('auto, 2'); // register trashbin hooks @@ -73,21 +86,21 @@ class TrashbinTest extends \Test\TestCase { public static function tearDownAfterClass(): void { // cleanup test user - $user = \OC::$server->getUserManager()->get(self::TEST_TRASHBIN_USER1); + $user = Server::get(IUserManager::class)->get(self::TEST_TRASHBIN_USER1); if ($user !== null) { $user->delete(); } - /** @var \OCA\Files_Trashbin\Expiration $expiration */ - $expiration = \OC::$server->query(\OCA\Files_Trashbin\Expiration::class); + /** @var Expiration $expiration */ + $expiration = Server::get(Expiration::class); $expiration->setRetentionObligation(self::$rememberRetentionObligation); \OC_Hook::clear(); - \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); + Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); if (self::$trashBinStatus) { - \OC::$server->getAppManager()->enableApp('files_trashbin'); + Server::get(IAppManager::class)->enableApp('files_trashbin'); } parent::tearDownAfterClass(); @@ -96,40 +109,33 @@ class TrashbinTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); - \OC::$server->getAppManager()->enableApp('files_trashbin'); - $config = \OC::$server->getConfig(); - $mockConfig = $this->createMock(\OCP\IConfig::class); - $mockConfig + Server::get(IAppManager::class)->enableApp('files_trashbin'); + $config = Server::get(IConfig::class); + $mockConfig = $this->getMockBuilder(AllConfig::class) + ->onlyMethods(['getSystemValue']) + ->setConstructorArgs([Server::get(SystemConfig::class)]) + ->getMock(); + $mockConfig->expects($this->any()) ->method('getSystemValue') ->willReturnCallback(static function ($key, $default) use ($config) { if ($key === 'filesystem_check_changes') { - return \OC\Files\Cache\Watcher::CHECK_ONCE; + return Watcher::CHECK_ONCE; } else { return $config->getSystemValue($key, $default); } }); - $mockConfig - ->method('getUserValue') - ->willReturnCallback(static function ($userId, $appName, $key, $default = '') use ($config) { - return $config->getUserValue($userId, $appName, $key, $default); - }); - $mockConfig - ->method('getAppValue') - ->willReturnCallback(static function ($appName, $key, $default = '') use ($config) { - return $config->getAppValue($appName, $key, $default); - }); - $this->overwriteService(\OC\AllConfig::class, $mockConfig); + $this->overwriteService(AllConfig::class, $mockConfig); $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin'; $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin'; - $this->rootView = new \OC\Files\View(); + $this->rootView = new View(); self::loginHelper(self::TEST_TRASHBIN_USER1); } protected function tearDown(): void { - $this->restoreService(\OC\AllConfig::class); + $this->restoreService(AllConfig::class); // disable trashbin to be able to properly clean up - \OC::$server->getAppManager()->disableApp('files_trashbin'); + Server::get(IAppManager::class)->disableApp('files_trashbin'); $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER1 . '/files'); $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER2 . '/files'); @@ -137,7 +143,7 @@ class TrashbinTest extends \Test\TestCase { $this->rootView->deleteAll($this->trashRoot2); // clear trash table - $connection = \OC::$server->getDatabaseConnection(); + $connection = Server::get(IDBConnection::class); $connection->executeUpdate('DELETE FROM `*PREFIX*files_trash`'); parent::tearDown(); @@ -146,26 +152,26 @@ class TrashbinTest extends \Test\TestCase { /** * test expiration of files older then the max storage time defined for the trash */ - public function testExpireOldFiles() { + public function testExpireOldFiles(): void { - /** @var \OCP\AppFramework\Utility\ITimeFactory $time */ - $time = \OC::$server->query(\OCP\AppFramework\Utility\ITimeFactory::class); + /** @var ITimeFactory $time */ + $time = Server::get(ITimeFactory::class); $currentTime = $time->getTime(); $expireAt = $currentTime - 2 * 24 * 60 * 60; $expiredDate = $currentTime - 3 * 24 * 60 * 60; // create some files - \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1'); - \OC\Files\Filesystem::file_put_contents('file2.txt', 'file2'); - \OC\Files\Filesystem::file_put_contents('file3.txt', 'file3'); + Filesystem::file_put_contents('file1.txt', 'file1'); + Filesystem::file_put_contents('file2.txt', 'file2'); + Filesystem::file_put_contents('file3.txt', 'file3'); // delete them so that they end up in the trash bin - \OC\Files\Filesystem::unlink('file1.txt'); - \OC\Files\Filesystem::unlink('file2.txt'); - \OC\Files\Filesystem::unlink('file3.txt'); + Filesystem::unlink('file1.txt'); + Filesystem::unlink('file2.txt'); + Filesystem::unlink('file3.txt'); //make sure that files are in the trash bin - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); $this->assertSame(3, count($filesInTrash)); // every second file will get a date in the past so that it will get expired @@ -179,14 +185,14 @@ class TrashbinTest extends \Test\TestCase { // only file2.txt should be left $remainingFiles = array_slice($manipulatedList, $count); - $this->assertSame(1, count($remainingFiles)); + $this->assertCount(1, $remainingFiles); $remainingFile = reset($remainingFiles); // TODO: failing test #$this->assertSame('file2.txt', $remainingFile['name']); // check that file1.txt and file3.txt was really deleted - $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); - $this->assertSame(1, count($newTrashContent)); + $newTrashContent = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); + $this->assertCount(1, $newTrashContent); $element = reset($newTrashContent); // TODO: failing test #$this->assertSame('file2.txt', $element['name']); @@ -198,35 +204,35 @@ class TrashbinTest extends \Test\TestCase { * the owner of the file and the one from the user who deleted the file get expired * correctly */ - public function testExpireOldFilesShared() { + public function testExpireOldFilesShared(): void { $currentTime = time(); - $folder = "trashTest-" . $currentTime . '/'; + $folder = 'trashTest-' . $currentTime . '/'; $expiredDate = $currentTime - 3 * 24 * 60 * 60; // create some files - \OC\Files\Filesystem::mkdir($folder); - \OC\Files\Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1'); - \OC\Files\Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2'); - \OC\Files\Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3'); - \OC\Files\Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4'); + Filesystem::mkdir($folder); + Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1'); + Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2'); + Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3'); + Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4'); //share user1-4.txt with user2 $node = \OC::$server->getUserFolder(self::TEST_TRASHBIN_USER1)->get($folder); - $share = \OC::$server->getShareManager()->newShare(); + $share = Server::get(\OCP\Share\IManager::class)->newShare(); $share->setShareType(IShare::TYPE_USER) ->setNode($node) ->setSharedBy(self::TEST_TRASHBIN_USER1) ->setSharedWith(self::TEST_TRASHBIN_USER2) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $share = \OC::$server->getShareManager()->createShare($share); - \OC::$server->getShareManager()->acceptShare($share, self::TEST_TRASHBIN_USER2); + ->setPermissions(Constants::PERMISSION_ALL); + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_TRASHBIN_USER2); // delete them so that they end up in the trash bin - \OC\Files\Filesystem::unlink($folder . 'user1-1.txt'); - \OC\Files\Filesystem::unlink($folder . 'user1-2.txt'); - \OC\Files\Filesystem::unlink($folder . 'user1-3.txt'); + Filesystem::unlink($folder . 'user1-1.txt'); + Filesystem::unlink($folder . 'user1-2.txt'); + Filesystem::unlink($folder . 'user1-3.txt'); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); $this->assertSame(3, count($filesInTrash)); // every second file will get a date in the past so that it will get expired @@ -235,27 +241,27 @@ class TrashbinTest extends \Test\TestCase { // login as user2 self::loginHelper(self::TEST_TRASHBIN_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists($folder . "user1-4.txt")); + $this->assertTrue(Filesystem::file_exists($folder . 'user1-4.txt')); // create some files - \OC\Files\Filesystem::file_put_contents('user2-1.txt', 'file1'); - \OC\Files\Filesystem::file_put_contents('user2-2.txt', 'file2'); + Filesystem::file_put_contents('user2-1.txt', 'file1'); + Filesystem::file_put_contents('user2-2.txt', 'file2'); // delete them so that they end up in the trash bin - \OC\Files\Filesystem::unlink('user2-1.txt'); - \OC\Files\Filesystem::unlink('user2-2.txt'); + Filesystem::unlink('user2-1.txt'); + Filesystem::unlink('user2-2.txt'); - $filesInTrashUser2 = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2, 'name'); + $filesInTrashUser2 = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2, 'name'); $this->assertSame(2, count($filesInTrashUser2)); // every second file will get a date in the past so that it will get expired $this->manipulateDeleteTime($filesInTrashUser2, $this->trashRoot2, $expiredDate); - \OC\Files\Filesystem::unlink($folder . 'user1-4.txt'); + Filesystem::unlink($folder . 'user1-4.txt'); $this->runCommands(); - $filesInTrashUser2AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2); + $filesInTrashUser2AfterDelete = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2); // user2-1.txt should have been expired $this->verifyArray($filesInTrashUser2AfterDelete, ['user2-2.txt', 'user1-4.txt']); @@ -263,7 +269,7 @@ class TrashbinTest extends \Test\TestCase { self::loginHelper(self::TEST_TRASHBIN_USER1); // user1-1.txt and user1-3.txt should have been expired - $filesInTrashUser1AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); + $filesInTrashUser1AfterDelete = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); $this->verifyArray($filesInTrashUser1AfterDelete, ['user1-2.txt', 'user1-4.txt']); } @@ -271,11 +277,11 @@ class TrashbinTest extends \Test\TestCase { /** * verify that the array contains the expected results * - * @param OCP\Files\FileInfo[] $result + * @param FileInfo[] $result * @param string[] $expected */ - private function verifyArray($result, $expected) { - $this->assertSame(count($expected), count($result)); + private function verifyArray(array $result, array $expected): void { + $this->assertCount(count($expected), $result); foreach ($expected as $expectedFile) { $found = false; foreach ($result as $fileInTrash) { @@ -292,18 +298,16 @@ class TrashbinTest extends \Test\TestCase { } /** - * @param OCP\Files\FileInfo[] $files - * @param string $trashRoot - * @param integer $expireDate + * @param FileInfo[] $files */ - private function manipulateDeleteTime($files, $trashRoot, $expireDate) { + private function manipulateDeleteTime(array $files, string $trashRoot, int $expireDate): array { $counter = 0; foreach ($files as &$file) { // modify every second file $counter = ($counter + 1) % 2; if ($counter === 1) { $source = $trashRoot . '/files/' . $file['name'] . '.d' . $file['mtime']; - $target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); + $target = Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); $this->rootView->rename($source, $target); $file['mtime'] = $expireDate; } @@ -316,22 +320,22 @@ class TrashbinTest extends \Test\TestCase { * test expiration of old files in the trash bin until the max size * of the trash bin is met again */ - public function testExpireOldFilesUtilLimitsAreMet() { + public function testExpireOldFilesUtilLimitsAreMet(): void { // create some files - \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1'); - \OC\Files\Filesystem::file_put_contents('file2.txt', 'file2'); - \OC\Files\Filesystem::file_put_contents('file3.txt', 'file3'); + Filesystem::file_put_contents('file1.txt', 'file1'); + Filesystem::file_put_contents('file2.txt', 'file2'); + Filesystem::file_put_contents('file3.txt', 'file3'); // delete them so that they end up in the trash bin - \OC\Files\Filesystem::unlink('file3.txt'); + Filesystem::unlink('file3.txt'); sleep(1); // make sure that every file has a unique mtime - \OC\Files\Filesystem::unlink('file2.txt'); + Filesystem::unlink('file2.txt'); sleep(1); // make sure that every file has a unique mtime - \OC\Files\Filesystem::unlink('file1.txt'); + Filesystem::unlink('file1.txt'); //make sure that files are in the trash bin - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertSame(3, count($filesInTrash)); $testClass = new TrashbinForTesting(); @@ -340,7 +344,7 @@ class TrashbinTest extends \Test\TestCase { // the two oldest files (file3.txt and file2.txt) should be deleted $this->assertSame(10, $sizeOfDeletedFiles); - $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); + $newTrashContent = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); $this->assertSame(1, count($newTrashContent)); $element = reset($newTrashContent); $this->assertSame('file1.txt', $element['name']); @@ -349,8 +353,8 @@ class TrashbinTest extends \Test\TestCase { /** * Test restoring a file */ - public function testRestoreFileInRoot() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFileInRoot(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $file = $userFolder->newFile('file1.txt'); $file->putContent('foo'); @@ -360,14 +364,14 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('file1.txt')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFile = $filesInTrash[0]; $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'file1.txt.d' . $trashedFile->getMtime(), $trashedFile->getName(), $trashedFile->getMtime() @@ -381,8 +385,8 @@ class TrashbinTest extends \Test\TestCase { /** * Test restoring a file in subfolder */ - public function testRestoreFileInSubfolder() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFileInSubfolder(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $folder = $userFolder->newFolder('folder'); $file = $folder->newFile('file1.txt'); $file->putContent('foo'); @@ -393,14 +397,14 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFile = $filesInTrash[0]; $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'file1.txt.d' . $trashedFile->getMtime(), $trashedFile->getName(), $trashedFile->getMtime() @@ -414,8 +418,8 @@ class TrashbinTest extends \Test\TestCase { /** * Test restoring a folder */ - public function testRestoreFolder() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFolder(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $folder = $userFolder->newFolder('folder'); $file = $folder->newFile('file1.txt'); $file->putContent('foo'); @@ -426,14 +430,14 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('folder')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFolder = $filesInTrash[0]; $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'folder.d' . $trashedFolder->getMtime(), $trashedFolder->getName(), $trashedFolder->getMtime() @@ -447,8 +451,8 @@ class TrashbinTest extends \Test\TestCase { /** * Test restoring a file from inside a trashed folder */ - public function testRestoreFileFromTrashedSubfolder() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFileFromTrashedSubfolder(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $folder = $userFolder->newFolder('folder'); $file = $folder->newFile('file1.txt'); $file->putContent('foo'); @@ -459,14 +463,14 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('folder')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFile = $filesInTrash[0]; $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'folder.d' . $trashedFile->getMtime() . '/file1.txt', 'file1.txt', $trashedFile->getMtime() @@ -481,8 +485,8 @@ class TrashbinTest extends \Test\TestCase { * Test restoring a file whenever the source folder was removed. * The file should then land in the root. */ - public function testRestoreFileWithMissingSourceFolder() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFileWithMissingSourceFolder(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $folder = $userFolder->newFolder('folder'); $file = $folder->newFile('file1.txt'); $file->putContent('foo'); @@ -493,17 +497,17 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFile = $filesInTrash[0]; // delete source folder $folder->delete(); $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'file1.txt.d' . $trashedFile->getMtime(), $trashedFile->getName(), $trashedFile->getMtime() @@ -518,8 +522,8 @@ class TrashbinTest extends \Test\TestCase { * Test restoring a file in the root folder whenever there is another file * with the same name in the root folder */ - public function testRestoreFileDoesNotOverwriteExistingInRoot() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFileDoesNotOverwriteExistingInRoot(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $file = $userFolder->newFile('file1.txt'); $file->putContent('foo'); @@ -529,10 +533,10 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('file1.txt')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFile = $filesInTrash[0]; // create another file @@ -540,7 +544,7 @@ class TrashbinTest extends \Test\TestCase { $file->putContent('bar'); $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'file1.txt.d' . $trashedFile->getMtime(), $trashedFile->getName(), $trashedFile->getMtime() @@ -558,8 +562,8 @@ class TrashbinTest extends \Test\TestCase { * Test restoring a file whenever there is another file * with the same name in the source folder */ - public function testRestoreFileDoesNotOverwriteExistingInSubfolder() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFileDoesNotOverwriteExistingInSubfolder(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $folder = $userFolder->newFolder('folder'); $file = $folder->newFile('file1.txt'); $file->putContent('foo'); @@ -570,10 +574,10 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFile = $filesInTrash[0]; // create another file @@ -581,7 +585,7 @@ class TrashbinTest extends \Test\TestCase { $file->putContent('bar'); $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'file1.txt.d' . $trashedFile->getMtime(), $trashedFile->getName(), $trashedFile->getMtime() @@ -598,9 +602,9 @@ class TrashbinTest extends \Test\TestCase { /** * Test restoring a non-existing file from trashbin, returns false */ - public function testRestoreUnexistingFile() { + public function testRestoreUnexistingFile(): void { $this->assertFalse( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'unexist.txt.d123456', 'unexist.txt', '123456' @@ -612,8 +616,8 @@ class TrashbinTest extends \Test\TestCase { * Test restoring a file into a read-only folder, will restore * the file to root instead */ - public function testRestoreFileIntoReadOnlySourceFolder() { - $userFolder = \OC::$server->getUserFolder(); + public function testRestoreFileIntoReadOnlySourceFolder(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); $folder = $userFolder->newFolder('folder'); $file = $folder->newFile('file1.txt'); $file->putContent('foo'); @@ -624,21 +628,21 @@ class TrashbinTest extends \Test\TestCase { $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertCount(1, $filesInTrash); - /** @var \OCP\Files\FileInfo */ + /** @var FileInfo */ $trashedFile = $filesInTrash[0]; // delete source folder [$storage, $internalPath] = $this->rootView->resolvePath('/' . self::TEST_TRASHBIN_USER1 . '/files/folder'); - if ($storage instanceof \OC\Files\Storage\Local) { + if ($storage instanceof Local) { $folderAbsPath = $storage->getSourcePath($internalPath); // make folder read-only chmod($folderAbsPath, 0555); $this->assertTrue( - OCA\Files_Trashbin\Trashbin::restore( + Trashbin::restore( 'file1.txt.d' . $trashedFile->getMtime(), $trashedFile->getName(), $trashedFile->getMtime() @@ -659,26 +663,26 @@ class TrashbinTest extends \Test\TestCase { public static function loginHelper($user, $create = false) { if ($create) { try { - \OC::$server->getUserManager()->createUser($user, $user); + Server::get(IUserManager::class)->createUser($user, $user); } catch (\Exception $e) { // catch username is already being used from previous aborted runs } } \OC_Util::tearDownFS(); \OC_User::setUserId(''); - \OC\Files\Filesystem::tearDown(); + Filesystem::tearDown(); \OC_User::setUserId($user); \OC_Util::setupFS($user); - \OC::$server->getUserFolder($user); + Server::get(IRootFolder::class)->getUserFolder($user); } } // just a dummy class to make protected methods available for testing -class TrashbinForTesting extends \OCA\Files_Trashbin\Trashbin { +class TrashbinForTesting extends Trashbin { /** - * @param OCP\Files\FileInfo[] $files + * @param FileInfo[] $files * @param integer $limit */ public function dummyDeleteExpiredFiles($files) { @@ -687,7 +691,7 @@ class TrashbinForTesting extends \OCA\Files_Trashbin\Trashbin { } /** - * @param OCP\Files\FileInfo[] $files + * @param FileInfo[] $files * @param integer $availableSpace */ public function dummyDeleteFiles($files, $availableSpace) { |