Back to index

plone3  3.1.7
test_DynamicType.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 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 DynamicType module.
00014 
00015 $Id: test_DynamicType.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from StringIO import StringIO
00022 
00023 from Acquisition import Implicit
00024 from Products.Five.browser import BrowserView
00025 from zope.component import getSiteManager
00026 from zope.component import provideAdapter
00027 from zope.component.interfaces import IDefaultViewName
00028 from zope.interface import alsoProvides
00029 from zope.publisher.interfaces.browser import IBrowserRequest
00030 from zope.publisher.interfaces.browser import IBrowserView
00031 from zope.testing.cleanup import cleanUp
00032 from ZPublisher.HTTPRequest import HTTPRequest
00033 from ZPublisher.HTTPResponse import HTTPResponse
00034 
00035 from Products.CMFCore.DynamicType import DynamicType
00036 from Products.CMFCore.tests.base.dummy import DummyObject
00037 from Products.CMFCore.tests.base.dummy import DummySite
00038 from Products.CMFCore.tests.base.dummy import DummyTool
00039 from Products.CMFCore.tests.base.testcase import SecurityRequestTest
00040 from Products.CMFCore.tests.base.tidata import FTIDATA_CMF15
00041 from Products.CMFCore.TypesTool import FactoryTypeInformation as FTI
00042 from Products.CMFCore.TypesTool import TypesTool
00043 
00044 
00045 def defineDefaultViewName(name, for_=None):
00046     provideAdapter(name, (for_, IBrowserRequest), IDefaultViewName, '')
00047 
00048 
00049 class DummyContent(DynamicType, Implicit):
00050     """ Basic dynamic content class.
00051     """
00052 
00053     portal_type = 'Dummy Content 15'
00054 
00055 
00056 class DummyView(BrowserView):
00057     """This is a view"""
00058 
00059 
00060 class DynamicTypeTests(unittest.TestCase):
00061 
00062     def test_z2interfaces(self):
00063         from Interface.Verify import verifyClass
00064         from Products.CMFCore.interfaces.Dynamic \
00065                 import DynamicType as IDynamicType
00066 
00067         verifyClass(IDynamicType, DynamicType)
00068 
00069     def test_z3interfaces(self):
00070         from zope.interface.verify import verifyClass
00071         from Products.CMFCore.interfaces import IDynamicType
00072 
00073         verifyClass(IDynamicType, DynamicType)
00074 
00075 
00076 class DynamicTypeDefaultTraversalTests(unittest.TestCase):
00077 
00078     def setUp(self):
00079         self.site = DummySite('site')
00080         self.site._setObject( 'portal_types', TypesTool() )
00081         fti = FTIDATA_CMF15[0].copy()
00082         self.site.portal_types._setObject( 'Dummy Content 15', FTI(**fti) )
00083         self.site._setObject( 'foo', DummyContent() )
00084         dummy_view = self.site._setObject( 'dummy_view', DummyObject() )
00085 
00086     def tearDown(self):
00087         cleanUp()
00088 
00089     def test_default_view_from_fti(self):
00090         response = HTTPResponse()
00091         environment = { 'URL': '',
00092                         'PARENTS': [self.site],
00093                         'REQUEST_METHOD': 'GET',
00094                         'SERVER_PORT': '80',
00095                         'REQUEST_METHOD': 'GET',
00096                         'steps': [],
00097                         'SERVER_NAME': 'localhost',
00098                         '_hacked_path': 0 }
00099         r = HTTPRequest(StringIO(), environment, response)
00100         r.other.update(environment)
00101         alsoProvides(r, IBrowserRequest)
00102 
00103         r.traverse('foo')
00104         self.assertEqual( r.URL, '/foo/dummy_view' )
00105         self.assertEqual( r.response.base, '/foo/',
00106                           'CMF Collector issue #192 (wrong base): %s'
00107                           % (r.response.base or 'empty',) )
00108 
00109     def test_default_viewname_but_no_view_doesnt_override_fti(self):
00110         response = HTTPResponse()
00111         environment = { 'URL': '',
00112                         'PARENTS': [self.site],
00113                         'REQUEST_METHOD': 'GET',
00114                         'SERVER_PORT': '80',
00115                         'REQUEST_METHOD': 'GET',
00116                         'steps': [],
00117                         'SERVER_NAME': 'localhost',
00118                         '_hacked_path': 0 }
00119         r = HTTPRequest(StringIO(), environment, response)
00120         r.other.update(environment)
00121         alsoProvides(r, IBrowserRequest)
00122 
00123         # we define a Zope3-style default view name, but no
00124         # corresponding view, no change in behaviour expected
00125         defineDefaultViewName('index.html', DummyContent)
00126         r.traverse('foo')
00127         self.assertEqual( r.URL, '/foo/dummy_view' )
00128         self.assertEqual( r.response.base, '/foo/' )
00129 
00130     def test_default_viewname_overrides_fti(self):
00131         response = HTTPResponse()
00132         environment = { 'URL': '',
00133                         'PARENTS': [self.site],
00134                         'REQUEST_METHOD': 'GET',
00135                         'SERVER_PORT': '80',
00136                         'REQUEST_METHOD': 'GET',
00137                         'steps': [],
00138                         'SERVER_NAME': 'localhost',
00139                         '_hacked_path': 0 }
00140         r = HTTPRequest(StringIO(), environment, response)
00141         r.other.update(environment)
00142         alsoProvides(r, IBrowserRequest)
00143 
00144         # we define a Zope3-style default view name for which a view
00145         # actually exists
00146         defineDefaultViewName('index.html', DummyContent)
00147         provideAdapter(
00148             DummyView, (DummyContent, IBrowserRequest), IBrowserView,
00149             'index.html')
00150 
00151         r.traverse('foo')
00152         self.assertEqual( r.URL, '/foo/index.html' )
00153         self.assertEqual( r.response.base, '/foo/' )
00154 
00155 
00156 class DynamicTypeSecurityTests(SecurityRequestTest):
00157 
00158     def setUp(self):
00159         SecurityRequestTest.setUp(self)
00160         self.site = DummySite('site').__of__(self.root)
00161         self.site._setObject( 'portal_membership', DummyTool() )
00162         self.site._setObject( 'portal_types', TypesTool() )
00163         self.site._setObject( 'portal_url', DummyTool() )
00164         fti = FTIDATA_CMF15[0].copy()
00165         self.site.portal_types._setObject( 'Dummy Content 15', FTI(**fti) )
00166         self.site._setObject( 'foo', DummyContent() )
00167 
00168     def tearDown(self):
00169         cleanUp()
00170         SecurityRequestTest.tearDown(self)
00171 
00172     def test_getTypeInfo(self):
00173         foo = self.site.foo
00174         self.assertEqual( foo.getTypeInfo().getId(), 'Dummy Content 15' )
00175 
00176     def test_getActionInfo(self):
00177         foo = self.site.foo
00178         self.assertEqual( foo.getActionInfo('object/view')['id'], 'view' )
00179 
00180         # The following is nasty, but I want to make sure the ValueError
00181         # carries some useful information
00182         INVALID_ID = 'invalid_id'
00183         try:
00184             rval = foo.getActionInfo('object/%s' % INVALID_ID)
00185         except ValueError, e:
00186             message = e.args[0]
00187             detail = '"%s" does not offer action "%s"' % (message, INVALID_ID)
00188             self.failUnless(message.find(INVALID_ID) != -1, detail)
00189 
00190 
00191 def test_suite():
00192     return unittest.TestSuite((
00193         unittest.makeSuite(DynamicTypeTests),
00194         unittest.makeSuite(DynamicTypeDefaultTraversalTests),
00195         unittest.makeSuite(DynamicTypeSecurityTests),
00196         ))
00197 
00198 if __name__ == '__main__':
00199     unittest.main(defaultTest='test_suite')