watcher.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. class Watcher extends \Test\TestCase {
  10. /**
  11. * @var \OC\Files\Storage\Storage[] $storages
  12. */
  13. private $storages = array();
  14. protected function setUp() {
  15. parent::setUp();
  16. $this->loginAsUser();
  17. }
  18. protected function tearDown() {
  19. foreach ($this->storages as $storage) {
  20. $cache = $storage->getCache();
  21. $ids = $cache->getAll();
  22. $cache->clear();
  23. }
  24. $this->logout();
  25. parent::tearDown();
  26. }
  27. /**
  28. * @medium
  29. */
  30. function testWatcher() {
  31. $storage = $this->getTestStorage();
  32. $cache = $storage->getCache();
  33. $updater = $storage->getWatcher();
  34. //set the mtime to the past so it can detect an mtime change
  35. $cache->put('', array('storage_mtime' => 10));
  36. $this->assertTrue($cache->inCache('folder/bar.txt'));
  37. $this->assertTrue($cache->inCache('folder/bar2.txt'));
  38. $this->assertFalse($cache->inCache('bar.test'));
  39. $storage->file_put_contents('bar.test', 'foo');
  40. $updater->checkUpdate('');
  41. $this->assertTrue($cache->inCache('bar.test'));
  42. $cachedData = $cache->get('bar.test');
  43. $this->assertEquals(3, $cachedData['size']);
  44. $cache->put('bar.test', array('storage_mtime' => 10));
  45. $storage->file_put_contents('bar.test', 'test data');
  46. // make sure that PHP can read the new size correctly
  47. clearstatcache();
  48. $updater->checkUpdate('bar.test');
  49. $cachedData = $cache->get('bar.test');
  50. $this->assertEquals(9, $cachedData['size']);
  51. $cache->put('folder', array('storage_mtime' => 10));
  52. $storage->unlink('folder/bar2.txt');
  53. $updater->checkUpdate('folder');
  54. $this->assertTrue($cache->inCache('folder/bar.txt'));
  55. $this->assertFalse($cache->inCache('folder/bar2.txt'));
  56. }
  57. /**
  58. * @medium
  59. */
  60. public function testFileToFolder() {
  61. $storage = $this->getTestStorage();
  62. $cache = $storage->getCache();
  63. $updater = $storage->getWatcher();
  64. //set the mtime to the past so it can detect an mtime change
  65. $cache->put('', array('storage_mtime' => 10));
  66. $storage->unlink('foo.txt');
  67. $storage->rename('folder', 'foo.txt');
  68. $updater->checkUpdate('');
  69. $entry = $cache->get('foo.txt');
  70. $this->assertEquals('httpd/unix-directory', $entry['mimetype']);
  71. $this->assertFalse($cache->inCache('folder'));
  72. $this->assertFalse($cache->inCache('folder/bar.txt'));
  73. $storage = $this->getTestStorage();
  74. $cache = $storage->getCache();
  75. $updater = $storage->getWatcher();
  76. //set the mtime to the past so it can detect an mtime change
  77. $cache->put('foo.txt', array('storage_mtime' => 10));
  78. $storage->unlink('foo.txt');
  79. $storage->rename('folder', 'foo.txt');
  80. $updater->checkUpdate('foo.txt');
  81. $entry = $cache->get('foo.txt');
  82. $this->assertEquals('httpd/unix-directory', $entry['mimetype']);
  83. $this->assertTrue($cache->inCache('foo.txt/bar.txt'));
  84. }
  85. public function testPolicyNever() {
  86. $storage = $this->getTestStorage();
  87. $cache = $storage->getCache();
  88. $updater = $storage->getWatcher();
  89. //set the mtime to the past so it can detect an mtime change
  90. $cache->put('foo.txt', array('storage_mtime' => 10));
  91. $updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_NEVER);
  92. $storage->file_put_contents('foo.txt', 'q');
  93. $this->assertFalse($updater->checkUpdate('foo.txt'));
  94. $cache->put('foo.txt', array('storage_mtime' => 20));
  95. $storage->file_put_contents('foo.txt', 'w');
  96. $this->assertFalse($updater->checkUpdate('foo.txt'));
  97. }
  98. public function testPolicyOnce() {
  99. $storage = $this->getTestStorage();
  100. $cache = $storage->getCache();
  101. $updater = $storage->getWatcher();
  102. //set the mtime to the past so it can detect an mtime change
  103. $cache->put('foo.txt', array('storage_mtime' => 10));
  104. $updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);
  105. $storage->file_put_contents('foo.txt', 'q');
  106. $this->assertTrue($updater->checkUpdate('foo.txt'));
  107. $cache->put('foo.txt', array('storage_mtime' => 20));
  108. $storage->file_put_contents('foo.txt', 'w');
  109. $this->assertFalse($updater->checkUpdate('foo.txt'));
  110. }
  111. public function testPolicyAlways() {
  112. $storage = $this->getTestStorage();
  113. $cache = $storage->getCache();
  114. $updater = $storage->getWatcher();
  115. //set the mtime to the past so it can detect an mtime change
  116. $cache->put('foo.txt', array('storage_mtime' => 10));
  117. $updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ALWAYS);
  118. $storage->file_put_contents('foo.txt', 'q');
  119. $this->assertTrue($updater->checkUpdate('foo.txt'));
  120. $cache->put('foo.txt', array('storage_mtime' => 20));
  121. $storage->file_put_contents('foo.txt', 'w');
  122. $this->assertTrue($updater->checkUpdate('foo.txt'));
  123. }
  124. /**
  125. * @param bool $scan
  126. * @return \OC\Files\Storage\Storage
  127. */
  128. private function getTestStorage($scan = true) {
  129. $storage = new \OC\Files\Storage\Temporary(array());
  130. $textData = "dummy file data\n";
  131. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  132. $storage->mkdir('folder');
  133. $storage->file_put_contents('foo.txt', $textData);
  134. $storage->file_put_contents('foo.png', $imgData);
  135. $storage->file_put_contents('folder/bar.txt', $textData);
  136. $storage->file_put_contents('folder/bar2.txt', $textData);
  137. if ($scan) {
  138. $scanner = $storage->getScanner();
  139. $scanner->scan('');
  140. }
  141. $this->storages[] = $storage;
  142. return $storage;
  143. }
  144. }