From 79c39f7d5518aee3dea412c622489de2588aa01a 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 | 86 +++++++++++++++++++++++++ 2 files changed, 103 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..def7f50 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,9 @@ protected function setUp(): void { $this->l10nFactory->expects($this->atMost(1)) ->method('getLanguages') ->willReturn($this->mockAvailableLanguages); + + // Mock \OC_Helper::getStorageInfo + $this->mockStorageInfo(); } /** @@ -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 */ @@ -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(); + } }