Five Site Manager
=================

In this test we want to test Five's implementation of a site manager.
First, we need to set a few things up...

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

  >>> import Products.Five
  >>> from Products.Five import zcml
  >>> import warnings
  >>> showwarning = warnings.showwarning
  >>> warnings.showwarning = lambda *a, **k: None

  >>> zcml.load_config("meta.zcml", Products.Five)
  >>> zcml.load_config("permissions.zcml", Products.Five)
  >>> zcml.load_config("configure.zcml", Products.Five.site)
  >>> zcml_text = """\
  ... <five:localsite
  ...     xmlns:five="http://namespaces.zope.org/five"
  ...     class="Products.Five.site.tests.dummy.DummySite" />"""
  >>> zcml.load_string(zcml_text)

...for example some sort of site object:

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

Local vs. global sites
----------------------

Let's make the possible site a real site:

  >>> from Products.Five.site.localsite import enableLocalSiteHook
  >>> enableLocalSiteHook(dummysite)

  >>> warnings.showwarning = showwarning

and tell Zope 3 about it:

  >>> from zope.app.component.hooks import setSite, setHooks
  >>> setSite(dummysite)

Also hook up 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.

  >>> setHooks()

That seems to have worked (we test this by using the context property
of FiveSiteManager):

  >>> from zope.app import zapi
  >>> zapi.getSiteManager().context == dummysite
  True

Since there's no other local site in between this one and the global
one, the next one should be the global one.  FiveSiteManager indicates
that to us by return ``None``:

  >>> from zope.app import zapi
  >>> zapi.getSiteManager().next is None
  True

To the the Zope 3 API, this means the next site manager should be the
global one:

  >>> from zope.app.component import getNextSiteManager
  >>> getNextSiteManager(dummysite.getSiteManager()) is zapi.getGlobalSiteManager()
  True


ISiteManager API
----------------

Site managers are supposed to have an ``adapters`` and a ``utilities``
attribute.  Five's site manager simply passes through the global
adapter registry:

  >>> zapi.getSiteManager().adapters is zapi.getGlobalSiteManager().adapters
  True

The utility registry, however, is an ``IFiveUtilityRegistry``:

  >>> from Products.Five.site.interfaces import IFiveUtilityRegistry
  >>> IFiveUtilityRegistry.providedBy(zapi.getSiteManager().utilities)
  True

The methods on registering and looking up utilities are covered by the
utility tests in depth.

We test some adapter look-up here.  It is also indirectly covered in
the functional test; view look up, for example, is adapter look up.

First we provide an adapter:

  >>> from Products.Five.tests.adapters import Adaptable, Adapter, IAdapted
  >>> import zope.component
  >>> zope.component.provideAdapter(Adapter)

Now let's check for a simple adaption:

  >>> adaptable = Adaptable()
  >>> IAdapted(adaptable) #doctest: +ELLIPSIS
  <Products.Five.tests.adapters.Adapter instance at ...>

Let's get all the adapters for ``adaptable``:

  >>> list(zapi.getAdapters((adaptable,), IAdapted)) #doctest: +ELLIPSIS
  [(u'', <Products.Five.tests.adapters.Adapter instance at ...>)]


Nesting sites
-------------

Let's set up another site to test nested sites:

  >>> nothing = manage_addDummySite(self.folder.dummysite, 'subsite')
  >>> subsite = self.folder.dummysite.subsite

Now we set the current site to the ``subsite``:

  >>> enableLocalSiteHook(subsite)
  >>> setSite(subsite)

When we call getServices() now, we get the correct site manager:

  >>> zapi.getSiteManager().context == subsite
  True

The "next" site is the less local one:

  >>> zapi.getSiteManager().next.context == dummysite
  True

The Zope 3 API for this agrees with that:

  >>> getNextSiteManager(subsite.getSiteManager()).context == dummysite
  True

The adapters is registry is passed through to the global one:

  >>> subsite.getSiteManager().adapters is zapi.getGlobalSiteManager().adapters
  True

Finally, some clean up:

  >>> tearDown()
