1 | <?php |
||
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() |
||
144 | |||
145 | public function testGetShortObjectDescriptionActionInvalidAdmin() |
||
160 | |||
161 | public function testGetShortObjectDescriptionActionObjectDoesNotExist() |
||
177 | |||
178 | public function testGetShortObjectDescriptionActionEmptyObjectId() |
||
194 | |||
195 | public function testGetShortObjectDescriptionActionObject() |
||
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')->shouldBeCalled(); |
||
521 | $targetAdmin->setFilterPersister(null)->shouldBeCalled(); |
||
522 | $targetAdmin->getDatagrid()->willReturn($datagrid->reveal()); |
||
523 | $targetAdmin->getObjectMetadata($entity)->willReturn($metadata->reveal()); |
||
524 | $metadata->getTitle()->willReturn('FOO'); |
||
525 | $datagrid->hasFilter('foo')->willReturn(true); |
||
526 | $datagrid->setValue('foo', null, 'sonata')->shouldBeCalled(); |
||
527 | $datagrid->setValue('_per_page', null, 10)->shouldBeCalled(); |
||
528 | $datagrid->setValue('_page', null, 1)->shouldBeCalled(); |
||
529 | $datagrid->buildPager()->willReturn(null); |
||
530 | $datagrid->getPager()->willReturn($pager->reveal()); |
||
531 | $pager->getResults()->willReturn([$entity]); |
||
532 | $pager->isLastPage()->willReturn(true); |
||
533 | $fieldDescription->getTargetEntity()->willReturn(Foo::class); |
||
534 | $fieldDescription->getName()->willReturn('barField'); |
||
535 | $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal()); |
||
536 | |||
537 | $response = $this->controller->retrieveAutocompleteItemsAction($request); |
||
538 | |||
539 | $this->isInstanceOf(Response::class, $response); |
||
540 | $this->assertSame('application/json', $response->headers->get('Content-Type')); |
||
541 | $this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent()); |
||
542 | } |
||
543 | |||
544 | private function configureFormConfig($field, $disabled = false) |
||
562 | |||
563 | private function configureFormRenderer() |
||
592 | } |
||
593 |
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
.