WatcherTest.php 5.5 KB

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