Bläddra i källkod

Add tests and doc

Emmanuel Vella 12 år sedan
förälder
incheckning
72c11bc980
2 ändrade filer med 78 tillägg och 0 borttagningar
  1. 43 0
      Resources/doc/reference/architecture.rst
  2. 35 0
      Tests/Admin/BaseAdminTest.php

+ 43 - 0
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
+
+    <!-- app/config/config.xml -->
+    <service id="sonata.news.admin.post" class="Sonata\NewsBundle\Admin\CommentAdmin">
+        ...
+
+        <call method="addChild">
+            <argument type="service" id="sonata.news.admin.comment" />
+        </call>
+    </service>
+
+Then, you have to set the CommentAdmin ``parentAssociationMapping`` attribute to ``post`` :
+
+.. code-block:: php
+
+    <?php
+    namespace Sonata\NewsBundle\Admin;
+
+    ...
+
+    class CommentAdmin extends Admin
+    {
+        protected $parentAssociationMapping = 'post';
+
+        // OR
+
+        public function getParentAssociationMapping()
+        {
+            return 'post';
+        }
+    }
+
+It also possible to set a dot-separated value, like ``post.author``, if your parent and child admins are not directly related.
+
 .. _`Django Project Website`: http://www.djangoproject.com/
 .. _`service name`: http://symfony.com/doc/2.1/cookbook/controller/service.html

+ 35 - 0
Tests/Admin/BaseAdminTest.php

@@ -48,6 +48,11 @@ class PostAdmin extends Admin
 
 class CommentAdmin extends Admin
 {
+    public function setParentAssociationMapping($associationMapping)
+    {
+        $this->parentAssociationMapping = $associationMapping;
+    }
+
     public function setClassnameLabel($label)
     {
         $this->classnameLabel = $label;
@@ -221,4 +226,34 @@ class BaseAdminTest extends \PHPUnit_Framework_TestCase
 
         $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']);
+    }
 }