From ab24db46fade5e19e85ae886d2c05a702446fadc 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 | 72 +++++++++++++++++++++++++ 2 files changed, 89 insertions(+) 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..709a962 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, @@ -183,6 +187,22 @@ protected function setUp(): void { $this->l10nFactory->expects($this->atMost(1)) ->method('getLanguages') ->willReturn($this->mockAvailableLanguages); + + // Mock the \OC_Helper::getStorageInfo method + \OCP\Util::mockStatic(\OC_Helper::class, 'getStorageInfo', function () { + return [ + 'quota' => FileInfo::SPACE_UNLIMITED, + 'total' => 1000000000, + 'free' => 500000000, + 'used' => 500000000, + 'relative' => 50, + ]; + }); + + // Mock the \OC_Helper::humanFileSize method + \OCP\Util::mockStatic(\OC_Helper::class, 'humanFileSize', function ($size) { + return $size . ' B'; + }); } /** @@ -360,4 +380,56 @@ public function testIndexProvidesInitialStateWithCustomClientURLs() { $this->controller->index(); } + + public function testIndexProvidesInitialStateWithUnlimitedTotalSpace() { + $expectedStorageInfo = [ + 'quota' => FileInfo::SPACE_UNLIMITED, + 'totalSpace' => 'Unlimited', + 'freeSpace' => '500000000 B', + 'usage' => '500000000 B', + 'usageRelative' => 50, + ]; + + $this->storageInfo['quota'] = FileInfo::SPACE_UNLIMITED; + $this->storageInfo['total'] = 1000000000; + + \OC_Helper::method('getStorageInfo') + ->willReturn($this->storageInfo); + + $this->initialState->expects($this->exactly(4)) + ->method('provideInitialState') + ->willReturnCallback(function ($stateName, $stateValue) use ($expectedStorageInfo) { + if ($stateName == "personalInfoParameters") { + $this->assertEquals($expectedStorageInfo, $stateValue['storageInfo']); + } + }); + + $this->controller->index(); + } + + public function testIndexProvidesInitialStateWithLimitedTotalSpace() { + $expectedStorageInfo = [ + 'quota' => 1000000000, + 'totalSpace' => '1000000000 B', + 'freeSpace' => '500000000 B', + 'usage' => '500000000 B', + 'usageRelative' => 50, + ]; + + $this->storageInfo['quota'] = 1000000000; + $this->storageInfo['total'] = 1000000000; + + \OC_Helper::method('getStorageInfo') + ->willReturn($this->storageInfo); + + $this->initialState->expects($this->exactly(4)) + ->method('provideInitialState') + ->willReturnCallback(function ($stateName, $stateValue) use ($expectedStorageInfo) { + if ($stateName == "personalInfoParameters") { + $this->assertEquals($expectedStorageInfo, $stateValue['storageInfo']); + } + }); + + $this->controller->index(); + } }