Skip to content

Commit 18af7cc

Browse files
committed
test: add test for cache mount provider
Signed-off-by: Daniel Kesselberg <[email protected]>
1 parent 4ddbc55 commit 18af7cc

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Daniel Kesselberg <[email protected]>
7+
*
8+
* @author Daniel Kesselberg <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This code is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License, version 3,
14+
* as published by the Free Software Foundation.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License, version 3,
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>
23+
*
24+
*/
25+
26+
namespace Test\Files\Mount;
27+
28+
use OC\Files\Mount\CacheMountProvider;
29+
use OC\Files\Storage\StorageFactory;
30+
use OCP\Files\Storage\IStorageFactory;
31+
use OCP\IConfig;
32+
use OCP\IUser;
33+
use Test\TestCase;
34+
35+
class CacheMountProviderTestStream {
36+
public static $statCounter = 0;
37+
public static $mkdirCounter = 0;
38+
39+
public $context;
40+
41+
public function mkdir(string $path, int $mode, int $options): bool {
42+
self::$mkdirCounter++;
43+
return true;
44+
}
45+
46+
public function url_stat(string $path, int $flags): array|false {
47+
self::$statCounter++;
48+
return false;
49+
}
50+
}
51+
52+
class CacheMountProviderTest extends TestCase {
53+
private IConfig $config;
54+
private IUser $user;
55+
private IStorageFactory $storageFactory;
56+
57+
protected function setUp(): void {
58+
$this->config = $this->createMock(IConfig::class);
59+
$this->user = $this->createMock(IUser::class);
60+
$this->storageFactory = new StorageFactory();
61+
stream_wrapper_register('cachemountprovidertest', CacheMountProviderTestStream::class);
62+
}
63+
64+
protected function tearDown(): void {
65+
stream_wrapper_unregister('cachemountprovidertest');
66+
}
67+
68+
public function testGetMountsForUser(): void {
69+
$provider = new CacheMountProvider($this->config);
70+
71+
$this->assertCount(0, $provider->getMountsForUser($this->user, $this->storageFactory));
72+
}
73+
74+
public function testGetMountsForUserCacheDir(): void {
75+
$this->config->expects($this->exactly(1))
76+
->method('getSystemValueString')
77+
->willReturnMap([
78+
['cache_path', '', 'cachemountprovidertest:////cache_path'],
79+
]);
80+
$this->user->method('getUID')
81+
->willReturn('bob');
82+
83+
$provider = new CacheMountProvider($this->config);
84+
$mounts = $provider->getMountsForUser($this->user, $this->storageFactory);
85+
86+
$this->assertCount(2, $mounts);
87+
$this->assertEquals(1, CacheMountProviderTestStream::$statCounter);
88+
$this->assertEquals(2, CacheMountProviderTestStream::$mkdirCounter);
89+
90+
$cacheMountProvider = $mounts[0];
91+
$this->assertEquals('/bob/cache/', $cacheMountProvider->getMountPoint());
92+
93+
$cacheStorage = $cacheMountProvider->getStorage();
94+
$this->assertEquals('local::cachemountprovidertest://cache_path/bob/', $cacheStorage->getId());
95+
96+
$uploadsMountProvider = $mounts[1];
97+
$this->assertEquals('/bob/uploads/', $uploadsMountProvider->getMountPoint());
98+
99+
$uploadsStorage = $uploadsMountProvider->getStorage();
100+
$this->assertEquals('local::cachemountprovidertest://cache_path/bob/uploads/', $uploadsStorage->getId());
101+
102+
$cacheStorage->mkdir('foobar');
103+
$this->assertEquals(3, CacheMountProviderTestStream::$mkdirCounter);
104+
105+
$uploadsStorage->mkdir('foobar');
106+
$this->assertEquals(4, CacheMountProviderTestStream::$mkdirCounter);
107+
}
108+
}

0 commit comments

Comments
 (0)