from plone import api
from ploneintranet.workspace.basecontent import utils
from ploneintranet.workspace.interfaces import IWorkspaceAppFormLayer
from ploneintranet.workspace.testing import PLONEINTRANET_WORKSPACE_INTEGRATION_TESTING
from ploneintranet.workspace.tests.base import BaseTestCase
from zope.interface import alsoProvides


class TestTagging(BaseTestCase):
    """Test that a comma separated string of tabs is saved as a tuple of
    strings
    """

    layer = PLONEINTRANET_WORKSPACE_INTEGRATION_TESTING

    def setUp(self):
        super().setUp()
        alsoProvides(self.request, IWorkspaceAppFormLayer)
        workspaces = self.portal["workspaces"]
        self.ws = api.content.create(
            workspaces,
            "ploneintranet.workspace.workspacefolder",
            "test-tag-workspace",
            title="Test Tag Workspace",
        )
        self.doc = api.content.create(
            self.ws, "Document", "tagged-doc", "Tagged Document"
        )

    def tearDown(self):
        super().tearDown()
        api.content.delete(obj=self.ws)

    def test_set_multiple_tags_on_doc(self):
        """Set multiple tags on an object as a comma separated string and
        verify that they are saved as a tuple of strings."""
        self.request.form["subjects"] = "a,b,c"
        utils.dexterity_update(self.doc, self.request)
        self.assertEqual(("a", "b", "c"), self.doc.subject)

    def test_set_multiple_tags_utf8_on_doc(self):
        """Set multiple tags on an object as a comma separated string and
        verify that they are saved as a tuple of strings."""
        self.request.form["subjects"] = "a ♥,b,c"
        utils.dexterity_update(self.doc, self.request)
        self.assertEqual(("a ♥", "b", "c"), self.doc.subject)

    def test_set_multiple_tags_on_workspace(self):
        """Set multiple tags on an object as a comma separated string and
        verify that they are saved as a tuple of strings."""
        self.request.form["subjects"] = "a,b,c"
        utils.dexterity_update(self.ws, self.request)
        self.assertEqual(("a", "b", "c"), self.ws.subject)

    def test_set_multiple_tags_utf8_on_workspace(self):
        """Set multiple tags on an object as a comma separated string and
        verify that they are saved as a tuple of strings."""
        self.request.form["subjects"] = "a ♥,b,c"
        utils.dexterity_update(self.ws, self.request)
        self.assertEqual(("a ♥", "b", "c"), self.ws.subject)
