Complex classes like AdminTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
76 | class AdminTest extends TestCase |
||
77 | { |
||
78 | protected $cacheTempFolder; |
||
79 | |||
80 | public function setUp(): void |
||
81 | { |
||
82 | $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route'; |
||
83 | |||
84 | exec('rm -rf '.$this->cacheTempFolder); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct |
||
89 | */ |
||
90 | public function testConstructor(): void |
||
91 | { |
||
92 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
93 | $baseControllerName = 'SonataNewsBundle:PostAdmin'; |
||
94 | |||
95 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
96 | $this->assertInstanceOf(AbstractAdmin::class, $admin); |
||
97 | $this->assertSame($class, $admin->getClass()); |
||
98 | $this->assertSame($baseControllerName, $admin->getBaseControllerName()); |
||
99 | } |
||
100 | |||
101 | public function testGetClass(): void |
||
102 | { |
||
103 | $class = Post::class; |
||
104 | $baseControllerName = 'SonataNewsBundle:PostAdmin'; |
||
105 | |||
106 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
107 | |||
108 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
109 | |||
110 | $admin->setSubject(new BlogPost()); |
||
111 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
112 | |||
113 | $admin->setSubClasses(['foo']); |
||
114 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
115 | |||
116 | $admin->setSubject(null); |
||
117 | $admin->setSubClasses([]); |
||
118 | $this->assertSame($class, $admin->getClass()); |
||
119 | |||
120 | $admin->setSubClasses(['foo' => 'bar']); |
||
121 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
122 | $this->assertSame('bar', $admin->getClass()); |
||
123 | } |
||
124 | |||
125 | public function testGetClassException(): void |
||
126 | { |
||
127 | $this->expectException(\RuntimeException::class); |
||
128 | $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass'); |
||
129 | |||
130 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
131 | $baseControllerName = 'SonataNewsBundle:PostAdmin'; |
||
132 | |||
133 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
134 | $admin->setParentFieldDescription(new FieldDescription()); |
||
135 | $admin->setSubClasses(['foo' => 'bar']); |
||
136 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
137 | $admin->getClass(); |
||
138 | } |
||
139 | |||
140 | public function testCheckAccessThrowsExceptionOnMadeUpAction(): void |
||
141 | { |
||
142 | $admin = new PostAdmin( |
||
143 | 'sonata.post.admin.post', |
||
144 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
145 | 'SonataNewsBundle:PostAdmin' |
||
146 | ); |
||
147 | $this->expectException( |
||
148 | \InvalidArgumentException::class |
||
149 | ); |
||
150 | $this->expectExceptionMessage( |
||
151 | 'Action "made-up" could not be found' |
||
152 | ); |
||
153 | $admin->checkAccess('made-up'); |
||
154 | } |
||
155 | |||
156 | public function testCheckAccessThrowsAccessDeniedException(): void |
||
157 | { |
||
158 | $admin = new PostAdmin( |
||
159 | 'sonata.post.admin.post', |
||
160 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
161 | 'SonataNewsBundle:PostAdmin' |
||
162 | ); |
||
163 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
164 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
165 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
166 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
167 | $customExtension->getAccessMapping($admin)->willReturn( |
||
168 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
169 | ); |
||
170 | $admin->addExtension($customExtension->reveal()); |
||
171 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
172 | $this->expectException( |
||
173 | AccessDeniedException::class |
||
174 | ); |
||
175 | $this->expectExceptionMessage( |
||
176 | 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE' |
||
177 | ); |
||
178 | $admin->checkAccess('custom_action'); |
||
179 | } |
||
180 | |||
181 | public function testHasAccessOnMadeUpAction(): void |
||
182 | { |
||
183 | $admin = new PostAdmin( |
||
184 | 'sonata.post.admin.post', |
||
185 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
186 | 'SonataNewsBundle:PostAdmin' |
||
187 | ); |
||
188 | |||
189 | $this->assertFalse($admin->hasAccess('made-up')); |
||
190 | } |
||
191 | |||
192 | public function testHasAccess(): void |
||
193 | { |
||
194 | $admin = new PostAdmin( |
||
195 | 'sonata.post.admin.post', |
||
196 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
197 | 'SonataNewsBundle:PostAdmin' |
||
198 | ); |
||
199 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
200 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
201 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
202 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
203 | $customExtension->getAccessMapping($admin)->willReturn( |
||
204 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
205 | ); |
||
206 | $admin->addExtension($customExtension->reveal()); |
||
207 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
208 | |||
209 | $this->assertFalse($admin->hasAccess('custom_action')); |
||
210 | } |
||
211 | |||
212 | public function testHasAccessAllowsAccess(): void |
||
213 | { |
||
214 | $admin = new PostAdmin( |
||
215 | 'sonata.post.admin.post', |
||
216 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
217 | 'SonataNewsBundle:PostAdmin' |
||
218 | ); |
||
219 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
220 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
221 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true); |
||
222 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
223 | $customExtension->getAccessMapping($admin)->willReturn( |
||
224 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
225 | ); |
||
226 | $admin->addExtension($customExtension->reveal()); |
||
227 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
228 | |||
229 | $this->assertTrue($admin->hasAccess('custom_action')); |
||
230 | } |
||
231 | |||
232 | public function testHasAccessAllowsAccessEditAction(): void |
||
233 | { |
||
234 | $admin = new PostAdmin( |
||
235 | 'sonata.post.admin.post', |
||
236 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
237 | 'SonataNewsBundle:PostAdmin' |
||
238 | ); |
||
239 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
240 | $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true); |
||
241 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
242 | $customExtension->getAccessMapping($admin)->willReturn( |
||
243 | ['edit_action' => ['EDIT_ROLE']] |
||
244 | ); |
||
245 | $admin->addExtension($customExtension->reveal()); |
||
246 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
247 | |||
248 | $this->assertTrue($admin->hasAccess('edit_action')); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild |
||
253 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild |
||
254 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild |
||
255 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild |
||
256 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren |
||
257 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren |
||
258 | */ |
||
259 | public function testChildren(): void |
||
260 | { |
||
261 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
262 | $this->assertFalse($postAdmin->hasChildren()); |
||
263 | $this->assertFalse($postAdmin->hasChild('comment')); |
||
264 | |||
265 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
266 | $postAdmin->addChild($commentAdmin, 'post'); |
||
267 | |||
268 | $this->assertTrue($postAdmin->hasChildren()); |
||
269 | $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment')); |
||
270 | |||
271 | $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode()); |
||
272 | $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute()); |
||
273 | $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent()); |
||
274 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
275 | |||
276 | $this->assertFalse($postAdmin->isChild()); |
||
277 | $this->assertTrue($commentAdmin->isChild()); |
||
278 | |||
279 | $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren()); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure |
||
284 | */ |
||
285 | public function testConfigure(): void |
||
286 | { |
||
287 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
288 | $this->assertNotNull($admin->getUniqid()); |
||
289 | |||
290 | $admin->initialize(); |
||
291 | $this->assertNotNull($admin->getUniqid()); |
||
292 | $this->assertSame('Post', $admin->getClassnameLabel()); |
||
293 | |||
294 | $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
295 | $admin->setClassnameLabel('postcomment'); |
||
296 | |||
297 | $admin->initialize(); |
||
298 | $this->assertSame('postcomment', $admin->getClassnameLabel()); |
||
299 | } |
||
300 | |||
301 | public function testConfigureWithValidParentAssociationMapping(): void |
||
302 | { |
||
303 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
304 | $admin->setParentAssociationMapping('Category'); |
||
305 | |||
306 | $admin->initialize(); |
||
307 | $this->assertSame('Category', $admin->getParentAssociationMapping()); |
||
308 | } |
||
309 | |||
310 | public function provideGetBaseRoutePattern() |
||
311 | { |
||
312 | return [ |
||
313 | [ |
||
314 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
315 | '/sonata/news/post', |
||
316 | ], |
||
317 | [ |
||
318 | 'Application\Sonata\NewsBundle\Document\Post', |
||
319 | '/sonata/news/post', |
||
320 | ], |
||
321 | [ |
||
322 | 'MyApplication\MyBundle\Entity\Post', |
||
323 | '/myapplication/my/post', |
||
324 | ], |
||
325 | [ |
||
326 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
327 | '/myapplication/my/post-category', |
||
328 | ], |
||
329 | [ |
||
330 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
331 | '/myapplication/my/product-category', |
||
332 | ], |
||
333 | [ |
||
334 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
335 | '/myapplication/my/other-product-category', |
||
336 | ], |
||
337 | [ |
||
338 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
339 | '/cmf/foo/menu', |
||
340 | ], |
||
341 | [ |
||
342 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
343 | '/cmf/foo/menu', |
||
344 | ], |
||
345 | [ |
||
346 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
347 | '/symfony/barbar/menu', |
||
348 | ], |
||
349 | [ |
||
350 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
351 | '/symfony/barbar/menu-item', |
||
352 | ], |
||
353 | [ |
||
354 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
355 | '/cmf/foo/menu', |
||
356 | ], |
||
357 | [ |
||
358 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
359 | '/cmf/foo/menu', |
||
360 | ], |
||
361 | [ |
||
362 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
363 | '/cmf/foo/menu', |
||
364 | ], |
||
365 | [ |
||
366 | 'AppBundle\Entity\User', |
||
367 | '/app/user', |
||
368 | ], |
||
369 | [ |
||
370 | 'App\Entity\User', |
||
371 | '/app/user', |
||
372 | ], |
||
373 | ]; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @dataProvider provideGetBaseRoutePattern |
||
378 | */ |
||
379 | public function testGetBaseRoutePattern(string $objFqn, string $expected): void |
||
380 | { |
||
381 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
382 | $this->assertSame($expected, $admin->getBaseRoutePattern()); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @dataProvider provideGetBaseRoutePattern |
||
387 | */ |
||
388 | public function testGetBaseRoutePatternWithChildAdmin(string $objFqn, string $expected): void |
||
389 | { |
||
390 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
391 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
392 | $commentAdmin->setParent($postAdmin); |
||
393 | |||
394 | $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern()); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @dataProvider provideGetBaseRoutePattern |
||
399 | */ |
||
400 | public function testGetBaseRoutePatternWithTwoNestedChildAdmin(string $objFqn, string $expected): void |
||
401 | { |
||
402 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
403 | $commentAdmin = new CommentAdmin( |
||
404 | 'sonata.post.admin.comment', |
||
405 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
406 | 'SonataNewsBundle:CommentAdmin' |
||
407 | ); |
||
408 | $commentVoteAdmin = new CommentVoteAdmin( |
||
409 | 'sonata.post.admin.comment_vote', |
||
410 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
411 | 'SonataNewsBundle:CommentVoteAdmin' |
||
412 | ); |
||
413 | $commentAdmin->setParent($postAdmin); |
||
414 | $commentVoteAdmin->setParent($commentAdmin); |
||
415 | |||
416 | $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern()); |
||
417 | } |
||
418 | |||
419 | public function testGetBaseRoutePatternWithSpecifedPattern(): void |
||
420 | { |
||
421 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin'); |
||
422 | |||
423 | $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern()); |
||
424 | } |
||
425 | |||
426 | public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void |
||
427 | { |
||
428 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
429 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin'); |
||
430 | $commentAdmin->setParent($postAdmin); |
||
431 | |||
432 | $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern()); |
||
433 | } |
||
434 | |||
435 | public function testGetBaseRoutePatternWithUnreconizedClassname(): void |
||
436 | { |
||
437 | $this->expectException(\RuntimeException::class); |
||
438 | |||
439 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin'); |
||
440 | $admin->getBaseRoutePattern(); |
||
441 | } |
||
442 | |||
443 | public function provideGetBaseRouteName() |
||
444 | { |
||
445 | return [ |
||
446 | [ |
||
447 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
448 | 'admin_sonata_news_post', |
||
449 | ], |
||
450 | [ |
||
451 | 'Application\Sonata\NewsBundle\Document\Post', |
||
452 | 'admin_sonata_news_post', |
||
453 | ], |
||
454 | [ |
||
455 | 'MyApplication\MyBundle\Entity\Post', |
||
456 | 'admin_myapplication_my_post', |
||
457 | ], |
||
458 | [ |
||
459 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
460 | 'admin_myapplication_my_post_category', |
||
461 | ], |
||
462 | [ |
||
463 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
464 | 'admin_myapplication_my_product_category', |
||
465 | ], |
||
466 | [ |
||
467 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
468 | 'admin_myapplication_my_other_product_category', |
||
469 | ], |
||
470 | [ |
||
471 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
472 | 'admin_cmf_foo_menu', |
||
473 | ], |
||
474 | [ |
||
475 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
476 | 'admin_cmf_foo_menu', |
||
477 | ], |
||
478 | [ |
||
479 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
480 | 'admin_symfony_barbar_menu', |
||
481 | ], |
||
482 | [ |
||
483 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
484 | 'admin_symfony_barbar_menu_item', |
||
485 | ], |
||
486 | [ |
||
487 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
488 | 'admin_cmf_foo_menu', |
||
489 | ], |
||
490 | [ |
||
491 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
492 | 'admin_cmf_foo_menu', |
||
493 | ], |
||
494 | [ |
||
495 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
496 | 'admin_cmf_foo_menu', |
||
497 | ], |
||
498 | [ |
||
499 | 'AppBundle\Entity\User', |
||
500 | 'admin_app_user', |
||
501 | ], |
||
502 | [ |
||
503 | 'App\Entity\User', |
||
504 | 'admin_app_user', |
||
505 | ], |
||
506 | ]; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * @dataProvider provideGetBaseRouteName |
||
511 | */ |
||
512 | public function testGetBaseRouteName(string $objFqn, string $expected): void |
||
513 | { |
||
514 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
515 | |||
516 | $this->assertSame($expected, $admin->getBaseRouteName()); |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @group legacy |
||
521 | * @expectedDeprecation Calling "addChild" without second argument is deprecated since sonata-project/admin-bundle 3.35 and will not be allowed in 4.0. |
||
522 | * @dataProvider provideGetBaseRouteName |
||
523 | */ |
||
524 | public function testGetBaseRouteNameWithChildAdmin(string $objFqn, string $expected): void |
||
525 | { |
||
526 | $routeGenerator = new DefaultRouteGenerator( |
||
527 | $this->createMock(RouterInterface::class), |
||
528 | new RoutesCache($this->cacheTempFolder, true) |
||
529 | ); |
||
530 | |||
531 | $container = new Container(); |
||
532 | $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
||
533 | |||
534 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
535 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
536 | $container->set('sonata.post.admin.post', $postAdmin); |
||
537 | $postAdmin->setConfigurationPool($pool); |
||
538 | $postAdmin->setRouteBuilder($pathInfo); |
||
539 | $postAdmin->setRouteGenerator($routeGenerator); |
||
540 | $postAdmin->initialize(); |
||
541 | |||
542 | $commentAdmin = new CommentAdmin( |
||
543 | 'sonata.post.admin.comment', |
||
544 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
545 | 'SonataNewsBundle:CommentAdmin' |
||
546 | ); |
||
547 | $container->set('sonata.post.admin.comment', $commentAdmin); |
||
548 | $commentAdmin->setConfigurationPool($pool); |
||
549 | $commentAdmin->setRouteBuilder($pathInfo); |
||
550 | $commentAdmin->setRouteGenerator($routeGenerator); |
||
551 | $commentAdmin->initialize(); |
||
552 | |||
553 | $postAdmin->addChild($commentAdmin, 'post'); |
||
554 | |||
555 | $commentVoteAdmin = new CommentVoteAdmin( |
||
556 | 'sonata.post.admin.comment_vote', |
||
557 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
558 | 'SonataNewsBundle:CommentVoteAdmin' |
||
559 | ); |
||
560 | |||
561 | $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin); |
||
562 | $commentVoteAdmin->setConfigurationPool($pool); |
||
563 | $commentVoteAdmin->setRouteBuilder($pathInfo); |
||
564 | $commentVoteAdmin->setRouteGenerator($routeGenerator); |
||
565 | $commentVoteAdmin->initialize(); |
||
566 | |||
567 | $commentAdmin->addChild($commentVoteAdmin); |
||
568 | $pool->setAdminServiceIds([ |
||
569 | 'sonata.post.admin.post', |
||
570 | 'sonata.post.admin.comment', |
||
571 | 'sonata.post.admin.comment_vote', |
||
572 | ]); |
||
573 | |||
574 | $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName()); |
||
575 | |||
576 | $this->assertTrue($postAdmin->hasRoute('show')); |
||
577 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show')); |
||
578 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show')); |
||
579 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show')); |
||
580 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list')); |
||
581 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list')); |
||
582 | $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit')); |
||
583 | $this->assertFalse($commentAdmin->hasRoute('edit')); |
||
584 | $this->assertSame('post', $commentAdmin->getParentAssociationMapping()); |
||
585 | |||
586 | /* |
||
587 | * Test the route name from request |
||
588 | */ |
||
589 | $postListRequest = new Request( |
||
590 | [], |
||
591 | [], |
||
592 | [ |
||
593 | '_route' => $postAdmin->getBaseRouteName().'_list', |
||
594 | ] |
||
595 | ); |
||
596 | |||
597 | $postAdmin->setRequest($postListRequest); |
||
598 | $commentAdmin->setRequest($postListRequest); |
||
599 | |||
600 | $this->assertTrue($postAdmin->isCurrentRoute('list')); |
||
601 | $this->assertFalse($postAdmin->isCurrentRoute('create')); |
||
602 | $this->assertFalse($commentAdmin->isCurrentRoute('list')); |
||
603 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('list')); |
||
604 | $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
605 | $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
606 | $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
607 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
608 | } |
||
609 | |||
610 | public function testGetBaseRouteNameWithUnreconizedClassname(): void |
||
611 | { |
||
612 | $this->expectException(\RuntimeException::class); |
||
613 | |||
614 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin'); |
||
615 | $admin->getBaseRouteName(); |
||
616 | } |
||
617 | |||
618 | public function testGetBaseRouteNameWithSpecifiedName(): void |
||
619 | { |
||
620 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
621 | |||
622 | $this->assertSame('post_custom', $postAdmin->getBaseRouteName()); |
||
623 | } |
||
624 | |||
625 | public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void |
||
626 | { |
||
627 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
628 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin'); |
||
629 | $commentAdmin->setParent($postAdmin); |
||
630 | |||
631 | $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName()); |
||
632 | } |
||
633 | |||
634 | public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void |
||
635 | { |
||
636 | $postAdmin = new PostAdmin( |
||
637 | 'sonata.post.admin.post', |
||
638 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
639 | 'SonataNewsBundle:PostAdmin' |
||
640 | ); |
||
641 | $commentAdmin = new CommentWithCustomRouteAdmin( |
||
642 | 'sonata.post.admin.comment_with_custom_route', |
||
643 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
644 | 'SonataNewsBundle:CommentWithCustomRouteAdmin' |
||
645 | ); |
||
646 | $commentVoteAdmin = new CommentVoteAdmin( |
||
647 | 'sonata.post.admin.comment_vote', |
||
648 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
649 | 'SonataNewsBundle:CommentVoteAdmin' |
||
650 | ); |
||
651 | $commentAdmin->setParent($postAdmin); |
||
652 | $commentVoteAdmin->setParent($commentAdmin); |
||
653 | |||
654 | $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName()); |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid |
||
659 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid |
||
660 | */ |
||
661 | public function testSetUniqid(): void |
||
662 | { |
||
663 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
664 | |||
665 | $uniqid = uniqid(); |
||
666 | $admin->setUniqid($uniqid); |
||
667 | |||
668 | $this->assertSame($uniqid, $admin->getUniqid()); |
||
669 | } |
||
670 | |||
671 | public function testToString(): void |
||
672 | { |
||
673 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
674 | |||
675 | $s = new \stdClass(); |
||
676 | |||
677 | $this->assertNotEmpty($admin->toString($s)); |
||
678 | |||
679 | $s = new FooToString(); |
||
680 | $this->assertSame('salut', $admin->toString($s)); |
||
681 | |||
682 | // To string method is implemented, but returns null |
||
683 | $s = new FooToStringNull(); |
||
684 | $this->assertNotEmpty($admin->toString($s)); |
||
685 | |||
686 | $this->assertSame('', $admin->toString(false)); |
||
|
|||
687 | } |
||
688 | |||
689 | public function testIsAclEnabled(): void |
||
690 | { |
||
691 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
692 | |||
693 | $this->assertFalse($postAdmin->isAclEnabled()); |
||
694 | |||
695 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
696 | $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class)); |
||
697 | $this->assertTrue($commentAdmin->isAclEnabled()); |
||
698 | } |
||
699 | |||
700 | /** |
||
701 | * @group legacy |
||
702 | * |
||
703 | * @expectedDeprecation Calling Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. Use Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass() to know if there is an active subclass. |
||
704 | * |
||
705 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses |
||
706 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass |
||
707 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses |
||
708 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass |
||
709 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
710 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass |
||
711 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode |
||
712 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass |
||
713 | */ |
||
714 | public function testSubClass(): void |
||
715 | { |
||
716 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
717 | $admin = new PostAdmin( |
||
718 | 'sonata.post.admin.post', |
||
719 | Post::class, |
||
720 | 'SonataNewsBundle:PostAdmin' |
||
721 | ); |
||
722 | $this->assertFalse($admin->hasSubClass('test')); |
||
723 | $this->assertFalse($admin->hasActiveSubClass()); |
||
724 | $this->assertCount(0, $admin->getSubClasses()); |
||
725 | $this->assertNull($admin->getActiveSubClass()); |
||
726 | $this->assertNull($admin->getActiveSubclassCode()); |
||
727 | $this->assertSame(Post::class, $admin->getClass()); |
||
728 | |||
729 | // Just for the record, if there is no inheritance set, the getSubject is not used |
||
730 | // the getSubject can also lead to some issue |
||
731 | $admin->setSubject(new BlogPost()); |
||
732 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
733 | |||
734 | $admin->setSubClasses([ |
||
735 | 'extended1' => 'NewsBundle\Entity\PostExtended1', |
||
736 | 'extended2' => 'NewsBundle\Entity\PostExtended2', |
||
737 | ]); |
||
738 | $this->assertFalse($admin->hasSubClass('test')); |
||
739 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
740 | $this->assertFalse($admin->hasActiveSubClass()); |
||
741 | $this->assertCount(2, $admin->getSubClasses()); |
||
742 | // NEXT_MAJOR: remove the following 2 `assertNull()` assertions |
||
743 | $this->assertNull($admin->getActiveSubClass()); |
||
744 | $this->assertNull($admin->getActiveSubclassCode()); |
||
745 | $this->assertSame( |
||
746 | BlogPost::class, |
||
747 | $admin->getClass(), |
||
748 | 'When there is no subclass in the query the class parameter should be returned' |
||
749 | ); |
||
750 | |||
751 | $request = new Request(['subclass' => 'extended1']); |
||
752 | $admin->setRequest($request); |
||
753 | $this->assertFalse($admin->hasSubClass('test')); |
||
754 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
755 | $this->assertTrue($admin->hasActiveSubClass()); |
||
756 | $this->assertCount(2, $admin->getSubClasses()); |
||
757 | $this->assertSame( |
||
758 | 'NewsBundle\Entity\PostExtended1', |
||
759 | $admin->getActiveSubClass(), |
||
760 | 'It should return the curently active sub class.' |
||
761 | ); |
||
762 | $this->assertSame('extended1', $admin->getActiveSubclassCode()); |
||
763 | $this->assertSame( |
||
764 | 'NewsBundle\Entity\PostExtended1', |
||
765 | $admin->getClass(), |
||
766 | 'getClass() should return the name of the sub class when passed through a request query parameter.' |
||
767 | ); |
||
768 | |||
769 | $request->query->set('subclass', 'inject'); |
||
770 | |||
771 | $this->assertNull($admin->getActiveSubclassCode()); |
||
772 | // NEXT_MAJOR: remove the previous `assertNull()` assertion and uncomment the following lines |
||
773 | // $this->expectException(\LogicException::class); |
||
774 | // $this->expectExceptionMessage(sprintf('Admin "%s" has no active subclass.', PostAdmin::class)); |
||
775 | } |
||
776 | |||
777 | /** |
||
778 | * @group legacy |
||
779 | * |
||
780 | * @expectedDeprecation Calling Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. Use Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass() to know if there is an active subclass. |
||
781 | */ |
||
782 | public function testNonExistantSubclass(): void |
||
783 | { |
||
784 | // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations |
||
785 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
786 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
787 | |||
788 | $admin->setRequest(new Request(['subclass' => 'inject'])); |
||
789 | |||
790 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
791 | |||
792 | $this->assertTrue($admin->hasActiveSubClass()); |
||
793 | |||
794 | $this->expectException(\RuntimeException::class); |
||
795 | |||
796 | $admin->getActiveSubClass(); |
||
797 | } |
||
798 | |||
799 | /** |
||
800 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
801 | */ |
||
802 | public function testOnlyOneSubclassNeededToBeActive(): void |
||
803 | { |
||
804 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
805 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']); |
||
806 | $request = new Request(['subclass' => 'extended1']); |
||
807 | $admin->setRequest($request); |
||
808 | $this->assertTrue($admin->hasActiveSubClass()); |
||
809 | } |
||
810 | |||
811 | /** |
||
812 | * @group legacy |
||
813 | * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0. |
||
814 | */ |
||
815 | public function testAddSubClassIsDeprecated(): void |
||
816 | { |
||
817 | $admin = new PostAdmin( |
||
818 | 'sonata.post.admin.post', |
||
819 | Post::class, |
||
820 | 'SonataNewsBundle:PostAdmin' |
||
821 | ); |
||
822 | $admin->addSubClass('whatever'); |
||
823 | } |
||
824 | |||
825 | public function testGetPerPageOptions(): void |
||
826 | { |
||
827 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
828 | |||
829 | $perPageOptions = $admin->getPerPageOptions(); |
||
830 | |||
831 | foreach ($perPageOptions as $perPage) { |
||
832 | $this->assertSame(0, $perPage % 4); |
||
833 | } |
||
834 | |||
835 | $admin->setPerPageOptions([500, 1000]); |
||
836 | $this->assertSame([500, 1000], $admin->getPerPageOptions()); |
||
837 | } |
||
838 | |||
839 | public function testGetLabelTranslatorStrategy(): void |
||
840 | { |
||
841 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
842 | |||
843 | $this->assertNull($admin->getLabelTranslatorStrategy()); |
||
844 | |||
845 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
846 | $admin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
847 | $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy()); |
||
848 | } |
||
849 | |||
850 | public function testGetRouteBuilder(): void |
||
851 | { |
||
852 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
853 | |||
854 | $this->assertNull($admin->getRouteBuilder()); |
||
855 | |||
856 | $routeBuilder = $this->createMock(RouteBuilderInterface::class); |
||
857 | $admin->setRouteBuilder($routeBuilder); |
||
858 | $this->assertSame($routeBuilder, $admin->getRouteBuilder()); |
||
859 | } |
||
860 | |||
861 | public function testGetMenuFactory(): void |
||
862 | { |
||
863 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
864 | |||
865 | $this->assertNull($admin->getMenuFactory()); |
||
866 | |||
867 | $menuFactory = $this->createMock(FactoryInterface::class); |
||
868 | $admin->setMenuFactory($menuFactory); |
||
869 | $this->assertSame($menuFactory, $admin->getMenuFactory()); |
||
870 | } |
||
871 | |||
872 | public function testGetExtensions(): void |
||
873 | { |
||
874 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
875 | |||
876 | $this->assertSame([], $admin->getExtensions()); |
||
877 | |||
878 | $adminExtension1 = $this->createMock(AdminExtensionInterface::class); |
||
879 | $adminExtension2 = $this->createMock(AdminExtensionInterface::class); |
||
880 | |||
881 | $admin->addExtension($adminExtension1); |
||
882 | $admin->addExtension($adminExtension2); |
||
883 | $this->assertSame([$adminExtension1, $adminExtension2], $admin->getExtensions()); |
||
884 | } |
||
885 | |||
886 | public function testGetFilterTheme(): void |
||
887 | { |
||
888 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
889 | |||
890 | $this->assertSame([], $admin->getFilterTheme()); |
||
891 | |||
892 | $admin->setFilterTheme(['FooTheme']); |
||
893 | $this->assertSame(['FooTheme'], $admin->getFilterTheme()); |
||
894 | } |
||
895 | |||
896 | public function testGetFormTheme(): void |
||
897 | { |
||
898 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
899 | |||
900 | $this->assertSame([], $admin->getFormTheme()); |
||
901 | |||
902 | $admin->setFormTheme(['FooTheme']); |
||
903 | |||
904 | $this->assertSame(['FooTheme'], $admin->getFormTheme()); |
||
905 | } |
||
906 | |||
907 | public function testGetValidator(): void |
||
908 | { |
||
909 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
910 | |||
911 | $this->assertNull($admin->getValidator()); |
||
912 | |||
913 | $validator = $this->getMockForAbstractClass(ValidatorInterface::class); |
||
914 | |||
915 | $admin->setValidator($validator); |
||
916 | $this->assertSame($validator, $admin->getValidator()); |
||
917 | } |
||
918 | |||
919 | public function testGetSecurityHandler(): void |
||
920 | { |
||
921 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
922 | |||
923 | $this->assertNull($admin->getSecurityHandler()); |
||
924 | |||
925 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
926 | $admin->setSecurityHandler($securityHandler); |
||
927 | $this->assertSame($securityHandler, $admin->getSecurityHandler()); |
||
928 | } |
||
929 | |||
930 | public function testGetSecurityInformation(): void |
||
931 | { |
||
932 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
933 | |||
934 | $this->assertSame([], $admin->getSecurityInformation()); |
||
935 | |||
936 | $securityInformation = [ |
||
937 | 'GUEST' => ['VIEW', 'LIST'], |
||
938 | 'STAFF' => ['EDIT', 'LIST', 'CREATE'], |
||
939 | ]; |
||
940 | |||
941 | $admin->setSecurityInformation($securityInformation); |
||
942 | $this->assertSame($securityInformation, $admin->getSecurityInformation()); |
||
943 | } |
||
944 | |||
945 | public function testGetManagerType(): void |
||
946 | { |
||
947 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
948 | |||
949 | $this->assertNull($admin->getManagerType()); |
||
950 | |||
951 | $admin->setManagerType('foo_orm'); |
||
952 | $this->assertSame('foo_orm', $admin->getManagerType()); |
||
953 | } |
||
954 | |||
955 | public function testGetModelManager(): void |
||
956 | { |
||
957 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
958 | |||
959 | $this->assertNull($admin->getModelManager()); |
||
960 | |||
961 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
962 | |||
963 | $admin->setModelManager($modelManager); |
||
964 | $this->assertSame($modelManager, $admin->getModelManager()); |
||
965 | } |
||
966 | |||
967 | /** |
||
968 | * NEXT_MAJOR: remove this method. |
||
969 | * |
||
970 | * @group legacy |
||
971 | */ |
||
972 | public function testGetBaseCodeRoute(): void |
||
973 | { |
||
974 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
975 | |||
976 | $this->assertSame('', $admin->getBaseCodeRoute()); |
||
977 | |||
978 | $admin->setBaseCodeRoute('foo'); |
||
979 | $this->assertSame('foo', $admin->getBaseCodeRoute()); |
||
980 | } |
||
981 | |||
982 | // NEXT_MAJOR: uncomment this method. |
||
983 | // public function testGetBaseCodeRoute() |
||
984 | // { |
||
985 | // $postAdmin = new PostAdmin( |
||
986 | // 'sonata.post.admin.post', |
||
987 | // 'NewsBundle\Entity\Post', |
||
988 | // 'SonataNewsBundle:PostAdmin' |
||
989 | // ); |
||
990 | // $commentAdmin = new CommentAdmin( |
||
991 | // 'sonata.post.admin.comment', |
||
992 | // 'Application\Sonata\NewsBundle\Entity\Comment', |
||
993 | // 'SonataNewsBundle:CommentAdmin' |
||
994 | // ); |
||
995 | // |
||
996 | // $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute()); |
||
997 | // |
||
998 | // $postAdmin->addChild($commentAdmin); |
||
999 | // |
||
1000 | // $this->assertSame( |
||
1001 | // 'sonata.post.admin.post|sonata.post.admin.comment', |
||
1002 | // $commentAdmin->getBaseCodeRoute() |
||
1003 | // ); |
||
1004 | // } |
||
1005 | |||
1006 | public function testGetRouteGenerator(): void |
||
1007 | { |
||
1008 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1009 | |||
1010 | $this->assertNull($admin->getRouteGenerator()); |
||
1011 | |||
1012 | $routeGenerator = $this->createMock(RouteGeneratorInterface::class); |
||
1013 | |||
1014 | $admin->setRouteGenerator($routeGenerator); |
||
1015 | $this->assertSame($routeGenerator, $admin->getRouteGenerator()); |
||
1016 | } |
||
1017 | |||
1018 | public function testGetConfigurationPool(): void |
||
1019 | { |
||
1020 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1021 | |||
1022 | $this->assertNull($admin->getConfigurationPool()); |
||
1023 | |||
1024 | $pool = $this->getMockBuilder(Pool::class) |
||
1025 | ->disableOriginalConstructor() |
||
1026 | ->getMock(); |
||
1027 | |||
1028 | $admin->setConfigurationPool($pool); |
||
1029 | $this->assertSame($pool, $admin->getConfigurationPool()); |
||
1030 | } |
||
1031 | |||
1032 | public function testGetShowBuilder(): void |
||
1033 | { |
||
1034 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1035 | |||
1036 | $this->assertNull($admin->getShowBuilder()); |
||
1037 | |||
1038 | $showBuilder = $this->createMock(ShowBuilderInterface::class); |
||
1039 | |||
1040 | $admin->setShowBuilder($showBuilder); |
||
1041 | $this->assertSame($showBuilder, $admin->getShowBuilder()); |
||
1042 | } |
||
1043 | |||
1044 | public function testGetListBuilder(): void |
||
1045 | { |
||
1046 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1047 | |||
1048 | $this->assertNull($admin->getListBuilder()); |
||
1049 | |||
1050 | $listBuilder = $this->createMock(ListBuilderInterface::class); |
||
1051 | |||
1052 | $admin->setListBuilder($listBuilder); |
||
1053 | $this->assertSame($listBuilder, $admin->getListBuilder()); |
||
1054 | } |
||
1055 | |||
1056 | public function testGetDatagridBuilder(): void |
||
1057 | { |
||
1058 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1059 | |||
1060 | $this->assertNull($admin->getDatagridBuilder()); |
||
1061 | |||
1062 | $datagridBuilder = $this->createMock(DatagridBuilderInterface::class); |
||
1063 | |||
1064 | $admin->setDatagridBuilder($datagridBuilder); |
||
1065 | $this->assertSame($datagridBuilder, $admin->getDatagridBuilder()); |
||
1066 | } |
||
1067 | |||
1068 | public function testGetFormContractor(): void |
||
1069 | { |
||
1070 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1071 | |||
1072 | $this->assertNull($admin->getFormContractor()); |
||
1073 | |||
1074 | $formContractor = $this->createMock(FormContractorInterface::class); |
||
1075 | |||
1076 | $admin->setFormContractor($formContractor); |
||
1077 | $this->assertSame($formContractor, $admin->getFormContractor()); |
||
1078 | } |
||
1079 | |||
1080 | public function testGetRequest(): void |
||
1081 | { |
||
1082 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1083 | |||
1084 | $this->assertFalse($admin->hasRequest()); |
||
1085 | |||
1086 | $request = new Request(); |
||
1087 | |||
1088 | $admin->setRequest($request); |
||
1089 | $this->assertSame($request, $admin->getRequest()); |
||
1090 | $this->assertTrue($admin->hasRequest()); |
||
1091 | } |
||
1092 | |||
1093 | public function testGetRequestWithException(): void |
||
1094 | { |
||
1095 | $this->expectException(\RuntimeException::class); |
||
1096 | $this->expectExceptionMessage('The Request object has not been set'); |
||
1097 | |||
1098 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1099 | $admin->getRequest(); |
||
1100 | } |
||
1101 | |||
1102 | public function testGetTranslationDomain(): void |
||
1103 | { |
||
1104 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1105 | |||
1106 | $this->assertSame('messages', $admin->getTranslationDomain()); |
||
1107 | |||
1108 | $admin->setTranslationDomain('foo'); |
||
1109 | $this->assertSame('foo', $admin->getTranslationDomain()); |
||
1110 | } |
||
1111 | |||
1112 | /** |
||
1113 | * @group legacy |
||
1114 | */ |
||
1115 | public function testGetTranslator(): void |
||
1116 | { |
||
1117 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1118 | |||
1119 | $this->assertNull($admin->getTranslator()); |
||
1120 | |||
1121 | $translator = $this->createMock(TranslatorInterface::class); |
||
1122 | |||
1123 | $admin->setTranslator($translator); |
||
1124 | $this->assertSame($translator, $admin->getTranslator()); |
||
1125 | } |
||
1126 | |||
1127 | public function testGetShowGroups(): void |
||
1128 | { |
||
1129 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1130 | |||
1131 | $this->assertFalse($admin->getShowGroups()); |
||
1132 | |||
1133 | $groups = ['foo', 'bar', 'baz']; |
||
1134 | |||
1135 | $admin->setShowGroups($groups); |
||
1136 | $this->assertSame($groups, $admin->getShowGroups()); |
||
1137 | } |
||
1138 | |||
1139 | public function testGetFormGroups(): void |
||
1140 | { |
||
1141 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1142 | |||
1143 | $this->assertFalse($admin->getFormGroups()); |
||
1144 | |||
1145 | $groups = ['foo', 'bar', 'baz']; |
||
1146 | |||
1147 | $admin->setFormGroups($groups); |
||
1148 | $this->assertSame($groups, $admin->getFormGroups()); |
||
1149 | } |
||
1150 | |||
1151 | public function testGetMaxPageLinks(): void |
||
1152 | { |
||
1153 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1154 | |||
1155 | $this->assertSame(25, $admin->getMaxPageLinks()); |
||
1156 | |||
1157 | $admin->setMaxPageLinks(14); |
||
1158 | $this->assertSame(14, $admin->getMaxPageLinks()); |
||
1159 | } |
||
1160 | |||
1161 | public function testGetMaxPerPage(): void |
||
1162 | { |
||
1163 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1164 | |||
1165 | $this->assertSame(32, $admin->getMaxPerPage()); |
||
1166 | |||
1167 | $admin->setMaxPerPage(94); |
||
1168 | $this->assertSame(94, $admin->getMaxPerPage()); |
||
1169 | } |
||
1170 | |||
1171 | public function testGetLabel(): void |
||
1172 | { |
||
1173 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1174 | |||
1175 | $this->assertNull($admin->getLabel()); |
||
1176 | |||
1177 | $admin->setLabel('FooLabel'); |
||
1178 | $this->assertSame('FooLabel', $admin->getLabel()); |
||
1179 | } |
||
1180 | |||
1181 | public function testGetBaseController(): void |
||
1182 | { |
||
1183 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1184 | |||
1185 | $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName()); |
||
1186 | |||
1187 | $admin->setBaseControllerName('SonataNewsBundle:FooAdmin'); |
||
1188 | $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName()); |
||
1189 | } |
||
1190 | |||
1191 | public function testGetTemplates(): void |
||
1192 | { |
||
1193 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1194 | |||
1195 | $templates = [ |
||
1196 | 'list' => '@FooAdmin/CRUD/list.html.twig', |
||
1197 | 'show' => '@FooAdmin/CRUD/show.html.twig', |
||
1198 | 'edit' => '@FooAdmin/CRUD/edit.html.twig', |
||
1199 | ]; |
||
1200 | |||
1201 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
1202 | $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates); |
||
1203 | |||
1204 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
1205 | |||
1206 | $this->assertSame($templates, $admin->getTemplates()); |
||
1207 | } |
||
1208 | |||
1209 | public function testGetTemplate1(): void |
||
1210 | { |
||
1211 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1212 | |||
1213 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
1214 | $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig'); |
||
1215 | $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig'); |
||
1216 | |||
1217 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
1218 | |||
1219 | $this->assertSame('@FooAdmin/CRUD/edit.html.twig', $admin->getTemplate('edit')); |
||
1220 | $this->assertSame('@FooAdmin/CRUD/show.html.twig', $admin->getTemplate('show')); |
||
1221 | } |
||
1222 | |||
1223 | public function testGetIdParameter(): void |
||
1224 | { |
||
1225 | $postAdmin = new PostAdmin( |
||
1226 | 'sonata.post.admin.post', |
||
1227 | 'NewsBundle\Entity\Post', |
||
1228 | 'SonataNewsBundle:PostAdmin' |
||
1229 | ); |
||
1230 | |||
1231 | $this->assertSame('id', $postAdmin->getIdParameter()); |
||
1232 | $this->assertFalse($postAdmin->isChild()); |
||
1233 | |||
1234 | $commentAdmin = new CommentAdmin( |
||
1235 | 'sonata.post.admin.comment', |
||
1236 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
1237 | 'SonataNewsBundle:CommentAdmin' |
||
1238 | ); |
||
1239 | $commentAdmin->setParent($postAdmin); |
||
1240 | |||
1241 | $this->assertTrue($commentAdmin->isChild()); |
||
1242 | $this->assertSame('childId', $commentAdmin->getIdParameter()); |
||
1243 | |||
1244 | $commentVoteAdmin = new CommentVoteAdmin( |
||
1245 | 'sonata.post.admin.comment_vote', |
||
1246 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
1247 | 'SonataNewsBundle:CommentVoteAdmin' |
||
1248 | ); |
||
1249 | $commentVoteAdmin->setParent($commentAdmin); |
||
1250 | |||
1251 | $this->assertTrue($commentVoteAdmin->isChild()); |
||
1252 | $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter()); |
||
1253 | } |
||
1254 | |||
1255 | public function testGetExportFormats(): void |
||
1256 | { |
||
1257 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1258 | |||
1259 | $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats()); |
||
1260 | } |
||
1261 | |||
1262 | public function testGetUrlsafeIdentifier(): void |
||
1263 | { |
||
1264 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1265 | |||
1266 | $entity = new \stdClass(); |
||
1267 | |||
1268 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
1269 | $modelManager->expects($this->once()) |
||
1270 | ->method('getUrlsafeIdentifier') |
||
1271 | ->with($this->equalTo($entity)) |
||
1272 | ->willReturn('foo'); |
||
1273 | $admin->setModelManager($modelManager); |
||
1274 | |||
1275 | $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity)); |
||
1276 | } |
||
1277 | |||
1278 | public function testDeterminedPerPageValue(): void |
||
1279 | { |
||
1280 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1281 | |||
1282 | $this->assertFalse($admin->determinedPerPageValue('foo')); |
||
1283 | $this->assertFalse($admin->determinedPerPageValue(123)); |
||
1284 | $this->assertTrue($admin->determinedPerPageValue(16)); |
||
1285 | |||
1286 | $admin->setPerPageOptions([101, 102, 103]); |
||
1287 | $this->assertFalse($admin->determinedPerPageValue(16)); |
||
1288 | $this->assertTrue($admin->determinedPerPageValue(101)); |
||
1289 | } |
||
1290 | |||
1291 | public function testIsGranted(): void |
||
1292 | { |
||
1293 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1294 | |||
1295 | $entity = new \stdClass(); |
||
1296 | |||
1297 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
1298 | $securityHandler |
||
1299 | ->method('isGranted') |
||
1300 | ->willReturnCallback(static function ( |
||
1301 | AdminInterface $adminIn, |
||
1302 | string $attributes, |
||
1303 | $object = null |
||
1304 | ) use ( |
||
1305 | $admin, |
||
1306 | $entity |
||
1307 | ): bool { |
||
1308 | if ($admin === $adminIn && 'FOO' === $attributes) { |
||
1309 | if (($object === $admin) || ($object === $entity)) { |
||
1310 | return true; |
||
1311 | } |
||
1312 | } |
||
1313 | |||
1314 | return false; |
||
1315 | }); |
||
1316 | |||
1317 | $admin->setSecurityHandler($securityHandler); |
||
1318 | |||
1319 | $this->assertTrue($admin->isGranted('FOO')); |
||
1320 | $this->assertTrue($admin->isGranted('FOO', $entity)); |
||
1321 | $this->assertFalse($admin->isGranted('BAR')); |
||
1322 | $this->assertFalse($admin->isGranted('BAR', $entity)); |
||
1323 | } |
||
1324 | |||
1325 | public function testSupportsPreviewMode(): void |
||
1326 | { |
||
1327 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1328 | |||
1329 | $this->assertFalse($admin->supportsPreviewMode()); |
||
1330 | } |
||
1331 | |||
1332 | public function testGetPermissionsShow(): void |
||
1333 | { |
||
1334 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1335 | |||
1336 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
1337 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU)); |
||
1338 | $this->assertSame(['LIST'], $admin->getPermissionsShow('foo')); |
||
1339 | } |
||
1340 | |||
1341 | public function testShowIn(): void |
||
1342 | { |
||
1343 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1344 | |||
1345 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
1346 | $securityHandler |
||
1347 | ->method('isGranted') |
||
1348 | ->willReturnCallback(static function (AdminInterface $adminIn, array $attributes, $object = null) use ($admin): bool { |
||
1349 | return $admin === $adminIn && $attributes === ['LIST']; |
||
1350 | }); |
||
1351 | |||
1352 | $admin->setSecurityHandler($securityHandler); |
||
1353 | |||
1354 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
1355 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU)); |
||
1356 | $this->assertTrue($admin->showIn('foo')); |
||
1357 | } |
||
1358 | |||
1359 | public function testGetObjectIdentifier(): void |
||
1360 | { |
||
1361 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1362 | |||
1363 | $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier()); |
||
1364 | } |
||
1365 | |||
1366 | /** |
||
1367 | * @group legacy |
||
1368 | */ |
||
1369 | public function testTrans(): void |
||
1370 | { |
||
1371 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1372 | $admin->setTranslationDomain('fooMessageDomain'); |
||
1373 | |||
1374 | $translator = $this->createMock(TranslatorInterface::class); |
||
1375 | $admin->setTranslator($translator); |
||
1376 | |||
1377 | $translator->expects($this->once()) |
||
1378 | ->method('trans') |
||
1379 | ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
1380 | ->willReturn('fooTranslated'); |
||
1381 | |||
1382 | $this->assertSame('fooTranslated', $admin->trans('foo')); |
||
1383 | } |
||
1384 | |||
1385 | /** |
||
1386 | * @group legacy |
||
1387 | */ |
||
1388 | public function testTransWithMessageDomain(): void |
||
1389 | { |
||
1390 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1391 | |||
1392 | $translator = $this->createMock(TranslatorInterface::class); |
||
1393 | $admin->setTranslator($translator); |
||
1394 | |||
1395 | $translator->expects($this->once()) |
||
1396 | ->method('trans') |
||
1397 | ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
1398 | ->willReturn('fooTranslated'); |
||
1399 | |||
1400 | $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain')); |
||
1401 | } |
||
1402 | |||
1403 | /** |
||
1404 | * @group legacy |
||
1405 | */ |
||
1406 | public function testTransChoice(): void |
||
1407 | { |
||
1408 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1409 | $admin->setTranslationDomain('fooMessageDomain'); |
||
1410 | |||
1411 | $translator = $this->createMock(TranslatorInterface::class); |
||
1412 | $admin->setTranslator($translator); |
||
1413 | |||
1414 | $translator->expects($this->once()) |
||
1415 | ->method('transChoice') |
||
1416 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
1417 | ->willReturn('fooTranslated'); |
||
1418 | |||
1419 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2)); |
||
1420 | } |
||
1421 | |||
1422 | /** |
||
1423 | * @group legacy |
||
1424 | */ |
||
1425 | public function testTransChoiceWithMessageDomain(): void |
||
1426 | { |
||
1427 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1428 | |||
1429 | $translator = $this->createMock(TranslatorInterface::class); |
||
1430 | $admin->setTranslator($translator); |
||
1431 | |||
1432 | $translator->expects($this->once()) |
||
1433 | ->method('transChoice') |
||
1434 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
1435 | ->willReturn('fooTranslated'); |
||
1436 | |||
1437 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain')); |
||
1438 | } |
||
1439 | |||
1440 | public function testSetFilterPersister(): void |
||
1441 | { |
||
1442 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1443 | |||
1444 | $filterPersister = $this->createMock('Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface'); |
||
1445 | |||
1446 | $this->assertAttributeSame(null, 'filterPersister', $admin); |
||
1447 | $admin->setFilterPersister($filterPersister); |
||
1448 | $this->assertAttributeSame($filterPersister, 'filterPersister', $admin); |
||
1449 | $this->assertAttributeSame(true, 'persistFilters', $admin); |
||
1450 | } |
||
1451 | |||
1452 | public function testGetRootCode(): void |
||
1453 | { |
||
1454 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1455 | |||
1456 | $this->assertSame('sonata.post.admin.post', $admin->getRootCode()); |
||
1457 | |||
1458 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin'); |
||
1459 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
1460 | $parentFieldDescription->expects($this->once()) |
||
1461 | ->method('getAdmin') |
||
1462 | ->willReturn($parentAdmin); |
||
1463 | |||
1464 | $this->assertNull($admin->getParentFieldDescription()); |
||
1465 | $admin->setParentFieldDescription($parentFieldDescription); |
||
1466 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
1467 | $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode()); |
||
1468 | } |
||
1469 | |||
1470 | public function testGetRoot(): void |
||
1471 | { |
||
1472 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1473 | |||
1474 | $this->assertSame($admin, $admin->getRoot()); |
||
1475 | |||
1476 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin'); |
||
1477 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
1478 | $parentFieldDescription->expects($this->once()) |
||
1479 | ->method('getAdmin') |
||
1480 | ->willReturn($parentAdmin); |
||
1481 | |||
1482 | $this->assertNull($admin->getParentFieldDescription()); |
||
1483 | $admin->setParentFieldDescription($parentFieldDescription); |
||
1484 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
1485 | $this->assertSame($parentAdmin, $admin->getRoot()); |
||
1486 | } |
||
1487 | |||
1488 | public function testGetExportFields(): void |
||
1489 | { |
||
1490 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1491 | |||
1492 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
1493 | $modelManager->expects($this->once()) |
||
1494 | ->method('getExportFields') |
||
1495 | ->with($this->equalTo('NewsBundle\Entity\Post')) |
||
1496 | ->willReturn(['foo', 'bar']); |
||
1497 | |||
1498 | $admin->setModelManager($modelManager); |
||
1499 | $this->assertSame(['foo', 'bar'], $admin->getExportFields()); |
||
1500 | } |
||
1501 | |||
1502 | public function testGetPersistentParametersWithNoExtension(): void |
||
1503 | { |
||
1504 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1505 | |||
1506 | $this->assertEmpty($admin->getPersistentParameters()); |
||
1507 | } |
||
1508 | |||
1509 | public function testGetPersistentParametersWithInvalidExtension(): void |
||
1510 | { |
||
1511 | $this->expectException(\RuntimeException::class); |
||
1512 | |||
1513 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1514 | |||
1515 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
1516 | $extension->expects($this->once())->method('getPersistentParameters')->willReturn(null); |
||
1517 | |||
1518 | $admin->addExtension($extension); |
||
1519 | |||
1520 | $admin->getPersistentParameters(); |
||
1521 | } |
||
1522 | |||
1523 | public function testGetPersistentParametersWithValidExtension(): void |
||
1524 | { |
||
1525 | $expected = [ |
||
1526 | 'context' => 'foobar', |
||
1527 | ]; |
||
1528 | |||
1529 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1530 | |||
1531 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
1532 | $extension->expects($this->once())->method('getPersistentParameters')->willReturn($expected); |
||
1533 | |||
1534 | $admin->addExtension($extension); |
||
1535 | |||
1536 | $this->assertSame($expected, $admin->getPersistentParameters()); |
||
1537 | } |
||
1538 | |||
1539 | public function testGetFormWithNonCollectionParentValue(): void |
||
1540 | { |
||
1541 | $post = new Post(); |
||
1542 | $tagAdmin = $this->createTagAdmin($post); |
||
1543 | $tag = $tagAdmin->getSubject(); |
||
1544 | |||
1545 | $tag->setPosts(null); |
||
1546 | $tagAdmin->getForm(); |
||
1547 | $this->assertSame($post, $tag->getPosts()); |
||
1548 | } |
||
1549 | |||
1550 | public function testGetFormWithCollectionParentValue(): void |
||
1551 | { |
||
1552 | $post = new Post(); |
||
1553 | $tagAdmin = $this->createTagAdmin($post); |
||
1554 | $tag = $tagAdmin->getSubject(); |
||
1555 | |||
1556 | // Case of a doctrine collection |
||
1557 | $this->assertInstanceOf(Collection::class, $tag->getPosts()); |
||
1558 | $this->assertCount(0, $tag->getPosts()); |
||
1559 | |||
1560 | $tag->addPost(new Post()); |
||
1561 | |||
1562 | $this->assertCount(1, $tag->getPosts()); |
||
1563 | |||
1564 | $tagAdmin->getForm(); |
||
1565 | |||
1566 | $this->assertInstanceOf(Collection::class, $tag->getPosts()); |
||
1567 | $this->assertCount(2, $tag->getPosts()); |
||
1568 | $this->assertContains($post, $tag->getPosts()); |
||
1569 | |||
1570 | // Case of an array |
||
1571 | $tag->setPosts([]); |
||
1572 | $this->assertCount(0, $tag->getPosts()); |
||
1573 | |||
1574 | $tag->addPost(new Post()); |
||
1575 | |||
1576 | $this->assertCount(1, $tag->getPosts()); |
||
1577 | |||
1578 | $tagAdmin->getForm(); |
||
1579 | |||
1580 | $this->assertInternalType('array', $tag->getPosts()); |
||
1581 | $this->assertCount(2, $tag->getPosts()); |
||
1582 | $this->assertContains($post, $tag->getPosts()); |
||
1583 | } |
||
1584 | |||
1585 | public function testFormAddPostSubmitEventForPreValidation(): void |
||
1586 | { |
||
1587 | $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin'); |
||
1588 | $object = new \stdClass(); |
||
1589 | |||
1590 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
1591 | $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
1592 | |||
1593 | $validator = $this->createMock(ValidatorInterface::class); |
||
1594 | $validator |
||
1595 | ->method('getMetadataFor') |
||
1596 | ->willReturn($this->createMock(MemberMetadata::class)); |
||
1597 | $modelAdmin->setValidator($validator); |
||
1598 | |||
1599 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
1600 | $modelManager |
||
1601 | ->method('getNewFieldDescriptionInstance') |
||
1602 | ->willReturn(new FieldDescription()); |
||
1603 | $modelAdmin->setModelManager($modelManager); |
||
1604 | |||
1605 | // a Admin class to test that preValidate is called |
||
1606 | $testAdminPreValidate = $this->createMock(AbstractAdmin::class); |
||
1607 | $testAdminPreValidate->expects($this->once()) |
||
1608 | ->method('preValidate') |
||
1609 | ->with($this->identicalTo($object)); |
||
1610 | |||
1611 | $event = $this->createMock(FormEvent::class); |
||
1612 | $event |
||
1613 | ->method('getData') |
||
1614 | ->willReturn($object); |
||
1615 | |||
1616 | $formBuild = $this->createMock(FormBuilder::class); |
||
1617 | $formBuild->expects($this->once()) |
||
1618 | ->method('addEventListener') |
||
1619 | ->with($this->identicalTo(FormEvents::POST_SUBMIT), |
||
1620 | $this->callback(static function ($callback) use ($testAdminPreValidate, $event): bool { |
||
1621 | if (\is_callable($callback)) { |
||
1622 | $closure = $callback->bindTo($testAdminPreValidate); |
||
1623 | $closure($event); |
||
1624 | |||
1625 | return true; |
||
1626 | } |
||
1627 | |||
1628 | return false; |
||
1629 | }), |
||
1630 | $this->greaterThan(0) |
||
1631 | ); |
||
1632 | |||
1633 | $formContractor = $this->createMock(FormContractorInterface::class); |
||
1634 | $formContractor |
||
1635 | ->method('getDefaultOptions') |
||
1636 | ->willReturn([]); |
||
1637 | $formContractor |
||
1638 | ->method('getFormBuilder') |
||
1639 | ->willReturn($formBuild); |
||
1640 | |||
1641 | $modelAdmin->setFormContractor($formContractor); |
||
1642 | $modelAdmin->defineFormBuilder($formBuild); |
||
1643 | $modelAdmin->getForm(); |
||
1644 | } |
||
1645 | |||
1646 | public function testRemoveFieldFromFormGroup(): void |
||
1647 | { |
||
1648 | $formGroups = [ |
||
1649 | 'foobar' => [ |
||
1650 | 'fields' => [ |
||
1651 | 'foo' => 'foo', |
||
1652 | 'bar' => 'bar', |
||
1653 | ], |
||
1654 | ], |
||
1655 | ]; |
||
1656 | |||
1657 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1658 | $admin->setFormGroups($formGroups); |
||
1659 | |||
1660 | $admin->removeFieldFromFormGroup('foo'); |
||
1661 | $this->assertSame($admin->getFormGroups(), [ |
||
1662 | 'foobar' => [ |
||
1663 | 'fields' => [ |
||
1664 | 'bar' => 'bar', |
||
1665 | ], |
||
1666 | ], |
||
1667 | ]); |
||
1668 | |||
1669 | $admin->removeFieldFromFormGroup('bar'); |
||
1670 | $this->assertSame($admin->getFormGroups(), []); |
||
1671 | } |
||
1672 | |||
1673 | public function testGetFilterParameters(): void |
||
1674 | { |
||
1675 | $authorId = uniqid(); |
||
1676 | |||
1677 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1678 | |||
1679 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
1680 | $commentAdmin->setParentAssociationMapping('post.author'); |
||
1681 | $commentAdmin->setParent($postAdmin); |
||
1682 | |||
1683 | $request = $this->createMock(Request::class); |
||
1684 | $query = $this->createMock(ParameterBag::class); |
||
1685 | $query |
||
1686 | ->method('get') |
||
1687 | ->willReturn([ |
||
1688 | 'filter' => [ |
||
1689 | '_page' => '1', |
||
1690 | '_per_page' => '32', |
||
1691 | ], |
||
1692 | ]); |
||
1693 | |||
1694 | $request->query = $query; |
||
1695 | $request |
||
1696 | ->method('get') |
||
1697 | ->willReturn($authorId); |
||
1698 | |||
1699 | $commentAdmin->setRequest($request); |
||
1700 | |||
1701 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
1702 | $modelManager |
||
1703 | ->method('getDefaultSortValues') |
||
1704 | ->willReturn([]); |
||
1705 | |||
1706 | $commentAdmin->setModelManager($modelManager); |
||
1707 | |||
1708 | $parameters = $commentAdmin->getFilterParameters(); |
||
1709 | |||
1710 | $this->assertTrue(isset($parameters['post__author'])); |
||
1711 | $this->assertSame(['value' => $authorId], $parameters['post__author']); |
||
1712 | } |
||
1713 | |||
1714 | public function testGetFilterFieldDescription(): void |
||
1785 | |||
1786 | public function testGetSubjectNoRequest(): void |
||
1798 | |||
1799 | public function testGetSideMenu(): void |
||
1823 | |||
1824 | /** |
||
1825 | * @return array |
||
1826 | */ |
||
1827 | public function provideGetSubject() |
||
1837 | |||
1838 | /** |
||
1839 | * @dataProvider provideGetSubject |
||
1840 | */ |
||
1841 | public function testGetSubjectFailed($id): void |
||
1856 | |||
1857 | /** |
||
1858 | * @dataProvider provideGetSubject |
||
1859 | */ |
||
1860 | public function testGetSubject($id): void |
||
1878 | |||
1879 | public function testGetSubjectWithParentDescription(): void |
||
1880 | { |
||
1881 | $adminId = 1; |
||
1882 | |||
1883 | $comment = new Comment(); |
||
1884 | |||
1885 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
1886 | $modelManager |
||
1887 | ->method('find') |
||
1888 | ->with('NewsBundle\Entity\Comment', $adminId) |
||
1889 | ->willReturn($comment); |
||
1890 | |||
1891 | $request = new Request(['id' => $adminId]); |
||
1892 | |||
1893 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1894 | $postAdmin->setRequest($request); |
||
1895 | |||
1896 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
1897 | $commentAdmin->setRequest($request); |
||
1898 | $commentAdmin->setModelManager($modelManager); |
||
1899 | |||
1900 | $this->assertSame($comment, $commentAdmin->getSubject()); |
||
1901 | |||
1902 | $commentAdmin->setSubject(null); |
||
1903 | $commentAdmin->setParentFieldDescription(new FieldDescription()); |
||
1904 | |||
1905 | $this->assertNull($commentAdmin->getSubject()); |
||
1906 | } |
||
1907 | |||
1908 | /** |
||
1909 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
1910 | */ |
||
1911 | public function testGetActionButtonsList(): void |
||
1912 | { |
||
1913 | $expected = [ |
||
1914 | 'create' => [ |
||
1915 | 'template' => 'Foo.html.twig', |
||
1916 | ], |
||
1917 | ]; |
||
1918 | |||
1919 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1920 | |||
1921 | $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
||
1922 | $templateRegistry->getTemplate('button_create')->willReturn('Foo.html.twig'); |
||
1923 | |||
1924 | $admin->setTemplateRegistry($templateRegistry->reveal()); |
||
1925 | |||
1926 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
1927 | $securityHandler |
||
1928 | ->expects($this->once()) |
||
1929 | ->method('isGranted') |
||
1930 | ->with($admin, 'CREATE', $admin) |
||
1931 | ->willReturn(true); |
||
1932 | $admin->setSecurityHandler($securityHandler); |
||
1933 | |||
1934 | $routeGenerator = $this->createMock(RouteGeneratorInterface::class); |
||
1935 | $routeGenerator |
||
1936 | ->expects($this->once()) |
||
1937 | ->method('hasAdminRoute') |
||
1938 | ->with($admin, 'create') |
||
1939 | ->willReturn(true); |
||
1940 | $admin->setRouteGenerator($routeGenerator); |
||
1941 | |||
1942 | $this->assertSame($expected, $admin->getActionButtons('list', null)); |
||
1943 | } |
||
1944 | |||
1945 | /** |
||
1946 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
1947 | */ |
||
1948 | public function testGetActionButtonsListCreateDisabled(): void |
||
1949 | { |
||
1950 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1951 | |||
1952 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
1953 | $securityHandler |
||
1954 | ->expects($this->once()) |
||
1955 | ->method('isGranted') |
||
1956 | ->with($admin, 'CREATE', $admin) |
||
1957 | ->willReturn(false); |
||
1958 | $admin->setSecurityHandler($securityHandler); |
||
1959 | |||
1960 | $this->assertSame([], $admin->getActionButtons('list', null)); |
||
1961 | } |
||
1962 | |||
1963 | /** |
||
1964 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions |
||
1965 | */ |
||
1966 | public function testGetBatchActions(): void |
||
1967 | { |
||
1968 | $expected = [ |
||
1969 | 'delete' => [ |
||
1970 | 'label' => 'action_delete', |
||
1971 | 'translation_domain' => 'SonataAdminBundle', |
||
2020 | |||
2021 | /** |
||
2022 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
2023 | */ |
||
2024 | public function testShowMosaicButton(): void |
||
2033 | |||
2034 | /** |
||
2035 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
2036 | */ |
||
2037 | public function testShowMosaicButtonHideMosaic(): void |
||
2047 | |||
2048 | /** |
||
2049 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions |
||
2050 | * @dataProvider provideGetBaseRouteName |
||
2051 | */ |
||
2052 | public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void |
||
2083 | |||
2084 | public function testDefaultFilters(): void |
||
2143 | |||
2144 | /** |
||
2145 | * @group legacy |
||
2146 | */ |
||
2147 | public function testDefaultBreadcrumbsBuilder(): void |
||
2171 | |||
2172 | /** |
||
2173 | * @group legacy |
||
2174 | */ |
||
2175 | public function testBreadcrumbsBuilderSetter(): void |
||
2185 | |||
2186 | /** |
||
2187 | * @group legacy |
||
2188 | */ |
||
2189 | public function testGetBreadcrumbs(): void |
||
2199 | |||
2200 | /** |
||
2201 | * @group legacy |
||
2202 | */ |
||
2203 | public function testBuildBreadcrumbs(): void |
||
2220 | |||
2221 | /** |
||
2222 | * NEXT_MAJOR: remove this method. |
||
2223 | * |
||
2224 | * @group legacy |
||
2225 | */ |
||
2226 | public function testCreateQueryLegacyCallWorks(): void |
||
2240 | |||
2241 | public function testGetDataSourceIterator(): void |
||
2283 | |||
2284 | public function testCircularChildAdmin(): void |
||
2304 | |||
2305 | public function testCircularChildAdminTripleLevel(): void |
||
2331 | |||
2332 | public function testCircularChildAdminWithItself(): void |
||
2346 | |||
2347 | public function testGetRootAncestor(): void |
||
2381 | |||
2382 | public function testGetChildDepth(): void |
||
2416 | |||
2417 | public function testGetCurrentLeafChildAdmin(): void |
||
2454 | |||
2455 | private function createTagAdmin(Post $post): TagAdmin |
||
2494 | } |
||
2495 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: