Back to index

plone3  3.1.7
test_intelligenttext.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 from Testing import ZopeTestCase
00003 
00004 from Products.CMFCore.utils import getToolByName
00005 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00006 
00007 class TransformTestCase(ATSiteTestCase):
00008     
00009     def afterSetUp(self):
00010         ATSiteTestCase.afterSetUp(self)
00011         self.transforms = self.portal.portal_transforms
00012 
00013 
00014 class TestIntelligentTextToHtml(TransformTestCase):
00015 
00016     def performTransform(self, orig, targetMimetype = 'text/html', mimetype='text/x-web-intelligent'):
00017         return self.transforms.convertTo(targetMimetype, orig, context=self.portal, mimetype=mimetype).getData()
00018     
00019     def testHyperlinks(self):
00020         orig = "A test http://test.com"
00021         new = self.performTransform(orig)
00022         self.assertEqual(new, 'A test <a href="http://test.com" rel="nofollow">http://test.com</a>')
00023 
00024     def testMailto(self):
00025         orig = "A test test@test.com of mailto"
00026         new = self.performTransform(orig)
00027         self.assertEqual(new, 'A test <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a> of mailto')
00028     
00029     def testTextAndLinks(self):
00030         orig = """A test
00031 URL: http://test.com End
00032 Mail: test@test.com End
00033 URL: http://foo.com End"""
00034         new = self.performTransform(orig)
00035         self.assertEqual(new, 'A test<br />' \
00036                               'URL: <a href="http://test.com" rel="nofollow">http://test.com</a> End<br />' \
00037                               'Mail: <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a> End<br />' \
00038                               'URL: <a href="http://foo.com" rel="nofollow">http://foo.com</a> End')
00039                               
00040     def testTextAndLinksAtEndOfLine(self):
00041         orig = """A test
00042 URL: http://test.com
00043 Mail: test@test.com
00044 URL: http://foo.com"""
00045         new = self.performTransform(orig)
00046         self.assertEqual(new, 'A test<br />' \
00047                               'URL: <a href="http://test.com" rel="nofollow">http://test.com</a><br />' \
00048                               'Mail: <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a><br />' \
00049                               'URL: <a href="http://foo.com" rel="nofollow">http://foo.com</a>')
00050         
00051         
00052     def testIndents(self):
00053         orig = """A test
00054   URL: http://test.com
00055     Mail: test@test.com
00056       URL: http://foo.com"""
00057         new = self.performTransform(orig)
00058         self.assertEqual(new, 'A test<br />' \
00059                               '&nbsp;&nbsp;URL: <a href="http://test.com" rel="nofollow">http://test.com</a><br />' \
00060                               '&nbsp;&nbsp;&nbsp;&nbsp;Mail: <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a><br />' \
00061                               '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL: <a href="http://foo.com" rel="nofollow">http://foo.com</a>')
00062 
00063     
00064     def testEntities(self):
00065         orig = "Some & funny < characters"
00066         new = self.performTransform(orig)
00067         self.assertEqual(new, "Some &amp; funny &lt; characters")
00068         
00069         
00070     def testAccentuatedCharacters(self):
00071         orig = "The French use é à ô ù à and ç"
00072         new = self.performTransform(orig)
00073         self.assertEqual(new, "The French use &eacute; &agrave; &ocirc; &ugrave; &agrave; and &ccedil;")
00074 
00075 class TestHtmlToIntelligentText(TransformTestCase):
00076 
00077     def performTransform(self, orig, targetMimetype = 'text/x-web-intelligent', mimetype='text/html'):
00078         return self.transforms.convertTo(targetMimetype, orig, context=self.portal, mimetype=mimetype).getData()
00079 
00080     def testStripTags(self):
00081         orig = "Some <b>bold</b> text."
00082         new = self.performTransform(orig)
00083         self.assertEqual(new, "Some bold text.")
00084     
00085     def testBreaks(self):
00086         orig = "Some<br/>broken<BR/>text<br />"
00087         new = self.performTransform(orig)
00088         self.assertEqual(new, "Some\nbroken\ntext\n")
00089     
00090     def testStartBlocks(self):
00091         orig = "A block<dt>there</dt>"
00092         new = self.performTransform(orig)
00093         self.assertEqual(new, "A block\n\nthere")
00094     
00095     def testEndBlocks(self):
00096         orig = "<p>Paragraph</p>Other stuff"
00097         new = self.performTransform(orig)
00098         self.assertEqual(new, "Paragraph\n\nOther stuff")
00099         
00100     def testIndentBlocks(self):
00101         orig = "A<blockquote>Indented blockquote</blockquote>"
00102         new = self.performTransform(orig)
00103         self.assertEqual(new, "A\n\n  Indented blockquote")
00104     
00105     def testListBlocks(self):
00106         orig = "A list<ul><li>Foo</li><li>Bar</li></ul>"
00107         new = self.performTransform(orig)
00108         self.assertEqual(new, "A list\n\n  - Foo\n\n  - Bar\n\n")
00109         
00110     def testNbsp(self):
00111         orig = "Some space &nbsp;&nbsp;here"
00112         new = self.performTransform(orig)
00113         self.assertEqual(new, "Some space   here")
00114         
00115     def testAngles(self):
00116         orig = "Watch &lt;this&gt; and &lsaquo;that&rsaquo;"
00117         new = self.performTransform(orig)
00118         self.assertEqual(new, "Watch <this> and &#8249;that&#8250;")
00119     
00120     def testBullets(self):
00121         orig = "A &bull; bullet"
00122         new = self.performTransform(orig)
00123         self.assertEqual(new, "A &#8226; bullet")
00124         
00125     def testAmpersands(self):
00126         orig = "An &amp; ampersand"
00127         new = self.performTransform(orig)
00128         self.assertEqual(new, "An & ampersand")
00129         
00130     def testEntities(self):
00131         orig = "A &mdash; dash"
00132         new = self.performTransform(orig)
00133         self.assertEqual(new, "A &#8212; dash")
00134         
00135     def testPre(self):
00136         orig = "A <pre>  pre\n  section</pre>"
00137         new = self.performTransform(orig)
00138         self.assertEqual(new, "A \n\n  pre\n  section\n\n")
00139         
00140     def testWhitespace(self):
00141         orig = "A \n\t spaceful, <b>  tag-filled</b>, <b> <i>  snippet\n</b></i>"
00142         new = self.performTransform(orig)
00143         self.assertEqual(new, "A spaceful, tag-filled, snippet ")
00144 
00145 
00146 def test_suite():
00147     from unittest import TestSuite, makeSuite
00148     suite = TestSuite()
00149     suite.addTest(makeSuite(TestIntelligentTextToHtml))
00150     suite.addTest(makeSuite(TestHtmlToIntelligentText))
00151     return suite