aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/Db/TransactionalTest.php
blob: a60c4386fea136be654663a47c35de2a7b2b1e72 (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
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace lib\AppFramework\Db;

use OCP\AppFramework\Db\TTransactional;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use RuntimeException;
use Test\TestCase;

class TransactionalTest extends TestCase {
	/** @var IDBConnection|MockObject */
	private IDBConnection $db;

	protected function setUp(): void {
		parent::setUp();

		$this->db = $this->createMock(IDBConnection::class);
	}

	public function testAtomicRollback(): void {
		$test = new class($this->db) {
			use TTransactional;

			private IDBConnection $db;

			public function __construct(IDBConnection $db) {
				$this->db = $db;
			}

			public function fail(): void {
				$this->atomic(function () {
					throw new RuntimeException('nope');
				}, $this->db);
			}
		};
		$this->db->expects(self::once())
			->method('beginTransaction');
		$this->db->expects(self::once())
			->method('rollback');
		$this->db->expects(self::never())
			->method('commit');
		$this->expectException(RuntimeException::class);

		$test->fail();
	}

	public function testAtomicCommit(): void {
		$test = new class($this->db) {
			use TTransactional;

			private IDBConnection $db;

			public function __construct(IDBConnection $db) {
				$this->db = $db;
			}

			public function succeed(): int {
				return $this->atomic(function () {
					return 3;
				}, $this->db);
			}
		};
		$this->db->expects(self::once())
			->method('beginTransaction');
		$this->db->expects(self::never())
			->method('rollback');
		$this->db->expects(self::once())
			->method('commit');

		$result = $test->succeed();

		self::assertEquals(3, $result);
	}
}
>); /** @var IMountManager $manager */ $manager = Server::get(IMountManager::class); $manager->removeMount('/' . $this->user); $this->storageMock = null; } protected function tearDown(): void { $this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage); $this->user = null; if ($this->storageMock) { $this->storageMock->getCache()->clear(); $this->storageMock = null; } Filesystem::tearDown(); \OC_User::setUserId(''); Server::get(IConfig::class)->deleteAllUserValues($this->user); parent::tearDown(); } /** * Returns a storage mock that returns the given value as * free space * * @param int $freeSpace free space value * @return \OC\Files\Storage\Storage */ private function getStorageMock($freeSpace = 12) { $this->storageMock = $this->getMockBuilder(Temporary::class) ->onlyMethods(['free_space']) ->setConstructorArgs([[]]) ->getMock(); $this->storageMock->expects($this->once()) ->method('free_space') ->willReturn($freeSpace); return $this->storageMock; } /** * Test getting the storage info */ public function testGetStorageInfo(): void { $homeStorage = $this->getStorageMock(12); Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free']); $this->assertEquals(5, $storageInfo['used']); $this->assertEquals(17, $storageInfo['total']); } private function getIncludeExternalStorage(): bool { $class = new \ReflectionClass(\OC_Helper::class); $prop = $class->getProperty('quotaIncludeExternalStorage'); $prop->setAccessible(true); return $prop->getValue(null) ?? false; } private function setIncludeExternalStorage(bool $include) { $class = new \ReflectionClass(\OC_Helper::class); $prop = $class->getProperty('quotaIncludeExternalStorage'); $prop->setAccessible(true); $prop->setValue(null, $include); } /** * Test getting the storage info, ignoring extra mount points */ public function testGetStorageInfoExcludingExtStorage(): void { $homeStorage = $this->getStorageMock(12); Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); $extStorage = new Temporary([]); $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size $this->setIncludeExternalStorage(false); Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free']); $this->assertEquals(5, $storageInfo['used']); $this->assertEquals(17, $storageInfo['total']); } /** * Test getting the storage info, including extra mount points */ public function testGetStorageInfoIncludingExtStorage(): void { $homeStorage = new Temporary([]); Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); $extStorage = new Temporary([]); $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); $this->setIncludeExternalStorage(true); $config = Server::get(IConfig::class); $config->setUserValue($this->user, 'files', 'quota', '25'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(3, $storageInfo['free']); $this->assertEquals(22, $storageInfo['used']); $this->assertEquals(25, $storageInfo['total']); $config->setUserValue($this->user, 'files', 'quota', 'default'); } /** * Test getting the storage info excluding extra mount points * when user has no quota set, even when quota ext storage option * was set */ public function testGetStorageInfoIncludingExtStorageWithNoUserQuota(): void { $homeStorage = $this->getStorageMock(12); Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); $extStorage = new Temporary([]); $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); $config = Server::get(IConfig::class); $this->setIncludeExternalStorage(true); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage'); $this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used'); $this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage'); } /** * Test getting the storage info with quota enabled */ public function testGetStorageInfoWithQuota(): void { $homeStorage = $this->getStorageMock(12); $homeStorage->file_put_contents('test.txt', '01234'); $homeStorage = new Quota( [ 'storage' => $homeStorage, 'quota' => 7 ] ); Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(2, $storageInfo['free']); $this->assertEquals(5, $storageInfo['used']); $this->assertEquals(7, $storageInfo['total']); } /** * Test getting the storage info when data exceeds quota */ public function testGetStorageInfoWhenSizeExceedsQuota(): void { $homeStorage = $this->getStorageMock(12); $homeStorage->file_put_contents('test.txt', '0123456789'); $homeStorage = new Quota( [ 'storage' => $homeStorage, 'quota' => 7 ] ); Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(0, $storageInfo['free']); $this->assertEquals(10, $storageInfo['used']); // total = quota $this->assertEquals(7, $storageInfo['total']); } /** * Test getting the storage info when the remaining * free storage space is less than the quota */ public function testGetStorageInfoWhenFreeSpaceLessThanQuota(): void { $homeStorage = $this->getStorageMock(12); $homeStorage->file_put_contents('test.txt', '01234'); $homeStorage = new Quota( [ 'storage' => $homeStorage, 'quota' => 18 ] ); Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free']); $this->assertEquals(5, $storageInfo['used']); // total = free + used (because quota > total) $this->assertEquals(17, $storageInfo['total']); } }