diff options
Diffstat (limited to 'tests/lib/Preview/BackgroundCleanupJobTest.php')
-rw-r--r-- | tests/lib/Preview/BackgroundCleanupJobTest.php | 139 |
1 files changed, 81 insertions, 58 deletions
diff --git a/tests/lib/Preview/BackgroundCleanupJobTest.php b/tests/lib/Preview/BackgroundCleanupJobTest.php index cd9f6ef0399..ab904f2b499 100644 --- a/tests/lib/Preview/BackgroundCleanupJobTest.php +++ b/tests/lib/Preview/BackgroundCleanupJobTest.php @@ -1,35 +1,27 @@ <?php + /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only */ 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; @@ -43,51 +35,41 @@ 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 { parent::setUp(); $this->userId = $this->getUniqueID(); - $this->createUser($this->userId, $this->userId); + $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(); - $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $this->userId); + $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->getRootFolder(); - $this->mimeTypeLoader = \OC::$server->getMimeTypeLoader(); + $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'); } @@ -98,8 +80,8 @@ class BackgroundCleanupJobTest extends \Test\TestCase { private function getRoot(): Root { return new Root( - \OC::$server->getRootFolder(), - \OC::$server->getSystemConfig() + Server::get(IRootFolder::class), + Server::get(SystemConfig::class) ); } @@ -108,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; @@ -133,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(); @@ -142,7 +124,7 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $root = $this->getRoot(); $this->assertSame(11, $this->countPreviews($root, $fileIds)); - $job = new BackgroundCleanupJob($this->connection, $root, $this->mimeTypeLoader, true); + $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $root, $this->mimeTypeLoader, true); $job->run([]); foreach ($files as $file) { @@ -157,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(); @@ -166,7 +152,7 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $root = $this->getRoot(); $this->assertSame(11, $this->countPreviews($root, $fileIds)); - $job = new BackgroundCleanupJob($this->connection, $root, $this->mimeTypeLoader, false); + $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $root, $this->mimeTypeLoader, false); $job->run([]); foreach ($files as $file) { @@ -185,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->connection, $this->getRoot(), $this->mimeTypeLoader, true); + $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\''); + } } } |