Back to index

plone3  3.1.7
test_FSSTXMethod.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 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 FSSTXMethod module.
00014 
00015 $Id: test_FSSTXMethod.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 import unittest
00018 import Testing
00019 
00020 import os
00021 import re
00022 
00023 from Acquisition import aq_base
00024 from zope.testing.cleanup import cleanUp
00025 
00026 from Products.CMFCore.testing import TraversingZCMLLayer
00027 from Products.CMFCore.tests.base.testcase import FSDVTest
00028 from Products.CMFCore.tests.base.testcase import RequestTest
00029 from Products.CMFCore.tests.base.testcase import SecurityTest
00030 
00031 
00032 class FSSTXMaker(FSDVTest):
00033 
00034     def _makeOne( self, id, filename ):
00035         from Products.CMFCore.FSMetadata import FSMetadata
00036         from Products.CMFCore.FSSTXMethod import FSSTXMethod
00037         path = os.path.join(self.skin_path_name, filename)
00038         metadata = FSMetadata(path)
00039         metadata.read()
00040         return FSSTXMethod( id, path, properties=metadata.getProperties() )
00041 
00042 _EXPECTED_HTML = """\
00043 <html>
00044 <body>
00045 
00046 <h1>Title Goes Here</h1>
00047 <h2>  Subhead Here</h2>
00048 <p>    And this is a paragraph,
00049     broken across lines.</p>
00050 
00051 </body>
00052 </html>
00053 """
00054 
00055 _TEST_MAIN_TEMPLATE = """\
00056 <html metal:define-macro="master">
00057 <body>
00058 
00059 <metal:block define-slot="body">
00060 BODY GOES HERE
00061 </metal:block>
00062 </body>
00063 </html>
00064 """
00065 
00066 WS = re.compile(r'\s+')
00067 
00068 def _normalize_whitespace(text):
00069     return ' '.join(WS.split(text.rstrip()))
00070 
00071 
00072 class _TemplateSwitcher:
00073 
00074     def setUp(self):
00075         import Products.CMFCore.FSSTXMethod
00076         self._old_STX_TEMPLATE = Products.CMFCore.FSSTXMethod._STX_TEMPLATE
00077 
00078     def tearDown(self):
00079         RequestTest.tearDown(self)
00080         FSSTXMaker.tearDown(self)
00081         self._setWhichTemplate(self._old_STX_TEMPLATE)
00082 
00083     def _setWhichTemplate(self, which):
00084         import Products.CMFCore.FSSTXMethod
00085         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
00086         Products.CMFCore.FSSTXMethod._STX_TEMPLATE = which
00087 
00088         if which == 'DTML':
00089             self.root.standard_html_header = (
00090                     lambda *args, **kw: '<html>\n<body>\n')
00091             self.root.standard_html_footer = (
00092                     lambda *args, **kw: '</body>\n</html>\n')
00093         elif which == 'ZPT':
00094             main = ZopePageTemplate('main_template', _TEST_MAIN_TEMPLATE)
00095             self.root._setOb('main_template', main)
00096 
00097 class FSSTXMethodTests(RequestTest,
00098                        FSSTXMaker,
00099                        _TemplateSwitcher,
00100                       ):
00101 
00102     layer = TraversingZCMLLayer
00103 
00104     def setUp(self):
00105         _TemplateSwitcher.setUp(self)
00106         RequestTest.setUp(self)
00107         FSSTXMaker.setUp(self)
00108 
00109     def tearDown(self):
00110         FSSTXMaker.tearDown(self)
00111         RequestTest.tearDown(self)
00112         _TemplateSwitcher.tearDown(self)
00113 
00114     def test___call___with_DTML( self ):
00115         self._setWhichTemplate('DTML')
00116         script = self._makeOne( 'testSTX', 'testSTX.stx' )
00117         script = script.__of__(self.app)
00118         self.assertEqual(_normalize_whitespace(script(self.REQUEST)),
00119                          _normalize_whitespace(_EXPECTED_HTML))
00120 
00121     def test___call___with_ZPT( self ):
00122         self._setWhichTemplate('ZPT')
00123         script = self._makeOne( 'testSTX', 'testSTX.stx' )
00124         script = script.__of__(self.app)
00125         self.assertEqual(_normalize_whitespace(script(self.REQUEST)),
00126                          _normalize_whitespace(_EXPECTED_HTML))
00127 
00128     def test_caching( self ):
00129         #   Test HTTP caching headers.
00130         from Products.CMFCore.tests.base.dummy import DummyCachingManager
00131         self._setWhichTemplate('DTML')
00132         self.root.caching_policy_manager = DummyCachingManager()
00133         original_len = len( self.RESPONSE.headers )
00134         script = self._makeOne('testSTX', 'testSTX.stx')
00135         script = script.__of__(self.root)
00136         script(self.REQUEST, self.RESPONSE)
00137         self.failUnless( len( self.RESPONSE.headers ) >= original_len + 2 )
00138         self.failUnless( 'foo' in self.RESPONSE.headers.keys() )
00139         self.failUnless( 'bar' in self.RESPONSE.headers.keys() )
00140 
00141     def test_ownership( self ):
00142         script = self._makeOne( 'testSTX', 'testSTX.stx' )
00143         script = script.__of__(self.root)
00144         # FSSTXMethod has no owner
00145         owner_tuple = script.getOwnerTuple()
00146         self.assertEqual(owner_tuple, None)
00147 
00148         # and ownership is not acquired [CMF/450]
00149         self.root._owner= ('/foobar', 'baz')
00150         owner_tuple = script.getOwnerTuple()
00151         self.assertEqual(owner_tuple, None)
00152 
00153     def test_304_response_from_cpm( self ):
00154         # test that we get a 304 response from the cpm via this template
00155         from DateTime import DateTime
00156         from webdav.common import rfc1123_date
00157         from Products.CMFCore.tests.base.dummy \
00158             import DummyCachingManagerWithPolicy
00159 
00160         mod_time = DateTime()
00161         self.root.caching_policy_manager = DummyCachingManagerWithPolicy()
00162         script = self._makeOne('testSTX', 'testSTX.stx')
00163         script = script.__of__(self.root)
00164         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00165                             ] = '%s;' % rfc1123_date( mod_time+3600 )
00166         data = script(self.REQUEST, self.RESPONSE)
00167 
00168         self.assertEqual( data, '' )
00169         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00170 
00171 
00172 ADD_ZPT = 'Add page templates'
00173 ZPT_META_TYPES = ( { 'name'        : 'Page Template'
00174                    , 'action'      : 'manage_addPageTemplate'
00175                    , 'permission'  : ADD_ZPT
00176                    }
00177                  ,
00178                  )
00179 
00180 
00181 class FSSTXMethodCustomizationTests(SecurityTest,
00182                                     FSSTXMaker,
00183                                     _TemplateSwitcher,
00184                                    ):
00185 
00186     def setUp(self):
00187         from OFS.Folder import Folder
00188 
00189         _TemplateSwitcher.setUp(self)
00190         SecurityTest.setUp(self)
00191         FSSTXMaker.setUp(self)
00192 
00193         self.root._setObject( 'portal_skins', Folder( 'portal_skins' ) )
00194         self.skins = self.root.portal_skins
00195 
00196         self.skins._setObject( 'custom', Folder( 'custom' ) )
00197         self.custom = self.skins.custom
00198 
00199         self.skins._setObject( 'fsdir', Folder( 'fsdir' ) )
00200         self.fsdir = self.skins.fsdir
00201 
00202         self.fsdir._setObject( 'testSTX'
00203                              , self._makeOne( 'testSTX', 'testSTX.stx' ) )
00204 
00205         self.fsSTX = self.fsdir.testSTX
00206 
00207     def tearDown(self):
00208         cleanUp()
00209         FSSTXMaker.tearDown(self)
00210         SecurityTest.tearDown(self)
00211         _TemplateSwitcher.tearDown(self)
00212 
00213     def test_customize_alternate_root( self ):
00214         from OFS.Folder import Folder
00215 
00216         self._setWhichTemplate('DTML')
00217         self.root.other = Folder('other')
00218 
00219         self.fsSTX.manage_doCustomize(folder_path='other', root=self.root)
00220 
00221         self.failIf('testSTX' in self.custom.objectIds())
00222         self.failUnless('testSTX' in self.root.other.objectIds())
00223 
00224     def test_customize_fspath_as_dot( self ):
00225         self._setWhichTemplate('DTML')
00226 
00227         self.fsSTX.manage_doCustomize(folder_path='.')
00228 
00229         self.failIf('testSTX' in self.custom.objectIds())
00230         self.failUnless('testSTX' in self.skins.objectIds())
00231 
00232     def test_customize_manual_clone( self ):
00233         from OFS.Folder import Folder
00234 
00235         clone = Folder('testSTX')
00236 
00237         self._setWhichTemplate('DTML')
00238 
00239         self.fsSTX.manage_doCustomize(folder_path='custom', obj=clone)
00240 
00241         self.failUnless('testSTX' in self.custom.objectIds())
00242         self.failUnless(aq_base(self.custom._getOb('testSTX')) is clone)
00243 
00244     def test_customize_with_DTML( self ):
00245         from OFS.DTMLDocument import DTMLDocument
00246         from Products.CMFCore.FSSTXMethod import _CUSTOMIZED_TEMPLATE_DTML
00247 
00248         self._setWhichTemplate('DTML')
00249 
00250         self.fsSTX.manage_doCustomize(folder_path='custom')
00251 
00252         self.assertEqual(len(self.custom.objectIds()), 1)
00253         self.failUnless('testSTX' in self.custom.objectIds())
00254         target = self.custom._getOb('testSTX')
00255 
00256         self.failUnless(isinstance(target, DTMLDocument))
00257 
00258         propinfo = target.propdict()['stx']
00259         self.assertEqual(propinfo['type'], 'text')
00260         self.assertEqual(target.stx, self.fsSTX.raw)
00261 
00262         self.assertEqual(target.document_src(), _CUSTOMIZED_TEMPLATE_DTML)
00263 
00264     def test_customize_with_ZPT( self ):
00265         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
00266         from Products.CMFCore.FSSTXMethod import _CUSTOMIZED_TEMPLATE_ZPT
00267 
00268         self._setWhichTemplate('ZPT')
00269         self.custom.all_meta_types = ZPT_META_TYPES
00270 
00271         self.fsSTX.manage_doCustomize(folder_path='custom')
00272 
00273         self.assertEqual(len(self.custom.objectIds()), 1)
00274         self.failUnless('testSTX' in self.custom.objectIds())
00275         target = self.custom._getOb('testSTX')
00276 
00277         self.failUnless(isinstance(target, ZopePageTemplate))
00278 
00279         propinfo = target.propdict()['stx']
00280         self.assertEqual(propinfo['type'], 'text')
00281         self.assertEqual(target.stx, self.fsSTX.raw)
00282 
00283         self.assertEqual(_normalize_whitespace(target.document_src()),
00284                          _normalize_whitespace(_CUSTOMIZED_TEMPLATE_ZPT))
00285 
00286     def test_customize_caching(self):
00287         # Test to ensure that cache manager associations survive customizing
00288         from Products.StandardCacheManagers import RAMCacheManager
00289         cache_id = 'gofast'
00290         self._setWhichTemplate('ZPT')
00291         self.custom.all_meta_types = ZPT_META_TYPES
00292         RAMCacheManager.manage_addRAMCacheManager( self.root
00293                                                  , cache_id
00294                                                  , REQUEST=None
00295                                                  )
00296         self.fsSTX.ZCacheable_setManagerId(cache_id, REQUEST=None)
00297 
00298         self.assertEqual(self.fsSTX.ZCacheable_getManagerId(), cache_id)
00299 
00300         self.fsSTX.manage_doCustomize(folder_path='custom')
00301         custom_pt = self.custom.testSTX
00302 
00303         self.assertEqual(custom_pt.ZCacheable_getManagerId(), cache_id)
00304 
00305 
00306 def test_suite():
00307     return unittest.TestSuite((
00308         unittest.makeSuite(FSSTXMethodTests),
00309         unittest.makeSuite(FSSTXMethodCustomizationTests),
00310         ))
00311 
00312 if __name__ == '__main__':
00313     from Products.CMFCore.testing import run
00314     run(test_suite())