1
0

UpdaterTest.php 9.7 KB

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