ObjectStoreScannerTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Files\ObjectStore;
  7. use OC\Files\Cache\Scanner;
  8. use OC\Files\ObjectStore\ObjectStoreScanner;
  9. use OC\Files\Storage\Temporary;
  10. use OCP\Files\Cache\ICache;
  11. use OCP\Files\Storage\IStorage;
  12. use Test\TestCase;
  13. /**
  14. * @group DB
  15. */
  16. class ObjectStoreScannerTest extends TestCase {
  17. private IStorage $storage;
  18. private ICache $cache;
  19. private ObjectStoreScanner $scanner;
  20. private Scanner $realScanner;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->storage = new Temporary([]);
  24. $this->cache = $this->storage->getCache();
  25. $this->scanner = new ObjectStoreScanner($this->storage);
  26. $this->realScanner = new Scanner($this->storage);
  27. }
  28. public function testFile(): void {
  29. $data = "dummy file data\n";
  30. $this->storage->file_put_contents('foo.txt', $data);
  31. $this->assertEquals(
  32. [],
  33. $this->scanner->scanFile('foo.txt'),
  34. 'Asserting that no error occurred while scanFile()'
  35. );
  36. }
  37. private function fillTestFolders() {
  38. $textData = "dummy file data\n";
  39. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo/logo.png');
  40. $this->storage->mkdir('folder');
  41. $this->storage->file_put_contents('foo.txt', $textData);
  42. $this->storage->file_put_contents('foo.png', $imgData);
  43. $this->storage->file_put_contents('folder/bar.txt', $textData);
  44. }
  45. public function testFolder(): void {
  46. $this->fillTestFolders();
  47. $this->assertEquals(
  48. [],
  49. $this->scanner->scan(''),
  50. 'Asserting that no error occurred while scan()'
  51. );
  52. }
  53. public function testBackgroundScan(): void {
  54. $this->fillTestFolders();
  55. $this->storage->mkdir('folder2');
  56. $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
  57. $this->realScanner->scan('');
  58. $this->assertEquals(6, $this->cache->get('folder2')->getSize());
  59. $this->cache->put('folder2', ['size' => -1]);
  60. $this->assertEquals(-1, $this->cache->get('folder2')->getSize());
  61. $this->scanner->backgroundScan();
  62. $this->assertEquals(6, $this->cache->get('folder2')->getSize());
  63. }
  64. }