from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import MOCK_MAILHOST_FIXTURE
from plone.app.testing import PloneSandboxLayer
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.testing import zope as zope_testing
from plone.testing.zope import WSGI_SERVER_FIXTURE
from ploneintranet import api as pi_api
from Products.CMFCore.indexing import processQueue


class PloneintranetuserprofileLayer(PloneSandboxLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    products = ("Products.membrane",)

    def setUpZope(self, app, configurationContext):
        import ploneintranet.userprofile

        self.loadZCML(package=ploneintranet.userprofile)

        for p in self.products:
            zope_testing.installProduct(app, p)

    def setUpPloneSite(self, portal):
        applyProfile(portal, "ploneintranet.userprofile:testing")
        setRoles(portal, TEST_USER_ID, ["Manager"])
        # Remove the memcached counter.
        # This is needed to prevent the same cache key to be used in different tests
        if hasattr(portal.acl_users, "_memcachedcounter"):
            delattr(portal.acl_users, "_memcachedcounter")

    def tearDownZope(self, app):
        """Tear down Zope."""
        for p in reversed(self.products):
            zope_testing.uninstallProduct(app, p)


class PloneintranetuserprofileLayerWithDummyContent(PloneintranetuserprofileLayer):
    def setUpPloneSite(self, portal):
        pi_api.userprofile.create(
            username="johndoe",
            email="johndoe@example.com",
            properties={"first_name": "John", "last_name": "Doe"},
            approve=True,
        )
        pi_api.userprofile.create(
            username="johndoe2",
            email="johndoe2@example.com",
            properties={"first_name": "John", "last_name": "Doe 2"},
            approve=True,
        )
        pi_api.workgroup.create(
            canonical="Test group canonical name",
            title="Test group",
            description="",
            email="testgroup@example.com",
        )
        pi_api.workgroup.create(
            canonical="Test unicode group canonical name (°⌣°)",
            title="Test unicode group (°⌣°)",
            description="",
            email="testunicodegroup@example.com",
        )
        processQueue()


PLONEINTRANET_USERPROFILE_FIXTURE = PloneintranetuserprofileLayer()
PLONEINTRANET_USERPROFILE_FIXTURE_WITH_DUMMY_CONTENT = (
    PloneintranetuserprofileLayerWithDummyContent()
)


PLONEINTRANET_USERPROFILE_INTEGRATION_TESTING = IntegrationTesting(
    bases=(PLONEINTRANET_USERPROFILE_FIXTURE, MOCK_MAILHOST_FIXTURE),
    name="PloneintranetuserprofileLayer:Integration",
)

PLONEINTRANET_USERPROFILE_INTEGRATION_TESTING_DUMMY_CONTENT = IntegrationTesting(
    bases=(
        PLONEINTRANET_USERPROFILE_FIXTURE,
        PLONEINTRANET_USERPROFILE_FIXTURE_WITH_DUMMY_CONTENT,
    ),
    name="PloneintranetuserprofileWithDummyContentLayer:Integration",
)

PLONEINTRANET_USERPROFILE_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(PLONEINTRANET_USERPROFILE_FIXTURE,),
    name="PloneintranetuserprofileLayer:Functional",
)

PLONEINTRANET_USERPROFILE_BROWSER_TESTING = FunctionalTesting(
    bases=(PLONEINTRANET_USERPROFILE_FIXTURE, WSGI_SERVER_FIXTURE),
    name="PloneintranetuserprofileLayer:Browser",
)
