| Total Complexity | 45 |
| Total Lines | 2339 |
| 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 |
||
| 85 | class AdminTest extends TestCase |
||
| 86 | { |
||
| 87 | protected $cacheTempFolder; |
||
| 88 | |||
| 89 | protected function setUp(): void |
||
| 90 | { |
||
| 91 | $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route'; |
||
| 92 | $filesystem = new Filesystem(); |
||
| 93 | $filesystem->remove($this->cacheTempFolder); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct |
||
| 98 | */ |
||
| 99 | public function testConstructor(): void |
||
| 100 | { |
||
| 101 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
| 102 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 103 | |||
| 104 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 105 | $this->assertInstanceOf(AbstractAdmin::class, $admin); |
||
| 106 | $this->assertSame($class, $admin->getClass()); |
||
| 107 | $this->assertSame($baseControllerName, $admin->getBaseControllerName()); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function testGetClass(): void |
||
| 111 | { |
||
| 112 | $class = Post::class; |
||
| 113 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 114 | |||
| 115 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 116 | |||
| 117 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
| 118 | |||
| 119 | $admin->setSubject(new BlogPost()); |
||
| 120 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 121 | |||
| 122 | $admin->setSubClasses(['foo']); |
||
| 123 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 124 | |||
| 125 | $admin->setSubject(null); |
||
| 126 | $admin->setSubClasses([]); |
||
| 127 | $this->assertSame($class, $admin->getClass()); |
||
| 128 | |||
| 129 | $admin->setSubClasses(['foo' => 'bar']); |
||
| 130 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
| 131 | $this->assertSame('bar', $admin->getClass()); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function testGetClassException(): void |
||
| 135 | { |
||
| 136 | $this->expectException(\RuntimeException::class); |
||
| 137 | $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass'); |
||
| 138 | |||
| 139 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
| 140 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 141 | |||
| 142 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 143 | $admin->setParentFieldDescription(new FieldDescription()); |
||
| 144 | $admin->setSubClasses(['foo' => 'bar']); |
||
| 145 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
| 146 | $admin->getClass(); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function testCheckAccessThrowsExceptionOnMadeUpAction(): void |
||
| 150 | { |
||
| 151 | $admin = new PostAdmin( |
||
| 152 | 'sonata.post.admin.post', |
||
| 153 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 154 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 155 | ); |
||
| 156 | $this->expectException( |
||
| 157 | \InvalidArgumentException::class |
||
| 158 | ); |
||
| 159 | $this->expectExceptionMessage( |
||
| 160 | 'Action "made-up" could not be found' |
||
| 161 | ); |
||
| 162 | $admin->checkAccess('made-up'); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function testCheckAccessThrowsAccessDeniedException(): void |
||
| 166 | { |
||
| 167 | $admin = new PostAdmin( |
||
| 168 | 'sonata.post.admin.post', |
||
| 169 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 170 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 171 | ); |
||
| 172 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 173 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 174 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
| 175 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 176 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 177 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 178 | ); |
||
| 179 | $admin->addExtension($customExtension->reveal()); |
||
| 180 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 181 | $this->expectException( |
||
| 182 | AccessDeniedException::class |
||
| 183 | ); |
||
| 184 | $this->expectExceptionMessage( |
||
| 185 | 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE' |
||
| 186 | ); |
||
| 187 | $admin->checkAccess('custom_action'); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function testHasAccessOnMadeUpAction(): void |
||
| 191 | { |
||
| 192 | $admin = new PostAdmin( |
||
| 193 | 'sonata.post.admin.post', |
||
| 194 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 195 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 196 | ); |
||
| 197 | |||
| 198 | $this->assertFalse($admin->hasAccess('made-up')); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function testHasAccess(): void |
||
| 202 | { |
||
| 203 | $admin = new PostAdmin( |
||
| 204 | 'sonata.post.admin.post', |
||
| 205 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 206 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 207 | ); |
||
| 208 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 209 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 210 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
| 211 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 212 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 213 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 214 | ); |
||
| 215 | $admin->addExtension($customExtension->reveal()); |
||
| 216 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 217 | |||
| 218 | $this->assertFalse($admin->hasAccess('custom_action')); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function testHasAccessAllowsAccess(): void |
||
| 222 | { |
||
| 223 | $admin = new PostAdmin( |
||
| 224 | 'sonata.post.admin.post', |
||
| 225 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 226 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 227 | ); |
||
| 228 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 229 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 230 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 231 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 232 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 233 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 234 | ); |
||
| 235 | $admin->addExtension($customExtension->reveal()); |
||
| 236 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 237 | |||
| 238 | $this->assertTrue($admin->hasAccess('custom_action')); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function testHasAccessAllowsAccessEditAction(): void |
||
| 242 | { |
||
| 243 | $admin = new PostAdmin( |
||
| 244 | 'sonata.post.admin.post', |
||
| 245 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 246 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 247 | ); |
||
| 248 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 249 | $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true); |
||
| 250 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 251 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 252 | ['edit_action' => ['EDIT_ROLE']] |
||
| 253 | ); |
||
| 254 | $admin->addExtension($customExtension->reveal()); |
||
| 255 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 256 | |||
| 257 | $this->assertTrue($admin->hasAccess('edit_action')); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild |
||
| 262 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild |
||
| 263 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild |
||
| 264 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild |
||
| 265 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren |
||
| 266 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren |
||
| 267 | */ |
||
| 268 | public function testChildren(): void |
||
| 269 | { |
||
| 270 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 271 | $this->assertFalse($postAdmin->hasChildren()); |
||
| 272 | $this->assertFalse($postAdmin->hasChild('comment')); |
||
| 273 | |||
| 274 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 275 | $postAdmin->addChild($commentAdmin, 'post'); |
||
| 276 | |||
| 277 | $this->assertTrue($postAdmin->hasChildren()); |
||
| 278 | $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment')); |
||
| 279 | |||
| 280 | $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode()); |
||
| 281 | $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute()); |
||
| 282 | $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent()); |
||
| 283 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
| 284 | |||
| 285 | $this->assertFalse($postAdmin->isChild()); |
||
| 286 | $this->assertTrue($commentAdmin->isChild()); |
||
| 287 | |||
| 288 | $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren()); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure |
||
| 293 | */ |
||
| 294 | public function testConfigure(): void |
||
| 295 | { |
||
| 296 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 297 | $this->assertNotNull($admin->getUniqid()); |
||
| 298 | |||
| 299 | $admin->initialize(); |
||
| 300 | $this->assertNotNull($admin->getUniqid()); |
||
| 301 | $this->assertSame('Post', $admin->getClassnameLabel()); |
||
| 302 | |||
| 303 | $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 304 | $admin->setClassnameLabel('postcomment'); |
||
| 305 | |||
| 306 | $admin->initialize(); |
||
| 307 | $this->assertSame('postcomment', $admin->getClassnameLabel()); |
||
| 308 | } |
||
| 309 | |||
| 310 | public function testConfigureWithValidParentAssociationMapping(): void |
||
| 311 | { |
||
| 312 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 313 | $admin->setParentAssociationMapping('Category'); |
||
| 314 | |||
| 315 | $admin->initialize(); |
||
| 316 | $this->assertSame('Category', $admin->getParentAssociationMapping()); |
||
| 317 | } |
||
| 318 | |||
| 319 | public function provideGetBaseRoutePattern() |
||
| 320 | { |
||
| 321 | return [ |
||
| 322 | [ |
||
| 323 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 324 | '/sonata/news/post', |
||
| 325 | ], |
||
| 326 | [ |
||
| 327 | 'Application\Sonata\NewsBundle\Document\Post', |
||
| 328 | '/sonata/news/post', |
||
| 329 | ], |
||
| 330 | [ |
||
| 331 | 'MyApplication\MyBundle\Entity\Post', |
||
| 332 | '/myapplication/my/post', |
||
| 333 | ], |
||
| 334 | [ |
||
| 335 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
| 336 | '/myapplication/my/post-category', |
||
| 337 | ], |
||
| 338 | [ |
||
| 339 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
| 340 | '/myapplication/my/product-category', |
||
| 341 | ], |
||
| 342 | [ |
||
| 343 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
| 344 | '/myapplication/my/other-product-category', |
||
| 345 | ], |
||
| 346 | [ |
||
| 347 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
| 348 | '/cmf/foo/menu', |
||
| 349 | ], |
||
| 350 | [ |
||
| 351 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
| 352 | '/cmf/foo/menu', |
||
| 353 | ], |
||
| 354 | [ |
||
| 355 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
| 356 | '/symfony/barbar/menu', |
||
| 357 | ], |
||
| 358 | [ |
||
| 359 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
| 360 | '/symfony/barbar/menu-item', |
||
| 361 | ], |
||
| 362 | [ |
||
| 363 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
| 364 | '/cmf/foo/menu', |
||
| 365 | ], |
||
| 366 | [ |
||
| 367 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
| 368 | '/cmf/foo/menu', |
||
| 369 | ], |
||
| 370 | [ |
||
| 371 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
| 372 | '/cmf/foo/menu', |
||
| 373 | ], |
||
| 374 | [ |
||
| 375 | 'AppBundle\Entity\User', |
||
| 376 | '/app/user', |
||
| 377 | ], |
||
| 378 | [ |
||
| 379 | 'App\Entity\User', |
||
| 380 | '/app/user', |
||
| 381 | ], |
||
| 382 | ]; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @dataProvider provideGetBaseRoutePattern |
||
| 387 | */ |
||
| 388 | public function testGetBaseRoutePattern(string $objFqn, string $expected): void |
||
| 389 | { |
||
| 390 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 391 | $this->assertSame($expected, $admin->getBaseRoutePattern()); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @dataProvider provideGetBaseRoutePattern |
||
| 396 | */ |
||
| 397 | public function testGetBaseRoutePatternWithChildAdmin(string $objFqn, string $expected): void |
||
| 398 | { |
||
| 399 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 400 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 401 | $commentAdmin->setParent($postAdmin); |
||
| 402 | |||
| 403 | $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern()); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @dataProvider provideGetBaseRoutePattern |
||
| 408 | */ |
||
| 409 | public function testGetBaseRoutePatternWithTwoNestedChildAdmin(string $objFqn, string $expected): void |
||
| 410 | { |
||
| 411 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 412 | $commentAdmin = new CommentAdmin( |
||
| 413 | 'sonata.post.admin.comment', |
||
| 414 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 415 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 416 | ); |
||
| 417 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 418 | 'sonata.post.admin.comment_vote', |
||
| 419 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 420 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 421 | ); |
||
| 422 | $commentAdmin->setParent($postAdmin); |
||
| 423 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 424 | |||
| 425 | $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern()); |
||
| 426 | } |
||
| 427 | |||
| 428 | public function testGetBaseRoutePatternWithSpecifedPattern(): void |
||
| 429 | { |
||
| 430 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostWithCustomRouteAdminController'); |
||
| 431 | |||
| 432 | $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern()); |
||
| 433 | } |
||
| 434 | |||
| 435 | public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void |
||
| 436 | { |
||
| 437 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 438 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'); |
||
| 439 | $commentAdmin->setParent($postAdmin); |
||
| 440 | |||
| 441 | $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern()); |
||
| 442 | } |
||
| 443 | |||
| 444 | public function testGetBaseRoutePatternWithUnreconizedClassname(): void |
||
| 445 | { |
||
| 446 | $this->expectException(\RuntimeException::class); |
||
| 447 | |||
| 448 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 449 | $admin->getBaseRoutePattern(); |
||
| 450 | } |
||
| 451 | |||
| 452 | public function provideGetBaseRouteName() |
||
| 453 | { |
||
| 454 | return [ |
||
| 455 | [ |
||
| 456 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 457 | 'admin_sonata_news_post', |
||
| 458 | ], |
||
| 459 | [ |
||
| 460 | 'Application\Sonata\NewsBundle\Document\Post', |
||
| 461 | 'admin_sonata_news_post', |
||
| 462 | ], |
||
| 463 | [ |
||
| 464 | 'MyApplication\MyBundle\Entity\Post', |
||
| 465 | 'admin_myapplication_my_post', |
||
| 466 | ], |
||
| 467 | [ |
||
| 468 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
| 469 | 'admin_myapplication_my_post_category', |
||
| 470 | ], |
||
| 471 | [ |
||
| 472 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
| 473 | 'admin_myapplication_my_product_category', |
||
| 474 | ], |
||
| 475 | [ |
||
| 476 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
| 477 | 'admin_myapplication_my_other_product_category', |
||
| 478 | ], |
||
| 479 | [ |
||
| 480 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
| 481 | 'admin_cmf_foo_menu', |
||
| 482 | ], |
||
| 483 | [ |
||
| 484 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
| 485 | 'admin_cmf_foo_menu', |
||
| 486 | ], |
||
| 487 | [ |
||
| 488 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
| 489 | 'admin_symfony_barbar_menu', |
||
| 490 | ], |
||
| 491 | [ |
||
| 492 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
| 493 | 'admin_symfony_barbar_menu_item', |
||
| 494 | ], |
||
| 495 | [ |
||
| 496 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
| 497 | 'admin_cmf_foo_menu', |
||
| 498 | ], |
||
| 499 | [ |
||
| 500 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
| 501 | 'admin_cmf_foo_menu', |
||
| 502 | ], |
||
| 503 | [ |
||
| 504 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
| 505 | 'admin_cmf_foo_menu', |
||
| 506 | ], |
||
| 507 | [ |
||
| 508 | 'AppBundle\Entity\User', |
||
| 509 | 'admin_app_user', |
||
| 510 | ], |
||
| 511 | [ |
||
| 512 | 'App\Entity\User', |
||
| 513 | 'admin_app_user', |
||
| 514 | ], |
||
| 515 | ]; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @dataProvider provideGetBaseRouteName |
||
| 520 | */ |
||
| 521 | public function testGetBaseRouteName(string $objFqn, string $expected): void |
||
| 522 | { |
||
| 523 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 524 | |||
| 525 | $this->assertSame($expected, $admin->getBaseRouteName()); |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @group legacy |
||
| 530 | * @expectedDeprecation Calling "addChild" without second argument is deprecated since sonata-project/admin-bundle 3.35 and will not be allowed in 4.0. |
||
| 531 | * @dataProvider provideGetBaseRouteName |
||
| 532 | */ |
||
| 533 | public function testGetBaseRouteNameWithChildAdmin(string $objFqn, string $expected): void |
||
| 534 | { |
||
| 535 | $routeGenerator = new DefaultRouteGenerator( |
||
| 536 | $this->createMock(RouterInterface::class), |
||
| 537 | new RoutesCache($this->cacheTempFolder, true) |
||
| 538 | ); |
||
| 539 | |||
| 540 | $container = new Container(); |
||
| 541 | $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
||
| 542 | |||
| 543 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
| 544 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 545 | $container->set('sonata.post.admin.post', $postAdmin); |
||
| 546 | $postAdmin->setConfigurationPool($pool); |
||
| 547 | $postAdmin->setRouteBuilder($pathInfo); |
||
| 548 | $postAdmin->setRouteGenerator($routeGenerator); |
||
| 549 | $postAdmin->initialize(); |
||
| 550 | |||
| 551 | $commentAdmin = new CommentAdmin( |
||
| 552 | 'sonata.post.admin.comment', |
||
| 553 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 554 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 555 | ); |
||
| 556 | $container->set('sonata.post.admin.comment', $commentAdmin); |
||
| 557 | $commentAdmin->setConfigurationPool($pool); |
||
| 558 | $commentAdmin->setRouteBuilder($pathInfo); |
||
| 559 | $commentAdmin->setRouteGenerator($routeGenerator); |
||
| 560 | $commentAdmin->initialize(); |
||
| 561 | |||
| 562 | $postAdmin->addChild($commentAdmin, 'post'); |
||
| 563 | |||
| 564 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 565 | 'sonata.post.admin.comment_vote', |
||
| 566 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 567 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 568 | ); |
||
| 569 | |||
| 570 | $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin); |
||
| 571 | $commentVoteAdmin->setConfigurationPool($pool); |
||
| 572 | $commentVoteAdmin->setRouteBuilder($pathInfo); |
||
| 573 | $commentVoteAdmin->setRouteGenerator($routeGenerator); |
||
| 574 | $commentVoteAdmin->initialize(); |
||
| 575 | |||
| 576 | $commentAdmin->addChild($commentVoteAdmin); |
||
| 577 | $pool->setAdminServiceIds([ |
||
| 578 | 'sonata.post.admin.post', |
||
| 579 | 'sonata.post.admin.comment', |
||
| 580 | 'sonata.post.admin.comment_vote', |
||
| 581 | ]); |
||
| 582 | |||
| 583 | $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName()); |
||
| 584 | |||
| 585 | $this->assertTrue($postAdmin->hasRoute('show')); |
||
| 586 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show')); |
||
| 587 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show')); |
||
| 588 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show')); |
||
| 589 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list')); |
||
| 590 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list')); |
||
| 591 | $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit')); |
||
| 592 | $this->assertFalse($commentAdmin->hasRoute('edit')); |
||
| 593 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
| 594 | |||
| 595 | /* |
||
| 596 | * Test the route name from request |
||
| 597 | */ |
||
| 598 | $postListRequest = new Request( |
||
| 599 | [], |
||
| 600 | [], |
||
| 601 | [ |
||
| 602 | '_route' => $postAdmin->getBaseRouteName().'_list', |
||
| 603 | ] |
||
| 604 | ); |
||
| 605 | |||
| 606 | $postAdmin->setRequest($postListRequest); |
||
| 607 | $commentAdmin->setRequest($postListRequest); |
||
| 608 | |||
| 609 | $this->assertTrue($postAdmin->isCurrentRoute('list')); |
||
| 610 | $this->assertFalse($postAdmin->isCurrentRoute('create')); |
||
| 611 | $this->assertFalse($commentAdmin->isCurrentRoute('list')); |
||
| 612 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('list')); |
||
| 613 | $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
| 614 | $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
| 615 | $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
| 616 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
| 617 | } |
||
| 618 | |||
| 619 | public function testGetBaseRouteNameWithUnreconizedClassname(): void |
||
| 620 | { |
||
| 621 | $this->expectException(\RuntimeException::class); |
||
| 622 | |||
| 623 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 624 | $admin->getBaseRouteName(); |
||
| 625 | } |
||
| 626 | |||
| 627 | public function testGetBaseRouteNameWithSpecifiedName(): void |
||
| 628 | { |
||
| 629 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 630 | |||
| 631 | $this->assertSame('post_custom', $postAdmin->getBaseRouteName()); |
||
| 632 | } |
||
| 633 | |||
| 634 | public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void |
||
| 635 | { |
||
| 636 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 637 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'); |
||
| 638 | $commentAdmin->setParent($postAdmin); |
||
| 639 | |||
| 640 | $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName()); |
||
| 641 | } |
||
| 642 | |||
| 643 | public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void |
||
| 644 | { |
||
| 645 | $postAdmin = new PostAdmin( |
||
| 646 | 'sonata.post.admin.post', |
||
| 647 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 648 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 649 | ); |
||
| 650 | $commentAdmin = new CommentWithCustomRouteAdmin( |
||
| 651 | 'sonata.post.admin.comment_with_custom_route', |
||
| 652 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 653 | 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController' |
||
| 654 | ); |
||
| 655 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 656 | 'sonata.post.admin.comment_vote', |
||
| 657 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 658 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 659 | ); |
||
| 660 | $commentAdmin->setParent($postAdmin); |
||
| 661 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 662 | |||
| 663 | $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName()); |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid |
||
| 668 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid |
||
| 669 | */ |
||
| 670 | public function testSetUniqid(): void |
||
| 671 | { |
||
| 672 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 673 | |||
| 674 | $uniqid = uniqid(); |
||
| 675 | $admin->setUniqid($uniqid); |
||
| 676 | |||
| 677 | $this->assertSame($uniqid, $admin->getUniqid()); |
||
| 678 | } |
||
| 679 | |||
| 680 | public function testToString(): void |
||
| 681 | { |
||
| 682 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 683 | |||
| 684 | $s = new \stdClass(); |
||
| 685 | |||
| 686 | $this->assertNotEmpty($admin->toString($s)); |
||
| 687 | |||
| 688 | $s = new FooToString(); |
||
| 689 | $this->assertSame('salut', $admin->toString($s)); |
||
| 690 | |||
| 691 | // To string method is implemented, but returns null |
||
| 692 | $s = new FooToStringNull(); |
||
| 693 | $this->assertNotEmpty($admin->toString($s)); |
||
| 694 | |||
| 695 | $this->assertSame('', $admin->toString(false)); |
||
|
|
|||
| 696 | } |
||
| 697 | |||
| 698 | public function testIsAclEnabled(): void |
||
| 699 | { |
||
| 700 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 701 | |||
| 702 | $this->assertFalse($postAdmin->isAclEnabled()); |
||
| 703 | |||
| 704 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 705 | $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class)); |
||
| 706 | $this->assertTrue($commentAdmin->isAclEnabled()); |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses |
||
| 711 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass |
||
| 712 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses |
||
| 713 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass |
||
| 714 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 715 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass |
||
| 716 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode |
||
| 717 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass |
||
| 718 | */ |
||
| 719 | public function testSubClass(): void |
||
| 720 | { |
||
| 721 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
| 722 | $admin = new PostAdmin( |
||
| 723 | 'sonata.post.admin.post', |
||
| 724 | Post::class, |
||
| 725 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 726 | ); |
||
| 727 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 728 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 729 | $this->assertCount(0, $admin->getSubClasses()); |
||
| 730 | $this->assertSame(Post::class, $admin->getClass()); |
||
| 731 | |||
| 732 | // Just for the record, if there is no inheritance set, the getSubject is not used |
||
| 733 | // the getSubject can also lead to some issue |
||
| 734 | $admin->setSubject(new BlogPost()); |
||
| 735 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 736 | |||
| 737 | $admin->setSubClasses([ |
||
| 738 | 'extended1' => 'NewsBundle\Entity\PostExtended1', |
||
| 739 | 'extended2' => 'NewsBundle\Entity\PostExtended2', |
||
| 740 | ]); |
||
| 741 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 742 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 743 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 744 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 745 | $this->assertSame( |
||
| 746 | BlogPost::class, |
||
| 747 | $admin->getClass(), |
||
| 748 | 'When there is no subclass in the query the class parameter should be returned' |
||
| 749 | ); |
||
| 750 | |||
| 751 | $request = new Request(['subclass' => 'extended1']); |
||
| 752 | $admin->setRequest($request); |
||
| 753 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 754 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 755 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 756 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 757 | $this->assertSame( |
||
| 758 | 'NewsBundle\Entity\PostExtended1', |
||
| 759 | $admin->getActiveSubClass(), |
||
| 760 | 'It should return the curently active sub class.' |
||
| 761 | ); |
||
| 762 | $this->assertSame('extended1', $admin->getActiveSubclassCode()); |
||
| 763 | $this->assertSame( |
||
| 764 | 'NewsBundle\Entity\PostExtended1', |
||
| 765 | $admin->getClass(), |
||
| 766 | 'getClass() should return the name of the sub class when passed through a request query parameter.' |
||
| 767 | ); |
||
| 768 | |||
| 769 | $request->query->set('subclass', 'inject'); |
||
| 770 | |||
| 771 | $this->expectException(\LogicException::class); |
||
| 772 | $this->expectExceptionMessage(sprintf('Admin "%s" has no active subclass.', PostAdmin::class)); |
||
| 773 | |||
| 774 | $admin->getActiveSubclassCode(); |
||
| 775 | } |
||
| 776 | |||
| 777 | public function testNonExistantSubclass(): void |
||
| 778 | { |
||
| 779 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 780 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
| 781 | |||
| 782 | $admin->setRequest(new Request(['subclass' => 'inject'])); |
||
| 783 | |||
| 784 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
| 785 | |||
| 786 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 787 | |||
| 788 | $this->expectException(\LogicException::class); |
||
| 789 | |||
| 790 | $admin->getActiveSubClass(); |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 795 | */ |
||
| 796 | public function testOnlyOneSubclassNeededToBeActive(): void |
||
| 797 | { |
||
| 798 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 799 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']); |
||
| 800 | $request = new Request(['subclass' => 'extended1']); |
||
| 801 | $admin->setRequest($request); |
||
| 802 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @group legacy |
||
| 807 | * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0. |
||
| 808 | */ |
||
| 809 | public function testAddSubClassIsDeprecated(): void |
||
| 810 | { |
||
| 811 | $admin = new PostAdmin( |
||
| 812 | 'sonata.post.admin.post', |
||
| 813 | Post::class, |
||
| 814 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 815 | ); |
||
| 816 | $admin->addSubClass('whatever'); |
||
| 817 | } |
||
| 818 | |||
| 819 | /** |
||
| 820 | * @group legacy |
||
| 821 | */ |
||
| 822 | public function testGetPerPageOptions(): void |
||
| 835 | |||
| 836 | public function testGetLabelTranslatorStrategy(): void |
||
| 846 | |||
| 847 | public function testGetRouteBuilder(): void |
||
| 857 | |||
| 858 | public function testGetMenuFactory(): void |
||
| 868 | |||
| 869 | public function testGetExtensions(): void |
||
| 882 | |||
| 883 | public function testGetFilterTheme(): void |
||
| 892 | |||
| 893 | public function testGetFormTheme(): void |
||
| 903 | |||
| 904 | public function testGetValidator(): void |
||
| 915 | |||
| 916 | public function testGetSecurityHandler(): void |
||
| 926 | |||
| 927 | public function testGetSecurityInformation(): void |
||
| 941 | |||
| 942 | public function testGetManagerType(): void |
||
| 951 | |||
| 952 | public function testGetModelManager(): void |
||
| 963 | |||
| 964 | public function testGetBaseCodeRoute(): void |
||
| 986 | |||
| 987 | public function testGetRouteGenerator(): void |
||
| 998 | |||
| 999 | public function testGetConfigurationPool(): void |
||
| 1012 | |||
| 1013 | public function testGetShowBuilder(): void |
||
| 1024 | |||
| 1025 | public function testGetListBuilder(): void |
||
| 1036 | |||
| 1037 | public function testGetDatagridBuilder(): void |
||
| 1048 | |||
| 1049 | public function testGetFormContractor(): void |
||
| 1060 | |||
| 1061 | public function testGetRequest(): void |
||
| 1073 | |||
| 1074 | public function testGetRequestWithException(): void |
||
| 1082 | |||
| 1083 | public function testGetTranslationDomain(): void |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * @group legacy |
||
| 1095 | */ |
||
| 1096 | public function testGetTranslator(): void |
||
| 1107 | |||
| 1108 | public function testGetShowGroups(): void |
||
| 1119 | |||
| 1120 | public function testGetFormGroups(): void |
||
| 1131 | |||
| 1132 | public function testGetMaxPageLinks(): void |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * @group legacy |
||
| 1144 | */ |
||
| 1145 | public function testGetMaxPerPage(): void |
||
| 1154 | |||
| 1155 | public function testGetLabel(): void |
||
| 1164 | |||
| 1165 | public function testGetBaseController(): void |
||
| 1174 | |||
| 1175 | public function testGetIdParameter(): void |
||
| 1206 | |||
| 1207 | public function testGetExportFormats(): void |
||
| 1213 | |||
| 1214 | public function testGetUrlsafeIdentifier(): void |
||
| 1229 | |||
| 1230 | public function testDeterminedPerPageValue(): void |
||
| 1231 | { |
||
| 1232 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1233 | |||
| 1234 | $this->assertFalse($admin->determinedPerPageValue(123)); |
||
| 1235 | $this->assertTrue($admin->determinedPerPageValue(16)); |
||
| 1236 | } |
||
| 1237 | |||
| 1238 | public function testIsGranted(): void |
||
| 1239 | { |
||
| 1240 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1241 | $modelManager = $this->createStub(ModelManagerInterface::class); |
||
| 1242 | $modelManager |
||
| 1243 | ->method('getNormalizedIdentifier') |
||
| 1244 | ->willReturnCallback(static function (?object $model = null): ?string { |
||
| 1245 | return $model ? $model->id : null; |
||
| 1246 | }); |
||
| 1247 | |||
| 1248 | $admin->setModelManager($modelManager); |
||
| 1249 | |||
| 1250 | $entity1 = new \stdClass(); |
||
| 1251 | $entity1->id = '1'; |
||
| 1252 | |||
| 1253 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1254 | $securityHandler |
||
| 1255 | ->expects($this->exactly(6)) |
||
| 1256 | ->method('isGranted') |
||
| 1257 | ->willReturnCallback(static function ( |
||
| 1258 | AdminInterface $adminIn, |
||
| 1259 | string $attributes, |
||
| 1260 | ?object $object = null |
||
| 1261 | ) use ( |
||
| 1262 | $admin, |
||
| 1263 | $entity1 |
||
| 1264 | ): bool { |
||
| 1265 | return $admin === $adminIn && 'FOO' === $attributes && |
||
| 1266 | ($object === $admin || $object === $entity1); |
||
| 1267 | }); |
||
| 1268 | |||
| 1269 | $admin->setSecurityHandler($securityHandler); |
||
| 1270 | |||
| 1271 | $this->assertTrue($admin->isGranted('FOO')); |
||
| 1272 | $this->assertTrue($admin->isGranted('FOO')); |
||
| 1273 | $this->assertTrue($admin->isGranted('FOO', $entity1)); |
||
| 1274 | $this->assertTrue($admin->isGranted('FOO', $entity1)); |
||
| 1275 | $this->assertFalse($admin->isGranted('BAR')); |
||
| 1276 | $this->assertFalse($admin->isGranted('BAR')); |
||
| 1277 | $this->assertFalse($admin->isGranted('BAR', $entity1)); |
||
| 1278 | $this->assertFalse($admin->isGranted('BAR', $entity1)); |
||
| 1279 | |||
| 1280 | $entity2 = new \stdClass(); |
||
| 1281 | $entity2->id = '2'; |
||
| 1282 | |||
| 1283 | $this->assertFalse($admin->isGranted('BAR', $entity2)); |
||
| 1284 | $this->assertFalse($admin->isGranted('BAR', $entity2)); |
||
| 1285 | |||
| 1286 | $entity3 = new \stdClass(); |
||
| 1287 | $entity3->id = '3'; |
||
| 1288 | |||
| 1289 | $this->assertFalse($admin->isGranted('BAR', $entity3)); |
||
| 1290 | $this->assertFalse($admin->isGranted('BAR', $entity3)); |
||
| 1291 | } |
||
| 1292 | |||
| 1293 | public function testSupportsPreviewMode(): void |
||
| 1294 | { |
||
| 1295 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1296 | |||
| 1297 | $this->assertFalse($admin->supportsPreviewMode()); |
||
| 1298 | } |
||
| 1299 | |||
| 1300 | public function testGetPermissionsShow(): void |
||
| 1301 | { |
||
| 1302 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1303 | |||
| 1304 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1305 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU)); |
||
| 1306 | $this->assertSame(['LIST'], $admin->getPermissionsShow('foo')); |
||
| 1307 | } |
||
| 1308 | |||
| 1309 | public function testShowIn(): void |
||
| 1310 | { |
||
| 1311 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1312 | |||
| 1313 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1314 | $securityHandler |
||
| 1315 | ->method('isGranted') |
||
| 1316 | ->willReturnCallback(static function (AdminInterface $adminIn, array $attributes, $object = null) use ($admin): bool { |
||
| 1317 | return $admin === $adminIn && $attributes === ['LIST']; |
||
| 1318 | }); |
||
| 1319 | |||
| 1320 | $admin->setSecurityHandler($securityHandler); |
||
| 1321 | |||
| 1322 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1323 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU)); |
||
| 1324 | $this->assertTrue($admin->showIn('foo')); |
||
| 1325 | } |
||
| 1326 | |||
| 1327 | public function testGetObjectIdentifier(): void |
||
| 1328 | { |
||
| 1329 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1330 | |||
| 1331 | $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier()); |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | /** |
||
| 1335 | * @group legacy |
||
| 1336 | */ |
||
| 1337 | public function testTrans(): void |
||
| 1338 | { |
||
| 1339 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1340 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1341 | |||
| 1342 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1343 | $admin->setTranslator($translator); |
||
| 1344 | |||
| 1345 | $translator->expects($this->once()) |
||
| 1346 | ->method('trans') |
||
| 1347 | ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1348 | ->willReturn('fooTranslated'); |
||
| 1349 | |||
| 1350 | $this->assertSame('fooTranslated', $admin->trans('foo')); |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | /** |
||
| 1354 | * @group legacy |
||
| 1355 | */ |
||
| 1356 | public function testTransWithMessageDomain(): void |
||
| 1357 | { |
||
| 1358 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1359 | |||
| 1360 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1361 | $admin->setTranslator($translator); |
||
| 1362 | |||
| 1363 | $translator->expects($this->once()) |
||
| 1364 | ->method('trans') |
||
| 1365 | ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1366 | ->willReturn('fooTranslated'); |
||
| 1367 | |||
| 1368 | $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1369 | } |
||
| 1370 | |||
| 1371 | /** |
||
| 1372 | * @group legacy |
||
| 1373 | */ |
||
| 1374 | public function testTransChoice(): void |
||
| 1375 | { |
||
| 1376 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1377 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1378 | |||
| 1379 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1380 | $admin->setTranslator($translator); |
||
| 1381 | |||
| 1382 | $translator->expects($this->once()) |
||
| 1383 | ->method('transChoice') |
||
| 1384 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1385 | ->willReturn('fooTranslated'); |
||
| 1386 | |||
| 1387 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2)); |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | /** |
||
| 1391 | * @group legacy |
||
| 1392 | */ |
||
| 1393 | public function testTransChoiceWithMessageDomain(): void |
||
| 1394 | { |
||
| 1395 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1396 | |||
| 1397 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1398 | $admin->setTranslator($translator); |
||
| 1399 | |||
| 1400 | $translator->expects($this->once()) |
||
| 1401 | ->method('transChoice') |
||
| 1402 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1403 | ->willReturn('fooTranslated'); |
||
| 1404 | |||
| 1405 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1406 | } |
||
| 1407 | |||
| 1408 | public function testSetFilterPersister(): void |
||
| 1409 | { |
||
| 1410 | $admin = new class('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle\Controller\PostAdminController') extends PostAdmin { |
||
| 1411 | public function persistFilters(): bool |
||
| 1415 | }; |
||
| 1416 | |||
| 1417 | $filterPersister = $this->createMock(FilterPersisterInterface::class); |
||
| 1418 | |||
| 1419 | $admin->setFilterPersister($filterPersister); |
||
| 1420 | $this->assertTrue($admin->persistFilters()); |
||
| 1421 | } |
||
| 1422 | |||
| 1423 | public function testGetRootCode(): void |
||
| 1424 | { |
||
| 1425 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1426 | |||
| 1427 | $this->assertSame('sonata.post.admin.post', $admin->getRootCode()); |
||
| 1428 | |||
| 1429 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController'); |
||
| 1430 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
| 1431 | $parentFieldDescription->expects($this->once()) |
||
| 1432 | ->method('getAdmin') |
||
| 1433 | ->willReturn($parentAdmin); |
||
| 1434 | |||
| 1435 | $this->assertFalse($admin->hasParentFieldDescription()); |
||
| 1436 | $admin->setParentFieldDescription($parentFieldDescription); |
||
| 1437 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
| 1438 | $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode()); |
||
| 1439 | } |
||
| 1440 | |||
| 1441 | public function testGetRoot(): void |
||
| 1442 | { |
||
| 1443 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1444 | |||
| 1445 | $this->assertSame($admin, $admin->getRoot()); |
||
| 1446 | |||
| 1447 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController'); |
||
| 1448 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
| 1449 | $parentFieldDescription->expects($this->once()) |
||
| 1450 | ->method('getAdmin') |
||
| 1451 | ->willReturn($parentAdmin); |
||
| 1452 | |||
| 1453 | $this->assertFalse($admin->hasParentFieldDescription()); |
||
| 1454 | $admin->setParentFieldDescription($parentFieldDescription); |
||
| 1455 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
| 1456 | $this->assertSame($parentAdmin, $admin->getRoot()); |
||
| 1457 | } |
||
| 1458 | |||
| 1459 | public function testGetExportFields(): void |
||
| 1460 | { |
||
| 1461 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1462 | |||
| 1472 | |||
| 1473 | public function testGetPersistentParametersWithNoExtension(): void |
||
| 1479 | |||
| 1480 | public function testGetPersistentParametersWithValidExtension(): void |
||
| 1495 | |||
| 1496 | public function testGetFormWithNonCollectionParentValue(): void |
||
| 1506 | |||
| 1507 | public function testGetFormWithCollectionParentValue(): void |
||
| 1527 | |||
| 1528 | public function testGetFormWithArrayParentValue(): void |
||
| 1548 | |||
| 1549 | public function testFormAddPostSubmitEventForPreValidation(): void |
||
| 1611 | |||
| 1612 | public function testRemoveFieldFromFormGroup(): void |
||
| 1638 | |||
| 1639 | public function testGetFilterParameters(): void |
||
| 1679 | |||
| 1680 | public function testGetFilterFieldDescription(): void |
||
| 1751 | |||
| 1752 | public function testGetSubjectNoRequest(): void |
||
| 1764 | |||
| 1765 | public function testGetSideMenu(): void |
||
| 1789 | |||
| 1790 | /** |
||
| 1791 | * @return array |
||
| 1792 | */ |
||
| 1793 | public function provideGetSubject() |
||
| 1803 | |||
| 1804 | /** |
||
| 1805 | * @dataProvider provideGetSubject |
||
| 1806 | */ |
||
| 1807 | public function testGetSubjectFailed($id): void |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * @dataProvider provideGetSubject |
||
| 1825 | */ |
||
| 1826 | public function testGetSubject($id): void |
||
| 1845 | |||
| 1846 | public function testGetSubjectWithParentDescription(): void |
||
| 1875 | |||
| 1876 | /** |
||
| 1877 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 1878 | */ |
||
| 1879 | public function testGetActionButtonsList(): void |
||
| 1912 | |||
| 1913 | /** |
||
| 1914 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 1915 | */ |
||
| 1916 | public function testGetActionButtonsListCreateDisabled(): void |
||
| 1930 | |||
| 1931 | /** |
||
| 1932 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions |
||
| 1933 | */ |
||
| 1934 | public function testGetBatchActions(): void |
||
| 1988 | |||
| 1989 | /** |
||
| 1990 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 1991 | */ |
||
| 1992 | public function testShowMosaicButton(): void |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 2004 | */ |
||
| 2005 | public function testShowMosaicButtonHideMosaic(): void |
||
| 2015 | |||
| 2016 | /** |
||
| 2017 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions |
||
| 2018 | * @dataProvider provideGetBaseRouteName |
||
| 2019 | */ |
||
| 2020 | public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void |
||
| 2054 | |||
| 2055 | public function testDefaultFilters(): void |
||
| 2114 | |||
| 2115 | /** |
||
| 2116 | * NEXT_MAJOR: remove this method. |
||
| 2117 | * |
||
| 2118 | * @group legacy |
||
| 2119 | */ |
||
| 2120 | public function testCreateQueryLegacyCallWorks(): void |
||
| 2135 | |||
| 2136 | public function testGetDataSourceIterator(): void |
||
| 2180 | |||
| 2181 | public function testCircularChildAdmin(): void |
||
| 2201 | |||
| 2202 | public function testCircularChildAdminTripleLevel(): void |
||
| 2228 | |||
| 2229 | public function testCircularChildAdminWithItself(): void |
||
| 2243 | |||
| 2244 | public function testGetRootAncestor(): void |
||
| 2278 | |||
| 2279 | public function testGetChildDepth(): void |
||
| 2313 | |||
| 2314 | public function testGetCurrentLeafChildAdmin(): void |
||
| 2351 | |||
| 2352 | public function testAdminAvoidInifiniteLoop(): void |
||
| 2383 | |||
| 2384 | private function createTagAdmin(Post $post): TagAdmin |
||
| 2423 | } |
||
| 2424 |
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: