aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/tests/BackgroundJob/ScanFilesTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/tests/BackgroundJob/ScanFilesTest.php')
-rw-r--r--apps/files/tests/BackgroundJob/ScanFilesTest.php185
1 files changed, 75 insertions, 110 deletions
diff --git a/apps/files/tests/BackgroundJob/ScanFilesTest.php b/apps/files/tests/BackgroundJob/ScanFilesTest.php
index 5b5557310d2..00d9ed823f9 100644
--- a/apps/files/tests/BackgroundJob/ScanFilesTest.php
+++ b/apps/files/tests/BackgroundJob/ScanFilesTest.php
@@ -1,136 +1,101 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @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: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Files\Tests\BackgroundJob;
+use OC\Files\Mount\MountPoint;
+use OC\Files\Storage\Temporary;
+use OCA\Files\BackgroundJob\ScanFiles;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\Config\IUserMountCache;
+use OCP\IConfig;
+use OCP\IDBConnection;
use OCP\IUser;
+use OCP\Server;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
-use OCP\IConfig;
-use OCP\IUserManager;
-use OCA\Files\BackgroundJob\ScanFiles;
+use Test\Traits\MountProviderTrait;
+use Test\Traits\UserTrait;
/**
* Class ScanFilesTest
*
* @package OCA\Files\Tests\BackgroundJob
+ * @group DB
*/
class ScanFilesTest extends TestCase {
- /** @var IConfig */
- private $config;
- /** @var IUserManager */
- private $userManager;
- /** @var ScanFiles */
- private $scanFiles;
-
- public function setUp() {
+ use UserTrait;
+ use MountProviderTrait;
+
+ private ScanFiles $scanFiles;
+ private IUserMountCache $mountCache;
+
+ protected function setUp(): void {
parent::setUp();
- $this->config = $this->createMock(IConfig::class);
- $this->userManager = $this->createMock(IUserManager::class);
+ $config = $this->createMock(IConfig::class);
+ $dispatcher = $this->createMock(IEventDispatcher::class);
+ $logger = $this->createMock(LoggerInterface::class);
+ $connection = Server::get(IDBConnection::class);
+ $this->mountCache = Server::get(IUserMountCache::class);
+
+ $this->scanFiles = $this->getMockBuilder(ScanFiles::class)
+ ->setConstructorArgs([
+ $config,
+ $dispatcher,
+ $logger,
+ $connection,
+ $this->createMock(ITimeFactory::class)
+ ])
+ ->onlyMethods(['runScanner'])
+ ->getMock();
+ }
- $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles')
- ->setConstructorArgs([
- $this->config,
- $this->userManager,
- ])
- ->setMethods(['runScanner'])
- ->getMock();
+ private function runJob(): void {
+ self::invokePrivate($this->scanFiles, 'run', [[]]);
}
- public function testRunWithoutUsers() {
- $this->config
- ->expects($this->at(0))
- ->method('getAppValue')
- ->with('files', 'cronjob_scan_files', 0)
- ->will($this->returnValue(50));
- $this->userManager
- ->expects($this->at(0))
- ->method('search')
- ->with('', 500, 50)
- ->will($this->returnValue([]));
- $this->userManager
- ->expects($this->at(1))
- ->method('search')
- ->with('', 500)
- ->will($this->returnValue([]));
- $this->config
- ->expects($this->at(1))
- ->method('setAppValue')
- ->with('files', 'cronjob_scan_files', 500);
-
- $this->invokePrivate($this->scanFiles, 'run', [[]]);
+ private function getUser(string $userId): IUser {
+ $user = $this->createMock(IUser::class);
+ $user->method('getUID')
+ ->willReturn($userId);
+ return $user;
}
- public function testRunWithUsers() {
- $fakeUser = $this->createMock(IUser::class);
- $this->config
- ->expects($this->at(0))
- ->method('getAppValue')
- ->with('files', 'cronjob_scan_files', 0)
- ->will($this->returnValue(50));
- $this->userManager
- ->expects($this->at(0))
- ->method('search')
- ->with('', 500, 50)
- ->will($this->returnValue([
- $fakeUser
- ]));
- $this->config
- ->expects($this->at(1))
- ->method('setAppValue')
- ->with('files', 'cronjob_scan_files', 550);
- $this->scanFiles
- ->expects($this->once())
- ->method('runScanner')
- ->with($fakeUser);
-
- $this->invokePrivate($this->scanFiles, 'run', [[]]);
+ private function setupStorage(string $user, string $mountPoint) {
+ $storage = new Temporary([]);
+ $storage->mkdir('foo');
+ $storage->getScanner()->scan('');
+
+ $this->createUser($user, '');
+ $this->mountCache->registerMounts($this->getUser($user), [
+ new MountPoint($storage, $mountPoint)
+ ]);
+
+ return $storage;
}
- public function testRunWithUsersAndOffsetAtEndOfUserList() {
- $this->config
- ->expects($this->at(0))
- ->method('getAppValue')
- ->with('files', 'cronjob_scan_files', 0)
- ->will($this->returnValue(50));
- $this->userManager
- ->expects($this->at(0))
- ->method('search')
- ->with('', 500, 50)
- ->will($this->returnValue([]));
- $this->userManager
- ->expects($this->at(1))
- ->method('search')
- ->with('', 500)
- ->will($this->returnValue([]));
- $this->config
- ->expects($this->at(1))
- ->method('setAppValue')
- ->with('files', 'cronjob_scan_files', 500);
- $this->scanFiles
- ->expects($this->never())
- ->method('runScanner');
-
- $this->invokePrivate($this->scanFiles, 'run', [[]]);
+ public function testAllScanned(): void {
+ $this->setupStorage('foouser', '/foousers/files/foo');
+
+ $this->scanFiles->expects($this->never())
+ ->method('runScanner');
+ $this->runJob();
}
+ public function testUnscanned(): void {
+ $storage = $this->setupStorage('foouser', '/foousers/files/foo');
+ $storage->getCache()->put('foo', ['size' => -1]);
+
+ $this->scanFiles->expects($this->once())
+ ->method('runScanner')
+ ->with('foouser');
+ $this->runJob();
+ }
}