from plone import api
from ploneintranet.search.solr import testing
from ploneintranet.search.solr.indexers import is_solr_enabled
from Products.CMFPlone.utils import get_installer


class DummyClass:
    @is_solr_enabled
    def dummy_method(self):
        return "solr enabled"


class TestDecorators(testing.IntegrationTestCase):
    def test_is_solr_enabled(self):
        # When the product is installed dummy_method returns 1
        dummy = DummyClass()
        self.portal = self.layer["portal"]
        self.installer = get_installer(self.portal, self.portal.REQUEST)
        self.assertEqual(dummy.dummy_method(), "solr enabled")

        # We can disabled it by setting a proper registry
        api.portal.set_registry_record("ploneintranet.search.solr.disabled", True)
        self.assertEqual(dummy.dummy_method(), None)
        api.portal.set_registry_record("ploneintranet.search.solr.disabled", False)
        self.assertEqual(dummy.dummy_method(), "solr enabled")

        # Of course if the product is uninstalled solr will be disabled
        self.installer.uninstall_product("ploneintranet.search")
        self.assertEqual(dummy.dummy_method(), None)
