Back to index

plone3  3.1.7
test_ActionInformation.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 ActionInformation module.
00014 
00015 $Id: test_ActionInformation.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from OFS.Folder import manage_addFolder
00022 from Products.PythonScripts.PythonScript import manage_addPythonScript
00023 
00024 from Products.CMFCore.Expression import createExprContext
00025 from Products.CMFCore.Expression import Expression
00026 from Products.CMFCore.testing import FunctionalZCMLLayer
00027 from Products.CMFCore.tests.base.dummy import DummyContent
00028 from Products.CMFCore.tests.base.dummy import DummySite
00029 from Products.CMFCore.tests.base.dummy import DummyTool as DummyMembershipTool
00030 from Products.CMFCore.tests.base.testcase import SecurityTest
00031 from Products.CMFCore.tests.base.testcase import TransactionalTest
00032 
00033 
00034 class ActionCategoryTests(unittest.TestCase):
00035 
00036     def _makeOne(self, *args, **kw):
00037         from Products.CMFCore.ActionInformation import ActionCategory
00038 
00039         return ActionCategory(*args, **kw)
00040 
00041     def test_z3interfaces(self):
00042         from zope.interface.verify import verifyClass
00043         from Products.CMFCore.ActionInformation import ActionCategory
00044         from Products.CMFCore.interfaces import IActionCategory
00045 
00046         verifyClass(IActionCategory, ActionCategory)
00047 
00048     def test_listActions(self):
00049         from Products.CMFCore.ActionInformation import Action
00050 
00051         ac = self._makeOne('foo')
00052         self.assertEqual( ac.listActions(), () )
00053 
00054         baz = Action('baz')
00055         ac._setObject('baz', baz)
00056         self.assertEqual( ac.listActions(), (baz,) )
00057 
00058 
00059 class ActionTests(unittest.TestCase):
00060 
00061     def _makeOne(self, *args, **kw):
00062         from Products.CMFCore.ActionInformation import Action
00063 
00064         return Action(*args, **kw)
00065 
00066     def test_z3interfaces(self):
00067         from zope.interface.verify import verifyClass
00068         from Products.CMFCore.ActionInformation import Action
00069         from Products.CMFCore.interfaces import IAction
00070 
00071         verifyClass(IAction, Action)
00072 
00073     def test_getInfoData_empty(self):
00074         WANTED = ( {'available': True, 'category': '', 'description': '',
00075                     'id': 'foo', 'icon': '', 'permissions': (), 'title': '',
00076                     'url': '', 'visible': True}, [] )
00077         a = self._makeOne('foo')
00078         self.assertEqual( a.getInfoData(), WANTED )
00079 
00080     def test_getInfoData_normal(self):
00081         a = self._makeOne('foo',
00082                           title='Foo Title',
00083                           description='Foo description.',
00084                           url_expr='string:${object_url}/foo_url',
00085                           icon_expr='string:foo_icon',
00086                           available_expr='',
00087                           permissions=('View',),
00088                           visible=False)
00089         WANTED = ( {'available': True, 'category': '',
00090                     'description': 'Foo description.',
00091                     'id': 'foo', 'icon': a.icon_expr_object,
00092                     'permissions': ('View',), 'title': 'Foo Title',
00093                     'url': a.url_expr_object, 'visible': False},
00094                    ['url', 'icon'] )
00095         self.assertEqual( a.getInfoData(), WANTED )
00096 
00097     def test_manage_propertiesForm_allows_adding(self):
00098         from OFS.SimpleItem import SimpleItem
00099         def _header(*args, **kw):
00100             return 'HEADER'
00101         def _footer(*args, **kw):
00102             return 'HEADER'
00103         container = SimpleItem()
00104 
00105         container.REQUEST = request = DummyRequest()
00106         request.set('manage_page_header', _header)
00107         request.set('manage_page_footer', _footer)
00108         request.set('BASEPATH1', '/one/two')
00109         request.set('URL1', '/one/two')
00110         request._steps = ['one', 'two']
00111 
00112         prd = {'ac_permissions': ('a', 'b')}
00113         container._getProductRegistryData = prd.get
00114 
00115         a = self._makeOne('extensible').__of__(container)
00116         form_html = a.manage_propertiesForm(request)
00117 
00118         self.failUnless('value=" Add "' in form_html)
00119 
00120 class DummyRequest:
00121     def __init__(self):
00122         self._data = {}
00123     def set(self, k, v):
00124         self._data[k] = v
00125     def get(self, k, default):
00126         return self._data.get(k, default)
00127     def __getitem__(self, k):
00128         return self._data[k]
00129     def __len__(self):
00130         return len(self._data)
00131 
00132 class ActionInfoTests(unittest.TestCase):
00133 
00134     def _makeOne(self, *args, **kw):
00135         from Products.CMFCore.ActionInformation import ActionInfo
00136 
00137         return ActionInfo(*args, **kw)
00138 
00139     def test_z2interfaces(self):
00140         from Interface.Verify import verifyClass
00141         from Products.CMFCore.ActionInformation import ActionInfo
00142         from Products.CMFCore.interfaces.portal_actions \
00143                 import ActionInfo as IActionInfo
00144 
00145         verifyClass(IActionInfo, ActionInfo)
00146 
00147     def test_z3interfaces(self):
00148         from zope.interface.verify import verifyClass
00149         from Products.CMFCore.ActionInformation import ActionInfo
00150         from Products.CMFCore.interfaces import IActionInfo
00151 
00152         verifyClass(IActionInfo, ActionInfo)
00153 
00154     def test_create_from_Action(self):
00155         from Products.CMFCore.ActionInformation import Action
00156 
00157         WANTED = {'allowed': True, 'available': True, 'category': '',
00158                   'description': '', 'icon': '', 'id': 'foo', 'title': '',
00159                   'url': '', 'visible': True}
00160 
00161         action = Action(id='foo')
00162         ec = None
00163         ai = self._makeOne(action, ec)
00164 
00165         self.assertEqual( ai['id'], WANTED['id'] )
00166         self.assertEqual( ai['title'], WANTED['title'] )
00167         self.assertEqual( ai['description'], WANTED['description'] )
00168         self.assertEqual( ai['url'], WANTED['url'] )
00169         self.assertEqual( ai['category'], WANTED['category'] )
00170         self.assertEqual( ai['visible'], WANTED['visible'] )
00171         self.assertEqual( ai['available'], WANTED['available'] )
00172         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00173         self.assertEqual( ai, WANTED )
00174 
00175     def test_create_from_ActionInformation(self):
00176         from Products.CMFCore.ActionInformation import ActionInformation
00177 
00178         WANTED = {'allowed': True, 'available': True, 'category': 'object',
00179                   'description': '', 'id': 'foo', 'title': 'foo', 'url': '',
00180                   'visible': True}
00181 
00182         action = ActionInformation(id='foo')
00183         ec = None
00184         ai = self._makeOne(action, ec)
00185 
00186         self.assertEqual( ai['id'], WANTED['id'] )
00187         self.assertEqual( ai['title'], WANTED['title'] )
00188         self.assertEqual( ai['description'], WANTED['description'] )
00189         self.assertEqual( ai['url'], WANTED['url'] )
00190         self.assertEqual( ai['category'], WANTED['category'] )
00191         self.assertEqual( ai['visible'], WANTED['visible'] )
00192         self.assertEqual( ai['available'], WANTED['available'] )
00193         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00194         self.assertEqual( ai, WANTED )
00195 
00196     def test_create_from_dict(self):
00197         WANTED = {'allowed': True, 'available': True, 'category': 'object',
00198                   'id': 'foo', 'title': 'foo', 'url': '', 'visible': True}
00199 
00200         action = {'name': 'foo', 'url': ''}
00201         ec = None
00202         ai = self._makeOne(action, ec)
00203 
00204         self.assertEqual( ai['id'], WANTED['id'] )
00205         self.assertEqual( ai['title'], WANTED['title'] )
00206         self.assertEqual( ai['url'], WANTED['url'] )
00207         self.assertEqual( ai['category'], WANTED['category'] )
00208         self.assertEqual( ai['visible'], WANTED['visible'] )
00209         self.assertEqual( ai['available'], WANTED['available'] )
00210         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00211         self.assertEqual( ai, WANTED )
00212 
00213 
00214 class ActionInfoSecurityTests(SecurityTest):
00215 
00216     def setUp(self):
00217         SecurityTest.setUp(self)
00218         self.site = DummySite('site').__of__(self.root)
00219         self.site._setObject( 'portal_membership', DummyMembershipTool() )
00220 
00221     def _makeOne(self, *args, **kw):
00222         from Products.CMFCore.ActionInformation import ActionInfo
00223 
00224         return ActionInfo(*args, **kw)
00225 
00226     def test_create_from_dict(self):
00227         WANTED = {'allowed': True, 'available': True, 'category': 'object',
00228                   'id': 'foo', 'title': 'foo', 'url': '', 'visible': True}
00229 
00230         action = {'name': 'foo', 'url': '', 'permissions': ('View',)}
00231         ec = createExprContext(self.site, self.site, None)
00232         ai = self._makeOne(action, ec)
00233 
00234         self.assertEqual( ai['id'], WANTED['id'] )
00235         self.assertEqual( ai['title'], WANTED['title'] )
00236         self.assertEqual( ai['url'], WANTED['url'] )
00237         self.assertEqual( ai['category'], WANTED['category'] )
00238         self.assertEqual( ai['visible'], WANTED['visible'] )
00239         self.assertEqual( ai['available'], WANTED['available'] )
00240         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00241         self.assertEqual( ai, WANTED )
00242 
00243     def test_category_object(self):
00244         # Permissions for action category 'object*' should be
00245         # evaluated in object context.
00246         manage_addFolder(self.site, 'actions_dummy')
00247         self.object = self.site.actions_dummy
00248         self.object.manage_permission('View', [], acquire=0)
00249 
00250         WANTED = {'allowed': False, 'category': 'object'}
00251 
00252         action = {'name': 'foo', 'url': '', 'permissions': ('View',)}
00253         ec = createExprContext(self.site, self.site, self.object)
00254         ai = self._makeOne(action, ec)
00255 
00256         self.assertEqual( ai['category'], WANTED['category'] )
00257         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00258 
00259     def test_category_folder(self):
00260         # Permissions for action category 'folder*' should be
00261         # evaluated in folder context.
00262         manage_addFolder(self.site, 'actions_dummy')
00263         self.folder = self.site.actions_dummy
00264         self.folder.manage_permission('View', [], acquire=0)
00265 
00266         WANTED = {'allowed': False, 'category': 'folder'}
00267 
00268         action = {'name': 'foo', 'url': '', 'permissions': ('View',)}
00269         ec = createExprContext(self.folder, self.site, None)
00270         ai = self._makeOne(action, ec)
00271         ai['category'] = 'folder' # pfff
00272 
00273         self.assertEqual( ai['category'], WANTED['category'] )
00274         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00275 
00276     def test_category_workflow(self):
00277         # Permissions for action category 'workflow*' should be
00278         # evaluated in object context.
00279         manage_addFolder(self.site, 'actions_dummy')
00280         self.object = self.site.actions_dummy
00281         self.object.manage_permission('View', [], acquire=0)
00282 
00283         WANTED = {'allowed': False, 'category': 'workflow'}
00284 
00285         action = {'name': 'foo', 'url': '', 'permissions': ('View',)}
00286         ec = createExprContext(self.site, self.site, self.object)
00287         ai = self._makeOne(action, ec)
00288         ai['category'] = 'workflow' # pfff
00289 
00290         self.assertEqual( ai['category'], WANTED['category'] )
00291         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00292 
00293     def test_category_document(self):
00294         # Permissions for action category 'document*' should be
00295         # evaluated in object context (not in portal context).
00296         manage_addFolder(self.site, 'actions_dummy')
00297         self.object = self.site.actions_dummy
00298         self.object.manage_permission('View', [], acquire=0)
00299 
00300         WANTED = {'allowed': False, 'category': 'document'}
00301 
00302         action = {'name': 'foo', 'url': '', 'permissions': ('View',)}
00303         ec = createExprContext(self.site, self.site, self.object)
00304         ai = self._makeOne(action, ec)
00305         ai['category'] = 'document' # pfff
00306 
00307         self.assertEqual( ai['category'], WANTED['category'] )
00308         self.assertEqual( ai['allowed'], WANTED['allowed'] )
00309 
00310     def test_copy(self):
00311         action = {'name': 'foo', 'url': '', 'permissions': ('View',)}
00312         ec = createExprContext(self.site, self.site, None)
00313         ai = self._makeOne(action, ec)
00314         ai2 = ai.copy()
00315 
00316         self.assertEqual( ai._lazy_keys, ['allowed'] )
00317         self.assertEqual( ai2._lazy_keys, ['allowed'] )
00318         self.failIf( ai2._lazy_keys is ai._lazy_keys )
00319         self.assertEqual( ai['allowed'], True )
00320         self.assertEqual( ai2['allowed'], True )
00321 
00322 
00323 class ActionInformationTests(TransactionalTest):
00324 
00325     layer = FunctionalZCMLLayer
00326 
00327     def setUp(self):
00328         TransactionalTest.setUp(self)
00329 
00330         root = self.root
00331         root._setObject('portal', DummyContent('portal', 'url_portal'))
00332         portal = self.portal = root.portal
00333         portal.portal_membership = DummyMembershipTool()
00334         self.folder = DummyContent('foo', 'url_foo')
00335         self.object = DummyContent('bar', 'url_bar')
00336 
00337     def _makeOne(self, *args, **kw):
00338         from Products.CMFCore.ActionInformation import ActionInformation
00339 
00340         return ActionInformation(*args, **kw)
00341 
00342     def test_z3interfaces(self):
00343         from zope.interface.verify import verifyClass
00344         from Products.CMFCore.ActionInformation import ActionInformation
00345         from Products.CMFCore.interfaces import IAction
00346 
00347         verifyClass(IAction, ActionInformation)
00348 
00349     def test_basic_construction(self):
00350         ai = self._makeOne(id='view')
00351 
00352         self.assertEqual(ai.getId(), 'view')
00353         self.assertEqual(ai.Title(), 'view')
00354         self.assertEqual(ai.Description(), '')
00355         self.assertEqual(ai.getCondition(), '')
00356         self.assertEqual(ai.getActionExpression(), '')
00357         self.assertEqual(ai.getVisibility(), 1)
00358         self.assertEqual(ai.getCategory(), 'object')
00359         self.assertEqual(ai.getPermissions(), ())
00360 
00361     def test_editing(self):
00362         ai = self._makeOne(id='view', category='folder')
00363         ai.edit(id='new_id', title='blah')
00364 
00365         self.assertEqual(ai.getId(), 'new_id')
00366         self.assertEqual(ai.Title(), 'blah')
00367         self.assertEqual(ai.Description(), '')
00368         self.assertEqual(ai.getCondition(), '')
00369         self.assertEqual(ai.getActionExpression(), '')
00370         self.assertEqual(ai.getVisibility(), 1)
00371         self.assertEqual(ai.getCategory(), 'folder')
00372         self.assertEqual(ai.getPermissions(), ())
00373 
00374     def test_setActionExpression_with_string_prefix(self):
00375         from Products.CMFCore.Expression import Expression
00376         ai = self._makeOne(id='view', category='folder')
00377         ai.setActionExpression('string:blah')
00378         self.failUnless(isinstance(ai.action,Expression))
00379         self.assertEqual(ai.getActionExpression(), 'string:blah')
00380 
00381     def test_construction_with_Expressions(self):
00382         ai = self._makeOne( id='view',
00383                             title='View',
00384                             action=Expression(text='view'),
00385                             condition=Expression(text='member'),
00386                             category='global',
00387                             visible=False )
00388 
00389         self.assertEqual(ai.getId(), 'view')
00390         self.assertEqual(ai.Title(), 'View')
00391         self.assertEqual(ai.Description(), '')
00392         self.assertEqual(ai.getCondition(), 'member')
00393         self.assertEqual(ai.getActionExpression(), 'string:${object_url}/view')
00394         self.assertEqual(ai.getVisibility(), 0)
00395         self.assertEqual(ai.getCategory(), 'global')
00396         self.assertEqual(ai.getPermissions(), ())
00397 
00398     def test_Condition(self):
00399         portal = self.portal
00400         folder = self.folder
00401         object = self.object
00402         ai = self._makeOne( id='view',
00403                             title='View',
00404                             action=Expression(text='view'),
00405                             condition=Expression(text='member'),
00406                             category='global',
00407                             visible=True )
00408         ec = createExprContext(folder, portal, object)
00409 
00410         self.failIf(ai.testCondition(ec))
00411 
00412     def test_Condition_PathExpression(self):
00413         portal = self.portal
00414         folder = self.folder
00415         object = self.object
00416         manage_addPythonScript(self.root, 'test_script')
00417         script = self.root.test_script
00418         script.ZPythonScript_edit('', 'return context.getId()')
00419         ai = self._makeOne( id='view',
00420                             title='View',
00421                             action=Expression(text='view'),
00422                             condition=Expression(text='portal/test_script'),
00423                             category='global',
00424                             visible=True )
00425         ec = createExprContext(folder, portal, object)
00426 
00427         self.failUnless(ai.testCondition(ec))
00428 
00429     def test_getInfoData_empty(self):
00430         WANTED = ( {'available': True, 'category': 'object',
00431                     'description': '', 'id': 'foo', 'permissions': (),
00432                     'title': 'foo', 'url': '', 'visible': True}, [] )
00433         a = self._makeOne('foo')
00434         self.assertEqual( a.getInfoData(), WANTED )
00435 
00436     def test_getInfoData_normal(self):
00437         a = self._makeOne('foo',
00438                           title='Foo Title',
00439                           description='Foo description.',
00440                           action='string:${object_url}/foo_url',
00441                           condition='',
00442                           permissions=('View',),
00443                           visible=False)
00444         WANTED = ( {'available': True, 'category': 'object',
00445                     'description': 'Foo description.', 'id': 'foo',
00446                     'permissions': ('View',), 'title': 'Foo Title',
00447                     'url': a._getActionObject(), 'visible': False},
00448                    ['url'] )
00449         self.assertEqual( a.getInfoData(), WANTED )
00450 
00451 
00452 def test_suite():
00453     return unittest.TestSuite((
00454         unittest.makeSuite(ActionCategoryTests),
00455         unittest.makeSuite(ActionTests),
00456         unittest.makeSuite(ActionInfoTests),
00457         unittest.makeSuite(ActionInfoSecurityTests),
00458         unittest.makeSuite(ActionInformationTests),
00459         ))
00460 
00461 if __name__ == '__main__':
00462     from Products.CMFCore.testing import run
00463     run(test_suite())