Explorar el Código

add actions buttons tests in templates (#4077)

Templates buttons conditions were removed in #4062 but it caused
regressions. This commit re-add those tests in their improved form.
Fabien Bourigault hace 8 años
padre
commit
22fcba2ab2

+ 31 - 17
Admin/AbstractAdmin.php

@@ -2797,19 +2797,28 @@ EOT;
     {
         $list = array();
 
-        if (in_array($action, array('tree', 'show', 'edit', 'delete', 'list', 'batch')) && $this->hasAccess('create')) {
+        if (in_array($action, array('tree', 'show', 'edit', 'delete', 'list', 'batch'))
+            && $this->hasAccess('create')
+            && $this->hasRoute('create')
+        ) {
             $list['create'] = array(
                 'template' => 'SonataAdminBundle:Button:create_button.html.twig',
             );
         }
 
-        if (in_array($action, array('show', 'delete', 'acl', 'history')) && $this->canAccessObject('edit', $object)) {
+        if (in_array($action, array('show', 'delete', 'acl', 'history'))
+            && $this->canAccessObject('edit', $object)
+            && $this->hasRoute('edit')
+        ) {
             $list['edit'] = array(
                 'template' => 'SonataAdminBundle:Button:edit_button.html.twig',
             );
         }
 
-        if (in_array($action, array('show', 'edit', 'acl')) && $this->canAccessObject('history', $object)) {
+        if (in_array($action, array('show', 'edit', 'acl'))
+            && $this->canAccessObject('history', $object)
+            && $this->hasRoute('history')
+        ) {
             $list['history'] = array(
                 'template' => 'SonataAdminBundle:Button:history_button.html.twig',
             );
@@ -2818,6 +2827,7 @@ EOT;
         if (in_array($action, array('edit', 'history'))
             && $this->isAclEnabled()
             && $this->canAccessObject('acl', $object)
+            && $this->hasRoute('acl')
         ) {
             $list['acl'] = array(
                 'template' => 'SonataAdminBundle:Button:acl_button.html.twig',
@@ -2827,13 +2837,17 @@ EOT;
         if (in_array($action, array('edit', 'history', 'acl'))
             && $this->canAccessObject('show', $object)
             && count($this->getShow()) > 0
+            && $this->hasRoute('show')
         ) {
             $list['show'] = array(
                 'template' => 'SonataAdminBundle:Button:show_button.html.twig',
             );
         }
 
-        if (in_array($action, array('show', 'edit', 'delete', 'acl', 'batch')) && $this->hasAccess('list')) {
+        if (in_array($action, array('show', 'edit', 'delete', 'acl', 'batch'))
+            && $this->hasAccess('list')
+            && $this->hasRoute('list')
+        ) {
             $list['list'] = array(
                 'template' => 'SonataAdminBundle:Button:list_button.html.twig',
             );
@@ -2922,6 +2936,19 @@ EOT;
         return;
     }
 
+    /**
+     * Check object existence and access, without throw Exception.
+     *
+     * @param string $action
+     * @param object $object
+     *
+     * @return bool
+     */
+    public function canAccessObject($action, $object)
+    {
+        return $object && $this->id($object) && $this->hasAccess($action, $object);
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -3218,17 +3245,4 @@ EOT;
             $extension->configureRoutes($this, $this->routes);
         }
     }
-
-    /**
-     * Check object existence and access, without throw Exception.
-     *
-     * @param string $action
-     * @param object $object
-     *
-     * @return bool
-     */
-    private function canAccessObject($action, $object)
-    {
-        return $object && $this->id($object) && $this->hasAccess($action, $object);
-    }
 }

+ 5 - 4
Resources/views/Button/acl_button.html.twig

@@ -8,7 +8,8 @@ For the full copyright and license information, please view the LICENSE
 file that was distributed with this source code.
 
 #}
-
-<a class="sonata-action-element" href="{{ admin.generateObjectUrl('acl', object) }}">
-    <i class="fa fa-users"></i>
-    {{ 'link_action_acl'|trans({}, 'SonataAdminBundle') }}</a>
+{% if admin.isAclEnabled() and admin.canAccessObject('acl', object) and admin.hasRoute('acl') %}
+    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('acl', object) }}">
+        <i class="fa fa-users"></i>
+        {{ 'link_action_acl'|trans({}, 'SonataAdminBundle') }}</a>
+{% endif %}

+ 17 - 15
Resources/views/Button/create_button.html.twig

@@ -9,19 +9,21 @@ file that was distributed with this source code.
 
 #}
 
-{% if admin.subClasses is empty %}
-    <a class="sonata-action-element" href="{{ admin.generateUrl('create') }}">
-        <i class="fa fa-plus-circle"></i>
-        {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }}</a>
-{% else %}
-    <li class="divider" role="presentation"></li>
-    {% for subclass in admin.subclasses|keys %}
-        <li>
-            <a href="{{ admin.generateUrl('create', {'subclass': subclass}) }}">
-                <i class="fa fa-plus-circle"></i>
-                {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }} {{ subclass|trans({}, admin.translationdomain) }}
-            </a>
-        </li>
-    {% endfor %}
-    <li class="divider" role="presentation"></li>
+{% if admin.hasAccess('create') and admin.hasRoute('create') %}
+    {% if admin.subClasses is empty %}
+        <a class="sonata-action-element" href="{{ admin.generateUrl('create') }}">
+            <i class="fa fa-plus-circle"></i>
+            {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }}</a>
+    {% else %}
+        <li class="divider" role="presentation"></li>
+        {% for subclass in admin.subclasses|keys %}
+            <li>
+                <a href="{{ admin.generateUrl('create', {'subclass': subclass}) }}">
+                    <i class="fa fa-plus-circle"></i>
+                    {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }} {{ subclass|trans({}, admin.translationdomain) }}
+                </a>
+            </li>
+        {% endfor %}
+        <li class="divider" role="presentation"></li>
+    {% endif %}
 {% endif %}

+ 5 - 3
Resources/views/Button/edit_button.html.twig

@@ -9,6 +9,8 @@ file that was distributed with this source code.
 
 #}
 
-<a class="sonata-action-element" href="{{ admin.generateObjectUrl('edit', object) }}">
-    <i class="fa fa-edit"></i>
-    {{ 'link_action_edit'|trans({}, 'SonataAdminBundle') }}</a>
+{% if admin.canAccessObject('edit', object) and admin.hasRoute('edit') %}
+    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('edit', object) }}">
+        <i class="fa fa-edit"></i>
+        {{ 'link_action_edit'|trans({}, 'SonataAdminBundle') }}</a>
+{% endif %}

