1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Menu\Provider; |
15
|
|
|
|
16
|
|
|
use Knp\Menu\FactoryInterface; |
17
|
|
|
use Knp\Menu\Integration\Symfony\RoutingExtension; |
18
|
|
|
use Knp\Menu\ItemInterface; |
19
|
|
|
use Knp\Menu\MenuFactory; |
20
|
|
|
use Knp\Menu\MenuItem; |
21
|
|
|
use Knp\Menu\Provider\MenuProviderInterface; |
22
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
25
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
26
|
|
|
use Sonata\AdminBundle\Menu\Provider\GroupMenuProvider; |
27
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
28
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
29
|
|
|
|
30
|
|
|
class GroupMenuProviderTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var MockObject|Pool |
34
|
|
|
*/ |
35
|
|
|
private $pool; |
36
|
|
|
/** |
37
|
|
|
* @var MockObject|MenuProviderInterface |
38
|
|
|
*/ |
39
|
|
|
private $provider; |
40
|
|
|
/** |
41
|
|
|
* @var MockObject|FactoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $factory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var MockObject |
47
|
|
|
*/ |
48
|
|
|
private $checker; |
49
|
|
|
|
50
|
|
|
protected function setUp(): void |
51
|
|
|
{ |
52
|
|
|
$this->pool = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock(); |
53
|
|
|
$this->checker = $this |
54
|
|
|
->getMockBuilder(AuthorizationCheckerInterface::class) |
55
|
|
|
->setMethods(['isGranted']) |
56
|
|
|
->disableOriginalConstructor() |
57
|
|
|
->getMock(); |
58
|
|
|
|
59
|
|
|
$this->factory = new MenuFactory(); |
60
|
|
|
|
61
|
|
|
$urlGenerator = $this->createStub(UrlGeneratorInterface::class); |
62
|
|
|
$urlGenerator->method('generate')->willReturnCallback(static function (string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string { |
63
|
|
|
switch ($referenceType) { |
64
|
|
|
case UrlGeneratorInterface::ABSOLUTE_URL: |
65
|
|
|
return sprintf('http://sonata-project/%s%s', $name, $parameters ? '?'.http_build_query($parameters) : ''); |
66
|
|
|
case UrlGeneratorInterface::ABSOLUTE_PATH: |
67
|
|
|
return sprintf('/%s%s', $name, $parameters ? '?'.http_build_query($parameters) : ''); |
68
|
|
|
default: |
69
|
|
|
throw new \InvalidArgumentException(sprintf( |
70
|
|
|
'Dummy router does not support the reference type "%s".', |
71
|
|
|
$referenceType |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
$this->factory->addExtension(new RoutingExtension($urlGenerator)); |
77
|
|
|
|
78
|
|
|
$this->provider = new GroupMenuProvider($this->factory, $this->pool, $this->checker); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testGroupMenuProviderName(): void |
82
|
|
|
{ |
83
|
|
|
$this->assertTrue($this->provider->has('sonata_group_menu')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @dataProvider getAdminGroups |
88
|
|
|
*/ |
89
|
|
|
public function testGetMenuProviderWithCheckerGrantedGroupRoles(array $adminGroups): void |
90
|
|
|
{ |
91
|
|
|
$this->pool |
|
|
|
|
92
|
|
|
->method('getInstance') |
93
|
|
|
->with($this->equalTo('sonata_admin_foo_service')) |
94
|
|
|
->willReturn($this->getAdminMock()); |
95
|
|
|
|
96
|
|
|
$this->checker |
97
|
|
|
->method('isGranted') |
98
|
|
|
->willReturn(false); |
99
|
|
|
|
100
|
|
|
$menu = $this->provider->get( |
101
|
|
|
'providerFoo', |
102
|
|
|
[ |
103
|
|
|
'name' => 'foo', |
104
|
|
|
'group' => $adminGroups, |
105
|
|
|
] |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
109
|
|
|
$this->assertSame('foo', $menu->getName()); |
110
|
|
|
|
111
|
|
|
$children = $menu->getChildren(); |
112
|
|
|
|
113
|
|
|
$this->assertCount(1, $children); |
114
|
|
|
$this->assertArrayHasKey('foo_admin_label', $children); |
115
|
|
|
$this->assertArrayNotHasKey('route_label', $children); |
116
|
|
|
$this->assertInstanceOf(MenuItem::class, $menu['foo_admin_label']); |
117
|
|
|
$this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel()); |
118
|
|
|
|
119
|
|
|
$extras = $menu['foo_admin_label']->getExtras(); |
120
|
|
|
$this->assertArrayHasKey('label_catalogue', $extras); |
121
|
|
|
$this->assertSame($extras['label_catalogue'], 'SonataAdminBundle'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function unanimousGrantCheckerMock(string $role): bool |
125
|
|
|
{ |
126
|
|
|
return \in_array($role, ['foo', 'bar', 'baz'], true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function unanimousGrantCheckerNoBazMock(string $role): bool |
130
|
|
|
{ |
131
|
|
|
return \in_array($role, ['foo', 'bar'], true); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @dataProvider getAdminGroupsMultipleRoles |
136
|
|
|
*/ |
137
|
|
|
public function testGetMenuProviderWithCheckerGrantedMultipleGroupRoles( |
138
|
|
|
array $adminGroups |
139
|
|
|
): void { |
140
|
|
|
$this->checker |
141
|
|
|
->method('isGranted') |
142
|
|
|
->willReturnCallback([$this, 'unanimousGrantCheckerMock']); |
143
|
|
|
|
144
|
|
|
$menu = $this->provider->get( |
145
|
|
|
'providerFoo', |
146
|
|
|
[ |
147
|
|
|
'name' => 'foo', |
148
|
|
|
'group' => $adminGroups, |
149
|
|
|
] |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
153
|
|
|
|
154
|
|
|
$children = $menu->getChildren(); |
155
|
|
|
|
156
|
|
|
$this->assertCount(4, $children); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @dataProvider getAdminGroupsMultipleRoles |
161
|
|
|
*/ |
162
|
|
|
public function testGetMenuProviderWithCheckerGrantedGroupAndItemRoles( |
163
|
|
|
array $adminGroups |
164
|
|
|
): void { |
165
|
|
|
$this->checker |
166
|
|
|
->method('isGranted') |
167
|
|
|
->willReturnCallback([$this, 'unanimousGrantCheckerNoBazMock']); |
168
|
|
|
|
169
|
|
|
$menu = $this->provider->get( |
170
|
|
|
'providerFoo', |
171
|
|
|
[ |
172
|
|
|
'name' => 'foo', |
173
|
|
|
'group' => $adminGroups, |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
$isBazItem = $adminGroups['roles'] === ['baz']; |
177
|
|
|
|
178
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
179
|
|
|
$this->assertSame(!$isBazItem, $menu->isDisplayed()); |
180
|
|
|
|
181
|
|
|
$children = $menu->getChildren(); |
182
|
|
|
$this->assertCount($isBazItem ? 0 : 3, $children); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @dataProvider getAdminGroupsMultipleRolesOnTop |
187
|
|
|
*/ |
188
|
|
|
public function testGetMenuProviderWithCheckerGrantedMultipleGroupRolesOnTop( |
189
|
|
|
array $adminGroups |
190
|
|
|
): void { |
191
|
|
|
$this->checker |
192
|
|
|
->method('isGranted') |
193
|
|
|
->willReturnCallback([$this, 'unanimousGrantCheckerMock']); |
194
|
|
|
|
195
|
|
|
$menu = $this->provider->get( |
196
|
|
|
'providerFoo', |
197
|
|
|
[ |
198
|
|
|
'name' => 'foo', |
199
|
|
|
'group' => $adminGroups, |
200
|
|
|
] |
201
|
|
|
); |
202
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
203
|
|
|
|
204
|
|
|
$this->assertTrue($menu->isDisplayed()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @dataProvider getAdminGroups |
209
|
|
|
*/ |
210
|
|
|
public function testGetMenuProviderWithAdmin(array $adminGroups): void |
211
|
|
|
{ |
212
|
|
|
$this->pool |
|
|
|
|
213
|
|
|
->method('getInstance') |
214
|
|
|
->with($this->equalTo('sonata_admin_foo_service')) |
215
|
|
|
->willReturn($this->getAdminMock()); |
216
|
|
|
|
217
|
|
|
$this->checker |
218
|
|
|
->method('isGranted') |
219
|
|
|
->willReturn(true); |
220
|
|
|
|
221
|
|
|
$menu = $this->provider->get( |
222
|
|
|
'providerFoo', |
223
|
|
|
[ |
224
|
|
|
'name' => 'foo', |
225
|
|
|
'group' => $adminGroups, |
226
|
|
|
] |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
230
|
|
|
$this->assertSame('foo', $menu->getName()); |
231
|
|
|
|
232
|
|
|
$children = $menu->getChildren(); |
233
|
|
|
|
234
|
|
|
$this->assertCount(3, $children); |
235
|
|
|
$this->assertArrayHasKey('foo_admin_label', $children); |
236
|
|
|
$this->assertArrayHasKey('route_label', $children); |
237
|
|
|
$this->assertInstanceOf(MenuItem::class, $menu['foo_admin_label']); |
238
|
|
|
$this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel()); |
239
|
|
|
|
240
|
|
|
$extras = $menu['foo_admin_label']->getExtras(); |
241
|
|
|
$this->assertArrayHasKey('label_catalogue', $extras); |
242
|
|
|
$this->assertSame('SonataAdminBundle', $extras['label_catalogue']); |
243
|
|
|
|
244
|
|
|
$extras = $menu['route_label']->getExtras(); |
245
|
|
|
$this->assertArrayHasKey('label_catalogue', $extras); |
246
|
|
|
$this->assertSame('SonataAdminBundle', $extras['label_catalogue']); |
247
|
|
|
|
248
|
|
|
$this->assertSame('http://sonata-project/FooRoute?foo=bar', $menu['route_label']->getUri()); |
249
|
|
|
$this->assertSame('/FooRelativeRoute?baz=qux', $menu['relative_route']->getUri()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @dataProvider getAdminGroups |
254
|
|
|
*/ |
255
|
|
|
public function testGetKnpMenuWithListRoute(array $adminGroups): void |
256
|
|
|
{ |
257
|
|
|
$this->pool |
|
|
|
|
258
|
|
|
->method('getInstance') |
259
|
|
|
->with($this->equalTo('sonata_admin_foo_service')) |
260
|
|
|
->willReturn($this->getAdminMock(false)); |
261
|
|
|
|
262
|
|
|
$this->checker |
263
|
|
|
->method('isGranted') |
264
|
|
|
->willReturn(true); |
265
|
|
|
|
266
|
|
|
$menu = $this->provider->get( |
267
|
|
|
'providerFoo', |
268
|
|
|
[ |
269
|
|
|
'name' => 'foo', |
270
|
|
|
'group' => $adminGroups, |
271
|
|
|
] |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
275
|
|
|
$this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren()); |
276
|
|
|
$this->assertArrayHasKey('route_label', $menu->getChildren()); |
277
|
|
|
$this->assertCount(2, $menu->getChildren()); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @dataProvider getAdminGroups |
282
|
|
|
*/ |
283
|
|
|
public function testGetKnpMenuWithGrantedList(array $adminGroups): void |
284
|
|
|
{ |
285
|
|
|
$this->pool |
|
|
|
|
286
|
|
|
->method('getInstance') |
287
|
|
|
->with($this->equalTo('sonata_admin_foo_service')) |
288
|
|
|
->willReturn($this->getAdminMock(true, false)); |
289
|
|
|
|
290
|
|
|
$this->checker |
291
|
|
|
->method('isGranted') |
292
|
|
|
->willReturn(true); |
293
|
|
|
|
294
|
|
|
$menu = $this->provider->get( |
295
|
|
|
'providerFoo', |
296
|
|
|
[ |
297
|
|
|
'name' => 'foo', |
298
|
|
|
'group' => $adminGroups, |
299
|
|
|
] |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
303
|
|
|
$this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren()); |
304
|
|
|
$this->assertArrayHasKey('route_label', $menu->getChildren()); |
305
|
|
|
$this->assertCount(2, $menu->getChildren()); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @dataProvider getAdminGroupsWithOnTopOption |
310
|
|
|
*/ |
311
|
|
|
public function testGetMenuProviderOnTopOptions(array $adminGroupsOnTopOption): void |
312
|
|
|
{ |
313
|
|
|
$this->pool |
|
|
|
|
314
|
|
|
->method('getInstance') |
315
|
|
|
->with($this->equalTo('sonata_admin_foo_service')) |
316
|
|
|
->willReturn($this->getAdminMock(true, false)); |
317
|
|
|
|
318
|
|
|
$menu = $this->provider->get( |
319
|
|
|
'providerFoo', |
320
|
|
|
[ |
321
|
|
|
'name' => 'foo', |
322
|
|
|
'group' => $adminGroupsOnTopOption, |
323
|
|
|
] |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
327
|
|
|
$this->assertCount(0, $menu->getChildren()); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @dataProvider getAdminGroups |
332
|
|
|
*/ |
333
|
|
|
public function testGetMenuProviderKeepOpenOption(array $adminGroups): void |
334
|
|
|
{ |
335
|
|
|
$this->pool |
|
|
|
|
336
|
|
|
->method('getInstance') |
337
|
|
|
->with($this->equalTo('sonata_admin_foo_service')) |
338
|
|
|
->willReturn($this->getAdminMock()); |
339
|
|
|
|
340
|
|
|
$this->checker |
341
|
|
|
->method('isGranted') |
342
|
|
|
->willReturn(true); |
343
|
|
|
|
344
|
|
|
$adminGroups['keep_open'] = true; |
345
|
|
|
|
346
|
|
|
$menu = $this->provider->get( |
347
|
|
|
'providerFoo', |
348
|
|
|
[ |
349
|
|
|
'name' => 'foo', |
350
|
|
|
'group' => $adminGroups, |
351
|
|
|
] |
352
|
|
|
); |
353
|
|
|
|
354
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
355
|
|
|
$this->assertSame('keep-open', $menu->getAttribute('class')); |
356
|
|
|
$this->assertTrue($menu->getExtra('keep_open')); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @dataProvider getRootMenuItemWithDifferentUrlTypes |
361
|
|
|
*/ |
362
|
|
|
public function testRootMenuItemUrl(string $expectedUrl, array $item): void |
363
|
|
|
{ |
364
|
|
|
$this->pool |
|
|
|
|
365
|
|
|
->method('getInstance') |
366
|
|
|
->with($this->equalTo('sonata_admin_absolute_url')) |
367
|
|
|
->willReturn($this->getAdminMock()); |
368
|
|
|
|
369
|
|
|
$this->checker |
370
|
|
|
->method('isGranted') |
371
|
|
|
->willReturn(true); |
372
|
|
|
|
373
|
|
|
$menu = $this->provider->get( |
374
|
|
|
'providerFoo', |
375
|
|
|
[ |
376
|
|
|
'name' => 'foo', |
377
|
|
|
'group' => $item, |
378
|
|
|
] |
379
|
|
|
); |
380
|
|
|
|
381
|
|
|
$this->assertInstanceOf(ItemInterface::class, $menu); |
382
|
|
|
$this->assertSame('foo', $menu->getName()); |
383
|
|
|
$this->assertSame($expectedUrl, $menu['foo_admin_label']->getUri()); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function getAdminGroups(): array |
387
|
|
|
{ |
388
|
|
|
return [ |
389
|
|
|
[ |
390
|
|
|
'bar' => [ |
391
|
|
|
'label' => 'foo', |
392
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
393
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
394
|
|
|
'items' => [ |
395
|
|
|
[ |
396
|
|
|
'admin' => 'sonata_admin_foo_service', |
397
|
|
|
'label' => 'fooLabel', |
398
|
|
|
'route' => 'FooServiceRoute', |
399
|
|
|
'route_absolute' => true, |
400
|
|
|
], |
401
|
|
|
[ |
402
|
|
|
'admin' => '', |
403
|
|
|
'label' => 'route_label', |
404
|
|
|
'route' => 'FooRoute', |
405
|
|
|
'route_params' => ['foo' => 'bar'], |
406
|
|
|
'route_absolute' => true, |
407
|
|
|
'roles' => [], |
408
|
|
|
], |
409
|
|
|
[ |
410
|
|
|
'admin' => '', |
411
|
|
|
'label' => 'relative_route', |
412
|
|
|
'route' => 'FooRelativeRoute', |
413
|
|
|
'route_params' => ['baz' => 'qux'], |
414
|
|
|
'route_absolute' => false, |
415
|
|
|
'roles' => [], |
416
|
|
|
], |
417
|
|
|
], |
418
|
|
|
'item_adds' => [], |
419
|
|
|
'roles' => ['foo'], |
420
|
|
|
], |
421
|
|
|
], |
422
|
|
|
]; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function getAdminGroupsMultipleRoles(): array |
426
|
|
|
{ |
427
|
|
|
return [ |
428
|
|
|
[ |
429
|
|
|
// group for all roles, children with different roles |
430
|
|
|
[ |
431
|
|
|
'label' => 'foo', |
432
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
433
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
434
|
|
|
'items' => [ |
435
|
|
|
[ |
436
|
|
|
'admin' => '', |
437
|
|
|
'label' => 'route_label1', |
438
|
|
|
'route' => 'FooRoute1', |
439
|
|
|
'route_params' => ['foo' => 'bar'], |
440
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
441
|
|
|
'roles' => ['foo', 'bar'], |
442
|
|
|
], |
443
|
|
|
[ |
444
|
|
|
'admin' => '', |
445
|
|
|
'label' => 'route_label2', |
446
|
|
|
'route' => 'FooRoute2', |
447
|
|
|
'route_params' => ['foo' => 'bar'], |
448
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
449
|
|
|
'roles' => ['foo'], |
450
|
|
|
], |
451
|
|
|
[ |
452
|
|
|
'admin' => '', |
453
|
|
|
'label' => 'route_label3', |
454
|
|
|
'route' => 'FooRoute3', |
455
|
|
|
'route_params' => ['foo' => 'bar'], |
456
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
457
|
|
|
'roles' => ['bar'], |
458
|
|
|
], |
459
|
|
|
[ |
460
|
|
|
'admin' => '', |
461
|
|
|
'label' => 'route_label4', |
462
|
|
|
'route' => 'FooRoute4', |
463
|
|
|
'route_params' => ['foo' => 'bar'], |
464
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
465
|
|
|
'roles' => ['baz'], |
466
|
|
|
], |
467
|
|
|
], |
468
|
|
|
'roles' => ['foo', 'bar'], |
469
|
|
|
'item_adds' => [], |
470
|
|
|
], |
471
|
|
|
], [ |
472
|
|
|
// group for one role, children with different roles |
473
|
|
|
[ |
474
|
|
|
'label' => 'foo', |
475
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
476
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
477
|
|
|
'items' => [ |
478
|
|
|
[ |
479
|
|
|
'admin' => '', |
480
|
|
|
'label' => 'route_label1', |
481
|
|
|
'route' => 'FooRoute1', |
482
|
|
|
'route_params' => ['foo' => 'bar'], |
483
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
484
|
|
|
'roles' => ['foo', 'bar'], |
485
|
|
|
], |
486
|
|
|
[ |
487
|
|
|
'admin' => '', |
488
|
|
|
'label' => 'route_label2', |
489
|
|
|
'route' => 'FooRoute2', |
490
|
|
|
'route_params' => ['foo' => 'bar'], |
491
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
492
|
|
|
'roles' => ['foo'], |
493
|
|
|
], |
494
|
|
|
[ |
495
|
|
|
'admin' => '', |
496
|
|
|
'label' => 'route_label3', |
497
|
|
|
'route' => 'FooRoute3', |
498
|
|
|
'route_params' => ['foo' => 'bar'], |
499
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
500
|
|
|
'roles' => ['bar'], |
501
|
|
|
], |
502
|
|
|
[ |
503
|
|
|
'admin' => '', |
504
|
|
|
'label' => 'route_label4', |
505
|
|
|
'route' => 'FooRoute4', |
506
|
|
|
'route_params' => ['foo' => 'bar'], |
507
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
508
|
|
|
'roles' => ['baz'], |
509
|
|
|
], |
510
|
|
|
], |
511
|
|
|
'roles' => ['baz'], |
512
|
|
|
'item_adds' => [], |
513
|
|
|
], |
514
|
|
|
], |
515
|
|
|
]; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
public function getAdminGroupsMultipleRolesOnTop(): array |
519
|
|
|
{ |
520
|
|
|
return [ |
521
|
|
|
[ |
522
|
|
|
[ |
523
|
|
|
'label' => 'foo1', |
524
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
525
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
526
|
|
|
'items' => [ |
527
|
|
|
[ |
528
|
|
|
'admin' => '', |
529
|
|
|
'label' => 'route_label1', |
530
|
|
|
'route' => 'FooRoute1', |
531
|
|
|
'route_params' => ['foo' => 'bar'], |
532
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
533
|
|
|
], |
534
|
|
|
], |
535
|
|
|
'item_adds' => [], |
536
|
|
|
'roles' => ['foo', 'bar'], |
537
|
|
|
'on_top' => true, |
538
|
|
|
], |
539
|
|
|
], [ |
540
|
|
|
[ |
541
|
|
|
'label' => 'foo2', |
542
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
543
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
544
|
|
|
'items' => [ |
545
|
|
|
[ |
546
|
|
|
'admin' => '', |
547
|
|
|
'label' => 'route_label2', |
548
|
|
|
'route' => 'FooRoute2', |
549
|
|
|
'route_params' => ['foo' => 'bar'], |
550
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
551
|
|
|
], |
552
|
|
|
], |
553
|
|
|
'item_adds' => [], |
554
|
|
|
'roles' => ['foo'], |
555
|
|
|
'on_top' => true, |
556
|
|
|
], |
557
|
|
|
], [ |
558
|
|
|
[ |
559
|
|
|
'label' => 'foo3', |
560
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
561
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
562
|
|
|
'items' => [ |
563
|
|
|
[ |
564
|
|
|
'admin' => '', |
565
|
|
|
'label' => 'route_label3', |
566
|
|
|
'route' => 'FooRoute3', |
567
|
|
|
'route_params' => ['foo' => 'bar'], |
568
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
569
|
|
|
], |
570
|
|
|
], |
571
|
|
|
'item_adds' => [], |
572
|
|
|
'roles' => ['bar'], |
573
|
|
|
'on_top' => true, |
574
|
|
|
], |
575
|
|
|
], |
576
|
|
|
]; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
public function getAdminGroupsWithOnTopOption(): array |
580
|
|
|
{ |
581
|
|
|
return [ |
582
|
|
|
[ |
583
|
|
|
'foo' => [ |
584
|
|
|
'label' => 'foo_on_top', |
585
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
586
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
587
|
|
|
'on_top' => true, |
588
|
|
|
'items' => [ |
589
|
|
|
[ |
590
|
|
|
'admin' => 'sonata_admin_foo_service', |
591
|
|
|
'label' => 'fooLabel', |
592
|
|
|
'route_absolute' => UrlGeneratorInterface::ABSOLUTE_PATH, |
593
|
|
|
'route_params' => [], |
594
|
|
|
], |
595
|
|
|
], |
596
|
|
|
'item_adds' => [], |
597
|
|
|
'roles' => [], |
598
|
|
|
], |
599
|
|
|
], |
600
|
|
|
]; |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
public function getRootMenuItemWithDifferentUrlTypes(): iterable |
604
|
|
|
{ |
605
|
|
|
yield 'absolute_url' => [ |
606
|
|
|
'http://sonata-project/list', |
607
|
|
|
[ |
608
|
|
|
'label' => 'foo', |
609
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
610
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
611
|
|
|
'items' => [ |
612
|
|
|
[ |
613
|
|
|
'admin' => 'sonata_admin_absolute_url', |
614
|
|
|
'label' => 'fooLabel', |
615
|
|
|
'route' => 'FooAbsoulteRoute', |
616
|
|
|
'route_absolute' => true, |
617
|
|
|
], |
618
|
|
|
], |
619
|
|
|
'item_adds' => [], |
620
|
|
|
'roles' => ['foo'], |
621
|
|
|
], |
622
|
|
|
]; |
623
|
|
|
|
624
|
|
|
yield 'absolute_path' => [ |
625
|
|
|
'/list', |
626
|
|
|
[ |
627
|
|
|
'label' => 'foo', |
628
|
|
|
'icon' => '<i class="fa fa-edit"></i>', |
629
|
|
|
'label_catalogue' => 'SonataAdminBundle', |
630
|
|
|
'items' => [ |
631
|
|
|
[ |
632
|
|
|
'admin' => 'sonata_admin_absolute_url', |
633
|
|
|
'label' => 'fooLabel', |
634
|
|
|
'route' => 'FooAbsolutePath', |
635
|
|
|
'route_absolute' => false, |
636
|
|
|
], |
637
|
|
|
], |
638
|
|
|
'item_adds' => [], |
639
|
|
|
'roles' => ['foo'], |
640
|
|
|
], |
641
|
|
|
]; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
private function getAdminMock(bool $hasRoute = true, bool $isGranted = true): AbstractAdmin |
645
|
|
|
{ |
646
|
|
|
$admin = $this->createMock(AbstractAdmin::class); |
647
|
|
|
$admin->expects($this->once()) |
648
|
|
|
->method('hasRoute') |
649
|
|
|
->with($this->equalTo('list')) |
650
|
|
|
->willReturn($hasRoute); |
651
|
|
|
|
652
|
|
|
$admin |
653
|
|
|
->method('hasAccess') |
654
|
|
|
->with($this->equalTo('list')) |
655
|
|
|
->willReturn($isGranted); |
656
|
|
|
|
657
|
|
|
$admin |
658
|
|
|
->method('getLabel') |
659
|
|
|
->willReturn('foo_admin_label'); |
660
|
|
|
|
661
|
|
|
$admin |
662
|
|
|
->method('generateMenuUrl') |
663
|
|
|
->willReturnCallback(static function (string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): array { |
664
|
|
|
if (!\in_array($referenceType, [UrlGeneratorInterface::ABSOLUTE_URL, UrlGeneratorInterface::ABSOLUTE_PATH], true)) { |
665
|
|
|
throw new \InvalidArgumentException(sprintf( |
666
|
|
|
'Dummy router does not support the reference type "%s".', |
667
|
|
|
$referenceType |
668
|
|
|
)); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
return [ |
672
|
|
|
'route' => $name, |
673
|
|
|
'routeParameters' => $parameters, |
674
|
|
|
'routeAbsolute' => UrlGeneratorInterface::ABSOLUTE_URL === $referenceType, |
675
|
|
|
]; |
676
|
|
|
}); |
677
|
|
|
|
678
|
|
|
$admin |
679
|
|
|
->method('getTranslationDomain') |
680
|
|
|
->willReturn('SonataAdminBundle'); |
681
|
|
|
|
682
|
|
|
return $admin; |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.