From 4e84489ba71e50af690eec99cefb39c862591ef1 Mon Sep 17 00:00:00 2001 From: Franziska Bath Date: Mon, 9 Sep 2024 16:14:52 +0200 Subject: [PATCH] feat(quota): provide storage info to PageController Signed-off-by: Franziska Bath --- lib/Controller/PageController.php | 17 +++++++ tests/Controller/PageControllerTest.php | 59 ++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index dc48e14..d9ee195 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -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; @@ -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; @@ -59,6 +62,7 @@ public function __construct( IConfig $config, IUserManager $userManager, IFactory $l10nFactory, + IL10N $l, IProvider $tokenProvider, ISession $session, IInitialState $initialState, @@ -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; @@ -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']), ] ); diff --git a/tests/Controller/PageControllerTest.php b/tests/Controller/PageControllerTest.php index fbd5a6d..d600a23 100644 --- a/tests/Controller/PageControllerTest.php +++ b/tests/Controller/PageControllerTest.php @@ -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; @@ -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); @@ -55,6 +58,7 @@ protected function setUp(): void { $this->config, $this->userManager, $this->l10nFactory, + $this->l, $this->tokenProvider, $this->session, $this->initialState, @@ -205,7 +209,6 @@ private function createMockAppToken(int $id, array $serializedJson = []): IToken return $token; } - /** * @throws Exception */ @@ -360,4 +363,58 @@ 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') + ->withConsecutive( + ['app_tokens', $this->anything()], + ['can_create_app_token', true], + ['personalInfoParameters', [ + 'quota' => $expectedStorageInfo['quota'], + 'totalSpace' => 'Unlimited', + 'freeSpace' => \OC_Helper::humanFileSize($expectedStorageInfo['free']), + 'usage' => \OC_Helper::humanFileSize($expectedStorageInfo['used']), + 'usageRelative' => round($expectedStorageInfo['relative']), + ]], + ['customClientURL', $this->anything()] + ); + + $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') + ->withConsecutive( + ['app_tokens', $this->anything()], + ['can_create_app_token', true], + ['personalInfoParameters', [ + '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']), + ]], + ['customClientURL', $this->anything()] + ); + + $this->controller->index(); + } }