Events in Archetypes
====================

This doctest makes sure that at relevant places in the Arcehtypes
code, events are fired.  To test events, we'll set up an event handler
that listens for object events for `IBaseObject`:

  >>> from pprint import pprint
  >>> from StringIO import StringIO
  >>> from zope.app.testing import ztapi
  >>> from Products.Archetypes.tests.utils import makeContent, aputrequest
  >>> from Products.Archetypes.interfaces import IBaseObject
  >>> from zope.app.container.interfaces import IObjectEvent

  >>> class Handler:
  ...     def __init__(self):
  ...         self.got = []
  ...     def __call__(self, *args):
  ...         self.got.append(args)
  ...     def print_and_flush(self):
  ...         print "%s events:" % len(self.got)
  ...         pprint(self.got)
  ...         self.got = []
  ...     def print_last_and_flush(self):
  ...	      self.got = self.got[-1:]
  ...         print "%s events:" % len(self.got)
  ...         pprint(self.got)
  ...         self.got = []
  >>> handler = Handler()
  >>> ztapi.subscribe([IBaseObject, IObjectEvent], None, handler)

Creating an AT object will a couple of events:

  >>> d = makeContent(self.folder, portal_type='DDocument', id='a')
  >>> handler.print_and_flush() #doctest: +ELLIPSIS
  7 events:
  [(<DDocument at a>,
    <zope.app.event.objectevent.ObjectCreatedEvent object at ...>),
   (<DDocument at a>, <OFS.event.ObjectWillBeAddedEvent object at ...>),
   (<DDocument at /plone/Members/test_user_1_/a>,
    <zope.app.container.contained.ObjectAddedEvent object at ...>),
   (<ATFolder at /plone/Members/test_user_1_>,
    <zope.app.container.contained.ContainerModifiedEvent object at ...>),
   (<DDocument at /plone/Members/test_user_1_/a>,
    <zope.app.event.objectevent.ObjectModifiedEvent object at ...),
   (<DDocument at /plone/Members/test_user_1_/a>,
    <Products.DCWorkflow.events.BeforeTransitionEvent object at ...>),
   (<DDocument at /plone/Members/test_user_1_/a>,
    <Products.DCWorkflow.events.AfterTransitionEvent object at ...>)]


Calling `processForm`, which is what happens on submitting a form,
will fire the modified event:

  >>> d.processForm()
  >>> handler.print_and_flush() #doctest: +ELLIPSIS
  1 events:
  [(<DDocument at /plone/Members/test_user_1_/a>,
    <Products.Archetypes.event.ObjectInitializedEvent object at ...>)]

Doing a `PUT` request that creates a brand new object should fire
IWebDAVObjectInitializedEvent:

  >>> from Products.Archetypes.tests.attestcase import user_name
  >>> from Products.Archetypes.tests.attestcase import user_password
  >>> from Products.Archetypes.tests.atsitetestcase import portal_name
  >>> self.setRoles(['Manager'])

  >>> 'some-document' in self.folder.objectIds()
  False

  >>> print http(r"""
  ... PUT /%s/some-document HTTP/1.1
  ... Authorization: Basic %s:%s
  ... Content-Type: text/plain; charset="utf-8"
  ...
  ... Some Content
  ... """ % ('/'.join(self.folder.getPhysicalPath()), user_name, user_password))
  HTTP/1.1 201 Created
  ...

  >>> 'some-document' in self.folder.objectIds()
  True

  >>> print str(self.folder['some-document']['text'])
  Some Content

  >>> handler.print_last_and_flush() #doctest: +ELLIPSIS
  1 events:
  [(<ATDocument at /plone/Members/test_user_1_/some-document>,
    <Products.Archetypes.event.WebDAVObjectInitializedEvent object at ...>)...]

Doing another `PUT` request to update the same object should fire an
IWebDAVObjectEditedEvent:

  >>> print http(r"""
  ... PUT /%s/some-document HTTP/1.1
  ... Authorization: Basic %s:%s
  ... Content-Type: text/plain; charset="utf-8"
  ...
  ... Some Other Content
  ... """ % ('/'.join(self.folder.getPhysicalPath()), user_name, user_password))
  HTTP/1.1 204 No Content
  ...

  >>> print str(self.folder['some-document']['text'])
  Some Other Content

  >>> handler.print_and_flush() #doctest: +ELLIPSIS
  1 events:
  [(<ATDocument at /plone/Members/test_user_1_/some-document>,
    <Products.Archetypes.event.WebDAVObjectEditedEvent object at ...>)]

Test that a MKCOL also fires an IWebDAVObjectInitialized event:

  >>> print http(r"""
  ... MKCOL /%s/some-folder HTTP/1.1
  ... Authorization: Basic %s:%s
  ... """ % ('/'.join(self.folder.getPhysicalPath()), user_name, user_password))
  HTTP/1.1 201 Created
  ...

  >>> handler.print_last_and_flush() #doctest: +ELLIPSIS
  1 events:
  [(<ATFolder at /plone/Members/test_user_1_/some-folder>,
    <Products.Archetypes.event.WebDAVObjectInitializedEvent object at ...>)]
