1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\AdminBundle\Tests\Controller; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Prophecy\Argument; |
16
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
17
|
|
|
use Sonata\AdminBundle\Admin\AdminHelper; |
18
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
19
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
20
|
|
|
use Sonata\AdminBundle\Controller\HelperController; |
21
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridInterface; |
22
|
|
|
use Sonata\AdminBundle\Datagrid\Pager; |
23
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
24
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
25
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo; |
26
|
|
|
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension; |
27
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
28
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
29
|
|
|
use Symfony\Bridge\Twig\Command\DebugCommand; |
30
|
|
|
use Symfony\Bridge\Twig\Extension\FormExtension; |
31
|
|
|
use Symfony\Bridge\Twig\Form\TwigRenderer; |
32
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
33
|
|
|
use Symfony\Component\Form\Form; |
34
|
|
|
use Symfony\Component\Form\FormBuilder; |
35
|
|
|
use Symfony\Component\Form\FormConfigInterface; |
36
|
|
|
use Symfony\Component\Form\FormRenderer; |
37
|
|
|
use Symfony\Component\Form\FormView; |
38
|
|
|
use Symfony\Component\HttpFoundation\Request; |
39
|
|
|
use Symfony\Component\HttpFoundation\Response; |
40
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
41
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
42
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
43
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
44
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
45
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
46
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
47
|
|
|
use Twig\Environment; |
48
|
|
|
use Twig\Template; |
49
|
|
|
use Twig\TemplateWrapper; |
50
|
|
|
|
51
|
|
|
class AdminControllerHelper_Foo |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
private $bar; |
54
|
|
|
|
55
|
|
|
public function getAdminTitle() |
56
|
|
|
{ |
57
|
|
|
return 'foo'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function setEnabled($value) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setBar(AdminControllerHelper_Bar $bar) |
65
|
|
|
{ |
66
|
|
|
$this->bar = $bar; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getBar() |
70
|
|
|
{ |
71
|
|
|
return $this->bar; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
class AdminControllerHelper_Bar |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
public function getAdminTitle() |
78
|
|
|
{ |
79
|
|
|
return 'bar'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setEnabled($value) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getEnabled() |
87
|
|
|
{ |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
class HelperControllerTest extends TestCase |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* @var Pool |
95
|
|
|
*/ |
96
|
|
|
private $pool; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var Environment |
100
|
|
|
*/ |
101
|
|
|
private $twig; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @var AdminHelper |
105
|
|
|
*/ |
106
|
|
|
private $helper; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var ValidatorInterface |
110
|
|
|
*/ |
111
|
|
|
private $validator; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @var AbstractAdmin |
115
|
|
|
*/ |
116
|
|
|
private $admin; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @var HelperController |
120
|
|
|
*/ |
121
|
|
|
private $controller; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
protected function setUp() |
127
|
|
|
{ |
128
|
|
|
$this->pool = $this->prophesize(Pool::class); |
129
|
|
|
$this->twig = $this->prophesize(Environment::class); |
130
|
|
|
$this->helper = $this->prophesize(AdminHelper::class); |
131
|
|
|
$this->validator = $this->prophesize(ValidatorInterface::class); |
132
|
|
|
$this->admin = $this->prophesize(AbstractAdmin::class); |
133
|
|
|
|
134
|
|
|
$this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal()); |
135
|
|
|
$this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled(); |
136
|
|
|
|
137
|
|
|
$this->controller = new HelperController( |
138
|
|
|
$this->twig->reveal(), |
139
|
|
|
$this->pool->reveal(), |
140
|
|
|
$this->helper->reveal(), |
141
|
|
|
$this->validator->reveal() |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testGetShortObjectDescriptionActionInvalidAdmin() |
146
|
|
|
{ |
147
|
|
|
$this->expectException(NotFoundHttpException::class); |
148
|
|
|
|
149
|
|
|
$request = new Request([ |
150
|
|
|
'code' => 'sonata.post.admin', |
151
|
|
|
'objectId' => 42, |
152
|
|
|
'uniqid' => 'asdasd123', |
153
|
|
|
]); |
154
|
|
|
|
155
|
|
|
$this->pool->getInstance('sonata.post.admin')->willReturn(null); |
|
|
|
|
156
|
|
|
$this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled(); |
|
|
|
|
157
|
|
|
|
158
|
|
|
$this->controller->getShortObjectDescriptionAction($request); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testGetShortObjectDescriptionActionObjectDoesNotExist() |
162
|
|
|
{ |
163
|
|
|
$this->expectException(\RuntimeException::class); |
164
|
|
|
$this->expectExceptionMessage('Invalid format'); |
165
|
|
|
|
166
|
|
|
$request = new Request([ |
167
|
|
|
'code' => 'sonata.post.admin', |
168
|
|
|
'objectId' => 42, |
169
|
|
|
'uniqid' => 'asdasd123', |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
173
|
|
|
$this->admin->getObject(42)->willReturn(false); |
174
|
|
|
|
175
|
|
|
$this->controller->getShortObjectDescriptionAction($request); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testGetShortObjectDescriptionActionEmptyObjectId() |
179
|
|
|
{ |
180
|
|
|
$request = new Request([ |
181
|
|
|
'code' => 'sonata.post.admin', |
182
|
|
|
'objectId' => '', |
183
|
|
|
'uniqid' => 'asdasd123', |
184
|
|
|
'_format' => 'html', |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
188
|
|
|
$this->admin->getObject(null)->willReturn(false); |
189
|
|
|
|
190
|
|
|
$response = $this->controller->getShortObjectDescriptionAction($request); |
191
|
|
|
|
192
|
|
|
$this->assertInstanceOf(Response::class, $response); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testGetShortObjectDescriptionActionObject() |
196
|
|
|
{ |
197
|
|
|
$request = new Request([ |
198
|
|
|
'code' => 'sonata.post.admin', |
199
|
|
|
'objectId' => 42, |
200
|
|
|
'uniqid' => 'asdasd123', |
201
|
|
|
'_format' => 'html', |
202
|
|
|
]); |
203
|
|
|
$object = new AdminControllerHelper_Foo(); |
204
|
|
|
|
205
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
206
|
|
|
$this->admin->getObject(42)->willReturn($object); |
207
|
|
|
$this->admin->getTemplate('short_object_description')->willReturn('template'); |
|
|
|
|
208
|
|
|
$this->admin->toString($object)->willReturn('bar'); |
|
|
|
|
209
|
|
|
$this->twig->render('template', [ |
|
|
|
|
210
|
|
|
'admin' => $this->admin->reveal(), |
|
|
|
|
211
|
|
|
'description' => 'bar', |
212
|
|
|
'object' => $object, |
213
|
|
|
'link_parameters' => [], |
214
|
|
|
])->willReturn('renderedTemplate'); |
215
|
|
|
|
216
|
|
|
$response = $this->controller->getShortObjectDescriptionAction($request); |
217
|
|
|
|
218
|
|
|
$this->assertSame('renderedTemplate', $response->getContent()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testSetObjectFieldValueAction() |
222
|
|
|
{ |
223
|
|
|
$object = new AdminControllerHelper_Foo(); |
224
|
|
|
$request = new Request([ |
225
|
|
|
'code' => 'sonata.post.admin', |
226
|
|
|
'objectId' => 42, |
227
|
|
|
'field' => 'enabled', |
228
|
|
|
'value' => 1, |
229
|
|
|
'context' => 'list', |
230
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
231
|
|
|
|
232
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
233
|
|
|
$pool = $this->prophesize(Pool::class); |
234
|
|
|
$template = $this->prophesize(Template::class); |
235
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
236
|
|
|
$propertyAccessor = new PropertyAccessor(); |
237
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
238
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
239
|
|
|
|
240
|
|
|
$this->admin->getObject(42)->willReturn($object); |
241
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
242
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
243
|
|
|
$this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
244
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
245
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
246
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
247
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
248
|
|
|
$this->twig->getExtension(SonataAdminExtension::class)->willReturn( |
|
|
|
|
249
|
|
|
new SonataAdminExtension($pool->reveal(), null, $translator->reveal(), $container->reveal()) |
250
|
|
|
); |
251
|
|
|
$this->twig->load('admin_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal())); |
|
|
|
|
252
|
|
|
$this->twig->isDebug()->willReturn(false); |
|
|
|
|
253
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
254
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
255
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
256
|
|
|
$fieldDescription->getTemplate()->willReturn(false); |
257
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
258
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
259
|
|
|
|
260
|
|
|
$response = $this->controller->setObjectFieldValueAction($request); |
261
|
|
|
|
262
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function testSetObjectFieldValueActionOnARelationField() |
266
|
|
|
{ |
267
|
|
|
$object = new AdminControllerHelper_Foo(); |
268
|
|
|
$associationObject = new AdminControllerHelper_Bar(); |
269
|
|
|
$request = new Request([ |
270
|
|
|
'code' => 'sonata.post.admin', |
271
|
|
|
'objectId' => 42, |
272
|
|
|
'field' => 'bar', |
273
|
|
|
'value' => 1, |
274
|
|
|
'context' => 'list', |
275
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
276
|
|
|
|
277
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
278
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
279
|
|
|
$template = $this->prophesize(Template::class); |
280
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
281
|
|
|
$propertyAccessor = new PropertyAccessor(); |
282
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
283
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
284
|
|
|
|
285
|
|
|
$this->admin->getObject(42)->willReturn($object); |
286
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
287
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
288
|
|
|
$this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal()); |
|
|
|
|
289
|
|
|
$this->admin->getClass()->willReturn(get_class($object)); |
|
|
|
|
290
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
291
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
292
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
293
|
|
|
$this->admin->getModelManager()->willReturn($modelManager->reveal()); |
|
|
|
|
294
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
295
|
|
|
$this->twig->getExtension(SonataAdminExtension::class)->willReturn( |
|
|
|
|
296
|
|
|
new SonataAdminExtension($this->pool->reveal(), null, $translator->reveal(), $container->reveal()) |
|
|
|
|
297
|
|
|
); |
298
|
|
|
$this->twig->load('field_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal())); |
|
|
|
|
299
|
|
|
$this->twig->isDebug()->willReturn(false); |
|
|
|
|
300
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
301
|
|
|
$fieldDescription->getType()->willReturn('choice'); |
302
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
303
|
|
|
$fieldDescription->getOption('class')->willReturn(AdminControllerHelper_Bar::class); |
304
|
|
|
$fieldDescription->getTargetEntity()->willReturn(AdminControllerHelper_Bar::class); |
305
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
306
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
307
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
308
|
|
|
$modelManager->find(get_class($associationObject), 1)->willReturn($associationObject); |
309
|
|
|
|
310
|
|
|
$response = $this->controller->setObjectFieldValueAction($request); |
311
|
|
|
|
312
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function testAppendFormFieldElementAction() |
316
|
|
|
{ |
317
|
|
|
$object = new AdminControllerHelper_Foo(); |
318
|
|
|
$request = new Request([ |
319
|
|
|
'code' => 'sonata.post.admin', |
320
|
|
|
'objectId' => 42, |
321
|
|
|
'field' => 'enabled', |
322
|
|
|
'value' => 1, |
323
|
|
|
'context' => 'list', |
324
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST']); |
325
|
|
|
|
326
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
327
|
|
|
$formView = new FormView(); |
328
|
|
|
$form = $this->prophesize(Form::class); |
329
|
|
|
|
330
|
|
|
$renderer = $this->configureFormRenderer(); |
331
|
|
|
|
332
|
|
|
$this->admin->getModelManager()->willReturn($modelManager->reveal()); |
|
|
|
|
333
|
|
|
$this->admin->getClass()->willReturn(get_class($object)); |
|
|
|
|
334
|
|
|
$this->admin->setSubject($object)->shouldBeCalled(); |
|
|
|
|
335
|
|
|
$this->admin->getFormTheme()->willReturn($formView); |
|
|
|
|
336
|
|
|
$this->helper->appendFormFieldElement($this->admin->reveal(), $object, null)->willReturn([ |
|
|
|
|
337
|
|
|
$this->prophesize(FieldDescriptionInterface::class), |
338
|
|
|
$form->reveal(), |
339
|
|
|
]); |
340
|
|
|
$this->helper->getChildFormView($formView, null) |
|
|
|
|
341
|
|
|
->willReturn($formView); |
342
|
|
|
$modelManager->find(get_class($object), 42)->willReturn($object); |
343
|
|
|
$form->createView()->willReturn($formView); |
344
|
|
|
$renderer->setTheme($formView, $formView)->shouldBeCalled(); |
345
|
|
|
$renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block'); |
346
|
|
|
|
347
|
|
|
$response = $this->controller->appendFormFieldElementAction($request); |
348
|
|
|
|
349
|
|
|
$this->isInstanceOf(Response::class, $response); |
350
|
|
|
$this->assertSame($response->getContent(), 'block'); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function testRetrieveFormFieldElementAction() |
354
|
|
|
{ |
355
|
|
|
$object = new AdminControllerHelper_Foo(); |
356
|
|
|
$request = new Request([ |
357
|
|
|
'code' => 'sonata.post.admin', |
358
|
|
|
'objectId' => 42, |
359
|
|
|
'field' => 'enabled', |
360
|
|
|
'value' => 1, |
361
|
|
|
'context' => 'list', |
362
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST']); |
363
|
|
|
|
364
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
365
|
|
|
$formView = new FormView(); |
366
|
|
|
$form = $this->prophesize(Form::class); |
367
|
|
|
$formBuilder = $this->prophesize(FormBuilder::class); |
368
|
|
|
|
369
|
|
|
$renderer = $this->configureFormRenderer(); |
370
|
|
|
|
371
|
|
|
$this->admin->getModelManager()->willReturn($modelManager->reveal()); |
|
|
|
|
372
|
|
|
$this->admin->getClass()->willReturn(get_class($object)); |
|
|
|
|
373
|
|
|
$this->admin->setSubject($object)->shouldBeCalled(); |
|
|
|
|
374
|
|
|
$this->admin->getFormTheme()->willReturn($formView); |
|
|
|
|
375
|
|
|
$this->admin->getFormBuilder()->willReturn($formBuilder->reveal()); |
|
|
|
|
376
|
|
|
$this->helper->getChildFormView($formView, null) |
|
|
|
|
377
|
|
|
->willReturn($formView); |
378
|
|
|
$modelManager->find(get_class($object), 42)->willReturn($object); |
379
|
|
|
$form->setData($object)->shouldBeCalled(); |
380
|
|
|
$form->handleRequest($request)->shouldBeCalled(); |
381
|
|
|
$form->createView()->willReturn($formView); |
382
|
|
|
$formBuilder->getForm()->willReturn($form->reveal()); |
383
|
|
|
$renderer->setTheme($formView, $formView)->shouldBeCalled(); |
384
|
|
|
$renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block'); |
385
|
|
|
|
386
|
|
|
$response = $this->controller->retrieveFormFieldElementAction($request); |
387
|
|
|
|
388
|
|
|
$this->isInstanceOf(Response::class, $response); |
389
|
|
|
$this->assertSame($response->getContent(), 'block'); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function testSetObjectFieldValueActionWithViolations() |
393
|
|
|
{ |
394
|
|
|
$bar = new AdminControllerHelper_Bar(); |
395
|
|
|
$object = new AdminControllerHelper_Foo(); |
396
|
|
|
$object->setBar($bar); |
397
|
|
|
$request = new Request([ |
398
|
|
|
'code' => 'sonata.post.admin', |
399
|
|
|
'objectId' => 42, |
400
|
|
|
'field' => 'bar.enabled', |
401
|
|
|
'value' => 1, |
402
|
|
|
'context' => 'list', |
403
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
404
|
|
|
|
405
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
406
|
|
|
$propertyAccessor = new PropertyAccessor(); |
407
|
|
|
|
408
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
409
|
|
|
$this->admin->getObject(42)->willReturn($object); |
410
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
411
|
|
|
$this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
412
|
|
|
$this->validator->validate($bar)->willReturn(new ConstraintViolationList([ |
|
|
|
|
413
|
|
|
new ConstraintViolation('error1', null, [], null, 'enabled', null), |
414
|
|
|
new ConstraintViolation('error2', null, [], null, 'enabled', null), |
415
|
|
|
])); |
416
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
417
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
418
|
|
|
|
419
|
|
|
$response = $this->controller->setObjectFieldValueAction($request); |
420
|
|
|
|
421
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
422
|
|
|
$this->assertSame(json_encode("error1\nerror2"), $response->getContent()); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function testRetrieveAutocompleteItemsActionNotGranted() |
426
|
|
|
{ |
427
|
|
|
$this->expectException(AccessDeniedException::class); |
428
|
|
|
|
429
|
|
|
$request = new Request([ |
430
|
|
|
'admin_code' => 'foo.admin', |
431
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
432
|
|
|
|
433
|
|
|
$this->admin->hasAccess('create')->willReturn(false); |
|
|
|
|
434
|
|
|
$this->admin->hasAccess('edit')->willReturn(false); |
|
|
|
|
435
|
|
|
|
436
|
|
|
$this->controller->retrieveAutocompleteItemsAction($request); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
public function testRetrieveAutocompleteItemsActionDisabledFormelememt() |
440
|
|
|
{ |
441
|
|
|
$this->expectException(AccessDeniedException::class); |
442
|
|
|
$this->expectExceptionMessage('Autocomplete list can`t be retrieved because the form element is disabled or read_only.'); |
443
|
|
|
|
444
|
|
|
$object = new AdminControllerHelper_Foo(); |
445
|
|
|
$request = new Request([ |
446
|
|
|
'admin_code' => 'foo.admin', |
447
|
|
|
'field' => 'barField', |
448
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
449
|
|
|
|
450
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
451
|
|
|
|
452
|
|
|
$this->configureFormConfig('barField', true); |
453
|
|
|
|
454
|
|
|
$this->admin->getNewInstance()->willReturn($object); |
455
|
|
|
$this->admin->setSubject($object)->shouldBeCalled(); |
|
|
|
|
456
|
|
|
$this->admin->hasAccess('create')->willReturn(true); |
|
|
|
|
457
|
|
|
$this->admin->getFormFieldDescriptions()->willReturn(null); |
|
|
|
|
458
|
|
|
$this->admin->getFormFieldDescription('barField')->willReturn($fieldDescription->reveal()); |
|
|
|
|
459
|
|
|
|
460
|
|
|
$fieldDescription->getTargetEntity()->willReturn(Foo::class); |
461
|
|
|
$fieldDescription->getName()->willReturn('barField'); |
462
|
|
|
|
463
|
|
|
$this->controller->retrieveAutocompleteItemsAction($request); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
public function testRetrieveAutocompleteItemsTooShortSearchString() |
467
|
|
|
{ |
468
|
|
|
$object = new AdminControllerHelper_Foo(); |
469
|
|
|
$request = new Request([ |
470
|
|
|
'admin_code' => 'foo.admin', |
471
|
|
|
'field' => 'barField', |
472
|
|
|
'q' => 'so', |
473
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
474
|
|
|
|
475
|
|
|
$targetAdmin = $this->prophesize(AbstractAdmin::class); |
476
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
477
|
|
|
|
478
|
|
|
$this->configureFormConfig('barField'); |
479
|
|
|
|
480
|
|
|
$this->admin->getNewInstance()->willReturn($object); |
481
|
|
|
$this->admin->setSubject($object)->shouldBeCalled(); |
|
|
|
|
482
|
|
|
$this->admin->hasAccess('create')->willReturn(true); |
|
|
|
|
483
|
|
|
$this->admin->getFormFieldDescription('barField')->willReturn($fieldDescription->reveal()); |
|
|
|
|
484
|
|
|
$this->admin->getFormFieldDescriptions()->willReturn(null); |
|
|
|
|
485
|
|
|
$targetAdmin->checkAccess('list')->willReturn(null); |
486
|
|
|
$fieldDescription->getTargetEntity()->willReturn(Foo::class); |
487
|
|
|
$fieldDescription->getName()->willReturn('barField'); |
488
|
|
|
$fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal()); |
489
|
|
|
|
490
|
|
|
$response = $this->controller->retrieveAutocompleteItemsAction($request); |
491
|
|
|
|
492
|
|
|
$this->isInstanceOf(Response::class, $response); |
493
|
|
|
$this->assertSame('application/json', $response->headers->get('Content-Type')); |
494
|
|
|
$this->assertSame('{"status":"KO","message":"Too short search string."}', $response->getContent()); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
public function testRetrieveAutocompleteItems() |
498
|
|
|
{ |
499
|
|
|
$entity = new Foo(); |
500
|
|
|
$request = new Request([ |
501
|
|
|
'admin_code' => 'foo.admin', |
502
|
|
|
'field' => 'barField', |
503
|
|
|
'q' => 'sonata', |
504
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
505
|
|
|
|
506
|
|
|
$targetAdmin = $this->prophesize(AbstractAdmin::class); |
507
|
|
|
$datagrid = $this->prophesize(DatagridInterface::class); |
508
|
|
|
$metadata = $this->prophesize(Metadata::class); |
509
|
|
|
$pager = $this->prophesize(Pager::class); |
510
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
511
|
|
|
|
512
|
|
|
$this->configureFormConfig('barField'); |
513
|
|
|
|
514
|
|
|
$this->admin->getNewInstance()->willReturn($entity); |
515
|
|
|
$this->admin->setSubject($entity)->shouldBeCalled(); |
|
|
|
|
516
|
|
|
$this->admin->hasAccess('create')->willReturn(true); |
|
|
|
|
517
|
|
|
$this->admin->getFormFieldDescription('barField')->willReturn($fieldDescription->reveal()); |
|
|
|
|
518
|
|
|
$this->admin->getFormFieldDescriptions()->willReturn(null); |
|
|
|
|
519
|
|
|
$this->admin->id($entity)->willReturn(123); |
|
|
|
|
520
|
|
|
$targetAdmin->checkAccess('list')->willReturn(null); |
521
|
|
|
$targetAdmin->setPersistFilters(false)->willReturn(null); |
522
|
|
|
$targetAdmin->setFilterPersister(null)->willReturn(null); |
523
|
|
|
$targetAdmin->getDatagrid()->willReturn($datagrid->reveal()); |
524
|
|
|
$targetAdmin->getObjectMetadata($entity)->willReturn($metadata->reveal()); |
525
|
|
|
$metadata->getTitle()->willReturn('FOO'); |
526
|
|
|
$datagrid->hasFilter('foo')->willReturn(true); |
527
|
|
|
$datagrid->setValue('foo', null, 'sonata')->shouldBeCalled(); |
528
|
|
|
$datagrid->setValue('_per_page', null, 10)->shouldBeCalled(); |
529
|
|
|
$datagrid->setValue('_page', null, 1)->shouldBeCalled(); |
530
|
|
|
$datagrid->buildPager()->willReturn(null); |
531
|
|
|
$datagrid->getPager()->willReturn($pager->reveal()); |
532
|
|
|
$pager->getResults()->willReturn([$entity]); |
533
|
|
|
$pager->isLastPage()->willReturn(true); |
534
|
|
|
$fieldDescription->getTargetEntity()->willReturn(Foo::class); |
535
|
|
|
$fieldDescription->getName()->willReturn('barField'); |
536
|
|
|
$fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal()); |
537
|
|
|
|
538
|
|
|
$response = $this->controller->retrieveAutocompleteItemsAction($request); |
539
|
|
|
|
540
|
|
|
$this->isInstanceOf(Response::class, $response); |
541
|
|
|
$this->assertSame('application/json', $response->headers->get('Content-Type')); |
542
|
|
|
$this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent()); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
private function configureFormConfig($field, $disabled = false) |
546
|
|
|
{ |
547
|
|
|
$form = $this->prophesize(Form::class); |
548
|
|
|
$formType = $this->prophesize(Form::class); |
549
|
|
|
$formConfig = $this->prophesize(FormConfigInterface::class); |
550
|
|
|
|
551
|
|
|
$this->admin->getForm()->willReturn($form->reveal()); |
|
|
|
|
552
|
|
|
$form->get($field)->willReturn($formType->reveal()); |
553
|
|
|
$formType->getConfig()->willReturn($formConfig->reveal()); |
554
|
|
|
$formConfig->getAttribute('disabled')->willReturn($disabled); |
555
|
|
|
$formConfig->getAttribute('property')->willReturn('foo'); |
556
|
|
|
$formConfig->getAttribute('callback')->willReturn(null); |
557
|
|
|
$formConfig->getAttribute('minimum_input_length')->willReturn(3); |
558
|
|
|
$formConfig->getAttribute('items_per_page')->willReturn(10); |
559
|
|
|
$formConfig->getAttribute('req_param_name_page_number')->willReturn('_page'); |
560
|
|
|
$formConfig->getAttribute('to_string_callback')->willReturn(null); |
561
|
|
|
$formConfig->getAttribute('target_admin_access_action')->willReturn('list'); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
private function configureFormRenderer() |
565
|
|
|
{ |
566
|
|
|
$runtime = $this->prophesize(FormRenderer::class); |
567
|
|
|
|
568
|
|
|
// Remove the condition when dropping sf < 3.2 |
569
|
|
|
if (!method_exists(AppVariable::class, 'getToken')) { |
570
|
|
|
$extension = $this->prophesize(FormExtension::class); |
571
|
|
|
|
572
|
|
|
$this->twig->getExtension(FormExtension::class)->willReturn($extension->reveal()); |
|
|
|
|
573
|
|
|
$extension->initRuntime($this->twig->reveal())->shouldBeCalled(); |
|
|
|
|
574
|
|
|
$extension->renderer = $runtime->reveal(); |
575
|
|
|
|
576
|
|
|
return $runtime; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
// Remove the condition when dropping sf < 3.4 |
580
|
|
|
if (!method_exists(DebugCommand::class, 'getLoaderPaths')) { |
581
|
|
|
$twigRuntime = $this->prophesize(TwigRenderer::class); |
582
|
|
|
|
583
|
|
|
$this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal()); |
584
|
|
|
$twigRuntime->setEnvironment($this->twig->reveal())->shouldBeCalled(); |
|
|
|
|
585
|
|
|
|
586
|
|
|
return $twigRuntime; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
$this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal()); |
590
|
|
|
|
591
|
|
|
return $runtime; |
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.