1
0

FileCacheTest.php 3.4 KB

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