from plone import api
from plone.browserlayer.utils import registered_layers
from ploneintranet.library.interfaces import ILibraryContentLayer
from ploneintranet.library.interfaces import IPloneintranetLibraryLayer
from ploneintranet.library.testing import IntegrationTestCase
from Products.CMFPlone.utils import get_installer

PROJECTNAME = "ploneintranet.library"


class TestInstall(IntegrationTestCase):
    def setUp(self):
        super().setUp()
        self.portal = self.layer["portal"]
        self.installer = get_installer(self.portal, self.layer["request"])

    def test_product_is_installed(self):
        self.assertTrue(self.installer.is_product_installed(PROJECTNAME))

    def test_addon_layer(self):
        self.assertIn(IPloneintranetLibraryLayer, registered_layers())

    def test_app_layer(self):
        self.assertIn(ILibraryContentLayer, registered_layers())


class TestUninstall(IntegrationTestCase):
    def setUp(self):
        super().setUp()
        self.portal = self.layer["portal"]
        self.installer = get_installer(self.portal, self.layer["request"])
        with api.env.adopt_roles(["Manager"]):
            self.installer.uninstall_product(PROJECTNAME)

    def test_uninstalled(self):
        self.assertFalse(self.installer.is_product_installed(PROJECTNAME))

    def test_addon_layer_removed(self):
        self.assertNotIn(IPloneintranetLibraryLayer, registered_layers())

    def test_app_layer_removed(self):
        self.assertNotIn(ILibraryContentLayer, registered_layers())
