FileCacheTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Cache;
  23. use OC\Files\Storage\Local;
  24. /**
  25. * Class FileCacheTest
  26. *
  27. * @group DB
  28. *
  29. * @package Test\Cache
  30. */
  31. class FileCacheTest extends TestCache {
  32. /**
  33. * @var string
  34. * */
  35. private $user;
  36. /**
  37. * @var string
  38. * */
  39. private $datadir;
  40. /**
  41. * @var \OC\Files\Storage\Storage
  42. * */
  43. private $storage;
  44. /**
  45. * @var \OC\Files\View
  46. * */
  47. private $rootView;
  48. public function skip() {
  49. //$this->skipUnless(OC_User::isLoggedIn());
  50. }
  51. protected function setUp(): void {
  52. parent::setUp();
  53. //clear all proxies and hooks so we can do clean testing
  54. \OC_Hook::clear('OC_Filesystem');
  55. //set up temporary storage
  56. $this->storage = \OC\Files\Filesystem::getStorage('/');
  57. \OC\Files\Filesystem::clearMounts();
  58. $storage = new \OC\Files\Storage\Temporary([]);
  59. \OC\Files\Filesystem::mount($storage,[],'/');
  60. $datadir = str_replace('local::', '', $storage->getId());
  61. $config = \OC::$server->getConfig();
  62. $this->datadir = $config->getSystemValue('cachedirectory', \OC::$SERVERROOT.'/data/cache');
  63. $config->setSystemValue('cachedirectory', $datadir);
  64. \OC_User::clearBackends();
  65. \OC_User::useBackend(new \Test\Util\User\Dummy());
  66. //login
  67. \OC::$server->getUserManager()->createUser('test', 'test');
  68. $this->user = \OC_User::getUser();
  69. \OC_User::setUserId('test');
  70. //set up the users dir
  71. $this->rootView = new \OC\Files\View('');
  72. $this->rootView->mkdir('/test');
  73. $this->instance = new \OC\Cache\File();
  74. // forces creation of cache folder for subsequent tests
  75. $this->instance->set('hack', 'hack');
  76. }
  77. protected function tearDown(): void {
  78. if ($this->instance) {
  79. $this->instance->remove('hack', 'hack');
  80. }
  81. \OC_User::setUserId($this->user);
  82. \OC::$server->getConfig()->setSystemValue('cachedirectory', $this->datadir);
  83. if ($this->instance) {
  84. $this->instance->clear();
  85. $this->instance = null;
  86. }
  87. //tear down the users dir aswell
  88. $user = \OC::$server->getUserManager()->get('test');
  89. $user->delete();
  90. // Restore the original mount point
  91. \OC\Files\Filesystem::clearMounts();
  92. \OC\Files\Filesystem::mount($this->storage, [], '/');
  93. parent::tearDown();
  94. }
  95. private function setupMockStorage() {
  96. $mockStorage = $this->getMockBuilder(Local::class)
  97. ->setMethods(['filemtime', 'unlink'])
  98. ->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]])
  99. ->getMock();
  100. \OC\Files\Filesystem::mount($mockStorage, [], '/test/cache');
  101. return $mockStorage;
  102. }
  103. public function testGarbageCollectOldKeys() {
  104. $mockStorage = $this->setupMockStorage();
  105. $mockStorage->expects($this->atLeastOnce())
  106. ->method('filemtime')
  107. ->willReturn(100);
  108. $mockStorage->expects($this->once())
  109. ->method('unlink')
  110. ->with('key1')
  111. ->willReturn(true);
  112. $this->instance->set('key1', 'value1');
  113. $this->instance->gc();
  114. }
  115. public function testGarbageCollectLeaveRecentKeys() {
  116. $mockStorage = $this->setupMockStorage();
  117. $mockStorage->expects($this->atLeastOnce())
  118. ->method('filemtime')
  119. ->willReturn(time() + 3600);
  120. $mockStorage->expects($this->never())
  121. ->method('unlink')
  122. ->with('key1');
  123. $this->instance->set('key1', 'value1');
  124. $this->instance->gc();
  125. }
  126. public function lockExceptionProvider() {
  127. return [
  128. [new \OCP\Lock\LockedException('key1')],
  129. [new \OCP\Files\LockNotAcquiredException('key1', 1)],
  130. ];
  131. }
  132. /**
  133. * @dataProvider lockExceptionProvider
  134. */
  135. public function testGarbageCollectIgnoreLockedKeys($testException) {
  136. $mockStorage = $this->setupMockStorage();
  137. $mockStorage->expects($this->atLeastOnce())
  138. ->method('filemtime')
  139. ->willReturn(100);
  140. $mockStorage->expects($this->atLeastOnce())
  141. ->method('unlink')
  142. ->will($this->onConsecutiveCalls(
  143. $this->throwException($testException),
  144. $this->returnValue(true)
  145. ));
  146. $this->instance->set('key1', 'value1');
  147. $this->instance->set('key2', 'value2');
  148. $this->instance->gc();
  149. }
  150. }