# -*- coding: utf-8 -*-
"""Base module for unittesting."""

from plone.app.robotframework.testing import AUTOLOGIN_LIBRARY_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import login
from plone.app.testing import PLONE_FIXTURE
from plone.app.testing import PloneSandboxLayer
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.app.testing import TEST_USER_NAME
from plone.testing import z2

import collective.dms.scanbehavior
import unittest


class CollectiveDmsScanbehaviorLayer(PloneSandboxLayer):

    PLONE_FIXTURE.products = (
        (
            "Products.GenericSetup",
            {"loadZCML": True},
        ),
        (
            "Products.DCWorkflow",
            {"loadZCML": True},
        ),
        (
            "Products.ZCTextIndex",
            {"loadZCML": True},
        ),
        (
            "Products.CMFActionIcons",
            {"loadZCML": True},
        ),
        (
            "Products.CMFUid",
            {"loadZCML": True},
        ),
        (
            "Products.CMFCalendar",
            {"loadZCML": True},
        ),
        (
            "Products.CMFCore",
            {"loadZCML": True},
        ),
        (
            "Products.CMFDefault",
            {"loadZCML": True},
        ),
        (
            "Products.PluggableAuthService",
            {"loadZCML": True},
        ),
        (
            "Products.PluginRegistry",
            {"loadZCML": True},
        ),
        (
            "Products.PlonePAS",
            {"loadZCML": True},
        ),
        (
            "Products.CMFQuickInstallerTool",
            {"loadZCML": True},
        ),
        (
            "Products.CMFFormController",
            {"loadZCML": True},
        ),
        (
            "Products.CMFDynamicViewFTI",
            {"loadZCML": True},
        ),
        (
            "Products.Archetypes",
            {"loadZCML": True},
        ),
        (
            "Products.MimetypesRegistry",
            {"loadZCML": True},
        ),
        (
            "Products.PortalTransforms",
            {"loadZCML": True},
        ),
        (
            "Products.ATContentTypes",
            {"loadZCML": True},
        ),
        (
            "Products.ATReferenceBrowserWidget",
            {"loadZCML": True},
        ),
        (
            "Products.ExternalEditor",
            {"loadZCML": True},
        ),
        (
            "Products.ExtendedPathIndex",
            {"loadZCML": True},
        ),
        (
            "Products.ResourceRegistries",
            {"loadZCML": True},
        ),
        (
            "Products.SecureMailHost",
            {"loadZCML": True},
        ),
        (
            "Products.PasswordResetTool",
            {"loadZCML": True},
        ),
        (
            "Products.TinyMCE",
            {"loadZCML": True},
        ),
        (
            "Products.CMFEditions",
            {"loadZCML": True},
        ),
        (
            "Products.CMFDiffTool",
            {"loadZCML": True},
        ),
        (
            "Products.PlacelessTranslationService",
            {"loadZCML": True},
        ),
        (
            "Products.PloneLanguageTool",
            {"loadZCML": True},
        ),
        (
            "plonetheme.classic",
            {"loadZCML": True},
        ),
        (
            "plonetheme.sunburst",
            {"loadZCML": True},
        ),
        (
            "plone.app.blob",
            {"loadZCML": True},
        ),
        (
            "plone.app.imaging",
            {"loadZCML": True},
        ),
        (
            "plone.app.collection",
            {"loadZCML": True},
        ),
        (
            "plone.app.dexterity",
            {"loadZCML": True},
        ),
        (
            "Products.CMFPlone",
            {"loadZCML": True},
        ),
        (
            "Products.PythonScripts",
            {"loadZCML": False},
        ),
    )
    defaultBases = (PLONE_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        """Set up Zope."""
        # Load ZCML
        self.loadZCML(package=collective.dms.scanbehavior, name="testing.zcml")
        z2.installProduct(app, "collective.dms.scanbehavior")

    def setUpPloneSite(self, portal):
        """Set up Plone."""
        # Install into Plone site using portal_setup
        applyProfile(portal, "collective.dms.scanbehavior:testing")

        # Login and create some test content
        setRoles(portal, TEST_USER_ID, ["Manager"])
        login(portal, TEST_USER_NAME)
        folder_id = portal.invokeFactory("Folder", "folder")
        portal[folder_id].reindexObject()

        # Commit so that the test browser sees these objects
        import transaction

        transaction.commit()

    def tearDownZope(self, app):
        """Tear down Zope."""
        z2.uninstallProduct(app, "collective.dms.scanbehavior")


FIXTURE = CollectiveDmsScanbehaviorLayer(name="FIXTURE")


INTEGRATION = IntegrationTesting(bases=(FIXTURE,), name="INTEGRATION")


FUNCTIONAL = FunctionalTesting(bases=(FIXTURE,), name="FUNCTIONAL")


ACCEPTANCE = FunctionalTesting(
    bases=(FIXTURE, AUTOLOGIN_LIBRARY_FIXTURE, z2.ZSERVER_FIXTURE), name="ACCEPTANCE"
)


class IntegrationTestCase(unittest.TestCase):
    """Base class for integration tests."""

    layer = INTEGRATION

    def setUp(self):
        super(IntegrationTestCase, self).setUp()
        self.portal = self.layer["portal"]


class FunctionalTestCase(unittest.TestCase):
    """Base class for functional tests."""

    layer = FUNCTIONAL
