UpdaterTest.php 11 KB

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