Back to index

plone3  3.1.7
test_event.py
Go to the documentation of this file.
00001 """
00002 Unittests for the events fired by Archetypes.
00003 """
00004 
00005 from zope.interface import implements, Interface, directlyProvides
00006 from zope import component
00007 
00008 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00009 from Products.Archetypes.tests import utils
00010 
00011 from zope.publisher.browser import TestRequest
00012 
00013 from Products.Archetypes.atapi import BaseContent
00014 
00015 from Products.Archetypes.interfaces import IObjectPreValidation
00016 from Products.Archetypes.interfaces import IObjectPostValidation
00017 
00018 from Products.Archetypes.interfaces import IObjectInitializedEvent
00019 from Products.Archetypes.interfaces import IObjectEditedEvent
00020 
00021 from zope.lifecycleevent.interfaces import IObjectCreatedEvent
00022 
00023 class IObject1(Interface):
00024     pass
00025 
00026 class IObject2(Interface):
00027     pass
00028 
00029 class IObject3(Interface):
00030     pass
00031 
00032 class Dummy(BaseContent):
00033     pass
00034 utils.gen_class(Dummy)
00035 
00036 # Subscription adapters for validation
00037 
00038 class PreValidation(object):
00039     implements(IObjectPreValidation)
00040     
00041     def __init__(self, context):
00042         self.context = context
00043     
00044     def __call__(self, request):
00045         return dict(foo="Foo was invalid.")
00046 
00047 class PostValidation(object):
00048     implements(IObjectPostValidation)
00049     
00050     def __init__(self, context):
00051         self.context = context
00052     
00053     def __call__(self, request):
00054         return dict(bar="Bar was invalid.")
00055     
00056 def created_handler(ob, event):
00057     ob._createdCaught = True
00058     
00059 def initialized_handler(ob, event):
00060     ob._initializedCaught = True
00061     
00062 def edited_handler(ob, event):
00063     ob._editedCaught = True
00064 
00065 class ValidationEventTests(ATSiteTestCase):
00066 
00067     def testPreValidatingEvent(self):
00068         
00069         # Register some subscription adapters for different types of objects
00070         # These will be called during validation
00071 
00072         component.provideSubscriptionAdapter(PreValidation, adapts=(IObject1,))
00073         component.provideSubscriptionAdapter(PreValidation, adapts=(IObject3,))
00074         
00075         # Verify that they are called only on the right type of object,
00076         # and that their return values are included in the error output
00077 
00078         ob = Dummy('dummy')
00079         directlyProvides(ob, IObject1)
00080         errors = ob.validate()
00081         self.failUnless(errors['foo'])
00082         del ob
00083         
00084         ob = Dummy('dummy')
00085         directlyProvides(ob, IObject2)
00086         errors = ob.validate()
00087         self.failIf(errors.has_key('foo'))
00088         del ob
00089         
00090         ob = Dummy('dummy')
00091         directlyProvides(ob, IObject3)
00092         errors = ob.validate()
00093         self.failUnless(errors['foo'])
00094         del ob
00095         
00096         sm = component.getSiteManager()
00097         sm.unregisterSubscriptionAdapter(PreValidation, required=(IObject1,))
00098         sm.unregisterSubscriptionAdapter(PreValidation, required=(IObject3,))
00099 
00100     def testPostValidatingEvent(self):
00101         
00102         # This test is similar to the test for pre-validation above. The
00103         # difference is that the post validation works after main schema
00104         # validation, whilst the pre-validation works before, and may
00105         # short-circuit schema validation.
00106         
00107         component.provideSubscriptionAdapter(PostValidation, adapts=(IObject2,))
00108         component.provideSubscriptionAdapter(PostValidation, adapts=(IObject3,))
00109         
00110         ob = Dummy('dummy')
00111         directlyProvides(ob, IObject1)
00112         errors = ob.validate()
00113         self.failIf(errors.has_key('bar'))
00114         del ob
00115         
00116         ob = Dummy('dummy')
00117         directlyProvides(ob, IObject2)
00118         errors = ob.validate()
00119         self.failUnless(errors['bar'])
00120         del ob
00121         
00122         ob = Dummy('dummy')
00123         directlyProvides(ob, IObject3)
00124         errors = ob.validate()
00125         self.failUnless(errors.has_key('bar'))
00126         del ob
00127         
00128         sm = component.getSiteManager()
00129         sm.unregisterSubscriptionAdapter(PostValidation, required=(IObject2,))
00130         sm.unregisterSubscriptionAdapter(PostValidation, required=(IObject3,))
00131         
00132     def testInitializedAndEditedEvent(self):
00133         
00134         component.provideHandler(created_handler, (IObject1, IObjectCreatedEvent,))
00135         component.provideHandler(initialized_handler, (IObject1, IObjectInitializedEvent,))
00136         component.provideHandler(edited_handler, (IObject1, IObjectEditedEvent,))
00137         
00138         ob = Dummy('dummy')
00139         directlyProvides(ob, IObject1)
00140         self.folder._setObject('dummy', ob)
00141         ob = self.folder.dummy
00142         
00143         ob._initializedCaught = False
00144         ob._editedCaught = False
00145         
00146         # Simulate first edit
00147         ob._at_creation_flag = True
00148         ob.processForm(REQUEST=TestRequest())
00149         
00150         self.assertEquals(True, ob._initializedCaught)
00151         self.assertEquals(False, ob._editedCaught)
00152         
00153         # Simulate subsequent edit
00154         ob.processForm(REQUEST=TestRequest())
00155         self.assertEquals(True, ob._editedCaught)
00156         
00157         sm = component.getSiteManager()
00158         sm.unregisterHandler(created_handler, (IObject1, IObjectCreatedEvent,))
00159         sm.unregisterHandler(initialized_handler, (IObject1, IObjectCreatedEvent,))
00160         sm.unregisterHandler(edited_handler, (IObject1, IObjectCreatedEvent,))
00161         
00162 
00163 def test_suite():
00164     from unittest import TestSuite, makeSuite
00165     suite = TestSuite()
00166     suite.addTest(makeSuite(ValidationEventTests))
00167     return suite