Zope's portaltestcase automatically prepares a member folder (self.folder
) for use in the tests. A common usecase is, however, to prevent the member folders from being created. Result: all unittests fail as the unittest mechanism cannot create that self.folder
...
The solution is to create a monkey patch and to place that as patch.py
inside your product's tests/
folder. And importing it in your tests __init__.py
. Here's the code:
from Testing.ZopeTestCase import PortalTestCase PATCH_PREFIX = '_monkey_' __refresh_module__ = 0 def monkeyPatch(originalClass, patchingClass): #print 'monkeyPatch', originalClass.__name__, patchingClass.__name__ """Monkey patch original class with attributes from new class (Swiped from SpeedPack -- thanks, Christian Heimes!) * Takes all attributes and methods except __doc__ and __module__ from patching class * Safes original attributes as _monkey_name * Overwrites/adds these attributes in original class """ for name, newAttr in patchingClass.__dict__.items(): # don't overwrite doc or module informations if name not in ('__doc__', '__module__'): # safe the old attribute as __monkey_name if exists # __dict__ doesn't show inherited attributes :/ orig = getattr(originalClass, name, None) if orig: stored_orig_name = PATCH_PREFIX + name stored_orig = getattr(originalClass, stored_orig_name, None) # don't double-patch on refresh! if stored_orig is None: setattr(originalClass, stored_orig_name, orig) # overwrite or add the new attribute setattr(originalClass, name, newAttr) class PatchedPortalTestCase: def _setupHomeFolder(self): """Creates the default user's home folder. """ try: self.createMemberarea(user_name) pm = self.portal.portal_membership self.folder = pm.getHomeFolder(user_name) except: pass monkeyPatch(PortalTestCase, PatchedPortalTestCase)
For good measure, here's the code that switches off the member area creation (and also allows only manager to add members). Put this in your Install.py:
def _customizeMemberCreation(portal): """Customize Member(area) creation. * Turn off member area creation * Remove Member folder and default 'Topic' items * Only manager is allowed to add members. """ print >> out, "Customizing member(area) creation." mtool = getToolByName( portal, 'portal_membership') if mtool.getMemberareaCreationFlag(): mtool.setMemberareaCreationFlag() if 'Members' in portal.objectIds(): portal._delObject('Members') portal.manage_permission('Add portal member', ['Manager'], 0)
My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):