HomeCacheTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Cache;
  8. class DummyUser extends \OC\User\User {
  9. /**
  10. * @var string $home
  11. */
  12. private $home;
  13. /**
  14. * @var string $uid
  15. */
  16. private $uid;
  17. /**
  18. * @param string $uid
  19. * @param string $home
  20. */
  21. public function __construct($uid, $home) {
  22. $this->home = $home;
  23. $this->uid = $uid;
  24. }
  25. /**
  26. * @return string
  27. */
  28. public function getHome() {
  29. return $this->home;
  30. }
  31. /**
  32. * @return string
  33. */
  34. public function getUID() {
  35. return $this->uid;
  36. }
  37. }
  38. /**
  39. * Class HomeCacheTest
  40. *
  41. * @group DB
  42. *
  43. * @package Test\Files\Cache
  44. */
  45. class HomeCacheTest extends \Test\TestCase {
  46. /**
  47. * @var \OC\Files\Storage\Home $storage
  48. */
  49. private $storage;
  50. /**
  51. * @var \OC\Files\Cache\HomeCache $cache
  52. */
  53. private $cache;
  54. /**
  55. * @var \OC\User\User $user
  56. */
  57. private $user;
  58. protected function setUp(): void {
  59. parent::setUp();
  60. $this->user = new DummyUser('foo', \OC::$server->getTempManager()->getTemporaryFolder());
  61. $this->storage = new \OC\Files\Storage\Home(['user' => $this->user]);
  62. $this->cache = $this->storage->getCache();
  63. }
  64. /**
  65. * Tests that the root and files folder size calculation ignores the subdirs
  66. * that have an unknown size. This makes sure that quota calculation still
  67. * works as it's based on the "files" folder size.
  68. */
  69. public function testRootFolderSizeIgnoresUnknownUpdate() {
  70. $dir1 = 'files/knownsize';
  71. $dir2 = 'files/unknownsize';
  72. $fileData = [];
  73. $fileData[''] = ['size' => -1, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  74. $fileData['files'] = ['size' => -1, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  75. $fileData[$dir1] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  76. $fileData[$dir2] = ['size' => -1, 'mtime' => 25, 'mimetype' => 'httpd/unix-directory'];
  77. $this->cache->put('', $fileData['']);
  78. $this->cache->put('files', $fileData['files']);
  79. $this->cache->put($dir1, $fileData[$dir1]);
  80. $this->cache->put($dir2, $fileData[$dir2]);
  81. $this->assertTrue($this->cache->inCache('files'));
  82. $this->assertTrue($this->cache->inCache($dir1));
  83. $this->assertTrue($this->cache->inCache($dir2));
  84. // check that files and root size ignored the unknown sizes
  85. $this->assertEquals(1000, $this->cache->calculateFolderSize('files'));
  86. // clean up
  87. $this->cache->remove('');
  88. $this->cache->remove('files');
  89. $this->cache->remove($dir1);
  90. $this->cache->remove($dir2);
  91. $this->assertFalse($this->cache->inCache('files'));
  92. $this->assertFalse($this->cache->inCache($dir1));
  93. $this->assertFalse($this->cache->inCache($dir2));
  94. }
  95. public function testRootFolderSizeIsFilesSize() {
  96. $dir1 = 'files';
  97. $afile = 'test.txt';
  98. $fileData = [];
  99. $fileData[''] = ['size' => 1500, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  100. $fileData[$dir1] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  101. $fileData[$afile] = ['size' => 500, 'mtime' => 20];
  102. $this->cache->put('', $fileData['']);
  103. $this->cache->put($dir1, $fileData[$dir1]);
  104. $this->assertTrue($this->cache->inCache($dir1));
  105. // check that root size ignored the unknown sizes
  106. $data = $this->cache->get('files');
  107. $this->assertEquals(1000, $data['size']);
  108. $data = $this->cache->get('');
  109. $this->assertEquals(1000, $data['size']);
  110. // clean up
  111. $this->cache->remove('');
  112. $this->cache->remove($dir1);
  113. $this->assertFalse($this->cache->inCache($dir1));
  114. }
  115. }