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