Back to index

plone3  3.1.7
test_ActionProviderBase.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 ActionProviderBase module.
00014 
00015 $Id: test_ActionProviderBase.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from zope.testing.cleanup import cleanUp
00022 
00023 from Products.CMFCore.tests.base.dummy import DummySite
00024 from Products.CMFCore.tests.base.dummy import DummyTool
00025 from Products.CMFCore.tests.base.testcase import SecurityRequestTest
00026 
00027 #
00028 #   We have to import these here to make the "ugly sharing" test case go.
00029 #
00030 from Products.CMFCore.ActionProviderBase import ActionProviderBase
00031 from Products.CMFCore.ActionInformation import ActionInformation
00032 
00033 
00034 class DummyProvider(ActionProviderBase, DummyTool):
00035 
00036     _actions = ( ActionInformation( id='an_id'
00037                                   , title='A Title'
00038                                   , action=''
00039                                   , condition=''
00040                                   , permissions=()
00041                                   , category=''
00042                                   , visible=False
00043                                   ), )
00044 
00045 
00046 class DummyAction:
00047 
00048     def __init__( self, value ):
00049         self.value = value
00050 
00051     def clone( self ):
00052         return self.__class__( self.value )
00053 
00054     def __cmp__( self, other ):
00055         return ( cmp( type( self ), type( other ) )
00056               or cmp( self.__class__, other.__class__ )
00057               or cmp( self.value, other.value )
00058               or 0
00059                )
00060 
00061 
00062 class ActionProviderBaseTests(SecurityRequestTest):
00063 
00064     def setUp(self):
00065         SecurityRequestTest.setUp(self)
00066         self.site = DummySite('site').__of__(self.root)
00067         utool = self.site._setObject( 'portal_url', DummyTool() )
00068         mtool = self.site._setObject( 'portal_membership', DummyTool() )
00069 
00070     def tearDown(self):
00071         cleanUp()
00072         SecurityRequestTest.tearDown(self)
00073 
00074     def _makeProvider( self, dummy=0 ):
00075 
00076         klass = dummy and DummyProvider or ActionProviderBase
00077         return klass()
00078 
00079     def test_z2interfaces(self):
00080         from Interface.Verify import verifyClass
00081         from Products.CMFCore.interfaces.portal_actions \
00082                 import ActionProvider as IActionProvider
00083 
00084         verifyClass(IActionProvider, ActionProviderBase)
00085 
00086     def test_z3interfaces(self):
00087         from zope.interface.verify import verifyClass
00088         from Products.CMFCore.interfaces import IActionProvider
00089 
00090         verifyClass(IActionProvider, ActionProviderBase)
00091 
00092     def test_addAction( self ):
00093 
00094         apb = self._makeProvider()
00095         self.failIf( apb._actions )
00096         old_actions = apb._actions
00097         apb.addAction( id='foo'
00098                      , name='foo_action'
00099                      , action=''
00100                      , condition=''
00101                      , permission=''
00102                      , category=''
00103                      )
00104         self.failUnless( apb._actions )
00105         self.failIf( apb._actions is old_actions )
00106 
00107     def test_addActionBlankPermission(self):
00108         # make sure a blank permission gets stored as an empty tuple
00109         # '' and () and ('',) should mean no permission.
00110 
00111         apb = self._makeProvider()
00112         apb.addAction(id='foo',
00113                       name='foo_action',
00114                       action='',
00115                       condition='',
00116                       permission='',
00117                       category='',
00118                       )
00119         self.assertEqual(apb._actions[0].permissions, ())
00120 
00121         apb.addAction(id='foo',
00122                       name='foo_action',
00123                       action='',
00124                       condition='',
00125                       permission=('',),
00126                       category='',
00127                       )
00128         self.assertEqual(apb._actions[1].permissions, ())
00129 
00130         apb.addAction(id='foo',
00131                       name='foo_action',
00132                       action='',
00133                       condition='',
00134                       permission=(),
00135                       category='',
00136                       )
00137         self.assertEqual(apb._actions[2].permissions, ())
00138 
00139     def test_extractActionBlankPermission(self):
00140         # make sure a blank permission gets stored as an empty tuple
00141         # both () and ('',) should mean no permission.
00142 
00143         apb = self._makeProvider()
00144 
00145         index = 5
00146         properties = {
00147             'id_5': 'foo',
00148             'name_5': 'foo_action',
00149             'permission_5': (),
00150             }
00151         action = apb._extractAction(properties, index)
00152         self.assertEqual(action.permissions, ())
00153 
00154         index = 2
00155         properties = {
00156             'id_2': 'foo',
00157             'name_2': 'foo_action',
00158             'permission_2': ('',),
00159             }
00160         action = apb._extractAction(properties, index)
00161         self.assertEqual(action.permissions, ())
00162 
00163 
00164     def test_changeActions( self ):
00165 
00166         apb = DummyTool()
00167         old_actions = list( apb._actions )
00168 
00169         keys = [ ( 'id_%d', None )
00170                , ( 'name_%d', None )
00171                , ( 'action_%d', '' )
00172                , ( 'condition_%d', '' )
00173                , ( 'permission_%d', None )
00174                , ( 'category_%d', None )
00175                , ( 'visible_%d', 0 )
00176                ]
00177 
00178         properties = {}
00179         for i in range( len( old_actions ) ):
00180             for key, value in keys:
00181                 token = key % i
00182                 if value is None:
00183                     value = token
00184                 properties[ token ] = value
00185 
00186         apb.changeActions( properties=properties )
00187 
00188         marker = []
00189         for i in range( len( apb._actions ) ):
00190 
00191             for key, value in keys:
00192                 attr = key[ : -3 ]
00193 
00194                 if value is None:
00195                     value = key % i
00196 
00197                 if attr == 'name':    # WAAAA
00198                     attr = 'title'
00199 
00200                 if attr == 'permission':    # WAAAA
00201                     attr = 'permissions'
00202                     value = ( value, )
00203 
00204                 attr_value = getattr( apb._actions[i], attr, marker )
00205                 self.assertEqual( attr_value
00206                                 , value
00207                                 , '%s, %s != %s, %s'
00208                                   % ( attr, attr_value, key, value )  )
00209         self.failIf( apb._actions is old_actions )
00210 
00211     def test_deleteActions( self ):
00212 
00213         apb = self._makeProvider()
00214         apb._actions = tuple( map( DummyAction, [ '0', '1', '2' ] ) )
00215         apb.deleteActions( selections=(0,2) )
00216         self.assertEqual( len( apb._actions ), 1 )
00217         self.failUnless( DummyAction('1') in apb._actions )
00218 
00219     def test_DietersNastySharingBug( self ):
00220 
00221         one = self._makeProvider( dummy=1 )
00222         another = self._makeProvider( dummy=1 )
00223 
00224         def idify( x ):
00225             return id( x )
00226 
00227         old_ids = one_ids = map( idify, one.listActions() )
00228         another_ids = map( idify, another.listActions() )
00229 
00230         self.assertEqual( one_ids, another_ids )
00231 
00232         one.changeActions( { 'id_0'            : 'different_id'
00233                            , 'name_0'          : 'A Different Title'
00234                            , 'action_0'        : 'arise_shine'
00235                            , 'condition_0'     : 'always'
00236                            , 'permissions_0'   : 'granted'
00237                            , 'category_0'      : 'quality'
00238                            , 'visible_0'       : 1
00239                            } )
00240 
00241         one_ids = map( idify, one.listActions() )
00242         another_ids = map( idify, another.listActions() )
00243         self.failIf( one_ids == another_ids )
00244         self.assertEqual( old_ids, another_ids )
00245 
00246     def test_listActionInfos(self):
00247         wanted = [{'id': 'an_id', 'title': 'A Title', 'description': '',
00248                    'url': '', 'category': 'object', 'visible': False,
00249                    'available': True, 'allowed': True}]
00250 
00251         apb = self.site._setObject( 'portal_apb', self._makeProvider(1) )
00252         rval = apb.listActionInfos()
00253         self.assertEqual( rval, [] )
00254         rval = apb.listActionInfos(check_visibility=0)
00255         self.assertEqual( rval, wanted )
00256         rval = apb.listActionInfos('foo/another_id', check_visibility=0)
00257         self.assertEqual( rval, [] )
00258 
00259     def test_getActionObject(self):
00260         apb = self.site._setObject( 'portal_apb', self._makeProvider(1) )
00261         rval = apb.getActionObject('object/an_id')
00262         self.assertEqual(rval, apb._actions[0])
00263         rval = apb.getActionObject('object/not_existing_id')
00264         self.assertEqual(rval, None)
00265         self.assertRaises(ValueError, apb.getActionObject, 'wrong_format')
00266 
00267     def test_getActionInfo(self):
00268         wanted = {'id': 'an_id', 'title': 'A Title', 'description': '',
00269                   'url': '', 'category': 'object', 'visible': False,
00270                   'available': True, 'allowed': True}
00271 
00272         apb = self.site._setObject( 'portal_apb', self._makeProvider(1) )
00273         rval = apb.getActionInfo( ('object/an_id',) )
00274         self.assertEqual( rval, wanted )
00275         rval = apb.getActionInfo('object/an_id')
00276         self.assertEqual( rval, wanted )
00277         self.assertRaises( ValueError,
00278                            apb.getActionInfo,
00279                            'object/an_id', check_visibility=1 )
00280 
00281         # The following is nasty, but I want to make sure the ValueError
00282         # carries some useful information
00283         INVALID_ID = 'invalid_id'
00284         try:
00285             rval = apb.getActionInfo('object/%s' % INVALID_ID)
00286         except ValueError, e:
00287             message = e.args[0]
00288             detail = '"%s" does not offer action "%s"' % (message, INVALID_ID)
00289             self.failUnless(message.find(INVALID_ID) != -1, detail)
00290 
00291 
00292 def test_suite():
00293     return unittest.TestSuite((
00294         unittest.makeSuite(ActionProviderBaseTests),
00295         ))
00296 
00297 if __name__ == '__main__':
00298     unittest.main(defaultTest='test_suite')