Back to index

plone3  3.1.7
test_kssview_core.py
Go to the documentation of this file.
00001 # -*- coding: latin-1 -*-
00002 # Copyright (c) 2005-2007
00003 # Authors: KSS Project Contributors (see docs/CREDITS.txt)
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License version 2 as published
00007 # by the Free Software Foundation.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017 # 02111-1307, USA.
00018 
00019 import unittest
00020 import textwrap
00021 from kss.core import KSSUnicodeError
00022 from kss.core.tests.base import KSSViewTestCase
00023 from Testing.ZopeTestCase import FunctionalDocFileSuite
00024 
00025 class TestKSSViewCoreCommandSet(KSSViewTestCase):
00026     
00027     def test_empty(self):
00028         view = self.createView()
00029         commands = view.getCommands()
00030         self.assertEqual(len(commands), 0)
00031 
00032     def test_addCommand(self):
00033         view = self.createView()
00034         commands = view.getCommands()
00035         command = commands.addCommand('replaceInnerHTML', 'selector')
00036         self.assertEqual(len(commands), 1)
00037         self.assertEqual(command.getName(), 'replaceInnerHTML')
00038         self.assertEqual(command.getSelector(), 'selector')
00039         params = command.getParams()
00040         self.assertEqual(len(params), 0)
00041         
00042     # XXX since lxml is gone, the next cases are no problem anymore
00043     # Nevertheless, we test all these cases
00044     
00045     def _checkSetHtmlResult(self, content, content2=None):
00046         view = self.createView()
00047         view.getCommandSet('core').replaceInnerHTML('div.class', content)
00048         commands = view.getCommands()
00049         self.assertEqual(len(commands), 1)
00050         command = commands[0]
00051         self.assertEqual(command.getName(), 'replaceInnerHTML')
00052         self.assertEqual(command.getSelector(), 'div.class')
00053         params = command.getParams()
00054         self.assertEqual(len(params), 2)
00055         self.assertEqual(params[0].getName(), 'html')
00056         self.assertEqual(params[1].getName(), 'withKssSetup')
00057         if content2 == None:
00058             content2 = content
00059         content2 = content2.encode('ascii', 'xmlcharrefreplace')
00060         # wrap content into CDATA
00061         content2 = '<![CDATA[%s]]>' % (content2, )
00062         # and finally check it
00063         self.assertEqual(params[0].getContent().encode('ascii', 'xmlcharrefreplace'), content2)
00064 
00065     def test_replaceInnerHTMLTextPlusEntity(self):
00066         'See if non breaking space entity works'
00067         ##self._checkSetHtmlResult('&nbsp;')
00068         # XXX we remove html named entities now
00069         self._checkSetHtmlResult('&nbsp;', '&#160;')
00070         
00071     def test_replaceInnerHTMLTextPlusEntityOthers(self):
00072         'See if the other HTML entities work as well'
00073         # XXX we remove html named entities now
00074         self._checkSetHtmlResult('<p xmlns="http://www.w3.org/1999/xhtml">&raquo;Hello world!&laquo;</p>',
00075                    '<p xmlns="http://www.w3.org/1999/xhtml">&#187;Hello world!&#171;</p>')
00076 
00077     def test_replaceInnerHTMLTextOnly(self):
00078         self._checkSetHtmlResult('new content')
00079 
00080     def test_replaceInnerHTMLTagOnly(self):
00081         self._checkSetHtmlResult('<p xmlns="http://www.w3.org/1999/xhtml">new_content</p>')
00082         
00083     def test_replaceInnerHTMLTagPlusText(self):
00084         self._checkSetHtmlResult('<p xmlns="http://www.w3.org/1999/xhtml">new_content</p>after')
00085         
00086     def test_replaceInnerHTMLTextTagPlusText(self):
00087         self._checkSetHtmlResult('before<p xmlns="http://www.w3.org/1999/xhtml">new_content</p>after')
00088         
00089     def test_setHtmlAcceptsUnicode(self):
00090         'Test that it accepts unicode'
00091         self._checkSetHtmlResult(u'abcá')
00092         
00093     def test_setHtmlChecksForNonUnicode(self):
00094         'Test that it does not accept non unicode (unless pure ascii)'
00095         self.assertRaises(KSSUnicodeError, self._checkSetHtmlResult, 'abcá')
00096 
00097 class FTestKSSViewCoreCommandSet(KSSViewTestCase):
00098     'Functional tests'
00099 
00100     def _wrapped_commands(self, inline):
00101         header = textwrap.dedent(u'''\
00102                 <?xml version="1.0" ?>
00103                 <kukit xmlns="http://www.kukit.org/commands/1.1">
00104                 <commands>
00105                 ''')
00106         footer = textwrap.dedent('''\
00107                 </commands>
00108                 </kukit>
00109                 ''')
00110         return header + inline + footer
00111 
00112     def assertXMLEquals(self, a, b):
00113         self.assertEqual(a, b)
00114 
00115     def assertCommandsEqual(self, a, b):
00116         self.assertXMLEquals(a, self._wrapped_commands(b))
00117 
00118     def test_empty(self):
00119         view = self.createView()
00120         result = view.render()
00121         self.assertEquals(view.request.response.getHeader('content-type'), 'text/xml;charset=utf-8')
00122         self.assertCommandsEqual(result, '')
00123     
00124     def test_replaceInnerHTML(self):
00125         view = self.createView()
00126         view.getCommandSet('core').replaceInnerHTML('div.class', 'new content')
00127         result = view.render()
00128         awaited = u'''\
00129 <command selector="div.class" name="replaceInnerHTML"
00130          selectorType="">
00131     <param name="html"><![CDATA[new content]]></param>
00132     <param name="withKssSetup">True</param>
00133 </command>
00134 '''
00135         self.assertCommandsEqual(result, awaited)
00136         
00137     def test_setCommandSet(self):
00138         view = self.createView()
00139         cs = view.getCommandSet('core')
00140         cs.replaceInnerHTML('div.class', 'new content')
00141         result = view.render()
00142         awaited = u'''\
00143 <command selector="div.class" name="replaceInnerHTML"
00144          selectorType="">
00145     <param name="html"><![CDATA[new content]]></param>
00146     <param name="withKssSetup">True</param>
00147 </command>
00148 '''
00149         self.assertCommandsEqual(result, awaited)
00150 
00151 def afterSetUp(self):
00152     KSSViewTestCase.afterSetUp(self)
00153     self.setDebugRequest()
00154 
00155 
00156 def test_suite():
00157     suites = []
00158     suites.append(unittest.makeSuite(TestKSSViewCoreCommandSet))
00159     suites.append(unittest.makeSuite(FTestKSSViewCoreCommandSet))
00160     suites.append(FunctionalDocFileSuite('../actionwrapper.py',
00161                                          test_class=KSSViewTestCase,
00162                                          setUp=afterSetUp,
00163                                          tearDown=KSSViewTestCase.beforeTearDown.im_func))
00164     return unittest.TestSuite(suites)