Back to index

plone3  3.1.7
test_FSPageTemplate.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 ##############################################################################
00003 #
00004 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00008 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00009 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00010 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00011 # FOR A PARTICULAR PURPOSE.
00012 #
00013 ##############################################################################
00014 """ Unit tests for FSPageTemplate module.
00015 
00016 $Id: test_FSPageTemplate.py 77113 2007-06-26 20:36:26Z yuppie $
00017 """
00018 
00019 import unittest
00020 from Testing import ZopeTestCase
00021 ZopeTestCase.installProduct('PageTemplates', 1)
00022 
00023 from os.path import join as path_join
00024 
00025 from Acquisition import aq_base
00026 from OFS.Folder import Folder
00027 from Products.StandardCacheManagers import RAMCacheManager
00028 from zope.tales.tales import Undefined
00029 from zope.testing.cleanup import cleanUp
00030 
00031 from Products.CMFCore.FSPageTemplate import FSPageTemplate
00032 from Products.CMFCore.FSMetadata import FSMetadata
00033 from Products.CMFCore.testing import TraversingZCMLLayer
00034 from Products.CMFCore.tests.base.dummy import DummyCachingManager
00035 from Products.CMFCore.tests.base.testcase import FSDVTest
00036 from Products.CMFCore.tests.base.testcase import RequestTest
00037 from Products.CMFCore.tests.base.testcase import SecurityTest
00038 
00039 
00040 class FSPTMaker(FSDVTest):
00041 
00042     def _makeOne( self, id, filename ):
00043         path = path_join(self.skin_path_name, filename)
00044         metadata = FSMetadata(path)
00045         metadata.read()
00046         return FSPageTemplate( id, path, properties=metadata.getProperties() )
00047 
00048 
00049 class FSPageTemplateTests( RequestTest, FSPTMaker ):
00050 
00051     layer = TraversingZCMLLayer
00052 
00053     def setUp(self):
00054         FSPTMaker.setUp(self)
00055         RequestTest.setUp(self)
00056 
00057     def tearDown(self):
00058         RequestTest.tearDown(self)
00059         FSPTMaker.tearDown(self)
00060 
00061     def _setupCachingPolicyManager(self, cpm_object):
00062         self.root.caching_policy_manager = cpm_object
00063 
00064     def test_Call( self ):
00065         script = self._makeOne( 'testPT', 'testPT.pt' )
00066         script = script.__of__(self.app)
00067         self.assertEqual(script(), 'nohost\n')
00068 
00069     def test_ContentType(self):
00070         script = self._makeOne( 'testXMLPT', 'testXMLPT.pt' )
00071         script = script.__of__(self.root)
00072         script()
00073         self.assertEqual(script.content_type, 'text/xml; charset=utf-8')
00074         self.assertEqual(self.RESPONSE.getHeader('content-type'), 'text/xml; charset=utf-8')
00075         # purge RESPONSE Content-Type header for new test
00076         del self.RESPONSE.headers['content-type']
00077         script = self._makeOne( 'testPT', 'testPT.pt' )
00078         script = script.__of__(self.root)
00079         script()
00080         self.assertEqual(script.content_type, 'text/html')
00081         self.assertEqual(self.RESPONSE.getHeader('content-type'), 'text/html')
00082 
00083     def test_ContentTypeOverride(self):
00084         script = self._makeOne( 'testPT_utf8', 'testPT_utf8.pt' )
00085         script = script.__of__(self.root)
00086         script()
00087         self.assertEqual( self.RESPONSE.getHeader('content-type')
00088                         , 'text/html; charset=utf-8')
00089 
00090     def test_ContentTypeFromFSMetadata(self):
00091         # Test to see if a content_type specified in a .metadata file
00092         # is respected
00093         script = self._makeOne('testPT2', 'testPT2.pt')
00094         script = script.__of__(self.root)
00095         script()
00096         self.assertEqual( self.RESPONSE.getHeader('content-type')
00097                         , 'text/xml'
00098                         )
00099     def test_CharsetFromFSMetadata(self):
00100         # testPT3 is an UTF-16 encoded file (see its .metadatafile)
00101         # is respected
00102         script = self._makeOne('testPT3', 'testPT3.pt')
00103         script = script.__of__(self.root)
00104         data = script.read()
00105         self.failUnless(u'123' in data)
00106         self.assertEqual(script.content_type, 'text/html')
00107 
00108     def test_CharsetFrom2FSMetadata(self):
00109         # testPT4 is an UTF-8 encoded file (see its .metadatafile)
00110         # is respected
00111         script = self._makeOne('testPT4', 'testPT4.pt')
00112         script = script.__of__(self.root)
00113         data = script.read()
00114         self.failUnless(u'123' in data)
00115         self.assertEqual(script.content_type, 'text/html')
00116 
00117     def test_BadCall( self ):
00118         script = self._makeOne( 'testPTbad', 'testPTbad.pt' )
00119         script = script.__of__(self.root)
00120 
00121         try: # can't use assertRaises, because different types raised.
00122             script()
00123         except (Undefined, KeyError):
00124             pass
00125         else:
00126             self.fail('Calling a bad template did not raise an exception')
00127 
00128     def test_caching( self ):
00129 
00130         #   Test HTTP caching headers.
00131         self._setupCachingPolicyManager(DummyCachingManager())
00132         original_len = len( self.RESPONSE.headers )
00133         script = self._makeOne('testPT', 'testPT.pt')
00134         script = script.__of__(self.root)
00135         script()
00136         self.failUnless( len( self.RESPONSE.headers ) >= original_len + 2 )
00137         self.failUnless( 'foo' in self.RESPONSE.headers.keys() )
00138         self.failUnless( 'bar' in self.RESPONSE.headers.keys() )
00139 
00140     def test_pt_properties( self ):
00141 
00142         script = self._makeOne( 'testPT', 'testPT.pt' )
00143         self.assertEqual( script.pt_source_file(), 'file:%s'
00144                                % path_join(self.skin_path_name, 'testPT.pt') )
00145 
00146     def test_foreign_line_endings( self ):
00147         # Lead the various line ending files and get their output
00148         for fformat in ('unix', 'dos', 'mac'):
00149             script = self._makeOne(fformat,
00150                                    'testPT_multiline_python_%s.pt' % fformat)
00151             script = script.__of__(self.root)
00152             self.assertEqual(script(), 'foo bar spam eggs\n')
00153 
00154 
00155 class FSPageTemplateCustomizationTests( SecurityTest, FSPTMaker ):
00156 
00157     def setUp(self):
00158         FSPTMaker.setUp(self)
00159         SecurityTest.setUp(self)
00160 
00161         self.root._setObject( 'portal_skins', Folder( 'portal_skins' ) )
00162         self.skins = self.root.portal_skins
00163 
00164         self.skins._setObject( 'custom', Folder( 'custom' ) )
00165         self.custom = self.skins.custom
00166 
00167         self.skins._setObject( 'fsdir', Folder( 'fsdir' ) )
00168         self.fsdir = self.skins.fsdir
00169 
00170         self.fsdir._setObject( 'testPT'
00171                              , self._makeOne( 'testPT', 'testPT.pt' ) )
00172 
00173         self.fsPT = self.fsdir.testPT
00174 
00175     def tearDown(self):
00176         cleanUp()
00177         SecurityTest.tearDown(self)
00178         FSPTMaker.tearDown(self)
00179 
00180     def test_customize( self ):
00181 
00182         self.fsPT.manage_doCustomize( folder_path='custom' )
00183 
00184         self.assertEqual( len( self.custom.objectIds() ), 1 )
00185         self.failUnless( 'testPT' in self.custom.objectIds() )
00186 
00187     def test_customize_alternate_root( self ):
00188 
00189         from OFS.Folder import Folder
00190         self.root.other = Folder('other')
00191 
00192         self.fsPT.manage_doCustomize( folder_path='other', root=self.root )
00193 
00194         self.failIf( 'testPT' in self.custom.objectIds() )  
00195         self.failUnless( 'testPT' in self.root.other.objectIds() )  
00196 
00197     def test_customize_fspath_as_dot( self ):
00198 
00199         self.fsPT.manage_doCustomize( folder_path='.' )
00200 
00201         self.failIf( 'testPT' in self.custom.objectIds() )  
00202         self.failUnless( 'testPT' in self.skins.objectIds() )  
00203 
00204     def test_customize_manual_clone( self ):
00205 
00206         clone = Folder('testPT')
00207 
00208         self.fsPT.manage_doCustomize( folder_path='custom', obj=clone )
00209 
00210         self.failUnless( 'testPT' in self.custom.objectIds() )  
00211         self.failUnless( aq_base(self.custom._getOb('testPT')) is clone )  
00212 
00213     def test_customize_caching(self):
00214         # Test to ensure that cache manager associations survive customizing
00215         cache_id = 'gofast'
00216         RAMCacheManager.manage_addRAMCacheManager( self.root
00217                                                  , cache_id
00218                                                  , REQUEST=None
00219                                                  )
00220         self.fsPT.ZCacheable_setManagerId(cache_id, REQUEST=None)
00221 
00222         self.assertEqual(self.fsPT.ZCacheable_getManagerId(), cache_id)
00223 
00224         self.fsPT.manage_doCustomize(folder_path='custom')
00225         custom_pt = self.custom.testPT
00226 
00227         self.assertEqual(custom_pt.ZCacheable_getManagerId(), cache_id)
00228 
00229 
00230     def test_dontExpandOnCreation( self ):
00231 
00232         self.fsPT.manage_doCustomize( folder_path='custom' )
00233 
00234         customized = self.custom.testPT
00235         self.failIf( customized.expand )
00236 
00237 
00238 def test_suite():
00239     return unittest.TestSuite((
00240         unittest.makeSuite(FSPageTemplateTests),
00241         unittest.makeSuite(FSPageTemplateCustomizationTests),
00242         ))
00243 
00244 if __name__ == '__main__':
00245     from Products.CMFCore.testing import run
00246     run(test_suite())