ScanFilesTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\Tests\BackgroundJob;
  8. use OC\Files\Mount\MountPoint;
  9. use OC\Files\Storage\Temporary;
  10. use OCA\Files\BackgroundJob\ScanFiles;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\Files\Config\IUserMountCache;
  14. use OCP\IConfig;
  15. use OCP\IUser;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. use Test\Traits\MountProviderTrait;
  19. use Test\Traits\UserTrait;
  20. /**
  21. * Class ScanFilesTest
  22. *
  23. * @package OCA\Files\Tests\BackgroundJob
  24. * @group DB
  25. */
  26. class ScanFilesTest extends TestCase {
  27. use UserTrait;
  28. use MountProviderTrait;
  29. /** @var ScanFiles */
  30. private $scanFiles;
  31. /** @var IUserMountCache */
  32. private $mountCache;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $config = $this->createMock(IConfig::class);
  36. $dispatcher = $this->createMock(IEventDispatcher::class);
  37. $logger = $this->createMock(LoggerInterface::class);
  38. $connection = \OC::$server->getDatabaseConnection();
  39. $this->mountCache = \OC::$server->getUserMountCache();
  40. $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles')
  41. ->setConstructorArgs([
  42. $config,
  43. $dispatcher,
  44. $logger,
  45. $connection,
  46. $this->createMock(ITimeFactory::class)
  47. ])
  48. ->setMethods(['runScanner'])
  49. ->getMock();
  50. }
  51. private function runJob() {
  52. $this->invokePrivate($this->scanFiles, 'run', [[]]);
  53. }
  54. private function getUser(string $userId): IUser {
  55. $user = $this->createMock(IUser::class);
  56. $user->method('getUID')
  57. ->willReturn($userId);
  58. return $user;
  59. }
  60. private function setupStorage(string $user, string $mountPoint) {
  61. $storage = new Temporary([]);
  62. $storage->mkdir('foo');
  63. $storage->getScanner()->scan('');
  64. $this->createUser($user, '');
  65. $this->mountCache->registerMounts($this->getUser($user), [
  66. new MountPoint($storage, $mountPoint)
  67. ]);
  68. return $storage;
  69. }
  70. public function testAllScanned(): void {
  71. $this->setupStorage('foouser', '/foousers/files/foo');
  72. $this->scanFiles->expects($this->never())
  73. ->method('runScanner');
  74. $this->runJob();
  75. }
  76. public function testUnscanned(): void {
  77. $storage = $this->setupStorage('foouser', '/foousers/files/foo');
  78. $storage->getCache()->put('foo', ['size' => -1]);
  79. $this->scanFiles->expects($this->once())
  80. ->method('runScanner')
  81. ->with('foouser');
  82. $this->runJob();
  83. }
  84. }