Back to index

plone3  3.1.7
CMFTestCase.py
Go to the documentation of this file.
00001 #
00002 # CMFTestCase
00003 #
00004 
00005 # $Id: CMFTestCase.py 44961 2007-07-01 19:36:36Z shh42 $
00006 
00007 from Testing.ZopeTestCase import hasProduct
00008 from Testing.ZopeTestCase import installProduct
00009 
00010 try:
00011     from Testing.ZopeTestCase import hasPackage
00012     from Testing.ZopeTestCase import installPackage
00013 except ImportError:
00014     pass
00015 
00016 from Testing.ZopeTestCase import Sandboxed
00017 from Testing.ZopeTestCase import Functional
00018 from Testing.ZopeTestCase import PortalTestCase
00019 
00020 from setup import CMF15
00021 from setup import CMF16
00022 from setup import CMF20
00023 from setup import CMF21
00024 from setup import USELAYER
00025 from setup import Z3INTERFACES
00026 from setup import portal_name
00027 from setup import portal_owner
00028 from setup import default_products
00029 from setup import default_base_profile
00030 from setup import default_extension_profiles
00031 from setup import default_user
00032 from setup import default_password
00033 from setup import _placefulSetUp
00034 from setup import _placefulTearDown
00035 
00036 from setup import setupCMFSite
00037 
00038 from interfaces import ICMFTestCase
00039 from interfaces import ICMFSecurity
00040 
00041 from AccessControl import getSecurityManager
00042 from AccessControl.SecurityManagement import setSecurityManager
00043 from AccessControl.SecurityManagement import newSecurityManager
00044 from warnings import warn
00045 
00046 import utils
00047 import setup
00048 
00049 
00050 class CMFTestCase(PortalTestCase):
00051     '''Base test case for CMF testing'''
00052 
00053     if Z3INTERFACES:
00054         from zope.interface import implements
00055         implements(ICMFTestCase, ICMFSecurity)
00056     else:
00057         __implements__ = (ICMFTestCase, ICMFSecurity,
00058                           PortalTestCase.__implements__)
00059 
00060     if USELAYER:
00061         import layer
00062         layer = layer.CMFSite
00063 
00064     def _portal(self):
00065         '''Returns the portal object for a test.'''
00066         portal = self.getPortal()
00067         if CMF21:
00068             _placefulSetUp(portal)
00069         return portal
00070 
00071     def _setup(self):
00072         '''Configures the portal.'''
00073         PortalTestCase._setup(self)
00074         if CMF21 and self.portal is not None:
00075             self._refreshSkinData()
00076 
00077     def _clear(self, call_close_hook=0):
00078         '''Clears the fixture.'''
00079         PortalTestCase._clear(self, call_close_hook)
00080         if CMF21:
00081             _placefulTearDown()
00082 
00083     # Portal interface
00084 
00085     def getPortal(self):
00086         '''Returns the portal object.
00087 
00088            Do not call this method! Use the self.portal
00089            attribute to access the portal object from tests.
00090         '''
00091         return getattr(self.app, portal_name)
00092 
00093     def createMemberarea(self, name):
00094         '''Creates a minimal memberarea.'''
00095         uf = self.portal.acl_users
00096         user = uf.getUserById(name)
00097         if user is None:
00098             raise ValueError, 'Member %s does not exist' % name
00099         if not hasattr(user, 'aq_base'):
00100             user = user.__of__(uf)
00101         pm = self.portal.portal_membership
00102         members = pm.getMembersFolder()
00103         members.manage_addPortalFolder(name)
00104         folder = pm.getHomeFolder(name)
00105         folder.changeOwnership(user)
00106         folder.__ac_local_roles__ = None
00107         folder.manage_setLocalRoles(name, ['Owner'])
00108 
00109     # Security interface
00110 
00111     def loginAsPortalOwner(self):
00112         '''Use if - AND ONLY IF - you need to manipulate the
00113            portal object itself.
00114         '''
00115         uf = self.app.acl_users
00116         user = uf.getUserById(portal_owner)
00117         if not hasattr(user, 'aq_base'):
00118             user = user.__of__(uf)
00119         newSecurityManager(None, user)
00120 
00121     # CMF interface
00122 
00123     def addProfile(self, name):
00124         '''Imports an extension profile into the site.'''
00125         sm = getSecurityManager()
00126         self.loginAsPortalOwner()
00127         try:
00128             installed = getattr(self.portal, '_installed_profiles', {})
00129             if not installed.has_key(name):
00130                 setup = self.portal.portal_setup
00131                 profile_id = 'profile-%s' % (name,)
00132                 if CMF21:
00133                     setup.runAllImportStepsFromProfile(profile_id)
00134                 else:
00135                     saved = setup.getImportContextID()
00136                     try:
00137                         setup.setImportContext(profile_id)
00138                         setup.runAllImportSteps()
00139                     finally:
00140                         setup.setImportContext(saved)
00141                 self._refreshSkinData()
00142         finally:
00143             setSecurityManager(sm)
00144 
00145     def addProduct(self, name):
00146         '''Installs a product into the site.'''
00147         sm = getSecurityManager()
00148         self.loginAsPortalOwner()
00149         try:
00150             installed = getattr(self.portal, '_installed_products', {})
00151             if not installed.has_key(name):
00152                 exec 'from Products.%s.Extensions.Install import install' % (name,)
00153                 install(self.portal)
00154                 self._refreshSkinData()
00155         finally:
00156             setSecurityManager(sm)
00157 
00158 
00159 class FunctionalTestCase(Functional, CMFTestCase):
00160     '''Base class for functional CMF tests'''
00161 
00162     if not Z3INTERFACES:
00163         __implements__ = (Functional.__implements__,
00164                           CMFTestCase.__implements__)
00165