Skip to content

Commit 533e89a

Browse files
committed
Add some tests
1 parent cfeb05b commit 533e89a

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

src/Router/AdminUrlGenerator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,13 @@ private function initialize(): void
364364
$this->currentPageReferrer = null;
365365
} else {
366366
$this->dashboardRoute = $adminContext->getDashboardRouteName();
367-
$routeParams = $adminContext->getRequest()->attributes->get('_route_params', []);
368-
unset($routeParams[EA::ROUTE_CREATED_BY_EASYADMIN]);
369-
$currentRouteParameters = $routeParametersForReferrer = array_merge($routeParams, $adminContext->getRequest()->query->all());
367+
$routeParameters = array_filter([
368+
EA::DASHBOARD_CONTROLLER_FQCN => $adminContext->getRequest()->attributes->get(EA::DASHBOARD_CONTROLLER_FQCN),
369+
EA::CRUD_CONTROLLER_FQCN => $adminContext->getRequest()->attributes->get(EA::CRUD_CONTROLLER_FQCN),
370+
EA::CRUD_ACTION => $adminContext->getRequest()->attributes->get(EA::CRUD_ACTION),
371+
EA::ENTITY_ID => $adminContext->getRequest()->attributes->get(EA::ENTITY_ID),
372+
]);
373+
$currentRouteParameters = $routeParametersForReferrer = array_merge($routeParameters, $adminContext->getRequest()->query->all());
370374
unset($routeParametersForReferrer[EA::REFERRER]);
371375
$this->currentPageReferrer = sprintf('%s%s?%s', $adminContext->getRequest()->getBaseUrl(), $adminContext->getRequest()->getPathInfo(), http_build_query($routeParametersForReferrer));
372376
}

tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\BlogPostCrudController;
99
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\CategoryCrudController;
1010
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\DashboardController;
11+
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\SecondDashboardController;
1112
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Entity\Category;
1213
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Kernel;
1314
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@@ -281,6 +282,59 @@ public function testAdminUrlGeneratorUsePrettyUrls()
281282
$this->assertSame('http://localhost/admin/pretty/urls/blog-post', $blogPostIndexUrl);
282283
}
283284

285+
public function testAdminUrlGenerator(): void
286+
{
287+
$client = static::createClient();
288+
$client->followRedirects();
289+
290+
self::bootKernel();
291+
$container = static::getContainer();
292+
$adminUrlGenerator = $container->get(AdminUrlGenerator::class);
293+
294+
$url = $adminUrlGenerator
295+
->setDashboard(DashboardController::class)
296+
->setController(CategoryCrudController::class)
297+
->setAction('customAction')
298+
->generateUrl()
299+
;
300+
$client->request('GET', $url);
301+
302+
$this->assertSelectorTextSame('#url1', 'http://localhost/admin/pretty/urls?'.http_build_query([
303+
'crudAction' => 'customAction',
304+
'crudControllerFqcn' => CategoryCrudController::class,
305+
'dashboardControllerFqcn' => DashboardController::class,
306+
]));
307+
308+
309+
$this->assertSelectorTextSame('#url2', 'http://localhost/admin/pretty/urls?'.http_build_query([
310+
'crudAction' => 'customAction',
311+
'crudControllerFqcn' => CategoryCrudController::class,
312+
'dashboardControllerFqcn' => DashboardController::class,
313+
'page' => 2,
314+
]));
315+
316+
$this->assertSelectorTextSame('#url3', 'http://localhost/admin/pretty/urls/category/new');
317+
318+
$this->assertSelectorTextSame('#url4', 'http://localhost/admin/pretty/urls?'.http_build_query([
319+
'crudAction' => 'customAction',
320+
'crudControllerFqcn' => BlogPostCrudController::class,
321+
'dashboardControllerFqcn' => DashboardController::class,
322+
]));
323+
324+
$this->assertSelectorTextSame('#url5', 'http://localhost/second/dashboard?'.http_build_query([
325+
'crudAction' => 'customAction',
326+
'crudControllerFqcn' => CategoryCrudController::class,
327+
'dashboardControllerFqcn' => SecondDashboardController::class,
328+
]));
329+
330+
$this->assertSelectorTextSame('#url6', 'http://localhost/second/dashboard?'.http_build_query([
331+
'crudAction' => 'detail',
332+
'crudControllerFqcn' => BlogPostCrudController::class,
333+
'dashboardControllerFqcn' => SecondDashboardController::class,
334+
'entityId' => 3,
335+
]));
336+
}
337+
284338
/**
285339
* @dataProvider provideUglyUrlRedirects
286340
*/

tests/PrettyUrlsTestApplication/src/Controller/CategoryCrudController.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
1010
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
1111
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
12-
use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
1312
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
1413
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
1514
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
16-
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
1715
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Config\Action as AppAction;
1816
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Entity\Category;
1917
use Symfony\Component\HttpFoundation\Response;
@@ -64,10 +62,6 @@ public function configureFilters(Filters $filters): Filters
6462

6563
public function customAction(AdminContext $context): Response
6664
{
67-
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => AppAction::CUSTOM_ACTION, 'entity' => $context->getEntity()])) {
68-
throw new ForbiddenActionException($context);
69-
}
70-
71-
return new Response();
65+
return $this->render('category/custom_action.html.twig');
7266
}
7367
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{# this temaplte tests that the admin URL generator works as expected when using pretty URLs #}
2+
<span id="url1">{{ ea_url() }}</span>
3+
<span id="url2">{{ ea_url().set('page', 2) }}</span>
4+
<span id="url3">{{ ea_url().setAction('new') }}</span>
5+
<span id="url4">{{ ea_url().setController('EasyCorp\\Bundle\\EasyAdminBundle\\Tests\\PrettyUrlsTestApplication\\Controller\\BlogPostCrudController') }}</span>
6+
<span id="url5">{{ ea_url().setDashboard('EasyCorp\\Bundle\\EasyAdminBundle\\Tests\\PrettyUrlsTestApplication\\Controller\\SecondDashboardController') }}</span>
7+
<span id="url6">{{ ea_url()
8+
.setAction('detail').setEntityId(3)
9+
.setController('EasyCorp\\Bundle\\EasyAdminBundle\\Tests\\PrettyUrlsTestApplication\\Controller\\BlogPostCrudController')
10+
.setDashboard('EasyCorp\\Bundle\\EasyAdminBundle\\Tests\\PrettyUrlsTestApplication\\Controller\\SecondDashboardController')
11+
}}</span>

0 commit comments

Comments
 (0)