Back to index

plone3  3.1.7
test_ActionIconsTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2003 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 ActionIconsTool module.
00014 
00015 $Id: test_ActionIconsTool.py 73137 2007-03-11 12:43:51Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from zope.interface.verify import verifyClass
00022 
00023 
00024 class ActionIconToolTests(unittest.TestCase):
00025 
00026     def _makeOne( self, *args, **kw ):
00027 
00028         from Products.CMFActionIcons.ActionIconsTool import ActionIconsTool
00029 
00030         return ActionIconsTool( *args, **kw )
00031 
00032     def test_interfaces(self):
00033         from Products.CMFActionIcons.ActionIconsTool import ActionIconsTool
00034         from Products.CMFActionIcons.interfaces import IActionIconsTool
00035 
00036         verifyClass(IActionIconsTool, ActionIconsTool)
00037 
00038     def test_empty( self ):
00039 
00040         tool = self._makeOne()
00041 
00042         self.assertEqual( len( tool.listActionIcons() ), 0 )
00043         self.assertRaises( KeyError, tool.getActionIcon, 'foo', 'bar' )
00044         self.assertEqual( tool.queryActionIcon( 'foo', 'bar' ), None )
00045         self.assertEqual( tool.queryActionIcon( 'foo', 'bar', 'baz' ), 'baz' )
00046         self.assertRaises( KeyError, tool.removeActionIcon, 'foo', 'bar' )
00047 
00048     def test_addActionIcon( self ):
00049 
00050         tool = self._makeOne()
00051 
00052         tool.addActionIcon( 'foo', 'bar', 'qux' )
00053 
00054         icons = tool.listActionIcons()
00055         self.assertEqual( len( icons ), 1 )
00056 
00057         icon = icons[0]
00058         self.assertEqual( icon.getCategory(), 'foo' )
00059         self.assertEqual( icon.getActionId(), 'bar' )
00060         self.assertEqual( icon.getExpression(), 'qux' )
00061         self.assertEqual( icon.getIconURL(), 'qux' )
00062 
00063         self.assertRaises( KeyError, tool.getActionIcon, 'foo', 'baz' )
00064         self.assertEqual( tool.getActionIcon( 'foo', 'bar' ), 'qux' )
00065         self.assertEqual( tool.queryActionIcon( 'foo', 'bar' ), 'qux' )
00066         self.assertEqual( tool.queryActionIcon( 'foo', 'bar', 'baz' ), 'qux' )
00067 
00068     def test_addActionIcon_duplicate( self ):
00069 
00070         tool = self._makeOne()
00071 
00072         tool.addActionIcon( 'foo', 'bar', 'qux' )
00073         self.assertRaises( KeyError, tool.addActionIcon, 'foo', 'bar', 'qux' )
00074 
00075     def test_addActionIcon_multiple( self ):
00076 
00077         tool = self._makeOne()
00078 
00079         tool.addActionIcon( 'foo', 'bar', 'qux' )
00080         tool.addActionIcon( 'foo', 'baz', 'spam' )
00081         tool.addActionIcon( 'abc', 'def', 'ghi' )
00082 
00083         icons = tool.listActionIcons()
00084         self.assertEqual( len( icons ), 3 )
00085 
00086         icon = icons[0]
00087         self.assertEqual( icon.getCategory(), 'foo' )
00088         self.assertEqual( icon.getActionId(), 'bar' )
00089         self.assertEqual( icon.getExpression(), 'qux' )
00090         self.assertEqual( icon.getIconURL(), 'qux' )
00091 
00092         icon = icons[1]
00093         self.assertEqual( icon.getCategory(), 'foo' )
00094         self.assertEqual( icon.getActionId(), 'baz' )
00095         self.assertEqual( icon.getExpression(), 'spam' )
00096         self.assertEqual( icon.getIconURL(), 'spam' )
00097 
00098         icon = icons[2]
00099         self.assertEqual( icon.getCategory(), 'abc' )
00100         self.assertEqual( icon.getActionId(), 'def' )
00101         self.assertEqual( icon.getExpression(), 'ghi' )
00102         self.assertEqual( icon.getIconURL(), 'ghi' )
00103 
00104     def test_updateActionIcon( self ):
00105 
00106         tool = self._makeOne()
00107 
00108         tool.addActionIcon( 'foo', 'bar', 'qux' )
00109 
00110         self.assertEqual( tool.getActionIcon( 'foo', 'bar' ), 'qux' )
00111         self.assertEqual( tool.queryActionIcon( 'foo', 'bar' ), 'qux' )
00112         self.assertEqual( tool.queryActionIcon( 'foo', 'bar', 'baz' ), 'qux' )
00113 
00114         tool.updateActionIcon( 'foo', 'bar', 'bam' )
00115 
00116         self.assertEqual( tool.getActionIcon( 'foo', 'bar' ), 'bam' )
00117         self.assertEqual( tool.queryActionIcon( 'foo', 'bar' ), 'bam' )
00118         self.assertEqual( tool.queryActionIcon( 'foo', 'bar', 'baz' ), 'bam' )
00119 
00120     def test_updateActionIcon_emptyiconexpr( self ):
00121 
00122         # CMF Collector # 239
00123 
00124         tool = self._makeOne()
00125         tool.addActionIcon( 'foo', 'bar', 'qux' )
00126         tool.updateActionIcon( 'foo', 'bar', '' )
00127         tool.updateActionIcon( 'foo', 'bar', 'bam' )
00128 
00129         self.assertEqual( tool.getActionIcon( 'foo', 'bar' ), 'bam' )
00130         self.assertEqual( tool.queryActionIcon( 'foo', 'bar' ), 'bam' )
00131         self.assertEqual( tool.queryActionIcon( 'foo', 'bar', 'baz' ), 'bam' )
00132 
00133     def test_updateActionIcon_nonesuch( self ):
00134 
00135         tool = self._makeOne()
00136         self.assertRaises( KeyError
00137                          , tool.updateActionIcon, 'foo', 'baz', 'qux' )
00138 
00139     def test_removeActionIcon( self ):
00140 
00141         tool = self._makeOne()
00142 
00143         tool.addActionIcon( 'foo', 'bar', 'qux' )
00144         tool.addActionIcon( 'foo', 'baz', 'spam' )
00145         tool.addActionIcon( 'abc', 'def', 'ghi' )
00146 
00147         tool.removeActionIcon( 'foo', 'baz' )
00148 
00149         icons = tool.listActionIcons()
00150         self.assertEqual( len( icons ), 2 )
00151 
00152         icon = icons[0]
00153         self.assertEqual( icon.getCategory(), 'foo' )
00154         self.assertEqual( icon.getActionId(), 'bar' )
00155         self.assertEqual( icon.getExpression(), 'qux' )
00156         self.assertEqual( icon.getIconURL(), 'qux' )
00157 
00158         icon = icons[1]
00159         self.assertEqual( icon.getCategory(), 'abc' )
00160         self.assertEqual( icon.getActionId(), 'def' )
00161         self.assertEqual( icon.getExpression(), 'ghi' )
00162         self.assertEqual( icon.getIconURL(), 'ghi' )
00163 
00164         self.assertEqual( tool.getActionIcon( 'foo', 'bar' ), 'qux' )
00165         self.assertRaises( KeyError, tool.getActionIcon, 'foo', 'baz' )
00166         self.assertEqual( tool.getActionIcon( 'abc', 'def' ), 'ghi' )
00167 
00168     def test_removeActionIcon_nonesuch( self ):
00169 
00170         tool = self._makeOne()
00171 
00172         tool.addActionIcon( 'foo', 'bar', 'qux' )
00173         tool.addActionIcon( 'foo', 'baz', 'spam' )
00174         tool.addActionIcon( 'abc', 'def', 'ghi' )
00175 
00176         self.assertRaises( KeyError, tool.removeActionIcon, 'jkl', 'mno' )
00177 
00178     def test_removeActionIcon_noIconURL( self ):
00179         # Regression test, issue #333
00180         tool = self._makeOne()
00181         tool.addActionIcon( 'foo', 'bar', '' )
00182 
00183         tool.removeActionIcon( 'foo', 'bar' )
00184         self.assertEqual(len(tool.listActionIcons()), 0)
00185 
00186     def test_updateActionDicts( self ):
00187 
00188         tool = self._makeOne()
00189 
00190         tool.addActionIcon( 'foo', 'bar', 'qux', 'FooBar' )
00191         tool.addActionIcon( 'foo', 'baz', 'spam', 'FooBaz', 1 )
00192         tool.addActionIcon( 'abc', 'def', 'ghi' )
00193 
00194         sod = { 'foo' : ( { 'id' : 'bar' }
00195                         , { 'id' : 'baz', 'icon' : 'qwerty' }
00196                         , { 'id' : 'bam' }
00197                         )
00198               , 'abc' : ( { 'id' : 'def', 'title' : 'DEF' }
00199                         ,
00200                         )
00201               }
00202 
00203         sod2 = tool.updateActionDicts( sod )
00204 
00205         self.assertEqual( len( sod2 ), len( sod ) )
00206 
00207         self.assertEqual( len( sod2['foo'] ), len( sod['foo'] ) )
00208         self.assertEqual( sod2['foo'][0]['icon'], 'qux' )
00209         self.assertEqual( sod2['foo'][0]['title'], 'FooBar' )
00210         self.assertEqual( sod2['foo'][0]['priority'], 0 )
00211 
00212         self.failIf( sod2['foo'][1].has_key( 'icon' ) ) # sorted higher!
00213 
00214         self.assertEqual( sod2['foo'][2]['icon'], 'spam' )
00215         self.assertEqual( sod2['foo'][2]['title'], 'FooBaz' )
00216         self.assertEqual( sod2['foo'][2]['priority'], 1 )
00217 
00218         self.assertEqual( len( sod2['abc'] ), len( sod['abc'] ) )
00219         self.assertEqual( sod2['abc'][0]['icon'], 'ghi' )
00220         self.assertEqual( sod2['abc'][0]['title'], 'DEF' )
00221 
00222         sod3 = tool( sod )
00223         self.assertEqual( sod2, sod3 )
00224 
00225 
00226 def test_suite():
00227     return unittest.TestSuite((
00228         unittest.makeSuite(ActionIconToolTests),
00229         ))
00230 
00231 if __name__ == '__main__':
00232     unittest.main(defaultTest='test_suite')