Functional test for local sites
===============================

This tests how local site managers are found during traversal and how
that affects component lookup (depending on whether a site is
traversed or not, local components might or might not be found).

First, we set up all of Five:

  >>> import Products.Five
  >>> from Products.Five import zcml
  >>> zcml.load_config("configure.zcml", Products.Five)

Then we hook up the custom component architecture calls; we need to do
this here because zope.app.component.hooks registers a cleanup with
the testing cleanup framework, so the hooks get torn down by
placelesssetup each time.

  >>> from zope.app.component.hooks import setHooks
  >>> setHooks()

Next we turn our DummySite class into a site in ZCML (and register
some views that will provide us with some test info),

  >>> zcml_text = """
  ... <configure xmlns="http://namespaces.zope.org/zope"
  ...            xmlns:meta="http://namespaces.zope.org/meta"
  ...            xmlns:five="http://namespaces.zope.org/five"
  ...            xmlns:browser="http://namespaces.zope.org/browser">
  ... 
  ...   <!-- make the zope2.Public permission work -->
  ...   <meta:redefinePermission from="zope2.Public" to="zope.Public" />
  ... 
  ...   <five:localsite class="Products.Five.site.tests.dummy.DummySite" />
  ...
  ...   <browser:page
  ...       for="Products.Five.site.tests.dummy.IDummySite"
  ...       name="checkSiteManager.html"
  ...       class="Products.Five.site.tests.test_functional.CheckSiteManagerView"
  ...       permission="zope2.Public"
  ...       />
  ...
  ...   <browser:page
  ...       for="Products.Five.site.tests.dummy.IDummySite"
  ...       name="lookupUtilities.html"
  ...       class="Products.Five.site.tests.test_functional.LookupUtilitiesView"
  ...       permission="zope2.Public"
  ...       />
  ... 
  ... </configure>"""
  >>> import warnings
  >>> showwarning = warnings.showwarning
  >>> warnings.showwarning = lambda *a, **k: None
  >>> zcml.load_string(zcml_text)
  >>> warnings.showwarning = showwarning

then we add an instance to our folder:

  >>> from Products.Five.site.tests.dummy import manage_addDummySite
  >>> nothing = manage_addDummySite(self.folder, 'site')

Now we check what the info view tells us about local component lookup:

  >>> import warnings
  >>> showwarning = warnings.showwarning
  >>> warnings.showwarning = lambda *a, **k: None
  >>> print http(r'''
  ... GET /test_folder_1_/site/@@checkSiteManager.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getSiteManager() is zapi.getGlobalSiteManager(): True
  IFiveUtilityRegistry.providedBy(utility_service): False
  isinstance(zapi.getSiteManager(), FiveSiteManager): False

We see that we have no local component lookup yet, because we haven't
set the site.  Therefore, enable the traversal hook by using the view
that's provided for this task (we first need to create a manager
account in order to be able to access it):

  >>> uf = self.folder.acl_users
  >>> uf._doAddUser('manager', 'r00t', ['Manager'], [])

  >>> import warnings
  >>> showwarning = warnings.showwarning
  >>> warnings.showwarning = lambda *a, **k: None
  >>> print http(r'''
  ... POST /test_folder_1_/site/@@manage_site.html HTTP/1.1
  ... Authorization: Basic manager:r00t
  ... Content-Length: 25
  ... 
  ... UPDATE_MAKESITE=Make site''')
  HTTP/1.1 200 OK
  ...

  >>> warnings.showwarning = showwarning

  >>> warnings.showwarning = showwarning

Now we call the info view again and find that local component lookup
is working:

  >>> print http(r'''
  ... GET /test_folder_1_/site/@@checkSiteManager.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getSiteManager() is zapi.getGlobalSiteManager(): False
  IFiveUtilityRegistry.providedBy(utility_service): True
  isinstance(zapi.getSiteManager(), FiveSiteManager): True

Of course, sites are only active *during* traversal; after traversal
they're gone:

  >>> from zope.app.component.hooks import getSite
  >>> getSite() is None
  True


We can also register utilities now:

  >>> from zope.app import zapi
  >>> sm = self.folder.site.getSiteManager()

  >>> from Products.Five.site.tests.dummy import IDummyUtility, DummyUtility
  >>> dummy = DummyUtility()
  >>> sm.registerUtility(IDummyUtility, dummy)

and find them being looked up just fine:

  >>> print http(r'''
  ... GET /test_folder_1_/site/@@lookupUtilities.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getUtility(IDummyUtility) == dummy: True

Of course, we can't look it up once the request has ended, because we
lose the local site setup:

  >>> zapi.getUtility(IDummyUtility)
  Traceback (most recent call last):
  ...
  ComponentLookupError: (<InterfaceClass Products.Five.site.tests.dummy.IDummyUtility>, '')

At last we can "unmake" the site using the browser view provided by
Five:

  >>> import warnings
  >>> showwarning = warnings.showwarning
  >>> warnings.showwarning = lambda *a, **k: None
  >>> print http(r'''
  ... POST /test_folder_1_/site/@@manage_site.html HTTP/1.1
  ... Authorization: Basic manager:r00t
  ... Content-Length: 29
  ... 
  ... UPDATE_UNMAKESITE=Unmake site''')
  HTTP/1.1 200 OK
  ...

  >>> warnings.showwarning = showwarning

And everything is back to normal with respect to local component
lookup:

  >>> import warnings
  >>> showwarning = warnings.showwarning
  >>> warnings.showwarning = lambda *a, **k: None
  >>> print http(r'''
  ... GET /test_folder_1_/site/@@checkSiteManager.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getSiteManager() is zapi.getGlobalSiteManager(): True
  IFiveUtilityRegistry.providedBy(utility_service): False
  isinstance(zapi.getSiteManager(), FiveSiteManager): False

Finally, global services and the monkeys:

  >>> from zope.app.testing.placelesssetup import tearDown
  >>> tearDown()

