1
0

WatcherTest.php 5.5 KB

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