FileCacheTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. use Test\Traits\UserTrait;
  25. /**
  26. * Class FileCacheTest
  27. *
  28. * @group DB
  29. *
  30. * @package Test\Cache
  31. */
  32. class FileCacheTest extends TestCache {
  33. use UserTrait;
  34. /**
  35. * @var string
  36. * */
  37. private $user;
  38. /**
  39. * @var string
  40. * */
  41. private $datadir;
  42. /**
  43. * @var \OC\Files\Storage\Storage
  44. * */
  45. private $storage;
  46. /**
  47. * @var \OC\Files\View
  48. * */
  49. private $rootView;
  50. public function skip() {
  51. //$this->skipUnless(OC_User::isLoggedIn());
  52. }
  53. protected function setUp(): void {
  54. parent::setUp();
  55. //login
  56. $this->createUser('test', 'test');
  57. $this->user = \OC_User::getUser();
  58. \OC_User::setUserId('test');
  59. //clear all proxies and hooks so we can do clean testing
  60. \OC_Hook::clear('OC_Filesystem');
  61. //set up temporary storage
  62. $this->storage = \OC\Files\Filesystem::getStorage('/');
  63. \OC\Files\Filesystem::clearMounts();
  64. $storage = new \OC\Files\Storage\Temporary([]);
  65. \OC\Files\Filesystem::mount($storage, [], '/');
  66. $datadir = str_replace('local::', '', $storage->getId());
  67. $config = \OC::$server->getConfig();
  68. $this->datadir = $config->getSystemValue('cachedirectory', \OC::$SERVERROOT.'/data/cache');
  69. $config->setSystemValue('cachedirectory', $datadir);
  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. // Restore the original mount point
  88. \OC\Files\Filesystem::clearMounts();
  89. \OC\Files\Filesystem::mount($this->storage, [], '/');
  90. parent::tearDown();
  91. }
  92. private function setupMockStorage() {
  93. $mockStorage = $this->getMockBuilder(Local::class)
  94. ->setMethods(['filemtime', 'unlink'])
  95. ->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]])
  96. ->getMock();
  97. \OC\Files\Filesystem::mount($mockStorage, [], '/test/cache');
  98. return $mockStorage;
  99. }
  100. public function testGarbageCollectOldKeys() {
  101. $mockStorage = $this->setupMockStorage();
  102. $mockStorage->expects($this->atLeastOnce())
  103. ->method('filemtime')
  104. ->willReturn(100);
  105. $mockStorage->expects($this->once())
  106. ->method('unlink')
  107. ->with('key1')
  108. ->willReturn(true);
  109. $this->instance->set('key1', 'value1');
  110. $this->instance->gc();
  111. }
  112. public function testGarbageCollectLeaveRecentKeys() {
  113. $mockStorage = $this->setupMockStorage();
  114. $mockStorage->expects($this->atLeastOnce())
  115. ->method('filemtime')
  116. ->willReturn(time() + 3600);
  117. $mockStorage->expects($this->never())
  118. ->method('unlink')
  119. ->with('key1');
  120. $this->instance->set('key1', 'value1');
  121. $this->instance->gc();
  122. }
  123. public function lockExceptionProvider() {
  124. return [
  125. [new \OCP\Lock\LockedException('key1')],
  126. [new \OCP\Files\LockNotAcquiredException('key1', 1)],
  127. ];
  128. }
  129. /**
  130. * @dataProvider lockExceptionProvider
  131. */
  132. public function testGarbageCollectIgnoreLockedKeys($testException) {
  133. $mockStorage = $this->setupMockStorage();
  134. $mockStorage->expects($this->atLeastOnce())
  135. ->method('filemtime')
  136. ->willReturn(100);
  137. $mockStorage->expects($this->atLeastOnce())
  138. ->method('unlink')
  139. ->will($this->onConsecutiveCalls(
  140. $this->throwException($testException),
  141. $this->returnValue(true)
  142. ));
  143. $this->instance->set('key1', 'value1');
  144. $this->instance->set('key2', 'value2');
  145. $this->instance->gc();
  146. }
  147. }