Complex classes like CRUDControllerTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CRUDControllerTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
70 | class CRUDControllerTest extends TestCase |
||
71 | { |
||
72 | /** |
||
73 | * @var CRUDController |
||
74 | */ |
||
75 | private $controller; |
||
76 | |||
77 | /** |
||
78 | * @var Request |
||
79 | */ |
||
80 | private $request; |
||
81 | |||
82 | /** |
||
83 | * @var AdminInterface |
||
84 | */ |
||
85 | private $admin; |
||
86 | |||
87 | /** |
||
88 | * @var TemplateRegistryInterface |
||
89 | */ |
||
90 | private $templateRegistry; |
||
91 | |||
92 | /** |
||
93 | * @var Pool |
||
94 | */ |
||
95 | private $pool; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | private $parameters; |
||
101 | |||
102 | /** |
||
103 | * @var Session |
||
104 | */ |
||
105 | private $session; |
||
106 | |||
107 | /** |
||
108 | * @var AuditManager |
||
109 | */ |
||
110 | private $auditManager; |
||
111 | |||
112 | /** |
||
113 | * @var ContainerInterface |
||
114 | */ |
||
115 | private $container; |
||
116 | |||
117 | /** |
||
118 | * @var AdminObjectAclManipulator |
||
119 | */ |
||
120 | private $adminObjectAclManipulator; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | */ |
||
125 | private $template; |
||
126 | |||
127 | /** |
||
128 | * @var array |
||
129 | */ |
||
130 | private $protectedTestedMethods; |
||
131 | |||
132 | /** |
||
133 | * @var CsrfTokenManagerInterface |
||
134 | */ |
||
135 | private $csrfProvider; |
||
136 | |||
137 | /** |
||
138 | * @var KernelInterface |
||
139 | */ |
||
140 | private $kernel; |
||
141 | |||
142 | /** |
||
143 | * @var TranslatorInterface |
||
144 | */ |
||
145 | private $translator; |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | protected function setUp(): void |
||
441 | |||
442 | public function testRenderJson1(): void |
||
452 | |||
453 | public function testRenderJson2(): void |
||
463 | |||
464 | public function testRenderJsonAjax(): void |
||
475 | |||
476 | public function testIsXmlHttpRequest(): void |
||
490 | |||
491 | public function testConfigure(): void |
||
507 | |||
508 | public function testConfigureChild(): void |
||
535 | |||
536 | public function testConfigureWithException(): void |
||
546 | |||
547 | public function testConfigureWithException2(): void |
||
559 | |||
560 | public function testGetBaseTemplate(): void |
||
585 | |||
586 | public function testRender(): void |
||
598 | |||
599 | public function testRenderWithResponse(): void |
||
613 | |||
614 | public function testRenderCustomParams(): void |
||
631 | |||
632 | public function testRenderAjax(): void |
||
650 | |||
651 | public function testListActionAccessDenied(): void |
||
662 | |||
663 | public function testPreList(): void |
||
682 | |||
683 | public function testListAction(): void |
||
727 | |||
728 | public function testBatchActionDeleteAccessDenied(): void |
||
739 | |||
740 | public function testBatchActionDelete(): void |
||
765 | |||
766 | public function testBatchActionDeleteWithModelManagerException(): void |
||
787 | |||
788 | public function testBatchActionDeleteWithModelManagerExceptionInDebugMode(): void |
||
809 | |||
810 | public function testShowActionNotFoundException(): void |
||
820 | |||
821 | public function testShowActionAccessDenied(): void |
||
836 | |||
837 | /** |
||
838 | * @group legacy |
||
839 | * @expectedDeprecation Calling this method without implementing "configureShowFields" is not supported since 3.x and will no longer be possible in 4.0 |
||
840 | */ |
||
841 | public function testShowActionDeprecation(): void |
||
842 | { |
||
843 | $object = new \stdClass(); |
||
844 | |||
845 | $this->admin->expects($this->once()) |
||
846 | ->method('getObject') |
||
847 | ->will($this->returnValue($object)); |
||
848 | |||
849 | $this->admin->expects($this->once()) |
||
850 | ->method('checkAccess') |
||
851 | ->with($this->equalTo('show')) |
||
852 | ->will($this->returnValue(true)); |
||
853 | |||
854 | $show = $this->createMock(FieldDescriptionCollection::class); |
||
855 | |||
856 | $this->admin->expects($this->once()) |
||
857 | ->method('getShow') |
||
858 | ->will($this->returnValue($show)); |
||
859 | |||
860 | $show->expects($this->once()) |
||
861 | ->method('getElements') |
||
862 | ->willReturn([]); |
||
863 | |||
864 | $show->expects($this->once()) |
||
865 | ->method('count') |
||
866 | ->willReturn(0); |
||
867 | |||
868 | $this->controller->showAction(null, $this->request); |
||
869 | } |
||
870 | |||
871 | public function testPreShow(): void |
||
872 | { |
||
873 | $object = new \stdClass(); |
||
874 | $object->foo = 123456; |
||
875 | |||
876 | $this->admin->expects($this->once()) |
||
877 | ->method('getObject') |
||
878 | ->will($this->returnValue($object)); |
||
879 | |||
880 | $this->admin->expects($this->once()) |
||
881 | ->method('checkAccess') |
||
882 | ->with($this->equalTo('show')) |
||
883 | ->will($this->returnValue(true)); |
||
884 | |||
885 | $controller = new PreCRUDController(); |
||
886 | $controller->setContainer($this->container); |
||
887 | |||
888 | $response = $controller->showAction(null, $this->request); |
||
889 | $this->assertInstanceOf(Response::class, $response); |
||
890 | $this->assertSame('preShow called: 123456', $response->getContent()); |
||
891 | } |
||
892 | |||
893 | public function testShowAction(): void |
||
894 | { |
||
895 | $object = new \stdClass(); |
||
896 | |||
897 | $this->admin->expects($this->once()) |
||
898 | ->method('getObject') |
||
899 | ->will($this->returnValue($object)); |
||
900 | |||
901 | $this->admin->expects($this->once()) |
||
902 | ->method('checkAccess') |
||
903 | ->with($this->equalTo('show')) |
||
904 | ->will($this->returnValue(true)); |
||
905 | |||
906 | $show = $this->createMock(FieldDescriptionCollection::class); |
||
907 | |||
908 | $this->admin->expects($this->once()) |
||
909 | ->method('getShow') |
||
910 | ->will($this->returnValue($show)); |
||
911 | |||
912 | $show->expects($this->once()) |
||
913 | ->method('getElements') |
||
914 | ->willReturn(['field' => 'fielddata']); |
||
915 | |||
916 | $this->assertInstanceOf(Response::class, $this->controller->showAction(null, $this->request)); |
||
917 | |||
918 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
919 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
920 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
921 | |||
922 | $this->assertSame('show', $this->parameters['action']); |
||
923 | $this->assertInstanceOf(FieldDescriptionCollection::class, $this->parameters['elements']); |
||
924 | $this->assertSame($object, $this->parameters['object']); |
||
925 | |||
926 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
927 | $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template); |
||
928 | } |
||
929 | |||
930 | /** |
||
931 | * @dataProvider getRedirectToTests |
||
932 | */ |
||
933 | public function testRedirectTo($expected, $route, $queryParams, $hasActiveSubclass): void |
||
934 | { |
||
935 | $this->admin->expects($this->any()) |
||
936 | ->method('hasActiveSubclass') |
||
937 | ->will($this->returnValue($hasActiveSubclass)); |
||
938 | |||
939 | $object = new \stdClass(); |
||
940 | |||
941 | foreach ($queryParams as $key => $value) { |
||
942 | $this->request->query->set($key, $value); |
||
943 | } |
||
944 | |||
945 | $this->admin->expects($this->any()) |
||
946 | ->method('hasRoute') |
||
947 | ->with($this->equalTo($route)) |
||
948 | ->will($this->returnValue(true)); |
||
949 | |||
950 | $this->admin->expects($this->any()) |
||
951 | ->method('hasAccess') |
||
952 | ->with($this->equalTo($route)) |
||
953 | ->will($this->returnValue(true)); |
||
954 | |||
955 | $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
||
956 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
957 | $this->assertSame($expected, $response->getTargetUrl()); |
||
958 | } |
||
959 | |||
960 | public function testRedirectToWithObject(): void |
||
961 | { |
||
962 | $this->admin->expects($this->any()) |
||
963 | ->method('hasActiveSubclass') |
||
964 | ->will($this->returnValue(false)); |
||
965 | |||
966 | $object = new \stdClass(); |
||
967 | |||
968 | $this->admin->expects($this->at(0)) |
||
969 | ->method('hasRoute') |
||
970 | ->with($this->equalTo('edit')) |
||
971 | ->will($this->returnValue(true)); |
||
972 | |||
973 | $this->admin->expects($this->any()) |
||
974 | ->method('hasAccess') |
||
975 | ->with($this->equalTo('edit'), $object) |
||
976 | ->will($this->returnValue(false)); |
||
977 | |||
978 | $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
||
979 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
980 | $this->assertSame('list', $response->getTargetUrl()); |
||
981 | } |
||
982 | |||
983 | public function getRedirectToTests() |
||
984 | { |
||
985 | return [ |
||
986 | ['stdClass_edit', 'edit', [], false], |
||
987 | ['list', 'list', ['btn_update_and_list' => true], false], |
||
988 | ['list', 'list', ['btn_create_and_list' => true], false], |
||
989 | ['create', 'create', ['btn_create_and_create' => true], false], |
||
990 | ['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], true], |
||
991 | ]; |
||
992 | } |
||
993 | |||
994 | public function testDeleteActionNotFoundException(): void |
||
995 | { |
||
996 | $this->expectException(NotFoundHttpException::class); |
||
997 | |||
998 | $this->admin->expects($this->once()) |
||
999 | ->method('getObject') |
||
1000 | ->will($this->returnValue(false)); |
||
1001 | |||
1002 | $this->controller->deleteAction(1, $this->request); |
||
1003 | } |
||
1004 | |||
1005 | public function testDeleteActionAccessDenied(): void |
||
1006 | { |
||
1007 | $this->expectException(AccessDeniedException::class); |
||
1008 | |||
1009 | $this->admin->expects($this->once()) |
||
1010 | ->method('getObject') |
||
1011 | ->will($this->returnValue(new \stdClass())); |
||
1012 | |||
1013 | $this->admin->expects($this->once()) |
||
1014 | ->method('checkAccess') |
||
1015 | ->with($this->equalTo('delete')) |
||
1016 | ->will($this->throwException(new AccessDeniedException())); |
||
1017 | |||
1018 | $this->controller->deleteAction(1, $this->request); |
||
1019 | } |
||
1020 | |||
1021 | public function testPreDelete(): void |
||
1022 | { |
||
1023 | $object = new \stdClass(); |
||
1024 | $object->foo = 123456; |
||
1025 | |||
1026 | $this->admin->expects($this->once()) |
||
1027 | ->method('getObject') |
||
1028 | ->will($this->returnValue($object)); |
||
1029 | |||
1030 | $this->admin->expects($this->once()) |
||
1031 | ->method('checkAccess') |
||
1032 | ->with($this->equalTo('delete')) |
||
1033 | ->will($this->returnValue(true)); |
||
1034 | |||
1035 | $controller = new PreCRUDController(); |
||
1036 | $controller->setContainer($this->container); |
||
1037 | |||
1038 | $response = $controller->deleteAction(null, $this->request); |
||
1039 | $this->assertInstanceOf(Response::class, $response); |
||
1040 | $this->assertSame('preDelete called: 123456', $response->getContent()); |
||
1041 | } |
||
1042 | |||
1043 | public function testDeleteAction(): void |
||
1044 | { |
||
1045 | $object = new \stdClass(); |
||
1046 | |||
1047 | $this->admin->expects($this->once()) |
||
1048 | ->method('getObject') |
||
1049 | ->will($this->returnValue($object)); |
||
1050 | |||
1051 | $this->admin->expects($this->once()) |
||
1052 | ->method('checkAccess') |
||
1053 | ->with($this->equalTo('delete')) |
||
1054 | ->will($this->returnValue(true)); |
||
1055 | |||
1056 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1, $this->request)); |
||
1057 | |||
1058 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1059 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1060 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1061 | |||
1062 | $this->assertSame('delete', $this->parameters['action']); |
||
1063 | $this->assertSame($object, $this->parameters['object']); |
||
1064 | $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
||
1065 | |||
1066 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1067 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
1068 | } |
||
1069 | |||
1070 | public function testDeleteActionNoCsrfToken(): void |
||
1071 | { |
||
1072 | $this->csrfProvider = null; |
||
1073 | |||
1074 | $object = new \stdClass(); |
||
1075 | |||
1076 | $this->admin->expects($this->once()) |
||
1077 | ->method('getObject') |
||
1078 | ->will($this->returnValue($object)); |
||
1079 | |||
1080 | $this->admin->expects($this->once()) |
||
1081 | ->method('checkAccess') |
||
1082 | ->with($this->equalTo('delete')) |
||
1083 | ->will($this->returnValue(true)); |
||
1084 | |||
1085 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1, $this->request)); |
||
1086 | |||
1087 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1088 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1089 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1090 | |||
1091 | $this->assertSame('delete', $this->parameters['action']); |
||
1092 | $this->assertSame($object, $this->parameters['object']); |
||
1093 | $this->assertFalse($this->parameters['csrf_token']); |
||
1094 | |||
1095 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1096 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
1097 | } |
||
1098 | |||
1099 | public function testDeleteActionAjaxSuccess1(): void |
||
1100 | { |
||
1101 | $object = new \stdClass(); |
||
1102 | |||
1103 | $this->admin->expects($this->once()) |
||
1104 | ->method('getObject') |
||
1105 | ->will($this->returnValue($object)); |
||
1106 | |||
1107 | $this->admin->expects($this->once()) |
||
1108 | ->method('checkAccess') |
||
1109 | ->with($this->equalTo('delete')) |
||
1110 | ->will($this->returnValue(true)); |
||
1111 | |||
1112 | $this->request->setMethod('DELETE'); |
||
1113 | |||
1114 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1115 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1116 | |||
1117 | $response = $this->controller->deleteAction(1, $this->request); |
||
1118 | |||
1119 | $this->assertInstanceOf(Response::class, $response); |
||
1120 | $this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
||
1121 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1122 | } |
||
1123 | |||
1124 | public function testDeleteActionAjaxSuccess2(): void |
||
1125 | { |
||
1126 | $object = new \stdClass(); |
||
1127 | |||
1128 | $this->admin->expects($this->once()) |
||
1129 | ->method('getObject') |
||
1130 | ->will($this->returnValue($object)); |
||
1131 | |||
1132 | $this->admin->expects($this->once()) |
||
1133 | ->method('checkAccess') |
||
1134 | ->with($this->equalTo('delete')) |
||
1135 | ->will($this->returnValue(true)); |
||
1136 | |||
1137 | $this->request->setMethod('POST'); |
||
1138 | $this->request->request->set('_method', 'DELETE'); |
||
1139 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1140 | |||
1141 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1142 | |||
1143 | $response = $this->controller->deleteAction(1, $this->request); |
||
1144 | |||
1145 | $this->assertInstanceOf(Response::class, $response); |
||
1146 | $this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
||
1147 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1148 | } |
||
1149 | |||
1150 | public function testDeleteActionAjaxError(): void |
||
1151 | { |
||
1152 | $object = new \stdClass(); |
||
1153 | |||
1154 | $this->admin->expects($this->once()) |
||
1155 | ->method('getObject') |
||
1156 | ->will($this->returnValue($object)); |
||
1157 | |||
1158 | $this->admin->expects($this->once()) |
||
1159 | ->method('checkAccess') |
||
1160 | ->with($this->equalTo('delete')) |
||
1161 | ->will($this->returnValue(true)); |
||
1162 | |||
1163 | $this->admin->expects($this->any()) |
||
1164 | ->method('getClass') |
||
1165 | ->will($this->returnValue('stdClass')); |
||
1166 | |||
1167 | $this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
||
1168 | |||
1169 | $this->request->setMethod('DELETE'); |
||
1170 | |||
1171 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1172 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1173 | |||
1174 | $response = $this->controller->deleteAction(1, $this->request); |
||
1175 | |||
1176 | $this->assertInstanceOf(Response::class, $response); |
||
1177 | $this->assertSame(json_encode(['result' => 'error']), $response->getContent()); |
||
1178 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1179 | } |
||
1180 | |||
1181 | public function testDeleteActionWithModelManagerExceptionInDebugMode(): void |
||
1182 | { |
||
1183 | $this->expectException(ModelManagerException::class); |
||
1184 | |||
1185 | $object = new \stdClass(); |
||
1186 | |||
1187 | $this->admin->expects($this->once()) |
||
1188 | ->method('getObject') |
||
1189 | ->will($this->returnValue($object)); |
||
1190 | |||
1191 | $this->admin->expects($this->once()) |
||
1192 | ->method('checkAccess') |
||
1193 | ->with($this->equalTo('delete')) |
||
1194 | ->will($this->returnValue(true)); |
||
1195 | |||
1196 | $this->admin->expects($this->once()) |
||
1197 | ->method('delete') |
||
1198 | ->will($this->returnCallback(function (): void { |
||
1199 | throw new ModelManagerException(); |
||
1200 | })); |
||
1201 | |||
1202 | $this->kernel->expects($this->once()) |
||
1203 | ->method('isDebug') |
||
1204 | ->will($this->returnValue(true)); |
||
1205 | |||
1206 | $this->request->setMethod('DELETE'); |
||
1207 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1208 | |||
1209 | $this->controller->deleteAction(1, $this->request); |
||
1210 | } |
||
1211 | |||
1212 | /** |
||
1213 | * @dataProvider getToStringValues |
||
1214 | */ |
||
1215 | public function testDeleteActionSuccess1($expectedToStringValue, $toStringValue): void |
||
1216 | { |
||
1217 | $object = new \stdClass(); |
||
1218 | |||
1219 | $this->admin->expects($this->once()) |
||
1220 | ->method('getObject') |
||
1221 | ->will($this->returnValue($object)); |
||
1222 | |||
1223 | $this->admin->expects($this->once()) |
||
1224 | ->method('toString') |
||
1225 | ->with($this->equalTo($object)) |
||
1226 | ->will($this->returnValue($toStringValue)); |
||
1227 | |||
1228 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1229 | |||
1230 | $this->admin->expects($this->once()) |
||
1231 | ->method('checkAccess') |
||
1232 | ->with($this->equalTo('delete')) |
||
1233 | ->will($this->returnValue(true)); |
||
1234 | |||
1235 | $this->request->setMethod('DELETE'); |
||
1236 | |||
1237 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1238 | |||
1239 | $response = $this->controller->deleteAction(1, $this->request); |
||
1240 | |||
1241 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1242 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1243 | $this->assertSame('list', $response->getTargetUrl()); |
||
1244 | } |
||
1245 | |||
1246 | /** |
||
1247 | * @dataProvider getToStringValues |
||
1248 | */ |
||
1249 | public function testDeleteActionSuccess2($expectedToStringValue, $toStringValue): void |
||
1250 | { |
||
1251 | $object = new \stdClass(); |
||
1252 | |||
1253 | $this->admin->expects($this->once()) |
||
1254 | ->method('getObject') |
||
1255 | ->will($this->returnValue($object)); |
||
1256 | |||
1257 | $this->admin->expects($this->once()) |
||
1258 | ->method('checkAccess') |
||
1259 | ->with($this->equalTo('delete')) |
||
1260 | ->will($this->returnValue(true)); |
||
1261 | |||
1262 | $this->admin->expects($this->once()) |
||
1263 | ->method('toString') |
||
1264 | ->with($this->equalTo($object)) |
||
1265 | ->will($this->returnValue($toStringValue)); |
||
1266 | |||
1267 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1268 | |||
1269 | $this->request->setMethod('POST'); |
||
1270 | $this->request->request->set('_method', 'DELETE'); |
||
1271 | |||
1272 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1273 | |||
1274 | $response = $this->controller->deleteAction(1, $this->request); |
||
1275 | |||
1276 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1277 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1278 | $this->assertSame('list', $response->getTargetUrl()); |
||
1279 | } |
||
1280 | |||
1281 | /** |
||
1282 | * @dataProvider getToStringValues |
||
1283 | */ |
||
1284 | public function testDeleteActionSuccessNoCsrfTokenProvider($expectedToStringValue, $toStringValue): void |
||
1285 | { |
||
1286 | $this->csrfProvider = null; |
||
1287 | |||
1288 | $object = new \stdClass(); |
||
1289 | |||
1290 | $this->admin->expects($this->once()) |
||
1291 | ->method('getObject') |
||
1292 | ->will($this->returnValue($object)); |
||
1293 | |||
1294 | $this->admin->expects($this->once()) |
||
1295 | ->method('checkAccess') |
||
1296 | ->with($this->equalTo('delete')) |
||
1297 | ->will($this->returnValue(true)); |
||
1298 | |||
1299 | $this->admin->expects($this->once()) |
||
1300 | ->method('toString') |
||
1301 | ->with($this->equalTo($object)) |
||
1302 | ->will($this->returnValue($toStringValue)); |
||
1303 | |||
1304 | $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1305 | |||
1306 | $this->request->setMethod('POST'); |
||
1307 | $this->request->request->set('_method', 'DELETE'); |
||
1308 | |||
1309 | $response = $this->controller->deleteAction(1, $this->request); |
||
1310 | |||
1311 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1312 | $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1313 | $this->assertSame('list', $response->getTargetUrl()); |
||
1314 | } |
||
1315 | |||
1316 | public function testDeleteActionWrongRequestMethod(): void |
||
1317 | { |
||
1318 | $object = new \stdClass(); |
||
1319 | |||
1320 | $this->admin->expects($this->once()) |
||
1321 | ->method('getObject') |
||
1322 | ->will($this->returnValue($object)); |
||
1323 | |||
1324 | $this->admin->expects($this->once()) |
||
1325 | ->method('checkAccess') |
||
1326 | ->with($this->equalTo('delete')) |
||
1327 | ->will($this->returnValue(true)); |
||
1328 | |||
1329 | //without POST request parameter "_method" should not be used as real REST method |
||
1330 | $this->request->query->set('_method', 'DELETE'); |
||
1331 | |||
1332 | $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1, $this->request)); |
||
1333 | |||
1334 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1335 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1336 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1337 | |||
1338 | $this->assertSame('delete', $this->parameters['action']); |
||
1339 | $this->assertSame($object, $this->parameters['object']); |
||
1340 | $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
||
1341 | |||
1342 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1343 | $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
||
1344 | } |
||
1345 | |||
1346 | /** |
||
1347 | * @dataProvider getToStringValues |
||
1348 | */ |
||
1349 | public function testDeleteActionError($expectedToStringValue, $toStringValue): void |
||
1350 | { |
||
1351 | $object = new \stdClass(); |
||
1352 | |||
1353 | $this->admin->expects($this->once()) |
||
1354 | ->method('getObject') |
||
1355 | ->will($this->returnValue($object)); |
||
1356 | |||
1357 | $this->admin->expects($this->once()) |
||
1358 | ->method('checkAccess') |
||
1359 | ->with($this->equalTo('delete')) |
||
1360 | ->will($this->returnValue(true)); |
||
1361 | |||
1362 | $this->admin->expects($this->once()) |
||
1363 | ->method('toString') |
||
1364 | ->with($this->equalTo($object)) |
||
1365 | ->will($this->returnValue($toStringValue)); |
||
1366 | |||
1367 | $this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1368 | |||
1369 | $this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
||
1370 | |||
1371 | $this->request->setMethod('DELETE'); |
||
1372 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
||
1373 | |||
1374 | $response = $this->controller->deleteAction(1, $this->request); |
||
1375 | |||
1376 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1377 | $this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
||
1378 | $this->assertSame('list', $response->getTargetUrl()); |
||
1379 | } |
||
1380 | |||
1381 | public function testDeleteActionInvalidCsrfToken(): void |
||
1382 | { |
||
1383 | $object = new \stdClass(); |
||
1384 | |||
1385 | $this->admin->expects($this->once()) |
||
1386 | ->method('getObject') |
||
1387 | ->will($this->returnValue($object)); |
||
1388 | |||
1389 | $this->admin->expects($this->once()) |
||
1390 | ->method('checkAccess') |
||
1391 | ->with($this->equalTo('delete')) |
||
1392 | ->will($this->returnValue(true)); |
||
1393 | |||
1394 | $this->request->setMethod('POST'); |
||
1395 | $this->request->request->set('_method', 'DELETE'); |
||
1396 | $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
||
1397 | |||
1398 | try { |
||
1399 | $this->controller->deleteAction(1, $this->request); |
||
1400 | } catch (HttpException $e) { |
||
1401 | $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
||
1402 | $this->assertSame(400, $e->getStatusCode()); |
||
1403 | } |
||
1404 | } |
||
1405 | |||
1406 | public function testEditActionNotFoundException(): void |
||
1407 | { |
||
1408 | $this->expectException(NotFoundHttpException::class); |
||
1409 | |||
1410 | $this->admin->expects($this->once()) |
||
1411 | ->method('getObject') |
||
1412 | ->will($this->returnValue(false)); |
||
1413 | |||
1414 | $this->controller->editAction(null, $this->request); |
||
1415 | } |
||
1416 | |||
1417 | public function testEditActionRuntimeException(): void |
||
1418 | { |
||
1419 | $this->expectException(\RuntimeException::class); |
||
1420 | |||
1421 | $this->admin->expects($this->once()) |
||
1422 | ->method('getObject') |
||
1423 | ->will($this->returnValue(new \stdClass())); |
||
1424 | |||
1425 | $this->admin->expects($this->once()) |
||
1426 | ->method('checkAccess') |
||
1427 | ->with($this->equalTo('edit')) |
||
1428 | ->will($this->returnValue(true)); |
||
1429 | |||
1430 | $form = $this->createMock(Form::class); |
||
1431 | |||
1432 | $this->admin->expects($this->once()) |
||
1433 | ->method('getForm') |
||
1434 | ->will($this->returnValue($form)); |
||
1435 | |||
1436 | $form->expects($this->once()) |
||
1437 | ->method('all') |
||
1438 | ->willReturn([]); |
||
1439 | |||
1440 | $this->controller->editAction(null, $this->request); |
||
1441 | } |
||
1442 | |||
1443 | public function testEditActionAccessDenied(): void |
||
1444 | { |
||
1445 | $this->expectException(AccessDeniedException::class); |
||
1446 | |||
1447 | $this->admin->expects($this->once()) |
||
1448 | ->method('getObject') |
||
1449 | ->will($this->returnValue(new \stdClass())); |
||
1450 | |||
1451 | $this->admin->expects($this->once()) |
||
1452 | ->method('checkAccess') |
||
1453 | ->with($this->equalTo('edit')) |
||
1454 | ->will($this->throwException(new AccessDeniedException())); |
||
1455 | |||
1456 | $this->controller->editAction(null, $this->request); |
||
1457 | } |
||
1458 | |||
1459 | public function testPreEdit(): void |
||
1460 | { |
||
1461 | $object = new \stdClass(); |
||
1462 | $object->foo = 123456; |
||
1463 | |||
1464 | $this->admin->expects($this->once()) |
||
1465 | ->method('getObject') |
||
1466 | ->will($this->returnValue($object)); |
||
1467 | |||
1468 | $this->admin->expects($this->once()) |
||
1469 | ->method('checkAccess') |
||
1470 | ->with($this->equalTo('edit')) |
||
1471 | ->will($this->returnValue(true)); |
||
1472 | |||
1473 | $controller = new PreCRUDController(); |
||
1474 | $controller->setContainer($this->container); |
||
1475 | |||
1476 | $response = $controller->editAction(null, $this->request); |
||
1477 | $this->assertInstanceOf(Response::class, $response); |
||
1478 | $this->assertSame('preEdit called: 123456', $response->getContent()); |
||
1479 | } |
||
1480 | |||
1481 | public function testEditAction(): void |
||
1482 | { |
||
1483 | $object = new \stdClass(); |
||
1484 | |||
1485 | $this->admin->expects($this->once()) |
||
1486 | ->method('getObject') |
||
1487 | ->will($this->returnValue($object)); |
||
1488 | |||
1489 | $this->admin->expects($this->once()) |
||
1490 | ->method('checkAccess') |
||
1491 | ->with($this->equalTo('edit')) |
||
1492 | ->will($this->returnValue(true)); |
||
1493 | |||
1494 | $form = $this->createMock(Form::class); |
||
1495 | |||
1496 | $this->admin->expects($this->once()) |
||
1497 | ->method('getForm') |
||
1498 | ->will($this->returnValue($form)); |
||
1499 | |||
1500 | $formView = $this->createMock(FormView::class); |
||
1501 | |||
1502 | $form->expects($this->any()) |
||
1503 | ->method('createView') |
||
1504 | ->will($this->returnValue($formView)); |
||
1505 | |||
1506 | $form->expects($this->once()) |
||
1507 | ->method('all') |
||
1508 | ->willReturn(['field' => 'fielddata']); |
||
1509 | |||
1510 | $this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
||
1511 | |||
1512 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1513 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1514 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1515 | |||
1516 | $this->assertSame('edit', $this->parameters['action']); |
||
1517 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1518 | $this->assertSame($object, $this->parameters['object']); |
||
1519 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1520 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1521 | } |
||
1522 | |||
1523 | /** |
||
1524 | * @dataProvider getToStringValues |
||
1525 | */ |
||
1526 | public function testEditActionSuccess($expectedToStringValue, $toStringValue): void |
||
1527 | { |
||
1528 | $object = new \stdClass(); |
||
1529 | |||
1530 | $this->admin->expects($this->once()) |
||
1531 | ->method('getObject') |
||
1532 | ->will($this->returnValue($object)); |
||
1533 | |||
1534 | $this->admin->expects($this->once()) |
||
1535 | ->method('update') |
||
1536 | ->will($this->returnArgument(0)); |
||
1537 | |||
1538 | $this->admin->expects($this->once()) |
||
1539 | ->method('checkAccess') |
||
1540 | ->with($this->equalTo('edit')); |
||
1541 | |||
1542 | $this->admin->expects($this->once()) |
||
1543 | ->method('hasRoute') |
||
1544 | ->with($this->equalTo('edit')) |
||
1545 | ->will($this->returnValue(true)); |
||
1546 | |||
1547 | $this->admin->expects($this->once()) |
||
1548 | ->method('hasAccess') |
||
1549 | ->with($this->equalTo('edit')) |
||
1550 | ->will($this->returnValue(true)); |
||
1551 | |||
1552 | $form = $this->createMock(Form::class); |
||
1553 | |||
1554 | $form->expects($this->once()) |
||
1555 | ->method('getData') |
||
1556 | ->will($this->returnValue($object)); |
||
1557 | |||
1558 | $this->admin->expects($this->once()) |
||
1559 | ->method('getForm') |
||
1560 | ->will($this->returnValue($form)); |
||
1561 | |||
1562 | $form->expects($this->once()) |
||
1563 | ->method('isSubmitted') |
||
1564 | ->will($this->returnValue(true)); |
||
1565 | |||
1566 | $form->expects($this->once()) |
||
1567 | ->method('isValid') |
||
1568 | ->will($this->returnValue(true)); |
||
1569 | |||
1570 | $form->expects($this->once()) |
||
1571 | ->method('all') |
||
1572 | ->willReturn(['field' => 'fielddata']); |
||
1573 | |||
1574 | $this->admin->expects($this->once()) |
||
1575 | ->method('toString') |
||
1576 | ->with($this->equalTo($object)) |
||
1577 | ->will($this->returnValue($toStringValue)); |
||
1578 | |||
1579 | $this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1580 | |||
1581 | $this->request->setMethod('POST'); |
||
1582 | |||
1583 | $response = $this->controller->editAction(null, $this->request); |
||
1584 | |||
1585 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
1586 | $this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
1587 | $this->assertSame('stdClass_edit', $response->getTargetUrl()); |
||
1588 | } |
||
1589 | |||
1590 | /** |
||
1591 | * @dataProvider getToStringValues |
||
1592 | */ |
||
1593 | public function testEditActionError($expectedToStringValue, $toStringValue): void |
||
1594 | { |
||
1595 | $object = new \stdClass(); |
||
1596 | |||
1597 | $this->admin->expects($this->once()) |
||
1598 | ->method('getObject') |
||
1599 | ->will($this->returnValue($object)); |
||
1600 | |||
1601 | $this->admin->expects($this->once()) |
||
1602 | ->method('checkAccess') |
||
1603 | ->with($this->equalTo('edit')) |
||
1604 | ->will($this->returnValue(true)); |
||
1605 | |||
1606 | $form = $this->createMock(Form::class); |
||
1607 | |||
1608 | $this->admin->expects($this->once()) |
||
1609 | ->method('getForm') |
||
1610 | ->will($this->returnValue($form)); |
||
1611 | |||
1612 | $form->expects($this->once()) |
||
1613 | ->method('isSubmitted') |
||
1614 | ->will($this->returnValue(true)); |
||
1615 | |||
1616 | $form->expects($this->once()) |
||
1617 | ->method('isValid') |
||
1618 | ->will($this->returnValue(false)); |
||
1619 | |||
1620 | $form->expects($this->once()) |
||
1621 | ->method('all') |
||
1622 | ->willReturn(['field' => 'fielddata']); |
||
1623 | |||
1624 | $this->admin->expects($this->once()) |
||
1625 | ->method('toString') |
||
1626 | ->with($this->equalTo($object)) |
||
1627 | ->will($this->returnValue($toStringValue)); |
||
1628 | |||
1629 | $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1630 | |||
1631 | $this->request->setMethod('POST'); |
||
1632 | |||
1633 | $formView = $this->createMock(FormView::class); |
||
1634 | |||
1635 | $form->expects($this->any()) |
||
1636 | ->method('createView') |
||
1637 | ->will($this->returnValue($formView)); |
||
1638 | |||
1639 | $this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
||
1640 | |||
1641 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1642 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1643 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1644 | |||
1645 | $this->assertSame('edit', $this->parameters['action']); |
||
1646 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1647 | $this->assertSame($object, $this->parameters['object']); |
||
1648 | |||
1649 | $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
||
1650 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1651 | } |
||
1652 | |||
1653 | public function testEditActionAjaxSuccess(): void |
||
1654 | { |
||
1655 | $object = new \stdClass(); |
||
1656 | |||
1657 | $this->admin->expects($this->once()) |
||
1658 | ->method('getObject') |
||
1659 | ->will($this->returnValue($object)); |
||
1660 | |||
1661 | $this->admin->expects($this->once()) |
||
1662 | ->method('update') |
||
1663 | ->will($this->returnArgument(0)); |
||
1664 | |||
1665 | $this->admin->expects($this->once()) |
||
1666 | ->method('checkAccess') |
||
1667 | ->with($this->equalTo('edit')) |
||
1668 | ->will($this->returnValue(true)); |
||
1669 | |||
1670 | $form = $this->createMock(Form::class); |
||
1671 | |||
1672 | $this->admin->expects($this->once()) |
||
1673 | ->method('getForm') |
||
1674 | ->will($this->returnValue($form)); |
||
1675 | |||
1676 | $form->expects($this->once()) |
||
1677 | ->method('isSubmitted') |
||
1678 | ->will($this->returnValue(true)); |
||
1679 | |||
1680 | $form->expects($this->once()) |
||
1681 | ->method('isValid') |
||
1682 | ->will($this->returnValue(true)); |
||
1683 | |||
1684 | $form->expects($this->once()) |
||
1685 | ->method('getData') |
||
1686 | ->will($this->returnValue($object)); |
||
1687 | |||
1688 | $form->expects($this->once()) |
||
1689 | ->method('all') |
||
1690 | ->willReturn(['field' => 'fielddata']); |
||
1691 | |||
1692 | $this->admin->expects($this->once()) |
||
1693 | ->method('getNormalizedIdentifier') |
||
1694 | ->with($this->equalTo($object)) |
||
1695 | ->will($this->returnValue('foo_normalized')); |
||
1696 | |||
1697 | $this->admin->expects($this->once()) |
||
1698 | ->method('toString') |
||
1699 | ->will($this->returnValue('foo')); |
||
1700 | |||
1701 | $this->request->setMethod('POST'); |
||
1702 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1703 | |||
1704 | $response = $this->controller->editAction(null, $this->request); |
||
1705 | |||
1706 | $this->assertInstanceOf(Response::class, $response); |
||
1707 | $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent()); |
||
1708 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1709 | } |
||
1710 | |||
1711 | public function testEditActionAjaxError(): void |
||
1712 | { |
||
1713 | $object = new \stdClass(); |
||
1714 | |||
1715 | $this->admin->expects($this->once()) |
||
1716 | ->method('getObject') |
||
1717 | ->will($this->returnValue($object)); |
||
1718 | |||
1719 | $this->admin->expects($this->once()) |
||
1720 | ->method('checkAccess') |
||
1721 | ->with($this->equalTo('edit')) |
||
1722 | ->will($this->returnValue(true)); |
||
1723 | |||
1724 | $form = $this->createMock(Form::class); |
||
1725 | |||
1726 | $this->admin->expects($this->once()) |
||
1727 | ->method('getForm') |
||
1728 | ->will($this->returnValue($form)); |
||
1729 | |||
1730 | $form->expects($this->once()) |
||
1731 | ->method('isSubmitted') |
||
1732 | ->will($this->returnValue(true)); |
||
1733 | |||
1734 | $form->expects($this->once()) |
||
1735 | ->method('isValid') |
||
1736 | ->will($this->returnValue(false)); |
||
1737 | |||
1738 | $form->expects($this->once()) |
||
1739 | ->method('all') |
||
1740 | ->willReturn(['field' => 'fielddata']); |
||
1741 | |||
1742 | $this->request->setMethod('POST'); |
||
1743 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
1744 | |||
1745 | $formView = $this->createMock(FormView::class); |
||
1746 | |||
1747 | $form->expects($this->any()) |
||
1748 | ->method('createView') |
||
1749 | ->will($this->returnValue($formView)); |
||
1750 | |||
1751 | $this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
||
1752 | |||
1753 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1754 | $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']); |
||
1755 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1756 | |||
1757 | $this->assertSame('edit', $this->parameters['action']); |
||
1758 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1759 | $this->assertSame($object, $this->parameters['object']); |
||
1760 | |||
1761 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1762 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1763 | } |
||
1764 | |||
1765 | /** |
||
1766 | * @dataProvider getToStringValues |
||
1767 | */ |
||
1768 | public function testEditActionWithModelManagerException($expectedToStringValue, $toStringValue): void |
||
1769 | { |
||
1770 | $object = new \stdClass(); |
||
1771 | |||
1772 | $this->admin->expects($this->once()) |
||
1773 | ->method('getObject') |
||
1774 | ->will($this->returnValue($object)); |
||
1775 | |||
1776 | $this->admin->expects($this->once()) |
||
1777 | ->method('checkAccess') |
||
1778 | ->with($this->equalTo('edit')) |
||
1779 | ->will($this->returnValue(true)); |
||
1780 | |||
1781 | $this->admin->expects($this->any()) |
||
1782 | ->method('getClass') |
||
1783 | ->will($this->returnValue('stdClass')); |
||
1784 | |||
1785 | $form = $this->createMock(Form::class); |
||
1786 | |||
1787 | $this->admin->expects($this->once()) |
||
1788 | ->method('getForm') |
||
1789 | ->will($this->returnValue($form)); |
||
1790 | |||
1791 | $form->expects($this->once()) |
||
1792 | ->method('isValid') |
||
1793 | ->will($this->returnValue(true)); |
||
1794 | |||
1795 | $form->expects($this->once()) |
||
1796 | ->method('getData') |
||
1797 | ->will($this->returnValue($object)); |
||
1798 | |||
1799 | $form->expects($this->once()) |
||
1800 | ->method('all') |
||
1801 | ->willReturn(['field' => 'fielddata']); |
||
1802 | |||
1803 | $this->admin->expects($this->once()) |
||
1804 | ->method('toString') |
||
1805 | ->with($this->equalTo($object)) |
||
1806 | ->will($this->returnValue($toStringValue)); |
||
1807 | |||
1808 | $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
1809 | |||
1810 | $form->expects($this->once()) |
||
1811 | ->method('isSubmitted') |
||
1812 | ->will($this->returnValue(true)); |
||
1813 | $this->request->setMethod('POST'); |
||
1814 | |||
1815 | $formView = $this->createMock(FormView::class); |
||
1816 | |||
1817 | $form->expects($this->any()) |
||
1818 | ->method('createView') |
||
1819 | ->will($this->returnValue($formView)); |
||
1820 | |||
1821 | $this->assertLoggerLogsModelManagerException($this->admin, 'update'); |
||
1822 | $this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
||
1823 | |||
1824 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1825 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1826 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1827 | |||
1828 | $this->assertSame('edit', $this->parameters['action']); |
||
1829 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1830 | $this->assertSame($object, $this->parameters['object']); |
||
1831 | |||
1832 | $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
||
1833 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
1834 | } |
||
1835 | |||
1836 | public function testEditActionWithPreview(): void |
||
1837 | { |
||
1838 | $object = new \stdClass(); |
||
1839 | |||
1840 | $this->admin->expects($this->once()) |
||
1841 | ->method('getObject') |
||
1842 | ->will($this->returnValue($object)); |
||
1843 | |||
1844 | $this->admin->expects($this->once()) |
||
1845 | ->method('checkAccess') |
||
1846 | ->with($this->equalTo('edit')) |
||
1847 | ->will($this->returnValue(true)); |
||
1848 | |||
1849 | $form = $this->createMock(Form::class); |
||
1850 | |||
1851 | $this->admin->expects($this->once()) |
||
1852 | ->method('getForm') |
||
1853 | ->will($this->returnValue($form)); |
||
1854 | |||
1855 | $this->admin->expects($this->once()) |
||
1856 | ->method('supportsPreviewMode') |
||
1857 | ->will($this->returnValue(true)); |
||
1858 | |||
1859 | $formView = $this->createMock(FormView::class); |
||
1860 | |||
1861 | $form->expects($this->any()) |
||
1862 | ->method('createView') |
||
1863 | ->will($this->returnValue($formView)); |
||
1864 | |||
1865 | $form->expects($this->once()) |
||
1866 | ->method('isSubmitted') |
||
1867 | ->will($this->returnValue(true)); |
||
1868 | |||
1869 | $form->expects($this->once()) |
||
1870 | ->method('isValid') |
||
1871 | ->will($this->returnValue(true)); |
||
1872 | |||
1873 | $form->expects($this->once()) |
||
1874 | ->method('all') |
||
1875 | ->willReturn(['field' => 'fielddata']); |
||
1876 | |||
1877 | $this->request->setMethod('POST'); |
||
1878 | $this->request->request->set('btn_preview', 'Preview'); |
||
1879 | |||
1880 | $this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
||
1881 | |||
1882 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
1883 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
1884 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
1885 | |||
1886 | $this->assertSame('edit', $this->parameters['action']); |
||
1887 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
1888 | $this->assertSame($object, $this->parameters['object']); |
||
1889 | |||
1890 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
1891 | $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
||
1892 | } |
||
1893 | |||
1894 | public function testEditActionWithLockException(): void |
||
1895 | { |
||
1896 | $object = new \stdClass(); |
||
1897 | $class = \get_class($object); |
||
1898 | |||
1899 | $this->admin->expects($this->any()) |
||
1900 | ->method('getObject') |
||
1901 | ->will($this->returnValue($object)); |
||
1902 | |||
1903 | $this->admin->expects($this->any()) |
||
1904 | ->method('checkAccess') |
||
1905 | ->with($this->equalTo('edit')) |
||
1906 | ->will($this->returnValue(true)); |
||
1907 | |||
1908 | $this->admin->expects($this->any()) |
||
1909 | ->method('getClass') |
||
1910 | ->will($this->returnValue($class)); |
||
1911 | |||
1912 | $form = $this->createMock(Form::class); |
||
1913 | |||
1914 | $form->expects($this->any()) |
||
1915 | ->method('isValid') |
||
1916 | ->will($this->returnValue(true)); |
||
1917 | |||
1918 | $form->expects($this->once()) |
||
1919 | ->method('getData') |
||
1920 | ->will($this->returnValue($object)); |
||
1921 | |||
1922 | $form->expects($this->once()) |
||
1923 | ->method('all') |
||
1924 | ->willReturn(['field' => 'fielddata']); |
||
1925 | |||
1926 | $this->admin->expects($this->any()) |
||
1927 | ->method('getForm') |
||
1928 | ->will($this->returnValue($form)); |
||
1929 | |||
1930 | $form->expects($this->any()) |
||
1931 | ->method('isSubmitted') |
||
1932 | ->will($this->returnValue(true)); |
||
1933 | $this->request->setMethod('POST'); |
||
1934 | |||
1935 | $this->admin->expects($this->any()) |
||
1936 | ->method('update') |
||
1937 | ->will($this->throwException(new LockException())); |
||
1938 | |||
1939 | $this->admin->expects($this->any()) |
||
1940 | ->method('toString') |
||
1941 | ->with($this->equalTo($object)) |
||
1942 | ->will($this->returnValue($class)); |
||
1943 | |||
1944 | $formView = $this->createMock(FormView::class); |
||
1945 | |||
1946 | $form->expects($this->any()) |
||
1947 | ->method('createView') |
||
1948 | ->will($this->returnValue($formView)); |
||
1949 | |||
1950 | $this->expectTranslate('flash_lock_error', [ |
||
1951 | '%name%' => $class, |
||
1952 | '%link_start%' => '<a href="stdClass_edit">', |
||
1953 | '%link_end%' => '</a>', |
||
1954 | ], 'SonataAdminBundle'); |
||
1955 | |||
1956 | $this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
||
1957 | } |
||
1958 | |||
1959 | public function testCreateActionAccessDenied(): void |
||
1960 | { |
||
1961 | $this->expectException(AccessDeniedException::class); |
||
1962 | |||
1963 | $this->admin->expects($this->once()) |
||
1964 | ->method('checkAccess') |
||
1965 | ->with($this->equalTo('create')) |
||
1966 | ->will($this->throwException(new AccessDeniedException())); |
||
1967 | |||
1968 | $this->controller->createAction($this->request); |
||
1969 | } |
||
1970 | |||
1971 | public function testCreateActionRuntimeException(): void |
||
1972 | { |
||
1973 | $this->expectException(\RuntimeException::class); |
||
1974 | |||
1975 | $this->admin->expects($this->once()) |
||
1976 | ->method('checkAccess') |
||
1977 | ->with($this->equalTo('create')) |
||
1978 | ->will($this->returnValue(true)); |
||
1979 | |||
1980 | $this->admin->expects($this->any()) |
||
1981 | ->method('getClass') |
||
1982 | ->will($this->returnValue('stdClass')); |
||
1983 | |||
1984 | $this->admin->expects($this->once()) |
||
1985 | ->method('getNewInstance') |
||
1986 | ->will($this->returnValue(new \stdClass())); |
||
1987 | |||
1988 | $form = $this->createMock(Form::class); |
||
1989 | |||
1990 | $this->admin->expects($this->once()) |
||
1991 | ->method('getForm') |
||
1992 | ->will($this->returnValue($form)); |
||
1993 | |||
1994 | $form->expects($this->once()) |
||
1995 | ->method('all') |
||
1996 | ->willReturn([]); |
||
1997 | |||
1998 | $this->controller->createAction($this->request); |
||
1999 | } |
||
2000 | |||
2001 | public function testPreCreate(): void |
||
2002 | { |
||
2003 | $object = new \stdClass(); |
||
2004 | $object->foo = 123456; |
||
2005 | |||
2006 | $this->admin->expects($this->once()) |
||
2007 | ->method('checkAccess') |
||
2008 | ->with($this->equalTo('create')) |
||
2009 | ->will($this->returnValue(true)); |
||
2010 | |||
2011 | $this->admin->expects($this->any()) |
||
2012 | ->method('getClass') |
||
2013 | ->will($this->returnValue('stdClass')); |
||
2014 | |||
2015 | $this->admin->expects($this->once()) |
||
2016 | ->method('getNewInstance') |
||
2017 | ->will($this->returnValue($object)); |
||
2018 | |||
2019 | $controller = new PreCRUDController(); |
||
2020 | $controller->setContainer($this->container); |
||
2021 | |||
2022 | $response = $controller->createAction($this->request); |
||
2023 | $this->assertInstanceOf(Response::class, $response); |
||
2024 | $this->assertSame('preCreate called: 123456', $response->getContent()); |
||
2025 | } |
||
2026 | |||
2027 | public function testCreateAction(): void |
||
2028 | { |
||
2029 | $this->admin->expects($this->once()) |
||
2030 | ->method('checkAccess') |
||
2031 | ->with($this->equalTo('create')) |
||
2032 | ->will($this->returnValue(true)); |
||
2033 | |||
2034 | $object = new \stdClass(); |
||
2035 | |||
2036 | $this->admin->expects($this->any()) |
||
2037 | ->method('getClass') |
||
2038 | ->will($this->returnValue('stdClass')); |
||
2039 | |||
2040 | $this->admin->expects($this->once()) |
||
2041 | ->method('getNewInstance') |
||
2042 | ->will($this->returnValue($object)); |
||
2043 | |||
2044 | $form = $this->createMock(Form::class); |
||
2045 | |||
2046 | $this->admin->expects($this->once()) |
||
2047 | ->method('getForm') |
||
2048 | ->will($this->returnValue($form)); |
||
2049 | |||
2050 | $form->expects($this->once()) |
||
2051 | ->method('all') |
||
2052 | ->willReturn(['field' => 'fielddata']); |
||
2053 | |||
2054 | $formView = $this->createMock(FormView::class); |
||
2055 | |||
2056 | $form->expects($this->any()) |
||
2057 | ->method('createView') |
||
2058 | ->will($this->returnValue($formView)); |
||
2059 | |||
2060 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
2061 | |||
2062 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2063 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2064 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2065 | |||
2066 | $this->assertSame('create', $this->parameters['action']); |
||
2067 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
2068 | $this->assertSame($object, $this->parameters['object']); |
||
2069 | |||
2070 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2071 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
2072 | } |
||
2073 | |||
2074 | /** |
||
2075 | * @dataProvider getToStringValues |
||
2076 | */ |
||
2077 | public function testCreateActionSuccess($expectedToStringValue, $toStringValue): void |
||
2078 | { |
||
2079 | $object = new \stdClass(); |
||
2080 | |||
2081 | $this->admin->expects($this->exactly(2)) |
||
2082 | ->method('checkAccess') |
||
2083 | ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) { |
||
2084 | if ('edit' == $name) { |
||
2085 | return true; |
||
2086 | } |
||
2087 | |||
2088 | if ('create' != $name) { |
||
2089 | return false; |
||
2090 | } |
||
2091 | |||
2092 | if (null === $objectIn) { |
||
2093 | return true; |
||
2094 | } |
||
2095 | |||
2096 | return $objectIn === $object; |
||
2097 | })); |
||
2098 | |||
2099 | $this->admin->expects($this->once()) |
||
2100 | ->method('hasRoute') |
||
2101 | ->with($this->equalTo('edit')) |
||
2102 | ->will($this->returnValue(true)); |
||
2103 | |||
2104 | $this->admin->expects($this->once()) |
||
2105 | ->method('hasAccess') |
||
2106 | ->with($this->equalTo('edit')) |
||
2107 | ->will($this->returnValue(true)); |
||
2108 | |||
2109 | $this->admin->expects($this->once()) |
||
2110 | ->method('getNewInstance') |
||
2111 | ->will($this->returnValue($object)); |
||
2112 | |||
2113 | $this->admin->expects($this->once()) |
||
2114 | ->method('create') |
||
2115 | ->will($this->returnArgument(0)); |
||
2116 | |||
2117 | $form = $this->createMock(Form::class); |
||
2118 | |||
2119 | $this->admin->expects($this->any()) |
||
2120 | ->method('getClass') |
||
2121 | ->will($this->returnValue('stdClass')); |
||
2122 | |||
2123 | $this->admin->expects($this->once()) |
||
2124 | ->method('getForm') |
||
2125 | ->will($this->returnValue($form)); |
||
2126 | |||
2127 | $form->expects($this->once()) |
||
2128 | ->method('all') |
||
2129 | ->willReturn(['field' => 'fielddata']); |
||
2130 | |||
2131 | $form->expects($this->once()) |
||
2132 | ->method('isSubmitted') |
||
2133 | ->will($this->returnValue(true)); |
||
2134 | |||
2135 | $form->expects($this->once()) |
||
2136 | ->method('isValid') |
||
2137 | ->will($this->returnValue(true)); |
||
2138 | |||
2139 | $form->expects($this->once()) |
||
2140 | ->method('getData') |
||
2141 | ->will($this->returnValue($object)); |
||
2142 | |||
2143 | $this->admin->expects($this->once()) |
||
2144 | ->method('toString') |
||
2145 | ->with($this->equalTo($object)) |
||
2146 | ->will($this->returnValue($toStringValue)); |
||
2147 | |||
2148 | $this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
2149 | |||
2150 | $this->request->setMethod('POST'); |
||
2151 | |||
2152 | $response = $this->controller->createAction($this->request); |
||
2153 | |||
2154 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
2155 | $this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
2156 | $this->assertSame('stdClass_edit', $response->getTargetUrl()); |
||
2157 | } |
||
2158 | |||
2159 | public function testCreateActionAccessDenied2(): void |
||
2160 | { |
||
2161 | $this->expectException(AccessDeniedException::class); |
||
2162 | |||
2163 | $object = new \stdClass(); |
||
2164 | |||
2165 | $this->admin->expects($this->any()) |
||
2166 | ->method('checkAccess') |
||
2167 | ->will($this->returnCallback(function ($name, $object = null) { |
||
2168 | if ('create' != $name) { |
||
2169 | throw new AccessDeniedException(); |
||
2170 | } |
||
2171 | if (null === $object) { |
||
2172 | return true; |
||
2173 | } |
||
2174 | |||
2175 | throw new AccessDeniedException(); |
||
2176 | })); |
||
2177 | |||
2178 | $this->admin->expects($this->once()) |
||
2179 | ->method('getNewInstance') |
||
2180 | ->will($this->returnValue($object)); |
||
2181 | |||
2182 | $form = $this->createMock(Form::class); |
||
2183 | |||
2184 | $this->admin->expects($this->any()) |
||
2185 | ->method('getClass') |
||
2186 | ->will($this->returnValue('stdClass')); |
||
2187 | |||
2188 | $this->admin->expects($this->once()) |
||
2189 | ->method('getForm') |
||
2190 | ->will($this->returnValue($form)); |
||
2191 | |||
2192 | $form->expects($this->once()) |
||
2193 | ->method('all') |
||
2194 | ->willReturn(['field' => 'fielddata']); |
||
2195 | |||
2196 | $form->expects($this->once()) |
||
2197 | ->method('isSubmitted') |
||
2198 | ->will($this->returnValue(true)); |
||
2199 | |||
2200 | $form->expects($this->once()) |
||
2201 | ->method('getData') |
||
2202 | ->will($this->returnValue($object)); |
||
2203 | |||
2204 | $form->expects($this->once()) |
||
2205 | ->method('isValid') |
||
2206 | ->will($this->returnValue(true)); |
||
2207 | |||
2208 | $this->request->setMethod('POST'); |
||
2209 | |||
2210 | $this->controller->createAction($this->request); |
||
2211 | } |
||
2212 | |||
2213 | /** |
||
2214 | * @dataProvider getToStringValues |
||
2215 | */ |
||
2216 | public function testCreateActionError($expectedToStringValue, $toStringValue): void |
||
2217 | { |
||
2218 | $this->admin->expects($this->once()) |
||
2219 | ->method('checkAccess') |
||
2220 | ->with($this->equalTo('create')) |
||
2221 | ->will($this->returnValue(true)); |
||
2222 | |||
2223 | $object = new \stdClass(); |
||
2224 | |||
2225 | $this->admin->expects($this->any()) |
||
2226 | ->method('getClass') |
||
2227 | ->will($this->returnValue('stdClass')); |
||
2228 | |||
2229 | $this->admin->expects($this->once()) |
||
2230 | ->method('getNewInstance') |
||
2231 | ->will($this->returnValue($object)); |
||
2232 | |||
2233 | $form = $this->createMock(Form::class); |
||
2234 | |||
2235 | $this->admin->expects($this->once()) |
||
2236 | ->method('getForm') |
||
2237 | ->will($this->returnValue($form)); |
||
2238 | |||
2239 | $form->expects($this->once()) |
||
2240 | ->method('all') |
||
2241 | ->willReturn(['field' => 'fielddata']); |
||
2242 | |||
2243 | $form->expects($this->once()) |
||
2244 | ->method('isSubmitted') |
||
2245 | ->will($this->returnValue(true)); |
||
2246 | |||
2247 | $form->expects($this->once()) |
||
2248 | ->method('isValid') |
||
2249 | ->will($this->returnValue(false)); |
||
2250 | |||
2251 | $this->admin->expects($this->once()) |
||
2252 | ->method('toString') |
||
2253 | ->with($this->equalTo($object)) |
||
2254 | ->will($this->returnValue($toStringValue)); |
||
2255 | |||
2256 | $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
2257 | |||
2258 | $this->request->setMethod('POST'); |
||
2259 | |||
2260 | $formView = $this->createMock(FormView::class); |
||
2261 | |||
2262 | $form->expects($this->any()) |
||
2263 | ->method('createView') |
||
2264 | ->will($this->returnValue($formView)); |
||
2265 | |||
2266 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
2267 | |||
2268 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2269 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2270 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2271 | |||
2272 | $this->assertSame('create', $this->parameters['action']); |
||
2273 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
2274 | $this->assertSame($object, $this->parameters['object']); |
||
2275 | |||
2276 | $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
||
2277 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
2278 | } |
||
2279 | |||
2280 | /** |
||
2281 | * @dataProvider getToStringValues |
||
2282 | */ |
||
2283 | public function testCreateActionWithModelManagerException($expectedToStringValue, $toStringValue): void |
||
2284 | { |
||
2285 | $this->admin->expects($this->exactly(2)) |
||
2286 | ->method('checkAccess') |
||
2287 | ->with($this->equalTo('create')) |
||
2288 | ->will($this->returnValue(true)); |
||
2289 | |||
2290 | $this->admin->expects($this->any()) |
||
2291 | ->method('getClass') |
||
2292 | ->will($this->returnValue('stdClass')); |
||
2293 | |||
2294 | $object = new \stdClass(); |
||
2295 | |||
2296 | $this->admin->expects($this->once()) |
||
2297 | ->method('getNewInstance') |
||
2298 | ->will($this->returnValue($object)); |
||
2299 | |||
2300 | $form = $this->createMock(Form::class); |
||
2301 | |||
2302 | $this->admin->expects($this->once()) |
||
2303 | ->method('getForm') |
||
2304 | ->will($this->returnValue($form)); |
||
2305 | |||
2306 | $form->expects($this->once()) |
||
2307 | ->method('all') |
||
2308 | ->willReturn(['field' => 'fielddata']); |
||
2309 | |||
2310 | $form->expects($this->once()) |
||
2311 | ->method('isValid') |
||
2312 | ->will($this->returnValue(true)); |
||
2313 | |||
2314 | $this->admin->expects($this->once()) |
||
2315 | ->method('toString') |
||
2316 | ->with($this->equalTo($object)) |
||
2317 | ->will($this->returnValue($toStringValue)); |
||
2318 | |||
2319 | $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
||
2320 | |||
2321 | $form->expects($this->once()) |
||
2322 | ->method('isSubmitted') |
||
2323 | ->will($this->returnValue(true)); |
||
2324 | |||
2325 | $form->expects($this->once()) |
||
2326 | ->method('getData') |
||
2327 | ->will($this->returnValue($object)); |
||
2328 | |||
2329 | $this->request->setMethod('POST'); |
||
2330 | |||
2331 | $formView = $this->createMock(FormView::class); |
||
2332 | |||
2333 | $form->expects($this->any()) |
||
2334 | ->method('createView') |
||
2335 | ->will($this->returnValue($formView)); |
||
2336 | |||
2337 | $this->assertLoggerLogsModelManagerException($this->admin, 'create'); |
||
2338 | |||
2339 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
2340 | |||
2341 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2342 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2343 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2344 | |||
2345 | $this->assertSame('create', $this->parameters['action']); |
||
2346 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
2347 | $this->assertSame($object, $this->parameters['object']); |
||
2348 | |||
2349 | $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
||
2350 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
2351 | } |
||
2352 | |||
2353 | public function testCreateActionAjaxSuccess(): void |
||
2354 | { |
||
2355 | $object = new \stdClass(); |
||
2356 | |||
2357 | $this->admin->expects($this->exactly(2)) |
||
2358 | ->method('checkAccess') |
||
2359 | ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) { |
||
2360 | if ('create' != $name) { |
||
2361 | return false; |
||
2362 | } |
||
2363 | |||
2364 | if (null === $objectIn) { |
||
2365 | return true; |
||
2366 | } |
||
2367 | |||
2368 | return $objectIn === $object; |
||
2369 | })); |
||
2370 | |||
2371 | $this->admin->expects($this->once()) |
||
2372 | ->method('getNewInstance') |
||
2373 | ->will($this->returnValue($object)); |
||
2374 | |||
2375 | $this->admin->expects($this->once()) |
||
2376 | ->method('create') |
||
2377 | ->will($this->returnArgument(0)); |
||
2378 | |||
2379 | $form = $this->createMock(Form::class); |
||
2380 | |||
2381 | $this->admin->expects($this->once()) |
||
2382 | ->method('getForm') |
||
2383 | ->will($this->returnValue($form)); |
||
2384 | |||
2385 | $form->expects($this->once()) |
||
2386 | ->method('all') |
||
2387 | ->willReturn(['field' => 'fielddata']); |
||
2388 | |||
2389 | $form->expects($this->once()) |
||
2390 | ->method('isSubmitted') |
||
2391 | ->will($this->returnValue(true)); |
||
2392 | |||
2393 | $form->expects($this->once()) |
||
2394 | ->method('isValid') |
||
2395 | ->will($this->returnValue(true)); |
||
2396 | |||
2397 | $form->expects($this->once()) |
||
2398 | ->method('getData') |
||
2399 | ->will($this->returnValue($object)); |
||
2400 | |||
2401 | $this->admin->expects($this->any()) |
||
2402 | ->method('getClass') |
||
2403 | ->will($this->returnValue('stdClass')); |
||
2404 | |||
2405 | $this->admin->expects($this->once()) |
||
2406 | ->method('getNormalizedIdentifier') |
||
2407 | ->with($this->equalTo($object)) |
||
2408 | ->will($this->returnValue('foo_normalized')); |
||
2409 | |||
2410 | $this->request->setMethod('POST'); |
||
2411 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
2412 | |||
2413 | $response = $this->controller->createAction($this->request); |
||
2414 | |||
2415 | $this->assertInstanceOf(Response::class, $response); |
||
2416 | $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized']), $response->getContent()); |
||
2417 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2418 | } |
||
2419 | |||
2420 | public function testCreateActionAjaxError(): void |
||
2421 | { |
||
2422 | $this->admin->expects($this->once()) |
||
2423 | ->method('checkAccess') |
||
2424 | ->with($this->equalTo('create')) |
||
2425 | ->will($this->returnValue(true)); |
||
2426 | |||
2427 | $object = new \stdClass(); |
||
2428 | |||
2429 | $this->admin->expects($this->once()) |
||
2430 | ->method('getNewInstance') |
||
2431 | ->will($this->returnValue($object)); |
||
2432 | |||
2433 | $form = $this->createMock(Form::class); |
||
2434 | |||
2435 | $this->admin->expects($this->any()) |
||
2436 | ->method('getClass') |
||
2437 | ->will($this->returnValue('stdClass')); |
||
2438 | |||
2439 | $this->admin->expects($this->once()) |
||
2440 | ->method('getForm') |
||
2441 | ->will($this->returnValue($form)); |
||
2442 | |||
2443 | $form->expects($this->once()) |
||
2444 | ->method('all') |
||
2445 | ->willReturn(['field' => 'fielddata']); |
||
2446 | |||
2447 | $form->expects($this->once()) |
||
2448 | ->method('isSubmitted') |
||
2449 | ->will($this->returnValue(true)); |
||
2450 | |||
2451 | $form->expects($this->once()) |
||
2452 | ->method('isValid') |
||
2453 | ->will($this->returnValue(false)); |
||
2454 | |||
2455 | $this->request->setMethod('POST'); |
||
2456 | $this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
||
2457 | |||
2458 | $formView = $this->createMock(FormView::class); |
||
2459 | |||
2460 | $form->expects($this->any()) |
||
2461 | ->method('createView') |
||
2462 | ->will($this->returnValue($formView)); |
||
2463 | |||
2464 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
2465 | |||
2466 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2467 | $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']); |
||
2468 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2469 | |||
2470 | $this->assertSame('create', $this->parameters['action']); |
||
2471 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
2472 | $this->assertSame($object, $this->parameters['object']); |
||
2473 | |||
2474 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2475 | $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
||
2476 | } |
||
2477 | |||
2478 | public function testCreateActionWithPreview(): void |
||
2479 | { |
||
2480 | $this->admin->expects($this->once()) |
||
2481 | ->method('checkAccess') |
||
2482 | ->with($this->equalTo('create')) |
||
2483 | ->will($this->returnValue(true)); |
||
2484 | |||
2485 | $object = new \stdClass(); |
||
2486 | |||
2487 | $this->admin->expects($this->once()) |
||
2488 | ->method('getNewInstance') |
||
2489 | ->will($this->returnValue($object)); |
||
2490 | |||
2491 | $form = $this->createMock(Form::class); |
||
2492 | |||
2493 | $this->admin->expects($this->any()) |
||
2494 | ->method('getClass') |
||
2495 | ->will($this->returnValue('stdClass')); |
||
2496 | |||
2497 | $this->admin->expects($this->once()) |
||
2498 | ->method('getForm') |
||
2499 | ->will($this->returnValue($form)); |
||
2500 | |||
2501 | $form->expects($this->once()) |
||
2502 | ->method('all') |
||
2503 | ->willReturn(['field' => 'fielddata']); |
||
2504 | |||
2505 | $this->admin->expects($this->once()) |
||
2506 | ->method('supportsPreviewMode') |
||
2507 | ->will($this->returnValue(true)); |
||
2508 | |||
2509 | $formView = $this->createMock(FormView::class); |
||
2510 | |||
2511 | $form->expects($this->any()) |
||
2512 | ->method('createView') |
||
2513 | ->will($this->returnValue($formView)); |
||
2514 | |||
2515 | $form->expects($this->once()) |
||
2516 | ->method('isSubmitted') |
||
2517 | ->will($this->returnValue(true)); |
||
2518 | |||
2519 | $form->expects($this->once()) |
||
2520 | ->method('isValid') |
||
2521 | ->will($this->returnValue(true)); |
||
2522 | |||
2523 | $this->request->setMethod('POST'); |
||
2524 | $this->request->request->set('btn_preview', 'Preview'); |
||
2525 | |||
2526 | $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
||
2527 | |||
2528 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2529 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2530 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2531 | |||
2532 | $this->assertSame('create', $this->parameters['action']); |
||
2533 | $this->assertInstanceOf(FormView::class, $this->parameters['form']); |
||
2534 | $this->assertSame($object, $this->parameters['object']); |
||
2535 | |||
2536 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2537 | $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
||
2538 | } |
||
2539 | |||
2540 | public function testExportActionAccessDenied(): void |
||
2541 | { |
||
2542 | $this->expectException(AccessDeniedException::class); |
||
2543 | |||
2544 | $this->admin->expects($this->once()) |
||
2545 | ->method('checkAccess') |
||
2546 | ->with($this->equalTo('export')) |
||
2547 | ->will($this->throwException(new AccessDeniedException())); |
||
2548 | |||
2549 | $this->controller->exportAction($this->request); |
||
2550 | } |
||
2551 | |||
2552 | public function testExportActionWrongFormat(): void |
||
2553 | { |
||
2554 | $this->expectException(\RuntimeException::class, 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`'); |
||
2555 | |||
2556 | $this->admin->expects($this->once()) |
||
2557 | ->method('checkAccess') |
||
2558 | ->with($this->equalTo('export')) |
||
2559 | ->will($this->returnValue(true)); |
||
2560 | |||
2561 | $this->admin->expects($this->once()) |
||
2562 | ->method('getExportFormats') |
||
2563 | ->will($this->returnValue(['json'])); |
||
2564 | |||
2565 | $this->admin->expects($this->any()) |
||
2566 | ->method('getClass') |
||
2567 | ->will($this->returnValue('Foo')); |
||
2568 | |||
2569 | $this->request->query->set('format', 'csv'); |
||
2570 | |||
2571 | $this->controller->exportAction($this->request); |
||
2572 | } |
||
2573 | |||
2574 | public function testExportAction(): void |
||
2575 | { |
||
2576 | $this->admin->expects($this->once()) |
||
2577 | ->method('checkAccess') |
||
2578 | ->with($this->equalTo('export')) |
||
2579 | ->will($this->returnValue(true)); |
||
2580 | |||
2581 | $this->admin->expects($this->once()) |
||
2582 | ->method('getExportFormats') |
||
2583 | ->will($this->returnValue(['json'])); |
||
2584 | |||
2585 | $this->admin->expects($this->once()) |
||
2586 | ->method('getClass') |
||
2587 | ->will($this->returnValue(\stdClass::class)); |
||
2588 | |||
2589 | $dataSourceIterator = $this->createMock(SourceIteratorInterface::class); |
||
2590 | |||
2591 | $this->admin->expects($this->once()) |
||
2592 | ->method('getDataSourceIterator') |
||
2593 | ->will($this->returnValue($dataSourceIterator)); |
||
2594 | |||
2595 | $this->request->query->set('format', 'json'); |
||
2596 | |||
2597 | $response = $this->controller->exportAction($this->request); |
||
2598 | $this->assertInstanceOf(StreamedResponse::class, $response); |
||
2599 | $this->assertSame(200, $response->getStatusCode()); |
||
2600 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2601 | } |
||
2602 | |||
2603 | public function testHistoryActionAccessDenied(): void |
||
2604 | { |
||
2605 | $this->expectException(AccessDeniedException::class); |
||
2606 | |||
2607 | $this->admin->expects($this->any()) |
||
2608 | ->method('getObject') |
||
2609 | ->will($this->returnValue(new \StdClass())); |
||
2610 | |||
2611 | $this->admin->expects($this->once()) |
||
2612 | ->method('checkAccess') |
||
2613 | ->with($this->equalTo('history')) |
||
2614 | ->will($this->throwException(new AccessDeniedException())); |
||
2615 | |||
2616 | $this->controller->historyAction(null, $this->request); |
||
2617 | } |
||
2618 | |||
2619 | public function testHistoryActionNotFoundException(): void |
||
2620 | { |
||
2621 | $this->expectException(NotFoundHttpException::class); |
||
2622 | |||
2623 | $this->admin->expects($this->once()) |
||
2624 | ->method('getObject') |
||
2625 | ->will($this->returnValue(false)); |
||
2626 | |||
2627 | $this->controller->historyAction(null, $this->request); |
||
2628 | } |
||
2629 | |||
2630 | public function testHistoryActionNoReader(): void |
||
2631 | { |
||
2632 | $this->expectException(NotFoundHttpException::class, 'unable to find the audit reader for class : Foo'); |
||
2633 | |||
2634 | $this->request->query->set('id', 123); |
||
2635 | |||
2636 | $this->admin->expects($this->once()) |
||
2637 | ->method('checkAccess') |
||
2638 | ->with($this->equalTo('history')) |
||
2639 | ->will($this->returnValue(true)); |
||
2640 | |||
2641 | $object = new \stdClass(); |
||
2642 | |||
2643 | $this->admin->expects($this->once()) |
||
2644 | ->method('getObject') |
||
2645 | ->will($this->returnValue($object)); |
||
2646 | |||
2647 | $this->admin->expects($this->any()) |
||
2648 | ->method('getClass') |
||
2649 | ->will($this->returnValue('Foo')); |
||
2650 | |||
2651 | $this->auditManager->expects($this->once()) |
||
2652 | ->method('hasReader') |
||
2653 | ->with($this->equalTo('Foo')) |
||
2654 | ->will($this->returnValue(false)); |
||
2655 | |||
2656 | $this->controller->historyAction(null, $this->request); |
||
2657 | } |
||
2658 | |||
2659 | public function testHistoryAction(): void |
||
2660 | { |
||
2661 | $this->request->query->set('id', 123); |
||
2662 | |||
2663 | $this->admin->expects($this->once()) |
||
2664 | ->method('checkAccess') |
||
2665 | ->with($this->equalTo('history')) |
||
2666 | ->will($this->returnValue(true)); |
||
2667 | |||
2668 | $object = new \stdClass(); |
||
2669 | |||
2670 | $this->admin->expects($this->once()) |
||
2671 | ->method('getObject') |
||
2672 | ->will($this->returnValue($object)); |
||
2673 | |||
2674 | $this->admin->expects($this->any()) |
||
2675 | ->method('getClass') |
||
2676 | ->will($this->returnValue('Foo')); |
||
2677 | |||
2678 | $this->auditManager->expects($this->once()) |
||
2679 | ->method('hasReader') |
||
2680 | ->with($this->equalTo('Foo')) |
||
2681 | ->will($this->returnValue(true)); |
||
2682 | |||
2683 | $reader = $this->createMock(AuditReaderInterface::class); |
||
2684 | |||
2685 | $this->auditManager->expects($this->once()) |
||
2686 | ->method('getReader') |
||
2687 | ->with($this->equalTo('Foo')) |
||
2688 | ->will($this->returnValue($reader)); |
||
2689 | |||
2690 | $reader->expects($this->once()) |
||
2691 | ->method('findRevisions') |
||
2692 | ->with($this->equalTo('Foo'), $this->equalTo(123)) |
||
2693 | ->will($this->returnValue([])); |
||
2694 | |||
2695 | $this->assertInstanceOf(Response::class, $this->controller->historyAction(null, $this->request)); |
||
2696 | |||
2697 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2698 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2699 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2700 | |||
2701 | $this->assertSame('history', $this->parameters['action']); |
||
2702 | $this->assertSame([], $this->parameters['revisions']); |
||
2703 | $this->assertSame($object, $this->parameters['object']); |
||
2704 | |||
2705 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2706 | $this->assertSame('@SonataAdmin/CRUD/history.html.twig', $this->template); |
||
2707 | } |
||
2708 | |||
2709 | public function testAclActionAclNotEnabled(): void |
||
2710 | { |
||
2711 | $this->expectException(NotFoundHttpException::class, 'ACL are not enabled for this admin'); |
||
2712 | |||
2713 | $this->controller->aclAction(null, $this->request); |
||
2714 | } |
||
2715 | |||
2716 | public function testAclActionNotFoundException(): void |
||
2717 | { |
||
2718 | $this->expectException(NotFoundHttpException::class); |
||
2719 | |||
2720 | $this->admin->expects($this->once()) |
||
2721 | ->method('isAclEnabled') |
||
2722 | ->will($this->returnValue(true)); |
||
2723 | |||
2724 | $this->admin->expects($this->once()) |
||
2725 | ->method('getObject') |
||
2726 | ->will($this->returnValue(false)); |
||
2727 | |||
2728 | $this->controller->aclAction(null, $this->request); |
||
2729 | } |
||
2730 | |||
2731 | public function testAclActionAccessDenied(): void |
||
2732 | { |
||
2733 | $this->expectException(AccessDeniedException::class); |
||
2734 | |||
2735 | $this->admin->expects($this->once()) |
||
2736 | ->method('isAclEnabled') |
||
2737 | ->will($this->returnValue(true)); |
||
2738 | |||
2739 | $object = new \stdClass(); |
||
2740 | |||
2741 | $this->admin->expects($this->once()) |
||
2742 | ->method('getObject') |
||
2743 | ->will($this->returnValue($object)); |
||
2744 | |||
2745 | $this->admin->expects($this->once()) |
||
2746 | ->method('checkAccess') |
||
2747 | ->with($this->equalTo('acl'), $this->equalTo($object)) |
||
2748 | ->will($this->throwException(new AccessDeniedException())); |
||
2749 | |||
2750 | $this->controller->aclAction(null, $this->request); |
||
2751 | } |
||
2752 | |||
2753 | public function testAclAction(): void |
||
2754 | { |
||
2755 | $this->request->query->set('id', 123); |
||
2756 | |||
2757 | $this->admin->expects($this->once()) |
||
2758 | ->method('isAclEnabled') |
||
2759 | ->will($this->returnValue(true)); |
||
2760 | |||
2761 | $object = new \stdClass(); |
||
2762 | |||
2763 | $this->admin->expects($this->once()) |
||
2764 | ->method('getObject') |
||
2765 | ->will($this->returnValue($object)); |
||
2766 | |||
2767 | $this->admin->expects($this->any()) |
||
2768 | ->method('checkAccess') |
||
2769 | ->will($this->returnValue(true)); |
||
2770 | |||
2771 | $this->admin->expects($this->any()) |
||
2772 | ->method('getSecurityInformation') |
||
2773 | ->will($this->returnValue([])); |
||
2774 | |||
2775 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2776 | ->method('getMaskBuilderClass') |
||
2777 | ->will($this->returnValue(AdminPermissionMap::class)); |
||
2778 | |||
2779 | $aclUsersForm = $this->getMockBuilder(Form::class) |
||
2780 | ->disableOriginalConstructor() |
||
2781 | ->getMock(); |
||
2782 | |||
2783 | $aclUsersForm->expects($this->once()) |
||
2784 | ->method('createView') |
||
2785 | ->will($this->returnValue($this->createMock(FormView::class))); |
||
2786 | |||
2787 | $aclRolesForm = $this->getMockBuilder(Form::class) |
||
2788 | ->disableOriginalConstructor() |
||
2789 | ->getMock(); |
||
2790 | |||
2791 | $aclRolesForm->expects($this->once()) |
||
2792 | ->method('createView') |
||
2793 | ->will($this->returnValue($this->createMock(FormView::class))); |
||
2794 | |||
2795 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2796 | ->method('createAclUsersForm') |
||
2797 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2798 | ->will($this->returnValue($aclUsersForm)); |
||
2799 | |||
2800 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2801 | ->method('createAclRolesForm') |
||
2802 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2803 | ->will($this->returnValue($aclRolesForm)); |
||
2804 | |||
2805 | $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
||
2806 | ->disableOriginalConstructor() |
||
2807 | ->getMock(); |
||
2808 | |||
2809 | $aclSecurityHandler->expects($this->any()) |
||
2810 | ->method('getObjectPermissions') |
||
2811 | ->will($this->returnValue([])); |
||
2812 | |||
2813 | $this->admin->expects($this->any()) |
||
2814 | ->method('getSecurityHandler') |
||
2815 | ->will($this->returnValue($aclSecurityHandler)); |
||
2816 | |||
2817 | $this->assertInstanceOf(Response::class, $this->controller->aclAction(null, $this->request)); |
||
2818 | |||
2819 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2820 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2821 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2822 | |||
2823 | $this->assertSame('acl', $this->parameters['action']); |
||
2824 | $this->assertSame([], $this->parameters['permissions']); |
||
2825 | $this->assertSame($object, $this->parameters['object']); |
||
2826 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']); |
||
2827 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']); |
||
2828 | $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']); |
||
2829 | $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']); |
||
2830 | |||
2831 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2832 | $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template); |
||
2833 | } |
||
2834 | |||
2835 | public function testAclActionInvalidUpdate(): void |
||
2836 | { |
||
2837 | $this->request->query->set('id', 123); |
||
2838 | $this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, []); |
||
2839 | |||
2840 | $this->admin->expects($this->once()) |
||
2841 | ->method('isAclEnabled') |
||
2842 | ->will($this->returnValue(true)); |
||
2843 | |||
2844 | $object = new \stdClass(); |
||
2845 | |||
2846 | $this->admin->expects($this->once()) |
||
2847 | ->method('getObject') |
||
2848 | ->will($this->returnValue($object)); |
||
2849 | |||
2850 | $this->admin->expects($this->any()) |
||
2851 | ->method('checkAccess') |
||
2852 | ->will($this->returnValue(true)); |
||
2853 | |||
2854 | $this->admin->expects($this->any()) |
||
2855 | ->method('getSecurityInformation') |
||
2856 | ->will($this->returnValue([])); |
||
2857 | |||
2858 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2859 | ->method('getMaskBuilderClass') |
||
2860 | ->will($this->returnValue(AdminPermissionMap::class)); |
||
2861 | |||
2862 | $aclUsersForm = $this->getMockBuilder(Form::class) |
||
2863 | ->disableOriginalConstructor() |
||
2864 | ->getMock(); |
||
2865 | |||
2866 | $aclUsersForm->expects($this->once()) |
||
2867 | ->method('isValid') |
||
2868 | ->will($this->returnValue(false)); |
||
2869 | |||
2870 | $aclUsersForm->expects($this->once()) |
||
2871 | ->method('createView') |
||
2872 | ->will($this->returnValue($this->createMock(FormView::class))); |
||
2873 | |||
2874 | $aclRolesForm = $this->getMockBuilder(Form::class) |
||
2875 | ->disableOriginalConstructor() |
||
2876 | ->getMock(); |
||
2877 | |||
2878 | $aclRolesForm->expects($this->once()) |
||
2879 | ->method('createView') |
||
2880 | ->will($this->returnValue($this->createMock(FormView::class))); |
||
2881 | |||
2882 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2883 | ->method('createAclUsersForm') |
||
2884 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2885 | ->will($this->returnValue($aclUsersForm)); |
||
2886 | |||
2887 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2888 | ->method('createAclRolesForm') |
||
2889 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2890 | ->will($this->returnValue($aclRolesForm)); |
||
2891 | |||
2892 | $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
||
2893 | ->disableOriginalConstructor() |
||
2894 | ->getMock(); |
||
2895 | |||
2896 | $aclSecurityHandler->expects($this->any()) |
||
2897 | ->method('getObjectPermissions') |
||
2898 | ->will($this->returnValue([])); |
||
2899 | |||
2900 | $this->admin->expects($this->any()) |
||
2901 | ->method('getSecurityHandler') |
||
2902 | ->will($this->returnValue($aclSecurityHandler)); |
||
2903 | |||
2904 | $this->request->setMethod('POST'); |
||
2905 | |||
2906 | $this->assertInstanceOf(Response::class, $this->controller->aclAction(null, $this->request)); |
||
2907 | |||
2908 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
2909 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
2910 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
2911 | |||
2912 | $this->assertSame('acl', $this->parameters['action']); |
||
2913 | $this->assertSame([], $this->parameters['permissions']); |
||
2914 | $this->assertSame($object, $this->parameters['object']); |
||
2915 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']); |
||
2916 | $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']); |
||
2917 | $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']); |
||
2918 | $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']); |
||
2919 | |||
2920 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
2921 | $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template); |
||
2922 | } |
||
2923 | |||
2924 | public function testAclActionSuccessfulUpdate(): void |
||
2925 | { |
||
2926 | $this->request->query->set('id', 123); |
||
2927 | $this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, []); |
||
2928 | |||
2929 | $this->admin->expects($this->once()) |
||
2930 | ->method('isAclEnabled') |
||
2931 | ->will($this->returnValue(true)); |
||
2932 | |||
2933 | $object = new \stdClass(); |
||
2934 | |||
2935 | $this->admin->expects($this->once()) |
||
2936 | ->method('getObject') |
||
2937 | ->will($this->returnValue($object)); |
||
2938 | |||
2939 | $this->admin->expects($this->any()) |
||
2940 | ->method('checkAccess') |
||
2941 | ->will($this->returnValue(true)); |
||
2942 | |||
2943 | $this->admin->expects($this->any()) |
||
2944 | ->method('getSecurityInformation') |
||
2945 | ->will($this->returnValue([])); |
||
2946 | |||
2947 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2948 | ->method('getMaskBuilderClass') |
||
2949 | ->will($this->returnValue(AdminPermissionMap::class)); |
||
2950 | |||
2951 | $aclUsersForm = $this->getMockBuilder(Form::class) |
||
2952 | ->disableOriginalConstructor() |
||
2953 | ->getMock(); |
||
2954 | |||
2955 | $aclUsersForm->expects($this->any()) |
||
2956 | ->method('createView') |
||
2957 | ->will($this->returnValue($this->createMock(FormView::class))); |
||
2958 | |||
2959 | $aclRolesForm = $this->getMockBuilder(Form::class) |
||
2960 | ->disableOriginalConstructor() |
||
2961 | ->getMock(); |
||
2962 | |||
2963 | $aclRolesForm->expects($this->any()) |
||
2964 | ->method('createView') |
||
2965 | ->will($this->returnValue($this->createMock(FormView::class))); |
||
2966 | |||
2967 | $aclRolesForm->expects($this->once()) |
||
2968 | ->method('isValid') |
||
2969 | ->will($this->returnValue(true)); |
||
2970 | |||
2971 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2972 | ->method('createAclUsersForm') |
||
2973 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2974 | ->will($this->returnValue($aclUsersForm)); |
||
2975 | |||
2976 | $this->adminObjectAclManipulator->expects($this->once()) |
||
2977 | ->method('createAclRolesForm') |
||
2978 | ->with($this->isInstanceOf(AdminObjectAclData::class)) |
||
2979 | ->will($this->returnValue($aclRolesForm)); |
||
2980 | |||
2981 | $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
||
2982 | ->disableOriginalConstructor() |
||
2983 | ->getMock(); |
||
2984 | |||
2985 | $aclSecurityHandler->expects($this->any()) |
||
2986 | ->method('getObjectPermissions') |
||
2987 | ->will($this->returnValue([])); |
||
2988 | |||
2989 | $this->admin->expects($this->any()) |
||
2990 | ->method('getSecurityHandler') |
||
2991 | ->will($this->returnValue($aclSecurityHandler)); |
||
2992 | |||
2993 | $this->expectTranslate('flash_acl_edit_success', [], 'SonataAdminBundle'); |
||
2994 | |||
2995 | $this->request->setMethod('POST'); |
||
2996 | |||
2997 | $response = $this->controller->aclAction(null, $this->request); |
||
2998 | |||
2999 | $this->assertInstanceOf(RedirectResponse::class, $response); |
||
3000 | |||
3001 | $this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
3002 | $this->assertSame('stdClass_acl', $response->getTargetUrl()); |
||
3003 | } |
||
3004 | |||
3005 | public function testHistoryViewRevisionActionAccessDenied(): void |
||
3006 | { |
||
3007 | $this->expectException(AccessDeniedException::class); |
||
3008 | |||
3009 | $this->admin->expects($this->any()) |
||
3010 | ->method('getObject') |
||
3011 | ->will($this->returnValue(new \StdClass())); |
||
3012 | |||
3013 | $this->admin->expects($this->once()) |
||
3014 | ->method('checkAccess') |
||
3015 | ->with($this->equalTo('historyViewRevision')) |
||
3016 | ->will($this->throwException(new AccessDeniedException())); |
||
3017 | |||
3018 | $this->controller->historyViewRevisionAction(null, null, $this->request); |
||
3019 | } |
||
3020 | |||
3021 | public function testHistoryViewRevisionActionNotFoundException(): void |
||
3022 | { |
||
3023 | $this->expectException(NotFoundHttpException::class, 'unable to find the object with id: 123'); |
||
3024 | |||
3025 | $this->request->query->set('id', 123); |
||
3026 | |||
3027 | $this->admin->expects($this->once()) |
||
3028 | ->method('getObject') |
||
3029 | ->will($this->returnValue(false)); |
||
3030 | |||
3031 | $this->controller->historyViewRevisionAction(null, null, $this->request); |
||
3032 | } |
||
3033 | |||
3034 | public function testHistoryViewRevisionActionNoReader(): void |
||
3035 | { |
||
3036 | $this->expectException(NotFoundHttpException::class, 'unable to find the audit reader for class : Foo'); |
||
3037 | |||
3038 | $this->request->query->set('id', 123); |
||
3039 | |||
3040 | $this->admin->expects($this->once()) |
||
3041 | ->method('checkAccess') |
||
3042 | ->with($this->equalTo('historyViewRevision')) |
||
3043 | ->will($this->returnValue(true)); |
||
3044 | |||
3045 | $object = new \stdClass(); |
||
3046 | |||
3047 | $this->admin->expects($this->once()) |
||
3048 | ->method('getObject') |
||
3049 | ->will($this->returnValue($object)); |
||
3050 | |||
3051 | $this->admin->expects($this->any()) |
||
3052 | ->method('getClass') |
||
3053 | ->will($this->returnValue('Foo')); |
||
3054 | |||
3055 | $this->auditManager->expects($this->once()) |
||
3056 | ->method('hasReader') |
||
3057 | ->with($this->equalTo('Foo')) |
||
3058 | ->will($this->returnValue(false)); |
||
3059 | |||
3060 | $this->controller->historyViewRevisionAction(null, null, $this->request); |
||
3061 | } |
||
3062 | |||
3063 | public function testHistoryViewRevisionActionNotFoundRevision(): void |
||
3064 | { |
||
3065 | $this->expectException(NotFoundHttpException::class, 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'); |
||
3066 | |||
3067 | $this->request->query->set('id', 123); |
||
3068 | |||
3069 | $this->admin->expects($this->once()) |
||
3070 | ->method('checkAccess') |
||
3071 | ->with($this->equalTo('historyViewRevision')) |
||
3072 | ->will($this->returnValue(true)); |
||
3073 | |||
3074 | $object = new \stdClass(); |
||
3075 | |||
3076 | $this->admin->expects($this->once()) |
||
3077 | ->method('getObject') |
||
3078 | ->will($this->returnValue($object)); |
||
3079 | |||
3080 | $this->admin->expects($this->any()) |
||
3081 | ->method('getClass') |
||
3082 | ->will($this->returnValue('Foo')); |
||
3083 | |||
3084 | $this->auditManager->expects($this->once()) |
||
3085 | ->method('hasReader') |
||
3086 | ->with($this->equalTo('Foo')) |
||
3087 | ->will($this->returnValue(true)); |
||
3088 | |||
3089 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3090 | |||
3091 | $this->auditManager->expects($this->once()) |
||
3092 | ->method('getReader') |
||
3093 | ->with($this->equalTo('Foo')) |
||
3094 | ->will($this->returnValue($reader)); |
||
3095 | |||
3096 | $reader->expects($this->once()) |
||
3097 | ->method('find') |
||
3098 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3099 | ->will($this->returnValue(null)); |
||
3100 | |||
3101 | $this->controller->historyViewRevisionAction(123, 456, $this->request); |
||
3102 | } |
||
3103 | |||
3104 | public function testHistoryViewRevisionAction(): void |
||
3105 | { |
||
3106 | $this->request->query->set('id', 123); |
||
3107 | |||
3108 | $this->admin->expects($this->once()) |
||
3109 | ->method('checkAccess') |
||
3110 | ->with($this->equalTo('historyViewRevision')) |
||
3111 | ->will($this->returnValue(true)); |
||
3112 | |||
3113 | $object = new \stdClass(); |
||
3114 | |||
3115 | $this->admin->expects($this->once()) |
||
3116 | ->method('getObject') |
||
3117 | ->will($this->returnValue($object)); |
||
3118 | |||
3119 | $this->admin->expects($this->any()) |
||
3120 | ->method('getClass') |
||
3121 | ->will($this->returnValue('Foo')); |
||
3122 | |||
3123 | $this->auditManager->expects($this->once()) |
||
3124 | ->method('hasReader') |
||
3125 | ->with($this->equalTo('Foo')) |
||
3126 | ->will($this->returnValue(true)); |
||
3127 | |||
3128 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3129 | |||
3130 | $this->auditManager->expects($this->once()) |
||
3131 | ->method('getReader') |
||
3132 | ->with($this->equalTo('Foo')) |
||
3133 | ->will($this->returnValue($reader)); |
||
3134 | |||
3135 | $objectRevision = new \stdClass(); |
||
3136 | $objectRevision->revision = 456; |
||
3137 | |||
3138 | $reader->expects($this->once()) |
||
3139 | ->method('find') |
||
3140 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3141 | ->will($this->returnValue($objectRevision)); |
||
3142 | |||
3143 | $this->admin->expects($this->once()) |
||
3144 | ->method('setSubject') |
||
3145 | ->with($this->equalTo($objectRevision)) |
||
3146 | ->will($this->returnValue(null)); |
||
3147 | |||
3148 | $fieldDescriptionCollection = new FieldDescriptionCollection(); |
||
3149 | $this->admin->expects($this->once()) |
||
3150 | ->method('getShow') |
||
3151 | ->will($this->returnValue($fieldDescriptionCollection)); |
||
3152 | |||
3153 | $this->assertInstanceOf(Response::class, $this->controller->historyViewRevisionAction(123, 456, $this->request)); |
||
3154 | |||
3155 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
3156 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
3157 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
3158 | |||
3159 | $this->assertSame('show', $this->parameters['action']); |
||
3160 | $this->assertSame($objectRevision, $this->parameters['object']); |
||
3161 | $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
||
3162 | |||
3163 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
3164 | $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template); |
||
3165 | } |
||
3166 | |||
3167 | public function testHistoryCompareRevisionsActionAccessDenied(): void |
||
3168 | { |
||
3169 | $this->expectException(AccessDeniedException::class); |
||
3170 | |||
3171 | $this->admin->expects($this->once()) |
||
3172 | ->method('checkAccess') |
||
3173 | ->with($this->equalTo('historyCompareRevisions')) |
||
3174 | ->will($this->throwException(new AccessDeniedException())); |
||
3175 | |||
3176 | $this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
||
3177 | } |
||
3178 | |||
3179 | public function testHistoryCompareRevisionsActionNotFoundException(): void |
||
3180 | { |
||
3181 | $this->expectException(NotFoundHttpException::class, 'unable to find the object with id: 123'); |
||
3182 | |||
3183 | $this->request->query->set('id', 123); |
||
3184 | |||
3185 | $this->admin->expects($this->once()) |
||
3186 | ->method('checkAccess') |
||
3187 | ->with($this->equalTo('historyCompareRevisions')) |
||
3188 | ->will($this->returnValue(true)); |
||
3189 | |||
3190 | $this->admin->expects($this->once()) |
||
3191 | ->method('getObject') |
||
3192 | ->will($this->returnValue(false)); |
||
3193 | |||
3194 | $this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
||
3195 | } |
||
3196 | |||
3197 | public function testHistoryCompareRevisionsActionNoReader(): void |
||
3198 | { |
||
3199 | $this->expectException(NotFoundHttpException::class, 'unable to find the audit reader for class : Foo'); |
||
3200 | |||
3201 | $this->request->query->set('id', 123); |
||
3202 | |||
3203 | $this->admin->expects($this->once()) |
||
3204 | ->method('checkAccess') |
||
3205 | ->with($this->equalTo('historyCompareRevisions')) |
||
3206 | ->will($this->returnValue(true)); |
||
3207 | |||
3208 | $object = new \stdClass(); |
||
3209 | |||
3210 | $this->admin->expects($this->once()) |
||
3211 | ->method('getObject') |
||
3212 | ->will($this->returnValue($object)); |
||
3213 | |||
3214 | $this->admin->expects($this->any()) |
||
3215 | ->method('getClass') |
||
3216 | ->will($this->returnValue('Foo')); |
||
3217 | |||
3218 | $this->auditManager->expects($this->once()) |
||
3219 | ->method('hasReader') |
||
3220 | ->with($this->equalTo('Foo')) |
||
3221 | ->will($this->returnValue(false)); |
||
3222 | |||
3223 | $this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
||
3224 | } |
||
3225 | |||
3226 | public function testHistoryCompareRevisionsActionNotFoundBaseRevision(): void |
||
3227 | { |
||
3228 | $this->expectException(NotFoundHttpException::class, 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'); |
||
3229 | |||
3230 | $this->request->query->set('id', 123); |
||
3231 | |||
3232 | $this->admin->expects($this->once()) |
||
3233 | ->method('checkAccess') |
||
3234 | ->with($this->equalTo('historyCompareRevisions')) |
||
3235 | ->will($this->returnValue(true)); |
||
3236 | |||
3237 | $object = new \stdClass(); |
||
3238 | |||
3239 | $this->admin->expects($this->once()) |
||
3240 | ->method('getObject') |
||
3241 | ->will($this->returnValue($object)); |
||
3242 | |||
3243 | $this->admin->expects($this->any()) |
||
3244 | ->method('getClass') |
||
3245 | ->will($this->returnValue('Foo')); |
||
3246 | |||
3247 | $this->auditManager->expects($this->once()) |
||
3248 | ->method('hasReader') |
||
3249 | ->with($this->equalTo('Foo')) |
||
3250 | ->will($this->returnValue(true)); |
||
3251 | |||
3252 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3253 | |||
3254 | $this->auditManager->expects($this->once()) |
||
3255 | ->method('getReader') |
||
3256 | ->with($this->equalTo('Foo')) |
||
3257 | ->will($this->returnValue($reader)); |
||
3258 | |||
3259 | // once because it will not be found and therefore the second call won't be executed |
||
3260 | $reader->expects($this->once()) |
||
3261 | ->method('find') |
||
3262 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3263 | ->will($this->returnValue(null)); |
||
3264 | |||
3265 | $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request); |
||
3266 | } |
||
3267 | |||
3268 | public function testHistoryCompareRevisionsActionNotFoundCompareRevision(): void |
||
3269 | { |
||
3270 | $this->expectException(NotFoundHttpException::class, 'unable to find the targeted object `123` from the revision `789` with classname : `Foo`'); |
||
3271 | |||
3272 | $this->request->query->set('id', 123); |
||
3273 | |||
3274 | $this->admin->expects($this->once()) |
||
3275 | ->method('checkAccess') |
||
3276 | ->with($this->equalTo('historyCompareRevisions')) |
||
3277 | ->will($this->returnValue(true)); |
||
3278 | |||
3279 | $object = new \stdClass(); |
||
3280 | |||
3281 | $this->admin->expects($this->once()) |
||
3282 | ->method('getObject') |
||
3283 | ->will($this->returnValue($object)); |
||
3284 | |||
3285 | $this->admin->expects($this->any()) |
||
3286 | ->method('getClass') |
||
3287 | ->will($this->returnValue('Foo')); |
||
3288 | |||
3289 | $this->auditManager->expects($this->once()) |
||
3290 | ->method('hasReader') |
||
3291 | ->with($this->equalTo('Foo')) |
||
3292 | ->will($this->returnValue(true)); |
||
3293 | |||
3294 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3295 | |||
3296 | $this->auditManager->expects($this->once()) |
||
3297 | ->method('getReader') |
||
3298 | ->with($this->equalTo('Foo')) |
||
3299 | ->will($this->returnValue($reader)); |
||
3300 | |||
3301 | $objectRevision = new \stdClass(); |
||
3302 | $objectRevision->revision = 456; |
||
3303 | |||
3304 | // first call should return, so the second call will throw an exception |
||
3305 | $reader->expects($this->at(0)) |
||
3306 | ->method('find') |
||
3307 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3308 | ->will($this->returnValue($objectRevision)); |
||
3309 | |||
3310 | $reader->expects($this->at(1)) |
||
3311 | ->method('find') |
||
3312 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
||
3313 | ->will($this->returnValue(null)); |
||
3314 | |||
3315 | $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request); |
||
3316 | } |
||
3317 | |||
3318 | public function testHistoryCompareRevisionsActionAction(): void |
||
3319 | { |
||
3320 | $this->request->query->set('id', 123); |
||
3321 | |||
3322 | $this->admin->expects($this->once()) |
||
3323 | ->method('checkAccess') |
||
3324 | ->with($this->equalTo('historyCompareRevisions')) |
||
3325 | ->will($this->returnValue(true)); |
||
3326 | |||
3327 | $object = new \stdClass(); |
||
3328 | |||
3329 | $this->admin->expects($this->once()) |
||
3330 | ->method('getObject') |
||
3331 | ->will($this->returnValue($object)); |
||
3332 | |||
3333 | $this->admin->expects($this->any()) |
||
3334 | ->method('getClass') |
||
3335 | ->will($this->returnValue('Foo')); |
||
3336 | |||
3337 | $this->auditManager->expects($this->once()) |
||
3338 | ->method('hasReader') |
||
3339 | ->with($this->equalTo('Foo')) |
||
3340 | ->will($this->returnValue(true)); |
||
3341 | |||
3342 | $reader = $this->createMock(AuditReaderInterface::class); |
||
3343 | |||
3344 | $this->auditManager->expects($this->once()) |
||
3345 | ->method('getReader') |
||
3346 | ->with($this->equalTo('Foo')) |
||
3347 | ->will($this->returnValue($reader)); |
||
3348 | |||
3349 | $objectRevision = new \stdClass(); |
||
3350 | $objectRevision->revision = 456; |
||
3351 | |||
3352 | $compareObjectRevision = new \stdClass(); |
||
3353 | $compareObjectRevision->revision = 789; |
||
3354 | |||
3355 | $reader->expects($this->at(0)) |
||
3356 | ->method('find') |
||
3357 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
||
3358 | ->will($this->returnValue($objectRevision)); |
||
3359 | |||
3360 | $reader->expects($this->at(1)) |
||
3361 | ->method('find') |
||
3362 | ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
||
3363 | ->will($this->returnValue($compareObjectRevision)); |
||
3364 | |||
3365 | $this->admin->expects($this->once()) |
||
3366 | ->method('setSubject') |
||
3367 | ->with($this->equalTo($objectRevision)) |
||
3368 | ->will($this->returnValue(null)); |
||
3369 | |||
3370 | $fieldDescriptionCollection = new FieldDescriptionCollection(); |
||
3371 | $this->admin->expects($this->once()) |
||
3372 | ->method('getShow') |
||
3373 | ->will($this->returnValue($fieldDescriptionCollection)); |
||
3374 | |||
3375 | $this->assertInstanceOf(Response::class, $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request)); |
||
3376 | |||
3377 | $this->assertSame($this->admin, $this->parameters['admin']); |
||
3378 | $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
||
3379 | $this->assertSame($this->pool, $this->parameters['admin_pool']); |
||
3380 | |||
3381 | $this->assertSame('show', $this->parameters['action']); |
||
3382 | $this->assertSame($objectRevision, $this->parameters['object']); |
||
3383 | $this->assertSame($compareObjectRevision, $this->parameters['object_compare']); |
||
3384 | $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
||
3385 | |||
3386 | $this->assertSame([], $this->session->getFlashBag()->all()); |
||
3387 | $this->assertSame('@SonataAdmin/CRUD/show_compare.html.twig', $this->template); |
||
3388 | } |
||
3389 | |||
3390 | public function testBatchActionWrongMethod(): void |
||
3391 | { |
||
3392 | $this->expectException(NotFoundHttpException::class, 'Invalid request type "GET", POST expected'); |
||
3393 | |||
3394 | $this->controller->batchAction($this->request); |
||
3395 | } |
||
3396 | |||
3397 | public function testBatchActionActionNotDefined(): void |
||
3398 | { |
||
3399 | $this->expectException(\RuntimeException::class, 'The `foo` batch action is not defined'); |
||
3400 | |||
3401 | $batchActions = []; |
||
3402 | |||
3403 | $this->admin->expects($this->once()) |
||
3404 | ->method('getBatchActions') |
||
3405 | ->will($this->returnValue($batchActions)); |
||
3406 | |||
3407 | $this->request->setMethod('POST'); |
||
3408 | $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
||
3409 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3410 | |||
3411 | $this->controller->batchAction($this->request); |
||
3412 | } |
||
3413 | |||
3414 | public function testBatchActionActionInvalidCsrfToken(): void |
||
3415 | { |
||
3416 | $this->request->setMethod('POST'); |
||
3417 | $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
||
3418 | $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
||
3419 | |||
3420 | try { |
||
3421 | $this->controller->batchAction($this->request); |
||
3422 | } catch (HttpException $e) { |
||
3423 | $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
||
3424 | $this->assertSame(400, $e->getStatusCode()); |
||
3425 | } |
||
3426 | } |
||
3427 | |||
3428 | public function testBatchActionMethodNotExist(): void |
||
3429 | { |
||
3430 | $this->expectException(\RuntimeException::class, 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable'); |
||
3431 | |||
3432 | $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3433 | |||
3434 | $this->admin->expects($this->once()) |
||
3435 | ->method('getBatchActions') |
||
3436 | ->will($this->returnValue($batchActions)); |
||
3437 | |||
3438 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3439 | $this->admin->expects($this->once()) |
||
3440 | ->method('getDatagrid') |
||
3441 | ->will($this->returnValue($datagrid)); |
||
3442 | |||
3443 | $this->request->setMethod('POST'); |
||
3444 | $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
||
3445 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3446 | |||
3447 | $this->controller->batchAction($this->request); |
||
3448 | } |
||
3449 | |||
3450 | public function testBatchActionWithoutConfirmation(): void |
||
3501 | |||
3502 | public function testBatchActionWithoutConfirmation2(): void |
||
3503 | { |
||
3504 | $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
||
3505 | |||
3506 | $this->admin->expects($this->once()) |
||
3507 | ->method('getBatchActions') |
||
3508 | ->will($this->returnValue($batchActions)); |
||
3509 | |||
3510 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3511 | |||
3512 | $query = $this->createMock(ProxyQueryInterface::class); |
||
3513 | $datagrid->expects($this->once()) |
||
3514 | ->method('getQuery') |
||
3515 | ->will($this->returnValue($query)); |
||
3516 | |||
3517 | $this->admin->expects($this->once()) |
||
3518 | ->method('getDatagrid') |
||
3519 | ->will($this->returnValue($datagrid)); |
||
3520 | |||
3521 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
3522 | |||
3523 | $this->admin->expects($this->once()) |
||
3524 | ->method('checkAccess') |
||
3525 | ->with($this->equalTo('batchDelete')) |
||
3526 | ->will($this->returnValue(true)); |
||
3527 | |||
3528 | $this->admin->expects($this->any()) |
||
3529 | ->method('getModelManager') |
||
3530 | ->will($this->returnValue($modelManager)); |
||
3531 | |||
3532 | $this->admin->expects($this->any()) |
||
3533 | ->method('getClass') |
||
3534 | ->will($this->returnValue('Foo')); |
||
3535 | |||
3536 | $modelManager->expects($this->once()) |
||
3537 | ->method('addIdentifiersToQuery') |
||
3538 | ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
||
3539 | ->will($this->returnValue(true)); |
||
3540 | |||
3541 | $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
||
3542 | |||
3543 | $this->request->setMethod('POST'); |
||
3544 | $this->request->request->set('action', 'delete'); |
||
3545 | $this->request->request->set('idx', ['123', '456']); |
||
3546 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3547 | |||
3548 | $result = $this->controller->batchAction($this->request); |
||
3549 | |||
3550 | $this->assertInstanceOf(RedirectResponse::class, $result); |
||
3551 | $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
||
3552 | $this->assertSame('list', $result->getTargetUrl()); |
||
3553 | } |
||
3554 | |||
3555 | public function testBatchActionWithConfirmation(): void |
||
3556 | { |
||
3557 | $batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]]; |
||
3558 | |||
3559 | $this->admin->expects($this->once()) |
||
3560 | ->method('getBatchActions') |
||
3561 | ->will($this->returnValue($batchActions)); |
||
3562 | |||
3563 | $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]; |
||
3564 | |||
3565 | $this->request->setMethod('POST'); |
||
3566 | $this->request->request->set('data', json_encode($data)); |
||
3567 | $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
||
3568 | |||
3569 | $datagrid = $this->createMock(DatagridInterface::class); |
||
3570 | |||
3571 | $this->admin->expects($this->once()) |
||
3572 | ->method('getDatagrid') |
||
3573 | ->will($this->returnValue($datagrid)); |
||
3574 | |||
3603 | |||
3604 | public function testBatchActionNonRelevantAction(): void |
||
3634 | |||
3635 | public function testBatchActionNonRelevantAction2(): void |
||
3665 | |||
3666 | public function testBatchActionNoItems(): void |
||
3693 | |||
3694 | public function testBatchActionNoItemsEmptyQuery(): void |
||
3737 | |||
3738 | public function testBatchActionWithRequesData(): void |
||
3791 | |||
3792 | public function testItThrowsWhenCallingAnUndefinedMethod(): void |
||
3802 | |||
3803 | /** |
||
3804 | * @expectedDeprecation Method Sonata\AdminBundle\Controller\CRUDController::render has been renamed to Sonata\AdminBundle\Controller\CRUDController::renderWithExtraParams. |
||
3805 | */ |
||
3806 | public function testRenderIsDeprecated(): void |
||
3810 | |||
3811 | public function getCsrfProvider() |
||
3815 | |||
3816 | public function getToStringValues() |
||
3825 | |||
3826 | private function assertLoggerLogsModelManagerException($subject, $method): void |
||
3847 | |||
3848 | private function expectTranslate($id, array $parameters = [], $domain = null, $locale = null): void |
||
3855 | } |
||
3856 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.