from plone.browserlayer import utils
from ploneintranet.network.browser.interfaces import IPloneIntranetNetworkLayer
from ploneintranet.network.testing import IntegrationTestCase
from Products.CMFPlone.utils import get_installer

PROJECTNAME = "ploneintranet.network"


class TestInstall(IntegrationTestCase):
    """Test installation of this product into Plone."""

    def setUp(self):
        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(IPloneIntranetNetworkLayer, utils.registered_layers())

    def test_dublincore_installed(self):
        tt = self.portal.portal_types
        self.assertIn("quaive.dublincore", tt.Document.behaviors)
        self.assertNotIn("plone.dublincore", tt.Document.behaviors)


class TestUninstall(IntegrationTestCase):
    """Test this product is properly uninstalled."""

    def setUp(self):
        self.portal = self.layer["portal"]
        self.installer = get_installer(self.portal, self.layer["request"])
        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(IPloneIntranetNetworkLayer, utils.registered_layers())

    def test_utility_removed(self):
        from ploneintranet.network.interfaces import INetworkTool
        from zope.component import queryUtility

        self.assertIsNone(queryUtility(INetworkTool))

    def test_tool_removed(self):
        self.assertNotIn("ploneintranet_network", self.portal)

    def test_dublincore_uninstalled(self):
        tt = self.portal.portal_types
        self.assertNotIn("quaive.dublincore", tt.Document.behaviors)
        self.assertIn("plone.dublincore", tt.Document.behaviors)
