Skip to content

Commit

Permalink
feat(quota): provide storage info to PageController
Browse files Browse the repository at this point in the history
Signed-off-by: Franziska Bath <[email protected]>
  • Loading branch information
fracado committed Sep 9, 2024
1 parent 49ec2fd commit 79c39f7
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -49,6 +51,7 @@ class PageController extends Controller {
private IConfig $config;
private IUserManager $userManager;
private IFactory $l10nFactory;
private $l;
private IProvider $tokenProvider;
private IInitialState $initialState;
private IUserSession $userSession;
Expand All @@ -59,6 +62,7 @@ public function __construct(
IConfig $config,
IUserManager $userManager,
IFactory $l10nFactory,
IL10N $l,
IProvider $tokenProvider,
ISession $session,
IInitialState $initialState,
Expand All @@ -68,6 +72,7 @@ public function __construct(
$this->config = $config;
$this->userManager = $userManager;
$this->l10nFactory = $l10nFactory;
$this->l = $l;
$this->tokenProvider = $tokenProvider;
$this->session = $session;
$this->initialState = $initialState;
Expand All @@ -92,10 +97,22 @@ public function index(): TemplateResponse {

$user = $this->userManager->get($this->uid);

$storageInfo = \OC_Helper::getStorageInfo('/');
if ($storageInfo['quota'] !== FileInfo::SPACE_UNLIMITED) {
$totalSpace = $this->l->t('Unlimited');
} else {
$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
}

$this->initialState->provideInitialState(
'personalInfoParameters',
[
'languageMap' => $this->getLanguageMap($user),
'quota' => $storageInfo['quota'],
'totalSpace' => $totalSpace,
'freeSpace' => \OC_Helper::humanFileSize($storageInfo['free']),
'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
'usageRelative' => round($storageInfo['relative']),
]
);

Expand Down
86 changes: 86 additions & 0 deletions tests/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OC\Authentication\Token\IProvider as IAuthTokenProvider;
use OC\Authentication\Token\IToken;
use OCP\AppFramework\Services\IInitialState;
use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -45,6 +47,7 @@ protected function setUp(): void {
$this->config = $this->createMock(IConfig::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->l = $this->createMock(IL10N::class);
$this->tokenProvider = $this->createMock(IAuthTokenProvider::class);
$this->session = $this->createMock(ISession::class);
$this->initialState = $this->createMock(IInitialState::class);
Expand All @@ -55,6 +58,7 @@ protected function setUp(): void {
$this->config,
$this->userManager,
$this->l10nFactory,
$this->l,
$this->tokenProvider,
$this->session,
$this->initialState,
Expand Down Expand Up @@ -183,6 +187,9 @@ protected function setUp(): void {
$this->l10nFactory->expects($this->atMost(1))
->method('getLanguages')
->willReturn($this->mockAvailableLanguages);

// Mock \OC_Helper::getStorageInfo
$this->mockStorageInfo();
}

/**
Expand All @@ -206,6 +213,33 @@ private function createMockAppToken(int $id, array $serializedJson = []): IToken
return $token;
}

private function mockStorageInfo(): void {
$expectedStorageInfo = [
'quota' => FileInfo::SPACE_UNLIMITED,
'total' => 1000000000,
'free' => 500000000,
'used' => 500000000,
'relative' => 50,
];

$this->mockStaticMethod('\OC_Helper', 'getStorageInfo', $expectedStorageInfo);
$this->mockStaticMethod('\OC_Helper', 'humanFileSize', fn ($size) => $size . ' B');
}

private function mockStaticMethod(string $class, string $method, $returnValue): void {
$mock = $this->getMockBuilder($class)
->disableOriginalConstructor()
->onlyMethods([$method])
->getMock();

$mock->method($method)->willReturn($returnValue);

$reflection = new \ReflectionClass($class);
$property = $reflection->getProperty('instance');
$property->setAccessible(true);
$property->setValue(null, $mock);
}

/**
* @throws Exception
*/
Expand Down Expand Up @@ -360,4 +394,56 @@ public function testIndexProvidesInitialStateWithCustomClientURLs() {

$this->controller->index();
}

public function testIndexProvidesInitialStateWithUnlimitedTotalSpace() {
$expectedStorageInfo = [
'quota' => FileInfo::SPACE_UNLIMITED,
'total' => 1000000000,
'free' => 500000000,
'used' => 500000000,
'relative' => 50,
];

$this->initialState->expects($this->exactly(4))
->method('provideInitialState')
->willReturnCallback(function ($stateName, $stateValue) use ($expectedStorageInfo) {
if ($stateName == "personalInfoParameters") {
$this->assertEquals([
'quota' => $expectedStorageInfo['quota'],
'totalSpace' => 'Unlimited',
'freeSpace' => \OC_Helper::humanFileSize($expectedStorageInfo['free']),
'usage' => \OC_Helper::humanFileSize($expectedStorageInfo['used']),
'usageRelative' => round($expectedStorageInfo['relative']),
], $stateValue);
}
});

$this->controller->index();
}

public function testIndexProvidesInitialStateWithLimitedTotalSpace() {
$expectedStorageInfo = [
'quota' => 1000000000,
'total' => 1000000000,
'free' => 500000000,
'used' => 500000000,
'relative' => 50,
];

$this->initialState->expects($this->exactly(4))
->method('provideInitialState')
->willReturnCallback(function ($stateName, $stateValue) use ($expectedStorageInfo) {
if ($stateName == "personalInfoParameters") {
$this->assertEquals([
'quota' => $expectedStorageInfo['quota'],
'totalSpace' => \OC_Helper::humanFileSize($expectedStorageInfo['total']),
'freeSpace' => \OC_Helper::humanFileSize($expectedStorageInfo['free']),
'usage' => \OC_Helper::humanFileSize($expectedStorageInfo['used']),
'usageRelative' => round($expectedStorageInfo['relative']),
], $stateValue);
}
});

$this->controller->index();
}
}

0 comments on commit 79c39f7

Please sign in to comment.