from plone import api
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.testing.zope import Browser
from plone.uuid.interfaces import IUUID
from ploneintranet.network.setuphandlers import restore_all_behaviors
from ploneintranet.network.testing import FunctionalTestCase

import transaction


class TestSubscribers(FunctionalTestCase):
    def setUp(self):
        app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.request = self.layer["request"]
        self.portal_url = self.portal.absolute_url()
        self.graph = api.portal.get_tool("ploneintranet_network")
        self.browser = Browser(app)
        self.browser.handleErrors = False
        self.browser.addHeader(
            "Authorization", f"Basic {SITE_OWNER_NAME}:{SITE_OWNER_PASSWORD}"
        )

    def add_testdocument(self):
        """Helper method"""
        self.browser.open(self.portal_url + "/++add++Document")
        self.browser.getControl(name="form.widgets.IDublinCore.title").value = (
            "TestDocument"
        )
        self.browser.getControl(name="form.widgets.IDublinCore.subjects").value = (
            self.separator.join(("foo", "bar", "foobar"))
        )
        self.browser.getControl("Save").click()
        self.assertTupleEqual(
            self.portal.testdocument.Subject(), ("foo", "bar", "foobar")
        )

    def test_add_document_adds_tags(self):
        self.separator = ","
        self.add_testdocument()
        doc = self.portal.testdocument
        uuid = IUUID(doc)
        tags = self.graph.unpack(self.graph.get_tags("content", uuid, SITE_OWNER_NAME))
        self.assertEqual(sorted(tags), ["bar", "foo", "foobar"])

    def test_add_document_standard_dublincore_does_not_add_tags(self):
        restore_all_behaviors()
        transaction.commit()
        self.separator = ";"
        self.add_testdocument()
        doc = self.portal.testdocument
        uuid = IUUID(doc)
        with self.assertRaises(KeyError):
            self.graph.get_tags("content", uuid, SITE_OWNER_NAME)
