Part-DB.Part-DB-server/tests/Controller/RedirectControllerTest.php

124 lines
3.8 KiB
PHP
Raw Normal View History

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).
*
* 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;
use Doctrine\ORM\EntityManagerInterface;
use Proxies\__CG__\App\Entity\UserSystem\User;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @group slow
* @group DB
*/
2019-10-20 14:35:19 +02:00
class RedirectControllerTest extends WebTestCase
{
protected $em;
protected $userRepo;
protected $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
{
$this->client = static::createClient([], [
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'test',
]);
$this->client->disableReboot();
$this->client->catchExceptions(false);
2019-10-20 14:35:19 +02:00
$this->em = self::$container->get(EntityManagerInterface::class);
$this->userRepo = $this->em->getRepository(User::class);
}
2020-08-21 22:43:37 +02:00
public function urlMatchDataProvider(): array
2019-10-20 14:35:19 +02:00
{
return [
['/', true],
['/part/2/info', true],
['/part/de/2', true],
['/part/en/', true],
['/de/', false],
['/de_DE/', false],
['/en/', false],
['/en_US/', false],
];
}
/**
* Test if a certain request to an url will be redirected.
*
2019-10-20 14:35:19 +02:00
* @dataProvider urlMatchDataProvider
* @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
{
//$client = static::createClient();
$this->client->request('GET', $url);
$response = $this->client->getResponse();
if ($expect_redirect) {
2020-01-05 15:55:16 +01:00
$this->assertSame(302, $response->getStatusCode());
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
}
2020-08-21 22:43:37 +02:00
public function urlAddLocaleDataProvider(): array
2019-10-20 14:35:19 +02:00
{
return [
//User locale, original target, redirect target
['de', '/', '/de/'],
['de', '/part/3', '/de/part/3'],
['en', '/', '/en/'],
['en', '/category/new', '/en/category/new'],
['en_US', '/part/3', '/en_US/part/3'],
//Without an explicit set value, the user should be redirect to english version
[null, '/', '/en/'],
['en_US', '/part/3', '/en_US/part/3'],
];
}
/**
* Test if the user is redirected to the localized version of a page, based on his settings.
*
2019-10-20 14:35:19 +02:00
* @dataProvider urlAddLocaleDataProvider
* @group slow
2019-10-20 14:35:19 +02:00
* @depends testUrlMatch
*
2019-10-20 14:35:19 +02:00
* @param $user_locale
* @param $input_path
* @param $redirect_path
*/
2020-01-05 15:55:16 +01:00
public function testAddLocale($user_locale, $input_path, $redirect_path): void
2019-10-20 14:35:19 +02:00
{
//Redirect path is absolute
$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();
$this->client->followRedirects(false);
$this->client->request('GET', $input_path);
2020-01-05 15:55:16 +01:00
$this->assertSame($redirect_path, $this->client->getResponse()->headers->get('Location'));
2019-10-20 14:35:19 +02:00
}
}