2019-10-20 14:35:19 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:37:33 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-10-20 14:35:19 +02:00
|
|
|
namespace App\Tests\Controller;
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
2022-12-18 19:45:04 +01:00
|
|
|
use App\Entity\UserSystem\User;
|
2023-04-15 22:05:29 +02:00
|
|
|
use App\Repository\UserRepository;
|
2019-10-20 14:35:19 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
|
2019-10-31 22:37:54 +01:00
|
|
|
/**
|
|
|
|
* @group slow
|
2019-12-27 15:20:06 +01:00
|
|
|
* @group DB
|
2019-10-31 22:37:54 +01:00
|
|
|
*/
|
2019-10-20 14:35:19 +02:00
|
|
|
class RedirectControllerTest extends WebTestCase
|
|
|
|
{
|
2023-04-15 22:05:29 +02:00
|
|
|
protected EntityManagerInterface $em;
|
|
|
|
protected UserRepository $userRepo;
|
2023-06-11 14:55:06 +02:00
|
|
|
protected KernelBrowser $client;
|
2019-10-20 14:35:19 +02:00
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
protected function setUp(): void
|
2019-10-20 14:35:19 +02:00
|
|
|
{
|
2019-11-23 15:03:08 +01:00
|
|
|
$this->client = static::createClient([], [
|
|
|
|
'PHP_AUTH_USER' => 'user',
|
|
|
|
'PHP_AUTH_PW' => 'test',
|
|
|
|
]);
|
|
|
|
$this->client->disableReboot();
|
2020-03-29 18:24:10 +02:00
|
|
|
$this->client->catchExceptions(false);
|
2022-12-18 19:45:04 +01:00
|
|
|
$this->em = self::getContainer()->get(EntityManagerInterface::class);
|
2019-10-20 14:35:19 +02:00
|
|
|
$this->userRepo = $this->em->getRepository(User::class);
|
|
|
|
}
|
|
|
|
|
2024-06-22 00:37:47 +02:00
|
|
|
public function urlMatchDataProvider(): \Iterator
|
2019-10-20 14:35:19 +02:00
|
|
|
{
|
2024-06-22 00:37:47 +02:00
|
|
|
yield ['/', true];
|
|
|
|
yield ['/part/2/info', true];
|
|
|
|
yield ['/part/de/2', true];
|
|
|
|
yield ['/part/en/', true];
|
|
|
|
yield ['/de/', false];
|
|
|
|
yield ['/de_DE/', false];
|
|
|
|
yield ['/en/', false];
|
|
|
|
yield ['/en_US/', false];
|
2019-10-20 14:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a certain request to an url will be redirected.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-10-20 14:35:19 +02:00
|
|
|
* @dataProvider urlMatchDataProvider
|
2019-10-31 22:37:54 +01:00
|
|
|
* @group slow
|
2019-10-20 14:35:19 +02:00
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testUrlMatch($url, $expect_redirect): void
|
2019-10-20 14:35:19 +02:00
|
|
|
{
|
2019-11-23 15:03:08 +01:00
|
|
|
//$client = static::createClient();
|
|
|
|
$this->client->request('GET', $url);
|
|
|
|
$response = $this->client->getResponse();
|
2019-11-09 00:47:20 +01:00
|
|
|
if ($expect_redirect) {
|
2023-06-11 14:18:53 +02:00
|
|
|
$this->assertResponseStatusCodeSame(302);
|
2019-10-20 14:35:19 +02:00
|
|
|
}
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expect_redirect, $response->isRedirect());
|
2019-10-20 14:35:19 +02:00
|
|
|
}
|
|
|
|
|
2024-06-22 00:37:47 +02:00
|
|
|
public function urlAddLocaleDataProvider(): \Iterator
|
2019-10-20 14:35:19 +02:00
|
|
|
{
|
2024-06-22 00:37:47 +02:00
|
|
|
//User locale, original target, redirect target
|
|
|
|
yield ['de', '/', '/de/'];
|
|
|
|
yield ['de', '/part/3', '/de/part/3'];
|
|
|
|
yield ['en', '/', '/en/'];
|
|
|
|
yield ['en', '/category/new', '/en/category/new'];
|
|
|
|
yield ['en_US', '/part/3', '/en_US/part/3'];
|
|
|
|
//Without an explicit set value, the user should be redirected to english version
|
|
|
|
yield [null, '/', '/en/'];
|
|
|
|
yield ['en_US', '/part/3', '/en_US/part/3'];
|
|
|
|
//Test that query parameters work
|
|
|
|
yield ['de', '/dialog?target_id=133&target_type=part', '/de/dialog?target_id=133&target_type=part'];
|
|
|
|
yield ['en', '/dialog?storelocation=1', '/en/dialog?storelocation=1'];
|
2019-10-20 14:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the user is redirected to the localized version of a page, based on his settings.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-10-20 14:35:19 +02:00
|
|
|
* @dataProvider urlAddLocaleDataProvider
|
2019-10-31 22:37:54 +01:00
|
|
|
* @group slow
|
2023-04-15 00:38:11 +02:00
|
|
|
* @depends testUrlMatch
|
2019-10-20 14:35:19 +02:00
|
|
|
*/
|
2023-04-15 00:38:11 +02:00
|
|
|
public function testAddLocale(?string $user_locale, string $input_path, string $redirect_path): void
|
2019-10-20 14:35:19 +02:00
|
|
|
{
|
|
|
|
//Redirect path is absolute
|
2019-11-09 00:47:20 +01:00
|
|
|
$redirect_path = 'http://localhost'.$redirect_path;
|
2019-10-20 14:35:19 +02:00
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = $this->userRepo->findOneBy(['name' => 'user']);
|
|
|
|
//Set user locale
|
|
|
|
$user->setLanguage($user_locale);
|
|
|
|
$this->em->flush();
|
|
|
|
|
2019-11-23 15:03:08 +01:00
|
|
|
$this->client->followRedirects(false);
|
|
|
|
$this->client->request('GET', $input_path);
|
2023-06-11 14:18:53 +02:00
|
|
|
$this->assertResponseRedirects($redirect_path);
|
2019-10-20 14:35:19 +02:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|