Back to index

plone3  3.1.7
test_exportimport.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 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 """ActionIconsTool setup handler unit tests.
00014 
00015 $Id: test_exportimport.py 75863 2007-05-21 17:40:54Z hannosch $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from zope.component import getSiteManager
00022 
00023 from Products.CMFActionIcons.interfaces import IActionIconsTool
00024 from Products.CMFCore.testing import ExportImportZCMLLayer
00025 from Products.CMFCore.tests.base.utils import _setUpDefaultTraversable
00026 from Products.GenericSetup.tests.common import BaseRegistryTests
00027 from Products.GenericSetup.tests.common import DummyExportContext
00028 from Products.GenericSetup.tests.common import DummyImportContext
00029 
00030 
00031 class _ActionIconsToolSetup(BaseRegistryTests):
00032 
00033     CATEGORY = 'testing'
00034     ACTION_ID = "test_action"
00035     TITLE = "Action Title"
00036     PRIORITY = 60
00037     ICON_EXPR = 'test_icon'
00038 
00039     _EMPTY_EXPORT = """\
00040 <?xml version="1.0"?>
00041 <action-icons>
00042 </action-icons>
00043 """
00044 
00045     _EMPTY_I18N_EXPORT = """\
00046 <?xml version="1.0"?>
00047 <action-icons xmlns:i18n="http://xml.zope.org/namespaces/i18n"
00048     i18n:domain="cmf">
00049 </action-icons>
00050 """
00051 
00052     _WITH_ICON_EXPORT = """\
00053 <?xml version="1.0"?>
00054 <action-icons>
00055  <action-icon
00056     category="%s"
00057     action_id="%s"
00058     title="%s"
00059     priority="%d"
00060     icon_expr="%s"
00061     />
00062 </action-icons>
00063 """ % (CATEGORY,
00064        ACTION_ID,
00065        TITLE,
00066        PRIORITY,
00067        ICON_EXPR,
00068       )
00069 
00070     _WITH_I18N_ICON_EXPORT = """\
00071 <?xml version="1.0"?>
00072 <action-icons xmlns:i18n="http://xml.zope.org/namespaces/i18n"
00073     i18n:domain="cmf">
00074 <action-icon
00075   category="%s"
00076   action_id="%s"
00077   title="%s"
00078   priority="%d"
00079   icon_expr="%s"
00080   i18n:attributes="title"
00081   />
00082 </action-icons>
00083 """ % (CATEGORY,
00084        ACTION_ID,
00085        TITLE,
00086        PRIORITY,
00087        ICON_EXPR,
00088       )
00089 
00090     def _initSite(self, with_icon=False):
00091         from OFS.Folder import Folder
00092         from Products.CMFActionIcons.ActionIconsTool import ActionIconsTool
00093 
00094         _setUpDefaultTraversable()
00095 
00096         self.root.site = Folder(id='site')
00097         site = self.root.site
00098         tool = ActionIconsTool()
00099         site._setObject( tool.getId(), tool )
00100 
00101         sm = getSiteManager()
00102         sm.registerUtility(site.portal_actionicons, IActionIconsTool)
00103 
00104         if with_icon:
00105             tool.addActionIcon( category=self.CATEGORY
00106                               , action_id=self.ACTION_ID
00107                               , title=self.TITLE
00108                               , priority=self.PRIORITY
00109                               , icon_expr=self.ICON_EXPR
00110                               )
00111         return site
00112 
00113 
00114 class ActionIconsToolExportConfiguratorTests(_ActionIconsToolSetup):
00115 
00116     layer = ExportImportZCMLLayer
00117 
00118     def _getTargetClass(self):
00119         from Products.CMFActionIcons.exportimport \
00120                 import ActionIconsToolExportConfigurator
00121 
00122         return ActionIconsToolExportConfigurator
00123 
00124     def test_generateXML_empty(self):
00125         site = self._initSite(with_icon=False)
00126         configurator = self._makeOne(site).__of__(site)
00127 
00128         self._compareDOM(configurator.generateXML(), self._EMPTY_EXPORT)
00129 
00130     def test_generateXML_with_icon(self):
00131         site = self._initSite(with_icon=True)
00132         configurator = self._makeOne(site).__of__(site)
00133 
00134         self._compareDOM(configurator.generateXML(), self._WITH_ICON_EXPORT)
00135 
00136 
00137 class ActionIconsToolImportConfiguratorTests(_ActionIconsToolSetup):
00138 
00139     layer = ExportImportZCMLLayer
00140 
00141     def _getTargetClass(self):
00142         from Products.CMFActionIcons.exportimport \
00143                 import ActionIconsToolImportConfigurator
00144 
00145         return ActionIconsToolImportConfigurator
00146 
00147     def test_parseXML_empty(self):
00148         site = self._initSite(with_icon=False)
00149         configurator = self._makeOne(site)
00150         ait_info = configurator.parseXML(self._EMPTY_EXPORT)
00151 
00152         self.assertEqual(len(ait_info['action_icons']), 0)
00153 
00154     def test_parseXML_empty_i18n(self):
00155         site = self._initSite(with_icon=False)
00156         configurator = self._makeOne(site)
00157         ait_info = configurator.parseXML(self._EMPTY_I18N_EXPORT)
00158 
00159         self.assertEqual(len(ait_info['action_icons']), 0)
00160 
00161     def test_parseXML_with_icon(self):
00162         site = self._initSite(with_icon=False)
00163         configurator = self._makeOne(site)
00164         ait_info = configurator.parseXML(self._WITH_ICON_EXPORT)
00165 
00166         self.assertEqual(len(ait_info['action_icons']), 1)
00167 
00168         info = ait_info['action_icons'][0]
00169         self.assertEqual(info['category'], self.CATEGORY)
00170         self.assertEqual(info['action_id'], self.ACTION_ID)
00171         self.assertEqual(info['title'], self.TITLE)
00172         self.assertEqual(info['priority'], self.PRIORITY)
00173         self.assertEqual(info['icon_expr'], self.ICON_EXPR)
00174 
00175     def test_parseXML_with_i18n_icon(self):
00176         site = self._initSite(with_icon=False)
00177         configurator = self._makeOne(site)
00178         ait_info = configurator.parseXML(self._WITH_I18N_ICON_EXPORT)
00179 
00180         self.assertEqual(len(ait_info['action_icons']), 1)
00181 
00182         info = ait_info['action_icons'][0]
00183         self.assertEqual(info['category'], self.CATEGORY)
00184         self.assertEqual(info['action_id'], self.ACTION_ID)
00185         self.assertEqual(info['title'], self.TITLE)
00186         self.assertEqual(info['priority'], self.PRIORITY)
00187         self.assertEqual(info['icon_expr'], self.ICON_EXPR)
00188 
00189 
00190 class Test_exportActionIconsTool(_ActionIconsToolSetup):
00191 
00192     layer = ExportImportZCMLLayer
00193 
00194     def test_empty(self):
00195         from Products.CMFActionIcons.exportimport \
00196             import exportActionIconsTool
00197 
00198         site = self._initSite(with_icon=False)
00199         context = DummyExportContext(site)
00200         exportActionIconsTool(context)
00201 
00202         self.assertEqual(len(context._wrote), 1)
00203         filename, text, content_type = context._wrote[0]
00204         self.assertEqual(filename, 'actionicons.xml')
00205         self._compareDOM(text, self._EMPTY_EXPORT)
00206         self.assertEqual(content_type, 'text/xml')
00207 
00208     def test_with_icon(self):
00209         from Products.CMFActionIcons.exportimport \
00210             import exportActionIconsTool
00211 
00212         site = self._initSite(with_icon=True)
00213         context = DummyExportContext(site)
00214         exportActionIconsTool(context)
00215 
00216         self.assertEqual(len(context._wrote), 1)
00217         filename, text, content_type = context._wrote[0]
00218         self.assertEqual(filename, 'actionicons.xml')
00219         self._compareDOM(text, self._WITH_ICON_EXPORT)
00220         self.assertEqual(content_type, 'text/xml')
00221 
00222 
00223 class Test_importActionIconsTool(_ActionIconsToolSetup):
00224 
00225     layer = ExportImportZCMLLayer
00226 
00227     def test_normal(self):
00228         from Products.CMFActionIcons.exportimport \
00229             import importActionIconsTool
00230 
00231         site = self._initSite(with_icon=False)
00232         ait = site.portal_actionicons
00233         self.assertEqual(len(ait.listActionIcons()), 0)
00234 
00235         context = DummyImportContext(site)
00236         context._files['actionicons.xml'] = self._WITH_ICON_EXPORT
00237         importActionIconsTool(context)
00238 
00239         self.assertEqual(len(ait.listActionIcons()), 1)
00240         action_icon = ait.listActionIcons()[0]
00241 
00242         self.assertEqual(action_icon.getCategory(), self.CATEGORY)
00243         self.assertEqual(action_icon.getActionId(), self.ACTION_ID)
00244         self.assertEqual(action_icon.getTitle(), self.TITLE)
00245         self.assertEqual(action_icon.getPriority(), self.PRIORITY)
00246         self.assertEqual(action_icon.getExpression(), self.ICON_EXPR)
00247 
00248     def test_nopurge(self):
00249         from Products.CMFActionIcons.exportimport \
00250             import importActionIconsTool
00251 
00252         site = self._initSite(with_icon=True)
00253         ait = site.portal_actionicons
00254         ait.updateActionIcon(self.CATEGORY, self.ACTION_ID, 'somexpr',
00255                              title='foo', priority=123)
00256         self.assertEqual(len(ait.listActionIcons()), 1)
00257 
00258         context = DummyImportContext(site, purge=False)
00259         context._files['actionicons.xml'] = self._WITH_ICON_EXPORT
00260         importActionIconsTool(context)
00261 
00262         self.assertEqual(len(ait.listActionIcons()), 1)
00263         action_icon = ait.listActionIcons()[0]
00264 
00265         self.assertEqual(action_icon.getCategory(), self.CATEGORY)
00266         self.assertEqual(action_icon.getActionId(), self.ACTION_ID)
00267         self.assertEqual(action_icon.getTitle(), self.TITLE)
00268         self.assertEqual(action_icon.getPriority(), self.PRIORITY)
00269         self.assertEqual(action_icon.getExpression(), self.ICON_EXPR)
00270 
00271 
00272 def test_suite():
00273     return unittest.TestSuite((
00274         unittest.makeSuite(ActionIconsToolExportConfiguratorTests),
00275         unittest.makeSuite(ActionIconsToolImportConfiguratorTests),
00276         unittest.makeSuite(Test_exportActionIconsTool),
00277         unittest.makeSuite(Test_importActionIconsTool),
00278         ))
00279 
00280 if __name__ == '__main__':
00281     from Products.GenericSetup.testing import run
00282     run(test_suite())