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