FileCacheTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 OCP\Files\Mount\IMountManager;
  25. use Test\Traits\UserTrait;
  26. /**
  27. * Class FileCacheTest
  28. *
  29. * @group DB
  30. *
  31. * @package Test\Cache
  32. */
  33. class FileCacheTest extends TestCache {
  34. use UserTrait;
  35. /**
  36. * @var string
  37. * */
  38. private $user;
  39. /**
  40. * @var string
  41. * */
  42. private $datadir;
  43. /**
  44. * @var \OC\Files\Storage\Storage
  45. * */
  46. private $storage;
  47. /**
  48. * @var \OC\Files\View
  49. * */
  50. private $rootView;
  51. public function skip() {
  52. //$this->skipUnless(OC_User::isLoggedIn());
  53. }
  54. protected function setUp(): void {
  55. parent::setUp();
  56. //login
  57. $this->createUser('test', 'test');
  58. $this->user = \OC_User::getUser();
  59. \OC_User::setUserId('test');
  60. //clear all proxies and hooks so we can do clean testing
  61. \OC_Hook::clear('OC_Filesystem');
  62. /** @var IMountManager $manager */
  63. $manager = \OC::$server->get(IMountManager::class);
  64. $manager->removeMount('/test');
  65. $storage = new \OC\Files\Storage\Temporary([]);
  66. \OC\Files\Filesystem::mount($storage, [], '/test/cache');
  67. //set up the users dir
  68. $this->rootView = new \OC\Files\View('');
  69. $this->rootView->mkdir('/test');
  70. $this->instance = new \OC\Cache\File();
  71. // forces creation of cache folder for subsequent tests
  72. $this->instance->set('hack', 'hack');
  73. }
  74. protected function tearDown(): void {
  75. if ($this->instance) {
  76. $this->instance->remove('hack', 'hack');
  77. }
  78. \OC_User::setUserId($this->user);
  79. if ($this->instance) {
  80. $this->instance->clear();
  81. $this->instance = null;
  82. }
  83. parent::tearDown();
  84. }
  85. private function setupMockStorage() {
  86. $mockStorage = $this->getMockBuilder(Local::class)
  87. ->setMethods(['filemtime', 'unlink'])
  88. ->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]])
  89. ->getMock();
  90. \OC\Files\Filesystem::mount($mockStorage, [], '/test/cache');
  91. return $mockStorage;
  92. }
  93. public function testGarbageCollectOldKeys() {
  94. $mockStorage = $this->setupMockStorage();
  95. $mockStorage->expects($this->atLeastOnce())
  96. ->method('filemtime')
  97. ->willReturn(100);
  98. $mockStorage->expects($this->once())
  99. ->method('unlink')
  100. ->with('key1')
  101. ->willReturn(true);
  102. $this->instance->set('key1', 'value1');
  103. $this->instance->gc();
  104. }
  105. public function testGarbageCollectLeaveRecentKeys() {
  106. $mockStorage = $this->setupMockStorage();
  107. $mockStorage->expects($this->atLeastOnce())
  108. ->method('filemtime')
  109. ->willReturn(time() + 3600);
  110. $mockStorage->expects($this->never())
  111. ->method('unlink')
  112. ->with('key1');
  113. $this->instance->set('key1', 'value1');
  114. $this->instance->gc();
  115. }
  116. public function lockExceptionProvider() {
  117. return [
  118. [new \OCP\Lock\LockedException('key1')],
  119. [new \OCP\Files\LockNotAcquiredException('key1', 1)],
  120. ];
  121. }
  122. /**
  123. * @dataProvider lockExceptionProvider
  124. */
  125. public function testGarbageCollectIgnoreLockedKeys($testException) {
  126. $mockStorage = $this->setupMockStorage();
  127. $mockStorage->expects($this->atLeastOnce())
  128. ->method('filemtime')
  129. ->willReturn(100);
  130. $mockStorage->expects($this->atLeastOnce())
  131. ->method('unlink')
  132. ->will($this->onConsecutiveCalls(
  133. $this->throwException($testException),
  134. $this->returnValue(true)
  135. ));
  136. $this->instance->set('key1', 'value1');
  137. $this->instance->set('key2', 'value2');
  138. $this->instance->gc();
  139. }
  140. }