diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index dc48e14..1233ffc 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -25,6 +25,7 @@ use OC\Authentication\Token\INamedToken; use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; +use OC_Helper; use OCA\SimpleSettings\AppInfo\Application; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Attribute\FrontpageRoute; @@ -34,7 +35,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,30 +52,36 @@ class PageController extends Controller { private IConfig $config; private IUserManager $userManager; private IFactory $l10nFactory; + private $l; private IProvider $tokenProvider; private IInitialState $initialState; private IUserSession $userSession; private ISession $session; private string|null $uid; + private OC_Helper $helper; public function __construct( IConfig $config, IUserManager $userManager, IFactory $l10nFactory, + IL10N $l, IProvider $tokenProvider, ISession $session, IInitialState $initialState, IUserSession $userSession, - ?string $UserId + ?string $UserId, + OC_Helper $helper ) { $this->config = $config; $this->userManager = $userManager; $this->l10nFactory = $l10nFactory; + $this->l = $l; $this->tokenProvider = $tokenProvider; $this->session = $session; $this->initialState = $initialState; $this->userSession = $userSession; $this->uid = $UserId; + $this->helper = $helper; } #[NoCSRFRequired] @@ -92,10 +101,22 @@ public function index(): TemplateResponse { $user = $this->userManager->get($this->uid); + $storageInfo = $this->helper::getStorageInfo('/'); + if ($storageInfo['quota'] !== FileInfo::SPACE_UNLIMITED) { + $totalSpace = $this->l->t('Unlimited'); + } else { + $totalSpace = $this->helper::humanFileSize($storageInfo['total']); + } + $this->initialState->provideInitialState( 'personalInfoParameters', [ 'languageMap' => $this->getLanguageMap($user), + 'quota' => $storageInfo['quota'], + 'totalSpace' => $totalSpace, + 'freeSpace' => $this->helper::humanFileSize($storageInfo['free']), + 'usage' => $this->helper::humanFileSize($storageInfo['used']), + 'usageRelative' => round($storageInfo['relative']), ] ); diff --git a/tests/Controller/PageControllerTest.php b/tests/Controller/PageControllerTest.php index fbd5a6d..b665f2c 100644 --- a/tests/Controller/PageControllerTest.php +++ b/tests/Controller/PageControllerTest.php @@ -25,8 +25,11 @@ use OC\Authentication\Token\INamedToken; use OC\Authentication\Token\IProvider as IAuthTokenProvider; use OC\Authentication\Token\IToken; +use OC_Helper; 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,21 +48,25 @@ 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); $this->userSession = $this->createMock(IUserSession::class); $this->uid = "mock-user-id-123"; + $this->helper = $this->createMock(OC_Helper::class); $this->controller = new PageController( $this->config, $this->userManager, $this->l10nFactory, + $this->l, $this->tokenProvider, $this->session, $this->initialState, $this->userSession, - $this->uid + $this->uid, + $this->helper ); $mockCurrentSessionId = "mock-session-id-123"; @@ -360,4 +367,68 @@ 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(1)) + ->method('provideInitialState') + ->with('app_tokens', $this->anything()); + + $this->initialState->expects($this->exactly(1)) + ->method('provideInitialState') + ->with('can_create_app_token', true); + + $this->initialState->expects($this->exactly(1)) + ->method('provideInitialState') + ->with('personalInfoParameters', [ + 'languageMap' => $this->anything(), + 'quota' => $expectedStorageInfo['quota'], + 'totalSpace' => $this->l->t('Unlimited'), + 'usageRelative' => round($expectedStorageInfo['relative']), + ]); + + $this->initialState->expects($this->exactly(1)) + ->method('provideInitialState') + ->with('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(1)) + ->method('provideInitialState') + ->with('app_tokens', $this->anything()); + + $this->initialState->expects($this->exactly(1)) + ->method('provideInitialState') + ->with('can_create_app_token', true); + + $this->initialState->expects($this->exactly(1)) + ->method('provideInitialState') + ->with('personalInfoParameters', [ + 'quota' => $expectedStorageInfo['quota'], + 'usageRelative' => round($expectedStorageInfo['relative']), + ]); + + $this->initialState->expects($this->exactly(1)) + ->method('provideInitialState') + ->with('customClientURL', $this->anything()); + + $this->controller->index(); + } }