1
0

FileCacheTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. function skip() {
  49. //$this->skipUnless(OC_User::isLoggedIn());
  50. }
  51. protected function setUp() {
  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(array());
  59. \OC\Files\Filesystem::mount($storage,array(),'/');
  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() {
  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. // Restore the original mount point
  84. \OC\Files\Filesystem::clearMounts();
  85. \OC\Files\Filesystem::mount($this->storage, array(), '/');
  86. parent::tearDown();
  87. }
  88. private function setupMockStorage() {
  89. $mockStorage = $this->getMockBuilder(Local::class)
  90. ->setMethods(['filemtime', 'unlink'])
  91. ->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]])
  92. ->getMock();
  93. \OC\Files\Filesystem::mount($mockStorage, array(), '/test/cache');
  94. return $mockStorage;
  95. }
  96. public function testGarbageCollectOldKeys() {
  97. $mockStorage = $this->setupMockStorage();
  98. $mockStorage->expects($this->atLeastOnce())
  99. ->method('filemtime')
  100. ->will($this->returnValue(100));
  101. $mockStorage->expects($this->once())
  102. ->method('unlink')
  103. ->with('key1')
  104. ->will($this->returnValue(true));
  105. $this->instance->set('key1', 'value1');
  106. $this->instance->gc();
  107. }
  108. public function testGarbageCollectLeaveRecentKeys() {
  109. $mockStorage = $this->setupMockStorage();
  110. $mockStorage->expects($this->atLeastOnce())
  111. ->method('filemtime')
  112. ->will($this->returnValue(time() + 3600));
  113. $mockStorage->expects($this->never())
  114. ->method('unlink')
  115. ->with('key1');
  116. $this->instance->set('key1', 'value1');
  117. $this->instance->gc();
  118. }
  119. public function lockExceptionProvider() {
  120. return [
  121. [new \OCP\Lock\LockedException('key1')],
  122. [new \OCP\Files\LockNotAcquiredException('key1', 1)],
  123. ];
  124. }
  125. /**
  126. * @dataProvider lockExceptionProvider
  127. */
  128. public function testGarbageCollectIgnoreLockedKeys($testException) {
  129. $mockStorage = $this->setupMockStorage();
  130. $mockStorage->expects($this->atLeastOnce())
  131. ->method('filemtime')
  132. ->will($this->returnValue(100));
  133. $mockStorage->expects($this->atLeastOnce())
  134. ->method('unlink')
  135. ->will($this->onConsecutiveCalls(
  136. $this->throwException($testException),
  137. $this->returnValue(true)
  138. ));
  139. $this->instance->set('key1', 'value1');
  140. $this->instance->set('key2', 'value2');
  141. $this->instance->gc();
  142. }
  143. }