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 """CMFFormController setup handler unit tests.
00014 """
00015 import unittest
00016 
00017 from Products.GenericSetup.tests.common import BaseRegistryTests
00018 from Products.GenericSetup.tests.common import DummyExportContext
00019 from Products.GenericSetup.tests.common import DummyImportContext
00020 from Products.CMFFormController.FormAction import FormAction
00021 from Products.CMFFormController.FormValidator import FormValidator
00022 
00023 class _CMFFormControllerSetup(BaseRegistryTests):
00024 
00025     V_OBJECT_ID = 'v_object_id'
00026     V_CONTEXT_TYPE = 'v_context_type'
00027     V_BUTTON = 'v_button'
00028     V_VALIDATORS = ['validator1', 'validator2']
00029 
00030     A_OBJECT_ID = 'a_object_id'
00031     A_STATUS = 'a_status'
00032     A_CONTEXT_TYPE = 'a_context_type'
00033     A_BUTTON = 'a_button'
00034     A_ACTION_TYPE = 'redirect_to'
00035     A_ACTION_ARG = 'string:action_arg'
00036 
00037     _EMPTY_EXPORT = """\
00038 <?xml version="1.0"?>
00039 <cmfformcontroller>
00040 </cmfformcontroller>
00041 """
00042 
00043     _WITH_INFO_EXPORT = """\
00044 <?xml version="1.0"?>
00045 <cmfformcontroller>
00046    <action
00047      object_id="%s"
00048      status="%s"
00049      context_type="%s"
00050      button="%s"
00051      action_type="%s"
00052      action_arg="%s"
00053      />
00054    <validator
00055      object_id="%s"
00056      context_type="%s"
00057      button="%s"
00058      validators="%s"
00059      />
00060 </cmfformcontroller>
00061 """ % (A_OBJECT_ID,
00062        A_STATUS,
00063        A_CONTEXT_TYPE,
00064        A_BUTTON,
00065        A_ACTION_TYPE,
00066        A_ACTION_ARG,
00067        V_OBJECT_ID,
00068        V_CONTEXT_TYPE,
00069        V_BUTTON,
00070        ','.join(V_VALIDATORS),
00071       )
00072 
00073     _WITH_PARTIAL_INFO_EXPORT = """\
00074 <?xml version="1.0"?>
00075 <cmfformcontroller>
00076    <action
00077      object_id="%s"
00078      status="%s"
00079      action_type="%s"
00080      action_arg="%s"
00081      />
00082    <validator
00083      object_id="%s"
00084      validators="%s"
00085      />
00086 </cmfformcontroller>
00087 """ % (A_OBJECT_ID,
00088        A_STATUS,
00089        A_ACTION_TYPE,
00090        A_ACTION_ARG,
00091        V_OBJECT_ID,
00092        ','.join(V_VALIDATORS),
00093       )
00094 
00095     def _initSite(self, with_info=False):
00096         from OFS.Folder import Folder
00097         from Products.CMFFormController.FormController import FormController
00098 
00099         self.root.site = Folder(id='site')
00100         site = self.root.site
00101         mgr = FormController()
00102         site._setObject( mgr.getId(), mgr )
00103 
00104         if with_info:
00105             fc = getattr(site, mgr.getId())
00106 
00107             fc.actions.set(FormAction(self.A_OBJECT_ID, self.A_STATUS, self.A_CONTEXT_TYPE, self.A_BUTTON, self.A_ACTION_TYPE, self.A_ACTION_ARG))
00108             fc.validators.set(FormValidator(self.V_OBJECT_ID, self.V_CONTEXT_TYPE, self.V_BUTTON, ','.join(self.V_VALIDATORS)))
00109 
00110         return site
00111 
00112 
00113 class CMFFormControllerExportConfiguratorTests(_CMFFormControllerSetup):
00114 
00115     def _getTargetClass(self):
00116         from Products.CMFFormController.exportimport \
00117                 import CMFFormControllerExportConfigurator
00118 
00119         return CMFFormControllerExportConfigurator
00120 
00121     def test_generateXML_empty(self):
00122         site = self._initSite(with_info=False)
00123         configurator = self._makeOne(site).__of__(site)
00124 
00125         self._compareDOM(configurator.generateXML(), self._EMPTY_EXPORT)
00126 
00127     def test_generateXML_with_info(self):
00128         site = self._initSite(with_info=True)
00129         configurator = self._makeOne(site).__of__(site)
00130 
00131         self._compareDOM(configurator.generateXML(), self._WITH_INFO_EXPORT)
00132 
00133 
00134 class CMFFormControllerImportConfiguratorTests(_CMFFormControllerSetup):
00135 
00136     def _getTargetClass(self):
00137         from Products.CMFFormController.exportimport \
00138                 import CMFFormControllerImportConfigurator
00139 
00140         return CMFFormControllerImportConfigurator
00141 
00142     def test_parseXML_empty(self):
00143         site = self._initSite(with_info=False)
00144         configurator = self._makeOne(site)
00145         fc_info = configurator.parseXML(self._EMPTY_EXPORT)
00146 
00147         self.assertEqual(len(fc_info['actions']), 0)
00148         self.assertEqual(len(fc_info['validators']), 0)
00149 
00150     def test_parseXML_with_info(self):
00151         site = self._initSite(with_info=False)
00152         configurator = self._makeOne(site)
00153         fc_info = configurator.parseXML(self._WITH_INFO_EXPORT)
00154 
00155         self.assertEqual(len(fc_info['actions']), 1)
00156         self.assertEqual(len(fc_info['validators']), 1)
00157 
00158         info = fc_info['actions'][0]
00159         self.assertEqual(info['object_id'], self.A_OBJECT_ID)
00160         self.assertEqual(info['status'], self.A_STATUS)
00161         self.assertEqual(info['context_type'], self.A_CONTEXT_TYPE)
00162         self.assertEqual(info['button'], self.A_BUTTON)
00163         self.assertEqual(info['action_type'], self.A_ACTION_TYPE)
00164         self.assertEqual(info['action_arg'], self.A_ACTION_ARG)
00165         info = fc_info['validators'][0]
00166         self.assertEqual(info['object_id'], self.V_OBJECT_ID)
00167         self.assertEqual(info['context_type'], self.V_CONTEXT_TYPE)
00168         self.assertEqual(info['button'], self.V_BUTTON)
00169         self.assertEqual(info['validators'], ','.join(self.V_VALIDATORS))
00170 
00171 
00172 class Test_exportCMFFormController(_CMFFormControllerSetup):
00173 
00174     def test_empty(self):
00175         from Products.CMFFormController.exportimport \
00176              import exportCMFFormController
00177 
00178         site = self._initSite(with_info=False)
00179         context = DummyExportContext(site)
00180         exportCMFFormController(context)
00181 
00182         self.assertEqual(len(context._wrote), 1)
00183         filename, text, content_type = context._wrote[0]
00184         self.assertEqual(filename, 'cmfformcontroller.xml')
00185         self._compareDOM(text, self._EMPTY_EXPORT)
00186         self.assertEqual(content_type, 'text/xml')
00187 
00188     def test_with_info(self):
00189         from Products.CMFFormController.exportimport \
00190              import exportCMFFormController
00191 
00192         site = self._initSite(with_info=True)
00193         context = DummyExportContext(site)
00194         exportCMFFormController(context)
00195 
00196         self.assertEqual(len(context._wrote), 1)
00197         filename, text, content_type = context._wrote[0]
00198         self.assertEqual(filename, 'cmfformcontroller.xml')
00199         self._compareDOM(text, self._WITH_INFO_EXPORT)
00200         self.assertEqual(content_type, 'text/xml')
00201 
00202 
00203 class Test_importCMFFormController(_CMFFormControllerSetup):
00204 
00205     def test_normal(self):
00206         from Products.CMFFormController.exportimport \
00207              import importCMFFormController
00208 
00209         site = self._initSite(with_info=False)
00210         fc = site.portal_form_controller
00211 
00212         self.assertEqual(len(fc.listFormValidators()), 0)
00213         self.assertEqual(len(fc.listFormActions()), 0)
00214 
00215         context = DummyImportContext(site)
00216         context._files['cmfformcontroller.xml'] = self._WITH_INFO_EXPORT
00217         importCMFFormController(context)
00218 
00219         self.assertEqual(len(fc.listFormActions()), 1)
00220         self.assertEqual(len(fc.listFormValidators()), 1)
00221 
00222         action = fc.listFormActions()[0]
00223         self.assertEqual(action.getObjectId(), self.A_OBJECT_ID)
00224         self.assertEqual(action.getStatus(), self.A_STATUS)
00225         self.assertEqual(action.getContextType(), self.A_CONTEXT_TYPE)
00226         self.assertEqual(action.getButton(), self.A_BUTTON)
00227         self.assertEqual(action.getActionType(), self.A_ACTION_TYPE)
00228         self.assertEqual(action.getActionArg(), self.A_ACTION_ARG)
00229 
00230         validator = fc.listFormValidators()[0]
00231         self.assertEqual(validator.getObjectId(), self.V_OBJECT_ID)
00232         self.assertEqual(validator.getContextType(), self.V_CONTEXT_TYPE)
00233         self.assertEqual(validator.getButton(), self.V_BUTTON)
00234         self.assertEqual(validator.getValidators(), self.V_VALIDATORS)
00235 
00236     def test_partial(self):
00237         from Products.CMFFormController.exportimport \
00238              import importCMFFormController
00239 
00240         site = self._initSite(with_info=False)
00241         fc = site.portal_form_controller
00242 
00243         self.assertEqual(len(fc.listFormValidators()), 0)
00244         self.assertEqual(len(fc.listFormActions()), 0)
00245 
00246         context = DummyImportContext(site)
00247         context._files['cmfformcontroller.xml'] = self._WITH_PARTIAL_INFO_EXPORT
00248         importCMFFormController(context)
00249 
00250         self.assertEqual(len(fc.listFormActions()), 1)
00251         self.assertEqual(len(fc.listFormValidators()), 1)
00252 
00253         action = fc.listFormActions()[0]
00254         self.assertEqual(action.getObjectId(), self.A_OBJECT_ID)
00255         self.assertEqual(action.getStatus(), self.A_STATUS)
00256         self.assertEqual(action.getContextType(), None)
00257         self.assertEqual(action.getButton(), None)
00258         self.assertEqual(action.getActionType(), self.A_ACTION_TYPE)
00259         self.assertEqual(action.getActionArg(), self.A_ACTION_ARG)
00260 
00261         validator = fc.listFormValidators()[0]
00262         self.assertEqual(validator.getObjectId(), self.V_OBJECT_ID)
00263         self.assertEqual(validator.getContextType(), None)
00264         self.assertEqual(validator.getButton(), None)
00265         self.assertEqual(validator.getValidators(), self.V_VALIDATORS)
00266 
00267     def test_action_not_unicode(self):
00268         # The action arg cannot be unicode for unrestrictedTraverse
00269         # calls to work properly
00270         from Products.CMFFormController.exportimport \
00271              import importCMFFormController
00272 
00273         site = self._initSite(with_info=False)
00274         fc = site.portal_form_controller
00275 
00276         context = DummyImportContext(site)
00277         context._files['cmfformcontroller.xml'] = self._WITH_INFO_EXPORT
00278         importCMFFormController(context)
00279 
00280         action = fc.listFormActions()[0]
00281         self.failUnless(isinstance(action.getActionArg(), str))
00282 
00283 
00284 def test_suite():
00285     return unittest.TestSuite((
00286         # TODO disabled broken tests
00287         # unittest.makeSuite(CMFFormControllerExportConfiguratorTests),
00288         # unittest.makeSuite(Test_exportCMFFormController),
00289         unittest.makeSuite(CMFFormControllerImportConfiguratorTests),
00290         unittest.makeSuite(Test_importCMFFormController),
00291         ))
00292 
00293 if __name__ == '__main__':
00294     unittest.main(defaultTest='test_suite')