Complex classes like CRUDControllerTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CRUDControllerTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
73 | class CRUDControllerTest extends TestCase |
||
74 | { |
||
75 | use ExpectDeprecationTrait; |
||
76 | |||
77 | /** |
||
78 | * @var CRUDController |
||
79 | */ |
||
80 | private $controller; |
||
81 | |||
82 | /** |
||
83 | * @var Request |
||
84 | */ |
||
85 | private $request; |
||
86 | |||
87 | /** |
||
88 | * @var AdminInterface |
||
89 | */ |
||
90 | private $admin; |
||
91 | |||
92 | /** |
||
93 | * @var TemplateRegistryInterface |
||
94 | */ |
||
95 | private $templateRegistry; |
||
96 | |||
97 | /** |
||
98 | * @var Pool |
||
99 | */ |
||
100 | private $pool; |
||
101 | |||
102 | /** |
||
103 | * @var array |
||
104 | */ |
||
105 | private $parameters; |
||
106 | |||
107 | /** |
||
108 | * @var Session |
||
109 | */ |
||
110 | private $session; |
||
111 | |||
112 | /** |
||
113 | * @var AuditManager |
||
114 | */ |
||
115 | private $auditManager; |
||
116 | |||
117 | /** |
||
118 | * @var ContainerInterface |
||
119 | */ |
||
120 | private $container; |
||
121 | |||
122 | /** |
||
123 | * @var AdminObjectAclManipulator |
||
124 | */ |
||
125 | private $adminObjectAclManipulator; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | private $template; |
||
131 | |||
132 | /** |
||
133 | * @var array |
||
134 | */ |
||
135 | private $protectedTestedMethods; |
||
136 | |||
137 | /** |
||
138 | * @var CsrfTokenManagerInterface |
||
139 | */ |
||
140 | private $csrfProvider; |
||
141 | |||
142 | /** |
||
143 | * @var KernelInterface |
||
144 | */ |
||
145 | private $kernel; |
||
146 | |||
147 | /** |
||
148 | * @var TranslatorInterface |
||
149 | */ |
||
150 | private $translator; |
||
151 | |||
152 | /** |
||
153 | * @var Exporter |
||
154 | */ |
||
155 | private $exporter; |
||
156 | |||
157 | /** |
||
158 | * @var LoggerInterface|MockObject |
||
159 | */ |
||
160 | private $logger; |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | protected function setUp(): void |
||
166 | { |
||
167 | $this->container = new Container(); |
||
168 | $this->request = new Request(); |
||
169 | $this->pool = new Pool($this->container, 'title', 'logo.png'); |
||
170 | $this->pool->setAdminServiceIds(['foo.admin']); |
||
171 | $this->request->attributes->set('_sonata_admin', 'foo.admin'); |
||
172 | $this->admin = $this->createMock(AdminInterface::class); |
||
173 | $this->translator = $this->createMock(TranslatorInterface::class); |
||
174 | $this->parameters = []; |
||
175 | $this->template = ''; |
||
176 | |||
177 | $this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
||
178 | |||
179 | $this->session = new Session(new MockArraySessionStorage()); |
||
180 | |||
181 | $this->exporter = new Exporter([new JsonWriter(sys_get_temp_dir().'/sonataadmin/export.json')]); |
||
182 | |||
183 | $this->auditManager = $this->getMockBuilder(AuditManager::class) |
||
184 | ->disableOriginalConstructor() |
||
185 | ->getMock(); |
||
186 | |||
187 | $this->adminObjectAclManipulator = $this->getMockBuilder(AdminObjectAclManipulator::class) |
||
188 | ->disableOriginalConstructor() |
||
189 | ->getMock(); |
||
190 | |||
191 | $this->csrfProvider = $this->getMockBuilder(CsrfTokenManagerInterface::class) |
||
192 | ->getMock(); |
||
193 | |||
194 | $this->csrfProvider |
||
195 | ->method('getToken') |
||
196 | ->willReturnCallback(static function (string $intention): CsrfToken { |
||
197 | return new CsrfToken($intention, sprintf('csrf-token-123_%s', $intention)); |
||
198 | }); |
||
199 | |||
200 | $this->csrfProvider |
||
201 | ->method('isTokenValid') |
||
202 | ->willReturnCallback(static function (CsrfToken $token): bool { |
||
203 | return $token->getValue() === sprintf('csrf-token-123_%s', $token->getId()); |
||
204 | }); |
||
205 | |||
206 | $this->logger = $this->createMock(LoggerInterface::class); |
||
207 | |||
208 | $requestStack = new RequestStack(); |
||
209 | $requestStack->push($this->request); |
||
210 | |||
211 | $this->kernel = $this->createMock(KernelInterface::class); |
||
212 | |||
213 | $this->container->set('sonata.admin.pool', $this->pool); |
||
214 | $this->container->set('request_stack', $requestStack); |
||
215 | $this->container->set('foo.admin', $this->admin); |
||
216 | $this->container->set('foo.admin.template_registry', $this->templateRegistry->reveal()); |
||
217 | $this->container->set('twig', $this->createTwig()); |
||
218 | $this->container->set('session', $this->session); |
||
219 | $this->container->set('sonata.exporter.exporter', $this->exporter); |
||
220 | $this->container->set('sonata.admin.audit.manager', $this->auditManager); |
||
221 | $this->container->set('sonata.admin.object.manipulator.acl.admin', $this->adminObjectAclManipulator); |
||
222 | $this->container->set('security.csrf.token_manager', $this->csrfProvider); |
||
223 | $this->container->set('logger', $this->logger); |
||
224 | $this->container->set('kernel', $this->kernel); |
||
225 | $this->container->set('translator', $this->translator); |
||
226 | $this->container->set('sonata.admin.breadcrumbs_builder', new BreadcrumbsBuilder([])); |
||
227 | |||
228 | $this->container->setParameter( |
||
229 | 'security.role_hierarchy.roles', |
||
230 | ['ROLE_SUPER_ADMIN' => ['ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN']] |
||
231 | ); |
||
232 | $this->container->setParameter('sonata.admin.security.acl_user_manager', null); |
||
233 | |||
234 | $this->templateRegistry->getTemplate('ajax')->willReturn('@SonataAdmin/ajax_layout.html.twig'); |
||
235 | $this->templateRegistry->getTemplate('layout')->willReturn('@SonataAdmin/standard_layout.html.twig'); |
||
236 | $this->templateRegistry->getTemplate('show')->willReturn('@SonataAdmin/CRUD/show.html.twig'); |
||
237 | $this->templateRegistry->getTemplate('show_compare')->willReturn('@SonataAdmin/CRUD/show_compare.html.twig'); |
||
238 | $this->templateRegistry->getTemplate('edit')->willReturn('@SonataAdmin/CRUD/edit.html.twig'); |
||
239 | $this->templateRegistry->getTemplate('dashboard')->willReturn('@SonataAdmin/Core/dashboard.html.twig'); |
||
240 | $this->templateRegistry->getTemplate('search')->willReturn('@SonataAdmin/Core/search.html.twig'); |
||
241 | $this->templateRegistry->getTemplate('list')->willReturn('@SonataAdmin/CRUD/list.html.twig'); |
||
242 | $this->templateRegistry->getTemplate('preview')->willReturn('@SonataAdmin/CRUD/preview.html.twig'); |
||
243 | $this->templateRegistry->getTemplate('history')->willReturn('@SonataAdmin/CRUD/history.html.twig'); |
||
244 | $this->templateRegistry->getTemplate('acl')->willReturn('@SonataAdmin/CRUD/acl.html.twig'); |
||
245 | $this->templateRegistry->getTemplate('delete')->willReturn('@SonataAdmin/CRUD/delete.html.twig'); |
||
246 | $this->templateRegistry->getTemplate('batch')->willReturn('@SonataAdmin/CRUD/list__batch.html.twig'); |
||
247 | $this->templateRegistry->getTemplate('batch_confirmation')->willReturn('@SonataAdmin/CRUD/batch_confirmation.html.twig'); |
||
248 | |||
249 | $this->admin |
||
250 | ->method('getIdParameter') |
||
251 | ->willReturn('id'); |
||
252 | |||
253 | $this->admin |
||
254 | ->method('getAccessMapping') |
||
255 | ->willReturn([]); |
||
256 | |||
257 | $this->admin |
||
258 | ->method('generateUrl') |
||
259 | ->willReturnCallback( |
||
260 | static function ($name, array $parameters = []) { |
||
261 | $result = $name; |
||
262 | if (!empty($parameters)) { |
||
263 | $result .= '?'.http_build_query($parameters); |
||
264 | } |
||
265 | |||
266 | return $result; |
||
267 | } |
||
268 | ); |
||
269 | |||
270 | $this->admin |
||
271 | ->method('generateObjectUrl') |
||
272 | ->willReturnCallback( |
||
273 | static function (string $name, $object, array $parameters = []): string { |
||
274 | $result = sprintf('%s_%s', \get_class($object), $name); |
||
275 | if (!empty($parameters)) { |
||
276 | $result .= '?'.http_build_query($parameters); |
||
277 | } |
||
278 | |||
279 | return $result; |
||
280 | } |
||
281 | ); |
||
282 | |||
283 | $this->admin |
||
284 | ->method('getCode') |
||
285 | ->willReturn('foo.admin'); |
||
286 | |||
287 | $this->controller = $this->createController(); |
||
288 | |||
289 | // Make some methods public to test them |
||
290 | $testedMethods = [ |
||
291 | 'renderJson', |
||
292 | 'isXmlHttpRequest', |
||
293 | 'configure', |
||
294 | 'getBaseTemplate', |
||
295 | 'redirectTo', |
||
296 | 'addFlash', |
||
297 | ]; |
||
298 | foreach ($testedMethods as $testedMethod) { |
||
299 | $method = new \ReflectionMethod(CRUDController::class, $testedMethod); |
||
300 | |||
301 | $method->setAccessible(true); |
||
302 | $this->protectedTestedMethods[$testedMethod] = $method; |
||
303 | } |
||
304 | } |
||
305 | |||
306 | public function testRenderJson1(): void |
||
307 | { |
||
308 | $data = ['example' => '123', 'foo' => 'bar']; |
||
309 | |||
310 | $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded'); |
||
311 | $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
||
312 | |||
313 | $this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
||
314 | $this->assertSame(json_encode($data), $response->getContent()); |
||
315 | } |
||
316 | |||
317 | public function testRenderJson2(): void |
||
318 | { |
||
319 | $data = ['example' => '123', 'foo' => 'bar']; |
||
320 | |||
321 | $this->request->headers->set('Content-Type', 'multipart/form-data'); |
||
322 | $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
||
323 | |||
324 | $this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
||
325 | $this->assertSame(json_encode($data), $response->getContent()); |
||
326 | } |
||
327 | |||
328 | public function testRenderJsonAjax(): void |
||
329 | { |
||
330 | $data = ['example' => '123', 'foo' => 'bar']; |
||
331 | |||
332 | $this->request->attributes->set('_xml_http_request', true); |
||
333 | $this->request->headers->set('Content-Type', 'multipart/form-data'); |
||
334 | $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
||
335 | |||
336 | $this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
||
337 | $this->assertSame(json_encode($data), $response->getContent()); |
||
338 | } |
||
339 | |||
340 | public function testIsXmlHttpRequest(): void |
||
341 | { |
||
342 | $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
343 | |||
344 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
345 | |||
346 | $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
347 | |||
348 | $this->request->headers->remove('X-Requested-With'); |
||
349 | $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
350 | |||
351 | $this->request->attributes->set('_xml_http_request', true); |
||
352 | $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
353 | } |
||
354 | |||
355 | public function testConfigure(): void |
||
356 | { |
||
357 | $uniqueId = ''; |
||
358 | |||
359 | $this->admin->expects($this->once()) |
||
|
|||
360 | ->method('setUniqid') |
||
361 | ->willReturnCallback(static function (string $uniqid) use (&$uniqueId): void { |
||
362 | $uniqueId = $uniqid; |
||
363 | }); |
||
364 | |||
365 | $this->request->query->set('uniqid', '123456'); |
||
366 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
367 | |||
368 | $this->assertSame('123456', $uniqueId); |
||
369 | } |
||
370 | |||
371 | public function testConfigureChild(): void |
||
372 | { |
||
373 | $uniqueId = ''; |
||
374 | |||
375 | $this->admin->expects($this->once()) |
||
376 | ->method('setUniqid') |
||
377 | ->willReturnCallback(static function (string $uniqid) use (&$uniqueId): void { |
||
378 | $uniqueId = $uniqid; |
||
379 | }); |
||
380 | |||
381 | $this->admin->expects($this->once()) |
||
382 | ->method('isChild') |
||
383 | ->willReturn(true); |
||
384 | |||
385 | $adminParent = $this->getMockBuilder(AdminInterface::class) |
||
386 | ->disableOriginalConstructor() |
||
387 | ->getMock(); |
||
388 | $this->admin->expects($this->once()) |
||
389 | ->method('getParent') |
||
390 | ->willReturn($adminParent); |
||
391 | |||
392 | $this->request->query->set('uniqid', '123456'); |
||
393 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
394 | |||
395 | $this->assertSame('123456', $uniqueId); |
||
396 | } |
||
397 | |||
398 | public function testConfigureWithException(): void |
||
399 | { |
||
400 | $this->expectException(\RuntimeException::class); |
||
401 | $this->expectExceptionMessage( |
||
402 | 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`' |
||
403 | ); |
||
404 | |||
405 | $this->request->attributes->remove('_sonata_admin'); |
||
406 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
407 | } |
||
408 | |||
409 | public function testConfigureWithException2(): void |
||
410 | { |
||
411 | $this->expectException(\RuntimeException::class); |
||
412 | $this->expectExceptionMessage('Unable to find the admin class related to the current controller'); |
||
413 | |||
414 | $this->pool->setAdminServiceIds(['nonexistent.admin']); |
||
415 | $this->request->attributes->set('_sonata_admin', 'nonexistent.admin'); |
||
416 | |||
417 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
418 | } |
||
419 | |||
420 | public function testGetBaseTemplate(): void |
||
421 | { |
||
422 | $this->assertSame( |
||
423 | '@SonataAdmin/standard_layout.html.twig', |
||
424 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
425 | ); |
||
426 | |||
427 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
428 | $this->assertSame( |
||
429 | '@SonataAdmin/ajax_layout.html.twig', |
||
430 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
431 | ); |
||
432 | |||
433 | $this->request->headers->remove('X-Requested-With'); |
||
434 | $this->assertSame( |
||
435 | '@SonataAdmin/standard_layout.html.twig', |
||
436 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
437 | ); |
||
438 | |||
439 | $this->request->attributes->set('_xml_http_request', true); |
||
440 | $this->assertSame( |
||
441 | '@SonataAdmin/ajax_layout.html.twig', |
||
442 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
443 | ); |
||
444 | } |
||
445 | |||
446 | public function testRender(): void |
||
447 | { |
||
448 | $this->parameters = []; |
||
449 | $this->assertInstanceOf( |
||
450 | Response::class, |
||
451 | $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', []) |
||
452 | ); |
||
453 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
454 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
455 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
456 | } |
||
457 | |||
458 | public function testRenderWithResponse(): void |
||
459 | { |
||
460 | $this->parameters = []; |
||
461 | $response = new Response(); |
||
462 | $response->headers->set('X-foo', 'bar'); |
||
463 | $responseResult = $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], $response); |
||
464 | |||
465 | $this->assertSame($response, $responseResult); |
||
466 | $this->assertSame('bar', $responseResult->headers->get('X-foo')); |
||
467 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
468 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
469 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
470 | } |
||
471 | |||
472 | public function testRenderCustomParams(): void |
||
473 | { |
||
474 | $this->parameters = []; |
||
475 | $this->assertInstanceOf( |
||
476 | Response::class, |
||
477 | $this->controller->renderWithExtraParams( |
||
478 | '@FooAdmin/foo.html.twig', |
||
479 | ['foo' => 'bar'], |
||
480 | null |
||
481 | ) |
||
482 | ); |
||
483 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
484 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
485 | $this->assertSame('bar', $this->parameters['foo']); |
||
486 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
487 | } |
||
488 | |||
489 | public function testRenderAjax(): void |
||
490 | { |
||
491 | $this->parameters = []; |
||
492 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
493 | $this->assertInstanceOf( |
||
494 | Response::class, |
||
495 | $this->controller->renderWithExtraParams( |
||
496 | '@FooAdmin/foo.html.twig', |
||
497 | ['foo' => 'bar'], |
||
498 | null |
||
499 | ) |
||
500 | ); |
||
501 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
502 | $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']); |
||
503 | $this->assertSame('bar', $this->parameters['foo']); |
||
504 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
505 | } |
||
506 | |||
507 | public function testListActionAccessDenied(): void |
||
508 | { |
||
509 | $this->expectException(AccessDeniedException::class); |
||
510 | |||
511 | $this->admin->expects($this->once()) |
||
512 | ->method('checkAccess') |
||
513 | ->with($this->equalTo('list')) |
||
514 | ->will($this->throwException(new AccessDeniedException())); |
||
515 | |||
516 | $this->controller->listAction($this->request); |
||
517 | } |
||
518 | |||
519 | public function testPreList(): void |
||
520 | { |
||
521 | $datagrid = $this->createMock(DatagridInterface::class); |
||
522 | |||
523 | $this->admin |
||
524 | ->method('hasRoute') |
||
525 | ->with($this->equalTo('list')) |
||
526 | ->willReturn(true); |
||
527 | |||
528 | $this->admin->expects($this->once()) |
||
529 | ->method('checkAccess') |
||
530 | ->with($this->equalTo('list')); |
||
531 | |||
532 | $controller = $this->createController(PreCRUDController::class); |
||
533 | |||
534 | $response = $controller->listAction($this->request); |
||
535 | $this->assertInstanceOf(Response::class, $response); |
||
536 | $this->assertSame('preList called', $response->getContent()); |
||
537 | } |
||
538 | |||
539 | public function testListAction(): void |
||
540 | { |
||
541 | $datagrid = $this->createMock(DatagridInterface::class); |
||
542 | |||
543 | $this->admin |
||
544 | ->method('hasRoute') |
||
545 | ->with($this->equalTo('list')) |
||
546 | ->willReturn(true); |
||
547 | |||
548 | $this->admin->expects($this->once()) |
||
549 | ->method('checkAccess') |
||
550 | ->with($this->equalTo('list')); |
||
551 | |||
552 | $form = $this->getMockBuilder(Form::class) |
||
553 | ->disableOriginalConstructor() |
||
554 | ->getMock(); |
||
555 | |||
556 | $form->expects($this->once()) |
||
557 | ->method('createView') |
||
558 | ->willReturn($this->createMock(FormView::class)); |
||
559 | |||
560 | $this->admin->expects($this->once()) |
||
561 | ->method('getDatagrid') |
||
562 | ->willReturn($datagrid); |
||
563 | |||
564 | $datagrid->expects($this->once()) |
||
565 | ->method('getForm') |
||
566 | ->willReturn($form); |
||
567 | |||
568 | $this->parameters = []; |
||
569 | $this->assertInstanceOf(Response::class, $this->controller->listAction($this->request)); |
||
570 | |||
571 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
572 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
573 | |||
574 | $this->assertSame('list', $this->parameters['action']); |
||
575 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
576 | $this->assertInstanceOf(DatagridInterface::class, $this->parameters['datagrid']); |
||
577 | $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']); |
||
578 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
579 | $this->assertSame('@SonataAdmin/CRUD/list.html.twig', $this->template); |
||
580 | } |
||
581 | |||
582 | public function testBatchActionDeleteAccessDenied(): void |
||
593 | |||
594 | public function testBatchActionDelete(): void |
||
595 | { |
||
596 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
597 | |||
598 | $this->admin->expects($this->once()) |
||
599 | ->method('checkAccess') |
||
600 | ->with($this->equalTo('batchDelete')); |
||
601 | |||
602 | $this->admin->expects($this->once()) |
||
603 | ->method('getModelManager') |
||
604 | ->willReturn($modelManager); |
||
605 | |||
606 | $this->admin->expects($this->once()) |
||
607 | ->method('getFilterParameters') |
||
608 | ->willReturn(['foo' => 'bar']); |
||
609 | |||
610 | $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
||
611 | |||
612 | $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
||
613 | |||
614 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
615 | $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
616 | $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
||
617 | } |
||
618 | |||
619 | public function testBatchActionDeleteWithModelManagerException(): void |
||
620 | { |
||
621 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
622 | $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete'); |
||
623 | |||
624 | $this->admin->expects($this->once()) |
||
625 | ->method('getModelManager') |
||
626 | ->willReturn($modelManager); |
||
627 | |||
628 | $this->admin->expects($this->once()) |
||
629 | ->method('getFilterParameters') |
||
630 | ->willReturn(['foo' => 'bar']); |
||
631 | |||
632 | $this->expectTranslate('flash_batch_delete_error', [], 'SonataAdminBundle'); |
||
633 | |||
634 | $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
||
635 | |||
636 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
637 | $this->assertSame(['flash_batch_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
||
638 | $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
||
639 | } |
||
640 | |||
641 | public function testBatchActionDeleteWithModelManagerExceptionInDebugMode(): void |
||
642 | { |
||
643 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
644 | $this->expectException(ModelManagerException::class); |
||
645 | |||
646 | $modelManager->expects($this->once()) |
||
647 | ->method('batchDelete') |
||
648 | ->willReturnCallback(static function (): void { |
||
649 | throw new ModelManagerException(); |
||
650 | }); |
||
651 | |||
652 | $this->admin->expects($this->once()) |
||
653 | ->method('getModelManager') |
||
654 | ->willReturn($modelManager); |
||
655 | |||
656 | $this->kernel->expects($this->once()) |
||
657 | ->method('isDebug') |
||
658 | ->willReturn(true); |
||
659 | |||
660 | $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
||
661 | } |
||
662 | |||
663 | public function testShowActionNotFoundException(): void |
||
664 | { |
||
665 | $this->expectException(NotFoundHttpException::class); |
||
666 | |||
667 | $this->admin->expects($this->once()) |
||
668 | ->method('getObject') |
||
669 | ->willReturn(null); |
||
670 | |||
671 | $this->controller->showAction($this->request); |
||
672 | } |
||
673 | |||
674 | public function testShowActionAccessDenied(): void |
||
675 | { |
||
676 | $this->expectException(AccessDeniedException::class); |
||
677 | |||
678 | $this->admin->expects($this->once()) |
||
679 | ->method('getObject') |
||
680 | ->willReturn(new \stdClass()); |
||
681 | |||
682 | $this->admin->expects($this->once()) |
||
683 | ->method('checkAccess') |
||
684 | ->with($this->equalTo('show')) |
||
685 | ->will($this->throwException(new AccessDeniedException())); |
||
686 | |||
687 | $this->controller->showAction($this->request); |
||
688 | } |
||
689 | |||
690 | public function testPreShow(): void |
||
691 | { |
||
692 | $object = new \stdClass(); |
||
693 | $object->foo = 123456; |
||
694 | |||
695 | $this->admin->expects($this->once()) |
||
696 | ->method('getObject') |
||
697 | ->willReturn($object); |
||
698 | |||
699 | $this->admin->expects($this->once()) |
||
700 | ->method('checkAccess') |
||
701 | ->with($this->equalTo('show')); |
||
702 | |||
703 | $this->container->set('foo.admin', $this->admin); |
||
704 | $controller = $this->createController(PreCRUDController::class); |
||
705 | |||
706 | $response = $controller->showAction($this->request); |
||
707 | $this->assertInstanceOf(Response::class, $response); |
||
708 | $this->assertSame('preShow called: 123456', $response->getContent()); |
||
709 | } |
||
710 | |||
711 | public function testShowAction(): void |
||
712 | { |
||
713 | $object = new \stdClass(); |
||
714 | |||
715 | $this->admin->expects($this->once()) |
||
716 | ->method('getObject') |
||
717 | ->willReturn($object); |
||
718 | |||
719 | $this->admin->expects($this->once()) |
||
720 | ->method('checkAccess') |
||
721 | ->with($this->equalTo('show')); |
||
722 | |||
723 | $show = $this->createMock(FieldDescriptionCollection::class); |
||
724 | |||
725 | $this->admin->expects($this->once()) |
||
726 | ->method('getShow') |
||
727 | ->willReturn($show); |
||
728 | |||
729 | $this->assertInstanceOf(Response::class, $this->controller->showAction($this->request)); |
||
730 | |||
731 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
732 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
733 | |||
734 | $this->assertSame('show', $this->parameters['action']); |
||
735 | $this->assertInstanceOf(FieldDescriptionCollection::class, $this->parameters['elements']); |
||
736 | $this->assertSame($object, $this->parameters['object']); |
||
737 | |||
738 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
739 | $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template); |
||
740 | } |
||
741 | |||
742 | /** |
||
743 | * @dataProvider getRedirectToTests |
||
744 | */ |
||
745 | public function testRedirectTo( |
||
746 | string $expected, |
||
747 | string $route, |
||
748 | array $queryParams, |
||
749 | array $requestParams, |
||
750 | bool $hasActiveSubclass |
||
751 | ): void { |
||
752 | $this->admin |
||
753 | ->method('hasActiveSubclass') |
||
754 | ->willReturn($hasActiveSubclass); |
||
755 | |||
756 | $object = new \stdClass(); |
||
757 | |||
758 | foreach ($queryParams as $key => $value) { |
||
759 | $this->request->query->set($key, $value); |
||
760 | } |
||
761 | |||
762 | foreach ($requestParams as $key => $value) { |
||
763 | $this->request->request->set($key, $value); |
||
764 | } |
||
765 | |||
766 | $this->admin |
||
767 | ->method('hasRoute') |
||
768 | ->with($this->equalTo($route)) |
||
769 | ->willReturn(true); |
||
770 | |||
771 | $this->admin |
||
772 | ->method('hasAccess') |
||
773 | ->with($this->equalTo($route)) |
||
774 | ->willReturn(true); |
||
775 | |||
776 | $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
||
777 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
778 | $this->assertSame($expected, $response->getTargetUrl()); |
||
779 | } |
||
780 | |||
781 | public function testRedirectToWithObject(): void |
||
782 | { |
||
783 | $this->admin |
||
784 | ->method('hasActiveSubclass') |
||
785 | ->willReturn(false); |
||
786 | |||
787 | $object = new \stdClass(); |
||
788 | |||
789 | $this->admin->expects($this->at(0)) |
||
790 | ->method('hasRoute') |
||
791 | ->with($this->equalTo('edit')) |
||
792 | ->willReturn(true); |
||
793 | |||
794 | $this->admin |
||
795 | ->method('hasAccess') |
||
796 | ->with($this->equalTo('edit'), $object) |
||
797 | ->willReturn(false); |
||
798 | |||
799 | $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
||
800 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
801 | $this->assertSame('list', $response->getTargetUrl()); |
||
802 | } |
||
803 | |||
804 | public function getRedirectToTests() |
||
805 | { |
||
806 | return [ |
||
807 | ['stdClass_edit', 'edit', [], [], false], |
||
808 | ['list', 'list', ['btn_update_and_list' => true], [], false], |
||
809 | ['list', 'list', ['btn_create_and_list' => true], [], false], |
||
810 | ['create', 'create', ['btn_create_and_create' => true], [], false], |
||
811 | ['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], [], true], |
||
812 | ['stdClass_edit?_tab=first_tab', 'edit', ['btn_update_and_edit' => true], ['_tab' => 'first_tab'], false], |
||
813 | ]; |
||
814 | } |
||
815 | |||
816 | public function testDeleteActionNotFoundException(): void |
||
817 | { |
||
818 | $this->expectException(NotFoundHttpException::class); |
||
819 | |||
820 | $this->admin->expects($this->once()) |
||
821 | ->method('getObject') |
||
822 | ->willReturn(null); |
||
823 | |||
824 | $this->controller->deleteAction($this->request); |
||
825 | } |
||
826 | |||
827 | public function testDeleteActionAccessDenied(): void |
||
828 | { |
||
829 | $this->expectException(AccessDeniedException::class); |
||
830 | |||
831 | $this->admin->expects($this->once()) |
||
832 | ->method('getObject') |
||
833 | ->willReturn(new \stdClass()); |
||
834 | |||
835 | $this->admin->expects($this->once()) |
||
836 | ->method('checkAccess') |
||
837 | ->with($this->equalTo('delete')) |
||
838 | ->will($this->throwException(new AccessDeniedException())); |
||
839 | |||
840 | $this->controller->deleteAction($this->request); |
||
841 | } |
||
842 | |||
843 | public function testPreDelete(): void |
||
844 | { |
||
845 | $object = new \stdClass(); |
||
846 | $object->foo = 123456; |
||
847 | |||
848 | $this->admin->expects($this->once()) |
||
849 | ->method('getObject') |
||
850 | ->willReturn($object); |
||
851 | |||
852 | $this->admin->expects($this->once()) |
||
853 | ->method('checkAccess') |
||
854 | ->with($this->equalTo('delete')); |
||
855 | |||
856 | $controller = $this->createController(PreCRUDController::class); |
||
857 | |||
858 | $response = $controller->deleteAction($this->request); |
||
859 | $this->assertInstanceOf(Response::class, $response); |
||
860 | $this->assertSame('preDelete called: 123456', $response->getContent()); |
||
861 | } |
||
862 | |||
863 | public function testDeleteAction(): void |
||
864 | { |
||
865 | $object = new \stdClass(); |
||
866 | |||
867 | $this->admin->expects($this->once()) |
||
868 | ->method('getObject') |
||
869 | ->willReturn($object); |
||
870 | |||
871 | $this->admin->expects($this->once()) |
||
872 | ->method('checkAccess') |
||
873 | ->with($this->equalTo('delete')); |
||
874 | |||
875 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request)); |
||
876 | |||
877 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
878 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
879 | |||
880 | $this->assertSame('delete', $this->parameters['action']); |
||
881 | $this->assertSame($object, $this->parameters['object']); |
||
882 | $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
||
883 | |||
884 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
885 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
886 | } |
||
887 | |||
888 | public function testDeleteActionNoCsrfToken(): void |
||
889 | { |
||
890 | $this->csrfProvider = null; |
||
891 | |||
892 | $object = new \stdClass(); |
||
893 | |||
894 | $this->admin->expects($this->once()) |
||
895 | ->method('getObject') |
||
896 | ->willReturn($object); |
||
897 | |||
898 | $this->admin->expects($this->once()) |
||
899 | ->method('checkAccess') |
||
900 | ->with($this->equalTo('delete')); |
||
901 | |||
902 | $controller = $this->createController(); |
||
903 | |||
904 | $this->assertInstanceOf(Response::class, $controller->deleteAction($this->request)); |
||
905 | |||
906 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
907 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
908 | |||
909 | $this->assertSame('delete', $this->parameters['action']); |
||
910 | $this->assertSame($object, $this->parameters['object']); |
||
911 | $this->assertFalse($this->parameters['csrf_token']); |
||
912 | |||
913 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
914 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
915 | } |
||
916 | |||
917 | public function testDeleteActionAjaxSuccess1(): void |
||
918 | { |
||
919 | $object = new \stdClass(); |
||
920 | |||
921 | $this->admin->expects($this->once()) |
||
922 | ->method('getObject') |
||
923 | ->willReturn($object); |
||
924 | |||
925 | $this->admin->expects($this->once()) |
||
926 | ->method('checkAccess') |
||
927 | ->with($this->equalTo('delete')); |
||
928 | |||
929 | $this->request->setMethod(Request::METHOD_DELETE); |
||
930 | |||
931 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
932 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
933 | |||
934 | $response = $this->controller->deleteAction($this->request); |
||
935 | |||
936 | $this->assertInstanceOf(Response::class, $response); |
||
937 | $this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
||
938 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
939 | } |
||
940 | |||
941 | public function testDeleteActionAjaxSuccess2(): void |
||
942 | { |
||
943 | $object = new \stdClass(); |
||
944 | |||
945 | $this->admin->expects($this->once()) |
||
946 | ->method('getObject') |
||
947 | ->willReturn($object); |
||
948 | |||
949 | $this->admin->expects($this->once()) |
||
950 | ->method('checkAccess') |
||
951 | ->with($this->equalTo('delete')); |
||
952 | |||
953 | $this->request->setMethod(Request::METHOD_POST); |
||
954 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
955 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
956 | |||
957 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
958 | |||
959 | $response = $this->controller->deleteAction($this->request); |
||
960 | |||
961 | $this->assertInstanceOf(Response::class, $response); |
||
962 | $this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
||
963 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
964 | } |
||
965 | |||
966 | public function testDeleteActionAjaxError(): void |
||
967 | { |
||
968 | $object = new \stdClass(); |
||
969 | |||
970 | $this->admin->expects($this->once()) |
||
971 | ->method('getObject') |
||
972 | ->willReturn($object); |
||
973 | |||
974 | $this->admin->expects($this->once()) |
||
975 | ->method('checkAccess') |
||
976 | ->with($this->equalTo('delete')); |
||
977 | |||
978 | $this->admin |
||
979 | ->method('getClass') |
||
980 | ->willReturn(\stdClass::class); |
||
981 | |||
982 | $this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
||
983 | |||
984 | $this->request->setMethod(Request::METHOD_DELETE); |
||
985 | |||
986 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
987 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
988 | |||
989 | $response = $this->controller->deleteAction($this->request); |
||
990 | |||
991 | $this->assertInstanceOf(Response::class, $response); |
||
992 | $this->assertSame(json_encode(['result' => 'error']), $response->getContent()); |
||
993 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
994 | } |
||
995 | |||
996 | public function testDeleteActionWithModelManagerExceptionInDebugMode(): void |
||
997 | { |
||
998 | $this->expectException(ModelManagerException::class); |
||
999 | |||
1000 | $object = new \stdClass(); |
||
1001 | |||
1002 | $this->admin->expects($this->once()) |
||
1003 | ->method('getObject') |
||
1004 | ->willReturn($object); |
||
1005 | |||
1006 | $this->admin->expects($this->once()) |
||
1007 | ->method('checkAccess') |
||
1008 | ->with($this->equalTo('delete')); |
||
1009 | |||
1010 | $this->admin->expects($this->once()) |
||
1011 | ->method('delete') |
||
1012 | ->willReturnCallback(static function (): void { |
||
1013 | throw new ModelManagerException(); |
||
1014 | }); |
||
1015 | |||
1016 | $this->kernel->expects($this->once()) |
||
1017 | ->method('isDebug') |
||
1018 | ->willReturn(true); |
||
1019 | |||
1020 | $this->request->setMethod(Request::METHOD_DELETE); |
||
1021 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1022 | |||
1023 | $this->controller->deleteAction($this->request); |
||
1024 | } |
||
1025 | |||
1026 | /** |
||
1027 | * @dataProvider getToStringValues |
||
1028 | */ |
||
1029 | public function testDeleteActionSuccess1(string $expectedToStringValue, string $toStringValue): void |
||
1030 | { |
||
1031 | $object = new \stdClass(); |
||
1032 | |||
1033 | $this->admin->expects($this->once()) |
||
1034 | ->method('getObject') |
||
1035 | ->willReturn($object); |
||
1036 | |||
1037 | $this->admin->expects($this->once()) |
||
1038 | ->method('toString') |
||
1039 | ->with($this->equalTo($object)) |
||
1040 | ->willReturn($toStringValue); |
||
1041 | |||
1042 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1043 | |||
1044 | $this->admin->expects($this->once()) |
||
1045 | ->method('checkAccess') |
||
1046 | ->with($this->equalTo('delete')); |
||
1047 | |||
1048 | $this->request->setMethod(Request::METHOD_DELETE); |
||
1049 | |||
1050 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1051 | |||
1052 | $response = $this->controller->deleteAction($this->request); |
||
1053 | |||
1054 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1055 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1056 | $this->assertSame('list', $response->getTargetUrl()); |
||
1057 | } |
||
1058 | |||
1059 | /** |
||
1060 | * @dataProvider getToStringValues |
||
1061 | */ |
||
1062 | public function testDeleteActionSuccess2(string $expectedToStringValue, string $toStringValue): void |
||
1063 | { |
||
1064 | $object = new \stdClass(); |
||
1065 | |||
1066 | $this->admin->expects($this->once()) |
||
1067 | ->method('getObject') |
||
1068 | ->willReturn($object); |
||
1069 | |||
1070 | $this->admin->expects($this->once()) |
||
1071 | ->method('checkAccess') |
||
1072 | ->with($this->equalTo('delete')); |
||
1073 | |||
1074 | $this->admin->expects($this->once()) |
||
1075 | ->method('toString') |
||
1076 | ->with($this->equalTo($object)) |
||
1077 | ->willReturn($toStringValue); |
||
1078 | |||
1079 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1080 | |||
1081 | $this->request->setMethod(Request::METHOD_POST); |
||
1082 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
1083 | |||
1084 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1085 | |||
1086 | $response = $this->controller->deleteAction($this->request); |
||
1087 | |||
1088 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1089 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1090 | $this->assertSame('list', $response->getTargetUrl()); |
||
1091 | } |
||
1092 | |||
1093 | /** |
||
1094 | * @dataProvider getToStringValues |
||
1095 | */ |
||
1096 | public function testDeleteActionSuccessNoCsrfTokenProvider(string $expectedToStringValue, string $toStringValue): void |
||
1097 | { |
||
1098 | $this->csrfProvider = null; |
||
1099 | |||
1100 | $object = new \stdClass(); |
||
1101 | |||
1102 | $this->admin->expects($this->once()) |
||
1103 | ->method('getObject') |
||
1104 | ->willReturn($object); |
||
1105 | |||
1106 | $this->admin->expects($this->once()) |
||
1107 | ->method('checkAccess') |
||
1108 | ->with($this->equalTo('delete')); |
||
1109 | |||
1110 | $this->admin->expects($this->once()) |
||
1111 | ->method('toString') |
||
1112 | ->with($this->equalTo($object)) |
||
1113 | ->willReturn($toStringValue); |
||
1114 | |||
1115 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1116 | |||
1117 | $this->request->setMethod(Request::METHOD_POST); |
||
1118 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
1119 | |||
1120 | $this->controller = $this->createController(); |
||
1121 | $response = $this->controller->deleteAction($this->request); |
||
1122 | |||
1123 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1124 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1125 | $this->assertSame('list', $response->getTargetUrl()); |
||
1126 | } |
||
1127 | |||
1128 | public function testDeleteActionWrongRequestMethod(): void |
||
1129 | { |
||
1130 | $object = new \stdClass(); |
||
1131 | |||
1132 | $this->admin->expects($this->once()) |
||
1133 | ->method('getObject') |
||
1134 | ->willReturn($object); |
||
1135 | |||
1136 | $this->admin->expects($this->once()) |
||
1137 | ->method('checkAccess') |
||
1138 | ->with($this->equalTo('delete')); |
||
1139 | |||
1140 | //without POST request parameter "_method" should not be used as real REST method |
||
1141 | $this->request->query->set('_method', Request::METHOD_DELETE); |
||
1142 | |||
1143 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request)); |
||
1144 | |||
1145 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1146 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1147 | |||
1148 | $this->assertSame('delete', $this->parameters['action']); |
||
1149 | $this->assertSame($object, $this->parameters['object']); |
||
1150 | $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
||
1151 | |||
1152 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1153 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
1154 | } |
||
1155 | |||
1156 | /** |
||
1157 | * @dataProvider getToStringValues |
||
1158 | */ |
||
1159 | public function testDeleteActionError(string $expectedToStringValue, string $toStringValue): void |
||
1160 | { |
||
1161 | $object = new \stdClass(); |
||
1162 | |||
1163 | $this->admin->expects($this->once()) |
||
1164 | ->method('getObject') |
||
1165 | ->willReturn($object); |
||
1166 | |||
1167 | $this->admin->expects($this->once()) |
||
1168 | ->method('checkAccess') |
||
1169 | ->with($this->equalTo('delete')); |
||
1170 | |||
1171 | $this->admin->expects($this->once()) |
||
1172 | ->method('toString') |
||
1173 | ->with($this->equalTo($object)) |
||
1174 | ->willReturn($toStringValue); |
||
1175 | |||
1176 | $this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1177 | |||
1178 | $this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
||
1179 | |||
1180 | $this->request->setMethod(Request::METHOD_DELETE); |
||
1181 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1182 | |||
1183 | $response = $this->controller->deleteAction($this->request); |
||
1184 | |||
1185 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1186 | $this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
||
1187 | $this->assertSame('list', $response->getTargetUrl()); |
||
1188 | } |
||
1189 | |||
1190 | public function testDeleteActionInvalidCsrfToken(): void |
||
1191 | { |
||
1192 | $object = new \stdClass(); |
||
1193 | |||
1194 | $this->admin->expects($this->once()) |
||
1195 | ->method('getObject') |
||
1196 | ->willReturn($object); |
||
1197 | |||
1198 | $this->admin->expects($this->once()) |
||
1199 | ->method('checkAccess') |
||
1200 | ->with($this->equalTo('delete')); |
||
1201 | |||
1202 | $this->request->setMethod(Request::METHOD_POST); |
||
1203 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
1204 | $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
||
1205 | |||
1206 | try { |
||
1207 | $this->controller->deleteAction($this->request); |
||
1208 | } catch (HttpException $e) { |
||
1209 | $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
||
1210 | $this->assertSame(400, $e->getStatusCode()); |
||
1211 | } |
||
1212 | } |
||
1213 | |||
1214 | public function testEditActionNotFoundException(): void |
||
1215 | { |
||
1216 | $this->expectException(NotFoundHttpException::class); |
||
1217 | |||
1218 | $this->admin->expects($this->once()) |
||
1219 | ->method('getObject') |
||
1220 | ->willReturn(null); |
||
1221 | |||
1222 | $this->controller->editAction($this->request); |
||
1223 | } |
||
1224 | |||
1225 | public function testEditActionAccessDenied(): void |
||
1226 | { |
||
1227 | $this->expectException(AccessDeniedException::class); |
||
1228 | |||
1229 | $this->admin->expects($this->once()) |
||
1230 | ->method('getObject') |
||
1231 | ->willReturn(new \stdClass()); |
||
1232 | |||
1233 | $this->admin->expects($this->once()) |
||
1234 | ->method('checkAccess') |
||
1235 | ->with($this->equalTo('edit')) |
||
1236 | ->will($this->throwException(new AccessDeniedException())); |
||
1237 | |||
1238 | $this->controller->editAction($this->request); |
||
1239 | } |
||
1240 | |||
1241 | public function testPreEdit(): void |
||
1242 | { |
||
1243 | $object = new \stdClass(); |
||
1244 | $object->foo = 123456; |
||
1245 | |||
1246 | $this->admin->expects($this->once()) |
||
1247 | ->method('getObject') |
||
1248 | ->willReturn($object); |
||
1249 | |||
1250 | $this->admin->expects($this->once()) |
||
1251 | ->method('checkAccess') |
||
1252 | ->with($this->equalTo('edit')); |
||
1253 | |||
1254 | $controller = $this->createController(PreCRUDController::class); |
||
1255 | |||
1256 | $response = $controller->editAction($this->request); |
||
1257 | $this->assertInstanceOf(Response::class, $response); |
||
1258 | $this->assertSame('preEdit called: 123456', $response->getContent()); |
||
1259 | } |
||
1260 | |||
1261 | public function testEditAction(): void |
||
1262 | { |
||
1263 | $object = new \stdClass(); |
||
1264 | |||
1265 | $this->admin->expects($this->once()) |
||
1266 | ->method('getObject') |
||
1267 | ->willReturn($object); |
||
1268 | |||
1269 | $this->admin->expects($this->once()) |
||
1270 | ->method('checkAccess') |
||
1271 | ->with($this->equalTo('edit')); |
||
1272 | |||
1273 | $form = $this->createMock(Form::class); |
||
1274 | |||
1275 | $this->admin->expects($this->once()) |
||
1276 | ->method('getForm') |
||
1277 | ->willReturn($form); |
||
1278 | |||
1279 | $formView = $this->createMock(FormView::class); |
||
1280 | |||
1281 | $form |
||
1282 | ->method('createView') |
||
1283 | ->willReturn($formView); |
||
1284 | |||
1285 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
1286 | |||
1287 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1288 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1289 | |||
1290 | $this->assertSame('edit', $this->parameters['action']); |
||
1291 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1292 | $this->assertSame($object, $this->parameters['object']); |
||
1293 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1294 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1295 | } |
||
1296 | |||
1297 | /** |
||
1298 | * @dataProvider getToStringValues |
||
1299 | */ |
||
1300 | public function testEditActionSuccess(string $expectedToStringValue, string $toStringValue): void |
||
1301 | { |
||
1302 | $object = new \stdClass(); |
||
1303 | |||
1304 | $this->admin->expects($this->once()) |
||
1305 | ->method('getObject') |
||
1306 | ->willReturn($object); |
||
1307 | |||
1308 | $this->admin->expects($this->once()) |
||
1309 | ->method('update') |
||
1310 | ->willReturnArgument(0); |
||
1311 | |||
1312 | $this->admin->expects($this->once()) |
||
1313 | ->method('checkAccess') |
||
1314 | ->with($this->equalTo('edit')); |
||
1315 | |||
1316 | $this->admin->expects($this->once()) |
||
1317 | ->method('hasRoute') |
||
1318 | ->with($this->equalTo('edit')) |
||
1319 | ->willReturn(true); |
||
1320 | |||
1321 | $this->admin->expects($this->once()) |
||
1322 | ->method('hasAccess') |
||
1323 | ->with($this->equalTo('edit')) |
||
1324 | ->willReturn(true); |
||
1325 | |||
1326 | $form = $this->createMock(Form::class); |
||
1327 | |||
1328 | $form->expects($this->once()) |
||
1329 | ->method('getData') |
||
1330 | ->willReturn($object); |
||
1331 | |||
1332 | $this->admin->expects($this->once()) |
||
1333 | ->method('getForm') |
||
1334 | ->willReturn($form); |
||
1335 | |||
1336 | $form->expects($this->once()) |
||
1337 | ->method('isSubmitted') |
||
1338 | ->willReturn(true); |
||
1339 | |||
1340 | $form->expects($this->once()) |
||
1341 | ->method('isValid') |
||
1342 | ->willReturn(true); |
||
1343 | |||
1344 | $this->admin->expects($this->once()) |
||
1345 | ->method('toString') |
||
1346 | ->with($this->equalTo($object)) |
||
1347 | ->willReturn($toStringValue); |
||
1348 | |||
1349 | $this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1350 | |||
1351 | $this->request->setMethod(Request::METHOD_POST); |
||
1352 | |||
1353 | $response = $this->controller->editAction($this->request); |
||
1354 | |||
1355 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1356 | $this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1357 | $this->assertSame('stdClass_edit', $response->getTargetUrl()); |
||
1358 | } |
||
1359 | |||
1360 | /** |
||
1361 | * @dataProvider getToStringValues |
||
1362 | */ |
||
1363 | public function testEditActionError(string $expectedToStringValue, string $toStringValue): void |
||
1364 | { |
||
1365 | $object = new \stdClass(); |
||
1366 | |||
1367 | $this->admin->expects($this->once()) |
||
1368 | ->method('getObject') |
||
1369 | ->willReturn($object); |
||
1370 | |||
1371 | $this->admin->expects($this->once()) |
||
1372 | ->method('checkAccess') |
||
1373 | ->with($this->equalTo('edit')); |
||
1374 | |||
1375 | $form = $this->createMock(Form::class); |
||
1376 | |||
1377 | $this->admin->expects($this->once()) |
||
1378 | ->method('getForm') |
||
1379 | ->willReturn($form); |
||
1380 | |||
1381 | $form->expects($this->once()) |
||
1382 | ->method('isSubmitted') |
||
1383 | ->willReturn(true); |
||
1384 | |||
1385 | $form->expects($this->once()) |
||
1386 | ->method('isValid') |
||
1387 | ->willReturn(false); |
||
1388 | |||
1389 | $this->admin->expects($this->once()) |
||
1390 | ->method('toString') |
||
1391 | ->with($this->equalTo($object)) |
||
1392 | ->willReturn($toStringValue); |
||
1393 | |||
1394 | $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1395 | |||
1396 | $this->request->setMethod(Request::METHOD_POST); |
||
1397 | |||
1398 | $formView = $this->createMock(FormView::class); |
||
1399 | |||
1400 | $form |
||
1401 | ->method('createView') |
||
1402 | ->willReturn($formView); |
||
1403 | |||
1404 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
1405 | |||
1406 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1407 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1408 | |||
1409 | $this->assertSame('edit', $this->parameters['action']); |
||
1410 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1411 | $this->assertSame($object, $this->parameters['object']); |
||
1412 | |||
1413 | $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
||
1414 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1415 | } |
||
1416 | |||
1417 | public function testEditActionAjaxSuccess(): void |
||
1418 | { |
||
1419 | $object = new \stdClass(); |
||
1420 | |||
1421 | $this->admin->expects($this->once()) |
||
1422 | ->method('getObject') |
||
1423 | ->willReturn($object); |
||
1424 | |||
1425 | $this->admin->expects($this->once()) |
||
1426 | ->method('update') |
||
1427 | ->willReturnArgument(0); |
||
1428 | |||
1429 | $this->admin->expects($this->once()) |
||
1430 | ->method('checkAccess') |
||
1431 | ->with($this->equalTo('edit')); |
||
1432 | |||
1433 | $form = $this->createMock(Form::class); |
||
1434 | |||
1435 | $this->admin->expects($this->once()) |
||
1436 | ->method('getForm') |
||
1437 | ->willReturn($form); |
||
1438 | |||
1439 | $form->expects($this->once()) |
||
1440 | ->method('isSubmitted') |
||
1441 | ->willReturn(true); |
||
1442 | |||
1443 | $form->expects($this->once()) |
||
1444 | ->method('isValid') |
||
1445 | ->willReturn(true); |
||
1446 | |||
1447 | $form->expects($this->once()) |
||
1448 | ->method('getData') |
||
1449 | ->willReturn($object); |
||
1450 | |||
1451 | $this->admin |
||
1452 | ->method('getNormalizedIdentifier') |
||
1453 | ->with($this->equalTo($object)) |
||
1454 | ->willReturn('foo_normalized'); |
||
1455 | |||
1456 | $this->admin->expects($this->once()) |
||
1457 | ->method('toString') |
||
1458 | ->willReturn('foo'); |
||
1459 | |||
1460 | $this->request->setMethod(Request::METHOD_POST); |
||
1461 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1462 | $this->request->headers->set('Accept', 'application/json'); |
||
1463 | |||
1464 | $response = $this->controller->editAction($this->request); |
||
1465 | |||
1466 | $this->assertInstanceOf(Response::class, $response); |
||
1467 | $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent()); |
||
1468 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1469 | } |
||
1470 | |||
1471 | public function testEditActionAjaxError(): void |
||
1472 | { |
||
1473 | $object = new \stdClass(); |
||
1474 | |||
1475 | $this->admin->expects($this->once()) |
||
1476 | ->method('getObject') |
||
1477 | ->willReturn($object); |
||
1478 | |||
1479 | $this->admin->expects($this->once()) |
||
1480 | ->method('checkAccess') |
||
1481 | ->with($this->equalTo('edit')); |
||
1482 | |||
1483 | $form = $this->createMock(Form::class); |
||
1484 | |||
1485 | $this->admin->expects($this->once()) |
||
1486 | ->method('getForm') |
||
1487 | ->willReturn($form); |
||
1488 | |||
1489 | $form->expects($this->once()) |
||
1490 | ->method('isSubmitted') |
||
1491 | ->willReturn(true); |
||
1492 | |||
1493 | $form->expects($this->once()) |
||
1494 | ->method('isValid') |
||
1495 | ->willReturn(false); |
||
1496 | |||
1497 | $formError = $this->createMock(FormError::class); |
||
1498 | $formError->expects($this->atLeastOnce()) |
||
1499 | ->method('getMessage') |
||
1500 | ->willReturn('Form error message'); |
||
1501 | |||
1502 | $form->expects($this->once()) |
||
1503 | ->method('getErrors') |
||
1504 | ->with(true) |
||
1505 | ->willReturn([$formError]); |
||
1506 | |||
1507 | $this->request->setMethod(Request::METHOD_POST); |
||
1508 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1509 | $this->request->headers->set('Accept', 'application/json'); |
||
1510 | |||
1511 | $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->editAction($this->request)); |
||
1512 | $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent()); |
||
1513 | } |
||
1514 | |||
1515 | public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void |
||
1516 | { |
||
1517 | $object = new \stdClass(); |
||
1518 | |||
1519 | $this->admin->expects($this->once()) |
||
1520 | ->method('getObject') |
||
1521 | ->willReturn($object); |
||
1522 | |||
1523 | $this->admin->expects($this->once()) |
||
1524 | ->method('checkAccess') |
||
1525 | ->with($this->equalTo('edit')); |
||
1526 | |||
1527 | $form = $this->createMock(Form::class); |
||
1528 | |||
1529 | $this->admin->expects($this->once()) |
||
1530 | ->method('getForm') |
||
1531 | ->willReturn($form); |
||
1532 | |||
1533 | $form->expects($this->once()) |
||
1534 | ->method('isSubmitted') |
||
1535 | ->willReturn(true); |
||
1536 | |||
1537 | $form->expects($this->once()) |
||
1538 | ->method('isValid') |
||
1539 | ->willReturn(false); |
||
1540 | |||
1541 | $this->request->setMethod(Request::METHOD_POST); |
||
1542 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1543 | |||
1544 | $formView = $this->createMock(FormView::class); |
||
1545 | $form |
||
1546 | ->method('createView') |
||
1547 | ->willReturn($formView); |
||
1548 | |||
1549 | $this->assertInstanceOf(Response::class, $response = $this->controller->editAction($this->request)); |
||
1550 | $this->assertSame(Response::HTTP_NOT_ACCEPTABLE, $response->getStatusCode()); |
||
1551 | } |
||
1552 | |||
1553 | /** |
||
1554 | * @dataProvider getToStringValues |
||
1555 | */ |
||
1556 | public function testEditActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void |
||
1557 | { |
||
1558 | $object = new \stdClass(); |
||
1559 | |||
1560 | $this->admin->expects($this->once()) |
||
1561 | ->method('getObject') |
||
1562 | ->willReturn($object); |
||
1563 | |||
1564 | $this->admin->expects($this->once()) |
||
1565 | ->method('checkAccess') |
||
1566 | ->with($this->equalTo('edit')); |
||
1567 | |||
1568 | $this->admin |
||
1569 | ->method('getClass') |
||
1570 | ->willReturn(\stdClass::class); |
||
1571 | |||
1572 | $form = $this->createMock(Form::class); |
||
1573 | |||
1574 | $this->admin->expects($this->once()) |
||
1575 | ->method('getForm') |
||
1576 | ->willReturn($form); |
||
1577 | |||
1578 | $form->expects($this->once()) |
||
1579 | ->method('isValid') |
||
1580 | ->willReturn(true); |
||
1581 | |||
1582 | $form->expects($this->once()) |
||
1583 | ->method('getData') |
||
1584 | ->willReturn($object); |
||
1585 | |||
1586 | $this->admin->expects($this->once()) |
||
1587 | ->method('toString') |
||
1588 | ->with($this->equalTo($object)) |
||
1589 | ->willReturn($toStringValue); |
||
1590 | |||
1591 | $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1592 | |||
1593 | $form->expects($this->once()) |
||
1594 | ->method('isSubmitted') |
||
1595 | ->willReturn(true); |
||
1596 | $this->request->setMethod(Request::METHOD_POST); |
||
1597 | |||
1598 | $formView = $this->createMock(FormView::class); |
||
1599 | |||
1600 | $form |
||
1601 | ->method('createView') |
||
1602 | ->willReturn($formView); |
||
1603 | |||
1604 | $this->assertLoggerLogsModelManagerException($this->admin, 'update'); |
||
1605 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
1606 | |||
1607 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1608 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1609 | |||
1610 | $this->assertSame('edit', $this->parameters['action']); |
||
1611 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1612 | $this->assertSame($object, $this->parameters['object']); |
||
1613 | |||
1614 | $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
||
1615 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1616 | } |
||
1617 | |||
1618 | public function testEditActionWithPreview(): void |
||
1619 | { |
||
1620 | $object = new \stdClass(); |
||
1621 | |||
1622 | $this->admin->expects($this->once()) |
||
1623 | ->method('getObject') |
||
1624 | ->willReturn($object); |
||
1625 | |||
1626 | $this->admin->expects($this->once()) |
||
1627 | ->method('checkAccess') |
||
1628 | ->with($this->equalTo('edit')); |
||
1629 | |||
1630 | $form = $this->createMock(Form::class); |
||
1631 | |||
1632 | $this->admin->expects($this->once()) |
||
1633 | ->method('getForm') |
||
1634 | ->willReturn($form); |
||
1635 | |||
1636 | $this->admin->expects($this->once()) |
||
1637 | ->method('supportsPreviewMode') |
||
1638 | ->willReturn(true); |
||
1639 | |||
1640 | $formView = $this->createMock(FormView::class); |
||
1641 | |||
1642 | $form |
||
1643 | ->method('createView') |
||
1644 | ->willReturn($formView); |
||
1645 | |||
1646 | $form->expects($this->once()) |
||
1647 | ->method('isSubmitted') |
||
1648 | ->willReturn(true); |
||
1649 | |||
1650 | $form->expects($this->once()) |
||
1651 | ->method('isValid') |
||
1652 | ->willReturn(true); |
||
1653 | |||
1654 | $this->request->setMethod(Request::METHOD_POST); |
||
1655 | $this->request->request->set('btn_preview', 'Preview'); |
||
1656 | |||
1657 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
1658 | |||
1659 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1660 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1661 | |||
1662 | $this->assertSame('edit', $this->parameters['action']); |
||
1663 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1664 | $this->assertSame($object, $this->parameters['object']); |
||
1665 | |||
1666 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1667 | $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
||
1668 | } |
||
1669 | |||
1670 | public function testEditActionWithLockException(): void |
||
1671 | { |
||
1672 | $object = new \stdClass(); |
||
1673 | $class = \get_class($object); |
||
1674 | |||
1675 | $this->admin |
||
1676 | ->method('getObject') |
||
1677 | ->willReturn($object); |
||
1678 | |||
1679 | $this->admin |
||
1680 | ->method('checkAccess') |
||
1681 | ->with($this->equalTo('edit')); |
||
1682 | |||
1683 | $this->admin |
||
1684 | ->method('getClass') |
||
1685 | ->willReturn($class); |
||
1686 | |||
1687 | $form = $this->createMock(Form::class); |
||
1688 | |||
1689 | $form |
||
1690 | ->method('isValid') |
||
1691 | ->willReturn(true); |
||
1692 | |||
1693 | $form->expects($this->once()) |
||
1694 | ->method('getData') |
||
1695 | ->willReturn($object); |
||
1696 | |||
1697 | $this->admin |
||
1698 | ->method('getForm') |
||
1699 | ->willReturn($form); |
||
1700 | |||
1701 | $form |
||
1702 | ->method('isSubmitted') |
||
1703 | ->willReturn(true); |
||
1704 | $this->request->setMethod(Request::METHOD_POST); |
||
1705 | |||
1706 | $this->admin |
||
1707 | ->method('update') |
||
1708 | ->will($this->throwException(new LockException())); |
||
1709 | |||
1710 | $this->admin |
||
1711 | ->method('toString') |
||
1712 | ->with($this->equalTo($object)) |
||
1713 | ->willReturn($class); |
||
1714 | |||
1715 | $formView = $this->createMock(FormView::class); |
||
1716 | |||
1717 | $form |
||
1718 | ->method('createView') |
||
1719 | ->willReturn($formView); |
||
1720 | |||
1721 | $this->expectTranslate('flash_lock_error', [ |
||
1722 | '%name%' => $class, |
||
1723 | '%link_start%' => '<a href="stdClass_edit">', |
||
1724 | '%link_end%' => '</a>', |
||
1725 | ], 'SonataAdminBundle'); |
||
1726 | |||
1727 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
1728 | } |
||
1729 | |||
1730 | public function testCreateActionAccessDenied(): void |
||
1731 | { |
||
1732 | $this->expectException(AccessDeniedException::class); |
||
1733 | |||
1734 | $this->admin->expects($this->once()) |
||
1735 | ->method('checkAccess') |
||
1736 | ->with($this->equalTo('create')) |
||
1737 | ->will($this->throwException(new AccessDeniedException())); |
||
1738 | |||
1739 | $this->controller->createAction($this->request); |
||
1740 | } |
||
1741 | |||
1742 | public function testPreCreate(): void |
||
1743 | { |
||
1744 | $object = new \stdClass(); |
||
1745 | $object->foo = 123456; |
||
1746 | |||
1747 | $this->admin->expects($this->once()) |
||
1748 | ->method('checkAccess') |
||
1749 | ->with($this->equalTo('create')); |
||
1750 | |||
1751 | $this->admin |
||
1752 | ->method('getClass') |
||
1753 | ->willReturn(\stdClass::class); |
||
1754 | |||
1755 | $this->admin->expects($this->once()) |
||
1756 | ->method('getNewInstance') |
||
1757 | ->willReturn($object); |
||
1758 | |||
1759 | $controller = $this->createController(PreCRUDController::class); |
||
1760 | |||
1761 | $response = $controller->createAction($this->request); |
||
1762 | $this->assertInstanceOf(Response::class, $response); |
||
1763 | $this->assertSame('preCreate called: 123456', $response->getContent()); |
||
1764 | } |
||
1765 | |||
1766 | public function testCreateAction(): void |
||
1767 | { |
||
1768 | $this->admin->expects($this->once()) |
||
1769 | ->method('checkAccess') |
||
1770 | ->with($this->equalTo('create')); |
||
1771 | |||
1772 | $object = new \stdClass(); |
||
1773 | |||
1774 | $this->admin |
||
1775 | ->method('getClass') |
||
1776 | ->willReturn(\stdClass::class); |
||
1777 | |||
1778 | $this->admin->expects($this->once()) |
||
1779 | ->method('getNewInstance') |
||
1780 | ->willReturn($object); |
||
1781 | |||
1782 | $form = $this->createMock(Form::class); |
||
1783 | |||
1784 | $this->admin->expects($this->once()) |
||
1785 | ->method('getForm') |
||
1786 | ->willReturn($form); |
||
1787 | |||
1788 | $formView = $this->createMock(FormView::class); |
||
1789 | |||
1790 | $form |
||
1791 | ->method('createView') |
||
1792 | ->willReturn($formView); |
||
1793 | |||
1794 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
1795 | |||
1796 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1797 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1798 | |||
1799 | $this->assertSame('create', $this->parameters['action']); |
||
1800 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1801 | $this->assertSame($object, $this->parameters['object']); |
||
1802 | |||
1803 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1804 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1805 | } |
||
1806 | |||
1807 | /** |
||
1808 | * @dataProvider getToStringValues |
||
1809 | */ |
||
1810 | public function testCreateActionSuccess(string $expectedToStringValue, string $toStringValue): void |
||
1811 | { |
||
1812 | $object = new \stdClass(); |
||
1813 | |||
1814 | $this->admin->expects($this->exactly(2)) |
||
1815 | ->method('checkAccess') |
||
1816 | ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): void { |
||
1817 | if ('edit' === $name) { |
||
1818 | return; |
||
1819 | } |
||
1820 | |||
1821 | if ('create' !== $name) { |
||
1822 | throw new AccessDeniedException(); |
||
1823 | } |
||
1824 | |||
1825 | if (null === $objectIn) { |
||
1826 | return; |
||
1827 | } |
||
1828 | |||
1829 | if ($objectIn !== $object) { |
||
1830 | throw new AccessDeniedException(); |
||
1831 | } |
||
1832 | }); |
||
1833 | |||
1834 | $this->admin->expects($this->once()) |
||
1835 | ->method('hasRoute') |
||
1836 | ->with($this->equalTo('edit')) |
||
1837 | ->willReturn(true); |
||
1838 | |||
1839 | $this->admin->expects($this->once()) |
||
1840 | ->method('hasAccess') |
||
1841 | ->with($this->equalTo('edit')) |
||
1842 | ->willReturn(true); |
||
1843 | |||
1844 | $this->admin->expects($this->once()) |
||
1845 | ->method('getNewInstance') |
||
1846 | ->willReturn($object); |
||
1847 | |||
1848 | $this->admin->expects($this->once()) |
||
1849 | ->method('create') |
||
1850 | ->willReturnArgument(0); |
||
1851 | |||
1852 | $form = $this->createMock(Form::class); |
||
1853 | |||
1854 | $this->admin |
||
1855 | ->method('getClass') |
||
1856 | ->willReturn(\stdClass::class); |
||
1857 | |||
1858 | $this->admin->expects($this->once()) |
||
1859 | ->method('getForm') |
||
1860 | ->willReturn($form); |
||
1861 | |||
1862 | $form->expects($this->once()) |
||
1863 | ->method('isSubmitted') |
||
1864 | ->willReturn(true); |
||
1865 | |||
1866 | $form->expects($this->once()) |
||
1867 | ->method('isValid') |
||
1868 | ->willReturn(true); |
||
1869 | |||
1870 | $form->expects($this->once()) |
||
1871 | ->method('getData') |
||
1872 | ->willReturn($object); |
||
1873 | |||
1874 | $this->admin->expects($this->once()) |
||
1875 | ->method('toString') |
||
1876 | ->with($this->equalTo($object)) |
||
1877 | ->willReturn($toStringValue); |
||
1878 | |||
1879 | $this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1880 | |||
1881 | $this->request->setMethod(Request::METHOD_POST); |
||
1882 | |||
1883 | $response = $this->controller->createAction($this->request); |
||
1884 | |||
1885 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1886 | $this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1887 | $this->assertSame('stdClass_edit', $response->getTargetUrl()); |
||
1888 | } |
||
1889 | |||
1890 | public function testCreateActionAccessDenied2(): void |
||
1891 | { |
||
1892 | $this->expectException(AccessDeniedException::class); |
||
1893 | |||
1894 | $object = new \stdClass(); |
||
1895 | |||
1896 | $this->admin |
||
1897 | ->method('checkAccess') |
||
1898 | ->willReturnCallback(static function (string $name, $object = null): void { |
||
1899 | if ('create' !== $name) { |
||
1900 | throw new AccessDeniedException(); |
||
1901 | } |
||
1902 | if (null === $object) { |
||
1903 | return; |
||
1904 | } |
||
1905 | |||
1906 | throw new AccessDeniedException(); |
||
1907 | }); |
||
1908 | |||
1909 | $this->admin->expects($this->once()) |
||
1910 | ->method('getNewInstance') |
||
1911 | ->willReturn($object); |
||
1912 | |||
1913 | $form = $this->createMock(Form::class); |
||
1914 | |||
1915 | $this->admin |
||
1916 | ->method('getClass') |
||
1917 | ->willReturn(\stdClass::class); |
||
1918 | |||
1919 | $this->admin->expects($this->once()) |
||
1920 | ->method('getForm') |
||
1921 | ->willReturn($form); |
||
1922 | |||
1923 | $form->expects($this->once()) |
||
1924 | ->method('isSubmitted') |
||
1925 | ->willReturn(true); |
||
1926 | |||
1927 | $form->expects($this->once()) |
||
1928 | ->method('getData') |
||
1929 | ->willReturn($object); |
||
1930 | |||
1931 | $form->expects($this->once()) |
||
1932 | ->method('isValid') |
||
1933 | ->willReturn(true); |
||
1934 | |||
1935 | $this->request->setMethod(Request::METHOD_POST); |
||
1936 | |||
1937 | $this->controller->createAction($this->request); |
||
1938 | } |
||
1939 | |||
1940 | /** |
||
1941 | * @dataProvider getToStringValues |
||
1942 | */ |
||
1943 | public function testCreateActionError(string $expectedToStringValue, string $toStringValue): void |
||
1944 | { |
||
1945 | $this->admin->expects($this->once()) |
||
1946 | ->method('checkAccess') |
||
1947 | ->with($this->equalTo('create')); |
||
1948 | |||
1949 | $object = new \stdClass(); |
||
1950 | |||
1951 | $this->admin |
||
1952 | ->method('getClass') |
||
1953 | ->willReturn(\stdClass::class); |
||
1954 | |||
1955 | $this->admin->expects($this->once()) |
||
1956 | ->method('getNewInstance') |
||
1957 | ->willReturn($object); |
||
1958 | |||
1959 | $form = $this->createMock(Form::class); |
||
1960 | |||
1961 | $this->admin->expects($this->once()) |
||
1962 | ->method('getForm') |
||
1963 | ->willReturn($form); |
||
1964 | |||
1965 | $form->expects($this->once()) |
||
1966 | ->method('isSubmitted') |
||
1967 | ->willReturn(true); |
||
1968 | |||
1969 | $form->expects($this->once()) |
||
1970 | ->method('isValid') |
||
1971 | ->willReturn(false); |
||
1972 | |||
1973 | $this->admin->expects($this->once()) |
||
1974 | ->method('toString') |
||
1975 | ->with($this->equalTo($object)) |
||
1976 | ->willReturn($toStringValue); |
||
1977 | |||
1978 | $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1979 | |||
1980 | $this->request->setMethod(Request::METHOD_POST); |
||
1981 | |||
1982 | $formView = $this->createMock(FormView::class); |
||
1983 | |||
1984 | $form |
||
1985 | ->method('createView') |
||
1986 | ->willReturn($formView); |
||
1987 | |||
1988 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
1989 | |||
1990 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1991 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1992 | |||
1993 | $this->assertSame('create', $this->parameters['action']); |
||
1994 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1995 | $this->assertSame($object, $this->parameters['object']); |
||
1996 | |||
1997 | $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
||
1998 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1999 | } |
||
2000 | |||
2001 | /** |
||
2002 | * @dataProvider getToStringValues |
||
2003 | */ |
||
2004 | public function testCreateActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void |
||
2005 | { |
||
2006 | $this->admin->expects($this->exactly(2)) |
||
2007 | ->method('checkAccess') |
||
2008 | ->with($this->equalTo('create')); |
||
2009 | |||
2010 | $this->admin |
||
2011 | ->method('getClass') |
||
2012 | ->willReturn(\stdClass::class); |
||
2013 | |||
2014 | $object = new \stdClass(); |
||
2015 | |||
2016 | $this->admin->expects($this->once()) |
||
2017 | ->method('getNewInstance') |
||
2018 | ->willReturn($object); |
||
2019 | |||
2020 | $form = $this->createMock(Form::class); |
||
2021 | |||
2022 | $this->admin->expects($this->once()) |
||
2023 | ->method('getForm') |
||
2024 | ->willReturn($form); |
||
2025 | |||
2026 | $form->expects($this->once()) |
||
2027 | ->method('isValid') |
||
2028 | ->willReturn(true); |
||
2029 | |||
2030 | $this->admin->expects($this->once()) |
||
2031 | ->method('toString') |
||
2032 | ->with($this->equalTo($object)) |
||
2033 | ->willReturn($toStringValue); |
||
2034 | |||
2035 | $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
2036 | |||
2037 | $form->expects($this->once()) |
||
2038 | ->method('isSubmitted') |
||
2039 | ->willReturn(true); |
||
2040 | |||
2041 | $form->expects($this->once()) |
||
2042 | ->method('getData') |
||
2043 | ->willReturn($object); |
||
2044 | |||
2045 | $this->request->setMethod(Request::METHOD_POST); |
||
2046 | |||
2047 | $formView = $this->createMock(FormView::class); |
||
2048 | |||
2049 | $form |
||
2050 | ->method('createView') |
||
2051 | ->willReturn($formView); |
||
2052 | |||
2053 | $this->assertLoggerLogsModelManagerException($this->admin, 'create'); |
||
2054 | |||
2055 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
2056 | |||
2057 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2058 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2059 | |||
2060 | $this->assertSame('create', $this->parameters['action']); |
||
2061 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
2062 | $this->assertSame($object, $this->parameters['object']); |
||
2063 | |||
2064 | $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
||
2065 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
2066 | } |
||
2067 | |||
2068 | public function testCreateActionAjaxSuccess(): void |
||
2069 | { |
||
2070 | $object = new \stdClass(); |
||
2071 | |||
2072 | $this->admin->expects($this->exactly(2)) |
||
2073 | ->method('checkAccess') |
||
2074 | ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): void { |
||
2075 | if ('create' !== $name) { |
||
2076 | throw new AccessDeniedException(); |
||
2077 | } |
||
2078 | |||
2079 | if (null === $objectIn) { |
||
2080 | return; |
||
2081 | } |
||
2082 | |||
2083 | if ($objectIn !== $object) { |
||
2084 | throw new AccessDeniedException(); |
||
2085 | } |
||
2086 | }); |
||
2087 | |||
2088 | $this->admin->expects($this->once()) |
||
2089 | ->method('getNewInstance') |
||
2090 | ->willReturn($object); |
||
2091 | |||
2092 | $this->admin->expects($this->once()) |
||
2093 | ->method('create') |
||
2094 | ->willReturnArgument(0); |
||
2095 | |||
2096 | $form = $this->createMock(Form::class); |
||
2097 | |||
2098 | $this->admin->expects($this->once()) |
||
2099 | ->method('getForm') |
||
2100 | ->willReturn($form); |
||
2101 | |||
2102 | $form->expects($this->once()) |
||
2103 | ->method('isSubmitted') |
||
2104 | ->willReturn(true); |
||
2105 | |||
2106 | $form->expects($this->once()) |
||
2107 | ->method('isValid') |
||
2108 | ->willReturn(true); |
||
2109 | |||
2110 | $form->expects($this->once()) |
||
2111 | ->method('getData') |
||
2112 | ->willReturn($object); |
||
2113 | |||
2114 | $this->admin |
||
2115 | ->method('getClass') |
||
2116 | ->willReturn(\stdClass::class); |
||
2117 | |||
2118 | $this->admin->expects($this->once()) |
||
2119 | ->method('getNormalizedIdentifier') |
||
2120 | ->with($this->equalTo($object)) |
||
2121 | ->willReturn('foo_normalized'); |
||
2122 | |||
2123 | $this->admin->expects($this->once()) |
||
2124 | ->method('toString') |
||
2125 | ->willReturn('foo'); |
||
2126 | |||
2127 | $this->request->setMethod(Request::METHOD_POST); |
||
2128 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
2129 | $this->request->headers->set('Accept', 'application/json'); |
||
2130 | |||
2131 | $response = $this->controller->createAction($this->request); |
||
2132 | |||
2133 | $this->assertInstanceOf(Response::class, $response); |
||
2134 | $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent()); |
||
2135 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2136 | } |
||
2137 | |||
2138 | public function testCreateActionAjaxError(): void |
||
2139 | { |
||
2140 | $this->admin->expects($this->once()) |
||
2141 | ->method('checkAccess') |
||
2142 | ->with($this->equalTo('create')); |
||
2143 | |||
2144 | $object = new \stdClass(); |
||
2145 | |||
2146 | $this->admin->expects($this->once()) |
||
2147 | ->method('getNewInstance') |
||
2148 | ->willReturn($object); |
||
2149 | |||
2150 | $form = $this->createMock(Form::class); |
||
2151 | |||
2152 | $this->admin |
||
2153 | ->method('getClass') |
||
2154 | ->willReturn(\stdClass::class); |
||
2155 | |||
2156 | $this->admin->expects($this->once()) |
||
2157 | ->method('getForm') |
||
2158 | ->willReturn($form); |
||
2159 | |||
2160 | $form->expects($this->once()) |
||
2161 | ->method('isSubmitted') |
||
2162 | ->willReturn(true); |
||
2163 | |||
2164 | $form->expects($this->once()) |
||
2165 | ->method('isValid') |
||
2166 | ->willReturn(false); |
||
2167 | |||
2168 | $formError = $this->createMock(FormError::class); |
||
2169 | $formError->expects($this->atLeastOnce()) |
||
2170 | ->method('getMessage') |
||
2171 | ->willReturn('Form error message'); |
||
2172 | |||
2173 | $form->expects($this->once()) |
||
2174 | ->method('getErrors') |
||
2175 | ->with(true) |
||
2176 | ->willReturn([$formError]); |
||
2177 | |||
2178 | $this->request->setMethod(Request::METHOD_POST); |
||
2179 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
2180 | $this->request->headers->set('Accept', 'application/json'); |
||
2181 | |||
2182 | $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->createAction($this->request)); |
||
2183 | $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent()); |
||
2184 | } |
||
2185 | |||
2186 | public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void |
||
2187 | { |
||
2188 | $this->admin->expects($this->once()) |
||
2189 | ->method('checkAccess') |
||
2190 | ->with($this->equalTo('create')); |
||
2191 | |||
2192 | $object = new \stdClass(); |
||
2193 | |||
2194 | $this->admin->expects($this->once()) |
||
2195 | ->method('getNewInstance') |
||
2196 | ->willReturn($object); |
||
2197 | |||
2198 | $form = $this->createMock(Form::class); |
||
2199 | |||
2200 | $this->admin |
||
2201 | ->method('getClass') |
||
2202 | ->willReturn(\stdClass::class); |
||
2203 | |||
2204 | $this->admin->expects($this->once()) |
||
2205 | ->method('getForm') |
||
2206 | ->willReturn($form); |
||
2207 | |||
2208 | $form->expects($this->once()) |
||
2209 | ->method('isSubmitted') |
||
2210 | ->willReturn(true); |
||
2211 | |||
2212 | $form->expects($this->once()) |
||
2213 | ->method('isValid') |
||
2214 | ->willReturn(false); |
||
2215 | |||
2216 | $this->request->setMethod(Request::METHOD_POST); |
||
2217 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
2218 | |||
2219 | $formView = $this->createMock(FormView::class); |
||
2220 | $form |
||
2221 | ->method('createView') |
||
2222 | ->willReturn($formView); |
||
2223 | |||
2224 | $this->assertInstanceOf(Response::class, $response = $this->controller->createAction($this->request)); |
||
2225 | $this->assertSame(Response::HTTP_NOT_ACCEPTABLE, $response->getStatusCode()); |
||
2226 | } |
||
2227 | |||
2228 | public function testCreateActionWithPreview(): void |
||
2229 | { |
||
2230 | $this->admin->expects($this->once()) |
||
2231 | ->method('checkAccess') |
||
2232 | ->with($this->equalTo('create')); |
||
2233 | |||
2234 | $object = new \stdClass(); |
||
2235 | |||
2236 | $this->admin->expects($this->once()) |
||
2237 | ->method('getNewInstance') |
||
2238 | ->willReturn($object); |
||
2239 | |||
2240 | $form = $this->createMock(Form::class); |
||
2241 | |||
2242 | $this->admin |
||
2243 | ->method('getClass') |
||
2244 | ->willReturn(\stdClass::class); |
||
2245 | |||
2246 | $this->admin->expects($this->once()) |
||
2247 | ->method('getForm') |
||
2248 | ->willReturn($form); |
||
2249 | |||
2250 | $this->admin->expects($this->once()) |
||
2251 | ->method('supportsPreviewMode') |
||
2252 | ->willReturn(true); |
||
2253 | |||
2254 | $formView = $this->createMock(FormView::class); |
||
2255 | |||
2256 | $form |
||
2257 | ->method('createView') |
||
2258 | ->willReturn($formView); |
||
2259 | |||
2260 | $form->expects($this->once()) |
||
2261 | ->method('isSubmitted') |
||
2262 | ->willReturn(true); |
||
2263 | |||
2264 | $form->expects($this->once()) |
||
2265 | ->method('isValid') |
||
2266 | ->willReturn(true); |
||
2267 | |||
2268 | $this->request->setMethod(Request::METHOD_POST); |
||
2269 | $this->request->request->set('btn_preview', 'Preview'); |
||
2270 | |||
2271 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
2272 | |||
2273 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2274 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2275 | |||
2276 | $this->assertSame('create', $this->parameters['action']); |
||
2277 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
2278 | $this->assertSame($object, $this->parameters['object']); |
||
2279 | |||
2280 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2281 | $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
||
2282 | } |
||
2283 | |||
2284 | public function testExportActionAccessDenied(): void |
||
2285 | { |
||
2286 | $this->expectException(AccessDeniedException::class); |
||
2287 | |||
2288 | $this->admin->expects($this->once()) |
||
2289 | ->method('checkAccess') |
||
2290 | ->with($this->equalTo('export')) |
||
2291 | ->will($this->throwException(new AccessDeniedException())); |
||
2292 | |||
2293 | $this->controller->exportAction($this->request); |
||
2294 | } |
||
2295 | |||
2296 | public function testExportActionWrongFormat(): void |
||
2297 | { |
||
2298 | $this->expectException(\RuntimeException::class); |
||
2299 | $this->expectExceptionMessage( |
||
2300 | 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`' |
||
2301 | ); |
||
2302 | |||
2303 | $this->admin->expects($this->once()) |
||
2304 | ->method('checkAccess') |
||
2305 | ->with($this->equalTo('export')); |
||
2306 | |||
2307 | $this->admin->expects($this->once()) |
||
2308 | ->method('getExportFormats') |
||
2309 | ->willReturn(['json']); |
||
2310 | |||
2311 | $this->admin |
||
2312 | ->method('getClass') |
||
2313 | ->willReturn('Foo'); |
||
2314 | |||
2315 | $this->request->query->set('format', 'csv'); |
||
2316 | |||
2317 | $this->controller->exportAction($this->request); |
||
2318 | } |
||
2319 | |||
2320 | public function testExportAction(): void |
||
2321 | { |
||
2322 | $this->admin->expects($this->once()) |
||
2323 | ->method('checkAccess') |
||
2324 | ->with($this->equalTo('export')); |
||
2325 | |||
2326 | $this->admin->expects($this->once()) |
||
2327 | ->method('getExportFormats') |
||
2328 | ->willReturn(['json']); |
||
2329 | |||
2330 | $this->admin->expects($this->once()) |
||
2331 | ->method('getClass') |
||
2332 | ->willReturn(\stdClass::class); |
||
2333 | |||
2334 | $dataSourceIterator = $this->createMock(SourceIteratorInterface::class); |
||
2335 | |||
2336 | $this->admin->expects($this->once()) |
||
2337 | ->method('getDataSourceIterator') |
||
2338 | ->willReturn($dataSourceIterator); |
||
2339 | |||
2340 | $this->request->query->set('format', 'json'); |
||
2341 | |||
2342 | $response = $this->controller->exportAction($this->request); |
||
2343 | $this->assertInstanceOf(StreamedResponse::class, $response); |
||
2344 | $this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
||
2345 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2346 | } |
||
2347 | |||
2348 | public function testHistoryActionAccessDenied(): void |
||
2349 | { |
||
2350 | $this->expectException(AccessDeniedException::class); |
||
2351 | |||
2352 | $this->admin |
||
2353 | ->method('getObject') |
||
2354 | ->willReturn(new \stdClass()); |
||
2355 | |||
2356 | $this->admin->expects($this->once()) |
||
2357 | ->method('checkAccess') |
||
2358 | ->with($this->equalTo('history')) |
||
2359 | ->will($this->throwException(new AccessDeniedException())); |
||
2360 | |||
2361 | $this->controller->historyAction($this->request); |
||
2362 | } |
||
2363 | |||
2364 | public function testHistoryActionNotFoundException(): void |
||
2365 | { |
||
2366 | $this->expectException(NotFoundHttpException::class); |
||
2367 | |||
2368 | $this->admin->expects($this->once()) |
||
2369 | ->method('getObject') |
||
2370 | ->willReturn(null); |
||
2371 | |||
2372 | $this->controller->historyAction($this->request); |
||
2373 | } |
||
2374 | |||
2375 | public function testHistoryActionNoReader(): void |
||
2376 | { |
||
2377 | $this->expectException(NotFoundHttpException::class); |
||
2378 | $this->expectExceptionMessage('unable to find the audit reader for class : Foo'); |
||
2379 | |||
2380 | $this->request->query->set('id', 123); |
||
2381 | |||
2382 | $this->admin->expects($this->once()) |
||
2383 | ->method('checkAccess') |
||
2384 | ->with($this->equalTo('history')); |
||
2385 | |||
2386 | $object = new \stdClass(); |
||
2387 | |||
2388 | $this->admin->expects($this->once()) |
||
2389 | ->method('getObject') |
||
2390 | ->willReturn($object); |
||
2391 | |||
2392 | $this->admin |
||
2393 | ->method('getClass') |
||
2394 | ->willReturn('Foo'); |
||
2395 | |||
2396 | $this->auditManager->expects($this->once()) |
||
2397 | ->method('hasReader') |
||
2398 | ->with($this->equalTo('Foo')) |
||
2399 | ->willReturn(false); |
||
2400 | |||
2401 | $this->controller->historyAction($this->request); |
||
2402 | } |
||
2403 | |||
2404 | public function testHistoryAction(): void |
||
2405 | { |
||
2406 | $this->request->query->set('id', 123); |
||
2407 | |||
2408 | $this->admin->expects($this->once()) |
||
2409 | ->method('checkAccess') |
||
2410 | ->with($this->equalTo('history')); |
||
2411 | |||
2412 | $object = new \stdClass(); |
||
2413 | |||
2414 | $this->admin->expects($this->once()) |
||
2415 | ->method('getObject') |
||
2416 | ->willReturn($object); |
||
2417 | |||
2418 | $this->admin |
||
2419 | ->method('getClass') |
||
2420 | ->willReturn('Foo'); |
||
2421 | |||
2422 | $this->auditManager->expects($this->once()) |
||
2423 | ->method('hasReader') |
||
2424 | ->with($this->equalTo('Foo')) |
||
2425 | ->willReturn(true); |
||
2426 | |||
2427 | $reader = $this->createMock(AuditReaderInterface::class); |
||
2428 | |||
2429 | $this->auditManager->expects($this->once()) |
||
2430 | ->method('getReader') |
||
2431 | ->with($this->equalTo('Foo')) |
||
2432 | ->willReturn($reader); |
||
2433 | |||
2434 | $reader->expects($this->once()) |
||
2435 | ->method('findRevisions') |
||
2436 | ->with($this->equalTo('Foo'), $this->equalTo(123)) |
||
2437 | ->willReturn([]); |
||
2438 | |||
2439 | $this->assertInstanceOf(Response::class, $this->controller->historyAction($this->request)); |
||
2440 | |||
2441 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2442 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2443 | |||
2444 | $this->assertSame('history', $this->parameters['action']); |
||
2445 | $this->assertSame([], $this->parameters['revisions']); |
||
2446 | $this->assertSame($object, $this->parameters['object']); |
||
2447 | |||
2448 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2449 | $this->assertSame('@SonataAdmin/CRUD/history.html.twig', $this->template); |
||
2450 | } |
||
2451 | |||
2452 | public function testAclActionAclNotEnabled(): void |
||
2453 | { |
||
2454 | $this->expectException(NotFoundHttpException::class); |
||
2455 | $this->expectExceptionMessage('ACL are not enabled for this admin'); |
||
2456 | |||
2457 | $this->controller->aclAction($this->request); |
||
2458 | } |
||
2459 | |||
2460 | public function testAclActionNotFoundException(): void |
||
2461 | { |
||
2462 | $this->expectException(NotFoundHttpException::class); |
||
2463 | |||
2464 | $this->admin->expects($this->once()) |
||
2465 | ->method('isAclEnabled') |
||
2466 | ->willReturn(true); |
||
2467 | |||
2468 | $this->admin->expects($this->once()) |
||
2469 | ->method('getObject') |
||
2470 | ->willReturn(null); |
||
2471 | |||
2472 | $this->controller->aclAction($this->request); |
||
2473 | } |
||
2474 | |||
2475 | public function testAclActionAccessDenied(): void |
||
2476 | { |
||
2477 | $this->expectException(AccessDeniedException::class); |
||
2478 | |||
2479 | $this->admin->expects($this->once()) |
||
2480 | ->method('isAclEnabled') |
||
2481 | ->willReturn(true); |
||
2482 | |||
2483 | $object = new \stdClass(); |
||
2484 | |||
2485 | $this->admin->expects($this->once()) |
||
2486 | ->method('getObject') |
||
2487 | ->willReturn($object); |
||
2488 | |||
2489 | $this->admin->expects($this->once()) |
||
2490 | ->method('checkAccess') |
||
2491 | ->with($this->equalTo('acl'), $this->equalTo($object)) |
||
2492 | ->will($this->throwException(new AccessDeniedException())); |
||
2493 | |||
2494 | $this->controller->aclAction($this->request); |
||
2495 | } |
||
2496 | |||
2497 | public function testAclAction(): void |
||
2498 | { |
||
2499 | $this->request->query->set('id', 123); |
||
2500 | |||
2501 | $this->admin->expects($this->exactly(2)) |
||
2502 | ->method('isAclEnabled') |
||
2503 | ->willReturn(true); |
||
2504 | |||
2505 | $object = new \stdClass(); |
||
2506 | |||
2507 | $this->admin->expects($this->once()) |
||
2508 | ->method('getObject') |
||
2509 | ->willReturn($object); |
||
2510 | |||
2511 | $this->admin |
||
2512 | ->expects($this->once()) |
||
2513 | ->method('checkAccess'); |
||
2514 | |||
2515 | $this->admin |
||
2516 | ->method('getSecurityInformation') |
||
2517 | ->willReturn([]); |
||
2518 | |||
2519 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2520 | ->method('getMaskBuilderClass') |
||
2521 | ->willReturn(AdminPermissionMap::class); |
||
2522 | |||
2523 | $aclUsersForm = $this->getMockBuilder(Form::class) |
||
2524 | ->disableOriginalConstructor() |
||
2525 | ->getMock(); |
||
2526 | |||
2527 | $aclUsersForm->expects($this->once()) |
||
2528 | ->method('createView') |
||
2529 | ->willReturn($this->createMock(FormView::class)); |
||
2530 | |||
2531 | $aclRolesForm = $this->getMockBuilder(Form::class) |
||
2532 | ->disableOriginalConstructor() |
||
2533 | ->getMock(); |
||
2534 | |||
2535 | $aclRolesForm->expects($this->once()) |
||
2536 | ->method('createView') |
||
2537 | ->willReturn($this->createMock(FormView::class)); |
||
2538 | |||
2539 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2540 | ->method('createAclUsersForm') |
||
2541 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2542 | ->willReturn($aclUsersForm); |
||
2543 | |||
2544 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2545 | ->method('createAclRolesForm') |
||
2546 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2547 | ->willReturn($aclRolesForm); |
||
2548 | |||
2549 | $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
||
2550 | ->disableOriginalConstructor() |
||
2551 | ->getMock(); |
||
2552 | |||
2553 | $aclSecurityHandler |
||
2554 | ->method('getObjectPermissions') |
||
2555 | ->willReturn([]); |
||
2556 | |||
2557 | $this->admin |
||
2558 | ->method('getSecurityHandler') |
||
2559 | ->willReturn($aclSecurityHandler); |
||
2560 | |||
2561 | $controller = $this->createController(); |
||
2562 | |||
2563 | $this->assertInstanceOf(Response::class, $controller->aclAction($this->request)); |
||
2564 | |||
2565 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2566 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2567 | |||
2568 | $this->assertSame('acl', $this->parameters['action']); |
||
2569 | $this->assertSame([], $this->parameters['permissions']); |
||
2570 | $this->assertSame($object, $this->parameters['object']); |
||
2571 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']); |
||
2572 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']); |
||
2573 | $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']); |
||
2574 | $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']); |
||
2575 | |||
2576 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2577 | $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template); |
||
2578 | } |
||
2579 | |||
2580 | public function testAclActionInvalidUpdate(): void |
||
2581 | { |
||
2582 | $this->request->query->set('id', 123); |
||
2583 | $this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, []); |
||
2584 | |||
2585 | $this->admin->expects($this->exactly(2)) |
||
2586 | ->method('isAclEnabled') |
||
2587 | ->willReturn(true); |
||
2588 | |||
2589 | $object = new \stdClass(); |
||
2590 | |||
2591 | $this->admin->expects($this->once()) |
||
2592 | ->method('getObject') |
||
2593 | ->willReturn($object); |
||
2594 | |||
2595 | $this->admin |
||
2596 | ->expects($this->once()) |
||
2597 | ->method('checkAccess'); |
||
2598 | |||
2599 | $this->admin |
||
2600 | ->method('getSecurityInformation') |
||
2601 | ->willReturn([]); |
||
2602 | |||
2603 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2604 | ->method('getMaskBuilderClass') |
||
2605 | ->willReturn(AdminPermissionMap::class); |
||
2606 | |||
2607 | $aclUsersForm = $this->getMockBuilder(Form::class) |
||
2608 | ->disableOriginalConstructor() |
||
2609 | ->getMock(); |
||
2610 | |||
2611 | $aclUsersForm->expects($this->once()) |
||
2612 | ->method('isValid') |
||
2613 | ->willReturn(false); |
||
2614 | |||
2615 | $aclUsersForm->expects($this->once()) |
||
2616 | ->method('createView') |
||
2617 | ->willReturn($this->createMock(FormView::class)); |
||
2618 | |||
2619 | $aclRolesForm = $this->getMockBuilder(Form::class) |
||
2620 | ->disableOriginalConstructor() |
||
2621 | ->getMock(); |
||
2622 | |||
2623 | $aclRolesForm->expects($this->once()) |
||
2624 | ->method('createView') |
||
2625 | ->willReturn($this->createMock(FormView::class)); |
||
2626 | |||
2627 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2628 | ->method('createAclUsersForm') |
||
2629 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2630 | ->willReturn($aclUsersForm); |
||
2631 | |||
2632 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2633 | ->method('createAclRolesForm') |
||
2634 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2635 | ->willReturn($aclRolesForm); |
||
2636 | |||
2637 | $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
||
2638 | ->disableOriginalConstructor() |
||
2639 | ->getMock(); |
||
2640 | |||
2641 | $aclSecurityHandler |
||
2642 | ->method('getObjectPermissions') |
||
2643 | ->willReturn([]); |
||
2644 | |||
2645 | $this->admin |
||
2646 | ->method('getSecurityHandler') |
||
2647 | ->willReturn($aclSecurityHandler); |
||
2648 | |||
2649 | $this->request->setMethod(Request::METHOD_POST); |
||
2650 | |||
2651 | $this->assertInstanceOf(Response::class, $this->controller->aclAction($this->request)); |
||
2652 | |||
2653 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2654 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2655 | |||
2656 | $this->assertSame('acl', $this->parameters['action']); |
||
2657 | $this->assertSame([], $this->parameters['permissions']); |
||
2658 | $this->assertSame($object, $this->parameters['object']); |
||
2659 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']); |
||
2660 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']); |
||
2661 | $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']); |
||
2662 | $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']); |
||
2663 | |||
2664 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2665 | $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template); |
||
2666 | } |
||
2667 | |||
2668 | public function testAclActionSuccessfulUpdate(): void |
||
2669 | { |
||
2670 | $this->request->query->set('id', 123); |
||
2671 | $this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, []); |
||
2672 | |||
2673 | $this->admin->expects($this->exactly(2)) |
||
2674 | ->method('isAclEnabled') |
||
2675 | ->willReturn(true); |
||
2676 | |||
2677 | $object = new \stdClass(); |
||
2678 | |||
2679 | $this->admin->expects($this->once()) |
||
2680 | ->method('getObject') |
||
2681 | ->willReturn($object); |
||
2682 | |||
2683 | $this->admin |
||
2684 | ->expects($this->once()) |
||
2685 | ->method('checkAccess'); |
||
2686 | |||
2687 | $this->admin |
||
2688 | ->method('getSecurityInformation') |
||
2689 | ->willReturn([]); |
||
2690 | |||
2691 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2692 | ->method('getMaskBuilderClass') |
||
2693 | ->willReturn(AdminPermissionMap::class); |
||
2694 | |||
2695 | $aclUsersForm = $this->getMockBuilder(Form::class) |
||
2696 | ->disableOriginalConstructor() |
||
2697 | ->getMock(); |
||
2698 | |||
2699 | $aclUsersForm |
||
2700 | ->method('createView') |
||
2701 | ->willReturn($this->createMock(FormView::class)); |
||
2702 | |||
2703 | $aclRolesForm = $this->getMockBuilder(Form::class) |
||
2704 | ->disableOriginalConstructor() |
||
2705 | ->getMock(); |
||
2706 | |||
2707 | $aclRolesForm |
||
2708 | ->method('createView') |
||
2709 | ->willReturn($this->createMock(FormView::class)); |
||
2710 | |||
2711 | $aclRolesForm->expects($this->once()) |
||
2712 | ->method('isValid') |
||
2713 | ->willReturn(true); |
||
2714 | |||
2715 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2716 | ->method('createAclUsersForm') |
||
2717 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2718 | ->willReturn($aclUsersForm); |
||
2719 | |||
2720 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2721 | ->method('createAclRolesForm') |
||
2722 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2723 | ->willReturn($aclRolesForm); |
||
2724 | |||
2725 | $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
||
2726 | ->disableOriginalConstructor() |
||
2727 | ->getMock(); |
||
2728 | |||
2729 | $aclSecurityHandler |
||
2730 | ->method('getObjectPermissions') |
||
2731 | ->willReturn([]); |
||
2732 | |||
2733 | $this->admin |
||
2734 | ->method('getSecurityHandler') |
||
2735 | ->willReturn($aclSecurityHandler); |
||
2736 | |||
2737 | $this->expectTranslate('flash_acl_edit_success', [], 'SonataAdminBundle'); |
||
2738 | |||
2739 | $this->request->setMethod(Request::METHOD_POST); |
||
2740 | |||
2741 | $controller = $this->createController(); |
||
2742 | $response = $controller->aclAction($this->request); |
||
2743 | |||
2744 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
2745 | |||
2746 | $this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
2747 | $this->assertSame('stdClass_acl', $response->getTargetUrl()); |
||
2748 | } |
||
2749 | |||
2750 | public function testHistoryViewRevisionActionAccessDenied(): void |
||
2751 | { |
||
2752 | $this->admin |
||
2753 | ->method('getObject') |
||
2754 | ->willReturn(new \stdClass()); |
||
2755 | |||
2756 | $this->admin->expects($this->once()) |
||
2757 | ->method('checkAccess') |
||
2758 | ->with($this->equalTo('historyViewRevision')) |
||
2759 | ->will($this->throwException(new AccessDeniedException())); |
||
2760 | |||
2761 | $this->expectException(AccessDeniedException::class); |
||
2762 | |||
2763 | $this->controller->historyViewRevisionAction($this->request, null); |
||
2764 | } |
||
2765 | |||
2766 | public function testHistoryViewRevisionActionNotFoundException(): void |
||
2767 | { |
||
2768 | $this->request->query->set('id', 123); |
||
2769 | |||
2770 | $this->admin->expects($this->once()) |
||
2771 | ->method('getObject') |
||
2772 | ->willReturn(null); |
||
2773 | |||
2774 | $this->expectException(NotFoundHttpException::class); |
||
2775 | $this->expectExceptionMessage('unable to find the object with id: 123'); |
||
2776 | |||
2777 | $this->controller->historyViewRevisionAction($this->request, null); |
||
2778 | } |
||
2779 | |||
2780 | public function testHistoryViewRevisionActionNoReader(): void |
||
2781 | { |
||
2782 | $this->expectException(NotFoundHttpException::class); |
||
2783 | $this->expectExceptionMessage('unable to find the audit reader for class : Foo'); |
||
2784 | |||
2785 | $this->request->query->set('id', 123); |
||
2786 | |||
2787 | $this->admin->expects($this->once()) |
||
2788 | ->method('checkAccess') |
||
2789 | ->with($this->equalTo('historyViewRevision')); |
||
2790 | |||
2791 | $object = new \stdClass(); |
||
2792 | |||
2793 | $this->admin->expects($this->once()) |
||
2794 | ->method('getObject') |
||
2795 | ->willReturn($object); |
||
2796 | |||
2797 | $this->admin |
||
2798 | ->method('getClass') |
||
2799 | ->willReturn('Foo'); |
||
2800 | |||
2801 | $this->auditManager->expects($this->once()) |
||
2802 | ->method('hasReader') |
||
2803 | ->with($this->equalTo('Foo')) |
||
2804 | ->willReturn(false); |
||
2805 | |||
2806 | $this->controller->historyViewRevisionAction($this->request, null); |
||
2807 | } |
||
2808 | |||
2809 | public function testHistoryViewRevisionActionNotFoundRevision(): void |
||
2810 | { |
||
2811 | $this->expectException(NotFoundHttpException::class); |
||
2812 | $this->expectExceptionMessage( |
||
2813 | 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`' |
||
2814 | ); |
||
2815 | |||
2816 | $this->request->query->set('id', 123); |
||
2817 | |||
2818 | $this->admin->expects($this->once()) |
||
2819 | ->method('checkAccess') |
||
2820 | ->with($this->equalTo('historyViewRevision')); |
||
2821 | |||
2822 | $object = new \stdClass(); |
||
2823 | |||
2824 | $this->admin->expects($this->once()) |
||
2825 | ->method('getObject') |
||
2826 | ->willReturn($object); |
||
2827 | |||
2828 | $this->admin |
||
2829 | ->method('getClass') |
||
2830 | ->willReturn('Foo'); |
||
2831 | |||
2832 | $this->auditManager->expects($this->once()) |
||
2833 | ->method('hasReader') |
||
2834 | ->with($this->equalTo('Foo')) |
||
2835 | ->willReturn(true); |
||
2836 | |||
2837 | $reader = $this->createMock(AuditReaderInterface::class); |
||
2838 | |||
2839 | $this->auditManager->expects($this->once()) |
||
2840 | ->method('getReader') |
||
2841 | ->with($this->equalTo('Foo')) |
||
2842 | ->willReturn($reader); |
||
2843 | |||
2844 | $reader->expects($this->once()) |
||
2845 | ->method('find') |
||
2846 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
2847 | ->willReturn(null); |
||
2848 | |||
2849 | $this->controller->historyViewRevisionAction($this->request, 456); |
||
2850 | } |
||
2851 | |||
2852 | public function testHistoryViewRevisionAction(): void |
||
2853 | { |
||
2854 | $this->request->query->set('id', 123); |
||
2855 | |||
2856 | $this->admin->expects($this->once()) |
||
2857 | ->method('checkAccess') |
||
2858 | ->with($this->equalTo('historyViewRevision')); |
||
2859 | |||
2860 | $object = new \stdClass(); |
||
2861 | |||
2862 | $this->admin->expects($this->once()) |
||
2863 | ->method('getObject') |
||
2864 | ->willReturn($object); |
||
2865 | |||
2866 | $this->admin |
||
2867 | ->method('getClass') |
||
2868 | ->willReturn('Foo'); |
||
2869 | |||
2870 | $this->auditManager->expects($this->once()) |
||
2871 | ->method('hasReader') |
||
2872 | ->with($this->equalTo('Foo')) |
||
2873 | ->willReturn(true); |
||
2874 | |||
2875 | $reader = $this->createMock(AuditReaderInterface::class); |
||
2876 | |||
2877 | $this->auditManager->expects($this->once()) |
||
2878 | ->method('getReader') |
||
2879 | ->with($this->equalTo('Foo')) |
||
2880 | ->willReturn($reader); |
||
2881 | |||
2882 | $objectRevision = new \stdClass(); |
||
2883 | $objectRevision->revision = 456; |
||
2884 | |||
2885 | $reader->expects($this->once()) |
||
2886 | ->method('find') |
||
2887 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
2888 | ->willReturn($objectRevision); |
||
2889 | |||
2890 | $this->admin->expects($this->once()) |
||
2891 | ->method('setSubject') |
||
2892 | ->with($this->equalTo($objectRevision)); |
||
2893 | |||
2894 | $fieldDescriptionCollection = new FieldDescriptionCollection(); |
||
2895 | $this->admin->expects($this->once()) |
||
2896 | ->method('getShow') |
||
2897 | ->willReturn($fieldDescriptionCollection); |
||
2898 | |||
2899 | $this->assertInstanceOf(Response::class, $this->controller->historyViewRevisionAction($this->request, 456)); |
||
2900 | |||
2901 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2902 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2903 | |||
2904 | $this->assertSame('show', $this->parameters['action']); |
||
2905 | $this->assertSame($objectRevision, $this->parameters['object']); |
||
2906 | $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
||
2907 | |||
2908 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2909 | $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template); |
||
2910 | } |
||
2911 | |||
2912 | public function testHistoryCompareRevisionsActionAccessDenied(): void |
||
2913 | { |
||
2914 | $this->expectException(AccessDeniedException::class); |
||
2915 | |||
2916 | $this->admin->expects($this->once()) |
||
2917 | ->method('checkAccess') |
||
2918 | ->with($this->equalTo('historyCompareRevisions')) |
||
2919 | ->will($this->throwException(new AccessDeniedException())); |
||
2920 | |||
2921 | $this->controller->historyCompareRevisionsAction($this->request, null, null); |
||
2922 | } |
||
2923 | |||
2924 | public function testHistoryCompareRevisionsActionNotFoundException(): void |
||
2925 | { |
||
2926 | $this->expectException(NotFoundHttpException::class); |
||
2927 | $this->expectExceptionMessage('unable to find the object with id: 123'); |
||
2928 | |||
2929 | $this->request->query->set('id', 123); |
||
2930 | |||
2931 | $this->admin->expects($this->once()) |
||
2932 | ->method('checkAccess') |
||
2933 | ->with($this->equalTo('historyCompareRevisions')); |
||
2934 | |||
2935 | $this->admin->expects($this->once()) |
||
2936 | ->method('getObject') |
||
2937 | ->willReturn(null); |
||
2938 | |||
2939 | $this->controller->historyCompareRevisionsAction($this->request, null, null); |
||
2940 | } |
||
2941 | |||
2942 | public function testHistoryCompareRevisionsActionNoReader(): void |
||
2943 | { |
||
2944 | $this->expectException(NotFoundHttpException::class); |
||
2945 | $this->expectExceptionMessage('unable to find the audit reader for class : Foo'); |
||
2946 | |||
2947 | $this->request->query->set('id', 123); |
||
2948 | |||
2949 | $this->admin->expects($this->once()) |
||
2950 | ->method('checkAccess') |
||
2951 | ->with($this->equalTo('historyCompareRevisions')); |
||
2952 | |||
2953 | $object = new \stdClass(); |
||
2954 | |||
2955 | $this->admin->expects($this->once()) |
||
2956 | ->method('getObject') |
||
2957 | ->willReturn($object); |
||
2958 | |||
2959 | $this->admin |
||
2960 | ->method('getClass') |
||
2961 | ->willReturn('Foo'); |
||
2962 | |||
2963 | $this->auditManager->expects($this->once()) |
||
2964 | ->method('hasReader') |
||
2965 | ->with($this->equalTo('Foo')) |
||
2966 | ->willReturn(false); |
||
2967 | |||
2968 | $this->controller->historyCompareRevisionsAction($this->request, null, null); |
||
2969 | } |
||
2970 | |||
2971 | public function testHistoryCompareRevisionsActionNotFoundBaseRevision(): void |
||
2972 | { |
||
2973 | $this->expectException(NotFoundHttpException::class); |
||
2974 | $this->expectExceptionMessage( |
||
2975 | 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`' |
||
2976 | ); |
||
2977 | |||
2978 | $this->request->query->set('id', 123); |
||
2979 | |||
2980 | $this->admin->expects($this->once()) |
||
2981 | ->method('checkAccess') |
||
2982 | ->with($this->equalTo('historyCompareRevisions')); |
||
2983 | |||
2984 | $object = new \stdClass(); |
||
2985 | |||
2986 | $this->admin->expects($this->once()) |
||
2987 | ->method('getObject') |
||
2988 | ->willReturn($object); |
||
2989 | |||
2990 | $this->admin |
||
2991 | ->method('getClass') |
||
2992 | ->willReturn('Foo'); |
||
2993 | |||
2994 | $this->auditManager->expects($this->once()) |
||
2995 | ->method('hasReader') |
||
2996 | ->with($this->equalTo('Foo')) |
||
2997 | ->willReturn(true); |
||
2998 | |||
2999 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3000 | |||
3001 | $this->auditManager->expects($this->once()) |
||
3002 | ->method('getReader') |
||
3003 | ->with($this->equalTo('Foo')) |
||
3004 | ->willReturn($reader); |
||
3005 | |||
3006 | // once because it will not be found and therefore the second call won't be executed |
||
3007 | $reader->expects($this->once()) |
||
3008 | ->method('find') |
||
3009 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3010 | ->willReturn(null); |
||
3011 | |||
3012 | $this->controller->historyCompareRevisionsAction($this->request, 456, 789); |
||
3013 | } |
||
3014 | |||
3015 | public function testHistoryCompareRevisionsActionNotFoundCompareRevision(): void |
||
3016 | { |
||
3017 | $this->expectException(NotFoundHttpException::class); |
||
3018 | $this->expectExceptionMessage( |
||
3019 | 'unable to find the targeted object `123` from the revision `789` with classname : `Foo`' |
||
3020 | ); |
||
3021 | |||
3022 | $this->request->query->set('id', 123); |
||
3023 | |||
3024 | $this->admin->expects($this->once()) |
||
3025 | ->method('checkAccess') |
||
3026 | ->with($this->equalTo('historyCompareRevisions')); |
||
3027 | |||
3028 | $object = new \stdClass(); |
||
3029 | |||
3030 | $this->admin->expects($this->once()) |
||
3031 | ->method('getObject') |
||
3032 | ->willReturn($object); |
||
3033 | |||
3034 | $this->admin |
||
3035 | ->method('getClass') |
||
3036 | ->willReturn('Foo'); |
||
3037 | |||
3038 | $this->auditManager->expects($this->once()) |
||
3039 | ->method('hasReader') |
||
3040 | ->with($this->equalTo('Foo')) |
||
3041 | ->willReturn(true); |
||
3042 | |||
3043 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3044 | |||
3045 | $this->auditManager->expects($this->once()) |
||
3046 | ->method('getReader') |
||
3047 | ->with($this->equalTo('Foo')) |
||
3048 | ->willReturn($reader); |
||
3049 | |||
3050 | $objectRevision = new \stdClass(); |
||
3051 | $objectRevision->revision = 456; |
||
3052 | |||
3053 | // first call should return, so the second call will throw an exception |
||
3054 | $reader->expects($this->at(0)) |
||
3055 | ->method('find') |
||
3056 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3057 | ->willReturn($objectRevision); |
||
3058 | |||
3059 | $reader->expects($this->at(1)) |
||
3060 | ->method('find') |
||
3061 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
||
3062 | ->willReturn(null); |
||
3063 | |||
3064 | $this->controller->historyCompareRevisionsAction($this->request, 456, 789); |
||
3065 | } |
||
3066 | |||
3067 | public function testHistoryCompareRevisionsActionAction(): void |
||
3068 | { |
||
3069 | $this->request->query->set('id', 123); |
||
3070 | |||
3071 | $this->admin->expects($this->once()) |
||
3072 | ->method('checkAccess') |
||
3073 | ->with($this->equalTo('historyCompareRevisions')); |
||
3074 | |||
3075 | $object = new \stdClass(); |
||
3076 | |||
3077 | $this->admin->expects($this->once()) |
||
3078 | ->method('getObject') |
||
3079 | ->willReturn($object); |
||
3080 | |||
3081 | $this->admin |
||
3082 | ->method('getClass') |
||
3083 | ->willReturn('Foo'); |
||
3084 | |||
3085 | $this->auditManager->expects($this->once()) |
||
3086 | ->method('hasReader') |
||
3087 | ->with($this->equalTo('Foo')) |
||
3088 | ->willReturn(true); |
||
3089 | |||
3090 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3091 | |||
3092 | $this->auditManager->expects($this->once()) |
||
3093 | ->method('getReader') |
||
3094 | ->with($this->equalTo('Foo')) |
||
3095 | ->willReturn($reader); |
||
3096 | |||
3097 | $objectRevision = new \stdClass(); |
||
3098 | $objectRevision->revision = 456; |
||
3099 | |||
3100 | $compareObjectRevision = new \stdClass(); |
||
3101 | $compareObjectRevision->revision = 789; |
||
3102 | |||
3103 | $reader->expects($this->at(0)) |
||
3104 | ->method('find') |
||
3105 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3106 | ->willReturn($objectRevision); |
||
3107 | |||
3108 | $reader->expects($this->at(1)) |
||
3109 | ->method('find') |
||
3110 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
||
3111 | ->willReturn($compareObjectRevision); |
||
3112 | |||
3113 | $this->admin->expects($this->once()) |
||
3114 | ->method('setSubject') |
||
3115 | ->with($this->equalTo($objectRevision)); |
||
3116 | |||
3117 | $fieldDescriptionCollection = new FieldDescriptionCollection(); |
||
3118 | $this->admin->expects($this->once()) |
||
3119 | ->method('getShow') |
||
3120 | ->willReturn($fieldDescriptionCollection); |
||
3121 | |||
3122 | $this->assertInstanceOf(Response::class, $this->controller->historyCompareRevisionsAction($this->request, 456, 789)); |
||
3123 | |||
3124 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
3125 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
3126 | |||
3127 | $this->assertSame('show', $this->parameters['action']); |
||
3128 | $this->assertSame($objectRevision, $this->parameters['object']); |
||
3129 | $this->assertSame($compareObjectRevision, $this->parameters['object_compare']); |
||
3130 | $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
||
3131 | |||
3132 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
3133 | $this->assertSame('@SonataAdmin/CRUD/show_compare.html.twig', $this->template); |
||
3134 | } |
||
3135 | |||
3136 | public function testBatchActionWrongMethod(): void |
||
3137 | { |
||
3138 | $this->expectException(NotFoundHttpException::class); |
||
3139 | $this->expectExceptionMessage('Invalid request method given "GET", POST expected'); |
||
3140 | |||
3141 | $this->controller->batchAction($this->request); |
||
3142 | } |
||
3143 | |||
3144 | public function testBatchActionActionNotDefined(): void |
||
3145 | { |
||
3146 | $this->expectException(\RuntimeException::class); |
||
3147 | $this->expectExceptionMessage('The `foo` batch action is not defined'); |
||
3148 | |||
3149 | $batchActions = []; |
||
3150 | |||
3151 | $this->admin->expects($this->once()) |
||
3152 | ->method('getBatchActions') |
||
3153 | ->willReturn($batchActions); |
||
3154 | |||
3155 | $this->request->setMethod(Request::METHOD_POST); |
||
3156 | $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
||
3157 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3158 | |||
3159 | $this->controller->batchAction($this->request); |
||
3160 | } |
||
3161 | |||
3162 | public function testBatchActionActionInvalidCsrfToken(): void |
||
3163 | { |
||
3164 | $this->request->setMethod(Request::METHOD_POST); |
||
3165 | $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
||
3166 | $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
||
3167 | |||
3168 | try { |
||
3169 | $this->controller->batchAction($this->request); |
||
3170 | } catch (HttpException $e) { |
||
3171 | $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
||
3172 | $this->assertSame(400, $e->getStatusCode()); |
||
3173 | } |
||
3174 | } |
||
3175 | |||
3176 | /** |
||
3177 | * NEXT_MAJOR: Remove this legacy group. |
||
3178 | * |
||
3179 | * @group legacy |
||
3180 | */ |
||
3181 | public function testBatchActionMethodNotExist(): void |
||
3182 | { |
||
3183 | $this->expectException(\RuntimeException::class); |
||
3184 | $this->expectExceptionMessage( |
||
3185 | 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable' |
||
3186 | ); |
||
3187 | |||
3188 | $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3189 | |||
3190 | $this->admin->expects($this->once()) |
||
3191 | ->method('getBatchActions') |
||
3192 | ->willReturn($batchActions); |
||
3193 | |||
3194 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3195 | $this->admin->expects($this->once()) |
||
3196 | ->method('getDatagrid') |
||
3197 | ->willReturn($datagrid); |
||
3198 | |||
3199 | $this->request->setMethod(Request::METHOD_POST); |
||
3200 | $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
||
3201 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3202 | |||
3203 | $this->controller->batchAction($this->request); |
||
3204 | } |
||
3205 | |||
3206 | public function testBatchActionWithoutConfirmation(): void |
||
3207 | { |
||
3208 | $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3209 | |||
3210 | $this->admin->expects($this->once()) |
||
3211 | ->method('getBatchActions') |
||
3212 | ->willReturn($batchActions); |
||
3213 | |||
3214 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3215 | |||
3216 | $query = $this->createMock(ProxyQueryInterface::class); |
||
3217 | $datagrid->expects($this->once()) |
||
3218 | ->method('getQuery') |
||
3219 | ->willReturn($query); |
||
3220 | |||
3221 | $this->admin->expects($this->once()) |
||
3222 | ->method('getDatagrid') |
||
3223 | ->willReturn($datagrid); |
||
3224 | |||
3225 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
3226 | |||
3227 | $this->admin->expects($this->once()) |
||
3228 | ->method('checkAccess') |
||
3229 | ->with($this->equalTo('batchDelete')); |
||
3230 | |||
3231 | $this->admin |
||
3232 | ->method('getModelManager') |
||
3233 | ->willReturn($modelManager); |
||
3234 | |||
3235 | $this->admin |
||
3236 | ->method('getClass') |
||
3237 | ->willReturn('Foo'); |
||
3238 | |||
3239 | $modelManager->expects($this->once()) |
||
3240 | ->method('addIdentifiersToQuery') |
||
3241 | ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
||
3242 | ->willReturn(true); |
||
3243 | |||
3244 | $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
||
3245 | |||
3246 | $this->request->setMethod(Request::METHOD_POST); |
||
3247 | $this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false])); |
||
3248 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3249 | |||
3250 | $result = $this->controller->batchAction($this->request); |
||
3251 | |||
3252 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
3253 | $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
3254 | $this->assertSame('list', $result->getTargetUrl()); |
||
3255 | } |
||
3256 | |||
3257 | public function testBatchActionWithoutConfirmation2(): void |
||
3258 | { |
||
3259 | $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3260 | |||
3261 | $this->admin->expects($this->once()) |
||
3262 | ->method('getBatchActions') |
||
3263 | ->willReturn($batchActions); |
||
3264 | |||
3265 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3266 | |||
3267 | $query = $this->createMock(ProxyQueryInterface::class); |
||
3268 | $datagrid->expects($this->once()) |
||
3269 | ->method('getQuery') |
||
3270 | ->willReturn($query); |
||
3271 | |||
3272 | $this->admin->expects($this->once()) |
||
3273 | ->method('getDatagrid') |
||
3274 | ->willReturn($datagrid); |
||
3275 | |||
3276 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
3277 | |||
3278 | $this->admin->expects($this->once()) |
||
3279 | ->method('checkAccess') |
||
3280 | ->with($this->equalTo('batchDelete')); |
||
3281 | |||
3282 | $this->admin |
||
3283 | ->method('getModelManager') |
||
3284 | ->willReturn($modelManager); |
||
3285 | |||
3286 | $this->admin |
||
3287 | ->method('getClass') |
||
3288 | ->willReturn('Foo'); |
||
3289 | |||
3290 | $modelManager->expects($this->once()) |
||
3291 | ->method('addIdentifiersToQuery') |
||
3292 | ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
||
3293 | ->willReturn(true); |
||
3294 | |||
3295 | $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
||
3296 | |||
3297 | $this->request->setMethod(Request::METHOD_POST); |
||
3298 | $this->request->request->set('action', 'delete'); |
||
3299 | $this->request->request->set('idx', ['123', '456']); |
||
3300 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3301 | |||
3302 | $result = $this->controller->batchAction($this->request); |
||
3303 | |||
3304 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
3305 | $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
3306 | $this->assertSame('list', $result->getTargetUrl()); |
||
3307 | } |
||
3308 | |||
3309 | public function testBatchActionWithConfirmation(): void |
||
3310 | { |
||
3311 | $batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]]; |
||
3312 | |||
3313 | $this->admin->expects($this->once()) |
||
3314 | ->method('getBatchActions') |
||
3315 | ->willReturn($batchActions); |
||
3316 | |||
3317 | $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]; |
||
3318 | |||
3319 | $this->request->setMethod(Request::METHOD_POST); |
||
3320 | $this->request->request->set('data', json_encode($data)); |
||
3321 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3322 | |||
3323 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3324 | |||
3325 | $this->admin->expects($this->once()) |
||
3326 | ->method('getDatagrid') |
||
3327 | ->willReturn($datagrid); |
||
3328 | |||
3329 | $form = $this->getMockBuilder(Form::class) |
||
3330 | ->disableOriginalConstructor() |
||
3331 | ->getMock(); |
||
3332 | |||
3333 | $form->expects($this->once()) |
||
3334 | ->method('createView') |
||
3335 | ->willReturn($this->createMock(FormView::class)); |
||
3336 | |||
3337 | $datagrid->expects($this->once()) |
||
3338 | ->method('getForm') |
||
3339 | ->willReturn($form); |
||
3340 | |||
3341 | $this->assertInstanceOf(Response::class, $this->controller->batchAction($this->request)); |
||
3342 | |||
3343 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
3344 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
3345 | |||
3346 | $this->assertSame('list', $this->parameters['action']); |
||
3347 | $this->assertSame($datagrid, $this->parameters['datagrid']); |
||
3348 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
3349 | $this->assertSame($data, $this->parameters['data']); |
||
3350 | $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']); |
||
3351 | $this->assertSame('Foo Bar', $this->parameters['action_label']); |
||
3352 | |||
3353 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
3354 | $this->assertSame('@SonataAdmin/CRUD/batch_confirmation.html.twig', $this->template); |
||
3355 | } |
||
3356 | |||
3357 | public function testBatchActionNonRelevantAction(): void |
||
3358 | { |
||
3359 | $controller = $this->createController(BatchAdminController::class); |
||
3360 | |||
3361 | $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3362 | |||
3363 | $this->admin->expects($this->once()) |
||
3364 | ->method('getBatchActions') |
||
3365 | ->willReturn($batchActions); |
||
3366 | |||
3367 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3368 | |||
3369 | $this->admin->expects($this->once()) |
||
3370 | ->method('getDatagrid') |
||
3371 | ->willReturn($datagrid); |
||
3372 | |||
3373 | $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle'); |
||
3374 | |||
3375 | $this->request->setMethod(Request::METHOD_POST); |
||
3376 | $this->request->request->set('action', 'foo'); |
||
3377 | $this->request->request->set('idx', ['789']); |
||
3378 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3379 | |||
3380 | $result = $controller->batchAction($this->request); |
||
3381 | |||
3382 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
3383 | $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info')); |
||
3384 | $this->assertSame('list', $result->getTargetUrl()); |
||
3385 | } |
||
3386 | |||
3387 | public function testBatchActionWithCustomConfirmationTemplate(): void |
||
3421 | |||
3422 | public function testBatchActionNonRelevantAction2(): void |
||
3423 | { |
||
3424 | $controller = $this->createController(BatchAdminController::class); |
||
3425 | |||
3426 | $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3427 | |||
3428 | $this->admin->expects($this->once()) |
||
3429 | ->method('getBatchActions') |
||
3430 | ->willReturn($batchActions); |
||
3431 | |||
3432 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3433 | |||
3434 | $this->admin->expects($this->once()) |
||
3435 | ->method('getDatagrid') |
||
3436 | ->willReturn($datagrid); |
||
3437 | |||
3438 | $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle'); |
||
3439 | |||
3440 | $this->request->setMethod(Request::METHOD_POST); |
||
3441 | $this->request->request->set('action', 'foo'); |
||
3442 | $this->request->request->set('idx', ['999']); |
||
3443 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3444 | |||
3445 | $result = $controller->batchAction($this->request); |
||
3446 | |||
3447 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
3448 | $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info')); |
||
3449 | $this->assertSame('list', $result->getTargetUrl()); |
||
3450 | } |
||
3451 | |||
3452 | public function testBatchActionNoItems(): void |
||
3453 | { |
||
3454 | $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true]]; |
||
3455 | |||
3456 | $this->admin->expects($this->once()) |
||
3457 | ->method('getBatchActions') |
||
3458 | ->willReturn($batchActions); |
||
3459 | |||
3460 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3461 | |||
3462 | $this->admin->expects($this->once()) |
||
3463 | ->method('getDatagrid') |
||
3464 | ->willReturn($datagrid); |
||
3465 | |||
3466 | $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle'); |
||
3467 | |||
3468 | $this->request->setMethod(Request::METHOD_POST); |
||
3469 | $this->request->request->set('action', 'delete'); |
||
3470 | $this->request->request->set('idx', []); |
||
3471 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3472 | |||
3473 | $result = $this->controller->batchAction($this->request); |
||
3474 | |||
3475 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
3476 | $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info')); |
||
3477 | $this->assertSame('list', $result->getTargetUrl()); |
||
3478 | } |
||
3479 | |||
3480 | public function testBatchActionNoItemsEmptyQuery(): void |
||
3481 | { |
||
3482 | $controller = $this->createController(BatchAdminController::class); |
||
3483 | |||
3484 | $batchActions = ['bar' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3485 | |||
3486 | $this->admin->expects($this->once()) |
||
3487 | ->method('getBatchActions') |
||
3488 | ->willReturn($batchActions); |
||
3489 | |||
3490 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3491 | |||
3492 | $query = $this->createMock(ProxyQueryInterface::class); |
||
3493 | $datagrid->expects($this->once()) |
||
3494 | ->method('getQuery') |
||
3495 | ->willReturn($query); |
||
3496 | |||
3497 | $this->admin->expects($this->once()) |
||
3498 | ->method('getDatagrid') |
||
3499 | ->willReturn($datagrid); |
||
3500 | |||
3501 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
3502 | |||
3503 | $this->admin |
||
3504 | ->method('getModelManager') |
||
3505 | ->willReturn($modelManager); |
||
3506 | |||
3507 | $this->admin |
||
3508 | ->method('getClass') |
||
3509 | ->willReturn('Foo'); |
||
3510 | |||
3511 | $this->request->setMethod(Request::METHOD_POST); |
||
3512 | $this->request->request->set('action', 'bar'); |
||
3513 | $this->request->request->set('idx', []); |
||
3514 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3515 | |||
3516 | $this->expectTranslate('flash_batch_no_elements_processed', [], 'SonataAdminBundle'); |
||
3517 | $result = $controller->batchAction($this->request); |
||
3518 | |||
3519 | $this->assertInstanceOf(Response::class, $result); |
||
3520 | $this->assertRegExp('/Redirecting to list/', $result->getContent()); |
||
3521 | } |
||
3522 | |||
3523 | public function testBatchActionWithRequesData(): void |
||
3575 | |||
3576 | public function getCsrfProvider() |
||
3580 | |||
3581 | public function getToStringValues() |
||
3590 | |||
3591 | private function assertLoggerLogsModelManagerException($subject, string $method): void |
||
3612 | |||
3613 | private function expectTranslate( |
||
3624 | |||
3625 | private function createTwig(): Environment |
||
3626 | { |
||
3627 | $templatingRenderReturnCallback = $this->returnCallback(function ( |
||
3628 | string $name, |
||
3629 | array $context = [] |
||
3630 | ): string { |
||
3631 | $this->template = $name; |
||
3632 | |||
3633 | $this->parameters = $context; |
||
3634 | |||
3635 | return ''; |
||
3636 | }); |
||
3637 | |||
3638 | $twig = $this->getMockBuilder(Environment::class) |
||
3639 | ->disableOriginalConstructor() |
||
3640 | ->getMock(); |
||
3641 | |||
3642 | $twig |
||
3643 | ->method('getRuntime') |
||
3644 | ->willReturn($this->createMock(FormRenderer::class)); |
||
3645 | |||
3646 | $twig |
||
3647 | ->method('render') |
||
3648 | ->will($templatingRenderReturnCallback); |
||
3649 | |||
3650 | return $twig; |
||
3651 | } |
||
3652 | |||
3653 | private function createController(string $classname = CRUDController::class): CRUDController |
||
3654 | { |
||
3655 | $requestStack = new RequestStack(); |
||
3656 | $requestStack->push($this->request); |
||
3657 | |||
3658 | $pool = new Pool($this->container, 'title', 'logo.png'); |
||
3659 | $pool->setAdminServiceIds(['foo.admin']); |
||
3660 | |||
3661 | return new $classname( |
||
3662 | $requestStack, |
||
3663 | $pool, |
||
3664 | $this->templateRegistry->reveal(), |
||
3665 | new BreadcrumbsBuilder([]), |
||
3666 | $this->createTwig(), |
||
3667 | $this->translator, |
||
3668 | $this->session, |
||
3669 | $this->kernel, |
||
3670 | $this->auditManager, |
||
3671 | $this->exporter, |
||
3672 | new AdminExporter($this->exporter), |
||
3673 | $this->csrfProvider, |
||
3674 | $this->logger, |
||
3675 | $this->adminObjectAclManipulator |
||
3676 | ); |
||
3677 | } |
||
3678 | } |
||
3679 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.