99namespace Test \Cache ;
1010
1111use OC \Cache \File ;
12- use OC \Files \Filesystem ;
13- use OC \Files \Storage \Local ;
14- use OC \Files \Storage \Storage ;
1512use OC \Files \Storage \Temporary ;
16- use OC \Files \View ;
17- use OCP \Files \LockNotAcquiredException ;
13+ use OCP \Files \ISetupManager ;
1814use OCP \Files \Mount \IMountManager ;
19- use OCP \ITempManager ;
20- use OCP \Lock \LockedException ;
15+ use OCP \Files \Storage \IStorage ;
16+ use OCP \IUserSession ;
17+ use OCP \Lock \ILockingProvider ;
2118use OCP \Server ;
19+ use Test \Traits \MountProviderTrait ;
2220use Test \Traits \UserTrait ;
2321
2422/**
3028#[\PHPUnit \Framework \Attributes \Group('DB ' )]
3129class FileCacheTest extends TestCache {
3230 use UserTrait;
31+ use MountProviderTrait;
3332
34- /**
35- * @var string
36- * */
37- private $ user ;
38- /**
39- * @var string
40- * */
41- private $ datadir ;
42- /**
43- * @var Storage
44- * */
45- private $ storage ;
46- /**
47- * @var View
48- * */
49- private $ rootView ;
50-
51- public function skip () {
52- //$this->skipUnless(OC_User::isLoggedIn());
53- }
33+ private IStorage $ storage ;
5434
5535 #[\Override]
5636 protected function setUp (): void {
5737 parent ::setUp ();
5838
59- //login
60- $ this ->createUser ('test ' , 'test ' );
61-
62- $ this ->user = \OC_User::getUser ();
63- \OC_User::setUserId ('test ' );
39+ $ user = $ this ->createUser ('test ' , 'test ' );
6440
65- //clear all proxies and hooks so we can do clean testing
66- \OC_Hook:: clear ( ' OC_Filesystem ' );
41+ $ userSession = Server:: get (IUserSession::class);
42+ $ userSession -> setUser ( $ user );
6743
6844 /** @var IMountManager $manager */
6945 $ manager = Server::get (IMountManager::class);
7046 $ manager ->removeMount ('/test ' );
7147
72- $ storage = new Temporary ([]);
73- Filesystem::mount ($ storage , [], '/test/cache ' );
74-
75- //set up the users dir
76- $ this ->rootView = new View ('' );
77- $ this ->rootView ->mkdir ('/test ' );
48+ $ this ->storage = new Temporary ([]);
49+ $ this ->registerMount ($ user ->getUID (), $ this ->storage , '/ ' . $ user ->getUID () . '/cache/ ' );
7850
7951 $ this ->instance = new File ();
8052
@@ -84,80 +56,55 @@ protected function setUp(): void {
8456
8557 #[\Override]
8658 protected function tearDown (): void {
87- if ($ this ->instance ) {
88- $ this ->instance ->remove ('hack ' , 'hack ' );
89- }
90-
91- \OC_User::setUserId ($ this ->user );
92-
9359 if ($ this ->instance ) {
9460 $ this ->instance ->clear ();
9561 $ this ->instance = null ;
9662 }
9763
98- parent ::tearDown ();
99- }
100-
101- private function setupMockStorage () {
102- $ mockStorage = $ this ->getMockBuilder (Local::class)
103- ->onlyMethods (['filemtime ' , 'unlink ' ])
104- ->setConstructorArgs ([['datadir ' => Server::get (ITempManager::class)->getTemporaryFolder ()]])
105- ->getMock ();
106-
107- Filesystem::mount ($ mockStorage , [], '/test/cache ' );
64+ Server::get (ISetupManager::class)->tearDown ();
10865
109- return $ mockStorage ;
66+ parent :: tearDown () ;
11067 }
11168
11269 public function testGarbageCollectOldKeys (): void {
113- $ mockStorage = $ this ->setupMockStorage ( );
70+ $ this ->instance -> set ( ' key1 ' , ' value1 ' );
11471
115- $ mockStorage ->expects ($ this ->atLeastOnce ())
116- ->method ('filemtime ' )
117- ->willReturn (100 );
118- $ mockStorage ->expects ($ this ->once ())
119- ->method ('unlink ' )
120- ->with ('key1 ' )
121- ->willReturn (true );
72+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
73+ $ this ->storage ->getCache ()->put ('key1 ' , ['mtime ' => 100 ]);
12274
123- $ this ->instance ->set ('key1 ' , 'value1 ' );
12475 $ this ->instance ->gc ();
76+ $ this ->assertFalse ($ this ->storage ->file_exists ('key1 ' ));
12577 }
12678
12779 public function testGarbageCollectLeaveRecentKeys (): void {
128- $ mockStorage = $ this ->setupMockStorage ();
129-
130- $ mockStorage ->expects ($ this ->atLeastOnce ())
131- ->method ('filemtime ' )
132- ->willReturn (time () + 3600 );
133- $ mockStorage ->expects ($ this ->never ())
134- ->method ('unlink ' )
135- ->with ('key1 ' );
13680 $ this ->instance ->set ('key1 ' , 'value1 ' );
81+
82+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
83+ $ this ->storage ->getCache ()->put ('key1 ' , ['mtime ' => time () + 3600 ]);
84+
13785 $ this ->instance ->gc ();
138- }
13986
140- public static function lockExceptionProvider (): array {
141- return [
142- [new LockedException ('key1 ' )],
143- [new LockNotAcquiredException ('key1 ' , 1 )],
144- ];
87+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
14588 }
14689
147- #[\PHPUnit \Framework \Attributes \DataProvider('lockExceptionProvider ' )]
148- public function testGarbageCollectIgnoreLockedKeys ($ testException ): void {
149- $ mockStorage = $ this ->setupMockStorage ();
150-
151- $ mockStorage ->expects ($ this ->atLeastOnce ())
152- ->method ('filemtime ' )
153- ->willReturn (100 );
154- $ mockStorage ->expects ($ this ->atLeastOnce ())
155- ->method ('unlink ' )
156- ->willReturnOnConsecutiveCalls ($ this ->throwException ($ testException ), true );
90+ public function testGarbageCollectIgnoreLockedKeys (): void {
91+ $ lockingProvider = \OC ::$ server ->get (ILockingProvider::class);
15792
15893 $ this ->instance ->set ('key1 ' , 'value1 ' );
94+ $ this ->storage ->getCache ()->put ('key1 ' , ['mtime ' => 100 ]);
15995 $ this ->instance ->set ('key2 ' , 'value2 ' );
96+ $ this ->storage ->getCache ()->put ('key2 ' , ['mtime ' => 100 ]);
97+ $ this ->storage ->acquireLock ('key2 ' , ILockingProvider::LOCK_SHARED , $ lockingProvider );
98+
99+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
100+ $ this ->assertTrue ($ this ->storage ->file_exists ('key2 ' ));
160101
161102 $ this ->instance ->gc ();
103+
104+ $ this ->storage ->releaseLock ('key2 ' , ILockingProvider::LOCK_SHARED , $ lockingProvider );
105+
106+ $ this ->assertFalse ($ this ->storage ->file_exists ('key1 ' ));
107+ $ this ->assertFalse ($ this ->storage ->file_exists ('key2 ' ));
108+
162109 }
163110}
0 commit comments