| Total Complexity | 53 |
| Total Lines | 2595 |
| 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 |
||
| 90 | class AdminTest extends TestCase |
||
| 91 | { |
||
| 92 | protected $cacheTempFolder; |
||
| 93 | |||
| 94 | protected function setUp(): void |
||
| 95 | { |
||
| 96 | $this->cacheTempFolder = sprintf('%s/sonata_test_route', sys_get_temp_dir()); |
||
| 97 | $filesystem = new Filesystem(); |
||
| 98 | $filesystem->remove($this->cacheTempFolder); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct |
||
| 103 | */ |
||
| 104 | public function testConstructor(): void |
||
| 105 | { |
||
| 106 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
| 107 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 108 | |||
| 109 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 110 | $this->assertInstanceOf(AbstractAdmin::class, $admin); |
||
| 111 | $this->assertSame($class, $admin->getClass()); |
||
| 112 | $this->assertSame($baseControllerName, $admin->getBaseControllerName()); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function testGetClass(): void |
||
| 116 | { |
||
| 117 | $class = Post::class; |
||
| 118 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 119 | |||
| 120 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 121 | |||
| 122 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
| 123 | |||
| 124 | $admin->setSubject(new BlogPost()); |
||
| 125 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 126 | |||
| 127 | $admin->setSubClasses(['foo']); |
||
| 128 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 129 | |||
| 130 | $admin->setSubject(null); |
||
| 131 | $admin->setSubClasses([]); |
||
| 132 | $this->assertSame($class, $admin->getClass()); |
||
| 133 | |||
| 134 | $admin->setSubClasses(['foo' => 'bar']); |
||
| 135 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
| 136 | $this->assertSame('bar', $admin->getClass()); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testGetClassException(): void |
||
| 140 | { |
||
| 141 | $this->expectException(\RuntimeException::class); |
||
| 142 | $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass'); |
||
| 143 | |||
| 144 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
| 145 | $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController'; |
||
| 146 | |||
| 147 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
| 148 | $admin->setParentFieldDescription(new FieldDescription()); |
||
| 149 | $admin->setSubClasses(['foo' => 'bar']); |
||
| 150 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
| 151 | $admin->getClass(); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function testCheckAccessThrowsExceptionOnMadeUpAction(): void |
||
| 155 | { |
||
| 156 | $admin = new PostAdmin( |
||
| 157 | 'sonata.post.admin.post', |
||
| 158 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 159 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 160 | ); |
||
| 161 | $this->expectException( |
||
| 162 | \InvalidArgumentException::class |
||
| 163 | ); |
||
| 164 | $this->expectExceptionMessage( |
||
| 165 | 'Action "made-up" could not be found' |
||
| 166 | ); |
||
| 167 | $admin->checkAccess('made-up'); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function testCheckAccessThrowsAccessDeniedException(): void |
||
| 171 | { |
||
| 172 | $admin = new PostAdmin( |
||
| 173 | 'sonata.post.admin.post', |
||
| 174 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 175 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 176 | ); |
||
| 177 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 178 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 179 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
| 180 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 181 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 182 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 183 | ); |
||
| 184 | $admin->addExtension($customExtension->reveal()); |
||
| 185 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 186 | $this->expectException( |
||
| 187 | AccessDeniedException::class |
||
| 188 | ); |
||
| 189 | $this->expectExceptionMessage( |
||
| 190 | 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE' |
||
| 191 | ); |
||
| 192 | $admin->checkAccess('custom_action'); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function testHasAccessOnMadeUpAction(): void |
||
| 196 | { |
||
| 197 | $admin = new PostAdmin( |
||
| 198 | 'sonata.post.admin.post', |
||
| 199 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 200 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 201 | ); |
||
| 202 | |||
| 203 | $this->assertFalse($admin->hasAccess('made-up')); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function testHasAccess(): void |
||
| 207 | { |
||
| 208 | $admin = new PostAdmin( |
||
| 209 | 'sonata.post.admin.post', |
||
| 210 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 211 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 212 | ); |
||
| 213 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 214 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 215 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
| 216 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 217 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 218 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 219 | ); |
||
| 220 | $admin->addExtension($customExtension->reveal()); |
||
| 221 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 222 | |||
| 223 | $this->assertFalse($admin->hasAccess('custom_action')); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function testHasAccessAllowsAccess(): void |
||
| 227 | { |
||
| 228 | $admin = new PostAdmin( |
||
| 229 | 'sonata.post.admin.post', |
||
| 230 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 231 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 232 | ); |
||
| 233 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 234 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 235 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true); |
||
| 236 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 237 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 238 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
| 239 | ); |
||
| 240 | $admin->addExtension($customExtension->reveal()); |
||
| 241 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 242 | |||
| 243 | $this->assertTrue($admin->hasAccess('custom_action')); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function testHasAccessAllowsAccessEditAction(): void |
||
| 247 | { |
||
| 248 | $admin = new PostAdmin( |
||
| 249 | 'sonata.post.admin.post', |
||
| 250 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 251 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 252 | ); |
||
| 253 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
| 254 | $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true); |
||
| 255 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
| 256 | $customExtension->getAccessMapping($admin)->willReturn( |
||
| 257 | ['edit_action' => ['EDIT_ROLE']] |
||
| 258 | ); |
||
| 259 | $admin->addExtension($customExtension->reveal()); |
||
| 260 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
| 261 | |||
| 262 | $this->assertTrue($admin->hasAccess('edit_action')); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild |
||
| 267 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild |
||
| 268 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild |
||
| 269 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild |
||
| 270 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren |
||
| 271 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren |
||
| 272 | */ |
||
| 273 | public function testChildren(): void |
||
| 274 | { |
||
| 275 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 276 | $this->assertFalse($postAdmin->hasChildren()); |
||
| 277 | $this->assertFalse($postAdmin->hasChild('comment')); |
||
| 278 | |||
| 279 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 280 | $postAdmin->addChild($commentAdmin, 'post'); |
||
| 281 | |||
| 282 | $this->assertTrue($postAdmin->hasChildren()); |
||
| 283 | $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment')); |
||
| 284 | |||
| 285 | $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode()); |
||
| 286 | $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute()); |
||
| 287 | $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent()); |
||
| 288 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
| 289 | |||
| 290 | $this->assertFalse($postAdmin->isChild()); |
||
| 291 | $this->assertTrue($commentAdmin->isChild()); |
||
| 292 | |||
| 293 | $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren()); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure |
||
| 298 | */ |
||
| 299 | public function testConfigure(): void |
||
| 300 | { |
||
| 301 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 302 | $this->assertNotNull($admin->getUniqid()); |
||
| 303 | |||
| 304 | $admin->initialize(); |
||
| 305 | $this->assertNotNull($admin->getUniqid()); |
||
| 306 | $this->assertSame('Post', $admin->getClassnameLabel()); |
||
| 307 | |||
| 308 | $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 309 | $admin->setClassnameLabel('postcomment'); |
||
| 310 | |||
| 311 | $admin->initialize(); |
||
| 312 | $this->assertSame('postcomment', $admin->getClassnameLabel()); |
||
| 313 | } |
||
| 314 | |||
| 315 | public function testConfigureWithValidParentAssociationMapping(): void |
||
| 316 | { |
||
| 317 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 318 | $admin->setParentAssociationMapping('Category'); |
||
| 319 | |||
| 320 | $admin->initialize(); |
||
| 321 | $this->assertSame('Category', $admin->getParentAssociationMapping()); |
||
| 322 | } |
||
| 323 | |||
| 324 | public function provideGetBaseRoutePattern() |
||
| 325 | { |
||
| 326 | return [ |
||
| 327 | [ |
||
| 328 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 329 | '/sonata/news/post', |
||
| 330 | ], |
||
| 331 | [ |
||
| 332 | 'Application\Sonata\NewsBundle\Document\Post', |
||
| 333 | '/sonata/news/post', |
||
| 334 | ], |
||
| 335 | [ |
||
| 336 | 'MyApplication\MyBundle\Entity\Post', |
||
| 337 | '/myapplication/my/post', |
||
| 338 | ], |
||
| 339 | [ |
||
| 340 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
| 341 | '/myapplication/my/post-category', |
||
| 342 | ], |
||
| 343 | [ |
||
| 344 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
| 345 | '/myapplication/my/product-category', |
||
| 346 | ], |
||
| 347 | [ |
||
| 348 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
| 349 | '/myapplication/my/other-product-category', |
||
| 350 | ], |
||
| 351 | [ |
||
| 352 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
| 353 | '/cmf/foo/menu', |
||
| 354 | ], |
||
| 355 | [ |
||
| 356 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
| 357 | '/cmf/foo/menu', |
||
| 358 | ], |
||
| 359 | [ |
||
| 360 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
| 361 | '/symfony/barbar/menu', |
||
| 362 | ], |
||
| 363 | [ |
||
| 364 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
| 365 | '/symfony/barbar/menu-item', |
||
| 366 | ], |
||
| 367 | [ |
||
| 368 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
| 369 | '/cmf/foo/menu', |
||
| 370 | ], |
||
| 371 | [ |
||
| 372 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
| 373 | '/cmf/foo/menu', |
||
| 374 | ], |
||
| 375 | [ |
||
| 376 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
| 377 | '/cmf/foo/menu', |
||
| 378 | ], |
||
| 379 | [ |
||
| 380 | 'AppBundle\Entity\User', |
||
| 381 | '/app/user', |
||
| 382 | ], |
||
| 383 | [ |
||
| 384 | 'App\Entity\User', |
||
| 385 | '/app/user', |
||
| 386 | ], |
||
| 387 | ]; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @dataProvider provideGetBaseRoutePattern |
||
| 392 | */ |
||
| 393 | public function testGetBaseRoutePattern(string $objFqn, string $expected): void |
||
| 394 | { |
||
| 395 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 396 | $this->assertSame($expected, $admin->getBaseRoutePattern()); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @dataProvider provideGetBaseRoutePattern |
||
| 401 | */ |
||
| 402 | public function testGetBaseRoutePatternWithChildAdmin(string $objFqn, string $expected): void |
||
| 403 | { |
||
| 404 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 405 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 406 | $commentAdmin->setParent($postAdmin); |
||
| 407 | |||
| 408 | $this->assertSame(sprintf('%s/{id}/comment', $expected), $commentAdmin->getBaseRoutePattern()); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @dataProvider provideGetBaseRoutePattern |
||
| 413 | */ |
||
| 414 | public function testGetBaseRoutePatternWithTwoNestedChildAdmin(string $objFqn, string $expected): void |
||
| 415 | { |
||
| 416 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 417 | $commentAdmin = new CommentAdmin( |
||
| 418 | 'sonata.post.admin.comment', |
||
| 419 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 420 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 421 | ); |
||
| 422 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 423 | 'sonata.post.admin.comment_vote', |
||
| 424 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 425 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 426 | ); |
||
| 427 | $commentAdmin->setParent($postAdmin); |
||
| 428 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 429 | |||
| 430 | $this->assertSame(sprintf('%s/{id}/comment/{childId}/commentvote', $expected), $commentVoteAdmin->getBaseRoutePattern()); |
||
| 431 | } |
||
| 432 | |||
| 433 | public function testGetBaseRoutePatternWithSpecifedPattern(): void |
||
| 434 | { |
||
| 435 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostWithCustomRouteAdminController'); |
||
| 436 | |||
| 437 | $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern()); |
||
| 438 | } |
||
| 439 | |||
| 440 | public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void |
||
| 441 | { |
||
| 442 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 443 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'); |
||
| 444 | $commentAdmin->setParent($postAdmin); |
||
| 445 | |||
| 446 | $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern()); |
||
| 447 | } |
||
| 448 | |||
| 449 | public function testGetBaseRoutePatternWithUnreconizedClassname(): void |
||
| 450 | { |
||
| 451 | $this->expectException(\RuntimeException::class); |
||
| 452 | |||
| 453 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 454 | $admin->getBaseRoutePattern(); |
||
| 455 | } |
||
| 456 | |||
| 457 | public function provideGetBaseRouteName() |
||
| 458 | { |
||
| 459 | return [ |
||
| 460 | [ |
||
| 461 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 462 | 'admin_sonata_news_post', |
||
| 463 | ], |
||
| 464 | [ |
||
| 465 | 'Application\Sonata\NewsBundle\Document\Post', |
||
| 466 | 'admin_sonata_news_post', |
||
| 467 | ], |
||
| 468 | [ |
||
| 469 | 'MyApplication\MyBundle\Entity\Post', |
||
| 470 | 'admin_myapplication_my_post', |
||
| 471 | ], |
||
| 472 | [ |
||
| 473 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
| 474 | 'admin_myapplication_my_post_category', |
||
| 475 | ], |
||
| 476 | [ |
||
| 477 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
| 478 | 'admin_myapplication_my_product_category', |
||
| 479 | ], |
||
| 480 | [ |
||
| 481 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
| 482 | 'admin_myapplication_my_other_product_category', |
||
| 483 | ], |
||
| 484 | [ |
||
| 485 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
| 486 | 'admin_cmf_foo_menu', |
||
| 487 | ], |
||
| 488 | [ |
||
| 489 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
| 490 | 'admin_cmf_foo_menu', |
||
| 491 | ], |
||
| 492 | [ |
||
| 493 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
| 494 | 'admin_symfony_barbar_menu', |
||
| 495 | ], |
||
| 496 | [ |
||
| 497 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
| 498 | 'admin_symfony_barbar_menu_item', |
||
| 499 | ], |
||
| 500 | [ |
||
| 501 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
| 502 | 'admin_cmf_foo_menu', |
||
| 503 | ], |
||
| 504 | [ |
||
| 505 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
| 506 | 'admin_cmf_foo_menu', |
||
| 507 | ], |
||
| 508 | [ |
||
| 509 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
| 510 | 'admin_cmf_foo_menu', |
||
| 511 | ], |
||
| 512 | [ |
||
| 513 | 'AppBundle\Entity\User', |
||
| 514 | 'admin_app_user', |
||
| 515 | ], |
||
| 516 | [ |
||
| 517 | 'App\Entity\User', |
||
| 518 | 'admin_app_user', |
||
| 519 | ], |
||
| 520 | ]; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @dataProvider provideGetBaseRouteName |
||
| 525 | */ |
||
| 526 | public function testGetBaseRouteName(string $objFqn, string $expected): void |
||
| 527 | { |
||
| 528 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 529 | |||
| 530 | $this->assertSame($expected, $admin->getBaseRouteName()); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @group legacy |
||
| 535 | * @expectedDeprecation Calling "addChild" without second argument is deprecated since sonata-project/admin-bundle 3.35 and will not be allowed in 4.0. |
||
| 536 | * @dataProvider provideGetBaseRouteName |
||
| 537 | */ |
||
| 538 | public function testGetBaseRouteNameWithChildAdmin(string $objFqn, string $expected): void |
||
| 539 | { |
||
| 540 | $routeGenerator = new DefaultRouteGenerator( |
||
| 541 | $this->createMock(RouterInterface::class), |
||
| 542 | new RoutesCache($this->cacheTempFolder, true) |
||
| 543 | ); |
||
| 544 | |||
| 545 | $container = new Container(); |
||
| 546 | $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
||
| 547 | |||
| 548 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
| 549 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 550 | $container->set('sonata.post.admin.post', $postAdmin); |
||
| 551 | $postAdmin->setConfigurationPool($pool); |
||
| 552 | $postAdmin->setRouteBuilder($pathInfo); |
||
| 553 | $postAdmin->setRouteGenerator($routeGenerator); |
||
| 554 | $postAdmin->initialize(); |
||
| 555 | |||
| 556 | $commentAdmin = new CommentAdmin( |
||
| 557 | 'sonata.post.admin.comment', |
||
| 558 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 559 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 560 | ); |
||
| 561 | $container->set('sonata.post.admin.comment', $commentAdmin); |
||
| 562 | $commentAdmin->setConfigurationPool($pool); |
||
| 563 | $commentAdmin->setRouteBuilder($pathInfo); |
||
| 564 | $commentAdmin->setRouteGenerator($routeGenerator); |
||
| 565 | $commentAdmin->initialize(); |
||
| 566 | |||
| 567 | $postAdmin->addChild($commentAdmin, 'post'); |
||
| 568 | |||
| 569 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 570 | 'sonata.post.admin.comment_vote', |
||
| 571 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 572 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 573 | ); |
||
| 574 | |||
| 575 | $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin); |
||
| 576 | $commentVoteAdmin->setConfigurationPool($pool); |
||
| 577 | $commentVoteAdmin->setRouteBuilder($pathInfo); |
||
| 578 | $commentVoteAdmin->setRouteGenerator($routeGenerator); |
||
| 579 | $commentVoteAdmin->initialize(); |
||
| 580 | |||
| 581 | $commentAdmin->addChild($commentVoteAdmin); |
||
| 582 | $pool->setAdminServiceIds([ |
||
| 583 | 'sonata.post.admin.post', |
||
| 584 | 'sonata.post.admin.comment', |
||
| 585 | 'sonata.post.admin.comment_vote', |
||
| 586 | ]); |
||
| 587 | |||
| 588 | $this->assertSame(sprintf('%s_comment', $expected), $commentAdmin->getBaseRouteName()); |
||
| 589 | |||
| 590 | $this->assertTrue($postAdmin->hasRoute('show')); |
||
| 591 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show')); |
||
| 592 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show')); |
||
| 593 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show')); |
||
| 594 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list')); |
||
| 595 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list')); |
||
| 596 | $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit')); |
||
| 597 | $this->assertFalse($commentAdmin->hasRoute('edit')); |
||
| 598 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
| 599 | |||
| 600 | /* |
||
| 601 | * Test the route name from request |
||
| 602 | */ |
||
| 603 | $postListRequest = new Request( |
||
| 604 | [], |
||
| 605 | [], |
||
| 606 | [ |
||
| 607 | '_route' => sprintf('%s_list', $postAdmin->getBaseRouteName()), |
||
| 608 | ] |
||
| 609 | ); |
||
| 610 | |||
| 611 | $postAdmin->setRequest($postListRequest); |
||
| 612 | $commentAdmin->setRequest($postListRequest); |
||
| 613 | |||
| 614 | $this->assertTrue($postAdmin->isCurrentRoute('list')); |
||
| 615 | $this->assertFalse($postAdmin->isCurrentRoute('create')); |
||
| 616 | $this->assertFalse($commentAdmin->isCurrentRoute('list')); |
||
| 617 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('list')); |
||
| 618 | $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
| 619 | $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
| 620 | $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
| 621 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
| 622 | } |
||
| 623 | |||
| 624 | public function testGetBaseRouteNameWithUnreconizedClassname(): void |
||
| 625 | { |
||
| 626 | $this->expectException(\RuntimeException::class); |
||
| 627 | |||
| 628 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 629 | $admin->getBaseRouteName(); |
||
| 630 | } |
||
| 631 | |||
| 632 | public function testGetBaseRouteNameWithSpecifiedName(): void |
||
| 633 | { |
||
| 634 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 635 | |||
| 636 | $this->assertSame('post_custom', $postAdmin->getBaseRouteName()); |
||
| 637 | } |
||
| 638 | |||
| 639 | public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void |
||
| 640 | { |
||
| 641 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 642 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'); |
||
| 643 | $commentAdmin->setParent($postAdmin); |
||
| 644 | |||
| 645 | $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName()); |
||
| 646 | } |
||
| 647 | |||
| 648 | public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void |
||
| 649 | { |
||
| 650 | $postAdmin = new PostAdmin( |
||
| 651 | 'sonata.post.admin.post', |
||
| 652 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
| 653 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 654 | ); |
||
| 655 | $commentAdmin = new CommentWithCustomRouteAdmin( |
||
| 656 | 'sonata.post.admin.comment_with_custom_route', |
||
| 657 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 658 | 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController' |
||
| 659 | ); |
||
| 660 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 661 | 'sonata.post.admin.comment_vote', |
||
| 662 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 663 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 664 | ); |
||
| 665 | $commentAdmin->setParent($postAdmin); |
||
| 666 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 667 | |||
| 668 | $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName()); |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid |
||
| 673 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid |
||
| 674 | */ |
||
| 675 | public function testSetUniqid(): void |
||
| 676 | { |
||
| 677 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 678 | |||
| 679 | $uniqid = uniqid(); |
||
| 680 | $admin->setUniqid($uniqid); |
||
| 681 | |||
| 682 | $this->assertSame($uniqid, $admin->getUniqid()); |
||
| 683 | } |
||
| 684 | |||
| 685 | public function testToString(): void |
||
| 686 | { |
||
| 687 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 688 | |||
| 689 | $s = new \stdClass(); |
||
| 690 | |||
| 691 | $this->assertNotEmpty($admin->toString($s)); |
||
| 692 | |||
| 693 | $s = new FooToString(); |
||
| 694 | $this->assertSame('salut', $admin->toString($s)); |
||
| 695 | |||
| 696 | // To string method is implemented, but returns null |
||
| 697 | $s = new FooToStringNull(); |
||
| 698 | $this->assertNotEmpty($admin->toString($s)); |
||
| 699 | |||
| 700 | $this->assertSame('', $admin->toString(false)); |
||
|
|
|||
| 701 | } |
||
| 702 | |||
| 703 | public function testIsAclEnabled(): void |
||
| 704 | { |
||
| 705 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 706 | |||
| 707 | $this->assertFalse($postAdmin->isAclEnabled()); |
||
| 708 | |||
| 709 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController'); |
||
| 710 | $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class)); |
||
| 711 | $this->assertTrue($commentAdmin->isAclEnabled()); |
||
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @group legacy |
||
| 716 | * |
||
| 717 | * @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. |
||
| 718 | * |
||
| 719 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses |
||
| 720 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass |
||
| 721 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses |
||
| 722 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass |
||
| 723 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 724 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass |
||
| 725 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode |
||
| 726 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass |
||
| 727 | */ |
||
| 728 | public function testSubClass(): void |
||
| 729 | { |
||
| 730 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
| 731 | $admin = new PostAdmin( |
||
| 732 | 'sonata.post.admin.post', |
||
| 733 | Post::class, |
||
| 734 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 735 | ); |
||
| 736 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 737 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 738 | $this->assertCount(0, $admin->getSubClasses()); |
||
| 739 | $this->assertNull($admin->getActiveSubClass()); |
||
| 740 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 741 | $this->assertSame(Post::class, $admin->getClass()); |
||
| 742 | |||
| 743 | // Just for the record, if there is no inheritance set, the getSubject is not used |
||
| 744 | // the getSubject can also lead to some issue |
||
| 745 | $admin->setSubject(new BlogPost()); |
||
| 746 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 747 | |||
| 748 | $admin->setSubClasses([ |
||
| 749 | 'extended1' => 'NewsBundle\Entity\PostExtended1', |
||
| 750 | 'extended2' => 'NewsBundle\Entity\PostExtended2', |
||
| 751 | ]); |
||
| 752 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 753 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 754 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 755 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 756 | // NEXT_MAJOR: remove the following 2 `assertNull()` assertions |
||
| 757 | $this->assertNull($admin->getActiveSubClass()); |
||
| 758 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 759 | $this->assertSame( |
||
| 760 | BlogPost::class, |
||
| 761 | $admin->getClass(), |
||
| 762 | 'When there is no subclass in the query the class parameter should be returned' |
||
| 763 | ); |
||
| 764 | |||
| 765 | $request = new Request(['subclass' => 'extended1']); |
||
| 766 | $admin->setRequest($request); |
||
| 767 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 768 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 769 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 770 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 771 | $this->assertSame( |
||
| 772 | 'NewsBundle\Entity\PostExtended1', |
||
| 773 | $admin->getActiveSubClass(), |
||
| 774 | 'It should return the curently active sub class.' |
||
| 775 | ); |
||
| 776 | $this->assertSame('extended1', $admin->getActiveSubclassCode()); |
||
| 777 | $this->assertSame( |
||
| 778 | 'NewsBundle\Entity\PostExtended1', |
||
| 779 | $admin->getClass(), |
||
| 780 | 'getClass() should return the name of the sub class when passed through a request query parameter.' |
||
| 781 | ); |
||
| 782 | |||
| 783 | $request->query->set('subclass', 'inject'); |
||
| 784 | |||
| 785 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 786 | // NEXT_MAJOR: remove the previous `assertNull()` assertion and uncomment the following lines |
||
| 787 | // $this->expectException(\LogicException::class); |
||
| 788 | // $this->expectExceptionMessage(sprintf('Admin "%s" has no active subclass.', PostAdmin::class)); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @group legacy |
||
| 793 | * |
||
| 794 | * @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. |
||
| 795 | */ |
||
| 796 | public function testNonExistantSubclass(): void |
||
| 797 | { |
||
| 798 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
| 799 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 800 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
| 801 | |||
| 802 | $admin->setRequest(new Request(['subclass' => 'inject'])); |
||
| 803 | |||
| 804 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
| 805 | |||
| 806 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 807 | |||
| 808 | $this->expectException(\RuntimeException::class); |
||
| 809 | |||
| 810 | $admin->getActiveSubClass(); |
||
| 811 | } |
||
| 812 | |||
| 813 | /** |
||
| 814 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 815 | */ |
||
| 816 | public function testOnlyOneSubclassNeededToBeActive(): void |
||
| 817 | { |
||
| 818 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 819 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']); |
||
| 820 | $request = new Request(['subclass' => 'extended1']); |
||
| 821 | $admin->setRequest($request); |
||
| 822 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * @group legacy |
||
| 827 | * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0. |
||
| 828 | */ |
||
| 829 | public function testAddSubClassIsDeprecated(): void |
||
| 830 | { |
||
| 831 | $admin = new PostAdmin( |
||
| 832 | 'sonata.post.admin.post', |
||
| 833 | Post::class, |
||
| 834 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 835 | ); |
||
| 836 | $admin->addSubClass('whatever'); |
||
| 837 | } |
||
| 838 | |||
| 839 | /** |
||
| 840 | * @group legacy |
||
| 841 | */ |
||
| 842 | public function testGetPerPageOptions(): void |
||
| 843 | { |
||
| 844 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 845 | |||
| 846 | $perPageOptions = $admin->getPerPageOptions(); |
||
| 847 | |||
| 848 | foreach ($perPageOptions as $perPage) { |
||
| 849 | $this->assertSame(0, $perPage % 4); |
||
| 850 | } |
||
| 851 | |||
| 852 | $admin->setPerPageOptions([500, 1000]); |
||
| 853 | $this->assertSame([500, 1000], $admin->getPerPageOptions()); |
||
| 854 | } |
||
| 855 | |||
| 856 | public function testGetLabelTranslatorStrategy(): void |
||
| 857 | { |
||
| 858 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 859 | |||
| 860 | $this->assertNull($admin->getLabelTranslatorStrategy()); |
||
| 861 | |||
| 862 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
| 863 | $admin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
| 864 | $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy()); |
||
| 865 | } |
||
| 866 | |||
| 867 | public function testGetRouteBuilder(): void |
||
| 868 | { |
||
| 869 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 870 | |||
| 871 | $this->assertNull($admin->getRouteBuilder()); |
||
| 872 | |||
| 873 | $routeBuilder = $this->createMock(RouteBuilderInterface::class); |
||
| 874 | $admin->setRouteBuilder($routeBuilder); |
||
| 875 | $this->assertSame($routeBuilder, $admin->getRouteBuilder()); |
||
| 876 | } |
||
| 877 | |||
| 878 | public function testGetMenuFactory(): void |
||
| 879 | { |
||
| 880 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 881 | |||
| 882 | $this->assertNull($admin->getMenuFactory()); |
||
| 883 | |||
| 884 | $menuFactory = $this->createMock(FactoryInterface::class); |
||
| 885 | $admin->setMenuFactory($menuFactory); |
||
| 886 | $this->assertSame($menuFactory, $admin->getMenuFactory()); |
||
| 887 | } |
||
| 888 | |||
| 889 | public function testGetExtensions(): void |
||
| 902 | |||
| 903 | public function testGetFilterTheme(): void |
||
| 912 | |||
| 913 | public function testGetFormTheme(): void |
||
| 923 | |||
| 924 | public function testGetValidator(): void |
||
| 935 | |||
| 936 | public function testGetSecurityHandler(): void |
||
| 946 | |||
| 947 | public function testGetSecurityInformation(): void |
||
| 961 | |||
| 962 | public function testGetManagerType(): void |
||
| 971 | |||
| 972 | public function testGetModelManager(): void |
||
| 983 | |||
| 984 | /** |
||
| 985 | * NEXT_MAJOR: remove this method. |
||
| 986 | * |
||
| 987 | * @group legacy |
||
| 988 | */ |
||
| 989 | public function testGetBaseCodeRoute(): void |
||
| 998 | |||
| 999 | // NEXT_MAJOR: uncomment this method. |
||
| 1000 | // public function testGetBaseCodeRoute() |
||
| 1001 | // { |
||
| 1002 | // $postAdmin = new PostAdmin( |
||
| 1003 | // 'sonata.post.admin.post', |
||
| 1004 | // 'NewsBundle\Entity\Post', |
||
| 1005 | // 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 1006 | // ); |
||
| 1007 | // $commentAdmin = new CommentAdmin( |
||
| 1008 | // 'sonata.post.admin.comment', |
||
| 1009 | // 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 1010 | // 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 1011 | // ); |
||
| 1012 | // |
||
| 1013 | // $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute()); |
||
| 1014 | // |
||
| 1015 | // $postAdmin->addChild($commentAdmin); |
||
| 1016 | // |
||
| 1017 | // $this->assertSame( |
||
| 1018 | // 'sonata.post.admin.post|sonata.post.admin.comment', |
||
| 1019 | // $commentAdmin->getBaseCodeRoute() |
||
| 1020 | // ); |
||
| 1021 | // } |
||
| 1022 | |||
| 1023 | public function testGetRouteGenerator(): void |
||
| 1034 | |||
| 1035 | public function testGetConfigurationPool(): void |
||
| 1048 | |||
| 1049 | public function testGetShowBuilder(): void |
||
| 1060 | |||
| 1061 | public function testGetListBuilder(): void |
||
| 1072 | |||
| 1073 | public function testGetDatagridBuilder(): void |
||
| 1084 | |||
| 1085 | public function testGetFormContractor(): void |
||
| 1096 | |||
| 1097 | public function testGetRequest(): void |
||
| 1109 | |||
| 1110 | public function testGetRequestWithException(): void |
||
| 1118 | |||
| 1119 | public function testGetTranslationDomain(): void |
||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * @group legacy |
||
| 1131 | */ |
||
| 1132 | public function testGetTranslator(): void |
||
| 1143 | |||
| 1144 | public function testGetShowGroups(): void |
||
| 1145 | { |
||
| 1146 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1147 | |||
| 1148 | // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call. |
||
| 1149 | $this->assertFalse($admin->getShowGroups('sonata_deprecation_mute')); |
||
| 1150 | |||
| 1151 | $groups = ['foo', 'bar', 'baz']; |
||
| 1152 | |||
| 1153 | $admin->setShowGroups($groups); |
||
| 1154 | $this->assertSame($groups, $admin->getShowGroups()); |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | public function testGetFormGroups(): void |
||
| 1158 | { |
||
| 1159 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1160 | |||
| 1161 | // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call. |
||
| 1162 | $this->assertFalse($admin->getFormGroups('sonata_deprecation_mute')); |
||
| 1163 | |||
| 1164 | $groups = ['foo', 'bar', 'baz']; |
||
| 1165 | |||
| 1166 | $admin->setFormGroups($groups); |
||
| 1167 | $this->assertSame($groups, $admin->getFormGroups()); |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | public function testGetMaxPageLinks(): void |
||
| 1171 | { |
||
| 1172 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1173 | |||
| 1174 | $this->assertSame(25, $admin->getMaxPageLinks()); |
||
| 1175 | |||
| 1176 | $admin->setMaxPageLinks(14); |
||
| 1177 | $this->assertSame(14, $admin->getMaxPageLinks()); |
||
| 1178 | } |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * @group legacy |
||
| 1182 | */ |
||
| 1183 | public function testGetMaxPerPage(): void |
||
| 1184 | { |
||
| 1185 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1186 | |||
| 1187 | $this->assertSame(32, $admin->getMaxPerPage()); |
||
| 1188 | |||
| 1189 | $admin->setMaxPerPage(94); |
||
| 1190 | $this->assertSame(94, $admin->getMaxPerPage()); |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | public function testGetLabel(): void |
||
| 1194 | { |
||
| 1195 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1196 | |||
| 1197 | $this->assertNull($admin->getLabel()); |
||
| 1198 | |||
| 1199 | $admin->setLabel('FooLabel'); |
||
| 1200 | $this->assertSame('FooLabel', $admin->getLabel()); |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | public function testGetBaseController(): void |
||
| 1204 | { |
||
| 1205 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1206 | |||
| 1207 | $this->assertSame('Sonata\NewsBundle\Controller\PostAdminController', $admin->getBaseControllerName()); |
||
| 1208 | |||
| 1209 | $admin->setBaseControllerName('Sonata\NewsBundle\Controller\FooAdminController'); |
||
| 1210 | $this->assertSame('Sonata\NewsBundle\Controller\FooAdminController', $admin->getBaseControllerName()); |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | public function testGetTemplates(): void |
||
| 1214 | { |
||
| 1215 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1216 | |||
| 1217 | $templates = [ |
||
| 1218 | 'list' => '@FooAdmin/CRUD/list.html.twig', |
||
| 1219 | 'show' => '@FooAdmin/CRUD/show.html.twig', |
||
| 1220 | 'edit' => '@FooAdmin/CRUD/edit.html.twig', |
||
| 1221 | ]; |
||
| 1222 | |||
| 1223 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 1224 | $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates); |
||
| 1225 | |||
| 1226 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 1227 | |||
| 1228 | $this->assertSame($templates, $admin->getTemplates()); |
||
| 1229 | } |
||
| 1230 | |||
| 1231 | public function testGetTemplate1(): void |
||
| 1232 | { |
||
| 1233 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1234 | |||
| 1235 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 1236 | $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig'); |
||
| 1237 | $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig'); |
||
| 1238 | |||
| 1239 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 1240 | |||
| 1241 | $this->assertSame('@FooAdmin/CRUD/edit.html.twig', $admin->getTemplate('edit')); |
||
| 1242 | $this->assertSame('@FooAdmin/CRUD/show.html.twig', $admin->getTemplate('show')); |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | public function testGetIdParameter(): void |
||
| 1246 | { |
||
| 1247 | $postAdmin = new PostAdmin( |
||
| 1248 | 'sonata.post.admin.post', |
||
| 1249 | 'NewsBundle\Entity\Post', |
||
| 1250 | 'Sonata\NewsBundle\Controller\PostAdminController' |
||
| 1251 | ); |
||
| 1252 | |||
| 1253 | $this->assertSame('id', $postAdmin->getIdParameter()); |
||
| 1254 | $this->assertFalse($postAdmin->isChild()); |
||
| 1255 | |||
| 1256 | $commentAdmin = new CommentAdmin( |
||
| 1257 | 'sonata.post.admin.comment', |
||
| 1258 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 1259 | 'Sonata\NewsBundle\Controller\CommentAdminController' |
||
| 1260 | ); |
||
| 1261 | $commentAdmin->setParent($postAdmin); |
||
| 1262 | |||
| 1263 | $this->assertTrue($commentAdmin->isChild()); |
||
| 1264 | $this->assertSame('childId', $commentAdmin->getIdParameter()); |
||
| 1265 | |||
| 1266 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 1267 | 'sonata.post.admin.comment_vote', |
||
| 1268 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 1269 | 'Sonata\NewsBundle\Controller\CommentVoteAdminController' |
||
| 1270 | ); |
||
| 1271 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 1272 | |||
| 1273 | $this->assertTrue($commentVoteAdmin->isChild()); |
||
| 1274 | $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter()); |
||
| 1275 | } |
||
| 1276 | |||
| 1277 | public function testGetExportFormats(): void |
||
| 1278 | { |
||
| 1279 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1280 | |||
| 1281 | $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats()); |
||
| 1282 | } |
||
| 1283 | |||
| 1284 | public function testGetUrlsafeIdentifier(): void |
||
| 1285 | { |
||
| 1286 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1287 | |||
| 1288 | $model = new \stdClass(); |
||
| 1289 | |||
| 1290 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1291 | $modelManager->expects($this->once()) |
||
| 1292 | ->method('getUrlSafeIdentifier') |
||
| 1293 | ->with($this->equalTo($model)) |
||
| 1294 | ->willReturn('foo'); |
||
| 1295 | $admin->setModelManager($modelManager); |
||
| 1296 | |||
| 1297 | $this->assertSame('foo', $admin->getUrlSafeIdentifier($model)); |
||
| 1298 | } |
||
| 1299 | |||
| 1300 | public function testDeterminedPerPageValue(): void |
||
| 1301 | { |
||
| 1302 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1303 | |||
| 1304 | $this->assertFalse($admin->determinedPerPageValue('foo')); |
||
| 1305 | $this->assertFalse($admin->determinedPerPageValue(123)); |
||
| 1306 | $this->assertTrue($admin->determinedPerPageValue(16)); |
||
| 1307 | } |
||
| 1308 | |||
| 1309 | public function testIsGranted(): void |
||
| 1310 | { |
||
| 1311 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1312 | $modelManager = $this->createStub(ModelManagerInterface::class); |
||
| 1313 | $modelManager |
||
| 1314 | ->method('getNormalizedIdentifier') |
||
| 1315 | ->willReturnCallback(static function (?object $model = null): ?string { |
||
| 1316 | return $model ? $model->id : null; |
||
| 1317 | }); |
||
| 1318 | |||
| 1319 | $admin->setModelManager($modelManager); |
||
| 1320 | |||
| 1321 | $entity1 = new \stdClass(); |
||
| 1322 | $entity1->id = '1'; |
||
| 1323 | |||
| 1324 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1325 | $securityHandler |
||
| 1326 | ->expects($this->exactly(6)) |
||
| 1327 | ->method('isGranted') |
||
| 1328 | ->willReturnCallback(static function ( |
||
| 1329 | AdminInterface $adminIn, |
||
| 1330 | string $attributes, |
||
| 1331 | ?object $object = null |
||
| 1332 | ) use ( |
||
| 1333 | $admin, |
||
| 1334 | $entity1 |
||
| 1335 | ): bool { |
||
| 1336 | return $admin === $adminIn && 'FOO' === $attributes && |
||
| 1337 | ($object === $admin || $object === $entity1); |
||
| 1338 | }); |
||
| 1339 | |||
| 1340 | $admin->setSecurityHandler($securityHandler); |
||
| 1341 | |||
| 1342 | $this->assertTrue($admin->isGranted('FOO')); |
||
| 1343 | $this->assertTrue($admin->isGranted('FOO')); |
||
| 1344 | $this->assertTrue($admin->isGranted('FOO', $entity1)); |
||
| 1345 | $this->assertTrue($admin->isGranted('FOO', $entity1)); |
||
| 1346 | $this->assertFalse($admin->isGranted('BAR')); |
||
| 1347 | $this->assertFalse($admin->isGranted('BAR')); |
||
| 1348 | $this->assertFalse($admin->isGranted('BAR', $entity1)); |
||
| 1349 | $this->assertFalse($admin->isGranted('BAR', $entity1)); |
||
| 1350 | |||
| 1351 | $entity2 = new \stdClass(); |
||
| 1352 | $entity2->id = '2'; |
||
| 1353 | |||
| 1354 | $this->assertFalse($admin->isGranted('BAR', $entity2)); |
||
| 1355 | $this->assertFalse($admin->isGranted('BAR', $entity2)); |
||
| 1356 | |||
| 1357 | $entity3 = new \stdClass(); |
||
| 1358 | $entity3->id = '3'; |
||
| 1359 | |||
| 1360 | $this->assertFalse($admin->isGranted('BAR', $entity3)); |
||
| 1361 | $this->assertFalse($admin->isGranted('BAR', $entity3)); |
||
| 1362 | } |
||
| 1363 | |||
| 1364 | public function testSupportsPreviewMode(): void |
||
| 1365 | { |
||
| 1366 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1367 | |||
| 1368 | $this->assertFalse($admin->supportsPreviewMode()); |
||
| 1369 | } |
||
| 1370 | |||
| 1371 | public function testGetPermissionsShow(): void |
||
| 1372 | { |
||
| 1373 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1374 | |||
| 1375 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1376 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU)); |
||
| 1377 | $this->assertSame(['LIST'], $admin->getPermissionsShow('foo')); |
||
| 1378 | } |
||
| 1379 | |||
| 1380 | public function testShowIn(): void |
||
| 1381 | { |
||
| 1382 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1383 | |||
| 1384 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1385 | $securityHandler |
||
| 1386 | ->method('isGranted') |
||
| 1387 | ->willReturnCallback(static function (AdminInterface $adminIn, array $attributes, $object = null) use ($admin): bool { |
||
| 1388 | return $admin === $adminIn && $attributes === ['LIST']; |
||
| 1389 | }); |
||
| 1390 | |||
| 1391 | $admin->setSecurityHandler($securityHandler); |
||
| 1392 | |||
| 1393 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1394 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU)); |
||
| 1395 | $this->assertTrue($admin->showIn('foo')); |
||
| 1396 | } |
||
| 1397 | |||
| 1398 | public function testGetObjectIdentifier(): void |
||
| 1399 | { |
||
| 1400 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1401 | |||
| 1402 | $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier()); |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * @group legacy |
||
| 1407 | */ |
||
| 1408 | public function testTrans(): void |
||
| 1409 | { |
||
| 1410 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1411 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1412 | |||
| 1413 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1414 | $admin->setTranslator($translator); |
||
| 1415 | |||
| 1416 | $translator->expects($this->once()) |
||
| 1417 | ->method('trans') |
||
| 1418 | ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1419 | ->willReturn('fooTranslated'); |
||
| 1420 | |||
| 1421 | $this->assertSame('fooTranslated', $admin->trans('foo')); |
||
| 1422 | } |
||
| 1423 | |||
| 1424 | /** |
||
| 1425 | * @group legacy |
||
| 1426 | */ |
||
| 1427 | public function testTransWithMessageDomain(): void |
||
| 1428 | { |
||
| 1429 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1430 | |||
| 1431 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1432 | $admin->setTranslator($translator); |
||
| 1433 | |||
| 1434 | $translator->expects($this->once()) |
||
| 1435 | ->method('trans') |
||
| 1436 | ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1437 | ->willReturn('fooTranslated'); |
||
| 1438 | |||
| 1439 | $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1440 | } |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * @group legacy |
||
| 1444 | */ |
||
| 1445 | public function testTransChoice(): void |
||
| 1446 | { |
||
| 1447 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1448 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1449 | |||
| 1450 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1451 | $admin->setTranslator($translator); |
||
| 1452 | |||
| 1453 | $translator->expects($this->once()) |
||
| 1454 | ->method('transChoice') |
||
| 1455 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1456 | ->willReturn('fooTranslated'); |
||
| 1457 | |||
| 1458 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2)); |
||
| 1459 | } |
||
| 1460 | |||
| 1461 | /** |
||
| 1462 | * @group legacy |
||
| 1463 | */ |
||
| 1464 | public function testTransChoiceWithMessageDomain(): void |
||
| 1465 | { |
||
| 1466 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1467 | |||
| 1468 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1469 | $admin->setTranslator($translator); |
||
| 1470 | |||
| 1471 | $translator->expects($this->once()) |
||
| 1472 | ->method('transChoice') |
||
| 1473 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1474 | ->willReturn('fooTranslated'); |
||
| 1475 | |||
| 1476 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1477 | } |
||
| 1478 | |||
| 1479 | public function testSetFilterPersister(): void |
||
| 1480 | { |
||
| 1481 | $admin = new class('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle\Controller\PostAdminController') extends PostAdmin { |
||
| 1482 | public function persistFilters(): bool |
||
| 1483 | { |
||
| 1484 | return $this->persistFilters; |
||
| 1485 | } |
||
| 1486 | }; |
||
| 1487 | |||
| 1488 | $filterPersister = $this->createMock(FilterPersisterInterface::class); |
||
| 1489 | |||
| 1490 | $admin->setFilterPersister($filterPersister); |
||
| 1491 | $this->assertTrue($admin->persistFilters()); |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | public function testGetRootCode(): void |
||
| 1511 | |||
| 1512 | public function testGetRoot(): void |
||
| 1529 | |||
| 1530 | public function testGetExportFields(): void |
||
| 1531 | { |
||
| 1532 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1533 | |||
| 1534 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1535 | $modelManager->expects($this->once()) |
||
| 1536 | ->method('getExportFields') |
||
| 1537 | ->with($this->equalTo('NewsBundle\Entity\Post')) |
||
| 1538 | ->willReturn(['foo', 'bar']); |
||
| 1539 | |||
| 1540 | $admin->setModelManager($modelManager); |
||
| 1541 | $this->assertSame(['foo', 'bar'], $admin->getExportFields()); |
||
| 1542 | } |
||
| 1543 | |||
| 1544 | public function testGetPersistentParametersWithNoExtension(): void |
||
| 1545 | { |
||
| 1546 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1547 | |||
| 1548 | $this->assertEmpty($admin->getPersistentParameters()); |
||
| 1549 | } |
||
| 1550 | |||
| 1551 | public function testGetPersistentParametersWithInvalidExtension(): void |
||
| 1552 | { |
||
| 1553 | $this->expectException(\RuntimeException::class); |
||
| 1554 | |||
| 1555 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1556 | |||
| 1557 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
| 1558 | $extension->expects($this->once())->method('getPersistentParameters')->willReturn(null); |
||
| 1559 | |||
| 1560 | $admin->addExtension($extension); |
||
| 1561 | |||
| 1562 | $admin->getPersistentParameters(); |
||
| 1563 | } |
||
| 1564 | |||
| 1565 | public function testGetPersistentParametersWithValidExtension(): void |
||
| 1566 | { |
||
| 1567 | $expected = [ |
||
| 1568 | 'context' => 'foobar', |
||
| 1569 | ]; |
||
| 1570 | |||
| 1571 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); |
||
| 1572 | |||
| 1573 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
| 1574 | $extension->expects($this->once())->method('getPersistentParameters')->willReturn($expected); |
||
| 1575 | |||
| 1576 | $admin->addExtension($extension); |
||
| 1577 | |||
| 1578 | $this->assertSame($expected, $admin->getPersistentParameters()); |
||
| 1579 | } |
||
| 1580 | |||
| 1581 | public function testGetNewInstanceForChildAdminWithParentValue(): void |
||
| 1582 | { |
||
| 1583 | $post = new Post(); |
||
| 1584 | |||
| 1585 | $postAdmin = $this->getMockBuilder(PostAdmin::class)->disableOriginalConstructor()->getMock(); |
||
| 1586 | $postAdmin->method('getObject')->willReturn($post); |
||
| 1587 | |||
| 1588 | $formBuilder = $this->createStub(FormBuilderInterface::class); |
||
| 1589 | $formBuilder->method('getForm')->willReturn(null); |
||
| 1590 | |||
| 1591 | $tag = new Tag(); |
||
| 1592 | |||
| 1593 | $modelManager = $this->createStub(ModelManagerInterface::class); |
||
| 1594 | $modelManager->method('getModelInstance')->willReturn($tag); |
||
| 1595 | |||
| 1596 | $tagAdmin = new TagAdmin('admin.tag', Tag::class, 'MyBundle\MyController'); |
||
| 1597 | $tagAdmin->setModelManager($modelManager); |
||
| 1598 | $tagAdmin->setParent($postAdmin); |
||
| 1599 | |||
| 1600 | $request = $this->createStub(Request::class); |
||
| 1601 | $tagAdmin->setRequest($request); |
||
| 1602 | |||
| 1603 | $configurationPool = $this->getMockBuilder(Pool::class) |
||
| 1604 | ->disableOriginalConstructor() |
||
| 1605 | ->getMock(); |
||
| 1606 | |||
| 1607 | $configurationPool->method('getPropertyAccessor')->willReturn(PropertyAccess::createPropertyAccessor()); |
||
| 1608 | |||
| 1609 | $tagAdmin->setConfigurationPool($configurationPool); |
||
| 1610 | |||
| 1611 | $tag = $tagAdmin->getNewInstance(); |
||
| 1612 | |||
| 1613 | $this->assertSame($post, $tag->getPost()); |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | public function testGetNewInstanceForChildAdminWithCollectionParentValue(): void |
||
| 1652 | |||
| 1653 | public function testGetNewInstanceForEmbededAdminWithParentValue(): void |
||
| 1654 | { |
||
| 1655 | $post = new Post(); |
||
| 1656 | |||
| 1657 | $postAdmin = $this->getMockBuilder(PostAdmin::class)->disableOriginalConstructor()->getMock(); |
||
| 1658 | $postAdmin->method('getObject')->willReturn($post); |
||
| 1659 | |||
| 1660 | $formBuilder = $this->createStub(FormBuilderInterface::class); |
||
| 1661 | $formBuilder->method('getForm')->willReturn(null); |
||
| 1662 | |||
| 1663 | $parentField = $this->createStub(FieldDescriptionInterface::class); |
||
| 1664 | $parentField->method('getAdmin')->willReturn($postAdmin); |
||
| 1665 | $parentField->method('getParentAssociationMappings')->willReturn([]); |
||
| 1666 | $parentField->method('getAssociationMapping')->willReturn(['fieldName' => 'tag', 'mappedBy' => 'post']); |
||
| 1667 | |||
| 1668 | $tag = new Tag(); |
||
| 1669 | |||
| 1670 | $modelManager = $this->createStub(ModelManagerInterface::class); |
||
| 1671 | $modelManager->method('getModelInstance')->willReturn($tag); |
||
| 1672 | |||
| 1673 | $tagAdmin = new TagAdmin('admin.tag', Tag::class, 'MyBundle\MyController'); |
||
| 1674 | $tagAdmin->setModelManager($modelManager); |
||
| 1675 | $tagAdmin->setParentFieldDescription($parentField); |
||
| 1676 | |||
| 1677 | $request = $this->createStub(Request::class); |
||
| 1678 | $tagAdmin->setRequest($request); |
||
| 1679 | |||
| 1680 | $configurationPool = $this->getMockBuilder(Pool::class) |
||
| 1681 | ->disableOriginalConstructor() |
||
| 1682 | ->getMock(); |
||
| 1683 | |||
| 1684 | $configurationPool->method('getPropertyAccessor')->willReturn(PropertyAccess::createPropertyAccessor()); |
||
| 1685 | |||
| 1686 | $tagAdmin->setConfigurationPool($configurationPool); |
||
| 1687 | |||
| 1688 | $tag = $tagAdmin->getNewInstance(); |
||
| 1689 | |||
| 1690 | $this->assertSame($post, $tag->getPost()); |
||
| 1691 | } |
||
| 1692 | |||
| 1693 | public function testFormAddPostSubmitEventForPreValidation(): void |
||
| 1694 | { |
||
| 1695 | $modelAdmin = new ModelAdmin('sonata.post.admin.model', \stdClass::class, 'Sonata\FooBundle\Controller\ModelAdminController'); |
||
| 1696 | $object = new \stdClass(); |
||
| 1697 | |||
| 1698 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
| 1699 | $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
| 1700 | |||
| 1701 | $validator = $this->createMock(ValidatorInterface::class); |
||
| 1702 | $validator |
||
| 1703 | ->method('getMetadataFor') |
||
| 1704 | ->willReturn($this->createMock(MemberMetadata::class)); |
||
| 1705 | $modelAdmin->setValidator($validator); |
||
| 1706 | |||
| 1707 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1708 | $modelManager |
||
| 1709 | ->method('getNewFieldDescriptionInstance') |
||
| 1755 | |||
| 1756 | public function testCanAddInlineValidationOnlyForGenericMetadata(): void |
||
| 1805 | |||
| 1806 | public function testRemoveFieldFromFormGroup(): void |
||
| 1832 | |||
| 1833 | public function testGetFilterParameters(): void |
||
| 1873 | |||
| 1874 | public function testGetFilterFieldDescription(): void |
||
| 1945 | |||
| 1946 | public function testGetSubjectNoRequest(): void |
||
| 1958 | |||
| 1959 | public function testGetSideMenu(): void |
||
| 1983 | |||
| 1984 | /** |
||
| 1985 | * @return array |
||
| 1986 | */ |
||
| 1987 | public function provideGetSubject() |
||
| 1997 | |||
| 1998 | /** |
||
| 1999 | * @dataProvider provideGetSubject |
||
| 2000 | */ |
||
| 2001 | public function testGetSubjectFailed($id): void |
||
| 2016 | |||
| 2017 | /** |
||
| 2018 | * @dataProvider provideGetSubject |
||
| 2019 | */ |
||
| 2020 | public function testGetSubject($id): void |
||
| 2039 | |||
| 2040 | public function testGetSubjectWithParentDescription(): void |
||
| 2069 | |||
| 2070 | /** |
||
| 2071 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 2072 | */ |
||
| 2073 | public function testGetActionButtonsList(): void |
||
| 2106 | |||
| 2107 | /** |
||
| 2108 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 2109 | */ |
||
| 2110 | public function testGetActionButtonsListCreateDisabled(): void |
||
| 2124 | |||
| 2125 | /** |
||
| 2126 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions |
||
| 2127 | */ |
||
| 2128 | public function testGetBatchActions(): void |
||
| 2182 | |||
| 2183 | /** |
||
| 2184 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 2185 | */ |
||
| 2186 | public function testShowMosaicButton(): void |
||
| 2195 | |||
| 2196 | /** |
||
| 2197 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 2198 | */ |
||
| 2199 | public function testShowMosaicButtonHideMosaic(): void |
||
| 2209 | |||
| 2210 | /** |
||
| 2211 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions |
||
| 2212 | * @dataProvider provideGetBaseRouteName |
||
| 2213 | */ |
||
| 2214 | public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void |
||
| 2245 | |||
| 2246 | public function testDefaultFilters(): void |
||
| 2305 | |||
| 2306 | /** |
||
| 2307 | * @group legacy |
||
| 2308 | */ |
||
| 2309 | public function testDefaultBreadcrumbsBuilder(): void |
||
| 2333 | |||
| 2334 | /** |
||
| 2335 | * @group legacy |
||
| 2336 | */ |
||
| 2337 | public function testBreadcrumbsBuilderSetter(): void |
||
| 2347 | |||
| 2348 | /** |
||
| 2349 | * @group legacy |
||
| 2350 | */ |
||
| 2351 | public function testGetBreadcrumbs(): void |
||
| 2361 | |||
| 2362 | /** |
||
| 2363 | * @group legacy |
||
| 2364 | */ |
||
| 2365 | public function testBuildBreadcrumbs(): void |
||
| 2382 | |||
| 2383 | /** |
||
| 2384 | * NEXT_MAJOR: remove this method. |
||
| 2385 | * |
||
| 2386 | * @group legacy |
||
| 2387 | */ |
||
| 2388 | public function testCreateQueryLegacyCallWorks(): void |
||
| 2403 | |||
| 2404 | public function testGetDataSourceIterator(): void |
||
| 2446 | |||
| 2447 | public function testCircularChildAdmin(): void |
||
| 2467 | |||
| 2468 | public function testCircularChildAdminTripleLevel(): void |
||
| 2494 | |||
| 2495 | public function testCircularChildAdminWithItself(): void |
||
| 2509 | |||
| 2510 | public function testGetRootAncestor(): void |
||
| 2544 | |||
| 2545 | public function testGetChildDepth(): void |
||
| 2579 | |||
| 2580 | public function testGetCurrentLeafChildAdmin(): void |
||
| 2617 | |||
| 2618 | public function testAdminWithoutControllerName(): void |
||
| 2624 | |||
| 2625 | public function testAdminAvoidInifiniteLoop(): void |
||
| 2655 | |||
| 2656 | /** |
||
| 2657 | * NEXT_MAJOR: Remove this test and its data provider. |
||
| 2658 | * |
||
| 2659 | * @group legacy |
||
| 2660 | * |
||
| 2661 | * @dataProvider getDeprecatedAbstractAdminConstructorArgs |
||
| 2662 | * |
||
| 2663 | * @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. |
||
| 2664 | */ |
||
| 2665 | public function testDeprecatedAbstractAdminConstructorArgs($code, $class, $baseControllerName): void |
||
| 2669 | |||
| 2670 | public function getDeprecatedAbstractAdminConstructorArgs(): iterable |
||
| 2684 | } |
||
| 2685 |
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: