Back to index

plone3  3.1.7
test_properties.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 ##############################################################################
00003 #
00004 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00008 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00009 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00010 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00011 # FOR A PARTICULAR PURPOSE.
00012 #
00013 ##############################################################################
00014 """Site properties xml adapter and setup handler unit tests.
00015 
00016 $Id: test_properties.py 72049 2007-01-16 11:06:30Z yuppie $
00017 """
00018 
00019 import unittest
00020 import Testing
00021 
00022 from Products.GenericSetup.testing import BodyAdapterTestCase
00023 from Products.GenericSetup.tests.common import BaseRegistryTests
00024 from Products.GenericSetup.tests.common import DummyExportContext
00025 from Products.GenericSetup.tests.common import DummyImportContext
00026 
00027 from Products.CMFCore.testing import ExportImportZCMLLayer
00028 
00029 _PROPERTIES_BODY = u"""\
00030 <?xml version="1.0"?>
00031 <site>
00032  <property name="title">Foo</property>
00033  <property name="default_charset" type="string">iso-8859-1</property>
00034  <property name="foo_string" type="string">foo</property>
00035  <property name="bar_string" type="string">Bär</property>
00036  <property name="foo_boolean" type="boolean">False</property>
00037 </site>
00038 """.encode('utf-8')
00039 
00040 _EMPTY_EXPORT = """\
00041 <?xml version="1.0" ?>
00042 <site>
00043  <property name="title"/>
00044 </site>
00045 """
00046 
00047 _NORMAL_EXPORT = """\
00048 <?xml version="1.0" ?>
00049 <site>
00050  <property name="title"/>
00051  <property name="foo" type="string">Foo</property>
00052  <property name="bar" type="tokens">
00053   <element value="Bar"/>
00054  </property>
00055  <property name="moo" type="tokens">
00056   <element value="Moo"/>
00057  </property>
00058 </site>
00059 """
00060 
00061 
00062 class PropertiesXMLAdapterTests(BodyAdapterTestCase):
00063 
00064     layer = ExportImportZCMLLayer
00065 
00066     def _getTargetClass(self):
00067         from Products.CMFCore.exportimport.properties \
00068                 import PropertiesXMLAdapter
00069 
00070         return PropertiesXMLAdapter
00071 
00072     def _populate(self, obj):
00073         obj._setPropValue('title', 'Foo')
00074         obj._setProperty('default_charset', 'iso-8859-1', 'string')
00075         obj._setProperty('foo_string', 'foo', 'string')
00076         obj._setProperty('bar_string', 'Bär', 'string')
00077         obj._setProperty('foo_boolean', False, 'boolean')
00078 
00079     def _verifyImport(self, obj):
00080         self.assertEqual(type(obj.title), str)
00081         self.assertEqual(obj.title, 'Foo')
00082         self.assertEqual(type(obj.foo_string), str)
00083         self.assertEqual(obj.foo_string, 'foo')
00084         self.assertEqual(type(obj.bar_string), str)
00085         self.assertEqual(obj.bar_string, 'Bär')
00086         self.assertEqual(type(obj.foo_boolean), bool)
00087         self.assertEqual(obj.foo_boolean, False)
00088 
00089     def setUp(self):
00090         from Products.CMFCore.PortalObject import PortalObjectBase
00091 
00092         BodyAdapterTestCase.setUp(self)
00093         self._obj = PortalObjectBase('foo_site')
00094         self._BODY = _PROPERTIES_BODY
00095 
00096 
00097 class _SitePropertiesSetup(BaseRegistryTests):
00098 
00099     def _initSite(self, foo=2, bar=2):
00100         from Products.CMFCore.PortalObject import PortalObjectBase
00101 
00102         self.root.site = PortalObjectBase('foo_site')
00103         site = self.root.site
00104 
00105         if foo > 0:
00106             site._setProperty('foo', '', 'string')
00107         if foo > 1:
00108             site._updateProperty('foo', 'Foo')
00109 
00110         if bar > 0:
00111             site._setProperty( 'bar', (), 'tokens' )
00112             site._setProperty( 'moo', (), 'tokens' )
00113         if bar > 1:
00114             site._updateProperty( 'bar', ('Bar',) )
00115             site.moo = ['Moo']
00116 
00117         return site
00118 
00119 
00120 class exportSitePropertiesTests(_SitePropertiesSetup):
00121 
00122     layer = ExportImportZCMLLayer
00123 
00124     def test_empty(self):
00125         from Products.CMFCore.exportimport.properties \
00126                 import exportSiteProperties
00127 
00128         site = self._initSite(0, 0)
00129         context = DummyExportContext(site)
00130         exportSiteProperties(context)
00131 
00132         self.assertEqual(len(context._wrote), 1)
00133         filename, text, content_type = context._wrote[0]
00134         self.assertEqual(filename, 'properties.xml')
00135         self._compareDOM(text, _EMPTY_EXPORT)
00136         self.assertEqual(content_type, 'text/xml')
00137 
00138     def test_normal(self):
00139         from Products.CMFCore.exportimport.properties \
00140                 import exportSiteProperties
00141 
00142         site = self._initSite()
00143         context = DummyExportContext( site )
00144         exportSiteProperties(context)
00145 
00146         self.assertEqual(len(context._wrote), 1)
00147         filename, text, content_type = context._wrote[0]
00148         self.assertEqual(filename, 'properties.xml')
00149         self._compareDOM(text, _NORMAL_EXPORT)
00150         self.assertEqual(content_type, 'text/xml')
00151 
00152 
00153 class importSitePropertiesTests(_SitePropertiesSetup):
00154 
00155     layer = ExportImportZCMLLayer
00156 
00157     def test_empty_default_purge(self):
00158         from Products.CMFCore.exportimport.properties \
00159                 import importSiteProperties
00160 
00161         site = self._initSite()
00162 
00163         self.assertEqual( len( site.propertyIds() ), 4 )
00164         self.failUnless( 'foo' in site.propertyIds() )
00165         self.assertEqual( site.getProperty('foo'), 'Foo' )
00166         self.failUnless( 'bar' in site.propertyIds() )
00167         self.assertEqual( site.getProperty('bar'), ('Bar',) )
00168 
00169         context = DummyImportContext(site)
00170         context._files['properties.xml'] = _EMPTY_EXPORT
00171         importSiteProperties(context)
00172 
00173         self.assertEqual( len( site.propertyIds() ), 1 )
00174 
00175     def test_empty_explicit_purge(self):
00176         from Products.CMFCore.exportimport.properties \
00177                 import importSiteProperties
00178 
00179         site = self._initSite()
00180 
00181         self.assertEqual( len( site.propertyIds() ), 4 )
00182         self.failUnless( 'foo' in site.propertyIds() )
00183         self.assertEqual( site.getProperty('foo'), 'Foo' )
00184         self.failUnless( 'bar' in site.propertyIds() )
00185         self.assertEqual( site.getProperty('bar'), ('Bar',) )
00186 
00187         context = DummyImportContext(site, True)
00188         context._files['properties.xml'] = _EMPTY_EXPORT
00189         importSiteProperties(context)
00190 
00191         self.assertEqual( len( site.propertyIds() ), 1 )
00192 
00193     def test_empty_skip_purge(self):
00194         from Products.CMFCore.exportimport.properties \
00195                 import importSiteProperties
00196 
00197         site = self._initSite()
00198 
00199         self.assertEqual( len( site.propertyIds() ), 4 )
00200         self.failUnless( 'foo' in site.propertyIds() )
00201         self.assertEqual( site.getProperty('foo'), 'Foo' )
00202         self.failUnless( 'bar' in site.propertyIds() )
00203         self.assertEqual( site.getProperty('bar'), ('Bar',) )
00204 
00205         context = DummyImportContext(site, False)
00206         context._files['properties.xml'] = _EMPTY_EXPORT
00207         importSiteProperties(context)
00208 
00209         self.assertEqual( len( site.propertyIds() ), 4 )
00210         self.failUnless( 'foo' in site.propertyIds() )
00211         self.assertEqual( site.getProperty('foo'), 'Foo' )
00212         self.failUnless( 'bar' in site.propertyIds() )
00213         self.assertEqual( site.getProperty('bar'), ('Bar',) )
00214 
00215     def test_normal(self):
00216         from Products.CMFCore.exportimport.properties \
00217                 import importSiteProperties
00218 
00219         site = self._initSite(0,0)
00220 
00221         self.assertEqual( len( site.propertyIds() ), 1 )
00222 
00223         context = DummyImportContext(site)
00224         context._files['properties.xml'] = _NORMAL_EXPORT
00225         importSiteProperties(context)
00226 
00227         self.assertEqual( len( site.propertyIds() ), 4 )
00228         self.failUnless( 'foo' in site.propertyIds() )
00229         self.assertEqual( site.getProperty('foo'), 'Foo' )
00230         self.failUnless( 'bar' in site.propertyIds() )
00231         self.assertEqual( site.getProperty('bar'), ('Bar',) )
00232 
00233 
00234 def test_suite():
00235     return unittest.TestSuite((
00236         unittest.makeSuite(PropertiesXMLAdapterTests),
00237         unittest.makeSuite(exportSitePropertiesTests),
00238         unittest.makeSuite(importSitePropertiesTests),
00239         ))
00240 
00241 if __name__ == '__main__':
00242     from Products.CMFCore.testing import run
00243     run(test_suite())