aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Files/Mount/CacheMountProviderTest.php
blob: d142e5fc3c2ac14e1611aca0cea58ed152d68a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de>
 *
 * @author Daniel Kesselberg <mail@danielkesselberg.de>
 *
 * @license AGPL-3.0-or-later
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace Test\Files\Mount;

use OC\Files\Mount\CacheMountProvider;
use OC\Files\Storage\StorageFactory;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IUser;
use Test\TestCase;

class CacheMountProviderTestStream {
	public static $statCounter = 0;
	public static $mkdirCounter = 0;

	public $context;

	public function mkdir(string $path, int $mode, int $options): bool {
		self::$mkdirCounter++;
		return true;
	}

	public function url_stat(string $path, int $flags): array|false {
		self::$statCounter++;
		return false;
	}
}

class CacheMountProviderTest extends TestCase {
	private IConfig $config;
	private IUser $user;
	private IStorageFactory $storageFactory;

	protected function setUp(): void {
		$this->config = $this->createMock(IConfig::class);
		$this->user = $this->createMock(IUser::class);
		$this->storageFactory = new StorageFactory();
		stream_wrapper_register('cachemountprovidertest', CacheMountProviderTestStream::class);
	}

	protected function tearDown(): void {
		stream_wrapper_unregister('cachemountprovidertest');
	}

	public function testGetMountsForUser(): void {
		$provider = new CacheMountProvider($this->config);

		$this->assertCount(0, $provider->getMountsForUser($this->user, $this->storageFactory));
	}

	public function testGetMountsForUserCacheDir(): void {
		$this->config->expects($this->exactly(1))
			->method('getSystemValueString')
			->willReturnMap([
				['cache_path', '', 'cachemountprovidertest:////cache_path'],
			]);
		$this->user->method('getUID')
			->willReturn('bob');

		$provider = new CacheMountProvider($this->config);
		$mounts = $provider->getMountsForUser($this->user, $this->storageFactory);

		$this->assertCount(2, $mounts);
		$this->assertEquals(1, CacheMountProviderTestStream::$statCounter);
		$this->assertEquals(2, CacheMountProviderTestStream::$mkdirCounter);

		$cacheMountProvider = $mounts[0];
		$this->assertEquals('/bob/cache/', $cacheMountProvider->getMountPoint());

		$cacheStorage = $cacheMountProvider->getStorage();
		$this->assertEquals('local::cachemountprovidertest://cache_path/bob/', $cacheStorage->getId());

		$uploadsMountProvider = $mounts[1];
		$this->assertEquals('/bob/uploads/', $uploadsMountProvider->getMountPoint());

		$uploadsStorage = $uploadsMountProvider->getStorage();
		$this->assertEquals('local::cachemountprovidertest://cache_path/bob/uploads/', $uploadsStorage->getId());

		$cacheStorage->mkdir('foobar');
		$this->assertEquals(3, CacheMountProviderTestStream::$mkdirCounter);

		$uploadsStorage->mkdir('foobar');
		$this->assertEquals(4, CacheMountProviderTestStream::$mkdirCounter);
	}
}