2015-11-16 16:23:16 -05:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
declare(strict_types=1);
|
2015-11-16 16:23:16 -05:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-11-16 16:23:16 -05:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files\Tests\BackgroundJob;
|
|
|
|
|
|
2020-09-22 09:03:17 -04:00
|
|
|
use OC\Files\Mount\MountPoint;
|
|
|
|
|
use OC\Files\Storage\Temporary;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Files\BackgroundJob\ScanFiles;
|
2022-06-28 06:55:26 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2020-09-22 09:03:17 -04:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2024-10-18 06:04:22 -04:00
|
|
|
use OCP\Files\Config\IUserMountCache;
|
2015-11-16 16:23:16 -05:00
|
|
|
use OCP\IConfig;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\IDBConnection;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IUser;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\Server;
|
2022-03-21 07:41:58 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-11-22 14:52:10 -05:00
|
|
|
use Test\TestCase;
|
2020-09-22 09:03:17 -04:00
|
|
|
use Test\Traits\MountProviderTrait;
|
|
|
|
|
use Test\Traits\UserTrait;
|
2015-11-16 16:23:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class ScanFilesTest
|
|
|
|
|
*
|
|
|
|
|
* @package OCA\Files\Tests\BackgroundJob
|
|
|
|
|
*/
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
2015-11-16 16:23:16 -05:00
|
|
|
class ScanFilesTest extends TestCase {
|
2020-09-22 09:03:17 -04:00
|
|
|
use UserTrait;
|
|
|
|
|
use MountProviderTrait;
|
|
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
private ScanFiles $scanFiles;
|
|
|
|
|
private IUserMountCache $mountCache;
|
2015-11-16 16:23:16 -05:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2015-11-16 16:23:16 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2020-09-22 09:03:17 -04:00
|
|
|
$config = $this->createMock(IConfig::class);
|
|
|
|
|
$dispatcher = $this->createMock(IEventDispatcher::class);
|
2022-03-21 07:41:58 -04:00
|
|
|
$logger = $this->createMock(LoggerInterface::class);
|
2025-02-03 09:34:01 -05:00
|
|
|
$connection = Server::get(IDBConnection::class);
|
|
|
|
|
$this->mountCache = Server::get(IUserMountCache::class);
|
2015-11-16 16:23:16 -05:00
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
$this->scanFiles = $this->getMockBuilder(ScanFiles::class)
|
2020-09-22 09:03:17 -04:00
|
|
|
->setConstructorArgs([
|
|
|
|
|
$config,
|
|
|
|
|
$dispatcher,
|
|
|
|
|
$logger,
|
|
|
|
|
$connection,
|
2022-06-28 06:55:26 -04:00
|
|
|
$this->createMock(ITimeFactory::class)
|
2020-09-22 09:03:17 -04:00
|
|
|
])
|
2025-05-29 06:19:28 -04:00
|
|
|
->onlyMethods(['runScanner'])
|
2020-09-22 09:03:17 -04:00
|
|
|
->getMock();
|
2015-11-16 16:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
private function runJob(): void {
|
|
|
|
|
self::invokePrivate($this->scanFiles, 'run', [[]]);
|
2015-11-16 16:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-09-22 09:03:17 -04:00
|
|
|
private function getUser(string $userId): IUser {
|
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
$user->method('getUID')
|
|
|
|
|
->willReturn($userId);
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
2015-11-16 16:23:16 -05:00
|
|
|
|
2020-09-22 09:03:17 -04:00
|
|
|
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;
|
2015-11-16 16:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testAllScanned(): void {
|
2020-09-22 09:03:17 -04:00
|
|
|
$this->setupStorage('foouser', '/foousers/files/foo');
|
2015-11-16 16:23:16 -05:00
|
|
|
|
2020-09-22 09:03:17 -04:00
|
|
|
$this->scanFiles->expects($this->never())
|
|
|
|
|
->method('runScanner');
|
|
|
|
|
$this->runJob();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testUnscanned(): void {
|
2020-09-22 09:03:17 -04:00
|
|
|
$storage = $this->setupStorage('foouser', '/foousers/files/foo');
|
|
|
|
|
$storage->getCache()->put('foo', ['size' => -1]);
|
|
|
|
|
|
|
|
|
|
$this->scanFiles->expects($this->once())
|
|
|
|
|
->method('runScanner')
|
|
|
|
|
->with('foouser');
|
|
|
|
|
$this->runJob();
|
2015-11-16 16:23:16 -05:00
|
|
|
}
|
|
|
|
|
}
|