Back to index

plone3  3.1.7
test_contenttyperegistry.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 """Content type registry xml adapter and setup handler unit tests.
00014 
00015 $Id: test_contenttyperegistry.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from OFS.Folder import Folder
00022 
00023 from Products.GenericSetup.testing import BodyAdapterTestCase
00024 from Products.GenericSetup.tests.common import BaseRegistryTests
00025 from Products.GenericSetup.tests.common import DummyExportContext
00026 from Products.GenericSetup.tests.common import DummyImportContext
00027 
00028 from Products.CMFCore.testing import ExportImportZCMLLayer
00029 
00030 _TEST_PREDICATES = (
00031  ('plain_text', 'major_minor', ('text', 'plain,javascript'), 'File'),
00032  ('stylesheets', 'extension', ('css,xsl,xslt',), 'Text File'),
00033  ('images', 'mimetype_regex', ('image/.*',), 'Image'),
00034  ('logfiles', 'name_regex', ('error_log-.*',), 'Log File'),
00035 )
00036 
00037 _CTR_BODY = """\
00038 <?xml version="1.0"?>
00039 <object name="content_type_registry" meta_type="Content Type Registry">
00040  <predicate name="foo_predicate" content_type_name="Foo Type"
00041     predicate_type="major_minor">
00042   <argument value="foo_major"/>
00043   <argument value="foo_minor"/>
00044  </predicate>
00045  <predicate name="bar_predicate" content_type_name="Bar Type"
00046     predicate_type="extension">
00047   <argument value="bar"/>
00048  </predicate>
00049  <predicate name="baz_predicate" content_type_name="Baz Type"
00050     predicate_type="mimetype_regex">
00051   <argument value="baz/.*"/>
00052  </predicate>
00053  <predicate name="foobar_predicate" content_type_name="Foobar Type"
00054     predicate_type="name_regex">
00055   <argument value="foobar-.*"/>
00056  </predicate>
00057 </object>
00058 """
00059 
00060 
00061 class ContentTypeRegistryXMLAdapterTests(BodyAdapterTestCase):
00062 
00063     layer = ExportImportZCMLLayer
00064 
00065     def _getTargetClass(self):
00066         from Products.CMFCore.exportimport.contenttyperegistry \
00067                 import ContentTypeRegistryXMLAdapter
00068 
00069         return ContentTypeRegistryXMLAdapter
00070 
00071     def _populate(self, obj):
00072         obj.addPredicate('foo_predicate', 'major_minor')
00073         obj.getPredicate('foo_predicate').edit('foo_major', 'foo_minor')
00074         obj.assignTypeName('foo_predicate', 'Foo Type')
00075         obj.addPredicate('bar_predicate', 'extension')
00076         obj.getPredicate('bar_predicate').edit('bar')
00077         obj.assignTypeName('bar_predicate', 'Bar Type')
00078         obj.addPredicate('baz_predicate', 'mimetype_regex')
00079         obj.getPredicate('baz_predicate').edit('baz/.*')
00080         obj.assignTypeName('baz_predicate', 'Baz Type')
00081         obj.addPredicate('foobar_predicate', 'name_regex')
00082         obj.getPredicate('foobar_predicate').edit('foobar-.*')
00083         obj.assignTypeName('foobar_predicate', 'Foobar Type')
00084 
00085     def setUp(self):
00086         from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
00087 
00088         BodyAdapterTestCase.setUp(self)
00089         self._obj = ContentTypeRegistry()
00090         self._BODY = _CTR_BODY
00091 
00092 
00093 class _ContentTypeRegistrySetup(BaseRegistryTests):
00094 
00095     MAJOR_MINOR_ID = _TEST_PREDICATES[0][0]
00096     MAJOR = _TEST_PREDICATES[0][2][0]
00097     MINOR = _TEST_PREDICATES[0][2][1]
00098     MAJOR_MINOR_TYPENAME = _TEST_PREDICATES[0][3]
00099     EXTENSION_ID = _TEST_PREDICATES[1][0]
00100     EXTENSIONS = _TEST_PREDICATES[1][2][0]
00101     EXTENSION_TYPENAME = _TEST_PREDICATES[1][3]
00102     MIMETYPE_REGEX_ID = _TEST_PREDICATES[2][0]
00103     MIMETYPE_REGEX = _TEST_PREDICATES[2][2][0]
00104     MIMETYPE_REGEX_TYPENAME = _TEST_PREDICATES[2][3]
00105     NAME_REGEX_ID = _TEST_PREDICATES[3][0]
00106     NAME_REGEX = _TEST_PREDICATES[3][2][0]
00107     NAME_REGEX_TYPENAME = _TEST_PREDICATES[3][3]
00108 
00109     _EMPTY_EXPORT = """\
00110 <?xml version="1.0"?>
00111 <object name="content_type_registry" meta_type="Content Type Registry"/>
00112 """
00113 
00114     _WITH_POLICY_EXPORT = """\
00115 <?xml version="1.0"?>
00116 <object name="content_type_registry" meta_type="Content Type Registry">
00117  <predicate name="%s" content_type_name="%s"
00118     predicate_type="major_minor">
00119   <argument value="%s"/>
00120   <argument value="%s"/>
00121  </predicate>
00122  <predicate name="%s" content_type_name="%s"
00123     predicate_type="extension">
00124   <argument value="%s"/>
00125  </predicate>
00126  <predicate name="%s" content_type_name="%s"
00127     predicate_type="mimetype_regex">
00128   <argument value="%s"/>
00129  </predicate>
00130  <predicate name="%s" content_type_name="%s"
00131     predicate_type="name_regex">
00132   <argument value="%s"/>
00133  </predicate>
00134 </object>
00135 """ % (MAJOR_MINOR_ID,
00136        MAJOR_MINOR_TYPENAME,
00137        MAJOR,
00138        MINOR,
00139        EXTENSION_ID,
00140        EXTENSION_TYPENAME,
00141        EXTENSIONS,
00142        MIMETYPE_REGEX_ID,
00143        MIMETYPE_REGEX_TYPENAME,
00144        MIMETYPE_REGEX,
00145        NAME_REGEX_ID,
00146        NAME_REGEX_TYPENAME,
00147        NAME_REGEX,
00148       )
00149 
00150     def _initSite(self, mit_predikat=False):
00151         from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
00152 
00153         self.root.site = Folder(id='site')
00154         site = self.root.site
00155         ctr = ContentTypeRegistry()
00156         site._setObject( ctr.getId(), ctr )
00157 
00158         if mit_predikat:
00159             for (predicate_id, predicate_type, edit_args, content_type_name
00160                 ) in _TEST_PREDICATES:
00161                 ctr.addPredicate(predicate_id, predicate_type) 
00162                 predicate = ctr.getPredicate(predicate_id)
00163                 predicate.edit(*edit_args)
00164                 ctr.assignTypeName(predicate_id, content_type_name)
00165 
00166         return site
00167 
00168 
00169 class exportContentTypeRegistryTests(_ContentTypeRegistrySetup):
00170 
00171     layer = ExportImportZCMLLayer
00172 
00173     def test_empty(self):
00174         from Products.CMFCore.exportimport.contenttyperegistry \
00175                 import exportContentTypeRegistry
00176 
00177         site = self._initSite(mit_predikat=False)
00178         context = DummyExportContext(site)
00179         exportContentTypeRegistry(context)
00180 
00181         self.assertEqual(len(context._wrote), 1)
00182         filename, text, content_type = context._wrote[0]
00183         self.assertEqual(filename, 'contenttyperegistry.xml')
00184         self._compareDOM(text, self._EMPTY_EXPORT)
00185         self.assertEqual(content_type, 'text/xml')
00186 
00187     def test_with_policy(self):
00188         from Products.CMFCore.exportimport.contenttyperegistry \
00189                 import exportContentTypeRegistry
00190 
00191         site = self._initSite(mit_predikat=True)
00192         context = DummyExportContext(site)
00193         exportContentTypeRegistry(context)
00194 
00195         self.assertEqual(len(context._wrote), 1)
00196         filename, text, content_type = context._wrote[0]
00197         self.assertEqual(filename, 'contenttyperegistry.xml')
00198         self._compareDOM(text, self._WITH_POLICY_EXPORT)
00199         self.assertEqual(content_type, 'text/xml')
00200 
00201 
00202 class importContentTypeRegistryTests(_ContentTypeRegistrySetup):
00203 
00204     layer = ExportImportZCMLLayer
00205 
00206     def test_normal(self):
00207         from Products.CMFCore.exportimport.contenttyperegistry \
00208                 import importContentTypeRegistry
00209 
00210         site = self._initSite(mit_predikat=False)
00211         ctr = site.content_type_registry
00212         self.assertEqual(len(ctr.listPredicates()), 0)
00213 
00214         context = DummyImportContext(site)
00215         context._files['contenttyperegistry.xml'] = self._WITH_POLICY_EXPORT
00216         importContentTypeRegistry(context)
00217 
00218         self.assertEqual(len(ctr.listPredicates()), len(_TEST_PREDICATES))
00219         predicate_id, (predicate, content_type_name) = ctr.listPredicates()[0]
00220         self.assertEqual(predicate_id, self.MAJOR_MINOR_ID)
00221         self.assertEqual(predicate.PREDICATE_TYPE, 'major_minor')
00222         self.assertEqual(content_type_name, self.MAJOR_MINOR_TYPENAME)
00223         self.assertEqual(predicate.major, self.MAJOR.split(','))
00224         self.assertEqual(predicate.minor, self.MINOR.split(','))
00225         predicate_id, (predicate, content_type_name) = ctr.listPredicates()[1]
00226         self.assertEqual(predicate_id, self.EXTENSION_ID)
00227         self.assertEqual(predicate.PREDICATE_TYPE, 'extension')
00228         self.assertEqual(content_type_name, self.EXTENSION_TYPENAME)
00229         self.assertEqual(predicate.extensions, self.EXTENSIONS.split(','))
00230         predicate_id, (predicate, content_type_name) = ctr.listPredicates()[2]
00231         self.assertEqual(predicate_id, self.MIMETYPE_REGEX_ID)
00232         self.assertEqual(predicate.PREDICATE_TYPE, 'mimetype_regex')
00233         self.assertEqual(content_type_name, self.MIMETYPE_REGEX_TYPENAME)
00234         self.assertEqual(predicate.pattern.pattern, self.MIMETYPE_REGEX)
00235         predicate_id, (predicate, content_type_name) = ctr.listPredicates()[3]
00236         self.assertEqual(predicate_id, self.NAME_REGEX_ID)
00237         self.assertEqual(predicate.PREDICATE_TYPE, 'name_regex')
00238         self.assertEqual(content_type_name, self.NAME_REGEX_TYPENAME)
00239         self.assertEqual(predicate.pattern.pattern, self.NAME_REGEX)
00240 
00241 
00242 def test_suite():
00243     return unittest.TestSuite((
00244         unittest.makeSuite(ContentTypeRegistryXMLAdapterTests),
00245         unittest.makeSuite(exportContentTypeRegistryTests),
00246         unittest.makeSuite(importContentTypeRegistryTests),
00247         ))
00248 
00249 if __name__ == '__main__':
00250     from Products.CMFCore.testing import run
00251     run(test_suite())