Back to index

plone3  3.1.7
test_ActionsTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for ActionsTool module.
00014 
00015 $Id: test_ActionsTool.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from zope.component import getSiteManager
00022 from zope.testing.cleanup import cleanUp
00023 
00024 from Products.CMFCore.ActionInformation import Action
00025 from Products.CMFCore.ActionInformation import ActionCategory
00026 from Products.CMFCore.ActionInformation import ActionInformation
00027 from Products.CMFCore.Expression import Expression
00028 from Products.CMFCore.interfaces import ISiteRoot
00029 from Products.CMFCore.MembershipTool import MembershipTool
00030 from Products.CMFCore.tests.base.testcase import SecurityRequestTest
00031 from Products.CMFCore.URLTool import URLTool
00032 
00033 
00034 class ActionsToolTests(unittest.TestCase):
00035 
00036     def _getTargetClass(self):
00037         from Products.CMFCore.ActionsTool import ActionsTool
00038 
00039         return ActionsTool
00040 
00041     def _makeOne(self, *args, **kw):
00042         return self._getTargetClass()(*args, **kw)
00043 
00044     def test_z2interfaces(self):
00045         from Interface.Verify import verifyClass
00046         from Products.CMFCore.interfaces.portal_actions \
00047                 import ActionProvider as IActionProvider
00048         from Products.CMFCore.interfaces.portal_actions \
00049                 import portal_actions as IActionsTool
00050 
00051         verifyClass(IActionProvider, self._getTargetClass())
00052         verifyClass(IActionsTool, self._getTargetClass())
00053 
00054     def test_z3interfaces(self):
00055         from zope.interface.verify import verifyClass
00056         from Products.CMFCore.interfaces import IActionProvider
00057         from Products.CMFCore.interfaces import IActionsTool
00058 
00059         verifyClass(IActionProvider, self._getTargetClass())
00060         verifyClass(IActionsTool, self._getTargetClass())
00061 
00062     def test_listActionProviders(self):
00063         tool = self._makeOne()
00064         tool.action_providers = ('portal_actions',)
00065         self.assertEqual(tool.listActionProviders(), ('portal_actions',))
00066 
00067     def test_addActionProvider(self):
00068         tool = self._makeOne()
00069         tool.foo = 'acquirable attribute'
00070         tool.portal_url = 'acquirable attribute'
00071         tool.action_providers = ('portal_actions',)
00072         tool.addActionProvider('foo')
00073         self.assertEqual(tool.listActionProviders(),
00074                           ('portal_actions', 'foo'))
00075         tool.addActionProvider('portal_url')
00076         tool.addActionProvider('foo')
00077         self.assertEqual(tool.listActionProviders(),
00078                           ('portal_actions', 'foo', 'portal_url'))
00079 
00080     def test_deleteActionProvider(self):
00081         tool = self._makeOne()
00082         tool.action_providers = ('portal_actions', 'foo')
00083         tool.deleteActionProvider('foo')
00084         self.assertEqual(tool.listActionProviders(), ('portal_actions',))
00085 
00086     def test_getActionObject(self):
00087         tool = self._makeOne()
00088         tool._setObject('object', ActionCategory('object'))
00089         tool.object._setObject('newstyle_id', Action('newstyle_id'))
00090         tool.addAction('an_id', 'name', '', '', '', 'object')
00091         rval = tool.getActionObject('object/an_id')
00092         self.assertEqual(rval, tool._actions[0])
00093         rval = tool.getActionObject('object/newstyle_id')
00094         self.assertEqual(rval, None)
00095         rval = tool.getActionObject('object/not_existing_id')
00096         self.assertEqual(rval, None)
00097         self.assertRaises(ValueError, tool.getActionObject, 'wrong_format')
00098 
00099 
00100 class ActionsToolSecurityRequestTests(SecurityRequestTest):
00101 
00102     def _getTargetClass(self):
00103         from Products.CMFCore.ActionsTool import ActionsTool
00104 
00105         return ActionsTool
00106 
00107     def _makeOne(self, *args, **kw):
00108         return self._getTargetClass()(*args, **kw)
00109 
00110     def setUp(self):
00111         SecurityRequestTest.setUp(self)
00112 
00113         root = self.root
00114         sm = getSiteManager()
00115         sm.registerUtility(root, ISiteRoot)
00116         root._setObject( 'portal_actions', self._makeOne() )
00117         root._setObject( 'portal_url', URLTool() )
00118         root._setObject( 'foo', URLTool() )
00119         root._setObject('portal_membership', MembershipTool())
00120         self.tool = root.portal_actions
00121         self.tool.action_providers = ('portal_actions',)
00122 
00123     def test_listActionInformationActions(self):
00124         # Check that listFilteredActionsFor works for objects that return
00125         # ActionInformation objects
00126         root = self.root
00127         tool = self.tool
00128         tool._actions = (
00129               ActionInformation(id='folderContents',
00130                                 title='Folder contents',
00131                                 action=Expression(text='string:'
00132                                              '${folder_url}/folder_contents'),
00133                                 condition=Expression(text='python: '
00134                                                       'folder is not object'),
00135                                 permissions=('List folder contents',),
00136                                 category='folder',
00137                                 visible=1)
00138             ,
00139             )
00140         self.assertEqual(tool.listFilteredActionsFor(root.foo),
00141                          {'workflow': [],
00142                           'user': [],
00143                           'object': [],
00144                           'folder': [{'id': 'folderContents',
00145                                       'url': 'http://nohost/folder_contents',
00146                                       'title': 'Folder contents',
00147                                       'description': '',
00148                                       'visible': True,
00149                                       'available': True,
00150                                       'allowed': True,
00151                                       'category': 'folder'}],
00152                           'global': []})
00153 
00154     def tearDown(self):
00155         cleanUp()
00156         SecurityRequestTest.tearDown(self)
00157 
00158 
00159 def test_suite():
00160     return unittest.TestSuite((
00161         unittest.makeSuite(ActionsToolTests),
00162         unittest.makeSuite(ActionsToolSecurityRequestTests),
00163         ))
00164 
00165 if __name__ == '__main__':
00166     unittest.main(defaultTest='test_suite')