diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-05-14 14:28:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-14 14:28:55 +0200 |
commit | 0dcb6b267524b645d3e058b935bf8f6b31bf1f56 (patch) | |
tree | f3e8a3826792e29f2fe2cefa8d61462521da7528 | |
parent | d2cc48c6dd07712b35b011e08223718d1fd5ec7d (diff) | |
parent | 0dffd07a60d5c928a0f380e6c072b424bf0184c0 (diff) | |
download | nextcloud-server-0dcb6b267524b645d3e058b935bf8f6b31bf1f56.tar.gz nextcloud-server-0dcb6b267524b645d3e058b935bf8f6b31bf1f56.zip |
Merge pull request #9420 from nextcloud/feature/5263/cleanup_previes
Cleanup previews in the background
21 files changed, 353 insertions, 74 deletions
diff --git a/apps/theming/tests/IconBuilderTest.php b/apps/theming/tests/IconBuilderTest.php index a2a330a2f24..1b9f204cd9e 100644 --- a/apps/theming/tests/IconBuilderTest.php +++ b/apps/theming/tests/IconBuilderTest.php @@ -25,13 +25,11 @@ */ namespace OCA\Theming\Tests; +use OC\Files\AppData\AppData; use OCA\Theming\IconBuilder; use OCA\Theming\ThemingDefaults; use OCA\Theming\Util; use OCP\App\IAppManager; -use OCP\AppFramework\Http\NotFoundResponse; -use OCP\Files\IAppData; -use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IConfig; use PHPUnit\Framework\Error\Warning; @@ -41,7 +39,7 @@ class IconBuilderTest extends TestCase { /** @var IConfig */ protected $config; - /** @var IAppData */ + /** @var AppData */ protected $appData; /** @var ThemingDefaults */ protected $themingDefaults; @@ -56,7 +54,7 @@ class IconBuilderTest extends TestCase { parent::setUp(); $this->config = $this->getMockBuilder(IConfig::class)->getMock(); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); $this->themingDefaults = $this->getMockBuilder('OCA\Theming\ThemingDefaults') ->disableOriginalConstructor()->getMock(); $this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock(); @@ -127,6 +125,10 @@ class IconBuilderTest extends TestCase { $this->themingDefaults->expects($this->once()) ->method('getColorPrimary') ->willReturn($color); + $this->appData->expects($this->once()) + ->method('getFolder') + ->with('images') + ->willThrowException(new NotFoundException()); $expectedIcon = new \Imagick(realpath(dirname(__FILE__)). "/data/" . $file); $icon = new \Imagick(); @@ -156,6 +158,10 @@ class IconBuilderTest extends TestCase { $this->themingDefaults->expects($this->once()) ->method('getColorPrimary') ->willReturn($color); + $this->appData->expects($this->once()) + ->method('getFolder') + ->with('images') + ->willThrowException(new NotFoundException()); $expectedIcon = new \Imagick(realpath(dirname(__FILE__)). "/data/" . $file); $actualIcon = $this->iconBuilder->getFavicon($app); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index bf223204e2b..48aa9533e81 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -790,6 +790,7 @@ return array( 'OC\\PreviewManager' => $baseDir . '/lib/private/PreviewManager.php', 'OC\\PreviewNotAvailableException' => $baseDir . '/lib/private/PreviewNotAvailableException.php', 'OC\\Preview\\BMP' => $baseDir . '/lib/private/Preview/BMP.php', + 'OC\\Preview\\BackgroundCleanupJob' => $baseDir . '/lib/private/Preview/BackgroundCleanupJob.php', 'OC\\Preview\\Bitmap' => $baseDir . '/lib/private/Preview/Bitmap.php', 'OC\\Preview\\Font' => $baseDir . '/lib/private/Preview/Font.php', 'OC\\Preview\\GIF' => $baseDir . '/lib/private/Preview/GIF.php', @@ -837,6 +838,7 @@ return array( 'OC\\Repair\\NC11\\FixMountStorages' => $baseDir . '/lib/private/Repair/NC11/FixMountStorages.php', 'OC\\Repair\\NC13\\AddLogRotateJob' => $baseDir . '/lib/private/Repair/NC13/AddLogRotateJob.php', 'OC\\Repair\\NC13\\RepairInvalidPaths' => $baseDir . '/lib/private/Repair/NC13/RepairInvalidPaths.php', + 'OC\\Repair\\NC14\\AddPreviewBackgroundCleanupJob' => $baseDir . '/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php', 'OC\\Repair\\OldGroupMembershipShares' => $baseDir . '/lib/private/Repair/OldGroupMembershipShares.php', 'OC\\Repair\\Owncloud\\DropAccountTermsTable' => $baseDir . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php', 'OC\\Repair\\Owncloud\\SaveAccountsTableData' => $baseDir . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index cb723ef70ee..0ed2d0a7632 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -820,6 +820,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\PreviewManager' => __DIR__ . '/../../..' . '/lib/private/PreviewManager.php', 'OC\\PreviewNotAvailableException' => __DIR__ . '/../../..' . '/lib/private/PreviewNotAvailableException.php', 'OC\\Preview\\BMP' => __DIR__ . '/../../..' . '/lib/private/Preview/BMP.php', + 'OC\\Preview\\BackgroundCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Preview/BackgroundCleanupJob.php', 'OC\\Preview\\Bitmap' => __DIR__ . '/../../..' . '/lib/private/Preview/Bitmap.php', 'OC\\Preview\\Font' => __DIR__ . '/../../..' . '/lib/private/Preview/Font.php', 'OC\\Preview\\GIF' => __DIR__ . '/../../..' . '/lib/private/Preview/GIF.php', @@ -867,6 +868,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Repair\\NC11\\FixMountStorages' => __DIR__ . '/../../..' . '/lib/private/Repair/NC11/FixMountStorages.php', 'OC\\Repair\\NC13\\AddLogRotateJob' => __DIR__ . '/../../..' . '/lib/private/Repair/NC13/AddLogRotateJob.php', 'OC\\Repair\\NC13\\RepairInvalidPaths' => __DIR__ . '/../../..' . '/lib/private/Repair/NC13/RepairInvalidPaths.php', + 'OC\\Repair\\NC14\\AddPreviewBackgroundCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php', 'OC\\Repair\\OldGroupMembershipShares' => __DIR__ . '/../../..' . '/lib/private/Repair/OldGroupMembershipShares.php', 'OC\\Repair\\Owncloud\\DropAccountTermsTable' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php', 'OC\\Repair\\Owncloud\\SaveAccountsTableData' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php', diff --git a/lib/private/Files/AppData/AppData.php b/lib/private/Files/AppData/AppData.php index 270e834b8e5..e25bf450446 100644 --- a/lib/private/Files/AppData/AppData.php +++ b/lib/private/Files/AppData/AppData.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @@ -31,6 +32,7 @@ use OC\SystemConfig; use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\ISimpleFolder; class AppData implements IAppData { @@ -55,7 +57,7 @@ class AppData implements IAppData { */ public function __construct(IRootFolder $rootFolder, SystemConfig $systemConfig, - $appId) { + string $appId) { $this->rootFolder = $rootFolder; $this->config = $systemConfig; @@ -66,7 +68,7 @@ class AppData implements IAppData { * @return Folder * @throws \RuntimeException */ - private function getAppDataFolder() { + private function getAppDataFolder(): Folder { if ($this->folder === null) { $instanceId = $this->config->getValue('instanceid', null); if ($instanceId === null) { @@ -101,20 +103,20 @@ class AppData implements IAppData { return $this->folder; } - public function getFolder($name) { + public function getFolder(string $name): ISimpleFolder { $node = $this->getAppDataFolder()->get($name); /** @var Folder $node */ return new SimpleFolder($node); } - public function newFolder($name) { + public function newFolder(string $name): ISimpleFolder { $folder = $this->getAppDataFolder()->newFolder($name); return new SimpleFolder($folder); } - public function getDirectoryListing() { + public function getDirectoryListing(): array { $listing = $this->getAppDataFolder()->getDirectoryListing(); $fileListing = array_map(function(Node $folder) { @@ -128,4 +130,8 @@ class AppData implements IAppData { return array_values($fileListing); } + + public function getId(): int { + return $this->getAppDataFolder()->getId(); + } } diff --git a/lib/private/Files/AppData/Factory.php b/lib/private/Files/AppData/Factory.php index 85c75733796..fba2232db06 100644 --- a/lib/private/Files/AppData/Factory.php +++ b/lib/private/Files/AppData/Factory.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @@ -44,7 +45,7 @@ class Factory { * @param string $appId * @return AppData */ - public function get($appId) { + public function get(string $appId): AppData { return new AppData($this->rootFolder, $this->config, $appId); } } diff --git a/lib/private/Preview/BackgroundCleanupJob.php b/lib/private/Preview/BackgroundCleanupJob.php new file mode 100644 index 00000000000..25bf354e28b --- /dev/null +++ b/lib/private/Preview/BackgroundCleanupJob.php @@ -0,0 +1,91 @@ +<?php +declare(strict_types=1); +/** + * @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/> + * + */ + +namespace OC\Preview; + +use OC\BackgroundJob\TimedJob; +use OC\Files\AppData\Factory; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\IDBConnection; + +class BackgroundCleanupJob extends TimedJob { + + /** @var IDBConnection */ + private $connection; + + /** @var Factory */ + private $appDataFactory; + + /** @var bool */ + private $isCLI; + + public function __construct(IDBConnection $connection, + Factory $appDataFactory, + bool $isCLI) { + // Run at most once an hour + $this->setInterval(3600); + + $this->connection = $connection; + $this->appDataFactory = $appDataFactory; + $this->isCLI = $isCLI; + } + + public function run($argument) { + $previews = $this->appDataFactory->get('preview'); + + $previewFodlerId = $previews->getId(); + + $qb = $this->connection->getQueryBuilder(); + $qb->select('a.name') + ->from('filecache', 'a') + ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq( + $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid' + )) + ->where( + $qb->expr()->isNull('b.fileid') + )->andWhere( + $qb->expr()->eq('a.parent', $qb->createNamedParameter($previewFodlerId)) + ); + + if (!$this->isCLI) { + $qb->setMaxResults(10); + } + + $cursor = $qb->execute(); + + while ($row = $cursor->fetch()) { + try { + $preview = $previews->getFolder($row['name']); + $preview->delete(); + } catch (NotFoundException $e) { + // continue + } catch (NotPermittedException $e) { + // continue + } + } + + $cursor->closeCursor(); + } +} diff --git a/lib/private/Preview/Watcher.php b/lib/private/Preview/Watcher.php index 8d091b84b0e..be462d9c935 100644 --- a/lib/private/Preview/Watcher.php +++ b/lib/private/Preview/Watcher.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * @@ -22,7 +23,6 @@ */ namespace OC\Preview; -use OCP\Files\File; use OCP\Files\Node; use OCP\Files\Folder; use OCP\Files\IAppData; @@ -39,9 +39,6 @@ class Watcher { /** @var IAppData */ private $appData; - /** @var int[] */ - private $toDelete = []; - /** * Watcher constructor. * @@ -58,47 +55,10 @@ class Watcher { } try { - $folder = $this->appData->getFolder($node->getId()); + $folder = $this->appData->getFolder((string)$node->getId()); $folder->delete(); } catch (NotFoundException $e) { //Nothing to do } } - - public function preDelete(Node $node) { - // To avoid cycles - if ($this->toDelete !== []) { - return; - } - - if ($node instanceof File) { - $this->toDelete[] = $node->getId(); - return; - } - - /** @var Folder $node */ - $this->deleteFolder($node); - } - - private function deleteFolder(Folder $folder) { - $nodes = $folder->getDirectoryListing(); - foreach ($nodes as $node) { - if ($node instanceof File) { - $this->toDelete[] = $node->getId(); - } else if ($node instanceof Folder) { - $this->deleteFolder($node); - } - } - } - - public function postDelete(Node $node) { - foreach ($this->toDelete as $fid) { - try { - $folder = $this->appData->getFolder($fid); - $folder->delete(); - } catch (NotFoundException $e) { - // continue - } - } - } } diff --git a/lib/private/Preview/WatcherConnector.php b/lib/private/Preview/WatcherConnector.php index 4e6e786cec7..bf9e6c29e4f 100644 --- a/lib/private/Preview/WatcherConnector.php +++ b/lib/private/Preview/WatcherConnector.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * @@ -49,7 +50,7 @@ class WatcherConnector { /** * @return Watcher */ - private function getWatcher() { + private function getWatcher(): Watcher { return \OC::$server->query(Watcher::class); } @@ -59,14 +60,6 @@ class WatcherConnector { $this->root->listen('\OC\Files', 'postWrite', function (Node $node) { $this->getWatcher()->postWrite($node); }); - - $this->root->listen('\OC\Files', 'preDelete', function (Node $node) { - $this->getWatcher()->preDelete($node); - }); - - $this->root->listen('\OC\Files', 'postDelete', function (Node $node) { - $this->getWatcher()->postDelete($node); - }); } } } diff --git a/lib/private/Repair.php b/lib/private/Repair.php index a257ef061e7..8746f1e6f27 100644 --- a/lib/private/Repair.php +++ b/lib/private/Repair.php @@ -36,6 +36,7 @@ use OC\Repair\Collation; use OC\Repair\MoveUpdaterStepFile; use OC\Repair\NC11\FixMountStorages; use OC\Repair\NC13\AddLogRotateJob; +use OC\Repair\NC14\AddPreviewBackgroundCleanupJob; use OC\Repair\OldGroupMembershipShares; use OC\Repair\Owncloud\DropAccountTermsTable; use OC\Repair\Owncloud\SaveAccountsTableData; @@ -132,7 +133,8 @@ class Repair implements IOutput{ new FixMountStorages(\OC::$server->getDatabaseConnection()), new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), new AddLogRotateJob(\OC::$server->getJobList()), - new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)) + new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)), + new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()), ]; } diff --git a/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php b/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php new file mode 100644 index 00000000000..b58fabcba50 --- /dev/null +++ b/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php @@ -0,0 +1,48 @@ +<?php +declare(strict_types=1); +/** + * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OC\Repair\NC14; + +use OC\Preview\BackgroundCleanupJob; +use OCP\BackgroundJob\IJobList; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; + +class AddPreviewBackgroundCleanupJob implements IRepairStep { + + /** @var IJobList */ + private $jobList; + + public function __construct(IJobList $jobList) { + $this->jobList = $jobList; + } + + public function getName(): string { + return 'Add preview background cleanup job'; + } + + public function run(IOutput $output) { + $this->jobList->add(BackgroundCleanupJob::class); + } + +} diff --git a/lib/private/Setup.php b/lib/private/Setup.php index 5564bb5b072..25e0b4d8817 100644 --- a/lib/private/Setup.php +++ b/lib/private/Setup.php @@ -47,6 +47,7 @@ use OC\App\AppStore\Bundles\BundleFetcher; use OC\Authentication\Token\DefaultTokenCleanupJob; use OC\Authentication\Token\DefaultTokenProvider; use OC\Log\Rotate; +use OC\Preview\BackgroundCleanupJob; use OCP\Defaults; use OCP\IL10N; use OCP\ILogger; @@ -419,6 +420,7 @@ class Setup { $jobList = \OC::$server->getJobList(); $jobList->add(DefaultTokenCleanupJob::class); $jobList->add(Rotate::class); + $jobList->add(BackgroundCleanupJob::class); } /** diff --git a/lib/public/Files/SimpleFS/ISimpleRoot.php b/lib/public/Files/SimpleFS/ISimpleRoot.php index 9b4b8d76947..054106fbaca 100644 --- a/lib/public/Files/SimpleFS/ISimpleRoot.php +++ b/lib/public/Files/SimpleFS/ISimpleRoot.php @@ -42,7 +42,7 @@ interface ISimpleRoot { * @throws \RuntimeException * @since 11.0.0 */ - public function getFolder($name); + public function getFolder(string $name): ISimpleFolder; /** * Get all the Folders @@ -52,7 +52,7 @@ interface ISimpleRoot { * @throws \RuntimeException * @since 11.0.0 */ - public function getDirectoryListing(); + public function getDirectoryListing(): array; /** * Create a new folder named $name @@ -63,5 +63,5 @@ interface ISimpleRoot { * @throws \RuntimeException * @since 11.0.0 */ - public function newFolder($name); + public function newFolder(string $name): ISimpleFolder; } diff --git a/tests/Core/Controller/CssControllerTest.php b/tests/Core/Controller/CssControllerTest.php index 8ec15f449a0..f4e6e83201e 100644 --- a/tests/Core/Controller/CssControllerTest.php +++ b/tests/Core/Controller/CssControllerTest.php @@ -23,6 +23,7 @@ namespace Tests\Core\Controller; use OC\Core\Controller\CssController; +use OC\Files\AppData\AppData; use OC\Files\AppData\Factory; use OCP\AppFramework\Http; use OCP\AppFramework\Http\FileDisplayResponse; @@ -51,7 +52,7 @@ class CssControllerTest extends TestCase { /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */ $factory = $this->createMock(Factory::class); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); $factory->expects($this->once()) ->method('get') ->with('css') diff --git a/tests/Core/Controller/JsControllerTest.php b/tests/Core/Controller/JsControllerTest.php index 3910045276b..307ef837786 100644 --- a/tests/Core/Controller/JsControllerTest.php +++ b/tests/Core/Controller/JsControllerTest.php @@ -23,6 +23,7 @@ namespace Tests\Core\Controller; use OC\Core\Controller\JsController; +use OC\Files\AppData\AppData; use OC\Files\AppData\Factory; use OCP\AppFramework\Http; use OCP\AppFramework\Http\FileDisplayResponse; @@ -51,7 +52,7 @@ class JsControllerTest extends TestCase { /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */ $factory = $this->createMock(Factory::class); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); $factory->expects($this->once()) ->method('get') ->with('js') diff --git a/tests/lib/App/AppStore/Fetcher/AppFetcherTest.php b/tests/lib/App/AppStore/Fetcher/AppFetcherTest.php index 59dc7366cc0..1465e09d08b 100644 --- a/tests/lib/App/AppStore/Fetcher/AppFetcherTest.php +++ b/tests/lib/App/AppStore/Fetcher/AppFetcherTest.php @@ -23,6 +23,7 @@ namespace Test\App\AppStore\Fetcher; use OC\App\AppStore\Fetcher\AppFetcher; use OC\App\CompareVersion; +use OC\Files\AppData\AppData; use OC\Files\AppData\Factory; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\IAppData; @@ -63,7 +64,7 @@ EOD; /** @var Factory|PHPUnit_Framework_MockObject_MockObject $factory */ $factory = $this->createMock(Factory::class); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); $factory->expects($this->once()) ->method('get') ->with('appstore') diff --git a/tests/lib/App/AppStore/Fetcher/FetcherBase.php b/tests/lib/App/AppStore/Fetcher/FetcherBase.php index 90b7523d850..851773a6506 100644 --- a/tests/lib/App/AppStore/Fetcher/FetcherBase.php +++ b/tests/lib/App/AppStore/Fetcher/FetcherBase.php @@ -22,6 +22,7 @@ namespace Test\App\AppStore\Fetcher; use OC\App\AppStore\Fetcher\Fetcher; +use OC\Files\AppData\AppData; use OC\Files\AppData\Factory; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\IAppData; @@ -58,7 +59,7 @@ abstract class FetcherBase extends TestCase { public function setUp() { parent::setUp(); $this->appDataFactory = $this->createMock(Factory::class); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); $this->appDataFactory->expects($this->once()) ->method('get') ->with('appstore') diff --git a/tests/lib/Preview/BackgroundCleanupJobTest.php b/tests/lib/Preview/BackgroundCleanupJobTest.php new file mode 100644 index 00000000000..9d10da025dd --- /dev/null +++ b/tests/lib/Preview/BackgroundCleanupJobTest.php @@ -0,0 +1,159 @@ +<?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/> + * + */ + +namespace Test\Preview; + +use OC\Files\AppData\Factory; +use OC\Preview\BackgroundCleanupJob; +use OC\PreviewManager; +use OCP\Files\IRootFolder; +use OCP\IDBConnection; +use Test\Traits\MountProviderTrait; +use Test\Traits\UserTrait; + +/** + * Class BackgroundCleanupJobTest + * + * @group DB + * + * @package Test\Preview + */ +class BackgroundCleanupJobTest extends \Test\TestCase { + + use MountProviderTrait; + use UserTrait; + + /** @var string */ + private $userId; + + /** @var bool */ + private $trashEnabled; + + /** @var Factory */ + private $appDataFactory; + + /** @var IDBConnection */ + private $connection; + + /** @var PreviewManager */ + private $previewManager; + + /** @var IRootFolder */ + private $rootFolder; + + public function setUp() { + parent::setUp(); + + $this->userId = $this->getUniqueID(); + $this->createUser($this->userId, $this->userId); + + $storage = new \OC\Files\Storage\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->disableApp('files_trashbin'); + + $this->appDataFactory = \OC::$server->query(Factory::class); + $this->connection = \OC::$server->getDatabaseConnection(); + $this->previewManager = \OC::$server->getPreviewManager(); + $this->rootFolder = \OC::$server->getRootFolder(); + } + + public function tearDown() { + if ($this->trashEnabled) { + $appManager = \OC::$server->getAppManager(); + $appManager->enableApp('files_trashbin'); + } + + $this->logout(); + + return parent::tearDown(); + } + + private function setup11Previews(): array { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + + $files = []; + for ($i = 0; $i < 11; $i++) { + $file = $userFolder->newFile($i.'.txt'); + $file->putContent('hello world!'); + $this->previewManager->getPreview($file); + $files[] = $file; + } + + return $files; + } + + public function testCleanupSystemCron() { + $files = $this->setup11Previews(); + + $preview = $this->appDataFactory->get('preview'); + + $previews = $preview->getDirectoryListing(); + $this->assertCount(11, $previews); + + $job = new BackgroundCleanupJob($this->connection, $this->appDataFactory, true); + $job->run([]); + + foreach ($files as $file) { + $file->delete(); + } + + $this->assertCount(11, $previews); + $job->run([]); + + $previews = $preview->getDirectoryListing(); + $this->assertCount(0, $previews); + } + + public function testCleanupAjax() { + $files = $this->setup11Previews(); + + $preview = $this->appDataFactory->get('preview'); + + $previews = $preview->getDirectoryListing(); + $this->assertCount(11, $previews); + + $job = new BackgroundCleanupJob($this->connection, $this->appDataFactory, false); + $job->run([]); + + foreach ($files as $file) { + $file->delete(); + } + + $this->assertCount(11, $previews); + $job->run([]); + + $previews = $preview->getDirectoryListing(); + $this->assertCount(1, $previews); + + $job->run([]); + + $previews = $preview->getDirectoryListing(); + $this->assertCount(0, $previews); + } +} diff --git a/tests/lib/Security/IdentityProof/ManagerTest.php b/tests/lib/Security/IdentityProof/ManagerTest.php index 290e7be5c94..9d17182e52e 100644 --- a/tests/lib/Security/IdentityProof/ManagerTest.php +++ b/tests/lib/Security/IdentityProof/ManagerTest.php @@ -21,6 +21,7 @@ namespace Test\Security\IdentityProof; +use OC\Files\AppData\AppData; use OC\Files\AppData\Factory; use OC\Security\IdentityProof\Key; use OC\Security\IdentityProof\Manager; @@ -50,7 +51,7 @@ class ManagerTest extends TestCase { /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */ $this->factory = $this->createMock(Factory::class); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); $this->config = $this->createMock(IConfig::class); $this->factory->expects($this->any()) ->method('get') diff --git a/tests/lib/Template/CSSResourceLocatorTest.php b/tests/lib/Template/CSSResourceLocatorTest.php index 5e2c3705efa..a8b123b8d5b 100644 --- a/tests/lib/Template/CSSResourceLocatorTest.php +++ b/tests/lib/Template/CSSResourceLocatorTest.php @@ -23,6 +23,7 @@ namespace Test\Template; +use OC\Files\AppData\AppData; use OC\Files\AppData\Factory; use OCP\Files\IAppData; use OCP\ICacheFactory; @@ -51,7 +52,7 @@ class CSSResourceLocatorTest extends \Test\TestCase { parent::setUp(); $this->logger = $this->createMock(ILogger::class); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->config = $this->createMock(IConfig::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); diff --git a/tests/lib/Template/SCSSCacherTest.php b/tests/lib/Template/SCSSCacherTest.php index a55ba30711b..5e3700477ff 100644 --- a/tests/lib/Template/SCSSCacherTest.php +++ b/tests/lib/Template/SCSSCacherTest.php @@ -23,6 +23,7 @@ namespace Test\Template; +use OC\Files\AppData\AppData; use OC\Files\AppData\Factory; use OC\Template\SCSSCacher; use OCA\Theming\ThemingDefaults; @@ -58,7 +59,7 @@ class SCSSCacherTest extends \Test\TestCase { protected function setUp() { parent::setUp(); $this->logger = $this->createMock(ILogger::class); - $this->appData = $this->createMock(IAppData::class); + $this->appData = $this->createMock(AppData::class); /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */ $factory = $this->createMock(Factory::class); diff --git a/version.php b/version.php index bd0de81ff33..433f248aec7 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ // between betas, final and RCs. This is _not_ the public version number. Reset minor/patchlevel // when updating major/minor version number. -$OC_Version = array(14, 0, 0, 2); +$OC_Version = array(14, 0, 0, 3); // The human readable string $OC_VersionString = '14.0.0 alpha'; |