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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 76 | class AdminTest extends TestCase |
||
| 77 | { |
||
| 78 | protected $cacheTempFolder; |
||
| 79 | |||
| 80 | public function setUp(): void |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct |
||
| 89 | */ |
||
| 90 | public function testConstructor(): void |
||
| 100 | |||
| 101 | public function testGetClass(): void |
||
| 124 | |||
| 125 | public function testGetClassException(): void |
||
| 139 | |||
| 140 | public function testCheckAccessThrowsExceptionOnMadeUpAction(): void |
||
| 155 | |||
| 156 | public function testCheckAccessThrowsAccessDeniedException(): void |
||
| 180 | |||
| 181 | public function testHasAccessOnMadeUpAction(): void |
||
| 191 | |||
| 192 | public function testHasAccess(): void |
||
| 211 | |||
| 212 | public function testHasAccessAllowsAccess(): void |
||
| 231 | |||
| 232 | public function testHasAccessAllowsAccessEditAction(): void |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild |
||
| 253 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild |
||
| 254 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild |
||
| 255 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild |
||
| 256 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren |
||
| 257 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren |
||
| 258 | */ |
||
| 259 | public function testChildren(): void |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure |
||
| 284 | */ |
||
| 285 | public function testConfigure(): void |
||
| 300 | |||
| 301 | public function testConfigureWithValidParentAssociationMapping(): void |
||
| 309 | |||
| 310 | public function provideGetBaseRoutePattern() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @dataProvider provideGetBaseRoutePattern |
||
| 378 | */ |
||
| 379 | public function testGetBaseRoutePattern($objFqn, $expected): void |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @dataProvider provideGetBaseRoutePattern |
||
| 387 | */ |
||
| 388 | public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected): void |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @dataProvider provideGetBaseRoutePattern |
||
| 399 | */ |
||
| 400 | public function testGetBaseRoutePatternWithTwoNestedChildAdmin($objFqn, $expected): void |
||
| 418 | |||
| 419 | public function testGetBaseRoutePatternWithSpecifedPattern(): void |
||
| 425 | |||
| 426 | public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void |
||
| 434 | |||
| 435 | public function testGetBaseRoutePatternWithUnreconizedClassname(): void |
||
| 442 | |||
| 443 | public function provideGetBaseRouteName() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @dataProvider provideGetBaseRouteName |
||
| 511 | */ |
||
| 512 | public function testGetBaseRouteName($objFqn, $expected): void |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @group legacy |
||
| 521 | * @expectedDeprecation Calling "addChild" without second argument is deprecated since 3.35 and will not be allowed in 4.0. |
||
| 522 | * @dataProvider provideGetBaseRouteName |
||
| 523 | */ |
||
| 524 | public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected): void |
||
| 609 | |||
| 610 | public function testGetBaseRouteNameWithUnreconizedClassname(): void |
||
| 617 | |||
| 618 | public function testGetBaseRouteNameWithSpecifiedName(): void |
||
| 624 | |||
| 625 | public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void |
||
| 633 | |||
| 634 | public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid |
||
| 659 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid |
||
| 660 | */ |
||
| 661 | public function testSetUniqid(): void |
||
| 670 | |||
| 671 | public function testUniqidConsistency(): void |
||
| 697 | |||
| 698 | public function testToString(): void |
||
| 715 | |||
| 716 | public function testIsAclEnabled(): void |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @group legacy |
||
| 729 | * |
||
| 730 | * @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. |
||
| 731 | * |
||
| 732 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses |
||
| 733 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass |
||
| 734 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses |
||
| 735 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass |
||
| 736 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 737 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass |
||
| 738 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode |
||
| 739 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass |
||
| 740 | */ |
||
| 741 | public function testSubClass(): void |
||
| 742 | { |
||
| 743 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
| 744 | $admin = new PostAdmin( |
||
| 745 | 'sonata.post.admin.post', |
||
| 746 | Post::class, |
||
| 747 | 'SonataNewsBundle:PostAdmin' |
||
| 748 | ); |
||
| 749 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 750 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 751 | $this->assertCount(0, $admin->getSubClasses()); |
||
| 752 | $this->assertNull($admin->getActiveSubClass()); |
||
| 753 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 754 | $this->assertSame(Post::class, $admin->getClass()); |
||
| 755 | |||
| 756 | // Just for the record, if there is no inheritance set, the getSubject is not used |
||
| 757 | // the getSubject can also lead to some issue |
||
| 758 | $admin->setSubject(new BlogPost()); |
||
| 759 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
| 760 | |||
| 761 | $admin->setSubClasses([ |
||
| 762 | 'extended1' => 'NewsBundle\Entity\PostExtended1', |
||
| 763 | 'extended2' => 'NewsBundle\Entity\PostExtended2', |
||
| 764 | ]); |
||
| 765 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 766 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 767 | $this->assertFalse($admin->hasActiveSubClass()); |
||
| 768 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 769 | // NEXT_MAJOR: remove the following 2 `assertNull()` assertions |
||
| 770 | $this->assertNull($admin->getActiveSubClass()); |
||
| 771 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 772 | $this->assertSame( |
||
| 773 | BlogPost::class, |
||
| 774 | $admin->getClass(), |
||
| 775 | 'When there is no subclass in the query the class parameter should be returned' |
||
| 776 | ); |
||
| 777 | |||
| 778 | $request = new Request(['subclass' => 'extended1']); |
||
| 779 | $admin->setRequest($request); |
||
| 780 | $this->assertFalse($admin->hasSubClass('test')); |
||
| 781 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
| 782 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 783 | $this->assertCount(2, $admin->getSubClasses()); |
||
| 784 | $this->assertSame( |
||
| 785 | 'NewsBundle\Entity\PostExtended1', |
||
| 786 | $admin->getActiveSubClass(), |
||
| 787 | 'It should return the curently active sub class.' |
||
| 788 | ); |
||
| 789 | $this->assertSame('extended1', $admin->getActiveSubclassCode()); |
||
| 790 | $this->assertSame( |
||
| 791 | 'NewsBundle\Entity\PostExtended1', |
||
| 792 | $admin->getClass(), |
||
| 793 | 'getClass() should return the name of the sub class when passed through a request query parameter.' |
||
| 794 | ); |
||
| 795 | |||
| 796 | $request->query->set('subclass', 'inject'); |
||
| 797 | |||
| 798 | $this->assertNull($admin->getActiveSubclassCode()); |
||
| 799 | // NEXT_MAJOR: remove the previous `assertNull()` assertion and uncomment the following lines |
||
| 800 | // $this->expectException(\LogicException::class); |
||
| 801 | // $this->expectExceptionMessage(sprintf('Admin "%s" has no active subclass.', PostAdmin::class)); |
||
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * @group legacy |
||
| 806 | * |
||
| 807 | * @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. |
||
| 808 | */ |
||
| 809 | public function testNonExistantSubclass(): void |
||
| 810 | { |
||
| 811 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
| 812 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 813 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
| 814 | |||
| 815 | $admin->setRequest(new Request(['subclass' => 'inject'])); |
||
| 816 | |||
| 817 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
| 818 | |||
| 819 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 820 | |||
| 821 | $this->expectException(\RuntimeException::class); |
||
| 822 | |||
| 823 | $admin->getActiveSubClass(); |
||
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
| 828 | */ |
||
| 829 | public function testOnlyOneSubclassNeededToBeActive(): void |
||
| 830 | { |
||
| 831 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 832 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']); |
||
| 833 | $request = new Request(['subclass' => 'extended1']); |
||
| 834 | $admin->setRequest($request); |
||
| 835 | $this->assertTrue($admin->hasActiveSubClass()); |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @group legacy |
||
| 840 | * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since 3.30 and will be removed in 4.0. |
||
| 841 | */ |
||
| 842 | public function testAddSubClassIsDeprecated(): void |
||
| 843 | { |
||
| 844 | $admin = new PostAdmin( |
||
| 845 | 'sonata.post.admin.post', |
||
| 846 | Post::class, |
||
| 847 | 'SonataNewsBundle:PostAdmin' |
||
| 848 | ); |
||
| 849 | $admin->addSubClass('whatever'); |
||
| 850 | } |
||
| 851 | |||
| 852 | public function testGetPerPageOptions(): void |
||
| 853 | { |
||
| 854 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 855 | |||
| 856 | $perPageOptions = $admin->getPerPageOptions(); |
||
| 857 | |||
| 858 | foreach ($perPageOptions as $perPage) { |
||
| 859 | $this->assertSame(0, $perPage % 4); |
||
| 860 | } |
||
| 861 | |||
| 862 | $admin->setPerPageOptions([500, 1000]); |
||
| 863 | $this->assertSame([500, 1000], $admin->getPerPageOptions()); |
||
| 864 | } |
||
| 865 | |||
| 866 | public function testGetLabelTranslatorStrategy(): void |
||
| 867 | { |
||
| 868 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 869 | |||
| 870 | $this->assertNull($admin->getLabelTranslatorStrategy()); |
||
| 871 | |||
| 872 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
| 873 | $admin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
| 874 | $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy()); |
||
| 875 | } |
||
| 876 | |||
| 877 | public function testGetRouteBuilder(): void |
||
| 878 | { |
||
| 879 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 880 | |||
| 881 | $this->assertNull($admin->getRouteBuilder()); |
||
| 882 | |||
| 883 | $routeBuilder = $this->createMock(RouteBuilderInterface::class); |
||
| 884 | $admin->setRouteBuilder($routeBuilder); |
||
| 885 | $this->assertSame($routeBuilder, $admin->getRouteBuilder()); |
||
| 886 | } |
||
| 887 | |||
| 888 | public function testGetMenuFactory(): void |
||
| 889 | { |
||
| 890 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 891 | |||
| 892 | $this->assertNull($admin->getMenuFactory()); |
||
| 893 | |||
| 894 | $menuFactory = $this->createMock(FactoryInterface::class); |
||
| 895 | $admin->setMenuFactory($menuFactory); |
||
| 896 | $this->assertSame($menuFactory, $admin->getMenuFactory()); |
||
| 897 | } |
||
| 898 | |||
| 899 | public function testGetExtensions(): void |
||
| 900 | { |
||
| 901 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 902 | |||
| 903 | $this->assertSame([], $admin->getExtensions()); |
||
| 904 | |||
| 905 | $adminExtension1 = $this->createMock(AdminExtensionInterface::class); |
||
| 906 | $adminExtension2 = $this->createMock(AdminExtensionInterface::class); |
||
| 907 | |||
| 908 | $admin->addExtension($adminExtension1); |
||
| 909 | $admin->addExtension($adminExtension2); |
||
| 910 | $this->assertSame([$adminExtension1, $adminExtension2], $admin->getExtensions()); |
||
| 911 | } |
||
| 912 | |||
| 913 | public function testGetFilterTheme(): void |
||
| 914 | { |
||
| 915 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 916 | |||
| 917 | $this->assertSame([], $admin->getFilterTheme()); |
||
| 918 | |||
| 919 | $admin->setFilterTheme(['FooTheme']); |
||
| 920 | $this->assertSame(['FooTheme'], $admin->getFilterTheme()); |
||
| 921 | } |
||
| 922 | |||
| 923 | public function testGetFormTheme(): void |
||
| 924 | { |
||
| 925 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 926 | |||
| 927 | $this->assertSame([], $admin->getFormTheme()); |
||
| 928 | |||
| 929 | $admin->setFormTheme(['FooTheme']); |
||
| 930 | |||
| 931 | $this->assertSame(['FooTheme'], $admin->getFormTheme()); |
||
| 932 | } |
||
| 933 | |||
| 934 | public function testGetValidator(): void |
||
| 935 | { |
||
| 936 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 937 | |||
| 938 | $this->assertNull($admin->getValidator()); |
||
| 939 | |||
| 940 | $validator = $this->getMockForAbstractClass(ValidatorInterface::class); |
||
| 941 | |||
| 942 | $admin->setValidator($validator); |
||
| 943 | $this->assertSame($validator, $admin->getValidator()); |
||
| 944 | } |
||
| 945 | |||
| 946 | public function testGetSecurityHandler(): void |
||
| 947 | { |
||
| 948 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 949 | |||
| 950 | $this->assertNull($admin->getSecurityHandler()); |
||
| 951 | |||
| 952 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
| 953 | $admin->setSecurityHandler($securityHandler); |
||
| 954 | $this->assertSame($securityHandler, $admin->getSecurityHandler()); |
||
| 955 | } |
||
| 956 | |||
| 957 | public function testGetSecurityInformation(): void |
||
| 958 | { |
||
| 959 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 960 | |||
| 961 | $this->assertSame([], $admin->getSecurityInformation()); |
||
| 962 | |||
| 963 | $securityInformation = [ |
||
| 964 | 'GUEST' => ['VIEW', 'LIST'], |
||
| 965 | 'STAFF' => ['EDIT', 'LIST', 'CREATE'], |
||
| 966 | ]; |
||
| 967 | |||
| 968 | $admin->setSecurityInformation($securityInformation); |
||
| 969 | $this->assertSame($securityInformation, $admin->getSecurityInformation()); |
||
| 970 | } |
||
| 971 | |||
| 972 | public function testGetManagerType(): void |
||
| 973 | { |
||
| 974 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 975 | |||
| 976 | $this->assertNull($admin->getManagerType()); |
||
| 977 | |||
| 978 | $admin->setManagerType('foo_orm'); |
||
| 979 | $this->assertSame('foo_orm', $admin->getManagerType()); |
||
| 980 | } |
||
| 981 | |||
| 982 | public function testGetModelManager(): void |
||
| 983 | { |
||
| 984 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 985 | |||
| 986 | $this->assertNull($admin->getModelManager()); |
||
| 987 | |||
| 988 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 989 | |||
| 990 | $admin->setModelManager($modelManager); |
||
| 991 | $this->assertSame($modelManager, $admin->getModelManager()); |
||
| 992 | } |
||
| 993 | |||
| 994 | /** |
||
| 995 | * NEXT_MAJOR: remove this method. |
||
| 996 | * |
||
| 997 | * @group legacy |
||
| 998 | */ |
||
| 999 | public function testGetBaseCodeRoute(): void |
||
| 1000 | { |
||
| 1001 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1002 | |||
| 1003 | $this->assertSame('', $admin->getBaseCodeRoute()); |
||
| 1004 | |||
| 1005 | $admin->setBaseCodeRoute('foo'); |
||
| 1006 | $this->assertSame('foo', $admin->getBaseCodeRoute()); |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | // NEXT_MAJOR: uncomment this method. |
||
| 1010 | // public function testGetBaseCodeRoute() |
||
| 1011 | // { |
||
| 1012 | // $postAdmin = new PostAdmin( |
||
| 1013 | // 'sonata.post.admin.post', |
||
| 1014 | // 'NewsBundle\Entity\Post', |
||
| 1015 | // 'SonataNewsBundle:PostAdmin' |
||
| 1016 | // ); |
||
| 1017 | // $commentAdmin = new CommentAdmin( |
||
| 1018 | // 'sonata.post.admin.comment', |
||
| 1019 | // 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 1020 | // 'SonataNewsBundle:CommentAdmin' |
||
| 1021 | // ); |
||
| 1022 | // |
||
| 1023 | // $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute()); |
||
| 1024 | // |
||
| 1025 | // $postAdmin->addChild($commentAdmin); |
||
| 1026 | // |
||
| 1027 | // $this->assertSame( |
||
| 1028 | // 'sonata.post.admin.post|sonata.post.admin.comment', |
||
| 1029 | // $commentAdmin->getBaseCodeRoute() |
||
| 1030 | // ); |
||
| 1031 | // } |
||
| 1032 | |||
| 1033 | public function testGetRouteGenerator(): void |
||
| 1034 | { |
||
| 1035 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1036 | |||
| 1037 | $this->assertNull($admin->getRouteGenerator()); |
||
| 1038 | |||
| 1039 | $routeGenerator = $this->createMock(RouteGeneratorInterface::class); |
||
| 1040 | |||
| 1041 | $admin->setRouteGenerator($routeGenerator); |
||
| 1042 | $this->assertSame($routeGenerator, $admin->getRouteGenerator()); |
||
| 1043 | } |
||
| 1044 | |||
| 1045 | public function testGetConfigurationPool(): void |
||
| 1046 | { |
||
| 1047 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1048 | |||
| 1049 | $this->assertNull($admin->getConfigurationPool()); |
||
| 1050 | |||
| 1051 | $pool = $this->getMockBuilder(Pool::class) |
||
| 1052 | ->disableOriginalConstructor() |
||
| 1053 | ->getMock(); |
||
| 1054 | |||
| 1055 | $admin->setConfigurationPool($pool); |
||
| 1056 | $this->assertSame($pool, $admin->getConfigurationPool()); |
||
| 1057 | } |
||
| 1058 | |||
| 1059 | public function testGetShowBuilder(): void |
||
| 1060 | { |
||
| 1061 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1062 | |||
| 1063 | $this->assertNull($admin->getShowBuilder()); |
||
| 1064 | |||
| 1065 | $showBuilder = $this->createMock(ShowBuilderInterface::class); |
||
| 1066 | |||
| 1067 | $admin->setShowBuilder($showBuilder); |
||
| 1068 | $this->assertSame($showBuilder, $admin->getShowBuilder()); |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | public function testGetListBuilder(): void |
||
| 1072 | { |
||
| 1073 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1074 | |||
| 1075 | $this->assertNull($admin->getListBuilder()); |
||
| 1076 | |||
| 1077 | $listBuilder = $this->createMock(ListBuilderInterface::class); |
||
| 1078 | |||
| 1079 | $admin->setListBuilder($listBuilder); |
||
| 1080 | $this->assertSame($listBuilder, $admin->getListBuilder()); |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | public function testGetDatagridBuilder(): void |
||
| 1084 | { |
||
| 1085 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1086 | |||
| 1087 | $this->assertNull($admin->getDatagridBuilder()); |
||
| 1088 | |||
| 1089 | $datagridBuilder = $this->createMock(DatagridBuilderInterface::class); |
||
| 1090 | |||
| 1091 | $admin->setDatagridBuilder($datagridBuilder); |
||
| 1092 | $this->assertSame($datagridBuilder, $admin->getDatagridBuilder()); |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | public function testGetFormContractor(): void |
||
| 1096 | { |
||
| 1097 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1098 | |||
| 1099 | $this->assertNull($admin->getFormContractor()); |
||
| 1100 | |||
| 1101 | $formContractor = $this->createMock(FormContractorInterface::class); |
||
| 1102 | |||
| 1103 | $admin->setFormContractor($formContractor); |
||
| 1104 | $this->assertSame($formContractor, $admin->getFormContractor()); |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | public function testGetRequest(): void |
||
| 1108 | { |
||
| 1109 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1110 | |||
| 1111 | $this->assertFalse($admin->hasRequest()); |
||
| 1112 | |||
| 1113 | $request = new Request(); |
||
| 1114 | |||
| 1115 | $admin->setRequest($request); |
||
| 1116 | $this->assertSame($request, $admin->getRequest()); |
||
| 1117 | $this->assertTrue($admin->hasRequest()); |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | public function testGetRequestWithException(): void |
||
| 1121 | { |
||
| 1122 | $this->expectException(\RuntimeException::class); |
||
| 1123 | $this->expectExceptionMessage('The Request object has not been set'); |
||
| 1124 | |||
| 1125 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1126 | $admin->getRequest(); |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | public function testGetTranslationDomain(): void |
||
| 1130 | { |
||
| 1131 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1132 | |||
| 1133 | $this->assertSame('messages', $admin->getTranslationDomain()); |
||
| 1134 | |||
| 1135 | $admin->setTranslationDomain('foo'); |
||
| 1136 | $this->assertSame('foo', $admin->getTranslationDomain()); |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * @group legacy |
||
| 1141 | */ |
||
| 1142 | public function testGetTranslator(): void |
||
| 1143 | { |
||
| 1144 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1145 | |||
| 1146 | $this->assertNull($admin->getTranslator()); |
||
| 1147 | |||
| 1148 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1149 | |||
| 1150 | $admin->setTranslator($translator); |
||
| 1151 | $this->assertSame($translator, $admin->getTranslator()); |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | public function testGetShowGroups(): void |
||
| 1155 | { |
||
| 1156 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1157 | |||
| 1158 | $this->assertFalse($admin->getShowGroups()); |
||
| 1159 | |||
| 1160 | $groups = ['foo', 'bar', 'baz']; |
||
| 1161 | |||
| 1162 | $admin->setShowGroups($groups); |
||
| 1163 | $this->assertSame($groups, $admin->getShowGroups()); |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | public function testGetFormGroups(): void |
||
| 1167 | { |
||
| 1168 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1169 | |||
| 1170 | $this->assertFalse($admin->getFormGroups()); |
||
| 1171 | |||
| 1172 | $groups = ['foo', 'bar', 'baz']; |
||
| 1173 | |||
| 1174 | $admin->setFormGroups($groups); |
||
| 1175 | $this->assertSame($groups, $admin->getFormGroups()); |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | public function testGetMaxPageLinks(): void |
||
| 1179 | { |
||
| 1180 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1181 | |||
| 1182 | $this->assertSame(25, $admin->getMaxPageLinks()); |
||
| 1183 | |||
| 1184 | $admin->setMaxPageLinks(14); |
||
| 1185 | $this->assertSame(14, $admin->getMaxPageLinks()); |
||
| 1186 | } |
||
| 1187 | |||
| 1188 | public function testGetMaxPerPage(): void |
||
| 1189 | { |
||
| 1190 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1191 | |||
| 1192 | $this->assertSame(32, $admin->getMaxPerPage()); |
||
| 1193 | |||
| 1194 | $admin->setMaxPerPage(94); |
||
| 1195 | $this->assertSame(94, $admin->getMaxPerPage()); |
||
| 1196 | } |
||
| 1197 | |||
| 1198 | public function testGetLabel(): void |
||
| 1199 | { |
||
| 1200 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1201 | |||
| 1202 | $this->assertNull($admin->getLabel()); |
||
| 1203 | |||
| 1204 | $admin->setLabel('FooLabel'); |
||
| 1205 | $this->assertSame('FooLabel', $admin->getLabel()); |
||
| 1206 | } |
||
| 1207 | |||
| 1208 | public function testGetBaseController(): void |
||
| 1209 | { |
||
| 1210 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1211 | |||
| 1212 | $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName()); |
||
| 1213 | |||
| 1214 | $admin->setBaseControllerName('SonataNewsBundle:FooAdmin'); |
||
| 1215 | $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName()); |
||
| 1216 | } |
||
| 1217 | |||
| 1218 | public function testGetTemplates(): void |
||
| 1219 | { |
||
| 1220 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1221 | |||
| 1222 | $templates = [ |
||
| 1223 | 'list' => '@FooAdmin/CRUD/list.html.twig', |
||
| 1224 | 'show' => '@FooAdmin/CRUD/show.html.twig', |
||
| 1225 | 'edit' => '@FooAdmin/CRUD/edit.html.twig', |
||
| 1226 | ]; |
||
| 1227 | |||
| 1228 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 1229 | $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates); |
||
| 1230 | |||
| 1231 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 1232 | |||
| 1233 | $this->assertSame($templates, $admin->getTemplates()); |
||
| 1234 | } |
||
| 1235 | |||
| 1236 | public function testGetTemplate1(): void |
||
| 1237 | { |
||
| 1238 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1239 | |||
| 1240 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 1241 | $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig'); |
||
| 1242 | $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig'); |
||
| 1243 | |||
| 1244 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 1245 | |||
| 1246 | $this->assertSame('@FooAdmin/CRUD/edit.html.twig', $admin->getTemplate('edit')); |
||
| 1247 | $this->assertSame('@FooAdmin/CRUD/show.html.twig', $admin->getTemplate('show')); |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | public function testGetIdParameter(): void |
||
| 1251 | { |
||
| 1252 | $postAdmin = new PostAdmin( |
||
| 1253 | 'sonata.post.admin.post', |
||
| 1254 | 'NewsBundle\Entity\Post', |
||
| 1255 | 'SonataNewsBundle:PostAdmin' |
||
| 1256 | ); |
||
| 1257 | |||
| 1258 | $this->assertSame('id', $postAdmin->getIdParameter()); |
||
| 1259 | $this->assertFalse($postAdmin->isChild()); |
||
| 1260 | |||
| 1261 | $commentAdmin = new CommentAdmin( |
||
| 1262 | 'sonata.post.admin.comment', |
||
| 1263 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
| 1264 | 'SonataNewsBundle:CommentAdmin' |
||
| 1265 | ); |
||
| 1266 | $commentAdmin->setParent($postAdmin); |
||
| 1267 | |||
| 1268 | $this->assertTrue($commentAdmin->isChild()); |
||
| 1269 | $this->assertSame('childId', $commentAdmin->getIdParameter()); |
||
| 1270 | |||
| 1271 | $commentVoteAdmin = new CommentVoteAdmin( |
||
| 1272 | 'sonata.post.admin.comment_vote', |
||
| 1273 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
| 1274 | 'SonataNewsBundle:CommentVoteAdmin' |
||
| 1275 | ); |
||
| 1276 | $commentVoteAdmin->setParent($commentAdmin); |
||
| 1277 | |||
| 1278 | $this->assertTrue($commentVoteAdmin->isChild()); |
||
| 1279 | $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter()); |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | public function testGetExportFormats(): void |
||
| 1283 | { |
||
| 1284 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1285 | |||
| 1286 | $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats()); |
||
| 1287 | } |
||
| 1288 | |||
| 1289 | public function testGetUrlsafeIdentifier(): void |
||
| 1290 | { |
||
| 1291 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1292 | |||
| 1293 | $entity = new \stdClass(); |
||
| 1294 | |||
| 1295 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1296 | $modelManager->expects($this->once()) |
||
| 1297 | ->method('getUrlsafeIdentifier') |
||
| 1298 | ->with($this->equalTo($entity)) |
||
| 1299 | ->willReturn('foo'); |
||
| 1300 | $admin->setModelManager($modelManager); |
||
| 1301 | |||
| 1302 | $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity)); |
||
| 1303 | } |
||
| 1304 | |||
| 1305 | public function testDeterminedPerPageValue(): void |
||
| 1306 | { |
||
| 1307 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1308 | |||
| 1309 | $this->assertFalse($admin->determinedPerPageValue('foo')); |
||
| 1310 | $this->assertFalse($admin->determinedPerPageValue(123)); |
||
| 1311 | $this->assertTrue($admin->determinedPerPageValue(16)); |
||
| 1312 | |||
| 1313 | $admin->setPerPageOptions([101, 102, 103]); |
||
| 1314 | $this->assertFalse($admin->determinedPerPageValue(16)); |
||
| 1315 | $this->assertTrue($admin->determinedPerPageValue(101)); |
||
| 1316 | } |
||
| 1317 | |||
| 1318 | public function testIsGranted(): void |
||
| 1319 | { |
||
| 1320 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1321 | |||
| 1322 | $entity = new \stdClass(); |
||
| 1323 | |||
| 1324 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1325 | $securityHandler |
||
| 1326 | ->method('isGranted') |
||
| 1327 | ->willReturnCallback(static function (AdminInterface $adminIn, $attributes, $object = null) use ($admin, $entity) { |
||
| 1328 | if ($admin === $adminIn && 'FOO' === $attributes) { |
||
| 1329 | if (($object === $admin) || ($object === $entity)) { |
||
| 1330 | return true; |
||
| 1331 | } |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | return false; |
||
| 1335 | }); |
||
| 1336 | |||
| 1337 | $admin->setSecurityHandler($securityHandler); |
||
| 1338 | |||
| 1339 | $this->assertTrue($admin->isGranted('FOO')); |
||
| 1340 | $this->assertTrue($admin->isGranted('FOO', $entity)); |
||
| 1341 | $this->assertFalse($admin->isGranted('BAR')); |
||
| 1342 | $this->assertFalse($admin->isGranted('BAR', $entity)); |
||
| 1343 | } |
||
| 1344 | |||
| 1345 | public function testSupportsPreviewMode(): void |
||
| 1346 | { |
||
| 1347 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1348 | |||
| 1349 | $this->assertFalse($admin->supportsPreviewMode()); |
||
| 1350 | } |
||
| 1351 | |||
| 1352 | public function testGetPermissionsShow(): void |
||
| 1353 | { |
||
| 1354 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1355 | |||
| 1356 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1357 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU)); |
||
| 1358 | $this->assertSame(['LIST'], $admin->getPermissionsShow('foo')); |
||
| 1359 | } |
||
| 1360 | |||
| 1361 | public function testShowIn(): void |
||
| 1362 | { |
||
| 1363 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1364 | |||
| 1365 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
| 1366 | $securityHandler |
||
| 1367 | ->method('isGranted') |
||
| 1368 | ->willReturnCallback(static function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) { |
||
| 1369 | return $admin === $adminIn && $attributes === ['LIST']; |
||
| 1370 | }); |
||
| 1371 | |||
| 1372 | $admin->setSecurityHandler($securityHandler); |
||
| 1373 | |||
| 1374 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
| 1375 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU)); |
||
| 1376 | $this->assertTrue($admin->showIn('foo')); |
||
| 1377 | } |
||
| 1378 | |||
| 1379 | public function testGetObjectIdentifier(): void |
||
| 1380 | { |
||
| 1381 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1382 | |||
| 1383 | $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier()); |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * @group legacy |
||
| 1388 | */ |
||
| 1389 | public function testTrans(): void |
||
| 1390 | { |
||
| 1391 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1392 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1393 | |||
| 1394 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1395 | $admin->setTranslator($translator); |
||
| 1396 | |||
| 1397 | $translator->expects($this->once()) |
||
| 1398 | ->method('trans') |
||
| 1399 | ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1400 | ->willReturn('fooTranslated'); |
||
| 1401 | |||
| 1402 | $this->assertSame('fooTranslated', $admin->trans('foo')); |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * @group legacy |
||
| 1407 | */ |
||
| 1408 | public function testTransWithMessageDomain(): void |
||
| 1409 | { |
||
| 1410 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1411 | |||
| 1412 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1413 | $admin->setTranslator($translator); |
||
| 1414 | |||
| 1415 | $translator->expects($this->once()) |
||
| 1416 | ->method('trans') |
||
| 1417 | ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1418 | ->willReturn('fooTranslated'); |
||
| 1419 | |||
| 1420 | $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1421 | } |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * @group legacy |
||
| 1425 | */ |
||
| 1426 | public function testTransChoice(): void |
||
| 1427 | { |
||
| 1428 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1429 | $admin->setTranslationDomain('fooMessageDomain'); |
||
| 1430 | |||
| 1431 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1432 | $admin->setTranslator($translator); |
||
| 1433 | |||
| 1434 | $translator->expects($this->once()) |
||
| 1435 | ->method('transChoice') |
||
| 1436 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
| 1437 | ->willReturn('fooTranslated'); |
||
| 1438 | |||
| 1439 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2)); |
||
| 1440 | } |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * @group legacy |
||
| 1444 | */ |
||
| 1445 | public function testTransChoiceWithMessageDomain(): void |
||
| 1446 | { |
||
| 1447 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1448 | |||
| 1449 | $translator = $this->createMock(TranslatorInterface::class); |
||
| 1450 | $admin->setTranslator($translator); |
||
| 1451 | |||
| 1452 | $translator->expects($this->once()) |
||
| 1453 | ->method('transChoice') |
||
| 1454 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
| 1455 | ->willReturn('fooTranslated'); |
||
| 1456 | |||
| 1457 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain')); |
||
| 1458 | } |
||
| 1459 | |||
| 1460 | public function testSetFilterPersister(): void |
||
| 1461 | { |
||
| 1462 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1463 | |||
| 1464 | $filterPersister = $this->createMock('Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface'); |
||
| 1465 | |||
| 1466 | $this->assertAttributeSame(null, 'filterPersister', $admin); |
||
| 1467 | $admin->setFilterPersister($filterPersister); |
||
| 1468 | $this->assertAttributeSame($filterPersister, 'filterPersister', $admin); |
||
| 1469 | $this->assertAttributeSame(true, 'persistFilters', $admin); |
||
| 1470 | } |
||
| 1471 | |||
| 1472 | public function testGetRootCode(): void |
||
| 1473 | { |
||
| 1474 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1475 | |||
| 1476 | $this->assertSame('sonata.post.admin.post', $admin->getRootCode()); |
||
| 1477 | |||
| 1478 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin'); |
||
| 1479 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
| 1480 | $parentFieldDescription->expects($this->once()) |
||
| 1481 | ->method('getAdmin') |
||
| 1482 | ->willReturn($parentAdmin); |
||
| 1483 | |||
| 1484 | $this->assertNull($admin->getParentFieldDescription()); |
||
| 1485 | $admin->setParentFieldDescription($parentFieldDescription); |
||
| 1486 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
| 1487 | $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode()); |
||
| 1488 | } |
||
| 1489 | |||
| 1490 | public function testGetRoot(): void |
||
| 1491 | { |
||
| 1492 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1493 | |||
| 1494 | $this->assertSame($admin, $admin->getRoot()); |
||
| 1495 | |||
| 1496 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin'); |
||
| 1497 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
| 1498 | $parentFieldDescription->expects($this->once()) |
||
| 1499 | ->method('getAdmin') |
||
| 1500 | ->willReturn($parentAdmin); |
||
| 1501 | |||
| 1502 | $this->assertNull($admin->getParentFieldDescription()); |
||
| 1503 | $admin->setParentFieldDescription($parentFieldDescription); |
||
| 1504 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
| 1505 | $this->assertSame($parentAdmin, $admin->getRoot()); |
||
| 1506 | } |
||
| 1507 | |||
| 1508 | public function testGetExportFields(): void |
||
| 1509 | { |
||
| 1510 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1511 | |||
| 1512 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1513 | $modelManager->expects($this->once()) |
||
| 1514 | ->method('getExportFields') |
||
| 1515 | ->with($this->equalTo('NewsBundle\Entity\Post')) |
||
| 1516 | ->willReturn(['foo', 'bar']); |
||
| 1517 | |||
| 1518 | $admin->setModelManager($modelManager); |
||
| 1519 | $this->assertSame(['foo', 'bar'], $admin->getExportFields()); |
||
| 1520 | } |
||
| 1521 | |||
| 1522 | public function testGetPersistentParametersWithNoExtension(): void |
||
| 1523 | { |
||
| 1524 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1525 | |||
| 1526 | $this->assertEmpty($admin->getPersistentParameters()); |
||
| 1527 | } |
||
| 1528 | |||
| 1529 | public function testGetPersistentParametersWithInvalidExtension(): void |
||
| 1530 | { |
||
| 1531 | $this->expectException(\RuntimeException::class); |
||
| 1532 | |||
| 1533 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1534 | |||
| 1535 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
| 1536 | $extension->expects($this->once())->method('getPersistentParameters')->willReturn(null); |
||
| 1537 | |||
| 1538 | $admin->addExtension($extension); |
||
| 1539 | |||
| 1540 | $admin->getPersistentParameters(); |
||
| 1541 | } |
||
| 1542 | |||
| 1543 | public function testGetPersistentParametersWithValidExtension(): void |
||
| 1544 | { |
||
| 1545 | $expected = [ |
||
| 1546 | 'context' => 'foobar', |
||
| 1547 | ]; |
||
| 1548 | |||
| 1549 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1550 | |||
| 1551 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
| 1552 | $extension->expects($this->once())->method('getPersistentParameters')->willReturn($expected); |
||
| 1553 | |||
| 1554 | $admin->addExtension($extension); |
||
| 1555 | |||
| 1556 | $this->assertSame($expected, $admin->getPersistentParameters()); |
||
| 1557 | } |
||
| 1558 | |||
| 1559 | public function testGetFormWithNonCollectionParentValue(): void |
||
| 1560 | { |
||
| 1561 | $post = new Post(); |
||
| 1562 | $tagAdmin = $this->createTagAdmin($post); |
||
| 1563 | $tag = $tagAdmin->getSubject(); |
||
| 1564 | |||
| 1565 | $tag->setPosts(null); |
||
| 1566 | $tagAdmin->getForm(); |
||
| 1567 | $this->assertSame($post, $tag->getPosts()); |
||
| 1568 | } |
||
| 1569 | |||
| 1570 | public function testGetFormWithCollectionParentValue(): void |
||
| 1571 | { |
||
| 1572 | $post = new Post(); |
||
| 1573 | $tagAdmin = $this->createTagAdmin($post); |
||
| 1574 | $tag = $tagAdmin->getSubject(); |
||
| 1575 | |||
| 1576 | // Case of a doctrine collection |
||
| 1577 | $this->assertInstanceOf(Collection::class, $tag->getPosts()); |
||
| 1578 | $this->assertCount(0, $tag->getPosts()); |
||
| 1579 | |||
| 1580 | $tag->addPost(new Post()); |
||
| 1581 | |||
| 1582 | $this->assertCount(1, $tag->getPosts()); |
||
| 1583 | |||
| 1584 | $tagAdmin->getForm(); |
||
| 1585 | |||
| 1586 | $this->assertInstanceOf(Collection::class, $tag->getPosts()); |
||
| 1587 | $this->assertCount(2, $tag->getPosts()); |
||
| 1588 | $this->assertContains($post, $tag->getPosts()); |
||
| 1589 | |||
| 1590 | // Case of an array |
||
| 1591 | $tag->setPosts([]); |
||
| 1592 | $this->assertCount(0, $tag->getPosts()); |
||
| 1593 | |||
| 1594 | $tag->addPost(new Post()); |
||
| 1595 | |||
| 1596 | $this->assertCount(1, $tag->getPosts()); |
||
| 1597 | |||
| 1598 | $tagAdmin->getForm(); |
||
| 1599 | |||
| 1600 | $this->assertInternalType('array', $tag->getPosts()); |
||
| 1601 | $this->assertCount(2, $tag->getPosts()); |
||
| 1602 | $this->assertContains($post, $tag->getPosts()); |
||
| 1603 | } |
||
| 1604 | |||
| 1605 | public function testFormAddPostSubmitEventForPreValidation(): void |
||
| 1606 | { |
||
| 1607 | $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin'); |
||
| 1608 | $object = new \stdClass(); |
||
| 1609 | |||
| 1610 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
| 1611 | $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
| 1612 | |||
| 1613 | $validator = $this->createMock(ValidatorInterface::class); |
||
| 1614 | $validator |
||
| 1615 | ->method('getMetadataFor') |
||
| 1616 | ->willReturn($this->createMock(MemberMetadata::class)); |
||
| 1617 | $modelAdmin->setValidator($validator); |
||
| 1618 | |||
| 1619 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1620 | $modelManager |
||
| 1621 | ->method('getNewFieldDescriptionInstance') |
||
| 1622 | ->willReturn(new FieldDescription()); |
||
| 1623 | $modelAdmin->setModelManager($modelManager); |
||
| 1624 | |||
| 1625 | // a Admin class to test that preValidate is called |
||
| 1626 | $testAdminPreValidate = $this->createMock(AbstractAdmin::class); |
||
| 1627 | $testAdminPreValidate->expects($this->once()) |
||
| 1628 | ->method('preValidate') |
||
| 1629 | ->with($this->identicalTo($object)); |
||
| 1630 | |||
| 1631 | $event = $this->createMock(FormEvent::class); |
||
| 1632 | $event |
||
| 1633 | ->method('getData') |
||
| 1634 | ->willReturn($object); |
||
| 1635 | |||
| 1636 | $formBuild = $this->createMock(FormBuilder::class); |
||
| 1637 | $formBuild->expects($this->once()) |
||
| 1638 | ->method('addEventListener') |
||
| 1639 | ->with($this->identicalTo(FormEvents::POST_SUBMIT), |
||
| 1640 | $this->callback(static function ($callback) use ($testAdminPreValidate, $event) { |
||
| 1641 | if (\is_callable($callback)) { |
||
| 1642 | $closure = $callback->bindTo($testAdminPreValidate); |
||
| 1643 | $closure($event); |
||
| 1644 | |||
| 1645 | return true; |
||
| 1646 | } |
||
| 1647 | |||
| 1648 | return false; |
||
| 1649 | }), |
||
| 1650 | $this->greaterThan(0) |
||
| 1651 | ); |
||
| 1652 | |||
| 1653 | $formContractor = $this->createMock(FormContractorInterface::class); |
||
| 1654 | $formContractor |
||
| 1655 | ->method('getDefaultOptions') |
||
| 1656 | ->willReturn([]); |
||
| 1657 | $formContractor |
||
| 1658 | ->method('getFormBuilder') |
||
| 1659 | ->willReturn($formBuild); |
||
| 1660 | |||
| 1661 | $modelAdmin->setFormContractor($formContractor); |
||
| 1662 | $modelAdmin->defineFormBuilder($formBuild); |
||
| 1663 | $modelAdmin->getForm(); |
||
| 1664 | } |
||
| 1665 | |||
| 1666 | public function testRemoveFieldFromFormGroup(): void |
||
| 1667 | { |
||
| 1668 | $formGroups = [ |
||
| 1669 | 'foobar' => [ |
||
| 1670 | 'fields' => [ |
||
| 1671 | 'foo' => 'foo', |
||
| 1672 | 'bar' => 'bar', |
||
| 1673 | ], |
||
| 1674 | ], |
||
| 1675 | ]; |
||
| 1676 | |||
| 1677 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1678 | $admin->setFormGroups($formGroups); |
||
| 1679 | |||
| 1680 | $admin->removeFieldFromFormGroup('foo'); |
||
| 1681 | $this->assertSame($admin->getFormGroups(), [ |
||
| 1682 | 'foobar' => [ |
||
| 1683 | 'fields' => [ |
||
| 1684 | 'bar' => 'bar', |
||
| 1685 | ], |
||
| 1686 | ], |
||
| 1687 | ]); |
||
| 1688 | |||
| 1689 | $admin->removeFieldFromFormGroup('bar'); |
||
| 1690 | $this->assertSame($admin->getFormGroups(), []); |
||
| 1691 | } |
||
| 1692 | |||
| 1693 | public function testGetFilterParameters(): void |
||
| 1694 | { |
||
| 1695 | $authorId = uniqid(); |
||
| 1696 | |||
| 1697 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1698 | |||
| 1699 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
| 1700 | $commentAdmin->setParentAssociationMapping('post.author'); |
||
| 1701 | $commentAdmin->setParent($postAdmin); |
||
| 1702 | |||
| 1703 | $request = $this->createMock(Request::class); |
||
| 1704 | $query = $this->createMock(ParameterBag::class); |
||
| 1705 | $query |
||
| 1706 | ->method('get') |
||
| 1707 | ->willReturn([ |
||
| 1708 | 'filter' => [ |
||
| 1709 | '_page' => '1', |
||
| 1710 | '_per_page' => '32', |
||
| 1711 | ], |
||
| 1712 | ]); |
||
| 1713 | |||
| 1714 | $request->query = $query; |
||
| 1715 | $request |
||
| 1716 | ->method('get') |
||
| 1717 | ->willReturn($authorId); |
||
| 1718 | |||
| 1719 | $commentAdmin->setRequest($request); |
||
| 1720 | |||
| 1721 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1722 | $modelManager |
||
| 1723 | ->method('getDefaultSortValues') |
||
| 1724 | ->willReturn([]); |
||
| 1725 | |||
| 1726 | $commentAdmin->setModelManager($modelManager); |
||
| 1727 | |||
| 1728 | $parameters = $commentAdmin->getFilterParameters(); |
||
| 1729 | |||
| 1730 | $this->assertTrue(isset($parameters['post__author'])); |
||
| 1731 | $this->assertSame(['value' => $authorId], $parameters['post__author']); |
||
| 1732 | } |
||
| 1733 | |||
| 1734 | public function testGetFilterFieldDescription(): void |
||
| 1735 | { |
||
| 1736 | $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin'); |
||
| 1737 | |||
| 1738 | $fooFieldDescription = new FieldDescription(); |
||
| 1739 | $barFieldDescription = new FieldDescription(); |
||
| 1740 | $bazFieldDescription = new FieldDescription(); |
||
| 1741 | |||
| 1742 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1743 | $modelManager->expects($this->exactly(3)) |
||
| 1744 | ->method('getNewFieldDescriptionInstance') |
||
| 1745 | ->willReturnCallback(static function ($adminClass, $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) { |
||
| 1746 | switch ($name) { |
||
| 1747 | case 'foo': |
||
| 1748 | $fieldDescription = $fooFieldDescription; |
||
| 1749 | |||
| 1750 | break; |
||
| 1751 | |||
| 1752 | case 'bar': |
||
| 1753 | $fieldDescription = $barFieldDescription; |
||
| 1754 | |||
| 1755 | break; |
||
| 1756 | |||
| 1757 | case 'baz': |
||
| 1758 | $fieldDescription = $bazFieldDescription; |
||
| 1759 | |||
| 1760 | break; |
||
| 1761 | |||
| 1762 | default: |
||
| 1763 | throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name)); |
||
| 1764 | break; |
||
| 1765 | } |
||
| 1766 | |||
| 1767 | $fieldDescription->setName($name); |
||
| 1768 | |||
| 1769 | return $fieldDescription; |
||
| 1770 | }); |
||
| 1771 | |||
| 1772 | $modelAdmin->setModelManager($modelManager); |
||
| 1773 | |||
| 1774 | $pager = $this->createMock(PagerInterface::class); |
||
| 1775 | |||
| 1776 | $datagrid = $this->createMock(DatagridInterface::class); |
||
| 1777 | $datagrid->expects($this->once()) |
||
| 1778 | ->method('getPager') |
||
| 1779 | ->willReturn($pager); |
||
| 1780 | |||
| 1781 | $datagridBuilder = $this->createMock(DatagridBuilderInterface::class); |
||
| 1782 | $datagridBuilder->expects($this->once()) |
||
| 1783 | ->method('getBaseDatagrid') |
||
| 1784 | ->with($this->identicalTo($modelAdmin), []) |
||
| 1785 | ->willReturn($datagrid); |
||
| 1786 | |||
| 1787 | $datagridBuilder->expects($this->exactly(3)) |
||
| 1788 | ->method('addFilter') |
||
| 1789 | ->willReturnCallback(static function ($datagrid, $type, $fieldDescription, AdminInterface $admin): void { |
||
| 1790 | $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription); |
||
| 1791 | $fieldDescription->mergeOption('field_options', ['required' => false]); |
||
| 1792 | }); |
||
| 1793 | |||
| 1794 | $modelAdmin->setDatagridBuilder($datagridBuilder); |
||
| 1795 | |||
| 1796 | $this->assertSame(['foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription], $modelAdmin->getFilterFieldDescriptions()); |
||
| 1797 | $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar')); |
||
| 1798 | $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo')); |
||
| 1799 | $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar')); |
||
| 1800 | $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz')); |
||
| 1801 | $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo')); |
||
| 1802 | $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar')); |
||
| 1803 | $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz')); |
||
| 1804 | } |
||
| 1805 | |||
| 1806 | public function testGetSubjectNoRequest(): void |
||
| 1807 | { |
||
| 1808 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1809 | $modelManager |
||
| 1810 | ->expects($this->never()) |
||
| 1811 | ->method('find'); |
||
| 1812 | |||
| 1813 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1814 | $admin->setModelManager($modelManager); |
||
| 1815 | |||
| 1816 | $this->assertNull($admin->getSubject()); |
||
| 1817 | } |
||
| 1818 | |||
| 1819 | public function testGetSideMenu(): void |
||
| 1820 | { |
||
| 1821 | $item = $this->createMock(ItemInterface::class); |
||
| 1822 | $item |
||
| 1823 | ->expects($this->once()) |
||
| 1824 | ->method('setChildrenAttribute') |
||
| 1825 | ->with('class', 'nav navbar-nav'); |
||
| 1826 | $item |
||
| 1827 | ->expects($this->once()) |
||
| 1828 | ->method('setExtra') |
||
| 1829 | ->with('translation_domain', 'foo_bar_baz'); |
||
| 1830 | |||
| 1831 | $menuFactory = $this->createMock(FactoryInterface::class); |
||
| 1832 | $menuFactory |
||
| 1833 | ->expects($this->once()) |
||
| 1834 | ->method('createItem') |
||
| 1835 | ->willReturn($item); |
||
| 1836 | |||
| 1837 | $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin'); |
||
| 1838 | $modelAdmin->setMenuFactory($menuFactory); |
||
| 1839 | $modelAdmin->setTranslationDomain('foo_bar_baz'); |
||
| 1840 | |||
| 1841 | $modelAdmin->getSideMenu('foo'); |
||
| 1842 | } |
||
| 1843 | |||
| 1844 | /** |
||
| 1845 | * @return array |
||
| 1846 | */ |
||
| 1847 | public function provideGetSubject() |
||
| 1848 | { |
||
| 1849 | return [ |
||
| 1850 | [23], |
||
| 1851 | ['azerty'], |
||
| 1852 | ['4f69bbb5f14a13347f000092'], |
||
| 1853 | ['0779ca8d-e2be-11e4-ac58-0242ac11000b'], |
||
| 1854 | ['123'.AdapterInterface::ID_SEPARATOR.'my_type'], // composite keys are supported |
||
| 1855 | ]; |
||
| 1856 | } |
||
| 1857 | |||
| 1858 | /** |
||
| 1859 | * @dataProvider provideGetSubject |
||
| 1860 | */ |
||
| 1861 | public function testGetSubjectFailed($id): void |
||
| 1862 | { |
||
| 1863 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1864 | $modelManager |
||
| 1865 | ->expects($this->once()) |
||
| 1866 | ->method('find') |
||
| 1867 | ->with('NewsBundle\Entity\Post', $id) |
||
| 1868 | ->willReturn(null); // entity not found |
||
| 1869 | |||
| 1870 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1871 | $admin->setModelManager($modelManager); |
||
| 1872 | |||
| 1873 | $admin->setRequest(new Request(['id' => $id])); |
||
| 1874 | $this->assertNull($admin->getSubject()); |
||
| 1875 | } |
||
| 1876 | |||
| 1877 | /** |
||
| 1878 | * @dataProvider provideGetSubject |
||
| 1879 | */ |
||
| 1880 | public function testGetSubject($id): void |
||
| 1881 | { |
||
| 1882 | $entity = new Post(); |
||
| 1883 | |||
| 1884 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1885 | $modelManager |
||
| 1886 | ->expects($this->once()) |
||
| 1887 | ->method('find') |
||
| 1888 | ->with('NewsBundle\Entity\Post', $id) |
||
| 1889 | ->willReturn($entity); |
||
| 1890 | |||
| 1891 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1892 | $admin->setModelManager($modelManager); |
||
| 1893 | |||
| 1894 | $admin->setRequest(new Request(['id' => $id])); |
||
| 1895 | $this->assertSame($entity, $admin->getSubject()); |
||
| 1896 | $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once |
||
| 1897 | } |
||
| 1898 | |||
| 1899 | public function testGetSubjectWithParentDescription(): void |
||
| 1900 | { |
||
| 1901 | $adminId = 1; |
||
| 1902 | |||
| 1903 | $comment = new Comment(); |
||
| 1904 | |||
| 1905 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
| 1906 | $modelManager |
||
| 1907 | ->method('find') |
||
| 1908 | ->with('NewsBundle\Entity\Comment', $adminId) |
||
| 1909 | ->willReturn($comment); |
||
| 1910 | |||
| 1911 | $request = new Request(['id' => $adminId]); |
||
| 1912 | |||
| 1913 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1914 | $postAdmin->setRequest($request); |
||
| 1915 | |||
| 1916 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
| 1917 | $commentAdmin->setRequest($request); |
||
| 1918 | $commentAdmin->setModelManager($modelManager); |
||
| 1919 | |||
| 1920 | $this->assertSame($comment, $commentAdmin->getSubject()); |
||
| 1921 | |||
| 1922 | $commentAdmin->setSubject(null); |
||
| 1923 | $commentAdmin->setParentFieldDescription(new FieldDescription()); |
||
| 1924 | |||
| 1925 | $this->assertNull($commentAdmin->getSubject()); |
||
| 1926 | } |
||
| 1927 | |||
| 1928 | /** |
||
| 1929 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 1930 | */ |
||
| 1931 | public function testGetActionButtonsList(): void |
||
| 1932 | { |
||
| 1933 | $expected = [ |
||
| 1934 | 'create' => [ |
||
| 1935 | 'template' => 'Foo.html.twig', |
||
| 1936 | ], |
||
| 1937 | ]; |
||
| 1938 | |||
| 1939 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1940 | |||
| 1941 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 1942 | $templateRegistry->getTemplate('button_create')->willReturn('Foo.html.twig'); |
||
| 1943 | |||
| 1944 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 1945 | |||
| 1946 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
| 1947 | $securityHandler |
||
| 1948 | ->expects($this->once()) |
||
| 1949 | ->method('isGranted') |
||
| 1950 | ->with($admin, 'CREATE', $admin) |
||
| 1951 | ->willReturn(true); |
||
| 1952 | $admin->setSecurityHandler($securityHandler); |
||
| 1953 | |||
| 1954 | $routeGenerator = $this->createMock(RouteGeneratorInterface::class); |
||
| 1955 | $routeGenerator |
||
| 1956 | ->expects($this->once()) |
||
| 1957 | ->method('hasAdminRoute') |
||
| 1958 | ->with($admin, 'create') |
||
| 1959 | ->willReturn(true); |
||
| 1960 | $admin->setRouteGenerator($routeGenerator); |
||
| 1961 | |||
| 1962 | $this->assertSame($expected, $admin->getActionButtons('list', null)); |
||
| 1963 | } |
||
| 1964 | |||
| 1965 | /** |
||
| 1966 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
| 1967 | */ |
||
| 1968 | public function testGetActionButtonsListCreateDisabled(): void |
||
| 1969 | { |
||
| 1970 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 1971 | |||
| 1972 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
| 1973 | $securityHandler |
||
| 1974 | ->expects($this->once()) |
||
| 1975 | ->method('isGranted') |
||
| 1976 | ->with($admin, 'CREATE', $admin) |
||
| 1977 | ->willReturn(false); |
||
| 1978 | $admin->setSecurityHandler($securityHandler); |
||
| 1979 | |||
| 1980 | $this->assertSame([], $admin->getActionButtons('list', null)); |
||
| 1981 | } |
||
| 1982 | |||
| 1983 | /** |
||
| 1984 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions |
||
| 1985 | */ |
||
| 1986 | public function testGetBatchActions(): void |
||
| 1987 | { |
||
| 1988 | $expected = [ |
||
| 1989 | 'delete' => [ |
||
| 1990 | 'label' => 'action_delete', |
||
| 1991 | 'translation_domain' => 'SonataAdminBundle', |
||
| 1992 | 'ask_confirmation' => true, // by default always true |
||
| 1993 | ], |
||
| 1994 | 'foo' => [ |
||
| 1995 | 'label' => 'action_foo', |
||
| 1996 | 'translation_domain' => 'SonataAdminBundle', |
||
| 1997 | ], |
||
| 1998 | 'bar' => [ |
||
| 1999 | 'label' => 'batch.label_bar', |
||
| 2000 | 'translation_domain' => 'SonataAdminBundle', |
||
| 2001 | ], |
||
| 2002 | 'baz' => [ |
||
| 2003 | 'label' => 'action_baz', |
||
| 2004 | 'translation_domain' => 'AcmeAdminBundle', |
||
| 2005 | ], |
||
| 2006 | ]; |
||
| 2007 | |||
| 2008 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
| 2009 | |||
| 2010 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
| 2011 | $labelTranslatorStrategy |
||
| 2012 | ->method('getLabel') |
||
| 2013 | ->willReturnCallback(static function ($label, $context = '', $type = '') { |
||
| 2014 | return $context.'.'.$type.'_'.$label; |
||
| 2015 | }); |
||
| 2016 | |||
| 2017 | $admin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin'); |
||
| 2018 | $admin->setRouteBuilder($pathInfo); |
||
| 2019 | $admin->setTranslationDomain('SonataAdminBundle'); |
||
| 2020 | $admin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
| 2021 | |||
| 2022 | $routeGenerator = $this->createMock(RouteGeneratorInterface::class); |
||
| 2023 | $routeGenerator |
||
| 2024 | ->expects($this->once()) |
||
| 2025 | ->method('hasAdminRoute') |
||
| 2026 | ->with($admin, 'delete') |
||
| 2027 | ->willReturn(true); |
||
| 2028 | $admin->setRouteGenerator($routeGenerator); |
||
| 2029 | |||
| 2030 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
| 2031 | $securityHandler |
||
| 2032 | ->method('isGranted') |
||
| 2033 | ->willReturnCallback(static function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) { |
||
| 2034 | return $admin === $adminIn && 'DELETE' === $attributes; |
||
| 2035 | }); |
||
| 2036 | $admin->setSecurityHandler($securityHandler); |
||
| 2037 | |||
| 2038 | $this->assertSame($expected, $admin->getBatchActions()); |
||
| 2039 | } |
||
| 2040 | |||
| 2041 | /** |
||
| 2042 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 2043 | */ |
||
| 2044 | public function testShowMosaicButton(): void |
||
| 2045 | { |
||
| 2046 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 2047 | $listModes = $admin->getListModes(); |
||
| 2048 | |||
| 2049 | $admin->showMosaicButton(true); |
||
| 2050 | |||
| 2051 | $this->assertSame($listModes, $admin->getListModes()); |
||
| 2052 | } |
||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
| 2056 | */ |
||
| 2057 | public function testShowMosaicButtonHideMosaic(): void |
||
| 2058 | { |
||
| 2059 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
| 2060 | $listModes = $admin->getListModes(); |
||
| 2061 | $expected['list'] = $listModes['list']; |
||
| 2062 | |||
| 2063 | $admin->showMosaicButton(false); |
||
| 2064 | |||
| 2065 | $this->assertSame($expected, $admin->getListModes()); |
||
| 2066 | } |
||
| 2067 | |||
| 2068 | /** |
||
| 2069 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions |
||
| 2070 | * @dataProvider provideGetBaseRouteName |
||
| 2071 | */ |
||
| 2072 | public function testDefaultDashboardActionsArePresent($objFqn, $expected): void |
||
| 2073 | { |
||
| 2074 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
| 2075 | |||
| 2076 | $routeGenerator = new DefaultRouteGenerator( |
||
| 2077 | $this->createMock(RouterInterface::class), |
||
| 2078 | new RoutesCache($this->cacheTempFolder, true) |
||
| 2079 | ); |
||
| 2080 | |||
| 2081 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
| 2082 | $admin->setRouteBuilder($pathInfo); |
||
| 2083 | $admin->setRouteGenerator($routeGenerator); |
||
| 2084 | $admin->initialize(); |
||
| 2085 | |||
| 2086 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
| 2087 | $templateRegistry->getTemplate('action_create')->willReturn('Foo.html.twig'); |
||
| 2088 | |||
| 2089 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
| 2090 | |||
| 2091 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
| 2092 | $securityHandler |
||
| 2093 | ->method('isGranted') |
||
| 2094 | ->willReturnCallback(static function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) { |
||
| 2095 | return $admin === $adminIn && ('CREATE' === $attributes || 'LIST' === $attributes); |
||
| 2096 | }); |
||
| 2097 | |||
| 2098 | $admin->setSecurityHandler($securityHandler); |
||
| 2099 | |||
| 2100 | $this->assertArrayHasKey('list', $admin->getDashboardActions()); |
||
| 2101 | $this->assertArrayHasKey('create', $admin->getDashboardActions()); |
||
| 2102 | } |
||
| 2103 | |||
| 2104 | public function testDefaultFilters(): void |
||
| 2163 | |||
| 2164 | /** |
||
| 2165 | * @group legacy |
||
| 2166 | */ |
||
| 2167 | public function testDefaultBreadcrumbsBuilder(): void |
||
| 2191 | |||
| 2192 | /** |
||
| 2193 | * @group legacy |
||
| 2194 | */ |
||
| 2195 | public function testBreadcrumbsBuilderSetter(): void |
||
| 2205 | |||
| 2206 | /** |
||
| 2207 | * @group legacy |
||
| 2208 | */ |
||
| 2209 | public function testGetBreadcrumbs(): void |
||
| 2219 | |||
| 2220 | /** |
||
| 2221 | * @group legacy |
||
| 2222 | */ |
||
| 2223 | public function testBuildBreadcrumbs(): void |
||
| 2240 | |||
| 2241 | /** |
||
| 2242 | * NEXT_MAJOR: remove this method. |
||
| 2243 | * |
||
| 2244 | * @group legacy |
||
| 2245 | */ |
||
| 2246 | public function testCreateQueryLegacyCallWorks(): void |
||
| 2260 | |||
| 2261 | public function testGetDataSourceIterator(): void |
||
| 2303 | |||
| 2304 | public function testCircularChildAdmin(): void |
||
| 2324 | |||
| 2325 | public function testCircularChildAdminTripleLevel(): void |
||
| 2351 | |||
| 2352 | public function testCircularChildAdminWithItself(): void |
||
| 2366 | |||
| 2367 | public function testGetRootAncestor(): void |
||
| 2401 | |||
| 2402 | public function testGetChildDepth(): void |
||
| 2436 | |||
| 2437 | public function testGetCurrentLeafChildAdmin(): void |
||
| 2474 | |||
| 2475 | private function createTagAdmin(Post $post): TagAdmin |
||
| 2514 | } |
||
| 2515 |
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: