1
0

ScanFilesTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files\Tests\BackgroundJob;
  25. use OC\Files\Mount\MountPoint;
  26. use OC\Files\Storage\Temporary;
  27. use OCA\Files\BackgroundJob\ScanFiles;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\IConfig;
  31. use OCP\IUser;
  32. use Psr\Log\LoggerInterface;
  33. use Test\TestCase;
  34. use Test\Traits\MountProviderTrait;
  35. use Test\Traits\UserTrait;
  36. /**
  37. * Class ScanFilesTest
  38. *
  39. * @package OCA\Files\Tests\BackgroundJob
  40. * @group DB
  41. */
  42. class ScanFilesTest extends TestCase {
  43. use UserTrait;
  44. use MountProviderTrait;
  45. /** @var ScanFiles */
  46. private $scanFiles;
  47. /** @var \OCP\Files\Config\IUserMountCache */
  48. private $mountCache;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $config = $this->createMock(IConfig::class);
  52. $dispatcher = $this->createMock(IEventDispatcher::class);
  53. $logger = $this->createMock(LoggerInterface::class);
  54. $connection = \OC::$server->getDatabaseConnection();
  55. $this->mountCache = \OC::$server->getUserMountCache();
  56. $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles')
  57. ->setConstructorArgs([
  58. $config,
  59. $dispatcher,
  60. $logger,
  61. $connection,
  62. $this->createMock(ITimeFactory::class)
  63. ])
  64. ->setMethods(['runScanner'])
  65. ->getMock();
  66. }
  67. private function runJob() {
  68. $this->invokePrivate($this->scanFiles, 'run', [[]]);
  69. }
  70. private function getUser(string $userId): IUser {
  71. $user = $this->createMock(IUser::class);
  72. $user->method('getUID')
  73. ->willReturn($userId);
  74. return $user;
  75. }
  76. private function setupStorage(string $user, string $mountPoint) {
  77. $storage = new Temporary([]);
  78. $storage->mkdir('foo');
  79. $storage->getScanner()->scan('');
  80. $this->createUser($user, '');
  81. $this->mountCache->registerMounts($this->getUser($user), [
  82. new MountPoint($storage, $mountPoint)
  83. ]);
  84. return $storage;
  85. }
  86. public function testAllScanned() {
  87. $this->setupStorage('foouser', '/foousers/files/foo');
  88. $this->scanFiles->expects($this->never())
  89. ->method('runScanner');
  90. $this->runJob();
  91. }
  92. public function testUnscanned() {
  93. $storage = $this->setupStorage('foouser', '/foousers/files/foo');
  94. $storage->getCache()->put('foo', ['size' => -1]);
  95. $this->scanFiles->expects($this->once())
  96. ->method('runScanner')
  97. ->with('foouser');
  98. $this->runJob();
  99. }
  100. }