Back to index

plone3  3.1.7
testActionsTool.py
Go to the documentation of this file.
00001 #
00002 # ActionsTool tests
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 
00007 from sets import Set
00008 from traceback import format_exception
00009 from zope.i18nmessageid.message import Message
00010 
00011 from Acquisition import Explicit
00012 from OFS.SimpleItem import Item
00013 from Products.CMFCore.ActionInformation import ActionInfo
00014 
00015 class ExplicitItem(Item, Explicit):
00016     '''Item without implicit acquisition'''
00017     id = 'dummy'
00018     meta_type = 'Dummy Item'
00019 
00020 expected_filtered_actions=Set(['site_actions', 'object', 'workflow', 'portal_tabs',
00021                                'global', 'object_buttons', 'document_actions',
00022                                'user', 'folder_buttons', 'folder'])
00023 
00024 
00025 class TestActionsTool(PloneTestCase.PloneTestCase):
00026 
00027     def afterSetUp(self):
00028         self.actions = self.portal.portal_actions
00029         self.portal.acl_users._doAddUser('user1', 'secret', ['Member'], [])
00030 
00031     def fail_tb(self, msg):
00032         """ special fail for capturing errors """
00033         import sys
00034         e, v, tb = sys.exc_info()
00035         tb = ''.join(format_exception(e, v, tb))
00036         self.fail("%s\n%s\n" % (msg, tb))
00037 
00038     def testAddAction(self):
00039         # addAction should work even though PloneTestCase patches _cloneActions
00040         action_infos = self.actions.listActions()
00041         length = len(action_infos)
00042         self.actions.addAction(id='foo',
00043                                name='foo_name',
00044                                action='foo_action',
00045                                condition='foo_condition',
00046                                permission='foo_permission',
00047                                category='foo_category',
00048                                visible=1)
00049         action_infos = self.actions.listActions()
00050         self.assertEqual(len(action_infos), length + 1)
00051         foo_action = self.actions.getActionObject('foo_category/foo')
00052         self.assertEqual(foo_action.id, 'foo')
00053         self.assertEqual(foo_action.title, 'foo_name')
00054         self.assertEqual(foo_action.permissions, ('foo_permission',))
00055         self.assertEqual(foo_action.category, 'foo_category')
00056 
00057     def testListFilteredActionsFor(self):
00058         self.assertEqual(Set(self.actions.listFilteredActionsFor(self.folder).keys()),
00059                          expected_filtered_actions)
00060 
00061     def testPortalTypesIsActionProvider(self):
00062         self.failUnless('portal_types' in self.actions.listActionProviders())
00063 
00064     def testMissingActionProvider(self):
00065         self.portal._delObject('portal_types')
00066         try:
00067             self.actions.listFilteredActionsFor(self.portal)
00068         except:
00069             self.fail_tb('Should not bomb out if a provider is missing')
00070 
00071     def testBrokenActionProvider(self):
00072         self.portal.portal_types = None
00073         try:
00074             self.actions.listFilteredActionsFor(self.portal)
00075         except:
00076             self.fail_tb('Should not bomb out if a provider is broken')
00077 
00078     def testMissingListActions(self):
00079         self.portal.portal_types = ExplicitItem()
00080         try:
00081             self.actions.listFilteredActionsFor(self.portal)
00082         except:
00083             self.fail_tb('Should not bomb out if a provider is broken')
00084 
00085     def testDocumentActionsPermissionBug(self):
00086         # Test to ensure that permissions for items categorized as
00087         # 'document_actions' have their permissions evaluated in the context
00088         # of the content object.
00089         self.actions.addAction(id='foo',
00090                                name='foo_name',
00091                                action='foo_action',
00092                                condition='',
00093                                permission='View',
00094                                category='document_actions',
00095                                visible=1)
00096         actions = self.actions.listFilteredActionsFor(self.folder)
00097         match = [a for a in actions['document_actions'] if a['id'] == 'foo']
00098         self.failUnless(match)
00099         self.portal.portal_workflow.doActionFor(self.folder, 'hide')
00100         self.login('user1')
00101         actions = self.actions.listFilteredActionsFor(self.folder)
00102         match = [a for a in actions.get('document_actions', []) if a['id'] == 'foo']
00103         self.failIf(match)
00104 
00105     def testActionNamespace(self):
00106         self.actions.addAction(id='foo',
00107                                name='foo_name',
00108                                action='string:${globals_view/isStructuralFolder}',
00109                                condition='',
00110                                permission='View',
00111                                category='folder',
00112                                visible=1)
00113 
00114         actions = self.actions.listFilteredActionsFor(self.folder)
00115         url = actions['folder'][0]['url']
00116 
00117     def testAllActionsAreRenderedAsMessages(self):
00118         actions = self.actions.listActions()
00119         for action in actions:
00120             info = ActionInfo(action, self.portal)
00121             self.failUnless(isinstance(info['title'], Message))
00122             self.failUnless(isinstance(info['description'], Message))
00123 
00124 
00125 def test_suite():
00126     from unittest import TestSuite, makeSuite
00127     suite = TestSuite()
00128     suite.addTest(makeSuite(TestActionsTool))
00129     return suite