Back to index

plone3  3.1.7
test_engine.py
Go to the documentation of this file.
00001 
00002 from Testing import ZopeTestCase
00003 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00004 
00005 from Products.PortalTransforms.utils import TransformException
00006 from Products.PortalTransforms.interfaces import *
00007 from Products.PortalTransforms.chain import chain
00008 
00009 import urllib
00010 import time
00011 import re
00012 
00013 class BaseTransform:
00014     def name(self):
00015         return getattr(self, '__name__', self.__class__.__name__)
00016 
00017 
00018 class HtmlToText(BaseTransform):
00019     __implements__ = itransform
00020     inputs = ('text/html',)
00021     output = 'text/plain'
00022 
00023     def __call__(self, orig, **kwargs):
00024         orig = re.sub('<[^>]*>(?i)(?m)', '', orig)
00025         return urllib.unquote(re.sub('\n+', '\n', orig)).strip()
00026 
00027     def convert(self, orig, data, **kwargs):
00028         orig = self.__call__(orig)
00029         data.setData(orig)
00030         return data
00031 
00032 class HtmlToTextWithEncoding(HtmlToText):
00033     output_encoding = 'ascii'
00034 
00035 class FooToBar(BaseTransform):
00036     __implements__ = itransform
00037     inputs = ('text/*',)
00038     output = 'text/plain'
00039 
00040     def __call__(self, orig, **kwargs):
00041         orig = re.sub('foo', 'bar', orig)
00042         return urllib.unquote(re.sub('\n+', '\n', orig)).strip()
00043 
00044     def convert(self, orig, data, **kwargs):
00045         orig = self.__call__(orig)
00046         data.setData(orig)
00047         return data
00048 
00049 class DummyHtmlFilter1(BaseTransform):
00050     __implements__ = itransform
00051     __name__ = 'dummy_html_filter1'
00052     inputs = ('text/html',)
00053     output = 'text/html'
00054 
00055     def convert(self, orig, data, **kwargs):
00056         data.setData("<span class='dummy'>%s</span>" % orig)
00057         return data
00058 
00059 class DummyHtmlFilter2(BaseTransform):
00060     __implements__ = itransform
00061     __name__ = 'dummy_html_filter2'
00062     inputs = ('text/html',)
00063     output = 'text/html'
00064 
00065     def convert(self, orig, data, **kwargs):
00066         data.setData("<div class='dummy'>%s</div>" % orig)
00067         return data
00068 
00069 class TransformNoIO(BaseTransform):
00070     __implements__ = itransform
00071 
00072 class BadTransformMissingImplements(BaseTransform):
00073     __implements__ = None
00074     inputs = ('text/*',)
00075     output = 'text/plain'
00076 
00077 class BadTransformBadMIMEType1(BaseTransform):
00078     __implements__ = itransform
00079     inputs = ('truc/muche',)
00080     output = 'text/plain'
00081 
00082 class BadTransformBadMIMEType2(BaseTransform):
00083     __implements__ = itransform
00084     inputs = ('text/plain',)
00085     output = 'truc/muche'
00086 
00087 class BadTransformNoInput(BaseTransform):
00088     __implements__ = itransform
00089     inputs = ()
00090     output = 'text/plain'
00091 
00092 class BadTransformWildcardOutput(BaseTransform):
00093     __implements__ = itransform
00094     inputs = ('text/plain',)
00095     output = 'text/*'
00096     
00097 class TestEngine(ATSiteTestCase):
00098 
00099     def afterSetUp(self):
00100         ATSiteTestCase.afterSetUp(self)
00101         self.engine = self.portal.portal_transforms
00102         self.data = '<b>foo</b>'
00103 
00104     def register(self):
00105         #A default set of transforms to prove the interfaces work
00106         self.engine.registerTransform(HtmlToText())
00107         self.engine.registerTransform(FooToBar())
00108 
00109     def testRegister(self):
00110         self.register()
00111 
00112     def testFailRegister(self):
00113         register = self.engine.registerTransform
00114         self.assertRaises(TransformException, register, TransformNoIO())
00115         self.assertRaises(TransformException, register, BadTransformMissingImplements())
00116         self.assertRaises(TransformException, register, BadTransformBadMIMEType1())
00117         self.assertRaises(TransformException, register, BadTransformBadMIMEType2())
00118         self.assertRaises(TransformException, register, BadTransformNoInput())
00119         self.assertRaises(TransformException, register, BadTransformWildcardOutput())
00120 
00121     def testCall(self):
00122         self.register()
00123         data = self.engine('HtmlToText', self.data)
00124         self.failUnlessEqual(data, "foo")
00125 
00126         data = self.engine('FooToBar', self.data)
00127         self.failUnlessEqual(data, "<b>bar</b>")
00128 
00129     def testConvert(self):
00130         self.register()
00131 
00132         data = self.engine.convert('HtmlToText', self.data)
00133         self.failUnlessEqual(data.getData(), "foo")
00134         self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
00135         self.failUnlessEqual(data.getMetadata().get('encoding'), None)
00136         self.failUnlessEqual(data.name(), "HtmlToText")
00137 
00138         self.engine.registerTransform(HtmlToTextWithEncoding())
00139         data = self.engine.convert('HtmlToTextWithEncoding', self.data)
00140         self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
00141         self.failUnlessEqual(data.getMetadata()['encoding'], 'ascii')
00142         self.failUnlessEqual(data.name(), "HtmlToTextWithEncoding")
00143 
00144     def testConvertTo(self):
00145         self.register()
00146 
00147         data = self.engine.convertTo('text/plain', self.data, mimetype="text/html")
00148         self.failUnlessEqual(data.getData(), "foo")
00149         self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
00150         self.failUnlessEqual(data.getMetadata().get('encoding'), None)
00151         self.failUnlessEqual(data.name(), "text/plain")
00152 
00153         self.engine.unregisterTransform('HtmlToText')
00154         self.engine.unregisterTransform('FooToBar')
00155         self.engine.registerTransform(HtmlToTextWithEncoding())
00156         data = self.engine.convertTo('text/plain', self.data, mimetype="text/html")
00157         self.failUnlessEqual(data.getMetadata()['mimetype'], 'text/plain')
00158         # HtmlToTextWithEncoding. Now None is the right 
00159         #self.failUnlessEqual(data.getMetadata()['encoding'], 'ascii')
00160         # XXX the new algorithm is choosing html_to_text instead of 
00161         self.failUnlessEqual(data.getMetadata()['encoding'], None)
00162         self.failUnlessEqual(data.name(), "text/plain")
00163 
00164     def testChain(self):
00165         self.register()
00166         hb = chain('hbar')
00167         hb.registerTransform(HtmlToText())
00168         hb.registerTransform(FooToBar())
00169 
00170         self.engine.registerTransform(hb)
00171         cache = self.engine.convert('hbar', self.data)
00172         self.failUnlessEqual(cache.getData(), "bar")
00173         self.failUnlessEqual(cache.name(), "hbar")
00174 
00175     def testPolicy(self):
00176         mt = 'text/x-html-safe'
00177         data = '<script>this_is_unsafe();</script><p>this is safe</p>'
00178         
00179         cache = self.engine.convertTo(mt, data, mimetype='text/html')
00180         self.failUnlessEqual(cache.getData(), '<p>this is safe</p>')
00181 
00182         self.engine.registerTransform(DummyHtmlFilter1())
00183         self.engine.registerTransform(DummyHtmlFilter2())
00184         required = ['dummy_html_filter1', 'dummy_html_filter2']
00185 
00186         self.engine.manage_addPolicy(mt, required)
00187         expected_policy = [('text/x-html-safe',
00188                             ('dummy_html_filter1', 'dummy_html_filter2'))]
00189         self.failUnlessEqual(self.engine.listPolicies(), expected_policy)
00190 
00191         cache = self.engine.convertTo(mt, data, mimetype='text/html')
00192         self.failUnlessEqual(cache.getData(), '<div class="dummy"><span class="dummy"><p>this is safe</p></span></div>')
00193 
00194         self.failUnlessEqual(cache.getMetadata()['mimetype'], mt)
00195         self.failUnlessEqual(cache.name(), mt)
00196 
00197         path = self.engine._findPath('text/html', mt, required)
00198         self.failUnlessEqual(str(path),
00199                              "[<Transform at dummy_html_filter1>, "
00200                              "<Transform at dummy_html_filter2>, "
00201                              "<Transform at safe_html>]")
00202 
00203     def testSame(self):
00204         data = "This is a test"
00205         mt = "text/plain"
00206         out = self.engine.convertTo('text/plain', data, mimetype=mt)
00207         self.failUnlessEqual(out.getData(), data)
00208         self.failUnlessEqual(out.getMetadata()['mimetype'], 'text/plain')
00209 
00210     def testCache(self):
00211         data = "This is a test"
00212         other_data = 'some different data'
00213         mt = "text/plain"
00214         self.engine.max_sec_in_cache = 20
00215         out = self.engine.convertTo(mt, data, mimetype=mt, object=self)
00216         self.failUnlessEqual(out.getData(), data, out.getData())
00217         out = self.engine.convertTo(mt, other_data, mimetype=mt, object=self)
00218         self.failUnlessEqual(out.getData(), data, out.getData())
00219         self.engine.max_sec_in_cache = -1
00220         out = self.engine.convertTo(mt, data, mimetype=mt, object=self)
00221         self.failUnlessEqual(out.getData(), data, out.getData())
00222         out = self.engine.convertTo(mt, other_data, mimetype=mt, object=self)
00223         self.failUnlessEqual(out.getData(), other_data, out.getData())
00224 
00225 
00226 def test_suite():
00227     from unittest import TestSuite, makeSuite
00228     suite = TestSuite()
00229     suite.addTest(makeSuite(TestEngine))
00230     return suite