Back to index

plone3  3.1.7
test_fields.py
Go to the documentation of this file.
00001 # Copyright (c) 2006
00002 # Authors:
00003 #   Jean-Paul Ladage <j.ladage@zestsoftware.nl>
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 
00020 import unittest
00021 from Products.PloneTestCase import PloneTestCase
00022 from plone.app.kss.tests.kss_and_plone_layer import KSSAndPloneTestCase
00023 from zope import component
00024 from zope.component import adapter
00025 from kss.core.interfaces import IKSSView
00026 from archetypes.kss.interfaces import IVersionedFieldModifiedEvent
00027 from kss.core.BeautifulSoup import BeautifulSoup
00028 
00029 PloneTestCase.setupPloneSite()
00030 
00031 @adapter(None, IKSSView, IVersionedFieldModifiedEvent)
00032 def field_modified_handler(ob, view, event):
00033     ob._eventCaught = True
00034 
00035 class FieldsViewTestCase(KSSAndPloneTestCase):
00036 
00037     def afterSetUp(self):
00038         PloneTestCase.PloneTestCase.afterSetUp(self)
00039         # commands will be rendered as data structures,
00040         self.setDebugRequest()
00041         self.loginAsPortalOwner()
00042         context = self.portal['front-page']
00043         # Set up a view 
00044         self.view = context.restrictedTraverse('saveField')
00045 
00046     # --
00047     # test the KSS methods
00048     # --
00049 
00050     def testReplaceField(self):
00051         self.view.context.changeSkin('Plone Default', self.view.request)
00052         result = self.view.replaceField('title', 'kss_generic_macros', 'title-field-view')
00053         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00054                              for r in result], [
00055             ('setStyle', '.portalMessage', 'css'),
00056             ('replaceInnerHTML', 'kssPortalMessage', 'htmlid'),
00057             ('setAttribute', 'kssPortalMessage', 'htmlid'),
00058             ('setStyle', 'kssPortalMessage', 'htmlid'),
00059             ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00060             ('focus', '#parent-fieldname-title .firstToFocus', '')
00061         ])
00062 
00063     def testReplaceWithView(self):
00064         self.view.context.changeSkin('Plone Default', self.view.request)
00065         result = self.view.replaceWithView('title', 'kss_generic_macros', 'title-field-view')
00066         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00067                              for r in result], [
00068              ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00069             ])
00070 
00071     # XXX this test should test without events, so it should stop events listening
00072     # but we have no more method for that
00073     def testSaveField(self):
00074         view = self.view
00075         result = view.saveField('title', {'title':'My Title'}, 
00076                                 'kss_generic_macros', 'title-field-view')
00077         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00078                              for r in result], [
00079              ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00080             ])
00081         self.assertEqual('My Title', self.portal['front-page'].Title())
00082         res = view.saveField('description',
00083                              {'description':'Woot a funky description!'},
00084                              'kss_generic_macros', 'description-field-view')
00085         self.assertEqual('Woot a funky description!', self.portal['front-page'].Description())
00086     
00087 
00088     # XXX note how these tests are wwrong, obviously events are not listened in this setup
00089     def testSaveFieldWithEvents(self):
00090         view = self.view
00091         result = view.saveField('title', {'title':'My Title'}, 
00092                                 'kss_generic_macros', 'title-field-view')
00093         self.assertEqual('My Title', self.portal['front-page'].Title())
00094         res = view.saveField('description',
00095                              {'description':'Woot a funky description!'},
00096                              'kss_generic_macros', 'description-field-view')
00097         self.assertEqual('Woot a funky description!', self.portal['front-page'].Description())
00098 
00099 
00100     def testSaveFieldWithValueFromRequest(self):
00101         view = self.view
00102         view.request.form['title'] = 'My Title'
00103         view.request.form['description'] = 'Woot a funky description!'
00104         result = view.saveField('title', None, 
00105                                 'kss_generic_macros', 'title-field-view')
00106         self.assertEqual('My Title', self.portal['front-page'].Title())
00107         res = view.saveField('description',
00108                              None,
00109                              'kss_generic_macros', 'description-field-view')
00110         self.assertEqual('Woot a funky description!', 
00111                          self.portal['front-page'].Description())
00112 
00113     # XXX this test would only run, if events are really listened 
00114     # (which they are not) 
00115     def _XXX_testSaveFieldWithVersioning(self):
00116         view = self.view
00117         component.provideHandler(field_modified_handler)
00118         try:
00119             res = view.saveField('title', {'title':'My Title'}, 
00120                                     'kss_generic_macros', 'title-field-view')
00121             self.assert_(getattr(view.context, '_eventCaught', False))
00122             view.context._eventCaught = False
00123             res = view.saveField('description',
00124                                  {'description':'Woot a funky description!'},
00125                                  'kss_generic_macros', 'description-field-view')
00126             self.assert_(getattr(view.context, '_eventCaught', False))
00127         finally:
00128             sm = component.getSiteManager()
00129             sm.unregisterHandler(field_modified_handler)
00130 
00131     # XXX these tests with a provided target node id are a bit silly because the feature
00132     # is not currently in use in any templates -- we provide a target node id of
00133     # 'parent-fieldname-title' which is the default anyway -- but the tests make sure
00134     # this api extension work to some degree.
00135     
00136     def testReplaceFieldWithProvidedTargetNodeId(self):
00137         self.view.context.changeSkin('Plone Default', self.view.request)
00138         target = 'parent-fieldname-title'
00139         result = self.view.replaceField('title', 'kss_generic_macros', 'title-field-view', target=target)
00140         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00141                              for r in result], [
00142             ('setStyle', '.portalMessage', 'css'),
00143             ('replaceInnerHTML', 'kssPortalMessage', 'htmlid'),
00144             ('setAttribute', 'kssPortalMessage', 'htmlid'),
00145             ('setStyle', 'kssPortalMessage', 'htmlid'),
00146             ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00147             ('focus', '#parent-fieldname-title .firstToFocus', '')
00148         ])
00149 
00150     def testReplaceWithViewWithProvidedTargetNodeId(self):
00151         self.view.context.changeSkin('Plone Default', self.view.request)
00152         target = 'parent-fieldname-title'
00153         result = self.view.replaceWithView('title', 'kss_generic_macros', 'title-field-view', target=target)
00154         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00155                              for r in result], [
00156              ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00157             ])
00158 
00159     def testSaveFieldWithProvidedTargetNodeId(self):
00160         view = self.view
00161         target = 'parent-fieldname-title'
00162         result = view.saveField('title', {'title':'My Title'}, 
00163                                 'kss_generic_macros', 'title-field-view', target=target)
00164         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00165                              for r in result], [
00166              ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00167             ])
00168         self.assertEqual('My Title', self.portal['front-page'].Title())
00169 
00170     def testReplaceFieldWithProvidedContext(self):
00171         # set the global context to /news
00172         context = self.portal['news']
00173         view = context.restrictedTraverse('saveField')
00174         context.changeSkin('Plone Default', view.request)
00175 
00176         frontpage_uid = self.portal['front-page'].UID()
00177         result = view.replaceField('title', 'kss_generic_macros', 'title-field-view', uid=frontpage_uid)
00178 
00179         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00180                              for r in result], [
00181             ('setStyle', '.portalMessage', 'css'),
00182             ('replaceInnerHTML', 'kssPortalMessage', 'htmlid'),
00183             ('setAttribute', 'kssPortalMessage', 'htmlid'),
00184             ('setStyle', 'kssPortalMessage', 'htmlid'),
00185             ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00186             ('focus', '#parent-fieldname-title .firstToFocus', '')
00187         ])
00188 
00189         # make sure we've got the right context:
00190         replaceHTML = ''.join([r['params'].get('html', '') for r in result])
00191         self.assertEqual(u"Welcome to Plone" in replaceHTML, True)       
00192 
00193     def testReplaceWithViewWithProvidedContext(self):
00194         # set the global context to /news
00195         context = self.portal['news']
00196         view = context.restrictedTraverse('saveField')
00197         context.changeSkin('Plone Default', view.request)
00198 
00199         frontpage_uid = self.portal['front-page'].UID()
00200         result = view.replaceWithView('title', 'kss_generic_macros', 'title-field-view', uid=frontpage_uid)
00201 
00202         self.assertEqual([(r['name'], r['selector'], r['selectorType'])
00203                              for r in result], [
00204              ('replaceHTML', 'parent-fieldname-title', 'htmlid'),
00205             ])
00206 
00207         # make sure we've got the right context:
00208         replaceHTML = ''.join([r['params'].get('html', '') for r in result])
00209         self.assertEqual(u"Welcome to Plone" in replaceHTML, True)       
00210 
00211     def testSaveFieldWithProvidedContext(self):
00212         # set the global context to /news
00213         context = self.portal['news']
00214         view = context.restrictedTraverse('saveField')
00215         context.changeSkin('Plone Default', view.request)
00216 
00217         frontpage_uid = self.portal['front-page'].UID()
00218         result = view.saveField('title', {'title': 'My Title'},
00219                                 'kss_generic_macros', 'title-field-view', uid=frontpage_uid)
00220 
00221         self.assertEqual('My Title', self.portal['front-page'].Title())
00222 
00223     
00224     def testMarkerInATField(self):
00225         # writeable
00226         view = self.portal['front-page'].restrictedTraverse('kss_field_decorator_view')
00227         result = view.getKssClasses('title')
00228         self.assertEqual(result, ' kssattr-atfieldname-title')
00229         result = view.getKssClasses('title', 'template')
00230         self.assertEqual(result, ' kssattr-atfieldname-title kssattr-templateId-template')
00231         result = view.getKssClasses('title', 'template', 'macro')
00232         self.assertEqual(result, ' kssattr-atfieldname-title kssattr-templateId-template kssattr-macro-macro')
00233         self.logout()
00234         result = view.getKssClasses('title')
00235         # not writeable
00236         self.assertEqual(result, '')
00237 
00238     def testMarkerInATFieldInlineEditable(self):
00239         # writeable
00240         view = self.portal['front-page'].restrictedTraverse('kss_field_decorator_view')
00241         result = view.getKssClassesInlineEditable('title', 'template')
00242         self.assertEqual(result, ' kssattr-atfieldname-title kssattr-templateId-template inlineEditable')
00243         result = view.getKssClassesInlineEditable('title', 'template', 'macro')
00244         self.assertEqual(result, ' kssattr-atfieldname-title kssattr-templateId-template kssattr-macro-macro inlineEditable')
00245         self.logout()
00246         # not writeable
00247         result = view.getKssClassesInlineEditable('title', 'template')
00248         self.assertEqual(result, '')
00249 
00250     def testMarkerInNonATField(self):
00251         # portal root is not an AT object
00252         view = self.portal.restrictedTraverse('kss_field_decorator_view')
00253         result = view.getKssClasses('title')
00254         # not writeable
00255         self.assertEqual(result, '')
00256 
00257     def testVersionPreviewIsNotInlineEditable(self):
00258         """If the kss_inline_editable variable is defined to False
00259         in a page template, all the fields will be globally prohibited 
00260         to be editable. This works via the getKssClasses method.
00261         Similarly, is suppress_preview is set to true, inline
00262         editing is prohibited. This is set from CMFEditions, in the
00263         versions_history_form.
00264 
00265         In this test we check that the versions history is not
00266         inline editable at all.
00267         """
00268         obj = self.portal['front-page']
00269         # Make sure we actually have a revision
00270         pr = self.portal.portal_repository
00271         pr.save(obj)
00272         # Render versions history of the front page
00273         obj.REQUEST.form['version_id'] = '0'
00274         rendered = obj.versions_history_form()
00275         soup = BeautifulSoup(rendered)
00276         # check that inline edit is not active, by looking at title
00277         tag = soup.find(id='parent-fieldname-title')
00278         klass = tag['class']
00279         # just to check that we are looking at the right bit...
00280         # XXX but this is no more, we can't check it
00281         #self.assert_('documentFirstHeading' in klass)
00282         # ... and now see we are really not inline editable:
00283         self.assert_('inlineEditable' not in klass)
00284         # make sure the rest is still there or instant validation and possibly
00285         # other stuff will fail
00286         self.assert_('kssattr-templateId-' in klass)
00287         self.assert_('kssattr-macro-' in klass)
00288 
00289 def test_suite():
00290     return unittest.TestSuite((
00291         unittest.makeSuite(FieldsViewTestCase),
00292         ))