forked from EasyCorp/EasyAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdminRouteTest.php
More file actions
680 lines (560 loc) · 34.5 KB
/
AdminRouteTest.php
File metadata and controls
680 lines (560 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\AdminRoute;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\CrudMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Entity\Product;
use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Kernel;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Filesystem\Filesystem;
/**
* @group legacy
*/
class AdminRouteTest extends WebTestCase
{
protected static function getKernelClass(): string
{
return Kernel::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();
// enable pretty URLs for this test
$buildDir = $client->getKernel()->getContainer()->getParameter('kernel.build_dir');
$filesystem = new Filesystem();
$filesystem->touch($buildDir.'/easyadmin_pretty_urls_enabled');
self::ensureKernelShutdown();
}
protected function tearDown(): void
{
// clean up the pretty URLs marker file
$filesystem = new Filesystem();
$buildDir = sys_get_temp_dir().'/EasyAdminBundle/tests/AdminRouteTestApplication/var/cache';
$filesystem->remove($buildDir.'/test/easyadmin_pretty_urls_enabled');
parent::tearDown();
}
public function testInvokableControllerRoute(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that the invokable controller route exists
$route = $router->getRouteCollection()->get('admin_custom_invokable');
$this->assertNotNull($route);
$this->assertSame('/admin/custom-invokable', $route->getPath());
$defaults = $route->getDefaults();
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\InvokableController::__invoke',
$defaults['_controller']
);
$this->assertSame('en', $defaults['_locale']);
$route2 = $router->getRouteCollection()->get('second_admin_custom_invokable');
$this->assertNotNull($route2);
$this->assertSame('/second-admin/custom-invokable', $route2->getPath());
}
public function testControllerWithClassAndMethodAttributes(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
$routes = $router->getRouteCollection();
$adminRoutes = [];
foreach ($routes as $name => $route) {
if (str_contains($name, 'foo')) {
$adminRoutes[$name] = $route->getPath();
}
}
// test the list route (should combine class + method)
$listRoute = $router->getRouteCollection()->get('admin_foo_list');
$this->assertNotNull($listRoute, 'Foo routes found: '.json_encode($adminRoutes));
$this->assertSame('/admin/foo/list', $listRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\FooController::list',
$listRoute->getDefault('_controller')
);
// test the export CSV route
$exportRoute = $router->getRouteCollection()->get('admin_foo_export_csv');
$this->assertNotNull($exportRoute);
$this->assertSame('/admin/foo/export/csv', $exportRoute->getPath());
$this->assertContains('GET', $exportRoute->getMethods());
$this->assertContains('POST', $exportRoute->getMethods());
// test public export route (overrides dashboard restrictions)
$publicRoute = $router->getRouteCollection()->get('admin_foo_public_export');
$this->assertNotNull($publicRoute);
$this->assertSame('/admin/foo/public-export', $publicRoute->getPath());
// the second dashboard should also have the public export route
$publicRoute2 = $router->getRouteCollection()->get('second_admin_foo_public_export');
$this->assertNotNull($publicRoute2);
$this->assertSame('/second-admin/foo/public-export', $publicRoute2->getPath());
// but the second dashboard should NOT have the restricted routes
$this->assertNull($router->getRouteCollection()->get('second_admin_foo_list'));
$this->assertNull($router->getRouteCollection()->get('second_admin_foo_export_csv'));
}
public function testControllerWithPartialClassConfiguration(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// reportsController is restricted to SecondDashboardController
// so it should NOT have routes for the main dashboard
$this->assertNull($router->getRouteCollection()->get('admin_sales_report'));
$this->assertNull($router->getRouteCollection()->get('admin_inventory_report'));
// but it SHOULD have routes for the second dashboard
$salesRoute = $router->getRouteCollection()->get('second_admin_sales_report');
$this->assertNotNull($salesRoute);
$this->assertSame('/second-admin/reports/sales', $salesRoute->getPath());
$inventoryRoute = $router->getRouteCollection()->get('second_admin_inventory_report');
$this->assertNotNull($inventoryRoute);
$this->assertSame('/second-admin/reports/inventory', $inventoryRoute->getPath());
}
public function testStandaloneMethodRoutes(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// standalone methods should create routes for all dashboards
$action1Route = $router->getRouteCollection()->get('admin_standalone_action1');
$this->assertNotNull($action1Route);
$this->assertSame('/admin/standalone/action1', $action1Route->getPath());
$action2Route = $router->getRouteCollection()->get('admin_standalone_action2');
$this->assertNotNull($action2Route);
$this->assertSame('/admin/standalone/action2', $action2Route->getPath());
$this->assertContains('POST', $action2Route->getMethods());
// should also exist for second dashboard
$action1Route2 = $router->getRouteCollection()->get('second_admin_standalone_action1');
$this->assertNotNull($action1Route2);
$this->assertSame('/second-admin/standalone/action1', $action1Route2->getPath());
// #[AdminRoute] applied only to the method should not create a route for the class
$this->assertNull($router->getRouteCollection()->get('admin_standalone'));
}
public function testStandaloneMethodCrudRoutes(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// standalone CRUD methods should create routes for all dashboards
$action1Route = $router->getRouteCollection()->get('admin_standalone_methods_crud_action1');
$this->assertNotNull($action1Route);
$this->assertSame('/admin/standalone-methods/crud/action1', $action1Route->getPath());
$action2Route = $router->getRouteCollection()->get('admin_standalone_methods_crud_action2');
$this->assertNotNull($action2Route);
$this->assertSame('/admin/standalone-methods/crud/action2', $action2Route->getPath());
$this->assertContains('POST', $action2Route->getMethods());
// should also exist for second dashboard
$action1Route2 = $router->getRouteCollection()->get('second_admin_standalone_methods_crud_action1');
$this->assertNotNull($action1Route2);
$this->assertSame('/second-admin/standalone-methods/crud/action1', $action1Route2->getPath());
// #[AdminRoute] applied only to the method should not create a route for the class
$this->assertNull($router->getRouteCollection()->get('admin_standalone_methods'));
}
public function testRouteAccessibility(): void
{
$client = static::createClient();
// test invokable controller
$client->request('GET', '/admin/custom-invokable');
$this->assertResponseIsSuccessful();
$this->assertSame('Invokable Controller Response', $client->getResponse()->getContent());
// test foo list
$client->request('GET', '/admin/foo/list');
$this->assertResponseIsSuccessful();
$this->assertSame('Foo List', $client->getResponse()->getContent());
// test standalone action
$client->request('GET', '/admin/standalone/action1');
$this->assertResponseIsSuccessful();
$this->assertSame('Standalone Action 1', $client->getResponse()->getContent());
// test reports (should work on second dashboard)
$client->request('GET', '/second-admin/reports/sales');
$this->assertResponseIsSuccessful();
$this->assertSame('Sales Report', $client->getResponse()->getContent());
}
public function testRouteNamesAreCorrectlyGenerated(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
$routes = $router->getRouteCollection();
// collect all AdminRoute-generated routes
$adminRoutes = [];
foreach ($routes as $name => $route) {
if ($route->hasDefault(EA::ROUTE_CREATED_BY_EASYADMIN)) {
$adminRoutes[$name] = $route;
}
}
// check expected route names exist
$expectedRoutes = [
// invokable controller routes
'admin_custom_invokable',
'second_admin_custom_invokable',
// foo routes (only for main dashboard except public_export)
'admin_foo_list',
'admin_foo_export_csv',
'admin_foo_public_export',
'second_admin_foo_public_export',
// reports routes (only for second dashboard)
'second_admin_sales_report',
'second_admin_inventory_report',
// standalone routes (for all dashboards)
'admin_standalone_action1',
'admin_standalone_action2',
'second_admin_standalone_action1',
'second_admin_standalone_action2',
];
foreach ($expectedRoutes as $routeName) {
$this->assertArrayHasKey($routeName, $adminRoutes, "Expected route '$routeName' not found");
}
}
public function testRepeatedAdminRouteAttributes(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that repeated AdminRoute attributes on the same method generate multiple routes
$route1 = $router->getRouteCollection()->get('admin_route1');
$this->assertNotNull($route1, 'admin_route1 route should exist');
$this->assertSame('/admin/route1/{id}', $route1->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\RepeatedRouteController::twoRoutes',
$route1->getDefault('_controller')
);
$route2 = $router->getRouteCollection()->get('admin_route2');
$this->assertNotNull($route2, 'admin_route2 route should exist');
$this->assertSame('/admin/route2/{id}', $route2->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\RepeatedRouteController::twoRoutes',
$route2->getDefault('_controller')
);
// test multiple routes pointing to the same action
$route1 = $router->getRouteCollection()->get('admin_multiple_route1');
$this->assertNotNull($route1, 'Multiple route 1 should exist');
$this->assertSame('/admin/multiple/route1', $route1->getPath());
$route2 = $router->getRouteCollection()->get('admin_multiple_route2');
$this->assertNotNull($route2, 'Multiple route 2 should exist');
$this->assertSame('/admin/multiple/route2', $route2->getPath());
$route3 = $router->getRouteCollection()->get('admin_multiple_route3');
$this->assertNotNull($route3, 'Multiple route 3 should exist');
$this->assertSame('/admin/multiple/route3', $route3->getPath());
// all three routes should point to the same controller action
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\RepeatedRouteController::multipleRoutes',
$route1->getDefault('_controller')
);
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\RepeatedRouteController::multipleRoutes',
$route2->getDefault('_controller')
);
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\RepeatedRouteController::multipleRoutes',
$route3->getDefault('_controller')
);
// test that routes work for second dashboard too
$this->assertNotNull($router->getRouteCollection()->get('second_admin_route1'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_route2'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route1'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route2'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route3'));
}
public function testMethodRoutesWithSameName(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that custom routes with same name in different CRUD controllers are generated for both dashboards
$this->assertNotNull($router->getRouteCollection()->get('admin_same_action_one_same_action_name'));
$this->assertNotNull($router->getRouteCollection()->get('admin_same_action_two_same_action_name'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_same_action_one_same_action_name'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_same_action_two_same_action_name'));
}
public function testRepeatedRoutesAreAccessible(): void
{
$client = static::createClient();
$client->request('GET', '/admin/route1/123');
$this->assertResponseIsSuccessful();
$this->assertSame('ID: 123', $client->getResponse()->getContent());
$client->request('GET', '/admin/route2/456');
$this->assertResponseIsSuccessful();
$this->assertSame('ID: 456', $client->getResponse()->getContent());
$client->request('GET', '/admin/multiple/route1');
$this->assertResponseIsSuccessful();
$this->assertSame('Multiple routes to same action', $client->getResponse()->getContent());
$client->request('GET', '/admin/multiple/route2');
$this->assertResponseIsSuccessful();
$this->assertSame('Multiple routes to same action', $client->getResponse()->getContent());
$client->request('GET', '/admin/multiple/route3');
$this->assertResponseIsSuccessful();
$this->assertSame('Multiple routes to same action', $client->getResponse()->getContent());
}
public function testClassLevelAdminRouteAsPrefix(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that class-level AdminRoute acts as a prefix when methods have AdminRoute
$usersRoute = $router->getRouteCollection()->get('admin_api_users');
$this->assertNotNull($usersRoute, 'API users route should exist');
$this->assertSame('/admin/api/users', $usersRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\PrefixedController::listUsers',
$usersRoute->getDefault('_controller')
);
$userDetailRoute = $router->getRouteCollection()->get('admin_api_user_detail');
$this->assertNotNull($userDetailRoute, 'API user detail route should exist');
$this->assertSame('/admin/api/users/{id}', $userDetailRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\PrefixedController::getUserDetail',
$userDetailRoute->getDefault('_controller')
);
// test that routes work for second dashboard too
$this->assertNotNull($router->getRouteCollection()->get('second_admin_api_users'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_api_user_detail'));
}
public function testClassLevelRoutesAreAccessible(): void
{
$client = static::createClient();
// test prefixed routes
$client->request('GET', '/admin/api/users');
$this->assertResponseIsSuccessful();
$this->assertSame('User list', $client->getResponse()->getContent());
$client->request('GET', '/admin/api/users/789');
$this->assertResponseIsSuccessful();
$this->assertSame('User detail: 789', $client->getResponse()->getContent());
}
public function testMultipleAdminRoutesOnSameCrudAction(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that multiple AdminRoute attributes on the same CRUD action generate multiple routes
// customAction1 with two routes
$action1Route = $router->getRouteCollection()->get('admin_multiple_route_action1');
$this->assertNotNull($action1Route, 'Action1 route should exist');
$this->assertSame('/admin/multiple-route/action1', $action1Route->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\MultipleRouteCrudController::customAction1',
$action1Route->getDefault('_controller')
);
$action1AltRoute = $router->getRouteCollection()->get('admin_multiple_route_action1_alt');
$this->assertNotNull($action1AltRoute, 'Action1 alt route should exist');
$this->assertSame('/admin/multiple-route/action1-alt', $action1AltRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\MultipleRouteCrudController::customAction1',
$action1AltRoute->getDefault('_controller')
);
// customAction2 with three routes and different HTTP methods
$action2Path1Route = $router->getRouteCollection()->get('admin_multiple_route_action2_path1');
$this->assertNotNull($action2Path1Route, 'Action2 path1 route should exist');
$this->assertSame('/admin/multiple-route/action2/path1', $action2Path1Route->getPath());
$this->assertEquals(['GET'], $action2Path1Route->getMethods());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\MultipleRouteCrudController::customAction2',
$action2Path1Route->getDefault('_controller')
);
$action2Path2Route = $router->getRouteCollection()->get('admin_multiple_route_action2_path2');
$this->assertNotNull($action2Path2Route, 'Action2 path2 route should exist');
$this->assertSame('/admin/multiple-route/action2/path2', $action2Path2Route->getPath());
$this->assertEquals(['GET'], $action2Path2Route->getMethods());
$action2Path3Route = $router->getRouteCollection()->get('admin_multiple_route_action2_path3');
$this->assertNotNull($action2Path3Route, 'Action2 path3 route should exist');
$this->assertSame('/admin/multiple-route/action2/path3', $action2Path3Route->getPath());
$this->assertContains('GET', $action2Path3Route->getMethods());
$this->assertContains('POST', $action2Path3Route->getMethods());
// customAction3 with entity ID parameter
$action3Route = $router->getRouteCollection()->get('admin_multiple_route_action3');
$this->assertNotNull($action3Route, 'Action3 route should exist');
$this->assertSame('/admin/multiple-route/action3/{entityId}', $action3Route->getPath());
$action3AltRoute = $router->getRouteCollection()->get('admin_multiple_route_action3_alt');
$this->assertNotNull($action3AltRoute, 'Action3 alt route should exist');
$this->assertSame('/admin/multiple-route/action3-alt/{entityId}', $action3AltRoute->getPath());
// customAction4, where one of the routes doesn't define its path, only its name
$action4Route = $router->getRouteCollection()->get('admin_multiple_route_action4');
$this->assertNotNull($action4Route, 'Action4 route should exist');
$this->assertSame('/admin/multiple-route/action4/{entityId}', $action4Route->getPath());
$action4AltRoute = $router->getRouteCollection()->get('admin_multiple_route_custom_action4');
$this->assertNotNull($action4AltRoute, 'Action4 alt route should exist with an autogenerated route name based on the method name');
$this->assertSame('/admin/multiple-route/action4-alt/{entityId}', $action4AltRoute->getPath());
// customAction5, where one of the routes doesn't define its name, only its path
$action5Route = $router->getRouteCollection()->get('admin_multiple_route_action5');
$this->assertNotNull($action5Route, 'Action5 route should exist');
$this->assertSame('/admin/multiple-route/action5', $action5Route->getPath());
$action5AltRoute = $router->getRouteCollection()->get('admin_multiple_route_action5_alt');
$this->assertNotNull($action5AltRoute, 'Action5 alt route should exist with an autogenerated route path based on the method name');
$this->assertSame('/admin/multiple-route/custom-action5', $action5AltRoute->getPath());
// customAction6, where one of the routes doesn't define neither its name nor path
$action6Route = $router->getRouteCollection()->get('admin_multiple_route_action6');
$this->assertNotNull($action6Route, 'Action6 route should exist');
$this->assertSame('/admin/multiple-route/action6', $action6Route->getPath());
$action6AltRoute = $router->getRouteCollection()->get('admin_multiple_route_custom_action6');
$this->assertNotNull($action6AltRoute, 'Action6 alt route should exist with an autogenerated route name and path based on the method name');
$this->assertSame('/admin/multiple-route/custom-action6', $action6AltRoute->getPath());
// test that routes work for second dashboard too
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route_action1'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route_action1_alt'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route_action2_path1'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route_action2_path2'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_multiple_route_action2_path3'));
}
public function testBuiltInActionsWithCustomRouteNames(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that when built-in actions have custom route names, only the custom routes are generated
// and the default routes are NOT generated (avoiding duplicates)
// 'index' action was customized with route name 'list' (path not customized, so it uses the default '/')
$indexCustomRoute = $router->getRouteCollection()->get('admin_built_in_action_list');
$this->assertNotNull($indexCustomRoute, 'Custom route for index action should exist');
$this->assertSame('/admin/built-in-action/index', $indexCustomRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\BuiltInActionCrudController::index',
$indexCustomRoute->getDefault('_controller')
);
// the default 'index' route should NOT exist
$indexDefaultRoute = $router->getRouteCollection()->get('admin_built_in_action_index');
$this->assertNull($indexDefaultRoute, 'Default route for index action should NOT exist when overridden');
// 'new' action was customized with route name 'create' and path '/create'
$newCustomRoute = $router->getRouteCollection()->get('admin_built_in_action_create');
$this->assertNotNull($newCustomRoute, 'Custom route for new action should exist');
$this->assertSame('/admin/built-in-action/create', $newCustomRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\BuiltInActionCrudController::new',
$newCustomRoute->getDefault('_controller')
);
// the default 'new' route should NOT exist
$newDefaultRoute = $router->getRouteCollection()->get('admin_built_in_action_new');
$this->assertNull($newDefaultRoute, 'Default route for new action should NOT exist when overridden');
// 'edit' action was customized with route name 'update' (path not customized, so it auto-generates based on action name)
$editCustomRoute = $router->getRouteCollection()->get('admin_built_in_action_update');
$this->assertNotNull($editCustomRoute, 'Custom route for edit action should exist');
$this->assertSame('/admin/built-in-action/edit', $editCustomRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\BuiltInActionCrudController::edit',
$editCustomRoute->getDefault('_controller')
);
// the default 'edit' route should NOT exist
$editDefaultRoute = $router->getRouteCollection()->get('admin_built_in_action_edit');
$this->assertNull($editDefaultRoute, 'Default route for edit action should NOT exist when overridden');
// 'detail' action was customized with route name 'show' (path not customized, so it auto-generates based on action name)
$detailCustomRoute = $router->getRouteCollection()->get('admin_built_in_action_show');
$this->assertNotNull($detailCustomRoute, 'Custom route for detail action should exist');
$this->assertSame('/admin/built-in-action/detail', $detailCustomRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\BuiltInActionCrudController::detail',
$detailCustomRoute->getDefault('_controller')
);
// the default 'detail' route should NOT exist
$detailDefaultRoute = $router->getRouteCollection()->get('admin_built_in_action_detail');
$this->assertNull($detailDefaultRoute, 'Default route for detail action should NOT exist when overridden');
// 'delete' action was NOT customized, so it should use the default route
$deleteDefaultRoute = $router->getRouteCollection()->get('admin_built_in_action_delete');
$this->assertNotNull($deleteDefaultRoute, 'Default route for delete action should exist when NOT overridden');
$this->assertSame('/admin/built-in-action/{entityId}/delete', $deleteDefaultRoute->getPath());
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\BuiltInActionCrudController::delete',
$deleteDefaultRoute->getDefault('_controller')
);
// test that routes work for second dashboard too
$this->assertNotNull($router->getRouteCollection()->get('second_admin_built_in_action_list'));
$this->assertNull($router->getRouteCollection()->get('second_admin_built_in_action_index'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_built_in_action_create'));
$this->assertNull($router->getRouteCollection()->get('second_admin_built_in_action_new'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_built_in_action_update'));
$this->assertNull($router->getRouteCollection()->get('second_admin_built_in_action_edit'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_built_in_action_show'));
$this->assertNull($router->getRouteCollection()->get('second_admin_built_in_action_detail'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_built_in_action_delete'));
}
/**
* Tests that the deprecated #[AdminCrud] attribute still works for backward compatibility.
*/
public function testLegacyAdminCrudAttribute(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that #[AdminCrud] generates routes with the custom path and name prefix
$indexRoute = $router->getRouteCollection()->get('admin_legacy_crud_custom_index');
$this->assertNotNull($indexRoute, 'Legacy #[AdminCrud] with #[AdminAction] should generate routes');
$this->assertSame('/admin/legacy-crud/custom-index', $indexRoute->getPath());
// test that the CRUD controller generates routes for all standard actions
$detailRoute = $router->getRouteCollection()->get('admin_legacy_crud_detail');
$this->assertNotNull($detailRoute);
$this->assertSame('/admin/legacy-crud/custom-detail/{entityId}', $detailRoute->getPath());
$newRoute = $router->getRouteCollection()->get('admin_legacy_crud_create_new');
$this->assertNotNull($newRoute);
$this->assertSame('/admin/legacy-crud/new', $newRoute->getPath());
$editRoute = $router->getRouteCollection()->get('admin_legacy_crud_modify');
$this->assertNotNull($editRoute);
$this->assertSame('/admin/legacy-crud/custom-edit/{entityId}', $editRoute->getPath());
// test custom action with #[AdminAction]
$exportRoute = $router->getRouteCollection()->get('admin_legacy_crud_export_data');
$this->assertNotNull($exportRoute);
$this->assertSame('/admin/legacy-crud/export', $exportRoute->getPath());
// test that delete (without customization) uses default naming
$deleteRoute = $router->getRouteCollection()->get('admin_legacy_crud_delete');
$this->assertNotNull($deleteRoute);
$this->assertStringContainsString('/admin/legacy-crud/', $deleteRoute->getPath());
}
/**
* Tests that routes generated by #[AdminCrud] and #[AdminAction] are accessible.
*/
public function testLegacyAttributesRoutesAreAccessible(): void
{
$client = static::createClient();
$client->request('GET', '/admin/legacy-crud/export');
$this->assertResponseIsSuccessful();
$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);
$this->assertInstanceOf(CrudMenuItem::class, $menuItem);
}
public function testCrudControllerNamedController(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
// test that ControllerCrudController generates routes with "controller" as the path segment
// (this was previously broken because str_replace removed "Controller" from the middle of the name)
$indexRoute = $router->getRouteCollection()->get('admin_controller_index');
$this->assertNotNull($indexRoute, 'Route "admin_controller_index" should exist');
$this->assertSame('/admin/controller', $indexRoute->getPath());
$detailRoute = $router->getRouteCollection()->get('admin_controller_detail');
$this->assertNotNull($detailRoute, 'Route "admin_controller_detail" should exist');
$this->assertSame('/admin/controller/{entityId}', $detailRoute->getPath());
// test that routes also exist for the second dashboard
$this->assertNotNull($router->getRouteCollection()->get('second_admin_controller_index'));
$this->assertNotNull($router->getRouteCollection()->get('second_admin_controller_detail'));
}
public function testAdminDashboardAdvancedRouteOptions(): void
{
$client = static::createClient();
$router = $client->getContainer()->get('router');
$route = $router->getRouteCollection()->get('advanced_admin');
$this->assertNotNull($route, 'Route "advanced_admin" should exist');
$this->assertSame('/advanced-admin/', $route->getPath());
$this->assertSame('admin.example.com', $route->getHost());
$this->assertSame(['https'], $route->getSchemes());
$this->assertSame(['GET', 'HEAD'], $route->getMethods());
$this->assertSame(['foo' => '.*'], $route->getRequirements());
$this->assertSame('Symfony\Component\Routing\RouteCompiler', $route->getOption('compiler_class'));
$this->assertTrue($route->getOption('utf8'));
$this->assertSame('context.getMethod() in ["GET", "HEAD"]', $route->getCondition());
$defaults = $route->getDefaults();
$this->assertSame('bar', $defaults['foo']);
$this->assertSame('en', $defaults['_locale']);
$this->assertSame('html', $defaults['_format']);
$this->assertTrue($defaults['_stateless']);
$this->assertTrue($defaults['routeCreatedByEasyAdmin']);
$this->assertSame(
'EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller\AdvancedOptionsDashboardController',
$defaults['dashboardControllerFqcn']
);
}
}