From 24137b30a5ff79ebba874c31578f843055f552e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 7 Jan 2025 14:58:26 +0100 Subject: [PATCH] Added tests for RedirectController that simulates the situation of a reverse proxy and subdirectory --- tests/Controller/RedirectControllerTest.php | 58 ++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/Controller/RedirectControllerTest.php b/tests/Controller/RedirectControllerTest.php index 029c93f58..f62c05c34 100644 --- a/tests/Controller/RedirectControllerTest.php +++ b/tests/Controller/RedirectControllerTest.php @@ -115,6 +115,62 @@ public function testAddLocale(?string $user_locale, string $input_path, string $ $this->client->followRedirects(false); $this->client->request('GET', $input_path); - $this->assertResponseRedirects($redirect_path); + self::assertResponseRedirects($redirect_path); } + + /** + * Test if the user is redirected to the localized version of a page, based on his settings. + * We simulate the situation of a reverse proxy here, by adding a prefix to the path. + * + * @dataProvider urlAddLocaleDataProvider + * @group slow + */ + public function testAddLocaleReverseProxy(?string $user_locale, string $input_path, string $redirect_path): void + { + //Input path remains unchanged, as this is what the server receives from the proxy + + //Redirect path must contain the proxy prefix + $redirect_path = 'http://localhost'. '/proxy' . $redirect_path; + + /** @var User $user */ + $user = $this->userRepo->findOneBy(['name' => 'user']); + //Set user locale + $user->setLanguage($user_locale); + $this->em->flush(); + + $this->client->followRedirects(false); + $this->client->request('GET', $input_path, [], [], ['HTTP_X_FORWARDED_PREFIX' => '/proxy']); + self::assertResponseRedirects($redirect_path); + } + + + /** + * Test if the user is redirected to the localized version of a page, based on his settings. + * We simulate the situation of serving Part-DB in a subfolder here. + * + * @dataProvider urlAddLocaleDataProvider + * @group slow + */ + public function testAddLocaleSubfolder(?string $user_locale, string $input_path, string $redirect_path): void + { + //Prefix our path with the proxy prefix + $input_path = '/folder'.$input_path; + + //Redirect path is absolute + $redirect_path = 'http://localhost'. '/folder' . $redirect_path; + + /** @var User $user */ + $user = $this->userRepo->findOneBy(['name' => 'user']); + //Set user locale + $user->setLanguage($user_locale); + $this->em->flush(); + + $this->client->followRedirects(false); + $this->client->request('GET', $input_path, [], [], [ + 'SCRIPT_FILENAME' => '/var/www/html/folder/public/index.php', + 'PHP_SELF' => '/folder/index.php', + ]); + self::assertResponseRedirects($redirect_path); + } + }