+ 5 - 3
Resources/views/Button/history_button.html.twig

@@ -9,6 +9,8 @@ file that was distributed with this source code.
 
 #}
 
-<a class="sonata-action-element" href="{{ admin.generateObjectUrl('history', object) }}">
-    <i class="fa fa-archive"></i>
-    {{ 'link_action_history'|trans({}, 'SonataAdminBundle') }}</a>
+{% if admin.canAccessObject('history', object) and admin.hasRoute('history') %}
+    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('history', object) }}">
+        <i class="fa fa-archive"></i>
+        {{ 'link_action_history'|trans({}, 'SonataAdminBundle') }}</a>
+{% endif %}

+ 5 - 3
Resources/views/Button/list_button.html.twig

@@ -9,6 +9,8 @@ file that was distributed with this source code.
 
 #}
 
-<a class="sonata-action-element" href="{{ admin.generateUrl('list') }}">
-    <i class="fa fa-list"></i>
-    {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}</a>
+{% if admin.hasAccess('list') and admin.hasRoute('list') %}
+    <a class="sonata-action-element" href="{{ admin.generateUrl('list') }}">
+        <i class="fa fa-list"></i>
+        {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}</a>
+{% endif %}

+ 5 - 4
Resources/views/Button/show_button.html.twig

@@ -8,7 +8,8 @@ For the full copyright and license information, please view the LICENSE
 file that was distributed with this source code.
 
 #}
-
-<a class="sonata-action-element" href="{{ admin.generateObjectUrl('show', object) }}">
-    <i class="fa fa-eye"></i>
-    {{ 'link_action_show'|trans({}, 'SonataAdminBundle') }}</a>
+{% if admin.canAccessObject('show', object) and admin.show|length > 0 and admin.hasRoute('show') %}
+    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('show', object) }}">
+        <i class="fa fa-eye"></i>
+        {{ 'link_action_show'|trans({}, 'SonataAdminBundle') }}</a>
+{% endif %}

+ 8 - 0
Tests/Admin/AdminTest.php

@@ -1651,6 +1651,14 @@ class AdminTest extends \PHPUnit_Framework_TestCase
             ->will($this->returnValue(true));
         $admin->setSecurityHandler($securityHandler);
 
+        $routeGenerator = $this->getMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
+        $routeGenerator
+            ->expects($this->once())
+            ->method('hasAdminRoute')
+            ->with($admin, 'create')
+            ->will($this->returnValue(true));
+        $admin->setRouteGenerator($routeGenerator);
+
         $this->assertSame($expected, $admin->getActionButtons('list', null));
     }