diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index 2357a63c6..982d7f878 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -47,7 +47,7 @@ public function __construct( #[PublicPage] #[NoCSRFRequired] #[BruteForceProtection('activityRssFeed')] - public function show(): TemplateResponse { + public function show(string $filter = 'all'): TemplateResponse { $response = new TemplateResponse('activity', 'rss', [], ''); try { $user = $this->activityManager->getCurrentUserId(); @@ -57,8 +57,9 @@ public function show(): TemplateResponse { $this->l = $this->l10nFactory->get('activity', $userLang); $this->helper->setL10n($this->l); + $filter = $this->data->validateFilter($filter); $description = $this->l->t('Personal activity feed for %s', $user); - $data = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all'); + $data = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', $filter); $activities = $data['data']; } catch (\UnexpectedValueException $e) { $this->l = $this->l10nFactory->get('activity'); diff --git a/lib/Data.php b/lib/Data.php index d19359696..b10278b82 100644 --- a/lib/Data.php +++ b/lib/Data.php @@ -615,15 +615,8 @@ public function countDownloads(string $user, int $objectId, ?int $since = null): /** * Verify that the filter is valid - * - * @param string $filterValue - * @return string */ - public function validateFilter($filterValue) { - if (!isset($filterValue)) { - return 'all'; - } - + public function validateFilter(string $filterValue): string { switch ($filterValue) { case 'filter': return $filterValue; diff --git a/tests/Controller/FeedControllerTest.php b/tests/Controller/FeedControllerTest.php index 2a4394016..5112b111c 100644 --- a/tests/Controller/FeedControllerTest.php +++ b/tests/Controller/FeedControllerTest.php @@ -96,6 +96,9 @@ public function testShow(?string $acceptHeader, string $expectedHeader): void { $this->data ->method('get') ->willReturn(['data' => []]); + $this->data + ->method('validateFilter') + ->willReturn('all'); if ($acceptHeader !== null) { $this->request ->method('getHeader') @@ -143,6 +146,54 @@ public function testShowNoToken(?string $acceptHeader, string $expectedHeader): $this->assertStringContainsString($description, $renderedResponse); } + public function testShowWithFilter(): void { + $this->mockUserSession('test'); + $this->data + ->method('validateFilter') + ->with('files') + ->willReturn('files'); + $this->data + ->expects($this->once()) + ->method('get') + ->with($this->helper, $this->userSettings, 'test', 0, FeedController::DEFAULT_PAGE_SIZE, 'desc', 'files') + ->willReturn(['data' => []]); + + $templateResponse = $this->controller->show('files'); + $this->assertInstanceOf(TemplateResponse::class, $templateResponse); + } + + public function testShowWithInvalidFilter(): void { + $this->mockUserSession('test'); + $this->data + ->method('validateFilter') + ->with('invalid_filter') + ->willReturn('all'); + $this->data + ->expects($this->once()) + ->method('get') + ->with($this->helper, $this->userSettings, 'test', 0, FeedController::DEFAULT_PAGE_SIZE, 'desc', 'all') + ->willReturn(['data' => []]); + + $templateResponse = $this->controller->show('invalid_filter'); + $this->assertInstanceOf(TemplateResponse::class, $templateResponse); + } + + public function testShowDefaultsToAllFilter(): void { + $this->mockUserSession('test'); + $this->data + ->method('validateFilter') + ->with('all') + ->willReturn('all'); + $this->data + ->expects($this->once()) + ->method('get') + ->with($this->helper, $this->userSettings, 'test', 0, FeedController::DEFAULT_PAGE_SIZE, 'desc', 'all') + ->willReturn(['data' => []]); + + $templateResponse = $this->controller->show(); + $this->assertInstanceOf(TemplateResponse::class, $templateResponse); + } + protected function mockUserSession(string $user): void { $mockUser = $this->createMock(IUser::class); $mockUser