From c58cc7916e9c19169bf0d0e11e127bd483296de9 Mon Sep 17 00:00:00 2001
From: Sebastien Robin <seb@nexedi.com>
Date: Tue, 5 Oct 2010 12:26:16 +0000
Subject: [PATCH] The priority on actions was somewhat working only by chance.
 There was only a sort for action inside a particular provider, and there was
 no sorting at all between actions coming from different action providers.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38891 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/ERP5Type.py            |  1 -
 product/ERP5Type/patches/ActionsTool.py |  1 +
 product/ERP5Type/tests/testERP5Type.py  | 37 +++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/product/ERP5Type/ERP5Type.py b/product/ERP5Type/ERP5Type.py
index 7ad6ea3b45..4b9d83c683 100644
--- a/product/ERP5Type/ERP5Type.py
+++ b/product/ERP5Type/ERP5Type.py
@@ -541,7 +541,6 @@ class ERP5TypeInformation(XMLObject,
 
     def _getActionList(self):
       action_list = self.getCacheableActionList()
-      action_list.sort(key=lambda x:x['priority'])
       return action_list
     _getActionList = CachingMethod(_getActionList,
       id='getActionList',
diff --git a/product/ERP5Type/patches/ActionsTool.py b/product/ERP5Type/patches/ActionsTool.py
index 3f7cee8a7c..c7d119a300 100644
--- a/product/ERP5Type/patches/ActionsTool.py
+++ b/product/ERP5Type/patches/ActionsTool.py
@@ -100,6 +100,7 @@ def listFilteredActionsFor(self, object=None):
             # IActionProviders:
             migrateNonProviders(self)
 
+    actions.sort(key=lambda x:x.get('priority', 0))
     # Reorganize the actions by category.
     filtered_actions={'user':[],
                       'folder':[],
diff --git a/product/ERP5Type/tests/testERP5Type.py b/product/ERP5Type/tests/testERP5Type.py
index 42f0c70171..0b982c5866 100644
--- a/product/ERP5Type/tests/testERP5Type.py
+++ b/product/ERP5Type/tests/testERP5Type.py
@@ -2735,6 +2735,43 @@ class TestPropertySheet:
       self.assertTrue(o.getIcon().endswith(portal.portal_types['Organisation']\
           .getTypeIcon()))
 
+    def test_actionPriority(self):
+      """Tests action priority
+      """
+      portal = self.getPortalObject()
+      portal_actions = self.getPortal().portal_actions
+      try:
+        module = self.getPersonModule()
+        person = module.newContent(id='1', portal_type='Person')
+        def addCustomAction(name, priority):
+          portal_actions.addAction(id=name,
+                                   title=name,
+                                   description='',
+                                   action='string:${object_url}/Base_viewDict',
+                                   condition='',
+                                   permission='View',
+                                   category='object_view',
+                                   priority=priority)
+        initial_action_list = portal_actions.listFilteredActionsFor(person)\
+            .get('object_view',[])
+        addCustomAction('test_before', -1)
+        max_priority = max([x.get('priority', 0) for x in initial_action_list])
+        addCustomAction('test_after', max_priority + 1)
+        final_action_list = portal_actions.listFilteredActionsFor(person)\
+            .get('object_view',[])
+        self.assertEquals(len(final_action_list), len(initial_action_list) + 2)
+        self.assertEquals(final_action_list[0]['id'], 'test_before')
+        self.assertEquals(final_action_list[-1]['id'], 'test_after')
+        # check that we have another portal types action in the middle
+        self.assertTrue('view' in [x['id'] for x in final_action_list[1:-1]])
+      finally:
+        index_list = []
+        action_list = portal_actions._cloneActions()
+        for action in action_list:
+          if action.id in ('test_before', 'test_after'):
+            index_list.append(action_list.index(action))
+        if len(index_list):
+          portal_actions.deleteActions(selections=index_list)
 
 class TestAccessControl(ERP5TypeTestCase):
   # Isolate test in a dedicaced class in order not to break other tests
-- 
2.30.9