1
0

UpdaterTest.php 11 KB

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