PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/lib/files/cache/updater.php

https://github.com/sezuan/core
PHP | 268 lines | 210 code | 39 blank | 19 comment | 2 complexity | da50537c0406659646175c5bb3297e65 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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 as Filesystem;
  10. class Updater extends \PHPUnit_Framework_TestCase {
  11. /**
  12. * @var \OC\Files\Storage\Storage $storage
  13. */
  14. private $storage;
  15. /**
  16. * @var \OC\Files\Cache\Scanner $scanner
  17. */
  18. private $scanner;
  19. /**
  20. * @var \OC\Files\Cache\Cache $cache
  21. */
  22. private $cache;
  23. private static $user;
  24. public function setUp() {
  25. $this->storage = new \OC\Files\Storage\Temporary(array());
  26. $textData = "dummy file data\n";
  27. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  28. $this->storage->mkdir('folder');
  29. $this->storage->file_put_contents('foo.txt', $textData);
  30. $this->storage->file_put_contents('foo.png', $imgData);
  31. $this->storage->file_put_contents('folder/bar.txt', $textData);
  32. $this->storage->file_put_contents('folder/bar2.txt', $textData);
  33. $this->scanner = $this->storage->getScanner();
  34. $this->scanner->scan('');
  35. $this->cache = $this->storage->getCache();
  36. \OC\Files\Filesystem::tearDown();
  37. if (!self::$user) {
  38. self::$user = uniqid();
  39. }
  40. \OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files');
  41. Filesystem::clearMounts();
  42. Filesystem::mount($this->storage, array(), '/' . self::$user . '/files');
  43. \OC_Hook::clear('OC_Filesystem');
  44. \OC_Hook::connect('OC_Filesystem', 'post_write', '\OC\Files\Cache\Updater', 'writeHook');
  45. \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook');
  46. \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook');
  47. \OC_Hook::connect('OC_Filesystem', 'post_touch', '\OC\Files\Cache\Updater', 'touchHook');
  48. }
  49. public function tearDown() {
  50. if ($this->cache) {
  51. $this->cache->clear();
  52. }
  53. Filesystem::tearDown();
  54. }
  55. public function testWrite() {
  56. $textSize = strlen("dummy file data\n");
  57. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  58. $this->cache->put('foo.txt', array('mtime' => 100));
  59. $rootCachedData = $this->cache->get('');
  60. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  61. $fooCachedData = $this->cache->get('foo.txt');
  62. Filesystem::file_put_contents('foo.txt', 'asd');
  63. $cachedData = $this->cache->get('foo.txt');
  64. $this->assertEquals(3, $cachedData['size']);
  65. $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
  66. $cachedData = $this->cache->get('');
  67. $this->assertEquals(2 * $textSize + $imageSize + 3, $cachedData['size']);
  68. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  69. $rootCachedData = $cachedData;
  70. $this->assertFalse($this->cache->inCache('bar.txt'));
  71. Filesystem::file_put_contents('bar.txt', 'asd');
  72. $this->assertTrue($this->cache->inCache('bar.txt'));
  73. $cachedData = $this->cache->get('bar.txt');
  74. $this->assertEquals(3, $cachedData['size']);
  75. $mtime = $cachedData['mtime'];
  76. $cachedData = $this->cache->get('');
  77. $this->assertEquals(2 * $textSize + $imageSize + 2 * 3, $cachedData['size']);
  78. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  79. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $mtime);
  80. }
  81. public function testWriteWithMountPoints() {
  82. $storage2 = new \OC\Files\Storage\Temporary(array());
  83. $cache2 = $storage2->getCache();
  84. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  85. $folderCachedData = $this->cache->get('folder');
  86. $substorageCachedData = $cache2->get('');
  87. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  88. $this->assertTrue($cache2->inCache('foo.txt'));
  89. $cachedData = $cache2->get('foo.txt');
  90. $this->assertEquals(3, $cachedData['size']);
  91. $mtime = $cachedData['mtime'];
  92. $cachedData = $cache2->get('');
  93. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  94. $this->assertEquals($mtime, $cachedData['mtime']);
  95. $cachedData = $this->cache->get('folder');
  96. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  97. $this->assertEquals($mtime, $cachedData['mtime']);
  98. }
  99. public function testDelete() {
  100. $textSize = strlen("dummy file data\n");
  101. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  102. $rootCachedData = $this->cache->get('');
  103. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  104. $this->assertTrue($this->cache->inCache('foo.txt'));
  105. Filesystem::unlink('foo.txt');
  106. $this->assertFalse($this->cache->inCache('foo.txt'));
  107. $cachedData = $this->cache->get('');
  108. $this->assertEquals(2 * $textSize + $imageSize, $cachedData['size']);
  109. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  110. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  111. $rootCachedData = $cachedData;
  112. Filesystem::mkdir('bar_folder');
  113. $this->assertTrue($this->cache->inCache('bar_folder'));
  114. $cachedData = $this->cache->get('');
  115. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  116. $rootCachedData = $cachedData;
  117. Filesystem::rmdir('bar_folder');
  118. $this->assertFalse($this->cache->inCache('bar_folder'));
  119. $cachedData = $this->cache->get('');
  120. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  121. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  122. }
  123. public function testDeleteWithMountPoints() {
  124. $storage2 = new \OC\Files\Storage\Temporary(array());
  125. $cache2 = $storage2->getCache();
  126. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  127. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  128. $this->assertTrue($cache2->inCache('foo.txt'));
  129. $folderCachedData = $this->cache->get('folder');
  130. $substorageCachedData = $cache2->get('');
  131. Filesystem::unlink('folder/substorage/foo.txt');
  132. $this->assertFalse($cache2->inCache('foo.txt'));
  133. $cachedData = $cache2->get('');
  134. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  135. $this->assertGreaterThanOrEqual($substorageCachedData['mtime'], $cachedData['mtime']);
  136. $cachedData = $this->cache->get('folder');
  137. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  138. $this->assertGreaterThanOrEqual($folderCachedData['mtime'], $cachedData['mtime']);
  139. }
  140. public function testRename() {
  141. $textSize = strlen("dummy file data\n");
  142. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  143. $rootCachedData = $this->cache->get('');
  144. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  145. $this->assertTrue($this->cache->inCache('foo.txt'));
  146. $fooCachedData = $this->cache->get('foo.txt');
  147. $this->assertFalse($this->cache->inCache('bar.txt'));
  148. Filesystem::rename('foo.txt', 'bar.txt');
  149. $this->assertFalse($this->cache->inCache('foo.txt'));
  150. $this->assertTrue($this->cache->inCache('bar.txt'));
  151. $cachedData = $this->cache->get('bar.txt');
  152. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  153. $mtime = $cachedData['mtime'];
  154. $cachedData = $this->cache->get('');
  155. $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
  156. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  157. }
  158. public function testRenameWithMountPoints() {
  159. $storage2 = new \OC\Files\Storage\Temporary(array());
  160. $cache2 = $storage2->getCache();
  161. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  162. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  163. $this->assertTrue($cache2->inCache('foo.txt'));
  164. $folderCachedData = $this->cache->get('folder');
  165. $substorageCachedData = $cache2->get('');
  166. $fooCachedData = $cache2->get('foo.txt');
  167. Filesystem::rename('folder/substorage/foo.txt', 'folder/substorage/bar.txt');
  168. $this->assertFalse($cache2->inCache('foo.txt'));
  169. $this->assertTrue($cache2->inCache('bar.txt'));
  170. $cachedData = $cache2->get('bar.txt');
  171. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  172. $mtime = $cachedData['mtime'];
  173. $cachedData = $cache2->get('');
  174. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  175. // rename can cause mtime change - invalid assert
  176. // $this->assertEquals($mtime, $cachedData['mtime']);
  177. $cachedData = $this->cache->get('folder');
  178. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  179. // rename can cause mtime change - invalid assert
  180. // $this->assertEquals($mtime, $cachedData['mtime']);
  181. }
  182. public function testTouch() {
  183. $rootCachedData = $this->cache->get('');
  184. $fooCachedData = $this->cache->get('foo.txt');
  185. Filesystem::touch('foo.txt');
  186. $cachedData = $this->cache->get('foo.txt');
  187. $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
  188. $this->assertGreaterThanOrEqual($fooCachedData['mtime'], $cachedData['mtime']);
  189. $cachedData = $this->cache->get('');
  190. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  191. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  192. $rootCachedData = $cachedData;
  193. $time = 1371006070;
  194. $barCachedData = $this->cache->get('folder/bar.txt');
  195. $folderCachedData = $this->cache->get('folder');
  196. Filesystem::touch('folder/bar.txt', $time);
  197. $cachedData = $this->cache->get('folder/bar.txt');
  198. $this->assertNotEquals($barCachedData['etag'], $cachedData['etag']);
  199. $this->assertEquals($time, $cachedData['mtime']);
  200. $cachedData = $this->cache->get('folder');
  201. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  202. $this->assertEquals($time, $cachedData['mtime']);
  203. $cachedData = $this->cache->get('');
  204. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  205. $this->assertEquals($time, $cachedData['mtime']);
  206. }
  207. public function testTouchWithMountPoints() {
  208. $storage2 = new \OC\Files\Storage\Temporary(array());
  209. $cache2 = $storage2->getCache();
  210. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  211. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  212. $this->assertTrue($cache2->inCache('foo.txt'));
  213. $folderCachedData = $this->cache->get('folder');
  214. $substorageCachedData = $cache2->get('');
  215. $fooCachedData = $cache2->get('foo.txt');
  216. $cachedData = $cache2->get('foo.txt');
  217. $time = 1371006070;
  218. Filesystem::touch('folder/substorage/foo.txt', $time);
  219. $cachedData = $cache2->get('foo.txt');
  220. $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
  221. $this->assertEquals($time, $cachedData['mtime']);
  222. $cachedData = $cache2->get('');
  223. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  224. $this->assertEquals($time, $cachedData['mtime']);
  225. $cachedData = $this->cache->get('folder');
  226. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  227. $this->assertEquals($time, $cachedData['mtime']);
  228. }
  229. }