vendor/CMF/1.6.3/CMFCore

view tests/test_FSPageTemplate.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 from unittest import TestSuite, makeSuite, main
2 import Testing
3 try:
4 import Zope2
5 except ImportError: # BBB: for Zope 2.7
6 import Zope as Zope2
7 Zope2.startup()
9 from os.path import join as path_join
11 from OFS.Folder import Folder
12 from Products.PageTemplates.TALES import Undefined
13 from Products.StandardCacheManagers import RAMCacheManager
15 from Products.CMFCore.FSPageTemplate import FSPageTemplate
16 from Products.CMFCore.FSMetadata import FSMetadata
17 from Products.CMFCore.tests.base.dummy import DummyCachingManager
18 from Products.CMFCore.tests.base.testcase import FSDVTest
19 from Products.CMFCore.tests.base.testcase import RequestTest
20 from Products.CMFCore.tests.base.testcase import SecurityTest
23 class FSPTMaker(FSDVTest):
25 def _makeOne( self, id, filename ):
26 path = path_join(self.skin_path_name, filename)
27 metadata = FSMetadata(path)
28 metadata.read()
29 return FSPageTemplate( id, path, properties=metadata.getProperties() )
32 class FSPageTemplateTests( RequestTest, FSPTMaker ):
34 def setUp(self):
35 FSPTMaker.setUp(self)
36 RequestTest.setUp(self)
38 def tearDown(self):
39 RequestTest.tearDown(self)
40 FSPTMaker.tearDown(self)
42 def test_Call( self ):
44 script = self._makeOne( 'testPT', 'testPT.pt' )
45 script = script.__of__(self.root)
46 self.assertEqual(script(),'foo\n')
48 def test_ContentType(self):
49 script = self._makeOne( 'testXMLPT', 'testXMLPT.pt' )
50 script = script.__of__(self.root)
51 script()
52 self.assertEqual(script.content_type, 'text/xml; charset=utf-8')
53 self.assertEqual(self.RESPONSE.getHeader('content-type'), 'text/xml; charset=utf-8')
54 # purge RESPONSE Content-Type header for new test
55 del self.RESPONSE.headers['content-type']
56 script = self._makeOne( 'testPT', 'testPT.pt' )
57 script = script.__of__(self.root)
58 script()
59 self.assertEqual(script.content_type, 'text/html')
60 self.assertEqual(self.RESPONSE.getHeader('content-type'), 'text/html')
62 def test_ContentTypeOverride(self):
63 script = self._makeOne( 'testPT_utf8', 'testPT_utf8.pt' )
64 script = script.__of__(self.root)
65 script()
66 self.assertEqual( self.RESPONSE.getHeader('content-type')
67 , 'text/html; charset=utf-8')
69 def test_ContentTypeFromFSMetadata(self):
70 # Test to see if a content_type specified in a .metadata file
71 # is respected
72 script = self._makeOne('testPT2', 'testPT2.pt')
73 script = script.__of__(self.root)
74 script()
75 self.assertEqual( self.RESPONSE.getHeader('content-type')
76 , 'text/plain'
77 )
79 def test_BadCall( self ):
80 script = self._makeOne( 'testPTbad', 'testPTbad.pt' )
81 script = script.__of__(self.root)
83 try: # can't use assertRaises, because different types raised.
84 script()
85 except (Undefined, KeyError):
86 pass
87 else:
88 self.fail('Calling a bad template did not raise an exception')
90 def test_caching( self ):
92 # Test HTTP caching headers.
93 self.root.caching_policy_manager = DummyCachingManager()
94 original_len = len( self.RESPONSE.headers )
95 script = self._makeOne('testPT', 'testPT.pt')
96 script = script.__of__(self.root)
97 script()
98 self.failUnless( len( self.RESPONSE.headers ) >= original_len + 2 )
99 self.failUnless( 'foo' in self.RESPONSE.headers.keys() )
100 self.failUnless( 'bar' in self.RESPONSE.headers.keys() )
102 def test_pt_properties( self ):
104 script = self._makeOne( 'testPT', 'testPT.pt' )
105 self.assertEqual( script.pt_source_file(), 'file:%s'
106 % path_join(self.skin_path_name, 'testPT.pt') )
108 def test_foreign_line_endings( self ):
109 # Lead the various line ending files and get their output
110 for fformat in ('unix', 'dos', 'mac'):
111 script = self._makeOne(fformat,
112 'testPT_multiline_python_%s.pt' % fformat)
113 script = script.__of__(self.root)
114 self.assertEqual(script(), 'foo bar spam eggs\n')
116 class FSPageTemplateCustomizationTests( SecurityTest, FSPTMaker ):
118 def setUp( self ):
119 FSPTMaker.setUp(self)
120 SecurityTest.setUp( self )
122 self.root._setObject( 'portal_skins', Folder( 'portal_skins' ) )
123 self.skins = self.root.portal_skins
125 self.skins._setObject( 'custom', Folder( 'custom' ) )
126 self.custom = self.skins.custom
128 self.skins._setObject( 'fsdir', Folder( 'fsdir' ) )
129 self.fsdir = self.skins.fsdir
131 self.fsdir._setObject( 'testPT'
132 , self._makeOne( 'testPT', 'testPT.pt' ) )
134 self.fsPT = self.fsdir.testPT
136 def test_customize( self ):
138 self.fsPT.manage_doCustomize( folder_path='custom' )
140 self.assertEqual( len( self.custom.objectIds() ), 1 )
141 self.failUnless( 'testPT' in self.custom.objectIds() )
143 def test_customize_caching(self):
144 # Test to ensure that cache manager associations survive customizing
145 cache_id = 'gofast'
146 RAMCacheManager.manage_addRAMCacheManager( self.root
147 , cache_id
148 , REQUEST=None
149 )
150 self.fsPT.ZCacheable_setManagerId(cache_id, REQUEST=None)
152 self.assertEqual(self.fsPT.ZCacheable_getManagerId(), cache_id)
154 self.fsPT.manage_doCustomize(folder_path='custom')
155 custom_pt = self.custom.testPT
157 self.assertEqual(custom_pt.ZCacheable_getManagerId(), cache_id)
160 def test_dontExpandOnCreation( self ):
162 self.fsPT.manage_doCustomize( folder_path='custom' )
164 customized = self.custom.testPT
165 self.failIf( customized.expand )
167 def tearDown(self):
168 SecurityTest.tearDown(self)
169 FSPTMaker.tearDown(self)
172 def test_suite():
173 return TestSuite((
174 makeSuite(FSPageTemplateTests),
175 makeSuite(FSPageTemplateCustomizationTests),
176 ))
178 if __name__ == '__main__':
179 main(defaultTest='test_suite')