| Total Complexity | 55 |
| Total Lines | 2628 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AdminTest 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.
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 AdminTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 92 | class AdminTest extends TestCase |
||
| 93 | { |
||
| 94 | protected $cacheTempFolder; |
||
| 95 | |||
| 96 | protected function setUp(): void |
||
| 97 | { |
||
| 98 | $this->cacheTempFolder = sprintf('%s/sonata_test_route', sys_get_temp_dir()); |
||
| 99 | $filesystem = new Filesystem(); |
||
| 100 | $filesystem->remove($this->cacheTempFolder); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct |
||
| 105 | */ |
||
| 106 | public function testConstructor(): void |
||
| 107 | { |
||
| 108 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
| 109 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 110 | |||
| 111 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 112 | $this->assertInstanceOf(AbstractAdmin::class, $admin); |
||
| 113 | $this->assertSame($class, $admin->getClass()); |
||
| 114 | $this->assertSame($baseControllerName, $admin->getBaseControllerName()); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testGetClass(): void |
||
| 118 | { |
||
| 119 | $class = Post::class; |
||
| 120 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 121 | |||
| 122 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 123 | |||
| 124 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
| 125 | |||
| 126 | $admin->setSubject(new BlogPost()); |
||
| 127 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 128 | |||
| 129 | $admin->setSubClasses(['foo']); |
||
| 130 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 131 | |||
| 132 | $admin->setSubject(null); |
||
| 133 | $admin->setSubClasses([]); |
||
| 134 | $this->assertSame($class, $admin->getClass()); |
||
| 135 | |||
| 136 | $admin->setSubClasses(['foo' => 'bar']); |
||
| 137 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
| 138 | $this->assertSame('bar', $admin->getClass()); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function testGetClassException(): void |
||
| 142 | { |
||
| 143 | $this->expectException(\RuntimeException::class); |
||
| 144 | $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass'); |
||
| 145 | |||
| 146 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
| 147 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 148 | |||
| 149 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 150 | $admin->setParentFieldDescription(new FieldDescription()); |
||
| 151 | $admin->setSubClasses(['foo' => 'bar']); |
||
| 152 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
| 153 | $admin->getClass(); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function testCheckAccessThrowsExceptionOnMadeUpAction(): void |
||
| 157 | { |
||
| 158 | $admin = new PostAdmin( |
||
| 159 | 'sonata.post.admin.post', |
||
| 160 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 161 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 162 | ); |
||
| 163 | $this->expectException( |
||
| 164 | \InvalidArgumentException::class |
||
| 165 | ); |
||
| 166 | $this->expectExceptionMessage( |
||
| 167 | 'Action "made-up" could not be found' |
||
| 168 | ); |
||
| 169 | $admin->checkAccess('made-up'); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function testCheckAccessThrowsAccessDeniedException(): void |
||
| 173 | { |
||
| 174 | $admin = new PostAdmin( |
||
| 175 | 'sonata.post.admin.post', |
||
| 176 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 177 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 178 | ); |
||
| 179 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 180 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 181 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
| 182 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 183 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 184 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 185 | ); |
||
| 186 | $admin->addExtension($customExtension->reveal()); |
||
| 187 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 188 | $this->expectException( |
||
| 189 | AccessDeniedException::class |
||
| 190 | ); |
||
| 191 | $this->expectExceptionMessage( |
||
| 192 | 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE' |
||
| 193 | ); |
||
| 194 | $admin->checkAccess('custom_action'); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function testHasAccessOnMadeUpAction(): void |
||
| 198 | { |
||
| 199 | $admin = new PostAdmin( |
||
| 200 | 'sonata.post.admin.post', |
||
| 201 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 202 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 203 | ); |
||
| 204 | |||
| 205 | $this->assertFalse($admin->hasAccess('made-up')); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function testHasAccess(): void |
||
| 209 | { |
||
| 210 | $admin = new PostAdmin( |
||
| 211 | 'sonata.post.admin.post', |
||
| 212 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 213 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 214 | ); |
||
| 215 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 216 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 217 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
| 218 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 219 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 220 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 221 | ); |
||
| 222 | $admin->addExtension($customExtension->reveal()); |
||
| 223 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 224 | |||
| 225 | $this->assertFalse($admin->hasAccess('custom_action')); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function testHasAccessAllowsAccess(): void |
||
| 229 | { |
||
| 230 | $admin = new PostAdmin( |
||
| 231 | 'sonata.post.admin.post', |
||
| 232 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 233 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 234 | ); |
||
| 235 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 236 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 237 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 238 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 239 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 240 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 241 | ); |
||
| 242 | $admin->addExtension($customExtension->reveal()); |
||
| 243 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 244 | |||
| 245 | $this->assertTrue($admin->hasAccess('custom_action')); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function testHasAccessAllowsAccessEditAction(): void |
||
| 249 | { |
||
| 250 | $admin = new PostAdmin( |
||
| 251 | 'sonata.post.admin.post', |
||
| 252 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 253 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 254 | ); |
||
| 255 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 256 | $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true); |
||
| 257 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 258 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 259 | ['edit_action' => ['EDIT_ROLE']] |
||
| 260 | ); |
||
| 261 | $admin->addExtension($customExtension->reveal()); |
||
| 262 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 263 | |||
| 264 | $this->assertTrue($admin->hasAccess('edit_action')); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild |
||
| 269 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild |
||
| 270 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild |
||
| 271 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild |
||
| 272 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren |
||
| 273 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren |
||
| 274 | */ |
||
| 275 | public function testChildren(): void |
||
| 276 | { |
||
| 277 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 278 | $this->assertFalse($postAdmin->hasChildren()); |
||
| 279 | $this->assertFalse($postAdmin->hasChild('comment')); |
||
| 280 | |||
| 281 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 282 | $postAdmin->addChild($commentAdmin, 'post'); |
||
| 283 | |||
| 284 | $this->assertTrue($postAdmin->hasChildren()); |
||
| 285 | $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment')); |
||
| 286 | |||
| 287 | $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode()); |
||
| 288 | $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute()); |
||
| 289 | $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent()); |
||
| 290 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
| 291 | |||
| 292 | $this->assertFalse($postAdmin->isChild()); |
||
| 293 | $this->assertTrue($commentAdmin->isChild()); |
||
| 294 | |||
| 295 | $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren()); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure |
||
| 300 | */ |
||
| 301 | public function testConfigure(): void |
||
| 302 | { |
||
| 303 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 304 | $this->assertNotNull($admin->getUniqid()); |
||
| 305 | |||
| 306 | $admin->initialize(); |
||
| 307 | $this->assertNotNull($admin->getUniqid()); |
||
| 308 | $this->assertSame('Post', $admin->getClassnameLabel()); |
||
| 309 | |||
| 310 | $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 311 | $admin->setClassnameLabel('postcomment'); |
||
| 312 | |||
| 313 | $admin->initialize(); |
||
| 314 | $this->assertSame('postcomment', $admin->getClassnameLabel()); |
||
| 315 | } |
||
| 316 | |||
| 317 | public function testConfigureWithValidParentAssociationMapping(): void |
||
| 318 | { |
||
| 319 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 320 | $admin->setParentAssociationMapping('Category'); |
||
| 321 | |||
| 322 | $admin->initialize(); |
||
| 323 | $this->assertSame('Category', $admin->getParentAssociationMapping()); |
||
| 324 | } |
||
| 325 | |||
| 326 | public function provideGetBaseRoutePattern() |
||
| 327 | { |
||
| 328 | return [ |
||
| 329 | [ |
||
| 330 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 331 | '/sonata/news/post', |
||
| 332 | ], |
||
| 333 | [ |
||
| 334 | 'Application\Sonata\NewsBundle\Document\Post', |
||
| 335 | '/sonata/news/post', |
||
| 336 | ], |
||
| 337 | [ |
||
| 338 | 'MyApplication\MyBundle\Entity\Post', |
||
| 339 | '/myapplication/my/post', |
||
| 340 | ], |
||
| 341 | [ |
||
| 342 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
| 343 | '/myapplication/my/post-category', |
||
| 344 | ], |
||
| 345 | [ |
||
| 346 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
| 347 | '/myapplication/my/product-category', |
||
| 348 | ], |
||
| 349 | [ |
||
| 350 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
| 351 | '/myapplication/my/other-product-category', |
||
| 352 | ], |
||
| 353 | [ |
||
| 354 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
| 355 | '/cmf/foo/menu', |
||
| 356 | ], |
||
| 357 | [ |
||
| 358 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
| 359 | '/cmf/foo/menu', |
||
| 360 | ], |
||
| 361 | [ |
||
| 362 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
| 363 | '/symfony/barbar/menu', |
||
| 364 | ], |
||
| 365 | [ |
||
| 366 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
| 367 | '/symfony/barbar/menu-item', |
||
| 368 | ], |
||
| 369 | [ |
||
| 370 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
| 371 | '/cmf/foo/menu', |
||
| 372 | ], |
||
| 373 | [ |
||
| 374 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
| 375 | '/cmf/foo/menu', |
||
| 376 | ], |
||
| 377 | [ |
||
| 378 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
| 379 | '/cmf/foo/menu', |
||
| 380 | ], |
||
| 381 | [ |
||
| 382 | 'AppBundle\Entity\User', |
||
| 383 | '/app/user', |
||
| 384 | ], |
||
| 385 | [ |
||
| 386 | 'App\Entity\User', |
||
| 387 | '/app/user', |
||
| 388 | ], |
||
| 389 | ]; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @dataProvider provideGetBaseRoutePattern |
||
| 394 | */ |
||
| 395 | public function testGetBaseRoutePattern(string $objFqn, string $expected): void |
||
| 396 | { |
||
| 397 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 398 | $this->assertSame($expected, $admin->getBaseRoutePattern()); |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @dataProvider provideGetBaseRoutePattern |
||
| 403 | */ |
||
| 404 | public function testGetBaseRoutePatternWithChildAdmin(string $objFqn, string $expected): void |
||
| 405 | { |
||
| 406 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 407 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 408 | $commentAdmin->setParent($postAdmin); |
||
| 409 | |||
| 410 | $this->assertSame(sprintf('%s/{id}/comment', $expected), $commentAdmin->getBaseRoutePattern()); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @dataProvider provideGetBaseRoutePattern |
||
| 415 | */ |
||
| 416 | public function testGetBaseRoutePatternWithTwoNestedChildAdmin(string $objFqn, string $expected): void |
||
| 417 | { |
||
| 418 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 419 | $commentAdmin = new CommentAdmin( |
||
| 420 | 'sonata.post.admin.comment', |
||
| 421 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 422 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 423 | ); |
||
| 424 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 425 | 'sonata.post.admin.comment_vote', |
||
| 426 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 427 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 428 | ); |
||
| 429 | $commentAdmin->setParent($postAdmin); |
||
| 430 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 431 | |||
| 432 | $this->assertSame(sprintf('%s/{id}/comment/{childId}/commentvote', $expected), $commentVoteAdmin->getBaseRoutePattern()); |
||
| 433 | } |
||
| 434 | |||
| 435 | public function testGetBaseRoutePatternWithSpecifedPattern(): void |
||
| 436 | { |
||
| 437 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostWithCustomRouteAdminController'); |
||
| 438 | |||
| 439 | $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern()); |
||
| 440 | } |
||
| 441 | |||
| 442 | public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void |
||
| 443 | { |
||
| 444 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 445 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'); |
||
| 446 | $commentAdmin->setParent($postAdmin); |
||
| 447 | |||
| 448 | $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern()); |
||
| 449 | } |
||
| 450 | |||
| 451 | public function testGetBaseRoutePatternWithUnreconizedClassname(): void |
||
| 452 | { |
||
| 453 | $this->expectException(\RuntimeException::class); |
||
| 454 | |||
| 455 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 456 | $admin->getBaseRoutePattern(); |
||
| 457 | } |
||
| 458 | |||
| 459 | public function provideGetBaseRouteName() |
||
| 460 | { |
||
| 461 | return [ |
||
| 462 | [ |
||
| 463 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 464 | 'admin_sonata_news_post', |
||
| 465 | ], |
||
| 466 | [ |
||
| 467 | 'Application\Sonata\NewsBundle\Document\Post', |
||
| 468 | 'admin_sonata_news_post', |
||
| 469 | ], |
||
| 470 | [ |
||
| 471 | 'MyApplication\MyBundle\Entity\Post', |
||
| 472 | 'admin_myapplication_my_post', |
||
| 473 | ], |
||
| 474 | [ |
||
| 475 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
| 476 | 'admin_myapplication_my_post_category', |
||
| 477 | ], |
||
| 478 | [ |
||
| 479 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
| 480 | 'admin_myapplication_my_product_category', |
||
| 481 | ], |
||
| 482 | [ |
||
| 483 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
| 484 | 'admin_myapplication_my_other_product_category', |
||
| 485 | ], |
||
| 486 | [ |
||
| 487 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
| 488 | 'admin_cmf_foo_menu', |
||
| 489 | ], |
||
| 490 | [ |
||
| 491 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
| 492 | 'admin_cmf_foo_menu', |
||
| 493 | ], |
||
| 494 | [ |
||
| 495 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
| 496 | 'admin_symfony_barbar_menu', |
||
| 497 | ], |
||
| 498 | [ |
||
| 499 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
| 500 | 'admin_symfony_barbar_menu_item', |
||
| 501 | ], |
||
| 502 | [ |
||
| 503 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
| 504 | 'admin_cmf_foo_menu', |
||
| 505 | ], |
||
| 506 | [ |
||
| 507 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
| 508 | 'admin_cmf_foo_menu', |
||
| 509 | ], |
||
| 510 | [ |
||
| 511 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
| 512 | 'admin_cmf_foo_menu', |
||
| 513 | ], |
||
| 514 | [ |
||
| 515 | 'AppBundle\Entity\User', |
||
| 516 | 'admin_app_user', |
||
| 517 | ], |
||
| 518 | [ |
||
| 519 | 'App\Entity\User', |
||
| 520 | 'admin_app_user', |
||
| 521 | ], |
||
| 522 | ]; |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @dataProvider provideGetBaseRouteName |
||
| 527 | */ |
||
| 528 | public function testGetBaseRouteName(string $objFqn, string $expected): void |
||
| 529 | { |
||
| 530 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 531 | |||
| 532 | $this->assertSame($expected, $admin->getBaseRouteName()); |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @group legacy |
||
| 537 | * @expectedDeprecation Calling "addChild" without second argument is deprecated since sonata-project/admin-bundle 3.35 and will not be allowed in 4.0. |
||
| 538 | * @dataProvider provideGetBaseRouteName |
||
| 539 | */ |
||
| 540 | public function testGetBaseRouteNameWithChildAdmin(string $objFqn, string $expected): void |
||
| 541 | { |
||
| 542 | $routeGenerator = new DefaultRouteGenerator( |
||
| 543 | $this->createMock(RouterInterface::class), |
||
| 544 | new RoutesCache($this->cacheTempFolder, true) |
||
| 545 | ); |
||
| 546 | |||
| 547 | $container = new Container(); |
||
| 548 | $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
||
| 549 | |||
| 550 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
| 551 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 552 | $container->set('sonata.post.admin.post', $postAdmin); |
||
| 553 | $postAdmin->setConfigurationPool($pool); |
||
| 554 | $postAdmin->setRouteBuilder($pathInfo); |
||
| 555 | $postAdmin->setRouteGenerator($routeGenerator); |
||
| 556 | $postAdmin->initialize(); |
||
| 557 | |||
| 558 | $commentAdmin = new CommentAdmin( |
||
| 559 | 'sonata.post.admin.comment', |
||
| 560 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 561 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 562 | ); |
||
| 563 | $container->set('sonata.post.admin.comment', $commentAdmin); |
||
| 564 | $commentAdmin->setConfigurationPool($pool); |
||
| 565 | $commentAdmin->setRouteBuilder($pathInfo); |
||
| 566 | $commentAdmin->setRouteGenerator($routeGenerator); |
||
| 567 | $commentAdmin->initialize(); |
||
| 568 | |||
| 569 | $postAdmin->addChild($commentAdmin, 'post'); |
||
| 570 | |||
| 571 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 572 | 'sonata.post.admin.comment_vote', |
||
| 573 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 574 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 575 | ); |
||
| 576 | |||
| 577 | $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin); |
||
| 578 | $commentVoteAdmin->setConfigurationPool($pool); |
||
| 579 | $commentVoteAdmin->setRouteBuilder($pathInfo); |
||
| 580 | $commentVoteAdmin->setRouteGenerator($routeGenerator); |
||
| 581 | $commentVoteAdmin->initialize(); |
||
| 582 | |||
| 583 | $commentAdmin->addChild($commentVoteAdmin); |
||
| 584 | $pool->setAdminServiceIds([ |
||
| 585 | 'sonata.post.admin.post', |
||
| 586 | 'sonata.post.admin.comment', |
||
| 587 | 'sonata.post.admin.comment_vote', |
||
| 588 | ]); |
||
| 589 | |||
| 590 | $this->assertSame(sprintf('%s_comment', $expected), $commentAdmin->getBaseRouteName()); |
||
| 591 | |||
| 592 | $this->assertTrue($postAdmin->hasRoute('show')); |
||
| 593 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show')); |
||
| 594 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show')); |
||
| 595 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show')); |
||
| 596 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list')); |
||
| 597 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list')); |
||
| 598 | $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit')); |
||
| 599 | $this->assertFalse($commentAdmin->hasRoute('edit')); |
||
| 600 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
| 601 | |||
| 602 | /* |
||
| 603 | * Test the route name from request |
||
| 604 | */ |
||
| 605 | $postListRequest = new Request( |
||
| 606 | [], |
||
| 607 | [], |
||
| 608 | [ |
||
| 609 | '_route' => sprintf('%s_list', $postAdmin->getBaseRouteName()), |
||
| 610 | ] |
||
| 611 | ); |
||
| 612 | |||
| 613 | $postAdmin->setRequest($postListRequest); |
||
| 614 | $commentAdmin->setRequest($postListRequest); |
||
| 615 | |||
| 616 | $this->assertTrue($postAdmin->isCurrentRoute('list')); |
||
| 617 | $this->assertFalse($postAdmin->isCurrentRoute('create')); |
||
| 618 | $this->assertFalse($commentAdmin->isCurrentRoute('list')); |
||
| 619 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('list')); |
||
| 620 | $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
| 621 | $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
| 622 | $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
| 623 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
| 624 | } |
||
| 625 | |||
| 626 | public function testGetBaseRouteNameWithUnreconizedClassname(): void |
||
| 627 | { |
||
| 628 | $this->expectException(\RuntimeException::class); |
||
| 629 | |||
| 630 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 631 | $admin->getBaseRouteName(); |
||
| 632 | } |
||
| 633 | |||
| 634 | public function testGetBaseRouteNameWithSpecifiedName(): void |
||
| 635 | { |
||
| 636 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 637 | |||
| 638 | $this->assertSame('post_custom', $postAdmin->getBaseRouteName()); |
||
| 639 | } |
||
| 640 | |||
| 641 | public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void |
||
| 642 | { |
||
| 643 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 644 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'); |
||
| 645 | $commentAdmin->setParent($postAdmin); |
||
| 646 | |||
| 647 | $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName()); |
||
| 648 | } |
||
| 649 | |||
| 650 | public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void |
||
| 651 | { |
||
| 652 | $postAdmin = new PostAdmin( |
||
| 653 | 'sonata.post.admin.post', |
||
| 654 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 655 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 656 | ); |
||
| 657 | $commentAdmin = new CommentWithCustomRouteAdmin( |
||
| 658 | 'sonata.post.admin.comment_with_custom_route', |
||
| 659 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 660 | 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController' |
||
| 661 | ); |
||
| 662 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 663 | 'sonata.post.admin.comment_vote', |
||
| 664 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 665 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 666 | ); |
||
| 667 | $commentAdmin->setParent($postAdmin); |
||
| 668 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 669 | |||
| 670 | $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName()); |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid |
||
| 675 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid |
||
| 676 | */ |
||
| 677 | public function testSetUniqid(): void |
||
| 678 | { |
||
| 679 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 680 | |||
| 681 | $uniqid = uniqid(); |
||
| 682 | $admin->setUniqid($uniqid); |
||
| 683 | |||
| 684 | $this->assertSame($uniqid, $admin->getUniqid()); |
||
| 685 | } |
||
| 686 | |||
| 687 | public function testToString(): void |
||
| 688 | { |
||
| 689 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 690 | |||
| 691 | $s = new \stdClass(); |
||
| 692 | |||
| 693 | $this->assertNotEmpty($admin->toString($s)); |
||
| 694 | |||
| 695 | $s = new FooToString(); |
||
| 696 | $this->assertSame('salut', $admin->toString($s)); |
||
| 697 | |||
| 698 | // To string method is implemented, but returns null |
||
| 699 | $s = new FooToStringNull(); |
||
| 700 | $this->assertNotEmpty($admin->toString($s)); |
||
| 701 | |||
| 702 | $this->assertSame('', $admin->toString(false)); |
||
|
|
|||
| 703 | } |
||
| 704 | |||
| 705 | public function testIsAclEnabled(): void |
||
| 706 | { |
||
| 707 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 708 | |||
| 709 | $this->assertFalse($postAdmin->isAclEnabled()); |
||
| 710 | |||
| 711 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 712 | $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class)); |
||
| 713 | $this->assertTrue($commentAdmin->isAclEnabled()); |
||
| 714 | } |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @group legacy |
||
| 718 | * |
||
| 719 | * @expectedDeprecation Calling Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. Use Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass() to know if there is an active subclass. |
||
| 720 | * |
||
| 721 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses |
||
| 722 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass |
||
| 723 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses |
||
| 724 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass |
||
| 725 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 726 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass |
||
| 727 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode |
||
| 728 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass |
||
| 729 | */ |
||
| 730 | public function testSubClass(): void |
||
| 731 | { |
||
| 732 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
| 733 | $admin = new PostAdmin( |
||
| 734 | 'sonata.post.admin.post', |
||
| 735 | Post::class, |
||
| 736 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 737 | ); |
||
| 738 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 739 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 740 | $this->assertCount(0, $admin->getSubClasses()); |
||
| 741 | $this->assertNull($admin->getActiveSubClass()); |
||
| 742 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 743 | $this->assertSame(Post::class, $admin->getClass()); |
||
| 744 | |||
| 745 | // Just for the record, if there is no inheritance set, the getSubject is not used |
||
| 746 | // the getSubject can also lead to some issue |
||
| 747 | $admin->setSubject(new BlogPost()); |
||
| 748 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 749 | |||
| 750 | $admin->setSubClasses([ |
||
| 751 | 'extended1' => 'NewsBundle\Entity\PostExtended1', |
||
| 752 | 'extended2' => 'NewsBundle\Entity\PostExtended2', |
||
| 753 | ]); |
||
| 754 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 755 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 756 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 757 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 758 | // NEXT_MAJOR: remove the following 2 `assertNull()` assertions |
||
| 759 | $this->assertNull($admin->getActiveSubClass()); |
||
| 760 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 761 | $this->assertSame( |
||
| 762 | BlogPost::class, |
||
| 763 | $admin->getClass(), |
||
| 764 | 'When there is no subclass in the query the class parameter should be returned' |
||
| 765 | ); |
||
| 766 | |||
| 767 | $request = new Request(['subclass' => 'extended1']); |
||
| 768 | $admin->setRequest($request); |
||
| 769 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 770 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 771 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 772 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 773 | $this->assertSame( |
||
| 774 | 'NewsBundle\Entity\PostExtended1', |
||
| 775 | $admin->getActiveSubClass(), |
||
| 776 | 'It should return the curently active sub class.' |
||
| 777 | ); |
||
| 778 | $this->assertSame('extended1', $admin->getActiveSubclassCode()); |
||
| 779 | $this->assertSame( |
||
| 780 | 'NewsBundle\Entity\PostExtended1', |
||
| 781 | $admin->getClass(), |
||
| 782 | 'getClass() should return the name of the sub class when passed through a request query parameter.' |
||
| 783 | ); |
||
| 784 | |||
| 785 | $request->query->set('subclass', 'inject'); |
||
| 786 | |||
| 787 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 788 | // NEXT_MAJOR: remove the previous `assertNull()` assertion and uncomment the following lines |
||
| 789 | // $this->expectException(\LogicException::class); |
||
| 790 | // $this->expectExceptionMessage(sprintf('Admin "%s" has no active subclass.', PostAdmin::class)); |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @group legacy |
||
| 795 | * |
||
| 796 | * @expectedDeprecation Calling Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. Use Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass() to know if there is an active subclass. |
||
| 797 | */ |
||
| 798 | public function testNonExistantSubclass(): void |
||
| 799 | { |
||
| 800 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
| 801 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 802 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
| 803 | |||
| 804 | $admin->setRequest(new Request(['subclass' => 'inject'])); |
||
| 805 | |||
| 806 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
| 807 | |||
| 808 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 809 | |||
| 810 | $this->expectException(\RuntimeException::class); |
||
| 811 | |||
| 812 | $admin->getActiveSubClass(); |
||
| 813 | } |
||
| 814 | |||
| 815 | /** |
||
| 816 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 817 | */ |
||
| 818 | public function testOnlyOneSubclassNeededToBeActive(): void |
||
| 826 | |||
| 827 | /** |
||
| 828 | * @group legacy |
||
| 829 | * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0. |
||
| 830 | */ |
||
| 831 | public function testAddSubClassIsDeprecated(): void |
||
| 840 | |||
| 841 | /** |
||
| 842 | * @group legacy |
||
| 843 | */ |
||
| 844 | public function testGetPerPageOptions(): void |
||
| 857 | |||
| 858 | public function testGetLabelTranslatorStrategy(): void |
||
| 868 | |||
| 869 | public function testGetRouteBuilder(): void |
||
| 879 | |||
| 880 | public function testGetMenuFactory(): void |
||
| 890 | |||
| 891 | public function testGetExtensions(): void |
||
| 892 | { |
||
| 893 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 894 | |||
| 895 | $this->assertSame([], $admin->getExtensions()); |
||
| 896 | |||
| 897 | $adminExtension1 = $this->createMock(AdminExtensionInterface::class); |
||
| 898 | $adminExtension2 = $this->createMock(AdminExtensionInterface::class); |
||
| 899 | |||
| 900 | $admin->addExtension($adminExtension1); |
||
| 901 | $admin->addExtension($adminExtension2); |
||
| 902 | $this->assertSame([$adminExtension1, $adminExtension2], $admin->getExtensions()); |
||
| 903 | } |
||
| 904 | |||
| 905 | public function testGetFilterTheme(): void |
||
| 906 | { |
||
| 907 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 908 | |||
| 909 | $this->assertSame([], $admin->getFilterTheme()); |
||
| 910 | |||
| 911 | $admin->setFilterTheme(['FooTheme']); |
||
| 912 | $this->assertSame(['FooTheme'], $admin->getFilterTheme()); |
||
| 913 | } |
||
| 914 | |||
| 915 | public function testGetFormTheme(): void |
||
| 916 | { |
||
| 917 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 918 | |||
| 919 | $this->assertSame([], $admin->getFormTheme()); |
||
| 920 | |||
| 921 | $admin->setFormTheme(['FooTheme']); |
||
| 922 | |||
| 923 | $this->assertSame(['FooTheme'], $admin->getFormTheme()); |
||
| 924 | } |
||
| 925 | |||
| 926 | public function testGetValidator(): void |
||
| 927 | { |
||
| 928 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 929 | |||
| 930 | $this->assertNull($admin->getValidator()); |
||
| 931 | |||
| 932 | $validator = $this->getMockForAbstractClass(ValidatorInterface::class); |
||
| 933 | |||
| 934 | $admin->setValidator($validator); |
||
| 935 | $this->assertSame($validator, $admin->getValidator()); |
||
| 936 | } |
||
| 937 | |||
| 938 | public function testGetSecurityHandler(): void |
||
| 939 | { |
||
| 940 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 941 | |||
| 942 | $this->assertNull($admin->getSecurityHandler()); |
||
| 943 | |||
| 944 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
| 945 | $admin->setSecurityHandler($securityHandler); |
||
| 946 | $this->assertSame($securityHandler, $admin->getSecurityHandler()); |
||
| 947 | } |
||
| 948 | |||
| 949 | public function testGetSecurityInformation(): void |
||
| 950 | { |
||
| 951 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 952 | |||
| 953 | $this->assertSame([], $admin->getSecurityInformation()); |
||
| 954 | |||
| 955 | $securityInformation = [ |
||
| 956 | 'GUEST' => ['VIEW', 'LIST'], |
||
| 957 | 'STAFF' => ['EDIT', 'LIST', 'CREATE'], |
||
| 958 | ]; |
||
| 959 | |||
| 960 | $admin->setSecurityInformation($securityInformation); |
||
| 961 | $this->assertSame($securityInformation, $admin->getSecurityInformation()); |
||
| 962 | } |
||
| 963 | |||
| 964 | public function testGetManagerType(): void |
||
| 965 | { |
||
| 966 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 967 | |||
| 968 | $this->assertNull($admin->getManagerType()); |
||
| 969 | |||
| 970 | $admin->setManagerType('foo_orm'); |
||
| 971 | $this->assertSame('foo_orm', $admin->getManagerType()); |
||
| 972 | } |
||
| 973 | |||
| 974 | public function testGetModelManager(): void |
||
| 975 | { |
||
| 976 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 977 | |||
| 978 | $this->assertNull($admin->getModelManager()); |
||
| 979 | |||
| 980 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 981 | |||
| 982 | $admin->setModelManager($modelManager); |
||
| 983 | $this->assertSame($modelManager, $admin->getModelManager()); |
||
| 984 | } |
||
| 985 | |||
| 986 | /** |
||
| 987 | * NEXT_MAJOR: remove this method. |
||
| 988 | * |
||
| 989 | * @group legacy |
||
| 990 | */ |
||
| 991 | public function testGetBaseCodeRoute(): void |
||
| 992 | { |
||
| 993 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 994 | |||
| 995 | $this->assertSame('', $admin->getBaseCodeRoute()); |
||
| 996 | |||
| 997 | $admin->setBaseCodeRoute('foo'); |
||
| 998 | $this->assertSame('foo', $admin->getBaseCodeRoute()); |
||
| 999 | } |
||
| 1000 | |||
| 1001 | // NEXT_MAJOR: uncomment this method. |
||
| 1002 | // public function testGetBaseCodeRoute() |
||
| 1003 | // { |
||
| 1004 | // $postAdmin = new PostAdmin( |
||
| 1005 | // 'sonata.post.admin.post', |
||
| 1006 | // 'NewsBundle\Entity\Post', |
||
| 1007 | // 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 1008 | // ); |
||
| 1009 | // $commentAdmin = new CommentAdmin( |
||
| 1010 | // 'sonata.post.admin.comment', |
||
| 1011 | // 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 1012 | // 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 1013 | // ); |
||
| 1014 | // |
||
| 1015 | // $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute()); |
||
| 1016 | // |
||
| 1017 | // $postAdmin->addChild($commentAdmin); |
||
| 1018 | // |
||
| 1019 | // $this->assertSame( |
||
| 1020 | // 'sonata.post.admin.post|sonata.post.admin.comment', |
||
| 1021 | // $commentAdmin->getBaseCodeRoute() |
||
| 1022 | // ); |
||
| 1023 | // } |
||
| 1024 | |||
| 1025 | public function testGetRouteGenerator(): void |
||
| 1026 | { |
||
| 1027 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1028 | |||
| 1029 | $this->assertNull($admin->getRouteGenerator()); |
||
| 1030 | |||
| 1031 | $routeGenerator = $this->createMock(RouteGeneratorInterface::class); |
||
| 1032 | |||
| 1033 | $admin->setRouteGenerator($routeGenerator); |
||
| 1034 | $this->assertSame($routeGenerator, $admin->getRouteGenerator()); |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | public function testGetConfigurationPool(): void |
||
| 1038 | { |
||
| 1039 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1040 | |||
| 1041 | $this->assertNull($admin->getConfigurationPool()); |
||
| 1042 | |||
| 1043 | $pool = $this->getMockBuilder(Pool::class) |
||
| 1044 | ->disableOriginalConstructor() |
||
| 1045 | ->getMock(); |
||
| 1046 | |||
| 1047 | $admin->setConfigurationPool($pool); |
||
| 1048 | $this->assertSame($pool, $admin->getConfigurationPool()); |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | public function testGetShowBuilder(): void |
||
| 1052 | { |
||
| 1053 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1054 | |||
| 1055 | $this->assertNull($admin->getShowBuilder()); |
||
| 1056 | |||
| 1057 | $showBuilder = $this->createMock(ShowBuilderInterface::class); |
||
| 1058 | |||
| 1059 | $admin->setShowBuilder($showBuilder); |
||
| 1060 | $this->assertSame($showBuilder, $admin->getShowBuilder()); |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | public function testGetListBuilder(): void |
||
| 1064 | { |
||
| 1065 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1066 | |||
| 1067 | $this->assertNull($admin->getListBuilder()); |
||
| 1068 | |||
| 1069 | $listBuilder = $this->createMock(ListBuilderInterface::class); |
||
| 1070 | |||
| 1071 | $admin->setListBuilder($listBuilder); |
||
| 1072 | $this->assertSame($listBuilder, $admin->getListBuilder()); |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | public function testGetDatagridBuilder(): void |
||
| 1076 | { |
||
| 1077 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1078 | |||
| 1079 | $this->assertNull($admin->getDatagridBuilder()); |
||
| 1080 | |||
| 1081 | $datagridBuilder = $this->createMock(DatagridBuilderInterface::class); |
||
| 1082 | |||
| 1083 | $admin->setDatagridBuilder($datagridBuilder); |
||
| 1084 | $this->assertSame($datagridBuilder, $admin->getDatagridBuilder()); |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | public function testGetFormContractor(): void |
||
| 1088 | { |
||
| 1089 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1090 | |||
| 1091 | $this->assertNull($admin->getFormContractor()); |
||
| 1092 | |||
| 1093 | $formContractor = $this->createMock(FormContractorInterface::class); |
||
| 1094 | |||
| 1095 | $admin->setFormContractor($formContractor); |
||
| 1096 | $this->assertSame($formContractor, $admin->getFormContractor()); |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | public function testGetRequest(): void |
||
| 1100 | { |
||
| 1101 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1102 | |||
| 1103 | $this->assertFalse($admin->hasRequest()); |
||
| 1104 | |||
| 1105 | $request = new Request(); |
||
| 1106 | |||
| 1107 | $admin->setRequest($request); |
||
| 1108 | $this->assertSame($request, $admin->getRequest()); |
||
| 1109 | $this->assertTrue($admin->hasRequest()); |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | public function testGetRequestWithException(): void |
||
| 1113 | { |
||
| 1114 | $this->expectException(\RuntimeException::class); |
||
| 1115 | $this->expectExceptionMessage('The Request object has not been set'); |
||
| 1116 | |||
| 1117 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1118 | $admin->getRequest(); |
||
| 1119 | } |
||
| 1120 | |||
| 1121 | public function testGetTranslationDomain(): void |
||
| 1122 | { |
||
| 1123 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1124 | |||
| 1125 | $this->assertSame('messages', $admin->getTranslationDomain()); |
||
| 1126 | |||
| 1127 | $admin->setTranslationDomain('foo'); |
||
| 1128 | $this->assertSame('foo', $admin->getTranslationDomain()); |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * @group legacy |
||
| 1133 | */ |
||
| 1134 | public function testGetTranslator(): void |
||
| 1135 | { |
||
| 1136 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1137 | |||
| 1138 | $this->assertNull($admin->getTranslator()); |
||
| 1139 | |||
| 1140 | $translator = $this->createStub($this->getTranslatorInterface()); |
||
| 1141 | |||
| 1142 | $admin->setTranslator($translator); |
||
| 1143 | $this->assertSame($translator, $admin->getTranslator()); |
||
| 1144 | } |
||
| 1145 | |||
| 1146 | public function testGetShowGroups(): void |
||
| 1147 | { |
||
| 1148 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1149 | |||
| 1150 | // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call. |
||
| 1151 | $this->assertFalse($admin->getShowGroups('sonata_deprecation_mute')); |
||
| 1152 | |||
| 1153 | $groups = ['foo', 'bar', 'baz']; |
||
| 1154 | |||
| 1155 | $admin->setShowGroups($groups); |
||
| 1156 | $this->assertSame($groups, $admin->getShowGroups()); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | public function testGetFormGroups(): void |
||
| 1160 | { |
||
| 1161 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1162 | |||
| 1163 | // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call. |
||
| 1164 | $this->assertFalse($admin->getFormGroups('sonata_deprecation_mute')); |
||
| 1165 | |||
| 1166 | $groups = ['foo', 'bar', 'baz']; |
||
| 1167 | |||
| 1168 | $admin->setFormGroups($groups); |
||
| 1169 | $this->assertSame($groups, $admin->getFormGroups()); |
||
| 1170 | } |
||
| 1171 | |||
| 1172 | public function testGetMaxPageLinks(): void |
||
| 1173 | { |
||
| 1174 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1175 | |||
| 1176 | $this->assertSame(25, $admin->getMaxPageLinks()); |
||
| 1177 | |||
| 1178 | $admin->setMaxPageLinks(14); |
||
| 1179 | $this->assertSame(14, $admin->getMaxPageLinks()); |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * @group legacy |
||
| 1184 | */ |
||
| 1185 | public function testGetMaxPerPage(): void |
||
| 1186 | { |
||
| 1187 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1188 | |||
| 1189 | $this->assertSame(32, $admin->getMaxPerPage()); |
||
| 1190 | |||
| 1191 | $admin->setMaxPerPage(94); |
||
| 1192 | $this->assertSame(94, $admin->getMaxPerPage()); |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | public function testGetLabel(): void |
||
| 1196 | { |
||
| 1197 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1198 | |||
| 1199 | $this->assertNull($admin->getLabel()); |
||
| 1200 | |||
| 1201 | $admin->setLabel('FooLabel'); |
||
| 1202 | $this->assertSame('FooLabel', $admin->getLabel()); |
||
| 1203 | } |
||
| 1204 | |||
| 1205 | public function testGetBaseController(): void |
||
| 1206 | { |
||
| 1207 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1208 | |||
| 1209 | $this->assertSame('Sonata\NewsBundle\Controller\PostAdminController', $admin->getBaseControllerName()); |
||
| 1210 | |||
| 1211 | $admin->setBaseControllerName('Sonata\NewsBundle\Controller\FooAdminController'); |
||
| 1212 | $this->assertSame('Sonata\NewsBundle\Controller\FooAdminController', $admin->getBaseControllerName()); |
||
| 1213 | } |
||
| 1214 | |||
| 1215 | public function testGetTemplates(): void |
||
| 1216 | { |
||
| 1217 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1218 | |||
| 1219 | $templates = [ |
||
| 1220 | 'list' => '@FooAdmin/CRUD/list.html.twig', |
||
| 1221 | 'show' => '@FooAdmin/CRUD/show.html.twig', |
||
| 1222 | 'edit' => '@FooAdmin/CRUD/edit.html.twig', |
||
| 1223 | ]; |
||
| 1224 | |||
| 1225 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 1226 | $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates); |
||
| 1227 | |||
| 1228 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 1229 | |||
| 1230 | $this->assertSame($templates, $admin->getTemplates()); |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | public function testGetTemplate1(): void |
||
| 1234 | { |
||
| 1235 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1236 | |||
| 1237 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 1238 | $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig'); |
||
| 1239 | $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig'); |
||
| 1240 | |||
| 1241 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 1242 | |||
| 1243 | $this->assertSame('@FooAdmin/CRUD/edit.html.twig', $admin->getTemplate('edit')); |
||
| 1244 | $this->assertSame('@FooAdmin/CRUD/show.html.twig', $admin->getTemplate('show')); |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | public function testGetIdParameter(): void |
||
| 1248 | { |
||
| 1249 | $postAdmin = new PostAdmin( |
||
| 1250 | 'sonata.post.admin.post', |
||
| 1251 | 'NewsBundle\Entity\Post', |
||
| 1252 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 1253 | ); |
||
| 1254 | |||
| 1255 | $this->assertSame('id', $postAdmin->getIdParameter()); |
||
| 1256 | $this->assertFalse($postAdmin->isChild()); |
||
| 1257 | |||
| 1258 | $commentAdmin = new CommentAdmin( |
||
| 1259 | 'sonata.post.admin.comment', |
||
| 1260 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 1261 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 1262 | ); |
||
| 1263 | $commentAdmin->setParent($postAdmin); |
||
| 1264 | |||
| 1265 | $this->assertTrue($commentAdmin->isChild()); |
||
| 1266 | $this->assertSame('childId', $commentAdmin->getIdParameter()); |
||
| 1267 | |||
| 1268 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 1269 | 'sonata.post.admin.comment_vote', |
||
| 1270 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 1271 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 1272 | ); |
||
| 1273 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 1274 | |||
| 1275 | $this->assertTrue($commentVoteAdmin->isChild()); |
||
| 1276 | $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter()); |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | public function testGetExportFormats(): void |
||
| 1280 | { |
||
| 1281 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1282 | |||
| 1283 | $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats()); |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | public function testGetUrlsafeIdentifier(): void |
||
| 1287 | { |
||
| 1288 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1289 | |||
| 1290 | $model = new \stdClass(); |
||
| 1291 | |||
| 1292 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1293 | $modelManager->expects($this->once()) |
||
| 1294 | ->method('getUrlSafeIdentifier') |
||
| 1295 | ->with($this->equalTo($model)) |
||
| 1296 | ->willReturn('foo'); |
||
| 1297 | $admin->setModelManager($modelManager); |
||
| 1298 | |||
| 1299 | $this->assertSame('foo', $admin->getUrlSafeIdentifier($model)); |
||
| 1300 | } |
||
| 1301 | |||
| 1302 | public function testDeterminedPerPageValue(): void |
||
| 1303 | { |
||
| 1304 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1305 | |||
| 1306 | $this->assertFalse($admin->determinedPerPageValue('foo')); |
||
| 1307 | $this->assertFalse($admin->determinedPerPageValue(123)); |
||
| 1308 | $this->assertTrue($admin->determinedPerPageValue(16)); |
||
| 1309 | } |
||
| 1310 | |||
| 1311 | public function testIsGranted(): void |
||
| 1312 | { |
||
| 1313 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1314 | $modelManager = $this->createStub(ModelManagerInterface::class); |
||
| 1315 | $modelManager |
||
| 1316 | ->method('getNormalizedIdentifier') |
||
| 1317 | ->willReturnCallback(static function (?object $model = null): ?string { |
||
| 1318 | return $model ? $model->id : null; |
||
| 1319 | }); |
||
| 1320 | |||
| 1321 | $admin->setModelManager($modelManager); |
||
| 1322 | |||
| 1323 | $entity1 = new \stdClass(); |
||
| 1324 | $entity1->id = '1'; |
||
| 1325 | |||
| 1326 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1327 | $securityHandler |
||
| 1328 | ->expects($this->exactly(6)) |
||
| 1329 | ->method('isGranted') |
||
| 1330 | ->willReturnCallback(static function ( |
||
| 1331 | AdminInterface $adminIn, |
||
| 1332 | string $attributes, |
||
| 1333 | ?object $object = null |
||
| 1334 | ) use ( |
||
| 1335 | $admin, |
||
| 1336 | $entity1 |
||
| 1337 | ): bool { |
||
| 1338 | return $admin === $adminIn && 'FOO' === $attributes && |
||
| 1339 | ($object === $admin || $object === $entity1); |
||
| 1340 | }); |
||
| 1341 | |||
| 1342 | $admin->setSecurityHandler($securityHandler); |
||
| 1343 | |||
| 1344 | $this->assertTrue($admin->isGranted('FOO')); |
||
| 1345 | $this->assertTrue($admin->isGranted('FOO')); |
||
| 1346 | $this->assertTrue($admin->isGranted('FOO', $entity1)); |
||
| 1347 | $this->assertTrue($admin->isGranted('FOO', $entity1)); |
||
| 1348 | $this->assertFalse($admin->isGranted('BAR')); |
||
| 1349 | $this->assertFalse($admin->isGranted('BAR')); |
||
| 1350 | $this->assertFalse($admin->isGranted('BAR', $entity1)); |
||
| 1351 | $this->assertFalse($admin->isGranted('BAR', $entity1)); |
||
| 1352 | |||
| 1353 | $entity2 = new \stdClass(); |
||
| 1354 | $entity2->id = '2'; |
||
| 1355 | |||
| 1356 | $this->assertFalse($admin->isGranted('BAR', $entity2)); |
||
| 1357 | $this->assertFalse($admin->isGranted('BAR', $entity2)); |
||
| 1358 | |||
| 1359 | $entity3 = new \stdClass(); |
||
| 1360 | $entity3->id = '3'; |
||
| 1361 | |||
| 1362 | $this->assertFalse($admin->isGranted('BAR', $entity3)); |
||
| 1363 | $this->assertFalse($admin->isGranted('BAR', $entity3)); |
||
| 1364 | } |
||
| 1365 | |||
| 1366 | public function testSupportsPreviewMode(): void |
||
| 1367 | { |
||
| 1368 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1369 | |||
| 1370 | $this->assertFalse($admin->supportsPreviewMode()); |
||
| 1371 | } |
||
| 1372 | |||
| 1373 | public function testGetPermissionsShow(): void |
||
| 1374 | { |
||
| 1375 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1376 | |||
| 1377 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1378 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU)); |
||
| 1379 | $this->assertSame(['LIST'], $admin->getPermissionsShow('foo')); |
||
| 1380 | } |
||
| 1381 | |||
| 1382 | public function testShowIn(): void |
||
| 1383 | { |
||
| 1384 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1385 | |||
| 1386 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1387 | $securityHandler |
||
| 1388 | ->method('isGranted') |
||
| 1389 | ->willReturnCallback(static function (AdminInterface $adminIn, array $attributes, $object = null) use ($admin): bool { |
||
| 1390 | return $admin === $adminIn && $attributes === ['LIST']; |
||
| 1391 | }); |
||
| 1392 | |||
| 1393 | $admin->setSecurityHandler($securityHandler); |
||
| 1394 | |||
| 1395 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1396 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU)); |
||
| 1397 | $this->assertTrue($admin->showIn('foo')); |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | public function testGetObjectIdentifier(): void |
||
| 1401 | { |
||
| 1402 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1403 | |||
| 1404 | $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier()); |
||
| 1405 | } |
||
| 1406 | |||
| 1407 | /** |
||
| 1408 | * @group legacy |
||
| 1409 | */ |
||
| 1410 | public function testTrans(): void |
||
| 1411 | { |
||
| 1412 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1413 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1414 | |||
| 1415 | $translator = $this->createStub($this->getTranslatorInterface()); |
||
| 1416 | $admin->setTranslator($translator); |
||
| 1417 | |||
| 1418 | $translator->expects($this->once()) |
||
| 1419 | ->method('trans') |
||
| 1420 | ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1421 | ->willReturn('fooTranslated'); |
||
| 1422 | |||
| 1423 | $this->assertSame('fooTranslated', $admin->trans('foo')); |
||
| 1424 | } |
||
| 1425 | |||
| 1426 | /** |
||
| 1427 | * @group legacy |
||
| 1428 | */ |
||
| 1429 | public function testTransWithMessageDomain(): void |
||
| 1430 | { |
||
| 1431 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1432 | |||
| 1433 | $translator = $this->createStub($this->getTranslatorInterface()); |
||
| 1434 | $admin->setTranslator($translator); |
||
| 1435 | |||
| 1436 | $translator->expects($this->once()) |
||
| 1437 | ->method('trans') |
||
| 1438 | ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1439 | ->willReturn('fooTranslated'); |
||
| 1440 | |||
| 1441 | $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * @group legacy |
||
| 1446 | */ |
||
| 1447 | public function testTransChoice(): void |
||
| 1448 | { |
||
| 1449 | if (!interface_exists(DeprecatedTranslatorInterface::class)) { |
||
| 1450 | $this->markTestSkipped('Test only available in Symfony 4'); |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1454 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1455 | |||
| 1456 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1457 | $admin->setTranslator($translator); |
||
| 1458 | |||
| 1459 | $translator->expects($this->once()) |
||
| 1460 | ->method('transChoice') |
||
| 1461 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1462 | ->willReturn('fooTranslated'); |
||
| 1463 | |||
| 1464 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2)); |
||
| 1465 | } |
||
| 1466 | |||
| 1467 | /** |
||
| 1468 | * @group legacy |
||
| 1469 | */ |
||
| 1470 | public function testTransChoiceWithMessageDomain(): void |
||
| 1471 | { |
||
| 1472 | if (!interface_exists(DeprecatedTranslatorInterface::class)) { |
||
| 1473 | $this->markTestSkipped('Test only available in Symfony 4'); |
||
| 1474 | } |
||
| 1475 | |||
| 1476 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1477 | |||
| 1478 | $translator = $this->createStub($this->getTranslatorInterface()); |
||
| 1479 | $admin->setTranslator($translator); |
||
| 1480 | |||
| 1481 | $translator->expects($this->once()) |
||
| 1482 | ->method('transChoice') |
||
| 1483 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1484 | ->willReturn('fooTranslated'); |
||
| 1485 | |||
| 1486 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1487 | } |
||
| 1488 | |||
| 1489 | public function testSetFilterPersister(): void |
||
| 1490 | { |
||
| 1491 | $admin = new class('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle\Controller\PostAdminController') extends PostAdmin { |
||
| 1492 | public function persistFilters(): bool |
||
| 1493 | { |
||
| 1494 | return $this->persistFilters; |
||
| 1495 | } |
||
| 1496 | }; |
||
| 1497 | |||
| 1498 | $filterPersister = $this->createMock(FilterPersisterInterface::class); |
||
| 1499 | |||
| 1500 | $admin->setFilterPersister($filterPersister); |
||
| 1501 | $this->assertTrue($admin->persistFilters()); |
||
| 1502 | } |
||
| 1503 | |||
| 1504 | public function testGetRootCode(): void |
||
| 1505 | { |
||
| 1506 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1507 | |||
| 1508 | $this->assertSame('sonata.post.admin.post', $admin->getRootCode()); |
||
| 1509 | |||
| 1510 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController'); |
||
| 1511 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
| 1512 | $parentFieldDescription->expects($this->once()) |
||
| 1513 | ->method('getAdmin') |
||
| 1514 | ->willReturn($parentAdmin); |
||
| 1515 | |||
| 1516 | $this->assertFalse($admin->hasParentFieldDescription()); |
||
| 1517 | $admin->setParentFieldDescription($parentFieldDescription); |
||
| 1518 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
| 1519 | $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode()); |
||
| 1520 | } |
||
| 1521 | |||
| 1522 | public function testGetRoot(): void |
||
| 1523 | { |
||
| 1524 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1525 | |||
| 1526 | $this->assertSame($admin, $admin->getRoot()); |
||
| 1527 | |||
| 1528 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController'); |
||
| 1529 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
| 1530 | $parentFieldDescription->expects($this->once()) |
||
| 1531 | ->method('getAdmin') |
||
| 1532 | ->willReturn($parentAdmin); |
||
| 1533 | |||
| 1534 | $this->assertFalse($admin->hasParentFieldDescription()); |
||
| 1535 | $admin->setParentFieldDescription($parentFieldDescription); |
||
| 1536 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
| 1537 | $this->assertSame($parentAdmin, $admin->getRoot()); |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | public function testGetExportFields(): void |
||
| 1541 | { |
||
| 1542 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1543 | |||
| 1544 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1545 | $modelManager->expects($this->once()) |
||
| 1546 | ->method('getExportFields') |
||
| 1547 | ->with($this->equalTo('NewsBundle\Entity\Post')) |
||
| 1548 | ->willReturn(['foo', 'bar']); |
||
| 1549 | |||
| 1550 | $admin->setModelManager($modelManager); |
||
| 1551 | $this->assertSame(['foo', 'bar'], $admin->getExportFields()); |
||
| 1552 | } |
||
| 1553 | |||
| 1554 | public function testGetPersistentParametersWithNoExtension(): void |
||
| 1555 | { |
||
| 1556 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1557 | |||
| 1558 | $this->assertEmpty($admin->getPersistentParameters()); |
||
| 1559 | } |
||
| 1560 | |||
| 1561 | public function testGetPersistentParametersWithInvalidExtension(): void |
||
| 1574 | |||
| 1575 | public function testGetPersistentParametersWithValidExtension(): void |
||
| 1576 | { |
||
| 1577 | $expected = [ |
||
| 1578 | 'context' => 'foobar', |
||
| 1579 | ]; |
||
| 1580 | |||
| 1590 | |||
| 1591 | public function testGetNewInstanceForChildAdminWithParentValue(): void |
||
| 1592 | { |
||
| 1593 | $post = new Post(); |
||
| 1594 | |||
| 1595 | $postAdmin = $this->getMockBuilder(PostAdmin::class)->disableOriginalConstructor()->getMock(); |
||
| 1596 | $postAdmin->method('getObject')->willReturn($post); |
||
| 1597 | $postAdmin->method('getIdParameter')->willReturn('id'); |
||
| 1598 | |||
| 1599 | $formBuilder = $this->createStub(FormBuilderInterface::class); |
||
| 1600 | $formBuilder->method('getForm')->willReturn(null); |
||
| 1601 | |||
| 1602 | $tag = new Tag(); |
||
| 1603 | |||
| 1604 | $modelManager = $this->createStub(ModelManagerInterface::class); |
||
| 1605 | $modelManager->method('getModelInstance')->willReturn($tag); |
||
| 1606 | |||
| 1607 | $tagAdmin = new TagAdmin('admin.tag', Tag::class, 'MyBundle\MyController'); |
||
| 1608 | $tagAdmin->setModelManager($modelManager); |
||
| 1609 | $tagAdmin->setParent($postAdmin); |
||
| 1610 | |||
| 1611 | $request = $this->createStub(Request::class); |
||
| 1612 | $tagAdmin->setRequest($request); |
||
| 1613 | |||
| 1614 | $configurationPool = $this->getMockBuilder(Pool::class) |
||
| 1615 | ->disableOriginalConstructor() |
||
| 1616 | ->getMock(); |
||
| 1617 | |||
| 1618 | $configurationPool->method('getPropertyAccessor')->willReturn(PropertyAccess::createPropertyAccessor()); |
||
| 1619 | |||
| 1620 | $tagAdmin->setConfigurationPool($configurationPool); |
||
| 1621 | |||
| 1622 | $tag = $tagAdmin->getNewInstance(); |
||
| 1623 | |||
| 1624 | $this->assertSame($post, $tag->getPost()); |
||
| 1625 | } |
||
| 1626 | |||
| 1627 | public function testGetNewInstanceForChildAdminWithCollectionParentValue(): void |
||
| 1628 | { |
||
| 1629 | $post = new Post(); |
||
| 1630 | |||
| 1631 | $postAdmin = $this->getMockBuilder(PostAdmin::class)->disableOriginalConstructor()->getMock(); |
||
| 1632 | $postAdmin->method('getObject')->willReturn($post); |
||
| 1633 | $postAdmin->method('getIdParameter')->willReturn('id'); |
||
| 1634 | |||
| 1635 | $formBuilder = $this->createStub(FormBuilderInterface::class); |
||
| 1636 | $formBuilder->method('getForm')->willReturn(null); |
||
| 1637 | |||
| 1638 | $postCategory = new PostCategory(); |
||
| 1639 | |||
| 1640 | $modelManager = $this->createStub(ModelManagerInterface::class); |
||
| 1641 | $modelManager->method('getModelInstance')->willReturn($postCategory); |
||
| 1642 | |||
| 1643 | $postCategoryAdmin = new PostCategoryAdmin('admin.post_category', PostCategoryAdmin::class, 'MyBundle\MyController'); |
||
| 1644 | $postCategoryAdmin->setModelManager($modelManager); |
||
| 1645 | $postCategoryAdmin->setParent($postAdmin); |
||
| 1646 | |||
| 1647 | $request = $this->createStub(Request::class); |
||
| 1648 | $postCategoryAdmin->setRequest($request); |
||
| 1649 | |||
| 1650 | $configurationPool = $this->getMockBuilder(Pool::class) |
||
| 1651 | ->disableOriginalConstructor() |
||
| 1652 | ->getMock(); |
||
| 1653 | |||
| 1654 | $configurationPool->method('getPropertyAccessor')->willReturn(PropertyAccess::createPropertyAccessor()); |
||
| 1655 | |||
| 1656 | $postCategoryAdmin->setConfigurationPool($configurationPool); |
||
| 1657 | |||
| 1658 | $postCategory = $postCategoryAdmin->getNewInstance(); |
||
| 1659 | |||
| 1660 | $this->assertInstanceOf(Collection::class, $postCategory->getPosts()); |
||
| 1661 | $this->assertCount(1, $postCategory->getPosts()); |
||
| 1662 | $this->assertContains($post, $postCategory->getPosts()); |
||
| 1663 | } |
||
| 1664 | |||
| 1665 | public function testGetNewInstanceForEmbededAdminWithParentValue(): void |
||
| 1705 | |||
| 1706 | public function testFormAddPostSubmitEventForPreValidation(): void |
||
| 1774 | |||
| 1775 | public function testCanAddInlineValidationOnlyForGenericMetadata(): void |
||
| 1824 | |||
| 1825 | public function testRemoveFieldFromFormGroup(): void |
||
| 1851 | |||
| 1852 | public function testGetFilterParameters(): void |
||
| 1892 | |||
| 1893 | public function testGetFilterFieldDescription(): void |
||
| 1964 | |||
| 1965 | public function testGetSubjectNoRequest(): void |
||
| 1977 | |||
| 1978 | public function testGetSideMenu(): void |
||
| 2002 | |||
| 2003 | /** |
||
| 2004 | * @return array |
||
| 2005 | */ |
||
| 2006 | public function provideGetSubject() |
||
| 2016 | |||
| 2017 | /** |
||
| 2018 | * @dataProvider provideGetSubject |
||
| 2019 | */ |
||
| 2020 | public function testGetSubjectFailed($id): void |
||
| 2035 | |||
| 2036 | /** |
||
| 2037 | * @dataProvider provideGetSubject |
||
| 2038 | */ |
||
| 2039 | public function testGetSubject($id): void |
||
| 2058 | |||
| 2059 | public function testGetSubjectWithParentDescription(): void |
||
| 2088 | |||
| 2089 | /** |
||
| 2090 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 2091 | */ |
||
| 2092 | public function testGetActionButtonsList(): void |
||
| 2125 | |||
| 2126 | /** |
||
| 2127 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 2128 | */ |
||
| 2129 | public function testGetActionButtonsListCreateDisabled(): void |
||
| 2143 | |||
| 2144 | /** |
||
| 2145 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions |
||
| 2146 | */ |
||
| 2147 | public function testGetBatchActions(): void |
||
| 2201 | |||
| 2202 | /** |
||
| 2203 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 2204 | */ |
||
| 2205 | public function testShowMosaicButton(): void |
||
| 2214 | |||
| 2215 | /** |
||
| 2216 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 2217 | */ |
||
| 2218 | public function testShowMosaicButtonHideMosaic(): void |
||
| 2228 | |||
| 2229 | /** |
||
| 2230 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions |
||
| 2231 | * @dataProvider provideGetBaseRouteName |
||
| 2232 | */ |
||
| 2233 | public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void |
||
| 2264 | |||
| 2265 | /** |
||
| 2266 | * NEXT_MAJOR: Remove the assertion about isDefaultFilter method and the legacy group. |
||
| 2267 | * |
||
| 2268 | * @group legacy |
||
| 2269 | * |
||
| 2270 | * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::isDefaultFilter" is deprecated since sonata-project/admin-bundle 3.x. |
||
| 2271 | */ |
||
| 2272 | public function testDefaultFilters(): void |
||
| 2331 | |||
| 2332 | /** |
||
| 2333 | * @group legacy |
||
| 2334 | */ |
||
| 2335 | public function testDefaultBreadcrumbsBuilder(): void |
||
| 2359 | |||
| 2360 | /** |
||
| 2361 | * @group legacy |
||
| 2362 | */ |
||
| 2363 | public function testBreadcrumbsBuilderSetter(): void |
||
| 2373 | |||
| 2374 | /** |
||
| 2375 | * @group legacy |
||
| 2376 | */ |
||
| 2377 | public function testGetBreadcrumbs(): void |
||
| 2387 | |||
| 2388 | /** |
||
| 2389 | * @group legacy |
||
| 2390 | */ |
||
| 2391 | public function testBuildBreadcrumbs(): void |
||
| 2408 | |||
| 2409 | /** |
||
| 2410 | * NEXT_MAJOR: remove this method. |
||
| 2411 | * |
||
| 2412 | * @group legacy |
||
| 2413 | */ |
||
| 2414 | public function testCreateQueryLegacyCallWorks(): void |
||
| 2429 | |||
| 2430 | public function testGetDataSourceIterator(): void |
||
| 2472 | |||
| 2473 | public function testCircularChildAdmin(): void |
||
| 2493 | |||
| 2494 | public function testCircularChildAdminTripleLevel(): void |
||
| 2520 | |||
| 2521 | public function testCircularChildAdminWithItself(): void |
||
| 2535 | |||
| 2536 | public function testGetRootAncestor(): void |
||
| 2570 | |||
| 2571 | public function testGetChildDepth(): void |
||
| 2605 | |||
| 2606 | public function testGetCurrentLeafChildAdmin(): void |
||
| 2643 | |||
| 2644 | public function testAdminWithoutControllerName(): void |
||
| 2650 | |||
| 2651 | public function testAdminAvoidInifiniteLoop(): void |
||
| 2681 | |||
| 2682 | /** |
||
| 2683 | * NEXT_MAJOR: Remove this test and its data provider. |
||
| 2684 | * |
||
| 2685 | * @group legacy |
||
| 2686 | * |
||
| 2687 | * @dataProvider getDeprecatedAbstractAdminConstructorArgs |
||
| 2688 | * |
||
| 2689 | * @expectedDeprecation Passing other type than string%S as argument %d for method Sonata\AdminBundle\Admin\AbstractAdmin::__construct() is deprecated since sonata-project/admin-bundle 3.65. It will accept only string%S in version 4.0. |
||
| 2690 | */ |
||
| 2691 | public function testDeprecatedAbstractAdminConstructorArgs($code, $class, $baseControllerName): void |
||
| 2695 | |||
| 2696 | public function getDeprecatedAbstractAdminConstructorArgs(): iterable |
||
| 2710 | |||
| 2711 | private function getTranslatorInterface(): string |
||
| 2719 | } |
||
| 2720 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: