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