From 72c11bc98002629f0a348f7db89a7181da6a2938 Mon Sep 17 00:00:00 2001 From: Emmanuel Vella Date: Fri, 12 Apr 2013 14:09:51 +0200 Subject: [PATCH] Add tests and doc --- Resources/doc/reference/architecture.rst | 43 ++++++++++++++++++++++++ Tests/Admin/BaseAdminTest.php | 35 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/Resources/doc/reference/architecture.rst b/Resources/doc/reference/architecture.rst index ea7dde975d..da5be68b75 100644 --- a/Resources/doc/reference/architecture.rst +++ b/Resources/doc/reference/architecture.rst @@ -157,5 +157,48 @@ Note that you can use both the Bundle:Controller format or a `service name`_ to specify what controller to load. If you provide null instead of SonataNewsBundle:PostAdmin, you will not need to create a controller class and the system will use the default. +Create child admins +--------------------------- + +Let's say you have a PostAdmin and a CommentAdmin. You can optionally declare the CommentAdmin +to be a child of the PostAdmin. This will create new routes like, for example, ``/post/{id}/comment/list``, +where the comments will automatically be filtered by post. + +To do this, you first need to call the ``addChild`` method in your PostAdmin service configuration : + +.. code-block:: xml + + + + ... + + + + + + +Then, you have to set the CommentAdmin ``parentAssociationMapping`` attribute to ``post`` : + +.. code-block:: php + + parentAssociationMapping = $associationMapping; + } + public function setClassnameLabel($label) { $this->classnameLabel = $label; @@ -221,4 +226,34 @@ public function testToString() $this->assertEquals("", $admin->toString(false)); } + + public function testGetFilterParameters() + { + $authorId = uniqid(); + + $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); + + $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); + $commentAdmin->setParentAssociationMapping('post.author'); + $commentAdmin->setParent($postAdmin); + + $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array('get')); + $request->expects($this->any()) + ->method('get') + ->will($this->returnValue($authorId)); + + $commentAdmin->setRequest($request); + + $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'); + $modelManager->expects($this->any()) + ->method('getDefaultSortValues') + ->will($this->returnValue(array())); + + $commentAdmin->setModelManager($modelManager); + + $parameters = $commentAdmin->getFilterParameters(); + + $this->assertTrue(isset($parameters['post__author'])); + $this->assertEquals(array('value' => $authorId), $parameters['post__author']); + } }