from plone import api
from ploneintranet.news.indexers import languages
from ploneintranet.news.testing import IntegrationTestCase
from ploneintranet.testing import temporary_registry_record
from zope.component import getMultiAdapter
from zope.interface.interfaces import ComponentLookupError


class TestLanguagesIndexers(IntegrationTestCase):
    def test_languages_index_registration(self):
        pc = api.portal.get_tool("portal_catalog")

        # The indexer is limited to the news items
        with self.assertRaises(ComponentLookupError):
            getMultiAdapter((self.portal, pc), name="languages")

        news = self.portal.news["press-mentions"]["fusce-fames"]

        # And it returns a list of languages for which the content is available
        self.assertListEqual(getMultiAdapter((news, pc), name="languages")(), ["en"])

    def test_languages_indexer(self):
        news = self.portal.news["press-mentions"]["fusce-fames"]

        # By default return all the available languages
        self.assertEqual(languages(news)(), ["en"])

        with temporary_registry_record("plone.available_languages", ["en", "es", "it"]):
            self.assertSetEqual(set(languages(news)()), {"en", "es", "it"})

        # unless we have a language defined
        with temporary_registry_record("plone.available_languages", ["en", "es", "it"]):
            news.language = "en"
            self.assertListEqual(languages(news)(), ["en"])

        # or if the content has a translation (in that case the translation is omitted)
        news.language = None
        translation = self.portal.news["press-mentions"]["proin-netus"]
        translation.language = "it"
        api.relation.create(news, translation, "translations")

        with temporary_registry_record("plone.available_languages", ["en", "es", "it"]):
            self.assertSetEqual(set(languages(news)()), {"en", "es"})

    def test_magazine_items_respect_multilingual(self):
        """Check that if we have a translation for a magazine item, we only
        show it if we want it.
        """
        section = self.portal.news["press-mentions"]
        news = section["fusce-fames"]
        translation_it = section["proin-netus"]
        translation_it.language = "it"
        translation_es = section["class-velit-dis-velit-integer"]
        translation_es.language = "es"
        with temporary_registry_record("plone.available_languages", ["en", "es", "it"]):
            for obj in section.listFolderContents():
                obj.reindexObject(idxs=["languages"])
            api.relation.create(news, translation_it, "translations")
            api.relation.create(news, translation_es, "translations")

        self.solr_commit()

        self.assertEqual(len(api.content.find(context=section, languages=["en"])), 3)
        self.assertEqual(len(api.content.find(context=section, languages=["it"])), 3)
        self.assertEqual(len(api.content.find(context=section, languages=["es"])), 3)

        view = api.content.get_view("view", section, self.request.clone())
        self.assertSetEqual(
            {item.getId for item in view.news_items()},
            {
                "felis-proin-senectus-quis-non-est",
                "fusce-fames",
                "massa-lacus-sociis-molestie-odio-pretium",
            },
        )

        news.REQUEST.set("LANGUAGE", "es")
        view = api.content.get_view("view", section, self.request.clone())
        self.assertSetEqual(
            {item.getId for item in view.news_items()},
            {
                "felis-proin-senectus-quis-non-est",
                "class-velit-dis-velit-integer",
                "massa-lacus-sociis-molestie-odio-pretium",
            },
        )
