1 | <?php |
||
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(): void |
||
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(): void |
||
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(): void |
||
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(): void |
||
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(): void |
||
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(): void |
||
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 | $templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
||
240 | $container = $this->prophesize(ContainerInterface::class); |
||
241 | |||
242 | $this->admin->getObject(42)->willReturn($object); |
||
243 | $this->admin->getCode()->willReturn('sonata.post.admin'); |
||
244 | $this->admin->hasAccess('edit', $object)->willReturn(true); |
||
245 | $this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
||
246 | $this->admin->update($object)->shouldBeCalled(); |
||
247 | $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
||
248 | $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
||
249 | $this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
||
250 | $this->twig->getExtension(SonataAdminExtension::class)->willReturn( |
||
251 | new SonataAdminExtension($pool->reveal(), null, $translator->reveal(), $container->reveal()) |
||
252 | ); |
||
253 | $this->twig->load('admin_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal())); |
||
254 | $this->twig->isDebug()->willReturn(false); |
||
255 | $fieldDescription->getOption('editable')->willReturn(true); |
||
256 | $fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
||
257 | $fieldDescription->getType()->willReturn('boolean'); |
||
258 | $fieldDescription->getTemplate()->willReturn(false); |
||
259 | $fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
||
260 | $this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
||
261 | |||
262 | $response = $this->controller->setObjectFieldValueAction($request); |
||
263 | |||
264 | $this->assertEquals(200, $response->getStatusCode()); |
||
265 | } |
||
266 | |||
267 | public function testappendFormFieldElementAction(): void |
||
268 | { |
||
269 | $object = new AdminControllerHelper_Foo(); |
||
270 | $associationObject = new AdminControllerHelper_Bar(); |
||
271 | $request = new Request([ |
||
272 | 'code' => 'sonata.post.admin', |
||
273 | 'objectId' => 42, |
||
274 | 'field' => 'bar', |
||
275 | 'value' => 1, |
||
276 | 'context' => 'list', |
||
277 | ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
||
278 | |||
279 | $fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
||
280 | $modelManager = $this->prophesize(ModelManagerInterface::class); |
||
281 | $template = $this->prophesize(Template::class); |
||
282 | $translator = $this->prophesize(TranslatorInterface::class); |
||
283 | $propertyAccessor = new PropertyAccessor(); |
||
284 | $templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
||
285 | $container = $this->prophesize(ContainerInterface::class); |
||
286 | |||
287 | $this->admin->getObject(42)->willReturn($object); |
||
288 | $this->admin->getCode()->willReturn('sonata.post.admin'); |
||
289 | $this->admin->hasAccess('edit', $object)->willReturn(true); |
||
290 | $this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal()); |
||
291 | $this->admin->getClass()->willReturn(get_class($object)); |
||
292 | $this->admin->update($object)->shouldBeCalled(); |
||
293 | $container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
||
294 | $templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
||
295 | $this->admin->getModelManager()->willReturn($modelManager->reveal()); |
||
296 | $this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
||
297 | $this->twig->getExtension(SonataAdminExtension::class)->willReturn( |
||
298 | new SonataAdminExtension($this->pool->reveal(), null, $translator->reveal(), $container->reveal()) |
||
299 | ); |
||
300 | $this->twig->load('field_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal())); |
||
301 | $this->twig->isDebug()->willReturn(false); |
||
302 | $this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
||
303 | $fieldDescription->getType()->willReturn('choice'); |
||
304 | $fieldDescription->getOption('editable')->willReturn(true); |
||
305 | $fieldDescription->getOption('class')->willReturn(AdminControllerHelper_Bar::class); |
||
306 | $fieldDescription->getTargetEntity()->willReturn(AdminControllerHelper_Bar::class); |
||
307 | $fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
||
308 | $fieldDescription->getTemplate()->willReturn('field_template'); |
||
309 | $fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
||
310 | $modelManager->find(get_class($associationObject), 1)->willReturn($associationObject); |
||
311 | |||
312 | $response = $this->controller->setObjectFieldValueAction($request); |
||
313 | |||
314 | $this->assertEquals(200, $response->getStatusCode()); |
||
315 | } |
||
316 | |||
317 | public function testRetrieveFormFieldElementAction(): void |
||
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 | $formBuilder = $this->prophesize(FormBuilder::class); |
||
332 | |||
333 | $renderer = $this->configureFormRenderer(); |
||
334 | |||
335 | $this->admin->getModelManager()->willReturn($modelManager->reveal()); |
||
336 | $this->admin->getClass()->willReturn(get_class($object)); |
||
337 | $this->admin->setSubject($object)->shouldBeCalled(); |
||
338 | $this->admin->getFormTheme()->willReturn($formView); |
||
339 | $this->admin->getFormBuilder()->willReturn($formBuilder->reveal()); |
||
340 | $this->helper->getChildFormView($formView, null) |
||
341 | ->willReturn($formView); |
||
342 | $modelManager->find(get_class($object), 42)->willReturn($object); |
||
343 | $form->setData($object)->shouldBeCalled(); |
||
344 | $form->handleRequest($request)->shouldBeCalled(); |
||
345 | $form->createView()->willReturn($formView); |
||
346 | $formBuilder->getForm()->willReturn($form->reveal()); |
||
347 | $renderer->setTheme($formView, $formView)->shouldBeCalled(); |
||
348 | $renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block'); |
||
349 | |||
350 | $response = $this->controller->retrieveFormFieldElementAction($request); |
||
351 | |||
352 | $this->isInstanceOf(Response::class, $response); |
||
353 | $this->assertSame($response->getContent(), 'block'); |
||
354 | } |
||
355 | |||
356 | public function testSetObjectFieldValueActionWithViolations(): void |
||
357 | { |
||
358 | $bar = new AdminControllerHelper_Bar(); |
||
359 | $object = new AdminControllerHelper_Foo(); |
||
360 | $object->setBar($bar); |
||
361 | $request = new Request([ |
||
362 | 'code' => 'sonata.post.admin', |
||
363 | 'objectId' => 42, |
||
364 | 'field' => 'bar.enabled', |
||
365 | 'value' => 1, |
||
366 | 'context' => 'list', |
||
367 | ], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
||
368 | |||
369 | $fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
||
370 | $propertyAccessor = new PropertyAccessor(); |
||
371 | |||
372 | $this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
||
373 | $this->admin->getObject(42)->willReturn($object); |
||
374 | $this->admin->hasAccess('edit', $object)->willReturn(true); |
||
375 | $this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal()); |
||
376 | $this->validator->validate($bar)->willReturn(new ConstraintViolationList([ |
||
377 | new ConstraintViolation('error1', null, [], null, 'enabled', null), |
||
378 | new ConstraintViolation('error2', null, [], null, 'enabled', null), |
||
379 | ])); |
||
380 | $fieldDescription->getOption('editable')->willReturn(true); |
||
381 | $fieldDescription->getType()->willReturn('boolean'); |
||
382 | |||
383 | $response = $this->controller->setObjectFieldValueAction($request); |
||
384 | |||
385 | $this->assertEquals(400, $response->getStatusCode()); |
||
386 | $this->assertSame(json_encode("error1\nerror2"), $response->getContent()); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @exceptionMessage Invalid format |
||
391 | */ |
||
392 | public function testRetrieveAutocompleteItemsActionNotGranted(): void |
||
405 | |||
406 | public function testRetrieveAutocompleteItemsActionDisabledFormelememt(): void |
||
432 | |||
433 | public function testRetrieveAutocompleteItemsTooShortSearchString(): void |
||
463 | |||
464 | public function testRetrieveAutocompleteItems(): void |
||
465 | { |
||
466 | $entity = new Foo(); |
||
467 | $request = new Request([ |
||
468 | 'admin_code' => 'foo.admin', |
||
469 | 'field' => 'barField', |
||
470 | 'q' => 'sonata', |
||
471 | ], [], [], [], [], ['REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
||
472 | |||
473 | $targetAdmin = $this->prophesize(AbstractAdmin::class); |
||
474 | $datagrid = $this->prophesize(DatagridInterface::class); |
||
475 | $metadata = $this->prophesize(Metadata::class); |
||
476 | $pager = $this->prophesize(Pager::class); |
||
477 | $fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
||
478 | |||
479 | $this->configureFormConfig('barField'); |
||
480 | |||
481 | $this->admin->getNewInstance()->willReturn($entity); |
||
482 | $this->admin->setSubject($entity)->shouldBeCalled(); |
||
483 | $this->admin->hasAccess('create')->willReturn(true); |
||
484 | $this->admin->getFormFieldDescription('barField')->willReturn($fieldDescription->reveal()); |
||
485 | $this->admin->getFormFieldDescriptions()->willReturn(null); |
||
486 | $this->admin->id($entity)->willReturn(123); |
||
487 | $targetAdmin->checkAccess('list')->shouldBeCalled(); |
||
488 | $targetAdmin->setFilterPersister(null)->shouldBeCalled(); |
||
489 | $targetAdmin->getDatagrid()->willReturn($datagrid->reveal()); |
||
490 | $targetAdmin->getObjectMetadata($entity)->willReturn($metadata->reveal()); |
||
491 | $metadata->getTitle()->willReturn('FOO'); |
||
492 | $datagrid->hasFilter('foo')->willReturn(true); |
||
493 | $datagrid->setValue('foo', null, 'sonata')->shouldBeCalled(); |
||
494 | $datagrid->setValue('_per_page', null, 10)->shouldBeCalled(); |
||
495 | $datagrid->setValue('_page', null, 1)->shouldBeCalled(); |
||
496 | $datagrid->buildPager()->willReturn(null); |
||
497 | $datagrid->getPager()->willReturn($pager->reveal()); |
||
498 | $pager->getResults()->willReturn([$entity]); |
||
499 | $pager->isLastPage()->willReturn(true); |
||
500 | $fieldDescription->getTargetEntity()->willReturn(Foo::class); |
||
501 | $fieldDescription->getName()->willReturn('barField'); |
||
502 | $fieldDescription->getAssociationAdmin()->willReturn($targetAdmin->reveal()); |
||
503 | |||
504 | $response = $this->controller->retrieveAutocompleteItemsAction($request); |
||
505 | |||
506 | $this->isInstanceOf(Response::class, $response); |
||
507 | $this->assertSame('application/json', $response->headers->get('Content-Type')); |
||
508 | $this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent()); |
||
509 | } |
||
510 | |||
511 | private function configureFormConfig($field, $disabled = false): void |
||
529 | |||
530 | private function configureFormRenderer() |
||
559 | } |
||
560 |
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
.