Skip to content

Commit 0a025ee

Browse files
committedJan 2, 2025·
bug #6661 Fix for issue #6519 (Pretty URLs in tests) (Matthieu Renard)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Fix for issue #6519 (Pretty URLs in tests) When using pretty URLs in tests, the AdminUrlGenerator fails to provide the pretty URLs because it checks the use of pretty URLs through the context, which is null when doing requests from tests. This fixes this behaviour by checking the use of pretty URLs directly from the UrlGenerator (Router), which is what is done by the context anyway. Tests have been adapted to use pretty URLs. <!-- Thanks for your contribution! If you are proposing a new feature that is complex, please open an issue first so we can discuss about it. Note: all your contributions adhere implicitly to the MIT license --> Commits ------- 8fdbca1 Fix for issue #6519 (Pretty URLs in tests)
2 parents 4eb8174 + 8fdbca1 commit 0a025ee

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed
 

‎.ddev/example/Controller/Admin/DashboardController.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
88
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
99
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
10-
use Symfony\Component\ExpressionLanguage\Expression;
1110
use Symfony\Component\HttpFoundation\Response;
12-
use Symfony\Component\Routing\Annotation\Route;
11+
use Symfony\Component\Routing\Attribute\Route;
1312

1413
class DashboardController extends AbstractDashboardController
1514
{

‎src/Router/AdminUrlGenerator.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ public function generateUrl(): string
222222
$this->initialize();
223223
}
224224

225+
$usePrettyUrls = $this->adminRouteGenerator->usesPrettyUrls();
226+
225227
if (true === $this->includeReferrer) {
226228
$this->setRouteParameter(EA::REFERRER, $this->customPageReferrer ?? $this->currentPageReferrer);
227229
}
@@ -239,7 +241,9 @@ public function generateUrl(): string
239241
}
240242

241243
$this->dashboardRoute = $dashboardRoute;
242-
$this->unset(EA::DASHBOARD_CONTROLLER_FQCN);
244+
if (!$usePrettyUrls) {
245+
$this->unset(EA::DASHBOARD_CONTROLLER_FQCN);
246+
}
243247
}
244248

245249
// if the current action is 'index' and an entity ID is defined, remove the entity ID to prevent exceptions automatically
@@ -276,20 +280,22 @@ public function generateUrl(): string
276280
}
277281

278282
$context = $this->adminContextProvider->getContext();
279-
$usePrettyUrls = null !== $context && $context->usePrettyUrls();
280283
$urlType = null !== $context && false === $context->getAbsoluteUrls() ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL;
281284

282285
if (null !== $this->get(EA::ROUTE_NAME)) {
283286
return $this->urlGenerator->generate($this->dashboardRoute, $routeParameters, $urlType);
284287
}
285288

286289
if ($usePrettyUrls) {
287-
$dashboardControllerFqcn = $this->get(EA::DASHBOARD_CONTROLLER_FQCN) ?? $context->getRequest()->attributes->get(EA::DASHBOARD_CONTROLLER_FQCN) ?? $this->dashboardControllerRegistry->getFirstDashboardFqcn();
288-
$crudControllerFqcn = $this->get(EA::CRUD_CONTROLLER_FQCN) ?? $context->getRequest()->attributes->get(EA::CRUD_CONTROLLER_FQCN);
289-
$actionName = $this->get(EA::CRUD_ACTION) ?? $context->getRequest()->attributes->get(EA::CRUD_ACTION);
290+
$dashboardControllerFqcn = $this->get(EA::DASHBOARD_CONTROLLER_FQCN) ?? $context?->getRequest()->attributes->get(EA::DASHBOARD_CONTROLLER_FQCN) ?? $this->dashboardControllerRegistry->getFirstDashboardFqcn();
291+
$crudControllerFqcn = $this->get(EA::CRUD_CONTROLLER_FQCN) ?? $context?->getRequest()->attributes->get(EA::CRUD_CONTROLLER_FQCN);
292+
$actionName = $this->get(EA::CRUD_ACTION) ?? $context?->getRequest()->attributes->get(EA::CRUD_ACTION);
290293

291294
if (null === $crudControllerFqcn || null === $routeName = $this->adminRouteGenerator->findRouteName($dashboardControllerFqcn, $crudControllerFqcn, $actionName)) {
292295
$routeName = $this->dashboardRoute;
296+
if (null === $crudControllerFqcn) {
297+
unset($routeParameters[EA::DASHBOARD_CONTROLLER_FQCN]);
298+
}
293299
} else {
294300
// remove these parameters so they don't appear in the query string when using pretty URLs
295301
unset($routeParameters[EA::DASHBOARD_CONTROLLER_FQCN]);

‎tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller\PrettyUrls;
44

5+
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
6+
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
7+
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\BlogPostCrudController;
8+
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\DashboardController;
59
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Kernel;
610
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
711

@@ -256,6 +260,24 @@ public function testCustomSortLinksUsePrettyUrls()
256260
$this->assertSame('http://localhost/second/dashboard/user-editor/custom/path-for-index?page=1&sort%5Bemail%5D=DESC', $crawler->filter('th.searchable a')->eq(2)->attr('href'));
257261
}
258262

263+
public function testAdminUrlGeneratorUsePrettyUrls()
264+
{
265+
self::bootKernel();
266+
$container = static::getContainer();
267+
$adminUrlGenerator = $container->get(AdminUrlGenerator::class);
268+
269+
$this->assertInstanceOf(AdminUrlGenerator::class, $adminUrlGenerator);
270+
271+
$blogPostIndexUrl = $adminUrlGenerator
272+
->setDashboard(DashboardController::class)
273+
->setController(BlogPostCrudController::class)
274+
->setAction(Action::INDEX)
275+
->generateUrl()
276+
;
277+
278+
$this->assertSame('http://localhost/admin/pretty/urls/blog-post', $blogPostIndexUrl);
279+
}
280+
259281
public static function provideActiveMenuUrls(): iterable
260282
{
261283
yield ['/admin/pretty/urls/category'];

0 commit comments

Comments
 (0)
Please sign in to comment.