vendor/CMF/1.6.3/CMFCore

view exportimport/tests/test_properties.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """Site properties xml adapter and setup handler unit tests.
15 $Id$
16 """
18 import unittest
19 import Testing
21 from Products.Five import zcml
23 from Products.GenericSetup.testing import BodyAdapterTestCase
24 from Products.GenericSetup.tests.common import BaseRegistryTests
25 from Products.GenericSetup.tests.common import DummyExportContext
26 from Products.GenericSetup.tests.common import DummyImportContext
28 from Products.CMFCore.tests.base.testcase import PlacelessSetup
30 _PROPERTIES_BODY = """\
31 <?xml version="1.0"?>
32 <site>
33 <property name="title">Foo</property>
34 <property name="foo_string" type="string">foo</property>
35 <property name="foo_boolean" type="boolean">False</property>
36 </site>
37 """
39 _EMPTY_EXPORT = """\
40 <?xml version="1.0" ?>
41 <site>
42 <property name="title"/>
43 </site>
44 """
46 _NORMAL_EXPORT = """\
47 <?xml version="1.0" ?>
48 <site>
49 <property name="title"/>
50 <property name="foo" type="string">Foo</property>
51 <property name="bar" type="tokens">
52 <element value="Bar"/>
53 </property>
54 <property name="moo" type="tokens">
55 <element value="Moo"/>
56 </property>
57 </site>
58 """
61 class PropertiesXMLAdapterTests(BodyAdapterTestCase):
63 def _getTargetClass(self):
64 from Products.CMFCore.exportimport.properties \
65 import PropertiesXMLAdapter
67 return PropertiesXMLAdapter
69 def _populate(self, obj):
70 obj._setPropValue('title', 'Foo')
71 obj._setProperty('foo_string', 'foo', 'string')
72 obj._setProperty('foo_boolean', False, 'boolean')
74 def setUp(self):
75 import Products.CMFCore.exportimport
76 from Products.CMFCore.PortalObject import PortalObjectBase
78 BodyAdapterTestCase.setUp(self)
79 zcml.load_config('configure.zcml', Products.CMFCore.exportimport)
81 self._obj = PortalObjectBase('foo_site')
82 self._BODY = _PROPERTIES_BODY
85 class _SitePropertiesSetup(PlacelessSetup, BaseRegistryTests):
87 def _initSite(self, foo=2, bar=2):
88 from Products.CMFCore.PortalObject import PortalObjectBase
90 self.root.site = PortalObjectBase('foo_site')
91 site = self.root.site
93 if foo > 0:
94 site._setProperty('foo', '', 'string')
95 if foo > 1:
96 site._updateProperty('foo', 'Foo')
98 if bar > 0:
99 site._setProperty( 'bar', (), 'tokens' )
100 site._setProperty( 'moo', (), 'tokens' )
101 if bar > 1:
102 site._updateProperty( 'bar', ('Bar',) )
103 site.moo = ['Moo']
105 return site
107 def setUp(self):
108 import Products.CMFCore.exportimport
110 PlacelessSetup.setUp(self)
111 BaseRegistryTests.setUp(self)
112 zcml.load_config('meta.zcml', Products.Five)
113 zcml.load_config('configure.zcml', Products.CMFCore.exportimport)
115 def tearDown(self):
116 BaseRegistryTests.tearDown(self)
117 PlacelessSetup.tearDown(self)
120 class exportSitePropertiesTests(_SitePropertiesSetup):
122 def test_empty(self):
123 from Products.CMFCore.exportimport.properties \
124 import exportSiteProperties
126 site = self._initSite(0, 0)
127 context = DummyExportContext(site)
128 exportSiteProperties(context)
130 self.assertEqual(len(context._wrote), 1)
131 filename, text, content_type = context._wrote[0]
132 self.assertEqual(filename, 'properties.xml')
133 self._compareDOM(text, _EMPTY_EXPORT)
134 self.assertEqual(content_type, 'text/xml')
136 def test_normal(self):
137 from Products.CMFCore.exportimport.properties \
138 import exportSiteProperties
140 site = self._initSite()
141 context = DummyExportContext( site )
142 exportSiteProperties(context)
144 self.assertEqual(len(context._wrote), 1)
145 filename, text, content_type = context._wrote[0]
146 self.assertEqual(filename, 'properties.xml')
147 self._compareDOM(text, _NORMAL_EXPORT)
148 self.assertEqual(content_type, 'text/xml')
151 class importSitePropertiesTests(_SitePropertiesSetup):
153 def test_empty_default_purge(self):
154 from Products.CMFCore.exportimport.properties \
155 import importSiteProperties
157 site = self._initSite()
159 self.assertEqual( len( site.propertyIds() ), 4 )
160 self.failUnless( 'foo' in site.propertyIds() )
161 self.assertEqual( site.getProperty('foo'), 'Foo' )
162 self.failUnless( 'bar' in site.propertyIds() )
163 self.assertEqual( site.getProperty('bar'), ('Bar',) )
165 context = DummyImportContext(site)
166 context._files['properties.xml'] = _EMPTY_EXPORT
167 importSiteProperties(context)
169 self.assertEqual( len( site.propertyIds() ), 1 )
171 def test_empty_explicit_purge(self):
172 from Products.CMFCore.exportimport.properties \
173 import importSiteProperties
175 site = self._initSite()
177 self.assertEqual( len( site.propertyIds() ), 4 )
178 self.failUnless( 'foo' in site.propertyIds() )
179 self.assertEqual( site.getProperty('foo'), 'Foo' )
180 self.failUnless( 'bar' in site.propertyIds() )
181 self.assertEqual( site.getProperty('bar'), ('Bar',) )
183 context = DummyImportContext(site, True)
184 context._files['properties.xml'] = _EMPTY_EXPORT
185 importSiteProperties(context)
187 self.assertEqual( len( site.propertyIds() ), 1 )
189 def test_empty_skip_purge(self):
190 from Products.CMFCore.exportimport.properties \
191 import importSiteProperties
193 site = self._initSite()
195 self.assertEqual( len( site.propertyIds() ), 4 )
196 self.failUnless( 'foo' in site.propertyIds() )
197 self.assertEqual( site.getProperty('foo'), 'Foo' )
198 self.failUnless( 'bar' in site.propertyIds() )
199 self.assertEqual( site.getProperty('bar'), ('Bar',) )
201 context = DummyImportContext(site, False)
202 context._files['properties.xml'] = _EMPTY_EXPORT
203 importSiteProperties(context)
205 self.assertEqual( len( site.propertyIds() ), 4 )
206 self.failUnless( 'foo' in site.propertyIds() )
207 self.assertEqual( site.getProperty('foo'), 'Foo' )
208 self.failUnless( 'bar' in site.propertyIds() )
209 self.assertEqual( site.getProperty('bar'), ('Bar',) )
211 def test_normal(self):
212 from Products.CMFCore.exportimport.properties \
213 import importSiteProperties
215 site = self._initSite(0,0)
217 self.assertEqual( len( site.propertyIds() ), 1 )
219 context = DummyImportContext(site)
220 context._files['properties.xml'] = _NORMAL_EXPORT
221 importSiteProperties(context)
223 self.assertEqual( len( site.propertyIds() ), 4 )
224 self.failUnless( 'foo' in site.propertyIds() )
225 self.assertEqual( site.getProperty('foo'), 'Foo' )
226 self.failUnless( 'bar' in site.propertyIds() )
227 self.assertEqual( site.getProperty('bar'), ('Bar',) )
230 def test_suite():
231 return unittest.TestSuite((
232 unittest.makeSuite(PropertiesXMLAdapterTests),
233 unittest.makeSuite(exportSitePropertiesTests),
234 unittest.makeSuite(importSitePropertiesTests),
235 ))
237 if __name__ == '__main__':
238 unittest.main(defaultTest='test_suite')