1
0

UpdaterTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. use OC\Files\Filesystem;
  9. use OC\Files\Storage\Temporary;
  10. /**
  11. * Class UpdaterTest
  12. *
  13. * @group DB
  14. *
  15. * @package Test\Files\Cache
  16. */
  17. class UpdaterTest extends \Test\TestCase {
  18. /**
  19. * @var \OC\Files\Storage\Storage
  20. */
  21. protected $storage;
  22. /**
  23. * @var \OC\Files\Cache\Cache
  24. */
  25. protected $cache;
  26. /**
  27. * @var \OC\Files\View
  28. */
  29. protected $view;
  30. /**
  31. * @var \OC\Files\Cache\Updater
  32. */
  33. protected $updater;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->loginAsUser();
  37. $this->storage = new Temporary([]);
  38. $this->updater = $this->storage->getUpdater();
  39. $this->cache = $this->storage->getCache();
  40. }
  41. protected function tearDown(): void {
  42. $this->logout();
  43. parent::tearDown();
  44. }
  45. public function testNewFile(): void {
  46. $this->storage->file_put_contents('foo.txt', 'bar');
  47. $this->assertFalse($this->cache->inCache('foo.txt'));
  48. $this->updater->update('foo.txt');
  49. $this->assertTrue($this->cache->inCache('foo.txt'));
  50. $cached = $this->cache->get('foo.txt');
  51. $this->assertEquals(3, $cached['size']);
  52. $this->assertEquals('text/plain', $cached['mimetype']);
  53. }
  54. public function testUpdatedFile(): void {
  55. $this->storage->file_put_contents('foo.txt', 'bar');
  56. $this->updater->update('foo.txt');
  57. $cached = $this->cache->get('foo.txt');
  58. $this->assertEquals(3, $cached['size']);
  59. $this->assertEquals('text/plain', $cached['mimetype']);
  60. $this->storage->file_put_contents('foo.txt', 'qwerty');
  61. $cached = $this->cache->get('foo.txt');
  62. $this->assertEquals(3, $cached['size']);
  63. $this->updater->update('/foo.txt');
  64. $cached = $this->cache->get('foo.txt');
  65. $this->assertEquals(6, $cached['size']);
  66. }
  67. public function testParentSize(): void {
  68. $this->storage->getScanner()->scan('');
  69. $parentCached = $this->cache->get('');
  70. $this->assertEquals(0, $parentCached['size']);
  71. $this->storage->file_put_contents('foo.txt', 'bar');
  72. $parentCached = $this->cache->get('');
  73. $this->assertEquals(0, $parentCached['size']);
  74. $this->updater->update('foo.txt');
  75. $parentCached = $this->cache->get('');
  76. $this->assertEquals(3, $parentCached['size']);
  77. $this->storage->file_put_contents('foo.txt', 'qwerty');
  78. $parentCached = $this->cache->get('');
  79. $this->assertEquals(3, $parentCached['size']);
  80. $this->updater->update('foo.txt');
  81. $parentCached = $this->cache->get('');
  82. $this->assertEquals(6, $parentCached['size']);
  83. $this->storage->unlink('foo.txt');
  84. $parentCached = $this->cache->get('');
  85. $this->assertEquals(6, $parentCached['size']);
  86. $this->updater->remove('foo.txt');
  87. $parentCached = $this->cache->get('');
  88. $this->assertEquals(0, $parentCached['size']);
  89. }
  90. public function testMove(): void {
  91. $this->storage->file_put_contents('foo.txt', 'qwerty');
  92. $this->updater->update('foo.txt');
  93. $this->assertTrue($this->cache->inCache('foo.txt'));
  94. $this->assertFalse($this->cache->inCache('bar.txt'));
  95. $cached = $this->cache->get('foo.txt');
  96. $this->storage->rename('foo.txt', 'bar.txt');
  97. $this->assertTrue($this->cache->inCache('foo.txt'));
  98. $this->assertFalse($this->cache->inCache('bar.txt'));
  99. $this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
  100. $this->assertFalse($this->cache->inCache('foo.txt'));
  101. $this->assertTrue($this->cache->inCache('bar.txt'));
  102. $cachedTarget = $this->cache->get('bar.txt');
  103. $this->assertEquals($cached['etag'], $cachedTarget['etag']);
  104. $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
  105. $this->assertEquals($cached['size'], $cachedTarget['size']);
  106. $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
  107. }
  108. public function testMoveNonExistingOverwrite(): void {
  109. $this->storage->file_put_contents('bar.txt', 'qwerty');
  110. $this->updater->update('bar.txt');
  111. $cached = $this->cache->get('bar.txt');
  112. $this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
  113. $this->assertFalse($this->cache->inCache('foo.txt'));
  114. $this->assertTrue($this->cache->inCache('bar.txt'));
  115. $cachedTarget = $this->cache->get('bar.txt');
  116. $this->assertEquals($cached['etag'], $cachedTarget['etag']);
  117. $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
  118. $this->assertEquals($cached['size'], $cachedTarget['size']);
  119. $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
  120. }
  121. public function testUpdateStorageMTime(): void {
  122. $this->storage->mkdir('sub');
  123. $this->storage->mkdir('sub2');
  124. $this->storage->file_put_contents('sub/foo.txt', 'qwerty');
  125. $this->updater->update('sub');
  126. $this->updater->update('sub/foo.txt');
  127. $this->updater->update('sub2');
  128. $cachedSourceParent = $this->cache->get('sub');
  129. $cachedSource = $this->cache->get('sub/foo.txt');
  130. $this->storage->rename('sub/foo.txt', 'sub2/bar.txt');
  131. // simulate storage having a different mtime
  132. $testmtime = 1433323578;
  133. // source storage mtime change
  134. $this->storage->touch('sub', $testmtime);
  135. // target storage mtime change
  136. $this->storage->touch('sub2', $testmtime);
  137. // some storages (like Dropbox) change storage mtime on rename
  138. $this->storage->touch('sub2/bar.txt', $testmtime);
  139. $this->updater->renameFromStorage($this->storage, 'sub/foo.txt', 'sub2/bar.txt');
  140. $cachedTargetParent = $this->cache->get('sub2');
  141. $cachedTarget = $this->cache->get('sub2/bar.txt');
  142. $this->assertEquals($cachedSource['mtime'], $cachedTarget['mtime'], 'file mtime preserved');
  143. $this->assertNotEquals($cachedTarget['storage_mtime'], $cachedTarget['mtime'], 'mtime is not storage_mtime for moved file');
  144. $this->assertEquals($testmtime, $cachedTarget['storage_mtime'], 'target file storage_mtime propagated');
  145. $this->assertNotEquals($testmtime, $cachedTarget['mtime'], 'target file mtime changed, not from storage');
  146. $this->assertEquals($testmtime, $cachedTargetParent['storage_mtime'], 'target parent storage_mtime propagated');
  147. $this->assertNotEquals($testmtime, $cachedTargetParent['mtime'], 'target folder mtime changed, not from storage');
  148. }
  149. public function testNewFileDisabled(): void {
  150. $this->storage->file_put_contents('foo.txt', 'bar');
  151. $this->assertFalse($this->cache->inCache('foo.txt'));
  152. $this->updater->disable();
  153. $this->updater->update('/foo.txt');
  154. $this->assertFalse($this->cache->inCache('foo.txt'));
  155. }
  156. public function testMoveCrossStorage(): void {
  157. $storage2 = new Temporary([]);
  158. $cache2 = $storage2->getCache();
  159. Filesystem::mount($storage2, [], '/bar');
  160. $this->storage->file_put_contents('foo.txt', 'qwerty');
  161. $this->updater->update('foo.txt');
  162. $this->assertTrue($this->cache->inCache('foo.txt'));
  163. $this->assertFalse($cache2->inCache('bar.txt'));
  164. $cached = $this->cache->get('foo.txt');
  165. // "rename"
  166. $storage2->file_put_contents('bar.txt', 'qwerty');
  167. $this->storage->unlink('foo.txt');
  168. $this->assertTrue($this->cache->inCache('foo.txt'));
  169. $this->assertFalse($cache2->inCache('bar.txt'));
  170. $storage2->getUpdater()->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
  171. $this->assertFalse($this->cache->inCache('foo.txt'));
  172. $this->assertTrue($cache2->inCache('bar.txt'));
  173. $cachedTarget = $cache2->get('bar.txt');
  174. $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
  175. $this->assertEquals($cached['size'], $cachedTarget['size']);
  176. $this->assertEquals($cached['etag'], $cachedTarget['etag']);
  177. $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
  178. }
  179. public function testMoveFolderCrossStorage(): void {
  180. $storage2 = new Temporary([]);
  181. $cache2 = $storage2->getCache();
  182. Filesystem::mount($storage2, [], '/bar');
  183. $this->storage->mkdir('foo');
  184. $this->storage->mkdir('foo/bar');
  185. $this->storage->file_put_contents('foo/foo.txt', 'qwerty');
  186. $this->storage->file_put_contents('foo/bar.txt', 'foo');
  187. $this->storage->file_put_contents('foo/bar/bar.txt', 'qwertyuiop');
  188. $this->storage->getScanner()->scan('');
  189. $this->assertTrue($this->cache->inCache('foo'));
  190. $this->assertTrue($this->cache->inCache('foo/foo.txt'));
  191. $this->assertTrue($this->cache->inCache('foo/bar.txt'));
  192. $this->assertTrue($this->cache->inCache('foo/bar'));
  193. $this->assertTrue($this->cache->inCache('foo/bar/bar.txt'));
  194. $cached = [];
  195. $cached[] = $this->cache->get('foo');
  196. $cached[] = $this->cache->get('foo/foo.txt');
  197. $cached[] = $this->cache->get('foo/bar.txt');
  198. $cached[] = $this->cache->get('foo/bar');
  199. $cached[] = $this->cache->get('foo/bar/bar.txt');
  200. // add extension to trigger the possible mimetype change
  201. $storage2->moveFromStorage($this->storage, 'foo', 'foo.b');
  202. $storage2->getUpdater()->renameFromStorage($this->storage, 'foo', 'foo.b');
  203. $this->assertFalse($this->cache->inCache('foo'));
  204. $this->assertFalse($this->cache->inCache('foo/foo.txt'));
  205. $this->assertFalse($this->cache->inCache('foo/bar.txt'));
  206. $this->assertFalse($this->cache->inCache('foo/bar'));
  207. $this->assertFalse($this->cache->inCache('foo/bar/bar.txt'));
  208. $this->assertTrue($cache2->inCache('foo.b'));
  209. $this->assertTrue($cache2->inCache('foo.b/foo.txt'));
  210. $this->assertTrue($cache2->inCache('foo.b/bar.txt'));
  211. $this->assertTrue($cache2->inCache('foo.b/bar'));
  212. $this->assertTrue($cache2->inCache('foo.b/bar/bar.txt'));
  213. $cachedTarget = [];
  214. $cachedTarget[] = $cache2->get('foo.b');
  215. $cachedTarget[] = $cache2->get('foo.b/foo.txt');
  216. $cachedTarget[] = $cache2->get('foo.b/bar.txt');
  217. $cachedTarget[] = $cache2->get('foo.b/bar');
  218. $cachedTarget[] = $cache2->get('foo.b/bar/bar.txt');
  219. foreach ($cached as $i => $old) {
  220. $new = $cachedTarget[$i];
  221. $this->assertEquals($old['mtime'], $new['mtime']);
  222. $this->assertEquals($old['size'], $new['size']);
  223. $this->assertEquals($old['etag'], $new['etag']);
  224. $this->assertEquals($old['fileid'], $new['fileid']);
  225. $this->assertEquals($old['mimetype'], $new['mimetype']);
  226. }
  227. }
  228. }