UpdaterTest.php 9.7 KB

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