from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.base.interfaces import ISearchSchema
from plone.registry.interfaces import IRegistry
from plone.testing.zope import Browser
from Products.CMFPlone.testing import PRODUCTS_CMFPLONE_FUNCTIONAL_TESTING
from zope.component import getMultiAdapter
from zope.component import getUtility

import unittest


class SearchControlPanelFunctionalTest(unittest.TestCase):
    """Test that changes in the search control panel are actually
    stored in the registry.
    """

    layer = PRODUCTS_CMFPLONE_FUNCTIONAL_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.portal_url = self.portal.absolute_url()
        self.browser = Browser(self.app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            "Authorization", f"Basic {SITE_OWNER_NAME}:{SITE_OWNER_PASSWORD}"
        )

    def test_search_control_panel_link(self):
        self.browser.open("%s/@@overview-controlpanel" % self.portal_url)
        # The first "Search" link actually points to "Advanced Search"...
        # self.browser.getLink("Search").click()
        self.browser.getLink(url=f"{self.portal_url}/@@search-controlpanel").click()

    def test_search_control_panel_backlink(self):
        self.browser.open("%s/@@search-controlpanel" % self.portal_url)
        self.assertTrue("General" in self.browser.contents)

    def test_search_control_panel_sidebar(self):
        self.browser.open("%s/@@search-controlpanel" % self.portal_url)
        self.browser.getLink("Site Setup").click()
        self.assertTrue(self.browser.url.endswith("/plone/@@overview-controlpanel"))

    def test_search_controlpanel_view(self):
        view = getMultiAdapter(
            (self.portal, self.portal.REQUEST), name="search-controlpanel"
        )
        self.assertTrue(view())

    def test_enable_livesearch(self):
        self.browser.open("%s/@@search-controlpanel" % self.portal_url)
        self.browser.getControl("Enable LiveSearch").selected = True
        self.browser.getControl("Save").click()

        registry = getUtility(IRegistry)
        settings = registry.forInterface(ISearchSchema, prefix="plone")
        self.assertEqual(settings.enable_livesearch, True)

    def test_types_not_searched(self):
        self.browser.open("%s/@@search-controlpanel" % self.portal_url)
        self.browser.getControl(name="form.widgets.types_not_searched:list").value = [
            "News Item",
        ]
        self.browser.getControl("Save").click()

        registry = getUtility(IRegistry)
        settings = registry.forInterface(ISearchSchema, prefix="plone")
        self.assertTrue("News Item" in settings.types_not_searched)
