Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@
->arg(2, service('cache.easyadmin'))
->arg(3, service('filesystem'))
->arg(4, '%kernel.build_dir%')
->arg(5, '%kernel.default_locale%')
->arg(6, tagged_iterator(EasyAdminExtension::TAG_ADMIN_ROUTE_CONTROLLER))
->arg(5, tagged_iterator(EasyAdminExtension::TAG_ADMIN_ROUTE_CONTROLLER))

->set(AdminRouteLoader::class)
->arg(0, service(AdminRouteGenerator::class))
Expand Down
2 changes: 0 additions & 2 deletions src/Router/AdminRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function __construct(
private readonly CacheItemPoolInterface $cache,
private readonly Filesystem $filesystem,
private readonly string $buildDir,
private readonly string $defaultLocale,
private readonly iterable $adminRouteControllers = [],
) {
}
Expand Down Expand Up @@ -210,7 +209,6 @@ private function generateAdminRoutes(): array
}

$defaults = [
'_locale' => $this->defaultLocale,
'_controller' => $crudControllerFqcn.'::'.$actionRouteConfig['actionName'],
EA::ROUTE_CREATED_BY_EASYADMIN => true,
EA::DASHBOARD_CONTROLLER_FQCN => $dashboardFqcn,
Expand Down
18 changes: 18 additions & 0 deletions tests/Functional/AdminRoute/AdminRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,24 @@ public function testLegacyAttributesRoutesAreAccessible(): void
$this->assertSame('Legacy export action', $client->getResponse()->getContent());
}

public function testCrudRoutesDoNotSetLocaleDefault(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');

// CRUD routes should NOT have '_locale' in defaults (regression test for #6842)
$crudRoute = $router->getRouteCollection()->get('admin_built_in_action_list');
$this->assertNotNull($crudRoute);
$this->assertArrayNotHasKey('_locale', $crudRoute->getDefaults(),
'CRUD routes must not set _locale in defaults, otherwise i18n prefix routing breaks');

// but routes with explicit '_locale' via #[AdminRoute] options should still have it
$invokableRoute = $router->getRouteCollection()->get('admin_custom_invokable');
$this->assertNotNull($invokableRoute);
$this->assertSame('en', $invokableRoute->getDefault('_locale'),
'Routes with explicit _locale in #[AdminRoute] options should keep it');
}

public function testLegacyLinkToCrudMenuItemStillWorks(): void
{
$menuItem = MenuItem::linkToCrud('Products', 'fas fa-box', Product::class);
Expand Down
104 changes: 104 additions & 0 deletions tests/Functional/AdminRoute/I18nAdminRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\AdminRoute;

use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\I18nKernel;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Filesystem\Filesystem;

/**
* @group legacy
*/
class I18nAdminRouteTest extends WebTestCase
{
private const LOCALES = ['en', 'ja', 'es'];

protected static function getKernelClass(): string
{
return I18nKernel::class;
}

protected function setUp(): void
{
if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '5.4.1', '<')) {
$this->markTestSkipped('AdminRoute attributes require Symfony 5.4.1 or higher');
}

parent::setUp();

$client = static::createClient();

$buildDir = $client->getKernel()->getContainer()->getParameter('kernel.build_dir');
$filesystem = new Filesystem();
$filesystem->touch($buildDir.'/easyadmin_pretty_urls_enabled');

self::ensureKernelShutdown();
}

protected function tearDown(): void
{
$filesystem = new Filesystem();
$cacheDir = sys_get_temp_dir().'/com.github.easycorp.easyadmin/tests/admin_route_i18n/var/test/cache';
$filesystem->remove($cacheDir.'/easyadmin_pretty_urls_enabled');

parent::tearDown();
}

public function testDashboardRoutesHaveLocaleVariants(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
$routes = $router->getRouteCollection();

foreach (['admin', 'second_admin'] as $dashboard) {
foreach (self::LOCALES as $locale) {
$routeName = $dashboard.'.'.$locale;
$route = $routes->get($routeName);

$this->assertNotNull($route, sprintf('Route "%s" should exist', $routeName));
$this->assertSame($locale, $route->getDefault('_locale'),
sprintf('Route "%s" should have _locale default set to "%s"', $routeName, $locale));

$expectedPrefix = '/'.$locale.'/';
$this->assertStringStartsWith($expectedPrefix, $route->getPath(),
sprintf('Route "%s" path should start with "%s"', $routeName, $expectedPrefix));
}
}
}

public function testCrudRoutesHaveLocaleVariants(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
$routes = $router->getRouteCollection();

$baseRouteName = 'admin_built_in_action_list';

foreach (self::LOCALES as $locale) {
$routeName = $baseRouteName.'.'.$locale;
$route = $routes->get($routeName);

$this->assertNotNull($route, sprintf('Route "%s" should exist', $routeName));
$this->assertSame($locale, $route->getDefault('_locale'),
sprintf('Route "%s" should have _locale default set to "%s"', $routeName, $locale));

$expectedPath = '/'.$locale.'/admin/built-in-action/index';
$this->assertSame($expectedPath, $route->getPath(),
sprintf('Route "%s" should have path "%s"', $routeName, $expectedPath));
}
}

public function testLocaleRoutesAreAccessible(): void
{
$client = static::createClient();

foreach (self::LOCALES as $locale) {
$client->request('GET', '/'.$locale.'/admin/foo/list');

$this->assertResponseIsSuccessful(
sprintf('Request to "/%s/admin/foo/list" should be successful', $locale)
);
$this->assertSame('Foo List', $client->getResponse()->getContent());
}
}
}
8 changes: 8 additions & 0 deletions tests/Functional/Apps/AdminRouteApp/config/i18n_routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return static function (RoutingConfigurator $routes) {
$routes->import('.', 'easyadmin.routes')
->prefix(['en' => '/en', 'ja' => '/ja', 'es' => '/es']);
};
23 changes: 23 additions & 0 deletions tests/Functional/Apps/AdminRouteApp/src/I18nKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp;

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

class I18nKernel extends Kernel
{
public function getCacheDir(): string
{
return sys_get_temp_dir().'/com.github.easycorp.easyadmin/tests/admin_route_i18n/var/'.$this->environment.'/cache';
}

public function getLogDir(): string
{
return sys_get_temp_dir().'/com.github.easycorp.easyadmin/tests/admin_route_i18n/var/'.$this->environment.'/log';
}

protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->import($this->getProjectDir().'/config/i18n_routes.php');
}
}
1 change: 0 additions & 1 deletion tests/Unit/Router/AdminRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function testFindRoute(?string $dashboardControllerFqcn, ?string $crudCon
$cacheMock,
new Filesystem(),
self::$kernel->getBuildDir(),
'en',
);

$routeName = $adminRouteGenerator->findRouteName($dashboardControllerFqcn, $crudControllerFqcn, $action);
Expand Down
Loading