Five compatibility tests
========================

Let's instantiate a Five page template.

  >>> from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile

  >>> import z3c.jbot.tests
  >>> directory = z3c.jbot.tests.__path__[0] + "/templates"

To play nicely we'll put the template inside a view.
  
  >>> from Products.Five.browser import BrowserView
  >>> class MockView(BrowserView):
  ...     template = ZopeTwoPageTemplateFile("example.pt", _prefix=directory)

Let's instantiate the view with a test request.
  
  >>> from zope.publisher.browser import TestRequest
  >>> request = TestRequest("en")  
  >>> view = MockView(None, request)

Verify that the original template is rendered.
  
  >>> view.template()
  u'This is an example page template.\n'

Register template overrides directory.
  
  >>> import z3c.jbot.manager
  >>> import z3c.jbot.interfaces
  
  >>> factory = z3c.jbot.manager.TemplateManagerFactory()
  >>> component.provideAdapter(
  ...     factory, (interface.Interface,), z3c.jbot.interfaces.ITemplateManager)

  >>> manager = factory.manager
  >>> manager.registerDirectory(directory)

Verify that the override is rendered.
  
  >>> view.template()
  u'This template will override the example template.\n'

  >>> manager.unregisterDirectory(directory)

Register override for HTTP-request layer.

  >>> from zope.publisher.interfaces.browser import IHTTPRequest
  >>> factory = z3c.jbot.manager.TemplateManagerFactory()
  >>> component.provideAdapter(
  ...     factory, (IHTTPRequest,),
  ...     z3c.jbot.interfaces.ITemplateManager, name='http')

  >>> manager = factory.manager
  >>> manager.registerDirectory(directory)
  
  >>> view.template()
  u'This is an example page template.\n'

On Zope 2, the request is acquired from the site as set by the
component site hook.
  
  >>> from zope.publisher.browser import TestRequest
  >>> class MockSite(object):
  ...     REQUEST = TestRequest()
  ...     getSiteManager = component.getSiteManager
  
  >>> from zope.app.component.hooks import setHooks, setSite
  >>> setHooks()
  >>> setSite(MockSite())

Examine the request.
  
  >>> import z3c.jbot.utility
  >>> z3c.jbot.utility.getRequest()
  <zope.publisher.browser.TestRequest ...>

  >>> managers = tuple(z3c.jbot.utility.getManagers())
  >>> len(managers)
  2

Verify that template attributes are set per request layer.

  >>> view.template._v_last_read is not False
  True

  >>> class ISpecificLayer(interface.Interface):
  ...     pass

  >>> interface.alsoProvides(MockSite.REQUEST, ISpecificLayer)
  >>> view.template._v_last_read = False
  >>> view.template._v_last_read is False
  True

No longer providing this layer, we expect an attribute lookup to
return the value that was set before the layer was provided.
  
  >>> interface.noLongerProvides(MockSite.REQUEST, ISpecificLayer)
  >>> view.template._v_cache.layer = z3c.jbot.utility.getLayer()
  >>> view.template._v_last_read is not False
  True  
  

