diff options
Diffstat (limited to 'tests/lib/Preview/BackgroundCleanupJobTest.php')
-rw-r--r-- | tests/lib/Preview/BackgroundCleanupJobTest.php | 109 |
1 files changed, 72 insertions, 37 deletions
diff --git a/tests/lib/Preview/BackgroundCleanupJobTest.php b/tests/lib/Preview/BackgroundCleanupJobTest.php index a221829b712..ab904f2b499 100644 --- a/tests/lib/Preview/BackgroundCleanupJobTest.php +++ b/tests/lib/Preview/BackgroundCleanupJobTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only @@ -6,15 +7,21 @@ namespace Test\Preview; +use OC\Files\Storage\Temporary; use OC\Preview\BackgroundCleanupJob; use OC\Preview\Storage\Root; use OC\PreviewManager; +use OC\SystemConfig; +use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Files\AppData\IAppDataFactory; use OCP\Files\File; use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IDBConnection; +use OCP\IPreview; +use OCP\Server; use Test\Traits\MountProviderTrait; use Test\Traits\UserTrait; @@ -28,25 +35,12 @@ use Test\Traits\UserTrait; class BackgroundCleanupJobTest extends \Test\TestCase { use MountProviderTrait; use UserTrait; - - /** @var string */ - private $userId; - - /** @var bool */ - private $trashEnabled; - - /** @var IDBConnection */ - private $connection; - - /** @var PreviewManager */ - private $previewManager; - - /** @var IRootFolder */ - private $rootFolder; - - /** @var IMimeTypeLoader */ - private $mimeTypeLoader; - + private string $userId; + private bool $trashEnabled; + private IDBConnection $connection; + private PreviewManager $previewManager; + private IRootFolder $rootFolder; + private IMimeTypeLoader $mimeTypeLoader; private ITimeFactory $timeFactory; protected function setUp(): void { @@ -55,27 +49,27 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $this->userId = $this->getUniqueID(); $user = $this->createUser($this->userId, $this->userId); - $storage = new \OC\Files\Storage\Temporary([]); + $storage = new Temporary([]); $this->registerMount($this->userId, $storage, ''); $this->loginAsUser($this->userId); $this->logout(); $this->loginAsUser($this->userId); - $appManager = \OC::$server->getAppManager(); + $appManager = Server::get(IAppManager::class); $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $user); $appManager->disableApp('files_trashbin'); - $this->connection = \OC::$server->getDatabaseConnection(); - $this->previewManager = \OC::$server->getPreviewManager(); - $this->rootFolder = \OC::$server->get(IRootFolder::class); - $this->mimeTypeLoader = \OC::$server->getMimeTypeLoader(); - $this->timeFactory = \OCP\Server::get(ITimeFactory::class); + $this->connection = Server::get(IDBConnection::class); + $this->previewManager = Server::get(IPreview::class); + $this->rootFolder = Server::get(IRootFolder::class); + $this->mimeTypeLoader = Server::get(IMimeTypeLoader::class); + $this->timeFactory = Server::get(ITimeFactory::class); } protected function tearDown(): void { if ($this->trashEnabled) { - $appManager = \OC::$server->getAppManager(); + $appManager = Server::get(IAppManager::class); $appManager->enableApp('files_trashbin'); } @@ -86,8 +80,8 @@ class BackgroundCleanupJobTest extends \Test\TestCase { private function getRoot(): Root { return new Root( - \OC::$server->get(IRootFolder::class), - \OC::$server->getSystemConfig() + Server::get(IRootFolder::class), + Server::get(SystemConfig::class) ); } @@ -96,7 +90,7 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $files = []; for ($i = 0; $i < 11; $i++) { - $file = $userFolder->newFile($i.'.txt'); + $file = $userFolder->newFile($i . '.txt'); $file->putContent('hello world!'); $this->previewManager->getPreview($file); $files[] = $file; @@ -121,7 +115,7 @@ class BackgroundCleanupJobTest extends \Test\TestCase { return $i; } - public function testCleanupSystemCron() { + public function testCleanupSystemCron(): void { $files = $this->setup11Previews(); $fileIds = array_map(function (File $f) { return $f->getId(); @@ -145,7 +139,11 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $this->assertSame(0, $this->countPreviews($root, $fileIds)); } - public function testCleanupAjax() { + public function testCleanupAjax(): void { + if ($this->connection->getShardDefinition('filecache')) { + $this->markTestSkipped('ajax cron is not supported for sharded setups'); + return; + } $files = $this->setup11Previews(); $fileIds = array_map(function (File $f) { return $f->getId(); @@ -173,21 +171,58 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $this->assertSame(0, $this->countPreviews($root, $fileIds)); } - public function testOldPreviews() { - $appdata = \OC::$server->getAppDataDir('preview'); + public function testOldPreviews(): void { + if ($this->connection->getShardDefinition('filecache')) { + $this->markTestSkipped('old previews are not supported for sharded setups'); + return; + } + $appdata = Server::get(IAppDataFactory::class)->get('preview'); $f1 = $appdata->newFolder('123456781'); $f1->newFile('foo.jpg', 'foo'); $f2 = $appdata->newFolder('123456782'); $f2->newFile('foo.jpg', 'foo'); + $f2 = $appdata->newFolder((string)PHP_INT_MAX - 1); + $f2->newFile('foo.jpg', 'foo'); - $appdata = \OC::$server->getAppDataDir('preview'); - $this->assertSame(2, count($appdata->getDirectoryListing())); + /* + * Cleanup of OldPreviewLocations should only remove numeric folders on AppData level, + * therefore these files should stay untouched. + */ + $appdata->getFolder('/')->newFile('not-a-directory', 'foo'); + $appdata->getFolder('/')->newFile('133742', 'bar'); + + $appdata = Server::get(IAppDataFactory::class)->get('preview'); + // AppData::getDirectoryListing filters all non-folders + $this->assertSame(3, count($appdata->getDirectoryListing())); + try { + $appdata->getFolder('/')->getFile('not-a-directory'); + } catch (NotFoundException) { + $this->fail('Could not find file \'not-a-directory\''); + } + try { + $appdata->getFolder('/')->getFile('133742'); + } catch (NotFoundException) { + $this->fail('Could not find file \'133742\''); + } $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true); $job->run([]); - $appdata = \OC::$server->getAppDataDir('preview'); + $appdata = Server::get(IAppDataFactory::class)->get('preview'); + + // Check if the files created above are still present + // Remember: AppData::getDirectoryListing filters all non-folders $this->assertSame(0, count($appdata->getDirectoryListing())); + try { + $appdata->getFolder('/')->getFile('not-a-directory'); + } catch (NotFoundException) { + $this->fail('Could not find file \'not-a-directory\''); + } + try { + $appdata->getFolder('/')->getFile('133742'); + } catch (NotFoundException) { + $this->fail('Could not find file \'133742\''); + } } } |