You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watcher.php 5.2KB

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