|
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\Admin; |
|
15
|
|
|
|
|
16
|
|
|
use Knp\Menu\ItemInterface; |
|
17
|
|
|
use Knp\Menu\MenuFactory; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
|
20
|
|
|
use Sonata\AdminBundle\Admin\BreadcrumbsBuilder; |
|
21
|
|
|
use Sonata\AdminBundle\Route\RouteGeneratorInterface; |
|
22
|
|
|
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This test class contains unit and integration tests. Maybe it could be |
|
27
|
|
|
* separated into two classes. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Grégoire Paris <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class BreadcrumbsBuilderTest extends TestCase |
|
32
|
|
|
{ |
|
33
|
|
|
public function testChildGetBreadCrumbs(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$menu = $this->prophesize(ItemInterface::class); |
|
36
|
|
|
$menu->getParent()->willReturn(null); |
|
37
|
|
|
|
|
38
|
|
|
$dashboardMenu = $this->prophesize(ItemInterface::class); |
|
39
|
|
|
$dashboardMenu->getParent()->willReturn($menu); |
|
40
|
|
|
|
|
41
|
|
|
$adminListMenu = $this->prophesize(ItemInterface::class); |
|
42
|
|
|
$adminListMenu->getParent()->willReturn($dashboardMenu); |
|
43
|
|
|
|
|
44
|
|
|
$adminSubjectMenu = $this->prophesize(ItemInterface::class); |
|
45
|
|
|
$adminSubjectMenu->getParent()->willReturn($adminListMenu); |
|
46
|
|
|
|
|
47
|
|
|
$childMenu = $this->prophesize(ItemInterface::class); |
|
48
|
|
|
$childMenu->getParent()->willReturn($adminSubjectMenu); |
|
49
|
|
|
|
|
50
|
|
|
$leafMenu = $this->prophesize(ItemInterface::class); |
|
51
|
|
|
$leafMenu->getParent()->willReturn($childMenu); |
|
52
|
|
|
|
|
53
|
|
|
$action = 'my_action'; |
|
54
|
|
|
$breadcrumbsBuilder = new BreadcrumbsBuilder(['child_admin_route' => 'show']); |
|
55
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
|
56
|
|
|
$admin->isChild()->willReturn(false); |
|
57
|
|
|
|
|
58
|
|
|
$menuFactory = $this->prophesize(MenuFactory::class); |
|
59
|
|
|
$menuFactory->createItem('root')->willReturn($menu); |
|
60
|
|
|
$admin->getMenuFactory()->willReturn($menuFactory); |
|
61
|
|
|
$labelTranslatorStrategy = $this->prophesize( |
|
62
|
|
|
LabelTranslatorStrategyInterface::class |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$routeGenerator = $this->prophesize(RouteGeneratorInterface::class); |
|
66
|
|
|
$routeGenerator->generate('sonata_admin_dashboard')->willReturn('/dashboard'); |
|
67
|
|
|
|
|
68
|
|
|
$admin->getRouteGenerator()->willReturn($routeGenerator->reveal()); |
|
69
|
|
|
$menu->addChild('link_breadcrumb_dashboard', [ |
|
70
|
|
|
'uri' => '/dashboard', |
|
71
|
|
|
'extras' => [ |
|
72
|
|
|
'translation_domain' => 'SonataAdminBundle', |
|
73
|
|
|
], |
|
74
|
|
|
])->willReturn( |
|
75
|
|
|
$dashboardMenu->reveal() |
|
76
|
|
|
); |
|
77
|
|
|
$labelTranslatorStrategy->getLabel( |
|
78
|
|
|
'my_class_name_list', |
|
79
|
|
|
'breadcrumb', |
|
80
|
|
|
'link' |
|
81
|
|
|
)->willReturn('My class'); |
|
82
|
|
|
$labelTranslatorStrategy->getLabel( |
|
83
|
|
|
'my_child_class_name_list', |
|
84
|
|
|
'breadcrumb', |
|
85
|
|
|
'link' |
|
86
|
|
|
)->willReturn('My child class'); |
|
87
|
|
|
$childAdmin = $this->prophesize(AbstractAdmin::class); |
|
88
|
|
|
$childAdmin->isChild()->willReturn(true); |
|
89
|
|
|
$childAdmin->getParent()->willReturn($admin->reveal()); |
|
90
|
|
|
$childAdmin->getTranslationDomain()->willReturn('ChildBundle'); |
|
91
|
|
|
$childAdmin->getLabelTranslatorStrategy() |
|
92
|
|
|
->shouldBeCalled() |
|
93
|
|
|
->willReturn($labelTranslatorStrategy->reveal()); |
|
94
|
|
|
$childAdmin->getClassnameLabel()->willReturn('my_child_class_name'); |
|
95
|
|
|
$childAdmin->hasRoute('list')->willReturn(true); |
|
96
|
|
|
$childAdmin->hasAccess('list')->willReturn(true); |
|
97
|
|
|
$childAdmin->generateUrl('list')->willReturn('/myadmin/my-object/mychildadmin/list'); |
|
98
|
|
|
$childAdmin->getCurrentChildAdmin()->willReturn(null); |
|
99
|
|
|
$childAdmin->hasSubject()->willReturn(true); |
|
100
|
|
|
$childAdmin->getSubject()->willReturn('my subject'); |
|
101
|
|
|
$childAdmin->toString('my subject')->willReturn('My subject'); |
|
102
|
|
|
|
|
103
|
|
|
$admin->hasAccess('show', 'my subject')->willReturn(true)->shouldBeCalled(); |
|
104
|
|
|
$admin->hasRoute('show')->willReturn(true); |
|
105
|
|
|
$admin->generateUrl('show', ['id' => 'my-object'])->willReturn('/myadmin/my-object'); |
|
106
|
|
|
|
|
107
|
|
|
$admin->trans('My class', [], null)->willReturn('Ma classe'); |
|
108
|
|
|
$admin->hasRoute('list')->willReturn(true); |
|
109
|
|
|
$admin->hasAccess('list')->willReturn(true); |
|
110
|
|
|
$admin->generateUrl('list')->willReturn('/myadmin/list'); |
|
111
|
|
|
$admin->getCurrentChildAdmin()->willReturn($childAdmin->reveal()); |
|
112
|
|
|
$request = $this->prophesize(Request::class); |
|
113
|
|
|
$request->get('slug')->willReturn('my-object'); |
|
114
|
|
|
|
|
115
|
|
|
$admin->getIdParameter()->willReturn('slug'); |
|
116
|
|
|
$admin->hasRoute('edit')->willReturn(true); |
|
117
|
|
|
$admin->hasAccess('edit')->willReturn(true); |
|
118
|
|
|
$admin->generateUrl('edit', ['id' => 'my-object'])->willReturn('/myadmin/my-object'); |
|
119
|
|
|
$admin->getRequest()->willReturn($request->reveal()); |
|
120
|
|
|
$admin->hasSubject()->willReturn(true); |
|
121
|
|
|
$admin->getSubject()->willReturn('my subject'); |
|
122
|
|
|
$admin->toString('my subject')->willReturn('My subject'); |
|
123
|
|
|
$admin->getTranslationDomain()->willReturn('FooBundle'); |
|
124
|
|
|
$admin->getLabelTranslatorStrategy()->willReturn( |
|
125
|
|
|
$labelTranslatorStrategy->reveal() |
|
126
|
|
|
); |
|
127
|
|
|
$admin->getClassnameLabel()->willReturn('my_class_name'); |
|
128
|
|
|
|
|
129
|
|
|
$dashboardMenu->addChild('My class', [ |
|
130
|
|
|
'extras' => [ |
|
131
|
|
|
'translation_domain' => 'FooBundle', |
|
132
|
|
|
], |
|
133
|
|
|
'uri' => '/myadmin/list', |
|
134
|
|
|
])->shouldBeCalled()->willReturn($adminListMenu->reveal()); |
|
135
|
|
|
|
|
136
|
|
|
$adminListMenu->addChild('My subject', [ |
|
137
|
|
|
'uri' => '/myadmin/my-object', |
|
138
|
|
|
'extras' => [ |
|
139
|
|
|
'translation_domain' => false, |
|
140
|
|
|
], |
|
141
|
|
|
])->shouldBeCalled()->willReturn($adminSubjectMenu->reveal()); |
|
142
|
|
|
|
|
143
|
|
|
$adminSubjectMenu->addChild('My child class', [ |
|
144
|
|
|
'extras' => [ |
|
145
|
|
|
'translation_domain' => 'ChildBundle', |
|
146
|
|
|
], |
|
147
|
|
|
'uri' => '/myadmin/my-object/mychildadmin/list', |
|
148
|
|
|
])->shouldBeCalled()->willReturn($childMenu->reveal()); |
|
149
|
|
|
$adminSubjectMenu->setExtra('safe_label', false)->willReturn($childMenu); |
|
150
|
|
|
|
|
151
|
|
|
$childMenu->addChild('My subject', [ |
|
152
|
|
|
'extras' => [ |
|
153
|
|
|
'translation_domain' => false, |
|
154
|
|
|
], |
|
155
|
|
|
])->shouldBeCalled()->willReturn($leafMenu->reveal()); |
|
156
|
|
|
|
|
157
|
|
|
$breadcrumbs = $breadcrumbsBuilder->getBreadcrumbs($childAdmin->reveal(), $action); |
|
158
|
|
|
$this->assertCount(5, $breadcrumbs); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function actionProvider() |
|
162
|
|
|
{ |
|
163
|
|
|
return [ |
|
164
|
|
|
['my_action'], |
|
165
|
|
|
['list'], |
|
166
|
|
|
['edit'], |
|
167
|
|
|
['create'], |
|
168
|
|
|
]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @dataProvider actionProvider |
|
173
|
|
|
*/ |
|
174
|
|
|
public function testBuildBreadcrumbs($action): void |
|
175
|
|
|
{ |
|
176
|
|
|
$breadcrumbsBuilder = new BreadcrumbsBuilder(); |
|
177
|
|
|
|
|
178
|
|
|
$menu = $this->prophesize(ItemInterface::class); |
|
179
|
|
|
$menuFactory = $this->prophesize(MenuFactory::class); |
|
180
|
|
|
$menuFactory->createItem('root')->willReturn($menu); |
|
181
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
|
182
|
|
|
$admin->getMenuFactory()->willReturn($menuFactory); |
|
183
|
|
|
$labelTranslatorStrategy = $this->prophesize(LabelTranslatorStrategyInterface::class); |
|
184
|
|
|
|
|
185
|
|
|
$routeGenerator = $this->prophesize(RouteGeneratorInterface::class); |
|
186
|
|
|
$routeGenerator->generate('sonata_admin_dashboard')->willReturn('/dashboard'); |
|
187
|
|
|
$admin->getRouteGenerator()->willReturn($routeGenerator->reveal()); |
|
188
|
|
|
$menu->addChild('link_breadcrumb_dashboard', [ |
|
189
|
|
|
'uri' => '/dashboard', |
|
190
|
|
|
'extras' => [ |
|
191
|
|
|
'translation_domain' => 'SonataAdminBundle', |
|
192
|
|
|
], |
|
193
|
|
|
])->willReturn( |
|
194
|
|
|
$menu->reveal() |
|
195
|
|
|
); |
|
196
|
|
|
$labelTranslatorStrategy->getLabel( |
|
197
|
|
|
'my_class_name_list', |
|
198
|
|
|
'breadcrumb', |
|
199
|
|
|
'link' |
|
200
|
|
|
)->willReturn('My class'); |
|
201
|
|
|
$labelTranslatorStrategy->getLabel( |
|
202
|
|
|
'my_child_class_name_list', |
|
203
|
|
|
'breadcrumb', |
|
204
|
|
|
'link' |
|
205
|
|
|
)->willReturn('My child class'); |
|
206
|
|
|
$labelTranslatorStrategy->getLabel( |
|
207
|
|
|
'my_child_class_name_my_action', |
|
208
|
|
|
'breadcrumb', |
|
209
|
|
|
'link' |
|
210
|
|
|
)->willReturn('My action'); |
|
211
|
|
|
if ('create' === $action) { |
|
212
|
|
|
$labelTranslatorStrategy->getLabel( |
|
213
|
|
|
'my_class_name_create', |
|
214
|
|
|
'breadcrumb', |
|
215
|
|
|
'link' |
|
216
|
|
|
)->willReturn('create my object'); |
|
217
|
|
|
$menu->addChild('create my object', [ |
|
218
|
|
|
'extras' => [ |
|
219
|
|
|
'translation_domain' => 'FooBundle', |
|
220
|
|
|
], |
|
221
|
|
|
])->willReturn($menu); |
|
222
|
|
|
} |
|
223
|
|
|
$childAdmin = $this->prophesize(AbstractAdmin::class); |
|
224
|
|
|
$childAdmin->getTranslationDomain()->willReturn('ChildBundle'); |
|
225
|
|
|
$childAdmin->getLabelTranslatorStrategy()->willReturn($labelTranslatorStrategy->reveal()); |
|
226
|
|
|
$childAdmin->getClassnameLabel()->willReturn('my_child_class_name'); |
|
227
|
|
|
$childAdmin->hasRoute('list')->willReturn(false); |
|
228
|
|
|
$childAdmin->getCurrentChildAdmin()->willReturn(null); |
|
229
|
|
|
$childAdmin->hasSubject()->willReturn(false); |
|
230
|
|
|
|
|
231
|
|
|
$admin->hasRoute('list')->willReturn(true); |
|
232
|
|
|
$admin->hasAccess('list')->willReturn(true); |
|
233
|
|
|
$admin->generateUrl('list')->willReturn('/myadmin/list'); |
|
234
|
|
|
$admin->getCurrentChildAdmin()->willReturn( |
|
235
|
|
|
'my_action' === $action ? $childAdmin->reveal() : false |
|
236
|
|
|
); |
|
237
|
|
|
if ('list' === $action) { |
|
238
|
|
|
$admin->isChild()->willReturn(true); |
|
239
|
|
|
$menu->setUri(false)->shouldBeCalled(); |
|
240
|
|
|
} else { |
|
241
|
|
|
$menu->setUri()->shouldNotBeCalled(); |
|
242
|
|
|
} |
|
243
|
|
|
$request = $this->prophesize(Request::class); |
|
244
|
|
|
$request->get('slug')->willReturn('my-object'); |
|
245
|
|
|
|
|
246
|
|
|
$admin->getIdParameter()->willReturn('slug'); |
|
247
|
|
|
$admin->hasRoute('edit')->willReturn(false); |
|
248
|
|
|
$admin->getRequest()->willReturn($request->reveal()); |
|
249
|
|
|
$admin->hasSubject()->willReturn(true); |
|
250
|
|
|
$admin->getSubject()->willReturn('my subject'); |
|
251
|
|
|
$admin->toString('my subject')->willReturn('My subject'); |
|
252
|
|
|
$admin->getTranslationDomain()->willReturn('FooBundle'); |
|
253
|
|
|
$admin->getLabelTranslatorStrategy()->willReturn( |
|
254
|
|
|
$labelTranslatorStrategy->reveal() |
|
255
|
|
|
); |
|
256
|
|
|
$admin->getClassnameLabel()->willReturn('my_class_name'); |
|
257
|
|
|
|
|
258
|
|
|
$menu->addChild('My class', [ |
|
259
|
|
|
'uri' => '/myadmin/list', |
|
260
|
|
|
'extras' => [ |
|
261
|
|
|
'translation_domain' => 'FooBundle', |
|
262
|
|
|
], |
|
263
|
|
|
])->willReturn($menu->reveal()); |
|
264
|
|
|
$menu->addChild('My subject', [ |
|
265
|
|
|
'extras' => [ |
|
266
|
|
|
'translation_domain' => false, |
|
267
|
|
|
], |
|
268
|
|
|
])->willReturn($menu); |
|
269
|
|
|
$menu->addChild('My subject', [ |
|
270
|
|
|
'uri' => null, |
|
271
|
|
|
'extras' => [ |
|
272
|
|
|
'translation_domain' => false, |
|
273
|
|
|
], |
|
274
|
|
|
])->willReturn($menu); |
|
275
|
|
|
$menu->addChild('My child class', [ |
|
276
|
|
|
'extras' => [ |
|
277
|
|
|
'translation_domain' => 'ChildBundle', |
|
278
|
|
|
], |
|
279
|
|
|
'uri' => null, |
|
280
|
|
|
])->willReturn($menu); |
|
281
|
|
|
$menu->setExtra('safe_label', false)->willReturn($menu); |
|
282
|
|
|
$menu->addChild('My action', [ |
|
283
|
|
|
'extras' => [ |
|
284
|
|
|
'translation_domain' => 'ChildBundle', |
|
285
|
|
|
], |
|
286
|
|
|
])->willReturn($menu); |
|
287
|
|
|
|
|
288
|
|
|
$reflection = new \ReflectionMethod('Sonata\AdminBundle\Admin\BreadcrumbsBuilder', 'buildBreadcrumbs'); |
|
289
|
|
|
$reflection->setAccessible(true); |
|
290
|
|
|
|
|
291
|
|
|
$reflection->invoke($breadcrumbsBuilder, $admin->reveal(), $action); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: