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 |
||
| 70 | class CRUDControllerTest extends TestCase |
||
| 71 | { |
||
| 72 | /** |
||
| 73 | * @var CRUDController |
||
| 74 | */ |
||
| 75 | private $controller; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Request |
||
| 79 | */ |
||
| 80 | private $request; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var AdminInterface |
||
| 84 | */ |
||
| 85 | private $admin; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var TemplateRegistryInterface |
||
| 89 | */ |
||
| 90 | private $templateRegistry; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Pool |
||
| 94 | */ |
||
| 95 | private $pool; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private $parameters; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var Session |
||
| 104 | */ |
||
| 105 | private $session; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var AuditManagerInterface |
||
| 109 | */ |
||
| 110 | private $auditManager; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var ContainerInterface |
||
| 114 | */ |
||
| 115 | private $container; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var AdminObjectAclManipulator |
||
| 119 | */ |
||
| 120 | private $adminObjectAclManipulator; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private $template; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | private $protectedTestedMethods; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var CsrfTokenManagerInterface |
||
| 134 | */ |
||
| 135 | private $csrfProvider; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var KernelInterface |
||
| 139 | */ |
||
| 140 | private $kernel; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var TranslatorInterface |
||
| 144 | */ |
||
| 145 | private $translator; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var LoggerInterface|MockObject |
||
| 149 | */ |
||
| 150 | private $logger; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | protected function setUp(): void |
||
| 156 | { |
||
| 157 | $this->container = new Container(); |
||
| 158 | $this->request = new Request(); |
||
| 159 | $this->pool = new Pool($this->container, 'title', 'logo.png'); |
||
| 160 | $this->pool->setAdminServiceIds(['foo.admin']); |
||
| 161 | $this->request->attributes->set('_sonata_admin', 'foo.admin'); |
||
| 162 | $this->admin = $this->createMock(AdminInterface::class); |
||
| 163 | $this->translator = $this->createMock(TranslatorInterface::class); |
||
| 164 | $this->parameters = []; |
||
| 165 | $this->template = ''; |
||
| 166 | |||
| 167 | $this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
||
| 168 | |||
| 169 | $templatingRenderReturnCallback = $this->returnCallback(function ( |
||
| 170 | string $name, |
||
| 171 | array $context = [] |
||
| 172 | ): string { |
||
| 173 | $this->template = $name; |
||
| 174 | |||
| 175 | $this->parameters = $context; |
||
| 176 | |||
| 177 | return ''; |
||
| 178 | }); |
||
| 179 | |||
| 180 | $this->session = new Session(new MockArraySessionStorage()); |
||
| 181 | |||
| 182 | $twig = $this->getMockBuilder(Environment::class) |
||
| 183 | ->disableOriginalConstructor() |
||
| 184 | ->getMock(); |
||
| 185 | |||
| 186 | $twig |
||
| 187 | ->method('getRuntime') |
||
| 188 | ->willReturn($this->createMock(FormRenderer::class)); |
||
| 189 | |||
| 190 | $twig |
||
| 191 | ->method('render') |
||
| 192 | ->will($templatingRenderReturnCallback); |
||
| 193 | |||
| 194 | $exporter = new Exporter([new JsonWriter(sys_get_temp_dir().'/sonataadmin/export.json')]); |
||
| 195 | |||
| 196 | $adminExporter = new AdminExporter($exporter); |
||
| 197 | |||
| 198 | $this->auditManager = $this->createMock(AuditManagerInterface::class); |
||
| 199 | |||
| 200 | $this->adminObjectAclManipulator = $this->getMockBuilder(AdminObjectAclManipulator::class) |
||
| 201 | ->disableOriginalConstructor() |
||
| 202 | ->getMock(); |
||
| 203 | |||
| 204 | $this->csrfProvider = $this->getMockBuilder(CsrfTokenManagerInterface::class) |
||
| 205 | ->getMock(); |
||
| 206 | |||
| 207 | $this->csrfProvider |
||
| 208 | ->method('getToken') |
||
| 209 | ->willReturnCallback(static function (string $intention): CsrfToken { |
||
| 210 | return new CsrfToken($intention, sprintf('csrf-token-123_%s', $intention)); |
||
| 211 | }); |
||
| 212 | |||
| 213 | $this->csrfProvider |
||
| 214 | ->method('isTokenValid') |
||
| 215 | ->willReturnCallback(static function (CsrfToken $token): bool { |
||
| 216 | return $token->getValue() === sprintf('csrf-token-123_%s', $token->getId()); |
||
| 217 | }); |
||
| 218 | |||
| 219 | $this->logger = $this->createMock(LoggerInterface::class); |
||
| 220 | |||
| 221 | $requestStack = new RequestStack(); |
||
| 222 | $requestStack->push($this->request); |
||
| 223 | |||
| 224 | $this->kernel = $this->createMock(KernelInterface::class); |
||
| 225 | |||
| 226 | $this->container->set('sonata.admin.pool', $this->pool); |
||
| 227 | $this->container->set('request_stack', $requestStack); |
||
| 228 | $this->container->set('foo.admin', $this->admin); |
||
| 229 | $this->container->set('foo.admin.template_registry', $this->templateRegistry->reveal()); |
||
| 230 | $this->container->set('twig', $twig); |
||
| 231 | $this->container->set('session', $this->session); |
||
| 232 | $this->container->set('sonata.exporter.exporter', $exporter); |
||
| 233 | $this->container->set('sonata.admin.admin_exporter', $adminExporter); |
||
| 234 | $this->container->set('sonata.admin.audit.manager', $this->auditManager); |
||
| 235 | $this->container->set('sonata.admin.object.manipulator.acl.admin', $this->adminObjectAclManipulator); |
||
| 236 | $this->container->set('security.csrf.token_manager', $this->csrfProvider); |
||
| 237 | $this->container->set('logger', $this->logger); |
||
| 238 | $this->container->set('kernel', $this->kernel); |
||
| 239 | $this->container->set('translator', $this->translator); |
||
| 240 | $this->container->set('sonata.admin.breadcrumbs_builder', new BreadcrumbsBuilder([])); |
||
| 241 | |||
| 242 | $this->container->setParameter( |
||
| 243 | 'security.role_hierarchy.roles', |
||
| 244 | ['ROLE_SUPER_ADMIN' => ['ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN']] |
||
| 245 | ); |
||
| 246 | $this->container->setParameter('sonata.admin.security.acl_user_manager', null); |
||
| 247 | |||
| 248 | $this->templateRegistry->getTemplate('ajax')->willReturn('@SonataAdmin/ajax_layout.html.twig'); |
||
| 249 | $this->templateRegistry->getTemplate('layout')->willReturn('@SonataAdmin/standard_layout.html.twig'); |
||
| 250 | $this->templateRegistry->getTemplate('show')->willReturn('@SonataAdmin/CRUD/show.html.twig'); |
||
| 251 | $this->templateRegistry->getTemplate('show_compare')->willReturn('@SonataAdmin/CRUD/show_compare.html.twig'); |
||
| 252 | $this->templateRegistry->getTemplate('edit')->willReturn('@SonataAdmin/CRUD/edit.html.twig'); |
||
| 253 | $this->templateRegistry->getTemplate('dashboard')->willReturn('@SonataAdmin/Core/dashboard.html.twig'); |
||
| 254 | $this->templateRegistry->getTemplate('search')->willReturn('@SonataAdmin/Core/search.html.twig'); |
||
| 255 | $this->templateRegistry->getTemplate('list')->willReturn('@SonataAdmin/CRUD/list.html.twig'); |
||
| 256 | $this->templateRegistry->getTemplate('preview')->willReturn('@SonataAdmin/CRUD/preview.html.twig'); |
||
| 257 | $this->templateRegistry->getTemplate('history')->willReturn('@SonataAdmin/CRUD/history.html.twig'); |
||
| 258 | $this->templateRegistry->getTemplate('acl')->willReturn('@SonataAdmin/CRUD/acl.html.twig'); |
||
| 259 | $this->templateRegistry->getTemplate('delete')->willReturn('@SonataAdmin/CRUD/delete.html.twig'); |
||
| 260 | $this->templateRegistry->getTemplate('batch')->willReturn('@SonataAdmin/CRUD/list__batch.html.twig'); |
||
| 261 | $this->templateRegistry->getTemplate('batch_confirmation')->willReturn('@SonataAdmin/CRUD/batch_confirmation.html.twig'); |
||
| 262 | |||
| 263 | $this->admin |
||
| 264 | ->method('getIdParameter') |
||
| 265 | ->willReturn('id'); |
||
| 266 | |||
| 267 | $this->admin |
||
| 268 | ->method('getAccessMapping') |
||
| 269 | ->willReturn([]); |
||
| 270 | |||
| 271 | $this->admin |
||
| 272 | ->method('generateUrl') |
||
| 273 | ->willReturnCallback( |
||
| 274 | static function ($name, array $parameters = []) { |
||
| 275 | $result = $name; |
||
| 276 | if (!empty($parameters)) { |
||
| 277 | $result .= '?'.http_build_query($parameters); |
||
| 278 | } |
||
| 279 | |||
| 280 | return $result; |
||
| 281 | } |
||
| 282 | ); |
||
| 283 | |||
| 284 | $this->admin |
||
| 285 | ->method('generateObjectUrl') |
||
| 286 | ->willReturnCallback( |
||
| 287 | static function (string $name, $object, array $parameters = []): string { |
||
| 288 | $result = sprintf('%s_%s', \get_class($object), $name); |
||
| 289 | if (!empty($parameters)) { |
||
| 290 | $result .= '?'.http_build_query($parameters); |
||
| 291 | } |
||
| 292 | |||
| 293 | return $result; |
||
| 294 | } |
||
| 295 | ); |
||
| 296 | |||
| 297 | $this->admin |
||
| 298 | ->method('getCode') |
||
| 299 | ->willReturn('foo.admin'); |
||
| 300 | |||
| 301 | $this->controller = new CRUDController(); |
||
| 302 | $this->controller->setContainer($this->container); |
||
| 303 | |||
| 304 | // Make some methods public to test them |
||
| 305 | $testedMethods = [ |
||
| 306 | 'renderJson', |
||
| 307 | 'isXmlHttpRequest', |
||
| 308 | 'configure', |
||
| 309 | 'getBaseTemplate', |
||
| 310 | 'redirectTo', |
||
| 311 | 'addFlash', |
||
| 312 | ]; |
||
| 313 | foreach ($testedMethods as $testedMethod) { |
||
| 314 | $method = new \ReflectionMethod(CRUDController::class, $testedMethod); |
||
| 315 | $method->setAccessible(true); |
||
| 316 | $this->protectedTestedMethods[$testedMethod] = $method; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | public function testRenderJson1(): void |
||
| 321 | { |
||
| 322 | $data = ['example' => '123', 'foo' => 'bar']; |
||
| 323 | |||
| 324 | $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded'); |
||
| 325 | $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
||
| 326 | |||
| 327 | $this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
||
| 328 | $this->assertSame(json_encode($data), $response->getContent()); |
||
| 329 | } |
||
| 330 | |||
| 331 | public function testRenderJson2(): void |
||
| 332 | { |
||
| 333 | $data = ['example' => '123', 'foo' => 'bar']; |
||
| 334 | |||
| 335 | $this->request->headers->set('Content-Type', 'multipart/form-data'); |
||
| 336 | $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
||
| 337 | |||
| 338 | $this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
||
| 339 | $this->assertSame(json_encode($data), $response->getContent()); |
||
| 340 | } |
||
| 341 | |||
| 342 | public function testRenderJsonAjax(): void |
||
| 343 | { |
||
| 344 | $data = ['example' => '123', 'foo' => 'bar']; |
||
| 345 | |||
| 346 | $this->request->attributes->set('_xml_http_request', true); |
||
| 347 | $this->request->headers->set('Content-Type', 'multipart/form-data'); |
||
| 348 | $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
||
| 349 | |||
| 350 | $this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
||
| 351 | $this->assertSame(json_encode($data), $response->getContent()); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function testIsXmlHttpRequest(): void |
||
| 355 | { |
||
| 356 | $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
| 357 | |||
| 358 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 359 | |||
| 360 | $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
| 361 | |||
| 362 | $this->request->headers->remove('X-Requested-With'); |
||
| 363 | $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
| 364 | |||
| 365 | $this->request->attributes->set('_xml_http_request', true); |
||
| 366 | $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
||
| 367 | } |
||
| 368 | |||
| 369 | public function testConfigure(): void |
||
| 370 | { |
||
| 371 | $uniqueId = ''; |
||
| 372 | |||
| 373 | $this->admin->expects($this->once()) |
||
|
|
|||
| 374 | ->method('setUniqid') |
||
| 375 | ->willReturnCallback(static function (string $uniqid) use (&$uniqueId): void { |
||
| 376 | $uniqueId = $uniqid; |
||
| 377 | }); |
||
| 378 | |||
| 379 | $this->request->query->set('uniqid', '123456'); |
||
| 380 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
| 381 | |||
| 382 | $this->assertSame('123456', $uniqueId); |
||
| 383 | } |
||
| 384 | |||
| 385 | public function testConfigureChild(): void |
||
| 386 | { |
||
| 387 | $uniqueId = ''; |
||
| 388 | |||
| 389 | $this->admin->expects($this->once()) |
||
| 390 | ->method('setUniqid') |
||
| 391 | ->willReturnCallback(static function (string $uniqid) use (&$uniqueId): void { |
||
| 392 | $uniqueId = $uniqid; |
||
| 393 | }); |
||
| 394 | |||
| 395 | $this->admin->expects($this->once()) |
||
| 396 | ->method('isChild') |
||
| 397 | ->willReturn(true); |
||
| 398 | |||
| 399 | $adminParent = $this->getMockBuilder(AdminInterface::class) |
||
| 400 | ->disableOriginalConstructor() |
||
| 401 | ->getMock(); |
||
| 402 | $this->admin->expects($this->once()) |
||
| 403 | ->method('getParent') |
||
| 404 | ->willReturn($adminParent); |
||
| 405 | |||
| 406 | $this->request->query->set('uniqid', '123456'); |
||
| 407 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
| 408 | |||
| 409 | $this->assertSame('123456', $uniqueId); |
||
| 410 | } |
||
| 411 | |||
| 412 | public function testConfigureWithException(): void |
||
| 413 | { |
||
| 414 | $this->expectException(\RuntimeException::class); |
||
| 415 | $this->expectExceptionMessage( |
||
| 416 | 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`' |
||
| 417 | ); |
||
| 418 | |||
| 419 | $this->request->attributes->remove('_sonata_admin'); |
||
| 420 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
| 421 | } |
||
| 422 | |||
| 423 | public function testConfigureWithException2(): void |
||
| 424 | { |
||
| 425 | $this->pool->setAdminServiceIds(['nonexistent.admin']); |
||
| 426 | $this->request->attributes->set('_sonata_admin', 'nonexistent.admin'); |
||
| 427 | |||
| 428 | $this->expectException(\RuntimeException::class); |
||
| 429 | $this->expectExceptionMessage('Unable to find the admin class related to the current controller (Sonata\AdminBundle\Controller\CRUDController)'); |
||
| 430 | |||
| 431 | $this->protectedTestedMethods['configure']->invoke($this->controller); |
||
| 432 | } |
||
| 433 | |||
| 434 | public function testGetBaseTemplate(): void |
||
| 435 | { |
||
| 436 | $this->assertSame( |
||
| 437 | '@SonataAdmin/standard_layout.html.twig', |
||
| 438 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
| 439 | ); |
||
| 440 | |||
| 441 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 442 | $this->assertSame( |
||
| 443 | '@SonataAdmin/ajax_layout.html.twig', |
||
| 444 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
| 445 | ); |
||
| 446 | |||
| 447 | $this->request->headers->remove('X-Requested-With'); |
||
| 448 | $this->assertSame( |
||
| 449 | '@SonataAdmin/standard_layout.html.twig', |
||
| 450 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
| 451 | ); |
||
| 452 | |||
| 453 | $this->request->attributes->set('_xml_http_request', true); |
||
| 454 | $this->assertSame( |
||
| 455 | '@SonataAdmin/ajax_layout.html.twig', |
||
| 456 | $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
||
| 457 | ); |
||
| 458 | } |
||
| 459 | |||
| 460 | public function testRender(): void |
||
| 461 | { |
||
| 462 | $this->parameters = []; |
||
| 463 | $this->assertInstanceOf( |
||
| 464 | Response::class, |
||
| 465 | $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], null) |
||
| 466 | ); |
||
| 467 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 468 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 469 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 470 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
| 471 | } |
||
| 472 | |||
| 473 | public function testRenderWithResponse(): void |
||
| 474 | { |
||
| 475 | $this->parameters = []; |
||
| 476 | $response = new Response(); |
||
| 477 | $response->headers->set('X-foo', 'bar'); |
||
| 478 | $responseResult = $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], $response); |
||
| 479 | |||
| 480 | $this->assertSame($response, $responseResult); |
||
| 481 | $this->assertSame('bar', $responseResult->headers->get('X-foo')); |
||
| 482 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 483 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 484 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 485 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
| 486 | } |
||
| 487 | |||
| 488 | public function testRenderCustomParams(): void |
||
| 489 | { |
||
| 490 | $this->parameters = []; |
||
| 491 | $this->assertInstanceOf( |
||
| 492 | Response::class, |
||
| 493 | $this->controller->renderWithExtraParams( |
||
| 494 | '@FooAdmin/foo.html.twig', |
||
| 495 | ['foo' => 'bar'], |
||
| 496 | null |
||
| 497 | ) |
||
| 498 | ); |
||
| 499 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 500 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 501 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 502 | $this->assertSame('bar', $this->parameters['foo']); |
||
| 503 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
| 504 | } |
||
| 505 | |||
| 506 | public function testRenderAjax(): void |
||
| 507 | { |
||
| 508 | $this->parameters = []; |
||
| 509 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 510 | $this->assertInstanceOf( |
||
| 511 | Response::class, |
||
| 512 | $this->controller->renderWithExtraParams( |
||
| 513 | '@FooAdmin/foo.html.twig', |
||
| 514 | ['foo' => 'bar'], |
||
| 515 | null |
||
| 516 | ) |
||
| 517 | ); |
||
| 518 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 519 | $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']); |
||
| 520 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 521 | $this->assertSame('bar', $this->parameters['foo']); |
||
| 522 | $this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
||
| 523 | } |
||
| 524 | |||
| 525 | public function testListActionAccessDenied(): void |
||
| 526 | { |
||
| 527 | $this->expectException(AccessDeniedException::class); |
||
| 528 | |||
| 529 | $this->admin->expects($this->once()) |
||
| 530 | ->method('checkAccess') |
||
| 531 | ->with($this->equalTo('list')) |
||
| 532 | ->will($this->throwException(new AccessDeniedException())); |
||
| 533 | |||
| 534 | $this->controller->listAction($this->request); |
||
| 535 | } |
||
| 536 | |||
| 537 | public function testPreList(): void |
||
| 538 | { |
||
| 539 | $this->admin |
||
| 540 | ->method('hasRoute') |
||
| 541 | ->with($this->equalTo('list')) |
||
| 542 | ->willReturn(true); |
||
| 543 | |||
| 544 | $this->admin->expects($this->once()) |
||
| 545 | ->method('checkAccess') |
||
| 546 | ->with($this->equalTo('list')); |
||
| 547 | |||
| 548 | $controller = new PreCRUDController(); |
||
| 549 | $controller->setContainer($this->container); |
||
| 550 | |||
| 551 | $response = $controller->listAction($this->request); |
||
| 552 | $this->assertInstanceOf(Response::class, $response); |
||
| 553 | $this->assertSame('preList called', $response->getContent()); |
||
| 554 | } |
||
| 555 | |||
| 556 | public function testListAction(): void |
||
| 557 | { |
||
| 558 | $datagrid = $this->createMock(DatagridInterface::class); |
||
| 559 | |||
| 560 | $this->admin |
||
| 561 | ->method('hasRoute') |
||
| 562 | ->with($this->equalTo('list')) |
||
| 563 | ->willReturn(true); |
||
| 564 | |||
| 565 | $this->admin->expects($this->once()) |
||
| 566 | ->method('checkAccess') |
||
| 567 | ->with($this->equalTo('list')); |
||
| 568 | |||
| 569 | $form = $this->getMockBuilder(Form::class) |
||
| 570 | ->disableOriginalConstructor() |
||
| 571 | ->getMock(); |
||
| 572 | |||
| 573 | $form->expects($this->once()) |
||
| 574 | ->method('createView') |
||
| 575 | ->willReturn($this->createMock(FormView::class)); |
||
| 576 | |||
| 577 | $this->admin->expects($this->once()) |
||
| 578 | ->method('getDatagrid') |
||
| 579 | ->willReturn($datagrid); |
||
| 580 | |||
| 581 | $datagrid->expects($this->once()) |
||
| 582 | ->method('getForm') |
||
| 583 | ->willReturn($form); |
||
| 584 | |||
| 585 | $this->parameters = []; |
||
| 586 | $this->assertInstanceOf(Response::class, $this->controller->listAction($this->request)); |
||
| 587 | |||
| 588 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 589 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 590 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 591 | |||
| 592 | $this->assertSame('list', $this->parameters['action']); |
||
| 593 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 594 | $this->assertInstanceOf(DatagridInterface::class, $this->parameters['datagrid']); |
||
| 595 | $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']); |
||
| 596 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 597 | $this->assertSame('@SonataAdmin/CRUD/list.html.twig', $this->template); |
||
| 598 | } |
||
| 599 | |||
| 600 | public function testBatchActionDeleteAccessDenied(): void |
||
| 601 | { |
||
| 602 | $this->expectException(AccessDeniedException::class); |
||
| 603 | |||
| 604 | $this->admin->expects($this->once()) |
||
| 605 | ->method('checkAccess') |
||
| 606 | ->with($this->equalTo('batchDelete')) |
||
| 607 | ->will($this->throwException(new AccessDeniedException())); |
||
| 608 | |||
| 609 | $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
||
| 610 | } |
||
| 611 | |||
| 612 | public function testBatchActionDelete(): void |
||
| 613 | { |
||
| 614 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 615 | |||
| 616 | $this->admin->expects($this->once()) |
||
| 617 | ->method('checkAccess') |
||
| 618 | ->with($this->equalTo('batchDelete')); |
||
| 619 | |||
| 620 | $this->admin->expects($this->once()) |
||
| 621 | ->method('getModelManager') |
||
| 622 | ->willReturn($modelManager); |
||
| 623 | |||
| 624 | $this->admin->expects($this->once()) |
||
| 625 | ->method('getFilterParameters') |
||
| 626 | ->willReturn(['foo' => 'bar']); |
||
| 627 | |||
| 628 | $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
||
| 629 | |||
| 630 | $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
||
| 631 | |||
| 632 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
| 633 | $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
| 634 | $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
||
| 635 | } |
||
| 636 | |||
| 637 | public function testBatchActionDeleteWithModelManagerException(): void |
||
| 638 | { |
||
| 639 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 640 | $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete'); |
||
| 641 | |||
| 642 | $this->admin->expects($this->once()) |
||
| 643 | ->method('getModelManager') |
||
| 644 | ->willReturn($modelManager); |
||
| 645 | |||
| 646 | $this->admin->expects($this->once()) |
||
| 647 | ->method('getFilterParameters') |
||
| 648 | ->willReturn(['foo' => 'bar']); |
||
| 649 | |||
| 650 | $this->expectTranslate('flash_batch_delete_error', [], 'SonataAdminBundle'); |
||
| 651 | |||
| 652 | $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
||
| 653 | |||
| 654 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
| 655 | $this->assertSame(['flash_batch_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
||
| 656 | $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
||
| 657 | } |
||
| 658 | |||
| 659 | public function testBatchActionDeleteWithModelManagerExceptionInDebugMode(): void |
||
| 660 | { |
||
| 661 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 662 | $this->expectException(ModelManagerException::class); |
||
| 663 | |||
| 664 | $modelManager->expects($this->once()) |
||
| 665 | ->method('batchDelete') |
||
| 666 | ->willReturnCallback(static function (): void { |
||
| 667 | throw new ModelManagerException(); |
||
| 668 | }); |
||
| 669 | |||
| 670 | $this->admin->expects($this->once()) |
||
| 671 | ->method('getModelManager') |
||
| 672 | ->willReturn($modelManager); |
||
| 673 | |||
| 674 | $this->kernel->expects($this->once()) |
||
| 675 | ->method('isDebug') |
||
| 676 | ->willReturn(true); |
||
| 677 | |||
| 678 | $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
||
| 679 | } |
||
| 680 | |||
| 681 | public function testShowActionNotFoundException(): void |
||
| 682 | { |
||
| 683 | $this->expectException(NotFoundHttpException::class); |
||
| 684 | |||
| 685 | $this->admin->expects($this->once()) |
||
| 686 | ->method('getObject') |
||
| 687 | ->willReturn(null); |
||
| 688 | |||
| 689 | $this->controller->showAction($this->request); |
||
| 690 | } |
||
| 691 | |||
| 692 | public function testShowActionAccessDenied(): void |
||
| 693 | { |
||
| 694 | $this->expectException(AccessDeniedException::class); |
||
| 695 | |||
| 696 | $this->admin->expects($this->once()) |
||
| 697 | ->method('getObject') |
||
| 698 | ->willReturn(new \stdClass()); |
||
| 699 | |||
| 700 | $this->admin->expects($this->once()) |
||
| 701 | ->method('checkAccess') |
||
| 702 | ->with($this->equalTo('show')) |
||
| 703 | ->will($this->throwException(new AccessDeniedException())); |
||
| 704 | |||
| 705 | $this->controller->showAction($this->request); |
||
| 706 | } |
||
| 707 | |||
| 708 | public function testPreShow(): void |
||
| 709 | { |
||
| 710 | $object = new \stdClass(); |
||
| 711 | $object->foo = 123456; |
||
| 712 | |||
| 713 | $this->admin->expects($this->once()) |
||
| 714 | ->method('getObject') |
||
| 715 | ->willReturn($object); |
||
| 716 | |||
| 717 | $this->admin->expects($this->once()) |
||
| 718 | ->method('checkAccess') |
||
| 719 | ->with($this->equalTo('show')); |
||
| 720 | |||
| 721 | $controller = new PreCRUDController(); |
||
| 722 | $controller->setContainer($this->container); |
||
| 723 | |||
| 724 | $response = $controller->showAction($this->request); |
||
| 725 | $this->assertInstanceOf(Response::class, $response); |
||
| 726 | $this->assertSame('preShow called: 123456', $response->getContent()); |
||
| 727 | } |
||
| 728 | |||
| 729 | public function testShowAction(): void |
||
| 730 | { |
||
| 731 | $object = new \stdClass(); |
||
| 732 | |||
| 733 | $this->admin->expects($this->once()) |
||
| 734 | ->method('getObject') |
||
| 735 | ->willReturn($object); |
||
| 736 | |||
| 737 | $this->admin->expects($this->once()) |
||
| 738 | ->method('checkAccess') |
||
| 739 | ->with($this->equalTo('show')); |
||
| 740 | |||
| 741 | $show = new FieldDescriptionCollection(); |
||
| 742 | |||
| 743 | $this->admin->expects($this->once()) |
||
| 744 | ->method('getShow') |
||
| 745 | ->willReturn($show); |
||
| 746 | |||
| 747 | $this->assertInstanceOf(Response::class, $this->controller->showAction($this->request)); |
||
| 748 | |||
| 749 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 750 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 751 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 752 | |||
| 753 | $this->assertSame('show', $this->parameters['action']); |
||
| 754 | $this->assertInstanceOf(FieldDescriptionCollection::class, $this->parameters['elements']); |
||
| 755 | $this->assertSame($object, $this->parameters['object']); |
||
| 756 | |||
| 757 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 758 | $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template); |
||
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * @dataProvider getRedirectToTests |
||
| 763 | */ |
||
| 764 | public function testRedirectTo( |
||
| 765 | string $expected, |
||
| 766 | string $route, |
||
| 767 | array $queryParams, |
||
| 768 | array $requestParams, |
||
| 769 | bool $hasActiveSubclass |
||
| 770 | ): void { |
||
| 771 | $this->admin |
||
| 772 | ->method('hasActiveSubclass') |
||
| 773 | ->willReturn($hasActiveSubclass); |
||
| 774 | |||
| 775 | $object = new \stdClass(); |
||
| 776 | |||
| 777 | foreach ($queryParams as $key => $value) { |
||
| 778 | $this->request->query->set($key, $value); |
||
| 779 | } |
||
| 780 | |||
| 781 | foreach ($requestParams as $key => $value) { |
||
| 782 | $this->request->request->set($key, $value); |
||
| 783 | } |
||
| 784 | |||
| 785 | $this->admin |
||
| 786 | ->method('hasRoute') |
||
| 787 | ->with($this->equalTo($route)) |
||
| 788 | ->willReturn(true); |
||
| 789 | |||
| 790 | $this->admin |
||
| 791 | ->method('hasAccess') |
||
| 792 | ->with($this->equalTo($route)) |
||
| 793 | ->willReturn(true); |
||
| 794 | |||
| 795 | $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
||
| 796 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 797 | $this->assertSame($expected, $response->getTargetUrl()); |
||
| 798 | } |
||
| 799 | |||
| 800 | public function testRedirectToWithObject(): void |
||
| 801 | { |
||
| 802 | $this->admin |
||
| 803 | ->method('hasActiveSubclass') |
||
| 804 | ->willReturn(false); |
||
| 805 | |||
| 806 | $object = new \stdClass(); |
||
| 807 | |||
| 808 | $this->admin->expects($this->at(0)) |
||
| 809 | ->method('hasRoute') |
||
| 810 | ->with($this->equalTo('edit')) |
||
| 811 | ->willReturn(true); |
||
| 812 | |||
| 813 | $this->admin |
||
| 814 | ->method('hasAccess') |
||
| 815 | ->with($this->equalTo('edit'), $object) |
||
| 816 | ->willReturn(false); |
||
| 817 | |||
| 818 | $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
||
| 819 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 820 | $this->assertSame('list', $response->getTargetUrl()); |
||
| 821 | } |
||
| 822 | |||
| 823 | public function getRedirectToTests() |
||
| 824 | { |
||
| 825 | return [ |
||
| 826 | ['stdClass_edit', 'edit', [], [], false], |
||
| 827 | ['list', 'list', ['btn_update_and_list' => true], [], false], |
||
| 828 | ['list', 'list', ['btn_create_and_list' => true], [], false], |
||
| 829 | ['create', 'create', ['btn_create_and_create' => true], [], false], |
||
| 830 | ['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], [], true], |
||
| 831 | ['stdClass_edit?_tab=first_tab', 'edit', ['btn_update_and_edit' => true], ['_tab' => 'first_tab'], false], |
||
| 832 | ]; |
||
| 833 | } |
||
| 834 | |||
| 835 | public function testDeleteActionNotFoundException(): void |
||
| 836 | { |
||
| 837 | $this->expectException(NotFoundHttpException::class); |
||
| 838 | |||
| 839 | $this->admin->expects($this->once()) |
||
| 840 | ->method('getObject') |
||
| 841 | ->willReturn(null); |
||
| 842 | |||
| 843 | $this->controller->deleteAction($this->request); |
||
| 844 | } |
||
| 845 | |||
| 846 | public function testDeleteActionAccessDenied(): void |
||
| 847 | { |
||
| 848 | $this->expectException(AccessDeniedException::class); |
||
| 849 | |||
| 850 | $this->admin->expects($this->once()) |
||
| 851 | ->method('getObject') |
||
| 852 | ->willReturn(new \stdClass()); |
||
| 853 | |||
| 854 | $this->admin->expects($this->once()) |
||
| 855 | ->method('checkAccess') |
||
| 856 | ->with($this->equalTo('delete')) |
||
| 857 | ->will($this->throwException(new AccessDeniedException())); |
||
| 858 | |||
| 859 | $this->controller->deleteAction($this->request); |
||
| 860 | } |
||
| 861 | |||
| 862 | public function testPreDelete(): void |
||
| 863 | { |
||
| 864 | $object = new \stdClass(); |
||
| 865 | $object->foo = 123456; |
||
| 866 | |||
| 867 | $this->admin->expects($this->once()) |
||
| 868 | ->method('getObject') |
||
| 869 | ->willReturn($object); |
||
| 870 | |||
| 871 | $this->admin->expects($this->once()) |
||
| 872 | ->method('checkAccess') |
||
| 873 | ->with($this->equalTo('delete')); |
||
| 874 | |||
| 875 | $controller = new PreCRUDController(); |
||
| 876 | $controller->setContainer($this->container); |
||
| 877 | |||
| 878 | $response = $controller->deleteAction($this->request); |
||
| 879 | $this->assertInstanceOf(Response::class, $response); |
||
| 880 | $this->assertSame('preDelete called: 123456', $response->getContent()); |
||
| 881 | } |
||
| 882 | |||
| 883 | public function testDeleteAction(): void |
||
| 884 | { |
||
| 885 | $object = new \stdClass(); |
||
| 886 | |||
| 887 | $this->admin->expects($this->once()) |
||
| 888 | ->method('getObject') |
||
| 889 | ->willReturn($object); |
||
| 890 | |||
| 891 | $this->admin->expects($this->once()) |
||
| 892 | ->method('checkAccess') |
||
| 893 | ->with($this->equalTo('delete')); |
||
| 894 | |||
| 895 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request)); |
||
| 896 | |||
| 897 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 898 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 899 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 900 | |||
| 901 | $this->assertSame('delete', $this->parameters['action']); |
||
| 902 | $this->assertSame($object, $this->parameters['object']); |
||
| 903 | $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
||
| 904 | |||
| 905 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 906 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
| 907 | } |
||
| 908 | |||
| 909 | public function testDeleteActionNoCsrfToken(): void |
||
| 910 | { |
||
| 911 | $this->container->set('security.csrf.token_manager', null); |
||
| 912 | |||
| 913 | $object = new \stdClass(); |
||
| 914 | |||
| 915 | $this->admin->expects($this->once()) |
||
| 916 | ->method('getObject') |
||
| 917 | ->willReturn($object); |
||
| 918 | |||
| 919 | $this->admin->expects($this->once()) |
||
| 920 | ->method('checkAccess') |
||
| 921 | ->with($this->equalTo('delete')); |
||
| 922 | |||
| 923 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request)); |
||
| 924 | |||
| 925 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 926 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 927 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 928 | |||
| 929 | $this->assertSame('delete', $this->parameters['action']); |
||
| 930 | $this->assertSame($object, $this->parameters['object']); |
||
| 931 | $this->assertFalse($this->parameters['csrf_token']); |
||
| 932 | |||
| 933 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 934 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
| 935 | } |
||
| 936 | |||
| 937 | public function testDeleteActionAjaxSuccess1(): 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 | |||
| 949 | $this->request->setMethod(Request::METHOD_DELETE); |
||
| 950 | |||
| 951 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 952 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
| 953 | |||
| 954 | $response = $this->controller->deleteAction($this->request); |
||
| 955 | |||
| 956 | $this->assertInstanceOf(Response::class, $response); |
||
| 957 | $this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
||
| 958 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 959 | } |
||
| 960 | |||
| 961 | public function testDeleteActionAjaxSuccess2(): void |
||
| 962 | { |
||
| 963 | $object = new \stdClass(); |
||
| 964 | |||
| 965 | $this->admin->expects($this->once()) |
||
| 966 | ->method('getObject') |
||
| 967 | ->willReturn($object); |
||
| 968 | |||
| 969 | $this->admin->expects($this->once()) |
||
| 970 | ->method('checkAccess') |
||
| 971 | ->with($this->equalTo('delete')); |
||
| 972 | |||
| 973 | $this->request->setMethod(Request::METHOD_POST); |
||
| 974 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
| 975 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
| 976 | |||
| 977 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 978 | |||
| 979 | $response = $this->controller->deleteAction($this->request); |
||
| 980 | |||
| 981 | $this->assertInstanceOf(Response::class, $response); |
||
| 982 | $this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
||
| 983 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 984 | } |
||
| 985 | |||
| 986 | public function testDeleteActionAjaxError(): void |
||
| 987 | { |
||
| 988 | $object = new \stdClass(); |
||
| 989 | |||
| 990 | $this->admin->expects($this->once()) |
||
| 991 | ->method('getObject') |
||
| 992 | ->willReturn($object); |
||
| 993 | |||
| 994 | $this->admin->expects($this->once()) |
||
| 995 | ->method('checkAccess') |
||
| 996 | ->with($this->equalTo('delete')); |
||
| 997 | |||
| 998 | $this->admin |
||
| 999 | ->method('getClass') |
||
| 1000 | ->willReturn(\stdClass::class); |
||
| 1001 | |||
| 1002 | $this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
||
| 1003 | |||
| 1004 | $this->request->setMethod(Request::METHOD_DELETE); |
||
| 1005 | |||
| 1006 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
| 1007 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 1008 | |||
| 1009 | $response = $this->controller->deleteAction($this->request); |
||
| 1010 | |||
| 1011 | $this->assertInstanceOf(Response::class, $response); |
||
| 1012 | $this->assertSame(json_encode(['result' => 'error']), $response->getContent()); |
||
| 1013 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | public function testDeleteActionWithModelManagerExceptionInDebugMode(): void |
||
| 1017 | { |
||
| 1018 | $this->expectException(ModelManagerException::class); |
||
| 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 | |||
| 1030 | $this->admin->expects($this->once()) |
||
| 1031 | ->method('delete') |
||
| 1032 | ->willReturnCallback(static function (): void { |
||
| 1033 | throw new ModelManagerException(); |
||
| 1034 | }); |
||
| 1035 | |||
| 1036 | $this->kernel->expects($this->once()) |
||
| 1037 | ->method('isDebug') |
||
| 1038 | ->willReturn(true); |
||
| 1039 | |||
| 1040 | $this->request->setMethod(Request::METHOD_DELETE); |
||
| 1041 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
| 1042 | |||
| 1043 | $this->controller->deleteAction($this->request); |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * @dataProvider getToStringValues |
||
| 1048 | */ |
||
| 1049 | public function testDeleteActionSuccess1(string $expectedToStringValue, string $toStringValue): void |
||
| 1050 | { |
||
| 1051 | $object = new \stdClass(); |
||
| 1052 | |||
| 1053 | $this->admin->expects($this->once()) |
||
| 1054 | ->method('getObject') |
||
| 1055 | ->willReturn($object); |
||
| 1056 | |||
| 1057 | $this->admin->expects($this->once()) |
||
| 1058 | ->method('toString') |
||
| 1059 | ->with($this->equalTo($object)) |
||
| 1060 | ->willReturn($toStringValue); |
||
| 1061 | |||
| 1062 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1063 | |||
| 1064 | $this->admin->expects($this->once()) |
||
| 1065 | ->method('checkAccess') |
||
| 1066 | ->with($this->equalTo('delete')); |
||
| 1067 | |||
| 1068 | $this->request->setMethod(Request::METHOD_DELETE); |
||
| 1069 | |||
| 1070 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
| 1071 | |||
| 1072 | $response = $this->controller->deleteAction($this->request); |
||
| 1073 | |||
| 1074 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 1075 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
| 1076 | $this->assertSame('list', $response->getTargetUrl()); |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * @dataProvider getToStringValues |
||
| 1081 | */ |
||
| 1082 | public function testDeleteActionSuccess2(string $expectedToStringValue, string $toStringValue): void |
||
| 1083 | { |
||
| 1084 | $object = new \stdClass(); |
||
| 1085 | |||
| 1086 | $this->admin->expects($this->once()) |
||
| 1087 | ->method('getObject') |
||
| 1088 | ->willReturn($object); |
||
| 1089 | |||
| 1090 | $this->admin->expects($this->once()) |
||
| 1091 | ->method('checkAccess') |
||
| 1092 | ->with($this->equalTo('delete')); |
||
| 1093 | |||
| 1094 | $this->admin->expects($this->once()) |
||
| 1095 | ->method('toString') |
||
| 1096 | ->with($this->equalTo($object)) |
||
| 1097 | ->willReturn($toStringValue); |
||
| 1098 | |||
| 1099 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1100 | |||
| 1101 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1102 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
| 1103 | |||
| 1104 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
| 1105 | |||
| 1106 | $response = $this->controller->deleteAction($this->request); |
||
| 1107 | |||
| 1108 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 1109 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
| 1110 | $this->assertSame('list', $response->getTargetUrl()); |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * @dataProvider getToStringValues |
||
| 1115 | */ |
||
| 1116 | public function testDeleteActionSuccessNoCsrfTokenProvider(string $expectedToStringValue, string $toStringValue): void |
||
| 1117 | { |
||
| 1118 | $this->container->set('security.csrf.token_manager', null); |
||
| 1119 | |||
| 1120 | $object = new \stdClass(); |
||
| 1121 | |||
| 1122 | $this->admin->expects($this->once()) |
||
| 1123 | ->method('getObject') |
||
| 1124 | ->willReturn($object); |
||
| 1125 | |||
| 1126 | $this->admin->expects($this->once()) |
||
| 1127 | ->method('checkAccess') |
||
| 1128 | ->with($this->equalTo('delete')); |
||
| 1129 | |||
| 1130 | $this->admin->expects($this->once()) |
||
| 1131 | ->method('toString') |
||
| 1132 | ->with($this->equalTo($object)) |
||
| 1133 | ->willReturn($toStringValue); |
||
| 1134 | |||
| 1135 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1136 | |||
| 1137 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1138 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
| 1139 | |||
| 1140 | $response = $this->controller->deleteAction($this->request); |
||
| 1141 | |||
| 1142 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 1143 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
| 1144 | $this->assertSame('list', $response->getTargetUrl()); |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | public function testDeleteActionWrongRequestMethod(): void |
||
| 1148 | { |
||
| 1149 | $object = new \stdClass(); |
||
| 1150 | |||
| 1151 | $this->admin->expects($this->once()) |
||
| 1152 | ->method('getObject') |
||
| 1153 | ->willReturn($object); |
||
| 1154 | |||
| 1155 | $this->admin->expects($this->once()) |
||
| 1156 | ->method('checkAccess') |
||
| 1157 | ->with($this->equalTo('delete')); |
||
| 1158 | |||
| 1159 | //without POST request parameter "_method" should not be used as real REST method |
||
| 1160 | $this->request->query->set('_method', Request::METHOD_DELETE); |
||
| 1161 | |||
| 1162 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request)); |
||
| 1163 | |||
| 1164 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 1165 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 1166 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 1167 | |||
| 1168 | $this->assertSame('delete', $this->parameters['action']); |
||
| 1169 | $this->assertSame($object, $this->parameters['object']); |
||
| 1170 | $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
||
| 1171 | |||
| 1172 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 1173 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * @dataProvider getToStringValues |
||
| 1178 | */ |
||
| 1179 | public function testDeleteActionError(string $expectedToStringValue, string $toStringValue): void |
||
| 1180 | { |
||
| 1181 | $object = new \stdClass(); |
||
| 1182 | |||
| 1183 | $this->admin->expects($this->once()) |
||
| 1184 | ->method('getObject') |
||
| 1185 | ->willReturn($object); |
||
| 1186 | |||
| 1187 | $this->admin->expects($this->once()) |
||
| 1188 | ->method('checkAccess') |
||
| 1189 | ->with($this->equalTo('delete')); |
||
| 1190 | |||
| 1191 | $this->admin->expects($this->once()) |
||
| 1192 | ->method('toString') |
||
| 1193 | ->with($this->equalTo($object)) |
||
| 1194 | ->willReturn($toStringValue); |
||
| 1195 | |||
| 1196 | $this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1197 | |||
| 1198 | $this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
||
| 1199 | |||
| 1200 | $this->request->setMethod(Request::METHOD_DELETE); |
||
| 1201 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
| 1202 | |||
| 1203 | $response = $this->controller->deleteAction($this->request); |
||
| 1204 | |||
| 1205 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 1206 | $this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
||
| 1207 | $this->assertSame('list', $response->getTargetUrl()); |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | public function testDeleteActionInvalidCsrfToken(): 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 | |||
| 1222 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1223 | $this->request->request->set('_method', Request::METHOD_DELETE); |
||
| 1224 | $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
||
| 1225 | |||
| 1226 | try { |
||
| 1227 | $this->controller->deleteAction($this->request); |
||
| 1228 | } catch (HttpException $e) { |
||
| 1229 | $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
||
| 1230 | $this->assertSame(400, $e->getStatusCode()); |
||
| 1231 | } |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | public function testEditActionNotFoundException(): void |
||
| 1235 | { |
||
| 1236 | $this->expectException(NotFoundHttpException::class); |
||
| 1237 | |||
| 1238 | $this->admin->expects($this->once()) |
||
| 1239 | ->method('getObject') |
||
| 1240 | ->willReturn(null); |
||
| 1241 | |||
| 1242 | $this->controller->editAction($this->request); |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | public function testEditActionAccessDenied(): void |
||
| 1246 | { |
||
| 1247 | $this->expectException(AccessDeniedException::class); |
||
| 1248 | |||
| 1249 | $this->admin->expects($this->once()) |
||
| 1250 | ->method('getObject') |
||
| 1251 | ->willReturn(new \stdClass()); |
||
| 1252 | |||
| 1253 | $this->admin->expects($this->once()) |
||
| 1254 | ->method('checkAccess') |
||
| 1255 | ->with($this->equalTo('edit')) |
||
| 1256 | ->will($this->throwException(new AccessDeniedException())); |
||
| 1257 | |||
| 1258 | $this->controller->editAction($this->request); |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | public function testPreEdit(): void |
||
| 1262 | { |
||
| 1263 | $object = new \stdClass(); |
||
| 1264 | $object->foo = 123456; |
||
| 1265 | |||
| 1266 | $this->admin->expects($this->once()) |
||
| 1267 | ->method('getObject') |
||
| 1268 | ->willReturn($object); |
||
| 1269 | |||
| 1270 | $this->admin->expects($this->once()) |
||
| 1271 | ->method('checkAccess') |
||
| 1272 | ->with($this->equalTo('edit')); |
||
| 1273 | |||
| 1274 | $controller = new PreCRUDController(); |
||
| 1275 | $controller->setContainer($this->container); |
||
| 1276 | |||
| 1277 | $response = $controller->editAction($this->request); |
||
| 1278 | $this->assertInstanceOf(Response::class, $response); |
||
| 1279 | $this->assertSame('preEdit called: 123456', $response->getContent()); |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | public function testEditAction(): void |
||
| 1283 | { |
||
| 1284 | $object = new \stdClass(); |
||
| 1285 | |||
| 1286 | $this->admin->expects($this->once()) |
||
| 1287 | ->method('getObject') |
||
| 1288 | ->willReturn($object); |
||
| 1289 | |||
| 1290 | $this->admin->expects($this->once()) |
||
| 1291 | ->method('checkAccess') |
||
| 1292 | ->with($this->equalTo('edit')); |
||
| 1293 | |||
| 1294 | $form = $this->createMock(Form::class); |
||
| 1295 | |||
| 1296 | $this->admin->expects($this->once()) |
||
| 1297 | ->method('getForm') |
||
| 1298 | ->willReturn($form); |
||
| 1299 | |||
| 1300 | $formView = $this->createMock(FormView::class); |
||
| 1301 | |||
| 1302 | $form |
||
| 1303 | ->method('createView') |
||
| 1304 | ->willReturn($formView); |
||
| 1305 | |||
| 1306 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
| 1307 | |||
| 1308 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 1309 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 1310 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 1311 | |||
| 1312 | $this->assertSame('edit', $this->parameters['action']); |
||
| 1313 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 1314 | $this->assertSame($object, $this->parameters['object']); |
||
| 1315 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 1316 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
| 1317 | } |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * @dataProvider getToStringValues |
||
| 1321 | */ |
||
| 1322 | public function testEditActionSuccess(string $expectedToStringValue, string $toStringValue): void |
||
| 1323 | { |
||
| 1324 | $object = new \stdClass(); |
||
| 1325 | |||
| 1326 | $this->admin->expects($this->once()) |
||
| 1327 | ->method('getObject') |
||
| 1328 | ->willReturn($object); |
||
| 1329 | |||
| 1330 | $this->admin->expects($this->once()) |
||
| 1331 | ->method('update') |
||
| 1332 | ->willReturnArgument(0); |
||
| 1333 | |||
| 1334 | $this->admin->expects($this->once()) |
||
| 1335 | ->method('checkAccess') |
||
| 1336 | ->with($this->equalTo('edit')); |
||
| 1337 | |||
| 1338 | $this->admin->expects($this->once()) |
||
| 1339 | ->method('hasRoute') |
||
| 1340 | ->with($this->equalTo('edit')) |
||
| 1341 | ->willReturn(true); |
||
| 1342 | |||
| 1343 | $this->admin->expects($this->once()) |
||
| 1344 | ->method('hasAccess') |
||
| 1345 | ->with($this->equalTo('edit')) |
||
| 1346 | ->willReturn(true); |
||
| 1347 | |||
| 1348 | $form = $this->createMock(Form::class); |
||
| 1349 | |||
| 1350 | $form->expects($this->once()) |
||
| 1351 | ->method('getData') |
||
| 1352 | ->willReturn($object); |
||
| 1353 | |||
| 1354 | $this->admin->expects($this->once()) |
||
| 1355 | ->method('getForm') |
||
| 1356 | ->willReturn($form); |
||
| 1357 | |||
| 1358 | $form->expects($this->once()) |
||
| 1359 | ->method('isSubmitted') |
||
| 1360 | ->willReturn(true); |
||
| 1361 | |||
| 1362 | $form->expects($this->once()) |
||
| 1363 | ->method('isValid') |
||
| 1364 | ->willReturn(true); |
||
| 1365 | |||
| 1366 | $this->admin->expects($this->once()) |
||
| 1367 | ->method('toString') |
||
| 1368 | ->with($this->equalTo($object)) |
||
| 1369 | ->willReturn($toStringValue); |
||
| 1370 | |||
| 1371 | $this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1372 | |||
| 1373 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1374 | |||
| 1375 | $response = $this->controller->editAction($this->request); |
||
| 1376 | |||
| 1377 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 1378 | $this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
| 1379 | $this->assertSame('stdClass_edit', $response->getTargetUrl()); |
||
| 1380 | } |
||
| 1381 | |||
| 1382 | /** |
||
| 1383 | * @dataProvider getToStringValues |
||
| 1384 | */ |
||
| 1385 | public function testEditActionError(string $expectedToStringValue, string $toStringValue): void |
||
| 1386 | { |
||
| 1387 | $object = new \stdClass(); |
||
| 1388 | |||
| 1389 | $this->admin->expects($this->once()) |
||
| 1390 | ->method('getObject') |
||
| 1391 | ->willReturn($object); |
||
| 1392 | |||
| 1393 | $this->admin->expects($this->once()) |
||
| 1394 | ->method('checkAccess') |
||
| 1395 | ->with($this->equalTo('edit')); |
||
| 1396 | |||
| 1397 | $form = $this->createMock(Form::class); |
||
| 1398 | |||
| 1399 | $this->admin->expects($this->once()) |
||
| 1400 | ->method('getForm') |
||
| 1401 | ->willReturn($form); |
||
| 1402 | |||
| 1403 | $form->expects($this->once()) |
||
| 1404 | ->method('isSubmitted') |
||
| 1405 | ->willReturn(true); |
||
| 1406 | |||
| 1407 | $form->expects($this->once()) |
||
| 1408 | ->method('isValid') |
||
| 1409 | ->willReturn(false); |
||
| 1410 | |||
| 1411 | $this->admin->expects($this->once()) |
||
| 1412 | ->method('toString') |
||
| 1413 | ->with($this->equalTo($object)) |
||
| 1414 | ->willReturn($toStringValue); |
||
| 1415 | |||
| 1416 | $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1417 | |||
| 1418 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1419 | |||
| 1420 | $formView = $this->createMock(FormView::class); |
||
| 1421 | |||
| 1422 | $form |
||
| 1423 | ->method('createView') |
||
| 1424 | ->willReturn($formView); |
||
| 1425 | |||
| 1426 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
| 1427 | |||
| 1428 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 1429 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 1430 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 1431 | |||
| 1432 | $this->assertSame('edit', $this->parameters['action']); |
||
| 1433 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 1434 | $this->assertSame($object, $this->parameters['object']); |
||
| 1435 | |||
| 1436 | $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
||
| 1437 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
| 1438 | } |
||
| 1439 | |||
| 1440 | public function testEditActionAjaxSuccess(): void |
||
| 1441 | { |
||
| 1442 | $object = new \stdClass(); |
||
| 1443 | |||
| 1444 | $this->admin->expects($this->once()) |
||
| 1445 | ->method('getObject') |
||
| 1446 | ->willReturn($object); |
||
| 1447 | |||
| 1448 | $this->admin->expects($this->once()) |
||
| 1449 | ->method('update') |
||
| 1450 | ->willReturnArgument(0); |
||
| 1451 | |||
| 1452 | $this->admin->expects($this->once()) |
||
| 1453 | ->method('checkAccess') |
||
| 1454 | ->with($this->equalTo('edit')); |
||
| 1455 | |||
| 1456 | $form = $this->createMock(Form::class); |
||
| 1457 | |||
| 1458 | $this->admin->expects($this->once()) |
||
| 1459 | ->method('getForm') |
||
| 1460 | ->willReturn($form); |
||
| 1461 | |||
| 1462 | $form->expects($this->once()) |
||
| 1463 | ->method('isSubmitted') |
||
| 1464 | ->willReturn(true); |
||
| 1465 | |||
| 1466 | $form->expects($this->once()) |
||
| 1467 | ->method('isValid') |
||
| 1468 | ->willReturn(true); |
||
| 1469 | |||
| 1470 | $form->expects($this->once()) |
||
| 1471 | ->method('getData') |
||
| 1472 | ->willReturn($object); |
||
| 1473 | |||
| 1474 | $this->admin |
||
| 1475 | ->method('getNormalizedIdentifier') |
||
| 1476 | ->with($this->equalTo($object)) |
||
| 1477 | ->willReturn('foo_normalized'); |
||
| 1478 | |||
| 1479 | $this->admin->expects($this->once()) |
||
| 1480 | ->method('toString') |
||
| 1481 | ->willReturn('foo'); |
||
| 1482 | |||
| 1483 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1484 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 1485 | $this->request->headers->set('Accept', 'application/json'); |
||
| 1486 | |||
| 1487 | $response = $this->controller->editAction($this->request); |
||
| 1488 | |||
| 1489 | $this->assertInstanceOf(Response::class, $response); |
||
| 1490 | $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent()); |
||
| 1491 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | public function testEditActionAjaxError(): void |
||
| 1495 | { |
||
| 1496 | $object = new \stdClass(); |
||
| 1497 | |||
| 1498 | $this->admin->expects($this->once()) |
||
| 1499 | ->method('getObject') |
||
| 1500 | ->willReturn($object); |
||
| 1501 | |||
| 1502 | $this->admin->expects($this->once()) |
||
| 1503 | ->method('checkAccess') |
||
| 1504 | ->with($this->equalTo('edit')); |
||
| 1505 | |||
| 1506 | $form = $this->createMock(Form::class); |
||
| 1507 | |||
| 1508 | $this->admin->expects($this->once()) |
||
| 1509 | ->method('getForm') |
||
| 1510 | ->willReturn($form); |
||
| 1511 | |||
| 1512 | $form->expects($this->once()) |
||
| 1513 | ->method('isSubmitted') |
||
| 1514 | ->willReturn(true); |
||
| 1515 | |||
| 1516 | $form->expects($this->once()) |
||
| 1517 | ->method('isValid') |
||
| 1518 | ->willReturn(false); |
||
| 1519 | |||
| 1520 | $formError = $this->createMock(FormError::class); |
||
| 1521 | $formError->expects($this->atLeastOnce()) |
||
| 1522 | ->method('getMessage') |
||
| 1523 | ->willReturn('Form error message'); |
||
| 1524 | |||
| 1525 | $form->expects($this->once()) |
||
| 1526 | ->method('getErrors') |
||
| 1527 | ->with(true) |
||
| 1528 | ->willReturn([$formError]); |
||
| 1529 | |||
| 1530 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1531 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 1532 | $this->request->headers->set('Accept', 'application/json'); |
||
| 1533 | |||
| 1534 | $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->editAction($this->request)); |
||
| 1535 | $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent()); |
||
| 1536 | } |
||
| 1537 | |||
| 1538 | public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void |
||
| 1539 | { |
||
| 1540 | $object = new \stdClass(); |
||
| 1541 | |||
| 1542 | $this->admin->expects($this->once()) |
||
| 1543 | ->method('getObject') |
||
| 1544 | ->willReturn($object); |
||
| 1545 | |||
| 1546 | $this->admin->expects($this->once()) |
||
| 1547 | ->method('checkAccess') |
||
| 1548 | ->with($this->equalTo('edit')); |
||
| 1549 | |||
| 1550 | $form = $this->createMock(Form::class); |
||
| 1551 | |||
| 1552 | $this->admin->expects($this->once()) |
||
| 1553 | ->method('getForm') |
||
| 1554 | ->willReturn($form); |
||
| 1555 | |||
| 1556 | $form->expects($this->once()) |
||
| 1557 | ->method('isSubmitted') |
||
| 1558 | ->willReturn(true); |
||
| 1559 | |||
| 1560 | $form->expects($this->once()) |
||
| 1561 | ->method('isValid') |
||
| 1562 | ->willReturn(false); |
||
| 1563 | |||
| 1564 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1565 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 1566 | |||
| 1567 | $formView = $this->createMock(FormView::class); |
||
| 1568 | $form |
||
| 1569 | ->method('createView') |
||
| 1570 | ->willReturn($formView); |
||
| 1571 | |||
| 1572 | $this->assertInstanceOf(Response::class, $response = $this->controller->editAction($this->request)); |
||
| 1573 | $this->assertSame(Response::HTTP_NOT_ACCEPTABLE, $response->getStatusCode()); |
||
| 1574 | } |
||
| 1575 | |||
| 1576 | /** |
||
| 1577 | * @dataProvider getToStringValues |
||
| 1578 | */ |
||
| 1579 | public function testEditActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void |
||
| 1580 | { |
||
| 1581 | $object = new \stdClass(); |
||
| 1582 | |||
| 1583 | $this->admin->expects($this->once()) |
||
| 1584 | ->method('getObject') |
||
| 1585 | ->willReturn($object); |
||
| 1586 | |||
| 1587 | $this->admin->expects($this->once()) |
||
| 1588 | ->method('checkAccess') |
||
| 1589 | ->with($this->equalTo('edit')); |
||
| 1590 | |||
| 1591 | $this->admin |
||
| 1592 | ->method('getClass') |
||
| 1593 | ->willReturn(\stdClass::class); |
||
| 1594 | |||
| 1595 | $form = $this->createMock(Form::class); |
||
| 1596 | |||
| 1597 | $this->admin->expects($this->once()) |
||
| 1598 | ->method('getForm') |
||
| 1599 | ->willReturn($form); |
||
| 1600 | |||
| 1601 | $form->expects($this->once()) |
||
| 1602 | ->method('isValid') |
||
| 1603 | ->willReturn(true); |
||
| 1604 | |||
| 1605 | $form->expects($this->once()) |
||
| 1606 | ->method('getData') |
||
| 1607 | ->willReturn($object); |
||
| 1608 | |||
| 1609 | $this->admin->expects($this->once()) |
||
| 1610 | ->method('toString') |
||
| 1611 | ->with($this->equalTo($object)) |
||
| 1612 | ->willReturn($toStringValue); |
||
| 1613 | |||
| 1614 | $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1615 | |||
| 1616 | $form->expects($this->once()) |
||
| 1617 | ->method('isSubmitted') |
||
| 1618 | ->willReturn(true); |
||
| 1619 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1620 | |||
| 1621 | $formView = $this->createMock(FormView::class); |
||
| 1622 | |||
| 1623 | $form |
||
| 1624 | ->method('createView') |
||
| 1625 | ->willReturn($formView); |
||
| 1626 | |||
| 1627 | $this->assertLoggerLogsModelManagerException($this->admin, 'update'); |
||
| 1628 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
| 1629 | |||
| 1630 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 1631 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 1632 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 1633 | |||
| 1634 | $this->assertSame('edit', $this->parameters['action']); |
||
| 1635 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 1636 | $this->assertSame($object, $this->parameters['object']); |
||
| 1637 | |||
| 1638 | $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
||
| 1639 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
| 1640 | } |
||
| 1641 | |||
| 1642 | public function testEditActionWithPreview(): void |
||
| 1643 | { |
||
| 1644 | $object = new \stdClass(); |
||
| 1645 | |||
| 1646 | $this->admin->expects($this->once()) |
||
| 1647 | ->method('getObject') |
||
| 1648 | ->willReturn($object); |
||
| 1649 | |||
| 1650 | $this->admin->expects($this->once()) |
||
| 1651 | ->method('checkAccess') |
||
| 1652 | ->with($this->equalTo('edit')); |
||
| 1653 | |||
| 1654 | $form = $this->createMock(Form::class); |
||
| 1655 | |||
| 1656 | $this->admin->expects($this->once()) |
||
| 1657 | ->method('getForm') |
||
| 1658 | ->willReturn($form); |
||
| 1659 | |||
| 1660 | $this->admin->expects($this->once()) |
||
| 1661 | ->method('supportsPreviewMode') |
||
| 1662 | ->willReturn(true); |
||
| 1663 | |||
| 1664 | $formView = $this->createMock(FormView::class); |
||
| 1665 | |||
| 1666 | $form |
||
| 1667 | ->method('createView') |
||
| 1668 | ->willReturn($formView); |
||
| 1669 | |||
| 1670 | $form->expects($this->once()) |
||
| 1671 | ->method('isSubmitted') |
||
| 1672 | ->willReturn(true); |
||
| 1673 | |||
| 1674 | $form->expects($this->once()) |
||
| 1675 | ->method('isValid') |
||
| 1676 | ->willReturn(true); |
||
| 1677 | |||
| 1678 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1679 | $this->request->request->set('btn_preview', 'Preview'); |
||
| 1680 | |||
| 1681 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
| 1682 | |||
| 1683 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 1684 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 1685 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 1686 | |||
| 1687 | $this->assertSame('edit', $this->parameters['action']); |
||
| 1688 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 1689 | $this->assertSame($object, $this->parameters['object']); |
||
| 1690 | |||
| 1691 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 1692 | $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
||
| 1693 | } |
||
| 1694 | |||
| 1695 | public function testEditActionWithLockException(): void |
||
| 1696 | { |
||
| 1697 | $object = new \stdClass(); |
||
| 1698 | $class = \get_class($object); |
||
| 1699 | |||
| 1700 | $this->admin |
||
| 1701 | ->method('getObject') |
||
| 1702 | ->willReturn($object); |
||
| 1703 | |||
| 1704 | $this->admin |
||
| 1705 | ->method('checkAccess') |
||
| 1706 | ->with($this->equalTo('edit')); |
||
| 1707 | |||
| 1708 | $this->admin |
||
| 1709 | ->method('getClass') |
||
| 1710 | ->willReturn($class); |
||
| 1711 | |||
| 1712 | $form = $this->createMock(Form::class); |
||
| 1713 | |||
| 1714 | $form |
||
| 1715 | ->method('isValid') |
||
| 1716 | ->willReturn(true); |
||
| 1717 | |||
| 1718 | $form->expects($this->once()) |
||
| 1719 | ->method('getData') |
||
| 1720 | ->willReturn($object); |
||
| 1721 | |||
| 1722 | $this->admin |
||
| 1723 | ->method('getForm') |
||
| 1724 | ->willReturn($form); |
||
| 1725 | |||
| 1726 | $form |
||
| 1727 | ->method('isSubmitted') |
||
| 1728 | ->willReturn(true); |
||
| 1729 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1730 | |||
| 1731 | $this->admin |
||
| 1732 | ->method('update') |
||
| 1733 | ->will($this->throwException(new LockException())); |
||
| 1734 | |||
| 1735 | $this->admin |
||
| 1736 | ->method('toString') |
||
| 1737 | ->with($this->equalTo($object)) |
||
| 1738 | ->willReturn($class); |
||
| 1739 | |||
| 1740 | $formView = $this->createMock(FormView::class); |
||
| 1741 | |||
| 1742 | $form |
||
| 1743 | ->method('createView') |
||
| 1744 | ->willReturn($formView); |
||
| 1745 | |||
| 1746 | $this->expectTranslate('flash_lock_error', [ |
||
| 1747 | '%name%' => $class, |
||
| 1748 | '%link_start%' => '<a href="stdClass_edit">', |
||
| 1749 | '%link_end%' => '</a>', |
||
| 1750 | ], 'SonataAdminBundle'); |
||
| 1751 | |||
| 1752 | $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request)); |
||
| 1753 | } |
||
| 1754 | |||
| 1755 | public function testCreateActionAccessDenied(): void |
||
| 1766 | |||
| 1767 | public function testPreCreate(): void |
||
| 1791 | |||
| 1792 | public function testCreateAction(): void |
||
| 1833 | |||
| 1834 | /** |
||
| 1835 | * @dataProvider getToStringValues |
||
| 1836 | */ |
||
| 1837 | public function testCreateActionSuccess(string $expectedToStringValue, string $toStringValue): void |
||
| 1838 | { |
||
| 1839 | $object = new \stdClass(); |
||
| 1840 | |||
| 1841 | $this->admin->expects($this->exactly(2)) |
||
| 1842 | ->method('checkAccess') |
||
| 1843 | ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): void { |
||
| 1844 | if ('edit' === $name) { |
||
| 1845 | return; |
||
| 1846 | } |
||
| 1847 | |||
| 1848 | if ('create' !== $name) { |
||
| 1849 | throw new AccessDeniedException(); |
||
| 1850 | } |
||
| 1851 | |||
| 1852 | if (null === $objectIn) { |
||
| 1853 | return; |
||
| 1854 | } |
||
| 1855 | |||
| 1856 | if ($objectIn !== $object) { |
||
| 1857 | throw new AccessDeniedException(); |
||
| 1858 | } |
||
| 1859 | }); |
||
| 1860 | |||
| 1861 | $this->admin->expects($this->once()) |
||
| 1862 | ->method('hasRoute') |
||
| 1863 | ->with($this->equalTo('edit')) |
||
| 1864 | ->willReturn(true); |
||
| 1865 | |||
| 1866 | $this->admin->expects($this->once()) |
||
| 1867 | ->method('hasAccess') |
||
| 1868 | ->with($this->equalTo('edit')) |
||
| 1869 | ->willReturn(true); |
||
| 1870 | |||
| 1871 | $this->admin->expects($this->once()) |
||
| 1872 | ->method('getNewInstance') |
||
| 1873 | ->willReturn($object); |
||
| 1874 | |||
| 1875 | $this->admin->expects($this->once()) |
||
| 1876 | ->method('create') |
||
| 1877 | ->willReturnArgument(0); |
||
| 1878 | |||
| 1879 | $form = $this->createMock(Form::class); |
||
| 1880 | |||
| 1881 | $this->admin |
||
| 1882 | ->method('getClass') |
||
| 1883 | ->willReturn(\stdClass::class); |
||
| 1884 | |||
| 1885 | $this->admin->expects($this->once()) |
||
| 1886 | ->method('getForm') |
||
| 1887 | ->willReturn($form); |
||
| 1888 | |||
| 1889 | $form->expects($this->once()) |
||
| 1890 | ->method('isSubmitted') |
||
| 1891 | ->willReturn(true); |
||
| 1892 | |||
| 1893 | $form->expects($this->once()) |
||
| 1894 | ->method('isValid') |
||
| 1895 | ->willReturn(true); |
||
| 1896 | |||
| 1897 | $form->expects($this->once()) |
||
| 1898 | ->method('getData') |
||
| 1899 | ->willReturn($object); |
||
| 1900 | |||
| 1901 | $this->admin->expects($this->once()) |
||
| 1902 | ->method('toString') |
||
| 1903 | ->with($this->equalTo($object)) |
||
| 1904 | ->willReturn($toStringValue); |
||
| 1905 | |||
| 1906 | $this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 1907 | |||
| 1908 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1909 | |||
| 1910 | $response = $this->controller->createAction($this->request); |
||
| 1911 | |||
| 1912 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
| 1913 | $this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
| 1914 | $this->assertSame('stdClass_edit', $response->getTargetUrl()); |
||
| 1915 | } |
||
| 1916 | |||
| 1917 | public function testCreateActionAccessDenied2(): void |
||
| 1918 | { |
||
| 1919 | $this->expectException(AccessDeniedException::class); |
||
| 1920 | |||
| 1921 | $object = new \stdClass(); |
||
| 1922 | |||
| 1923 | $this->admin |
||
| 1924 | ->method('checkAccess') |
||
| 1925 | ->willReturnCallback(static function (string $name, $object = null): void { |
||
| 1926 | if ('create' !== $name) { |
||
| 1927 | throw new AccessDeniedException(); |
||
| 1928 | } |
||
| 1929 | if (null === $object) { |
||
| 1930 | return; |
||
| 1931 | } |
||
| 1932 | |||
| 1933 | throw new AccessDeniedException(); |
||
| 1934 | }); |
||
| 1935 | |||
| 1936 | $this->admin->expects($this->once()) |
||
| 1937 | ->method('getNewInstance') |
||
| 1938 | ->willReturn($object); |
||
| 1939 | |||
| 1940 | $form = $this->createMock(Form::class); |
||
| 1941 | |||
| 1942 | $this->admin |
||
| 1943 | ->method('getClass') |
||
| 1944 | ->willReturn(\stdClass::class); |
||
| 1945 | |||
| 1946 | $this->admin->expects($this->once()) |
||
| 1947 | ->method('getForm') |
||
| 1948 | ->willReturn($form); |
||
| 1949 | |||
| 1950 | $form->expects($this->once()) |
||
| 1951 | ->method('isSubmitted') |
||
| 1952 | ->willReturn(true); |
||
| 1953 | |||
| 1954 | $form->expects($this->once()) |
||
| 1955 | ->method('getData') |
||
| 1956 | ->willReturn($object); |
||
| 1957 | |||
| 1958 | $form->expects($this->once()) |
||
| 1959 | ->method('isValid') |
||
| 1960 | ->willReturn(true); |
||
| 1961 | |||
| 1962 | $this->request->setMethod(Request::METHOD_POST); |
||
| 1963 | |||
| 1964 | $this->controller->createAction($this->request); |
||
| 1965 | } |
||
| 1966 | |||
| 1967 | /** |
||
| 1968 | * @dataProvider getToStringValues |
||
| 1969 | */ |
||
| 1970 | public function testCreateActionError(string $expectedToStringValue, string $toStringValue): void |
||
| 1971 | { |
||
| 1972 | $this->admin->expects($this->once()) |
||
| 1973 | ->method('checkAccess') |
||
| 1974 | ->with($this->equalTo('create')); |
||
| 1975 | |||
| 1976 | $object = new \stdClass(); |
||
| 1977 | |||
| 1978 | $this->admin |
||
| 1979 | ->method('getClass') |
||
| 1980 | ->willReturn(\stdClass::class); |
||
| 1981 | |||
| 1982 | $this->admin->expects($this->once()) |
||
| 1983 | ->method('getNewInstance') |
||
| 1984 | ->willReturn($object); |
||
| 1985 | |||
| 1986 | $form = $this->createMock(Form::class); |
||
| 1987 | |||
| 1988 | $this->admin->expects($this->once()) |
||
| 1989 | ->method('getForm') |
||
| 1990 | ->willReturn($form); |
||
| 1991 | |||
| 1992 | $form->expects($this->once()) |
||
| 1993 | ->method('isSubmitted') |
||
| 1994 | ->willReturn(true); |
||
| 1995 | |||
| 1996 | $form->expects($this->once()) |
||
| 1997 | ->method('isValid') |
||
| 1998 | ->willReturn(false); |
||
| 1999 | |||
| 2000 | $this->admin->expects($this->once()) |
||
| 2001 | ->method('toString') |
||
| 2002 | ->with($this->equalTo($object)) |
||
| 2003 | ->willReturn($toStringValue); |
||
| 2004 | |||
| 2005 | $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 2006 | |||
| 2007 | $this->request->setMethod(Request::METHOD_POST); |
||
| 2008 | |||
| 2009 | $formView = $this->createMock(FormView::class); |
||
| 2010 | |||
| 2011 | $form |
||
| 2012 | ->method('createView') |
||
| 2013 | ->willReturn($formView); |
||
| 2014 | |||
| 2015 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
| 2016 | |||
| 2017 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 2018 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 2019 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 2020 | |||
| 2021 | $this->assertSame('create', $this->parameters['action']); |
||
| 2022 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 2023 | $this->assertSame($object, $this->parameters['object']); |
||
| 2024 | |||
| 2025 | $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
||
| 2026 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
| 2027 | } |
||
| 2028 | |||
| 2029 | /** |
||
| 2030 | * @dataProvider getToStringValues |
||
| 2031 | */ |
||
| 2032 | public function testCreateActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void |
||
| 2033 | { |
||
| 2034 | $this->admin->expects($this->exactly(2)) |
||
| 2035 | ->method('checkAccess') |
||
| 2036 | ->with($this->equalTo('create')); |
||
| 2037 | |||
| 2038 | $this->admin |
||
| 2039 | ->method('getClass') |
||
| 2040 | ->willReturn(\stdClass::class); |
||
| 2041 | |||
| 2042 | $object = new \stdClass(); |
||
| 2043 | |||
| 2044 | $this->admin->expects($this->once()) |
||
| 2045 | ->method('getNewInstance') |
||
| 2046 | ->willReturn($object); |
||
| 2047 | |||
| 2048 | $form = $this->createMock(Form::class); |
||
| 2049 | |||
| 2050 | $this->admin->expects($this->once()) |
||
| 2051 | ->method('getForm') |
||
| 2052 | ->willReturn($form); |
||
| 2053 | |||
| 2054 | $form->expects($this->once()) |
||
| 2055 | ->method('isValid') |
||
| 2056 | ->willReturn(true); |
||
| 2057 | |||
| 2058 | $this->admin->expects($this->once()) |
||
| 2059 | ->method('toString') |
||
| 2060 | ->with($this->equalTo($object)) |
||
| 2061 | ->willReturn($toStringValue); |
||
| 2062 | |||
| 2063 | $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
| 2064 | |||
| 2065 | $form->expects($this->once()) |
||
| 2066 | ->method('isSubmitted') |
||
| 2067 | ->willReturn(true); |
||
| 2068 | |||
| 2069 | $form->expects($this->once()) |
||
| 2070 | ->method('getData') |
||
| 2071 | ->willReturn($object); |
||
| 2072 | |||
| 2073 | $this->request->setMethod(Request::METHOD_POST); |
||
| 2074 | |||
| 2075 | $formView = $this->createMock(FormView::class); |
||
| 2076 | |||
| 2077 | $form |
||
| 2078 | ->method('createView') |
||
| 2079 | ->willReturn($formView); |
||
| 2080 | |||
| 2081 | $this->assertLoggerLogsModelManagerException($this->admin, 'create'); |
||
| 2082 | |||
| 2083 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
| 2084 | |||
| 2085 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 2086 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 2087 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 2088 | |||
| 2089 | $this->assertSame('create', $this->parameters['action']); |
||
| 2090 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 2091 | $this->assertSame($object, $this->parameters['object']); |
||
| 2092 | |||
| 2093 | $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
||
| 2094 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
| 2095 | } |
||
| 2096 | |||
| 2097 | public function testCreateActionAjaxSuccess(): void |
||
| 2098 | { |
||
| 2099 | $object = new \stdClass(); |
||
| 2100 | |||
| 2101 | $this->admin->expects($this->exactly(2)) |
||
| 2102 | ->method('checkAccess') |
||
| 2103 | ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): void { |
||
| 2104 | if ('create' !== $name) { |
||
| 2105 | throw new AccessDeniedException(); |
||
| 2106 | } |
||
| 2107 | |||
| 2108 | if (null === $objectIn) { |
||
| 2109 | return; |
||
| 2110 | } |
||
| 2111 | |||
| 2112 | if ($objectIn !== $object) { |
||
| 2113 | throw new AccessDeniedException(); |
||
| 2114 | } |
||
| 2115 | }); |
||
| 2116 | |||
| 2117 | $this->admin->expects($this->once()) |
||
| 2118 | ->method('getNewInstance') |
||
| 2119 | ->willReturn($object); |
||
| 2120 | |||
| 2121 | $this->admin->expects($this->once()) |
||
| 2122 | ->method('create') |
||
| 2123 | ->willReturnArgument(0); |
||
| 2124 | |||
| 2125 | $form = $this->createMock(Form::class); |
||
| 2126 | |||
| 2127 | $this->admin->expects($this->once()) |
||
| 2128 | ->method('getForm') |
||
| 2129 | ->willReturn($form); |
||
| 2130 | |||
| 2131 | $form->expects($this->once()) |
||
| 2132 | ->method('isSubmitted') |
||
| 2133 | ->willReturn(true); |
||
| 2134 | |||
| 2135 | $form->expects($this->once()) |
||
| 2136 | ->method('isValid') |
||
| 2137 | ->willReturn(true); |
||
| 2138 | |||
| 2139 | $form->expects($this->once()) |
||
| 2140 | ->method('getData') |
||
| 2141 | ->willReturn($object); |
||
| 2142 | |||
| 2143 | $this->admin |
||
| 2144 | ->method('getClass') |
||
| 2145 | ->willReturn(\stdClass::class); |
||
| 2146 | |||
| 2147 | $this->admin->expects($this->once()) |
||
| 2148 | ->method('getNormalizedIdentifier') |
||
| 2149 | ->with($this->equalTo($object)) |
||
| 2150 | ->willReturn('foo_normalized'); |
||
| 2151 | |||
| 2152 | $this->admin->expects($this->once()) |
||
| 2153 | ->method('toString') |
||
| 2154 | ->willReturn('foo'); |
||
| 2155 | |||
| 2156 | $this->request->setMethod(Request::METHOD_POST); |
||
| 2157 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 2158 | $this->request->headers->set('Accept', 'application/json'); |
||
| 2159 | |||
| 2160 | $response = $this->controller->createAction($this->request); |
||
| 2161 | |||
| 2162 | $this->assertInstanceOf(Response::class, $response); |
||
| 2163 | $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent()); |
||
| 2164 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 2165 | } |
||
| 2166 | |||
| 2167 | public function testCreateActionAjaxError(): void |
||
| 2168 | { |
||
| 2169 | $this->admin->expects($this->once()) |
||
| 2170 | ->method('checkAccess') |
||
| 2171 | ->with($this->equalTo('create')); |
||
| 2172 | |||
| 2173 | $object = new \stdClass(); |
||
| 2174 | |||
| 2175 | $this->admin->expects($this->once()) |
||
| 2176 | ->method('getNewInstance') |
||
| 2177 | ->willReturn($object); |
||
| 2178 | |||
| 2179 | $form = $this->createMock(Form::class); |
||
| 2180 | |||
| 2181 | $this->admin |
||
| 2182 | ->method('getClass') |
||
| 2183 | ->willReturn(\stdClass::class); |
||
| 2184 | |||
| 2185 | $this->admin->expects($this->once()) |
||
| 2186 | ->method('getForm') |
||
| 2187 | ->willReturn($form); |
||
| 2188 | |||
| 2189 | $form->expects($this->once()) |
||
| 2190 | ->method('isSubmitted') |
||
| 2191 | ->willReturn(true); |
||
| 2192 | |||
| 2193 | $form->expects($this->once()) |
||
| 2194 | ->method('isValid') |
||
| 2195 | ->willReturn(false); |
||
| 2196 | |||
| 2197 | $formError = $this->createMock(FormError::class); |
||
| 2198 | $formError->expects($this->atLeastOnce()) |
||
| 2199 | ->method('getMessage') |
||
| 2200 | ->willReturn('Form error message'); |
||
| 2201 | |||
| 2202 | $form->expects($this->once()) |
||
| 2203 | ->method('getErrors') |
||
| 2204 | ->with(true) |
||
| 2205 | ->willReturn([$formError]); |
||
| 2206 | |||
| 2207 | $this->request->setMethod(Request::METHOD_POST); |
||
| 2208 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 2209 | $this->request->headers->set('Accept', 'application/json'); |
||
| 2210 | |||
| 2211 | $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->createAction($this->request)); |
||
| 2212 | $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent()); |
||
| 2213 | } |
||
| 2214 | |||
| 2215 | public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void |
||
| 2216 | { |
||
| 2217 | $this->admin->expects($this->once()) |
||
| 2218 | ->method('checkAccess') |
||
| 2219 | ->with($this->equalTo('create')); |
||
| 2220 | |||
| 2221 | $object = new \stdClass(); |
||
| 2222 | |||
| 2223 | $this->admin->expects($this->once()) |
||
| 2224 | ->method('getNewInstance') |
||
| 2225 | ->willReturn($object); |
||
| 2226 | |||
| 2227 | $form = $this->createMock(Form::class); |
||
| 2228 | |||
| 2229 | $this->admin |
||
| 2230 | ->method('getClass') |
||
| 2231 | ->willReturn(\stdClass::class); |
||
| 2232 | |||
| 2233 | $this->admin->expects($this->once()) |
||
| 2234 | ->method('getForm') |
||
| 2235 | ->willReturn($form); |
||
| 2236 | |||
| 2237 | $form->expects($this->once()) |
||
| 2238 | ->method('isSubmitted') |
||
| 2239 | ->willReturn(true); |
||
| 2240 | |||
| 2241 | $form->expects($this->once()) |
||
| 2242 | ->method('isValid') |
||
| 2243 | ->willReturn(false); |
||
| 2244 | |||
| 2245 | $this->request->setMethod(Request::METHOD_POST); |
||
| 2246 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
| 2247 | |||
| 2248 | $formView = $this->createMock(FormView::class); |
||
| 2249 | $form |
||
| 2250 | ->method('createView') |
||
| 2251 | ->willReturn($formView); |
||
| 2252 | |||
| 2253 | $this->assertInstanceOf(Response::class, $response = $this->controller->createAction($this->request)); |
||
| 2254 | $this->assertSame(Response::HTTP_NOT_ACCEPTABLE, $response->getStatusCode()); |
||
| 2255 | } |
||
| 2256 | |||
| 2257 | public function testCreateActionWithPreview(): void |
||
| 2258 | { |
||
| 2259 | $this->admin->expects($this->once()) |
||
| 2260 | ->method('checkAccess') |
||
| 2261 | ->with($this->equalTo('create')); |
||
| 2262 | |||
| 2263 | $object = new \stdClass(); |
||
| 2264 | |||
| 2265 | $this->admin->expects($this->once()) |
||
| 2266 | ->method('getNewInstance') |
||
| 2267 | ->willReturn($object); |
||
| 2268 | |||
| 2269 | $form = $this->createMock(Form::class); |
||
| 2270 | |||
| 2271 | $this->admin |
||
| 2272 | ->method('getClass') |
||
| 2273 | ->willReturn(\stdClass::class); |
||
| 2274 | |||
| 2275 | $this->admin->expects($this->once()) |
||
| 2276 | ->method('getForm') |
||
| 2277 | ->willReturn($form); |
||
| 2278 | |||
| 2279 | $this->admin->expects($this->once()) |
||
| 2280 | ->method('supportsPreviewMode') |
||
| 2281 | ->willReturn(true); |
||
| 2282 | |||
| 2283 | $formView = $this->createMock(FormView::class); |
||
| 2284 | |||
| 2285 | $form |
||
| 2286 | ->method('createView') |
||
| 2287 | ->willReturn($formView); |
||
| 2288 | |||
| 2289 | $form->expects($this->once()) |
||
| 2290 | ->method('isSubmitted') |
||
| 2291 | ->willReturn(true); |
||
| 2292 | |||
| 2293 | $form->expects($this->once()) |
||
| 2294 | ->method('isValid') |
||
| 2295 | ->willReturn(true); |
||
| 2296 | |||
| 2297 | $this->request->setMethod(Request::METHOD_POST); |
||
| 2298 | $this->request->request->set('btn_preview', 'Preview'); |
||
| 2299 | |||
| 2300 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
| 2301 | |||
| 2302 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 2303 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
| 2304 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
| 2305 | |||
| 2306 | $this->assertSame('create', $this->parameters['action']); |
||
| 2307 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
| 2308 | $this->assertSame($object, $this->parameters['object']); |
||
| 2309 | |||
| 2310 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 2311 | $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
||
| 2312 | } |
||
| 2313 | |||
| 2314 | public function testExportActionAccessDenied(): void |
||
| 2315 | { |
||
| 2316 | $this->expectException(AccessDeniedException::class); |
||
| 2317 | |||
| 2318 | $this->admin->expects($this->once()) |
||
| 2319 | ->method('checkAccess') |
||
| 2320 | ->with($this->equalTo('export')) |
||
| 2321 | ->will($this->throwException(new AccessDeniedException())); |
||
| 2322 | |||
| 2323 | $this->controller->exportAction($this->request); |
||
| 2324 | } |
||
| 2325 | |||
| 2326 | public function testExportActionWrongFormat(): void |
||
| 2327 | { |
||
| 2328 | $this->expectException(\RuntimeException::class); |
||
| 2329 | $this->expectExceptionMessage( |
||
| 2330 | 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`' |
||
| 2331 | ); |
||
| 2332 | |||
| 2333 | $this->admin->expects($this->once()) |
||
| 2334 | ->method('checkAccess') |
||
| 2335 | ->with($this->equalTo('export')); |
||
| 2336 | |||
| 2337 | $this->admin->expects($this->once()) |
||
| 2338 | ->method('getExportFormats') |
||
| 2339 | ->willReturn(['json']); |
||
| 2340 | |||
| 2341 | $this->admin |
||
| 2342 | ->method('getClass') |
||
| 2343 | ->willReturn('Foo'); |
||
| 2344 | |||
| 2345 | $this->request->query->set('format', 'csv'); |
||
| 2346 | |||
| 2347 | $this->controller->exportAction($this->request); |
||
| 2348 | } |
||
| 2349 | |||
| 2350 | public function testExportAction(): void |
||
| 2351 | { |
||
| 2352 | $this->admin->expects($this->once()) |
||
| 2353 | ->method('checkAccess') |
||
| 2354 | ->with($this->equalTo('export')); |
||
| 2355 | |||
| 2356 | $this->admin->expects($this->once()) |
||
| 2357 | ->method('getExportFormats') |
||
| 2358 | ->willReturn(['json']); |
||
| 2359 | |||
| 2360 | $this->admin->expects($this->once()) |
||
| 2361 | ->method('getClass') |
||
| 2362 | ->willReturn(\stdClass::class); |
||
| 2363 | |||
| 2364 | $dataSourceIterator = $this->createMock(SourceIteratorInterface::class); |
||
| 2365 | |||
| 2366 | $this->admin->expects($this->once()) |
||
| 2367 | ->method('getDataSourceIterator') |
||
| 2368 | ->willReturn($dataSourceIterator); |
||
| 2369 | |||
| 2370 | $this->request->query->set('format', 'json'); |
||
| 2371 | |||
| 2372 | $response = $this->controller->exportAction($this->request); |
||
| 2373 | $this->assertInstanceOf(StreamedResponse::class, $response); |
||
| 2374 | $this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
||
| 2375 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
| 2376 | } |
||
| 2377 | |||
| 2378 | public function testHistoryActionAccessDenied(): void |
||
| 2393 | |||
| 2394 | public function testHistoryActionNotFoundException(): void |
||
| 2404 | |||
| 2405 | public function testHistoryActionNoReader(): void |
||
| 2406 | { |
||
| 2407 | $this->expectException(NotFoundHttpException::class); |
||
| 2408 | $this->expectExceptionMessage('unable to find the audit reader for class : Foo'); |
||
| 2409 | |||
| 2410 | $this->request->query->set('id', 123); |
||
| 2411 | |||
| 2412 | $this->admin->expects($this->once()) |
||
| 2413 | ->method('checkAccess') |
||
| 2414 | ->with($this->equalTo('history')); |
||
| 2415 | |||
| 2416 | $object = new \stdClass(); |
||
| 2417 | |||
| 2418 | $this->admin->expects($this->once()) |
||
| 2419 | ->method('getObject') |
||
| 2420 | ->willReturn($object); |
||
| 2421 | |||
| 2422 | $this->admin |
||
| 2423 | ->method('getClass') |
||
| 2424 | ->willReturn('Foo'); |
||
| 2425 | |||
| 2426 | $this->auditManager->expects($this->once()) |
||
| 2427 | ->method('hasReader') |
||
| 2428 | ->with($this->equalTo('Foo')) |
||
| 2429 | ->willReturn(false); |
||
| 2430 | |||
| 2431 | $this->controller->historyAction($this->request); |
||
| 2432 | } |
||
| 2433 | |||
| 2434 | public function testHistoryAction(): void |
||
| 2435 | { |
||
| 2436 | $this->request->query->set('id', 123); |
||
| 2437 | |||
| 2438 | $this->admin->expects($this->once()) |
||
| 2439 | ->method('checkAccess') |
||
| 2440 | ->with($this->equalTo('history')); |
||
| 2441 | |||
| 2442 | $object = new \stdClass(); |
||
| 2443 | |||
| 2444 | $this->admin->expects($this->once()) |
||
| 2445 | ->method('getObject') |
||
| 2446 | ->willReturn($object); |
||
| 2447 | |||
| 2448 | $this->admin |
||
| 2449 | ->method('getClass') |
||
| 2450 | ->willReturn('Foo'); |
||
| 2451 | |||
| 2452 | $this->auditManager->expects($this->once()) |
||
| 2453 | ->method('hasReader') |
||
| 2454 | ->with($this->equalTo('Foo')) |
||
| 2455 | ->willReturn(true); |
||
| 2456 | |||
| 2457 | $reader = $this->createMock(AuditReaderInterface::class); |
||
| 2458 | |||
| 2459 | $this->auditManager->expects($this->once()) |
||
| 2460 | ->method('getReader') |
||
| 2461 | ->with($this->equalTo('Foo')) |
||
| 2462 | ->willReturn($reader); |
||
| 2463 | |||
| 2464 | $reader->expects($this->once()) |
||
| 2465 | ->method('findRevisions') |
||
| 2466 | ->with($this->equalTo('Foo'), $this->equalTo(123)) |
||
| 2467 | ->willReturn([]); |
||
| 2468 | |||
| 2469 | $this->assertInstanceOf(Response::class, $this->controller->historyAction($this->request)); |
||
| 2470 | |||
| 2471 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
| 2482 | |||
| 2483 | public function testAclActionAclNotEnabled(): void |
||
| 2490 | |||
| 2491 | public function testAclActionNotFoundException(): void |
||
| 2505 | |||
| 2506 | public function testAclActionAccessDenied(): void |
||
| 2527 | |||
| 2528 | public function testAclAction(): void |
||
| 2607 | |||
| 2608 | public function testAclActionInvalidUpdate(): void |
||
| 2694 | |||
| 2695 | public function testAclActionSuccessfulUpdate(): void |
||
| 2773 | |||
| 2774 | public function testHistoryViewRevisionActionAccessDenied(): void |
||
| 2789 | |||
| 2790 | public function testHistoryViewRevisionActionNotFoundException(): void |
||
| 2803 | |||
| 2804 | public function testHistoryViewRevisionActionNoReader(): void |
||
| 2832 | |||
| 2833 | public function testHistoryViewRevisionActionNotFoundRevision(): void |
||
| 2875 | |||
| 2876 | public function testHistoryViewRevisionAction(): void |
||
| 2936 | |||
| 2937 | public function testHistoryCompareRevisionsActionAccessDenied(): void |
||
| 2948 | |||
| 2949 | public function testHistoryCompareRevisionsActionNotFoundException(): void |
||
| 2966 | |||
| 2967 | public function testHistoryCompareRevisionsActionNoReader(): void |
||
| 2995 | |||
| 2996 | public function testHistoryCompareRevisionsActionNotFoundBaseRevision(): void |
||
| 3039 | |||
| 3040 | public function testHistoryCompareRevisionsActionNotFoundCompareRevision(): void |
||
| 3091 | |||
| 3092 | public function testHistoryCompareRevisionsActionAction(): void |
||
| 3161 | |||
| 3162 | public function testBatchActionWrongMethod(): void |
||
| 3169 | |||
| 3170 | public function testBatchActionActionNotDefined(): void |
||
| 3187 | |||
| 3188 | public function testBatchActionActionInvalidCsrfToken(): void |
||
| 3201 | |||
| 3202 | public function testBatchActionWithoutConfirmation(): void |
||
| 3252 | |||
| 3253 | public function testBatchActionWithoutConfirmation2(): void |
||
| 3304 | |||
| 3305 | public function testBatchActionWithConfirmation(): void |
||
| 3353 | |||
| 3354 | public function testBatchActionNonRelevantAction(): void |
||
| 3384 | |||
| 3385 | public function testBatchActionWithCustomConfirmationTemplate(): void |
||
| 3419 | |||
| 3420 | public function testBatchActionNonRelevantAction2(): void |
||
| 3450 | |||
| 3451 | public function testBatchActionNoItems(): void |
||
| 3478 | |||
| 3479 | public function testBatchActionNoItemsEmptyQuery(): void |
||
| 3522 | |||
| 3523 | public function testBatchActionWithRequesData(): void |
||
| 3575 | |||
| 3576 | public function getCsrfProvider() |
||
| 3580 | |||
| 3581 | public function getToStringValues() |
||
| 3590 | |||
| 3591 | private function assertLoggerLogsModelManagerException($subject, string $method): void |
||
| 3612 | |||
| 3613 | private function expectTranslate( |
||
| 3624 | } |
||
| 3625 |
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.