##############################################################################
#
# Copyright (c) 2004, 2005 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test forms

$Id: test_forms.py 14595 2005-07-12 21:26:12Z philikon $
"""
import logging
import unittest
from Testing.ZopeTestCase import Zope2

# Get our monkeys lined up
import Products.Five

from zope.component.testing import setUp
from zope.component.testing import tearDown as zcTearDown

from Products.CMFPlone.tests import PloneTestCase
from Products.Five.site.localsite import enableLocalSiteHook
from zope.app.component.hooks import setSite, setHooks

from Products.listen.interfaces import IMemberLookup
from Products.listen.interfaces import IListLookup
from Products.listen.utilities.list_lookup import ListLookup
from Products.listen.utilities.token_to_email import MemberToEmail

def test_suite():
    import unittest
    from zope.testing.doctestunit import DocTestSuite
    from Testing.ZopeTestCase import FunctionalDocFileSuite
    from Testing.ZopeTestCase import FunctionalDocTestSuite
    from Products.PloneTestCase.PloneTestCase import FunctionalTestCase
    all_tests = unittest.TestSuite((
            FunctionalDocTestSuite('Products.listen.content.mailinglist', setUp=setup_utility, tearDown=tearDown, test_class=FunctionalTestCase),
            FunctionalDocTestSuite('Products.listen.content.subscriptions', setUp=setup_utility, tearDown=tearDown, test_class=FunctionalTestCase),
            FunctionalDocTestSuite('Products.listen.content.membership_handlers', setUp=setup_utility, tearDown=tearDown, test_class=FunctionalTestCase),
            FunctionalDocTestSuite('Products.listen.content.membership_policies', setUp=setup_utility, tearDown=tearDown, test_class=FunctionalTestCase),
            FunctionalDocTestSuite('Products.listen.content.send_mail', setUp=setup_utility, tearDown=tearDown, test_class=FunctionalTestCase),
            FunctionalDocTestSuite('Products.listen.content.post_policies', setUp=setup_utility, tearDown=tearDown, test_class=FunctionalTestCase),
            FunctionalDocTestSuite('Products.listen.content.mailboxer_list'),),)
    return all_tests

def setup_utility(self):
     """ register the IMemberLookup utility with the portal """
     portal = self.portal
     import Products.Five
     from Products.Five import zcml
     zcml.load_config('meta.zcml', Products.Five)
     zcml.load_config('permissions.zcml', Products.Five)
     zcml.load_config("configure.zcml", Products.Five.site)
     from Products.listen.utilities import tests
     zcml.load_config('configure.zcml', tests)
     from Products.listen.content import tests as content_tests
     zcml.load_config('configure.zcml', content_tests)

     site = portal
     enableLocalSiteHook(site)
     setSite(site)
     sm = site.getSiteManager()
     member_to_email_utility = MemberToEmail()
     sm.registerUtility(IMemberLookup, member_to_email_utility)
     sm.registerUtility(IListLookup, ListLookup('list_lookup'))
     setHooks()
     start_log_capture('listen')

def start_log_capture(name=''):
    """Temporarily swallow logging.
    """
    logger = logging.getLogger(name)
    logger._oldpropagate = logger.propagate
    logger._oldhandlers = logger.handlers
    logger.propagate = 0
    class MockHandler(logging.Handler):

        def emit(self, record):
            # Print the message, useful for doctest.
            #print "Logged:"
            #print "%s: %s" % (record.levelname, record.msg)
            pass

    logger.handlers = [MockHandler()]


def stop_log_capture(name=''):
    logger = logging.getLogger(name)
    if hasattr(logger, '_oldhandlers'):
        logger.handlers = logger._oldhandlers
    if hasattr(logger, '_oldpropagate'):
        logger.propagate = logger._oldpropagate

def tearDown(*args, **kw):
    stop_log_capture('listen')
    zcTearDown(*args, **kw)


if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')
