ScanFilesTest.php 2.4 KB

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