Back to index

plone3  3.1.7
PloneTestCase.py
Go to the documentation of this file.
00001 #
00002 # PloneTestCase
00003 #
00004 
00005 # $Id: PloneTestCase.py 54876 2007-12-04 16:02: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 PLONE21
00021 from setup import PLONE25
00022 from setup import PLONE30
00023 from setup import PLONE31
00024 from setup import PLONE40
00025 from setup import USELAYER
00026 from setup import Z3INTERFACES
00027 from setup import portal_name
00028 from setup import portal_owner
00029 from setup import default_policy
00030 from setup import default_products
00031 from setup import default_base_profile
00032 from setup import default_extension_profiles
00033 from setup import default_user
00034 from setup import default_password
00035 from setup import _placefulSetUp
00036 from setup import _placefulTearDown
00037 from setup import _createHomeFolder
00038 
00039 from setup import setupPloneSite
00040 
00041 from interfaces import IPloneTestCase
00042 from interfaces import IPloneSecurity
00043 
00044 from AccessControl import getSecurityManager
00045 from AccessControl.SecurityManagement import setSecurityManager
00046 from AccessControl.SecurityManagement import newSecurityManager
00047 from warnings import warn
00048 
00049 import utils
00050 import setup
00051 
00052 
00053 class PloneTestCase(PortalTestCase):
00054     '''Base test case for Plone testing'''
00055 
00056     if Z3INTERFACES:
00057         from zope.interface import implements
00058         implements(IPloneTestCase, IPloneSecurity)
00059     else:
00060         __implements__ = (IPloneTestCase, IPloneSecurity,
00061                           PortalTestCase.__implements__)
00062 
00063     if USELAYER:
00064         import layer
00065         layer = layer.PloneSite
00066 
00067     def _portal(self):
00068         '''Returns the portal object for a test.'''
00069         portal = self.getPortal()
00070         if PLONE30:
00071             _placefulSetUp(portal)
00072         return portal
00073 
00074     def _setup(self):
00075         '''Configures the portal.'''
00076         PortalTestCase._setup(self)
00077         if PLONE30 and self.portal is not None:
00078             self._refreshSkinData()
00079 
00080     def _clear(self, call_close_hook=0):
00081         '''Clears the fixture.'''
00082         PortalTestCase._clear(self, call_close_hook)
00083         if PLONE30:
00084             _placefulTearDown()
00085 
00086     # Portal interface
00087 
00088     def getPortal(self):
00089         '''Returns the portal object.
00090 
00091            Do not call this method! Use the self.portal
00092            attribute to access the portal object from tests.
00093         '''
00094         return getattr(self.app, portal_name)
00095 
00096     def createMemberarea(self, name):
00097         '''Creates a minimal memberarea.'''
00098         _createHomeFolder(self.portal, name)
00099 
00100     # Security interface
00101 
00102     def setRoles(self, roles, name=default_user):
00103         '''Changes the user's roles.'''
00104         uf = self.portal.acl_users
00105         if PLONE25:
00106             uf.userFolderEditUser(name, None, utils.makelist(roles), [])
00107         else:
00108             uf._updateUser(name, roles=utils.makelist(roles))
00109         if name == getSecurityManager().getUser().getId():
00110             self.login(name)
00111 
00112     def setGroups(self, groups, name=default_user):
00113         '''Changes the user's groups.'''
00114         uf = self.portal.acl_users
00115         if PLONE25:
00116             uf.userSetGroups(name, utils.makelist(groups))
00117         else:
00118             uf._updateUser(name, groups=utils.makelist(groups))
00119         if name == getSecurityManager().getUser().getId():
00120             self.login(name)
00121 
00122     def loginAsPortalOwner(self):
00123         '''Use if - AND ONLY IF - you need to manipulate
00124            the portal object itself.
00125         '''
00126         uf = self.app.acl_users
00127         user = uf.getUserById(portal_owner)
00128         if not hasattr(user, 'aq_base'):
00129             user = user.__of__(uf)
00130         newSecurityManager(None, user)
00131 
00132     # Plone interface
00133 
00134     def addProfile(self, name):
00135         '''Imports an extension profile into the site.'''
00136         sm = getSecurityManager()
00137         self.loginAsPortalOwner()
00138         try:
00139             installed = getattr(self.portal, '_installed_profiles', {})
00140             if not installed.has_key(name):
00141                 setup = self.portal.portal_setup
00142                 profile_id = 'profile-%s' % (name,)
00143                 if PLONE30:
00144                     setup.runAllImportStepsFromProfile(profile_id)
00145                 else:
00146                     saved = setup.getImportContextID()
00147                     try:
00148                         setup.setImportContext(profile_id)
00149                         setup.runAllImportSteps()
00150                     finally:
00151                         setup.setImportContext(saved)
00152                 self._refreshSkinData()
00153         finally:
00154             setSecurityManager(sm)
00155 
00156     def addProduct(self, name):
00157         '''Quickinstalls a product into the site.'''
00158         sm = getSecurityManager()
00159         self.loginAsPortalOwner()
00160         try:
00161             qi = self.portal.portal_quickinstaller
00162             if not qi.isProductInstalled(name):
00163                 qi.installProduct(name)
00164                 self._refreshSkinData()
00165         finally:
00166             setSecurityManager(sm)
00167 
00168 
00169 class FunctionalTestCase(Functional, PloneTestCase):
00170     '''Base class for functional Plone tests'''
00171 
00172     if not Z3INTERFACES:
00173         __implements__ = (Functional.__implements__,
00174                           PloneTestCase.__implements__)
00175