Back to index

plone3  3.1.7
test_ContentTypeRegistry.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 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 """ Unit tests for ContentTypeRegistry module.
00014 
00015 $Id: test_ContentTypeRegistry.py 68496 2006-06-05 16:30:18Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 
00022 class MajorMinorPredicateTests(unittest.TestCase):
00023 
00024     def _makeOne(self, *args, **kw):
00025         from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate
00026 
00027         return MajorMinorPredicate(*args, **kw)
00028 
00029     def test_z2interfaces(self):
00030         from Interface.Verify import verifyClass
00031         from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate
00032         from Products.CMFCore.interfaces.ContentTypeRegistry \
00033                 import ContentTypeRegistryPredicate \
00034                 as IContentTypeRegistryPredicate
00035 
00036         verifyClass(IContentTypeRegistryPredicate, MajorMinorPredicate)
00037 
00038     def test_z3interfaces(self):
00039         from zope.interface.verify import verifyClass
00040         from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate
00041         from Products.CMFCore.interfaces import IContentTypeRegistryPredicate
00042 
00043         verifyClass(IContentTypeRegistryPredicate, MajorMinorPredicate)
00044 
00045     def test_empty( self ):
00046         pred = self._makeOne('empty')
00047         assert pred.getMajorType() == 'None'
00048         assert pred.getMinorType() == 'None'
00049         assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
00050 
00051     def test_simple( self ):
00052         pred = self._makeOne('plaintext')
00053         pred.edit( 'text', 'plain' )
00054         assert pred.getMajorType() == 'text'
00055         assert pred.getMinorType() == 'plain'
00056         assert pred( 'foo', 'text/plain', 'asdfljksadf' )
00057         assert not pred( 'foo', 'text/html', 'asdfljksadf' )
00058         assert not pred( '', '', '' )
00059         assert not pred( '', 'asdf', '' )
00060 
00061     def test_wildcard( self ):
00062         pred = self._makeOne('alltext')
00063         pred.edit( 'text', '' )
00064         assert pred.getMajorType() == 'text'
00065         assert pred.getMinorType() == ''
00066         assert pred( 'foo', 'text/plain', 'asdfljksadf' )
00067         assert pred( 'foo', 'text/html', 'asdfljksadf' )
00068         assert not pred( 'foo', 'image/png', 'asdfljksadf' )
00069 
00070         pred.edit( '', 'html' )
00071         assert pred.getMajorType() == ''
00072         assert pred.getMinorType() == 'html'
00073         assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
00074         assert pred( 'foo', 'text/html', 'asdfljksadf' )
00075         assert not pred( 'foo', 'image/png', 'asdfljksadf' )
00076 
00077 
00078 class ExtensionPredicateTests(unittest.TestCase):
00079 
00080     def _makeOne(self, *args, **kw):
00081         from Products.CMFCore.ContentTypeRegistry import ExtensionPredicate
00082 
00083         return ExtensionPredicate(*args, **kw)
00084 
00085     def test_z2interfaces(self):
00086         from Interface.Verify import verifyClass
00087         from Products.CMFCore.ContentTypeRegistry import ExtensionPredicate
00088         from Products.CMFCore.interfaces.ContentTypeRegistry \
00089                 import ContentTypeRegistryPredicate \
00090                 as IContentTypeRegistryPredicate
00091 
00092         verifyClass(IContentTypeRegistryPredicate, ExtensionPredicate)
00093 
00094     def test_z3interfaces(self):
00095         from zope.interface.verify import verifyClass
00096         from Products.CMFCore.ContentTypeRegistry import ExtensionPredicate
00097         from Products.CMFCore.interfaces import IContentTypeRegistryPredicate
00098 
00099         verifyClass(IContentTypeRegistryPredicate, ExtensionPredicate)
00100 
00101     def test_empty( self ):
00102         pred = self._makeOne('empty')
00103         assert pred.getExtensions() == 'None'
00104         assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
00105         assert not pred( 'foo.txt', 'text/plain', 'asdfljksadf' )
00106         assert not pred( 'foo.bar', 'text/html', 'asdfljksadf' )
00107 
00108     def test_simple( self ):
00109         pred = self._makeOne('stardottext')
00110         pred.edit( 'txt' )
00111         assert pred.getExtensions() == 'txt'
00112         assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
00113         assert pred( 'foo.txt', 'text/plain', 'asdfljksadf' )
00114         assert not pred( 'foo.bar', 'text/html', 'asdfljksadf' )
00115 
00116     def test_multi( self ):
00117         pred = self._makeOne('stardottext')
00118         pred.edit( 'txt text html htm' )
00119         assert pred.getExtensions() == 'txt text html htm'
00120         assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
00121         assert pred( 'foo.txt', 'text/plain', 'asdfljksadf' )
00122         assert pred( 'foo.text', 'text/plain', 'asdfljksadf' )
00123         assert pred( 'foo.html', 'text/plain', 'asdfljksadf' )
00124         assert pred( 'foo.htm', 'text/plain', 'asdfljksadf' )
00125         assert not pred( 'foo.bar', 'text/html', 'asdfljksadf' )
00126 
00127 
00128 class MimeTypeRegexPredicateTests(unittest.TestCase):
00129 
00130     def _makeOne(self, *args, **kw):
00131         from Products.CMFCore.ContentTypeRegistry \
00132                 import MimeTypeRegexPredicate
00133 
00134         return MimeTypeRegexPredicate(*args, **kw)
00135 
00136     def test_z2interfaces(self):
00137         from Interface.Verify import verifyClass
00138         from Products.CMFCore.ContentTypeRegistry \
00139                 import MimeTypeRegexPredicate
00140         from Products.CMFCore.interfaces.ContentTypeRegistry \
00141                 import ContentTypeRegistryPredicate \
00142                 as IContentTypeRegistryPredicate
00143 
00144         verifyClass(IContentTypeRegistryPredicate, MimeTypeRegexPredicate)
00145 
00146     def test_z3interfaces(self):
00147         from zope.interface.verify import verifyClass
00148         from Products.CMFCore.ContentTypeRegistry \
00149                 import MimeTypeRegexPredicate
00150         from Products.CMFCore.interfaces import IContentTypeRegistryPredicate
00151 
00152         verifyClass(IContentTypeRegistryPredicate, MimeTypeRegexPredicate)
00153 
00154     def test_empty( self ):
00155         pred = self._makeOne('empty')
00156         assert pred.getPatternStr() == 'None'
00157         assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
00158 
00159     def test_simple( self ):
00160         pred = self._makeOne('plaintext')
00161         pred.edit( 'text/plain' )
00162         assert pred.getPatternStr() == 'text/plain'
00163         assert pred( 'foo', 'text/plain', 'asdfljksadf' )
00164         assert not pred( 'foo', 'text/html', 'asdfljksadf' )
00165 
00166     def test_pattern( self ):
00167         pred = self._makeOne('alltext')
00168         pred.edit( 'text/*' )
00169         assert pred.getPatternStr() == 'text/*'
00170         assert pred( 'foo', 'text/plain', 'asdfljksadf' )
00171         assert pred( 'foo', 'text/html', 'asdfljksadf' )
00172         assert not pred( 'foo', 'image/png', 'asdfljksadf' )
00173 
00174 
00175 class NameRegexPredicateTests(unittest.TestCase):
00176 
00177     def _makeOne(self, *args, **kw):
00178         from Products.CMFCore.ContentTypeRegistry import NameRegexPredicate
00179 
00180         return NameRegexPredicate(*args, **kw)
00181 
00182     def test_z2interfaces(self):
00183         from Interface.Verify import verifyClass
00184         from Products.CMFCore.ContentTypeRegistry import NameRegexPredicate
00185         from Products.CMFCore.interfaces.ContentTypeRegistry \
00186                 import ContentTypeRegistryPredicate \
00187                 as IContentTypeRegistryPredicate
00188 
00189         verifyClass(IContentTypeRegistryPredicate, NameRegexPredicate)
00190 
00191     def test_z3interfaces(self):
00192         from zope.interface.verify import verifyClass
00193         from Products.CMFCore.ContentTypeRegistry import NameRegexPredicate
00194         from Products.CMFCore.interfaces import IContentTypeRegistryPredicate
00195 
00196         verifyClass(IContentTypeRegistryPredicate, NameRegexPredicate)
00197 
00198     def test_empty( self ):
00199         pred = self._makeOne('empty')
00200         assert pred.getPatternStr() == 'None'
00201         assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
00202 
00203     def test_simple( self ):
00204         pred = self._makeOne('onlyfoo')
00205         pred.edit( 'foo' )
00206         assert pred.getPatternStr() == 'foo'
00207         assert pred( 'foo', 'text/plain', 'asdfljksadf' )
00208         assert not pred( 'fargo', 'text/plain', 'asdfljksadf' )
00209         assert not pred( 'bar', 'text/plain', 'asdfljksadf' )
00210 
00211     def test_pattern( self ):
00212         pred = self._makeOne('allfwords')
00213         pred.edit( 'f.*' )
00214         assert pred.getPatternStr() == 'f.*'
00215         assert pred( 'foo', 'text/plain', 'asdfljksadf' )
00216         assert pred( 'fargo', 'text/plain', 'asdfljksadf' )
00217         assert not pred( 'bar', 'text/plain', 'asdfljksadf' )
00218 
00219 
00220 class ContentTypeRegistryTests(unittest.TestCase):
00221 
00222     def _makeOne(self, *args, **kw):
00223         from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
00224 
00225         return ContentTypeRegistry(*args, **kw)
00226 
00227     def setUp( self ):
00228         self.reg = self._makeOne()
00229 
00230     def test_z2interfaces(self):
00231         from Interface.Verify import verifyClass
00232         from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
00233         from Products.CMFCore.interfaces.ContentTypeRegistry \
00234                 import ContentTypeRegistry as IContentTypeRegistry
00235 
00236         verifyClass(IContentTypeRegistry, ContentTypeRegistry)
00237 
00238     def test_z3interfaces(self):
00239         from zope.interface.verify import verifyClass
00240         from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
00241         from Products.CMFCore.interfaces import IContentTypeRegistry
00242 
00243         verifyClass(IContentTypeRegistry, ContentTypeRegistry)
00244 
00245     def test_empty( self ):
00246         reg=self.reg
00247         assert reg.findTypeName( 'foo', 'text/plain', 'asdfljksadf' ) is None
00248         assert reg.findTypeName( 'fargo', 'text/plain', 'asdfljksadf' ) is None
00249         assert reg.findTypeName( 'bar', 'text/plain', 'asdfljksadf' ) is None
00250         assert not reg.listPredicates()
00251         self.assertRaises( KeyError, reg.removePredicate, 'xyzzy' )
00252 
00253     def test_reorder( self ):
00254         reg=self.reg
00255         predIDs = ( 'foo', 'bar', 'baz', 'qux' )
00256         for predID in predIDs:
00257             reg.addPredicate( predID, 'name_regex' )
00258         ids = tuple( map( lambda x: x[0], reg.listPredicates() ) )
00259         assert ids == predIDs
00260         reg.reorderPredicate( 'bar', 3 )
00261         ids = tuple( map( lambda x: x[0], reg.listPredicates() ) )
00262         assert ids == ( 'foo', 'baz', 'qux', 'bar' )
00263 
00264     def test_lookup( self ):
00265         reg=self.reg
00266         reg.addPredicate( 'image', 'major_minor' )
00267         reg.getPredicate( 'image' ).edit( 'image', '' )
00268         reg.addPredicate( 'onlyfoo', 'name_regex' )
00269         reg.getPredicate( 'onlyfoo' ).edit( 'foo' )
00270         reg.assignTypeName( 'onlyfoo', 'Foo' )
00271         assert reg.findTypeName( 'foo', 'text/plain', 'asdfljksadf' ) == 'Foo'
00272         assert not reg.findTypeName( 'fargo', 'text/plain', 'asdfljksadf' )
00273         assert not reg.findTypeName( 'bar', 'text/plain', 'asdfljksadf' )
00274         assert reg.findTypeName( 'foo', '', '' ) == 'Foo'
00275         assert reg.findTypeName( 'foo', None, None ) == 'Foo'
00276 
00277 
00278 def test_suite():
00279     return unittest.TestSuite((
00280         unittest.makeSuite(MajorMinorPredicateTests),
00281         unittest.makeSuite(ExtensionPredicateTests),
00282         unittest.makeSuite(MimeTypeRegexPredicateTests),
00283         unittest.makeSuite(NameRegexPredicateTests),
00284         unittest.makeSuite(ContentTypeRegistryTests),
00285         ))
00286 
00287 if __name__ == '__main__':
00288     unittest.main(defaultTest='test_suite')