Viewlets
========

Some imports:

  >>> from zope.component import getMultiAdapter
  >>> from zope.interface import alsoProvides
  >>> from pprint import pprint as pp

Set up a test request

  >>> from Products.Collage.tests.utils import TestRequest
  >>> request = TestRequest("en")

Let's add some content:

  >>> _ = folder.invokeFactory(id='collage', type_name='Collage')
  >>> collage = folder[_]
  >>> _ = collage.invokeFactory(id='row', type_name='CollageRow')
  >>> row = collage[_]
  >>> _ = row.invokeFactory(id='column', type_name='CollageColumn')
  >>> column = row[_]

To use the Collage views we need to add a layer to the request:

  >>> from Products.Collage.interfaces import ICollageBrowserLayer
  >>> alsoProvides(request, ICollageBrowserLayer)

Let's examine the actions viewlet

  >>> view = getMultiAdapter((row, request), name=u"fallback")

  >>> from zope.viewlet.manager import ViewletManager
  >>> from Products.Collage.interfaces import IContentMenu
  >>> try:
  ...   ContentMenu = ViewletManager(u'contentmenu', IContentMenu, template=None, bases=())
  ... except TypeError:
  ...   # BBB: support for for Zope 2.9
  ...   ContentMenu = ViewletManager(IContentMenu)
  >>> manager = ContentMenu(row, request, view)

Viewlets are registered on a custom layer and available only to users with
permission to modify the context.

  >>> from Products.Collage.interfaces import ICollageEditLayer
  >>> alsoProvides(request, ICollageEditLayer)
  >>> self.setRoles(['Manager'])


Actions
-------

  >>> viewlet = getMultiAdapter((row, request, view, manager), name=u"130-actions")

Let's look at the actions for our folder:
  >>> request.set('ACTUAL_URL', collage.absolute_url())
  >>> for action in viewlet.getViewActions():
  ...   for key in ['url', 'selected', 'id', 'title']:
  ...     if key not in action.keys(): raise ValueError


Insert new items
----------------

  >>> viewlet = getMultiAdapter((column, request, view, manager), name=u"080-insert-new-item")

Let's look at the returned content types:

  >>> for info in viewlet.getAddableTypes():
  ...   if set(info.keys()) != set(['id', 'description', 'icon', 'title']): raise ValueError

Insert an event item
  
  >>> _ = column.invokeFactory(id='test_event', type_name='Event')
  >>> column[_].setTitle('test event')
  >>> 'test event' in column.restrictedTraverse('@@renderer')()
  True
  




Insert existing items
---------------------

  >>> viewlet = getMultiAdapter((column, request, view, manager), name=u"070-insert-existing-item")

Next we examine the ajax-view; items are inserted by UID---we verify
that the form output contains the front page content object's UID
string:

  >>> view = column.restrictedTraverse('@@existing-items-form')
  >>> front_page_uid = self.portal['front-page'].UID()
  >>> front_page_uid in view()
  True

  

Copy/paste
----------

  >>> viewlet = getMultiAdapter((column, request, view, manager), name=u"120-paste")

Let's setup the clipboard as if we'd copied the front-page element:

  >>> from OFS.CopySupport import _cb_encode
  >>> from OFS import Moniker
  >>> __cp = _cb_encode((0, [Moniker.Moniker(portal['front-page']).dump()]))

For some reason there's two different request objects; let's set the clipboard on both:

  >>> viewlet.request.set('__cp', __cp)
  >>> viewlet.context.REQUEST.set('__cp', __cp)

Let's examine the paste viewlet:

  >>> viewlet.clipboard_data_valid
  1
  >>> viewlet._get_clipboard_item()
  <ATDocument at /plone/front-page>
  
