"""Setup/installation tests for this package."""

from plone import api
from plone.browserlayer import utils
from ploneintranet.todo.browser.interfaces import IPloneintranetTodoLayer
from ploneintranet.todo.testing import IntegrationTestCase
from Products.CMFPlone.utils import get_installer

PROJECTNAME = "ploneintranet.todo"


class TestInstall(IntegrationTestCase):
    """Test installation of ploneintranet.todo into Plone."""

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer["portal"]
        self.installer = get_installer(self.portal, self.layer["request"])

    def test_product_installed(self):
        """Test if ploneintranet.todo is installed"""
        self.assertTrue(self.installer.is_product_installed(PROJECTNAME))

    def test_type_installed(self):
        portal_types = api.portal.get_tool("portal_types")
        fti = portal_types.get("todo", None)
        self.assertEqual(fti.id, "todo")

    def test_app_installed(self):
        self.assertEqual(self.portal.apps.todo.app, "@@app-todo")

    def test_browserlayer(self):
        self.assertIn(IPloneintranetTodoLayer, utils.registered_layers())


class TestUninstall(IntegrationTestCase):
    """Test that ploneintranet.todo is properly uninstalled."""

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer["portal"]
        self.installer = get_installer(self.portal, self.layer["request"])
        self.installer.uninstall_product("ploneintranet.todo")

    def test_uninstall(self):
        """Test if ploneintranet.todo is cleanly uninstalled."""
        self.assertFalse(self.installer.is_product_installed("ploneintranet.todo"))

    def test_type_removed(self):
        portal_types = api.portal.get_tool("portal_types")
        fti = portal_types.get("todo", None)
        self.assertIsNone(fti)

    def test_roles_removed(self):
        valid_roles = self.portal.valid_roles()
        pas = api.portal.get_tool("acl_users")
        roles = pas.portal_role_manager.listRoleIds()
        for role in ["Assignee"]:
            self.assertNotIn(role, valid_roles)
            self.assertNotIn(role, roles)

    def test_browserlayer(self):
        self.assertNotIn(IPloneintranetTodoLayer, utils.registered_layers())
