Back to index

plone3  3.1.7
test_exportimport.py
Go to the documentation of this file.
00001 from StringIO import StringIO
00002 
00003 from zope.app.component.hooks import setSite, setHooks
00004 from zope.component import getSiteManager
00005 from zope.component import getUtility
00006 from zope.component import queryUtility
00007 from zope.i18nmessageid import Message
00008 from zope.interface import Interface
00009 
00010 from xml.dom.minidom import parseString
00011 
00012 from Products.GenericSetup.testing import DummySetupEnviron
00013 
00014 from plone.portlets.interfaces import IPortletManager
00015 from plone.portlets.interfaces import IPortletType
00016 from plone.portlets.manager import PortletManager
00017 
00018 from plone.app.portlets.exportimport.portlets import PortletsXMLAdapter
00019 from plone.app.portlets.interfaces import IColumn
00020 from plone.app.portlets.interfaces import IDashboard
00021 from plone.app.portlets.tests.base import PortletsTestCase
00022 from plone.app.portlets.tests.utils import FooPortletManager
00023 
00024 class PortletsExportImportTestCase(PortletsTestCase):
00025     def afterSetUp(self):
00026         setHooks()
00027         setSite(self.portal)
00028         self.sm = getSiteManager(self.portal)
00029         self.importer = self.exporter = PortletsXMLAdapter(self.sm,
00030           DummySetupEnviron())
00031    
00032     def _searchPortletManagerRegistrations(self, name = None):  
00033         results = [r for r in self.sm.registeredUtilities()
00034           if r.provided.isOrExtends(IPortletManager)]
00035         if name:
00036             results = [r for r in results if r.name == name]
00037         return results
00038 
00039     def _node_as_string(self, node):
00040         file = StringIO()
00041         node.writexml(file)
00042         file.seek(0)
00043         return file.read()
00044 
00045 class TestImportPortlets(PortletsExportImportTestCase):
00046 
00047     def test_removePortlet(self):
00048         self.failUnless(queryUtility(IPortletType,
00049           name='portlets.Calendar') is not None)
00050         self.assertEqual(True,
00051           self.importer._removePortlet('portlets.Calendar'))
00052         self.failUnless(queryUtility(IPortletType,
00053           name='portlets.Calendar') is None)
00054         self.assertEqual(False, self.importer._removePortlet('foo'))
00055     
00056     def test_checkBasicPortletNodeErrors(self):
00057         node = parseString(_XML_INVALID_EXTEND_AND_PURGE).documentElement
00058         self.assertEqual(
00059           True, self.importer._checkBasicPortletNodeErrors(node,
00060           ['portlets.Exists'])
00061           ) 
00062         node = parseString(_XML_INVALID_EXTEND_NONEXISTS).documentElement
00063         self.assertEqual(
00064           True, self.importer._checkBasicPortletNodeErrors(node,
00065           ['portlets.Exists'])
00066           )
00067         node = parseString(_XML_INVALID_ADD_EXISTING).documentElement
00068         self.assertEqual(
00069           True, self.importer._checkBasicPortletNodeErrors(node,
00070           ['portlets.Exists'])
00071           )
00072         node = parseString(_XML_EXTEND_EXISTING).documentElement
00073         self.assertEqual(
00074           False, self.importer._checkBasicPortletNodeErrors(node,
00075           ['portlets.Exists'])
00076           )        
00077     
00078     def test_modifyForList(self):
00079         node = parseString(_XML_SWITCH_COLUMNS).documentElement
00080         self.assertEqual([IColumn],
00081           self.importer._modifyForList(node, [IDashboard]))
00082         node = parseString(_XML_BBB_INTERFACE).documentElement
00083         self.assertEqual([IColumn],
00084           self.importer._modifyForList(node, []))
00085     
00086     def test_initPortletNode_duplicateInterfaces(self): 
00087         node = parseString(_XML_DUPLICATE_INTERFACES).documentElement 
00088         self.importer._initPortletNode(node) 
00089         portlet = queryUtility(IPortletType, name="portlets.New") 
00090         self.failUnless(portlet is not None) 
00091         self.assertEqual([IColumn], portlet.for_) 
00092     
00093     def test_initPortletNode_basic(self):
00094         node = parseString(_XML_BASIC).documentElement
00095         self.importer._initPortletNode(node)
00096         portlet = queryUtility(IPortletType, name="portlets.New")
00097         self.failUnless(portlet is not None)
00098         self.assertEqual('Foo', portlet.title)
00099         self.assertEqual('Bar', portlet.description)
00100         self.assertEqual([IColumn], portlet.for_)
00101 
00102     def disabled_test_initPortletNode_i18n(self):
00103         node = parseString(_XML_BASIC_I18N).documentElement
00104         self.importer._initPortletNode(node)
00105         portlet = queryUtility(IPortletType, name="portlets.New")
00106         self.failUnless(portlet is not None)
00107         self.assertEqual([IColumn], portlet.for_)
00108         # XXX Missing i18n support in the exportimport code
00109         self.failUnless(isinstance(portlet.title, Message),
00110                         "Portlet title should be a Message instance")
00111         self.failUnless(isinstance(portlet.description, Message),
00112                         "Portlet description should be a Message instance")
00113         self.assertEquals(u"title_foo_portlet", portlet.title)
00114         self.assertEquals(u"description_foo_portlet", portlet.description)
00115         self.assertEquals(u"Foo", portlet.title.default)
00116         self.assertEquals(u"Bar", portlet.description.default)
00117         self.assertEquals(u"foodomain", portlet.title.domain)
00118         self.assertEquals(u"foodomain", portlet.description.domain)
00119 
00120     def test_initPortletNode_multipleInterfaces(self):
00121         node = parseString(_XML_MULTIPLE_INTERFACES).documentElement
00122         self.importer._initPortletNode(node)
00123         portlet = queryUtility(IPortletType, name="portlets.New")
00124         self.failUnless(portlet is not None)
00125         self.assertEqual([IColumn, IDashboard], portlet.for_)
00126     
00127     def test_initPortletNode_defaultManagerInterface(self):
00128         node = parseString(_XML_DEFAULT_INTERFACE).documentElement
00129         self.importer._initPortletNode(node)
00130         portlet = queryUtility(IPortletType, name="portlets.New")
00131         self.failUnless(portlet is not None)
00132         self.assertEqual([Interface], portlet.for_)
00133     
00134     def test_initPortletNode_BBBInterface(self):
00135         node = parseString(_XML_BBB_INTERFACE).documentElement
00136         self.importer._initPortletNode(node)
00137         portlet = queryUtility(IPortletType, name="portlets.BBB")
00138         self.failUnless(portlet is not None)
00139         self.assertEqual([IColumn], portlet.for_)
00140     
00141     def test_initPortletNode_extend(self):
00142         node = parseString(_XML_EXTENDME_SETUP).documentElement
00143         self.importer._initPortletNode(node)
00144         node = parseString(_XML_EXTENDME_EXTENSION).documentElement
00145         self.importer._initPortletNode(node) 
00146         portlet = queryUtility(IPortletType, name="portlets.ExtendMe")
00147         self.failUnless(portlet is not None)
00148         self.assertEqual([IDashboard], portlet.for_)
00149         self.assertEqual('Bar', portlet.title)
00150         self.assertEqual('Bar', portlet.description)
00151     
00152     def test_initPortletNode_purge(self):
00153         node = parseString(_XML_PURGEME_SETUP).documentElement
00154         self.importer._initPortletNode(node)
00155         node = parseString(_XML_PURGEME_PURGE).documentElement
00156         self.importer._initPortletNode(node)
00157         portlet = queryUtility(IPortletType, name="portlets.PurgeMe")
00158         self.failUnless(portlet is not None)
00159         self.assertEqual([IColumn], portlet.for_)
00160         self.assertEqual('Bar', portlet.title)
00161         self.assertEqual('Bar', portlet.description)
00162     
00163     def test_initPortletNode_remove(self):
00164         node = parseString(_XML_REMOVEME_SETUP).documentElement
00165         self.importer._initPortletNode(node)
00166         portlet = queryUtility(IPortletType, name='portlets.RemoveMe')
00167         self.failUnless(portlet is not None)
00168         node = parseString(_XML_REMOVEME_REMOVE).documentElement
00169         self.importer._initPortletNode(node)
00170         portlet = queryUtility(IPortletType, name='portlets.RemoveMe')
00171         self.failUnless(portlet is None)
00172 
00173 class TestExportPortlets(PortletsExportImportTestCase):
00174 
00175     def test_extractPortletNode(self):
00176         node = parseString(_XML_MULTIPLE_INTERFACES).documentElement
00177         self.importer._initPortletNode(node)
00178         portlet = getUtility(IPortletType, 'portlets.New')
00179         node = self.exporter._extractPortletNode('portlets.New', portlet)
00180         file = StringIO()
00181         node.writexml(file)
00182         file.seek(0)
00183         self.assertEqual("""<portlet title="Foo" addview="portlets.New" description="Foo"><for interface="plone.app.portlets.interfaces.IColumn"/><for interface="plone.app.portlets.interfaces.IDashboard"/></portlet>""", file.read())
00184     
00185     def test_extractPortletNode_defaultManagerInterface(self):
00186         node = parseString(_XML_EXPLICIT_DEFAULT_INTERFACE).documentElement
00187         self.importer._initPortletNode(node)
00188         portlet = getUtility(IPortletType, 'portlets.New')
00189         node = self.exporter._extractPortletNode('portlets.New', portlet)
00190         file = StringIO()
00191         node.writexml(file)
00192         file.seek(0)
00193         self.assertEqual("""<portlet title="Foo" addview="portlets.New" description="Foo"/>""", file.read())
00194 
00195 class TestHelperMethods(PortletsExportImportTestCase):
00196 
00197     def test_BBB_for(self):
00198         self.assertEqual([Interface], self.importer._BBB_for(None))
00199         self.assertEqual([], self.importer._BBB_for([]))
00200         self.assertEqual([Interface], self.importer._BBB_for(Interface))
00201         self.assertEqual([Interface], self.importer._BBB_for([Interface]))
00202 
00203 class TestImportPortletManagers(PortletsExportImportTestCase):
00204         
00205     def test_initPortletManagerNode_basic(self):
00206         node = parseString(_XML_PORTLET_MANAGER_BASIC).documentElement
00207         self.importer._initPortletManagerNode(node)      
00208         
00209         manager = queryUtility(IPortletManager, name='plone.foo_column')
00210         self.failUnless(manager is not None)
00211         self.assertEqual(PortletManager, manager.__class__)
00212         
00213     def test_initPortletManagerNode_customType(self):
00214         node = parseString(_XML_PORTLET_MANAGER_CUSTOM_TYPE).documentElement
00215         self.importer._initPortletManagerNode(node)
00216 
00217         manager = queryUtility(IPortletManager, name='plone.foo_column')
00218         self.failUnless(manager is not None)
00219         self.failUnless(IColumn.providedBy(manager))
00220    
00221     def test_initPortletManagerNode_customClass(self):
00222         node = parseString(_XML_PORTLET_MANAGER_CUSTOM_CLASS).documentElement
00223         self.importer._initPortletManagerNode(node)
00224 
00225         manager = queryUtility(IPortletManager, name='plone.foo_column')
00226         self.failUnless(manager is not None)
00227         self.assertEqual(FooPortletManager, manager.__class__)
00228 
00229 
00230 class TestExportPortletManagers(PortletsExportImportTestCase):
00231 
00232     def test_extractPortletManagerNode_defaultTypeAndClass(self):
00233         node = parseString(_XML_PORTLET_MANAGER_BASIC).documentElement
00234         self.importer._initPortletManagerNode(node)
00235         results = self._searchPortletManagerRegistrations('plone.foo_column')
00236         r = results[0]
00237         node = self.exporter._extractPortletManagerNode(r)
00238         self.assertEqual('<portletmanager name="plone.foo_column"/>', self._node_as_string(node))
00239 
00240     def test_extractPortletManagerNode_customType(self):
00241         node = parseString(_XML_PORTLET_MANAGER_CUSTOM_TYPE).documentElement
00242         self.importer._initPortletManagerNode(node)
00243         results = self._searchPortletManagerRegistrations('plone.foo_column')
00244         r = results[0]
00245         node = self.exporter._extractPortletManagerNode(r)
00246         self.assertEqual('<portletmanager name="plone.foo_column"  type="plone.app.portlets.interfaces.IColumn"/>', self._node_as_string(node))
00247 
00248     def test_extractPortletManagerNode_customClass(self):
00249         node = parseString(_XML_PORTLET_MANAGER_CUSTOM_CLASS).documentElement
00250         self.importer._initPortletManagerNode(node)
00251         results = self._searchPortletManagerRegistrations('plone.foo_column')
00252         r = results[0]
00253         node = self.exporter._extractPortletManagerNode(r)
00254         self.assertEqual('<portletmanager name="plone.foo_column"  class="plone.app.portlets.tests.utils.FooPortletManager"/>', self._node_as_string(node))
00255 
00256 def test_suite():
00257     from unittest import TestSuite, makeSuite
00258     suite = TestSuite()
00259     suite.addTest(makeSuite(TestImportPortlets))
00260     suite.addTest(makeSuite(TestExportPortlets))
00261     suite.addTest(makeSuite(TestHelperMethods))
00262     suite.addTest(makeSuite(TestImportPortletManagers))
00263     suite.addTest(makeSuite(TestExportPortletManagers))
00264     return suite
00265 
00266 _XML_INVALID_EXTEND_AND_PURGE = """<?xml version="1.0"?>
00267 <portlet addview="portlets.Exists" extend="" purge="" />
00268 """
00269 
00270 _XML_INVALID_EXTEND_NONEXISTS = """<?xml version="1.0"?>
00271 <portlet addview="portlets.NonExists" extend="" />
00272 """
00273 
00274 _XML_INVALID_ADD_EXISTING = """<?xml version="1.0"?>
00275 <portlet addview="portlets.Exists" title="Foo" description="Foo" />
00276 """
00277 
00278 _XML_EXTEND_EXISTING = """<?xml version="1.0"?>
00279 <portlet addview="portlets.Exists" extend="" />
00280 """
00281 
00282 _XML_SWITCH_COLUMNS = """<?xml version="1.0"?>
00283 <portlet addview="portlets.Exists" extend="">
00284   <for interface="plone.app.portlets.interfaces.IColumn" />
00285   <for interface="plone.app.portlets.interfaces.IDashboard" remove ="" />
00286 </portlet>
00287 """
00288 
00289 _XML_BASIC = """<?xml version="1.0"?>
00290 <portlet addview="portlets.New" title="Foo" description="Bar">
00291   <for interface="plone.app.portlets.interfaces.IColumn" />
00292 </portlet>
00293 """
00294 
00295 _XML_BASIC_I18N = """<?xml version="1.0"?>
00296 <portlet xmlns:i18n="http://xml.zope.org/namespaces/i18n"
00297     i18n:domain="foodomain"
00298     addview="portlets.New" title="Foo" description="Bar"
00299     i18n:attributes="title title_foo_portlet;
00300                      description description_foo_portlet">
00301   <for interface="plone.app.portlets.interfaces.IColumn" />
00302 </portlet>
00303 """
00304 
00305 _XML_MULTIPLE_INTERFACES = """<?xml version="1.0"?>
00306 <portlet addview="portlets.New" title="Foo" description="Foo">
00307   <for interface="plone.app.portlets.interfaces.IColumn" />
00308   <for interface="plone.app.portlets.interfaces.IDashboard" />
00309 </portlet>
00310 """
00311 
00312 _XML_DUPLICATE_INTERFACES = """<?xml version="1.0"?> 
00313 <portlet addview="portlets.New" title="Foo" description="Foo"> 
00314   <for interface="plone.app.portlets.interfaces.IColumn" /> 
00315   <for interface="plone.app.portlets.interfaces.IColumn" /> 
00316 </portlet> 
00317 """
00318 
00319 _XML_DEFAULT_INTERFACE = """<?xml version="1.0"?>
00320 <portlet addview="portlets.New" title="Foo" description="Foo" />
00321 """
00322 
00323 _XML_EXTENDME_SETUP = """<?xml version="1.0"?>
00324 <portlet addview="portlets.ExtendMe" title="Foo" description="Foo">
00325   <for interface="plone.app.portlets.interfaces.IColumn" />
00326 </portlet>
00327 """
00328 
00329 _XML_EXTENDME_EXTENSION = """<?xml version="1.0"?>
00330 <portlet addview="portlets.ExtendMe" extend="" title="Bar" description="Bar">
00331   <for interface="plone.app.portlets.interfaces.IColumn" remove="" />
00332   <for interface="plone.app.portlets.interfaces.IDashboard" />
00333 </portlet>
00334 """
00335 
00336 _XML_PURGEME_SETUP = """<?xml version="1.0"?>
00337 <portlet addview="portlets.PurgeMe" title="Foo" description="Foo">
00338   <for interface="plone.app.portlets.interfaces.IDashboard" />
00339 </portlet>
00340 """
00341 
00342 _XML_PURGEME_PURGE = """<?xml version="1.0"?>
00343 <portlet addview="portlets.PurgeMe" purge="" title="Bar" description="Bar">
00344   <for interface="plone.app.portlets.interfaces.IColumn" />
00345 </portlet>
00346 """
00347 
00348 _XML_REMOVEME_SETUP = """<?xml version="1.0"?>
00349 <portlet addview="portlets.RemoveMe" title="Foo" description="Foo" />
00350 """
00351 
00352 _XML_REMOVEME_REMOVE = """<?xml version="1.0"?>
00353 <portlet addview="portlets.RemoveMe" remove="" />
00354 """
00355 
00356 _XML_EXPLICIT_DEFAULT_INTERFACE = """<?xml version="1.0"?>
00357 <portlet addview="portlets.New" title="Foo" description="Foo">
00358   <for interface="zope.interface.Interface" />
00359 </portlet>
00360 """
00361 
00362 _XML_BBB_INTERFACE = """<?xml version="1.0"?>
00363 <portlet addview="portlets.BBB" title="Foo" description="Foo" for="plone.app.portlets.interfaces.IColumn" />
00364 """
00365 
00366 _XML_PORTLET_MANAGER_BASIC = """<?xml version="1.0"?>
00367 <portletmanager name="plone.foo_column" />
00368 """
00369 
00370 _XML_PORTLET_MANAGER_CUSTOM_TYPE = """<?xml version="1.0"?>
00371 <portletmanager name="plone.foo_column" type="plone.app.portlets.interfaces.IColumn" />
00372 """
00373 
00374 _XML_PORTLET_MANAGER_CUSTOM_CLASS = """<?xml version="1.0"?>
00375 <portletmanager name="plone.foo_column" class="plone.app.portlets.tests.utils.FooPortletManager" />
00376 """