# -*- coding: utf-8 -*-
"""
Unit tests for the Person plugin and its model
"""
from django import forms
from django.conf import settings
from django.test import TestCase

from cms.api import add_plugin, create_page
from cmsplugin_plain_text.cms_plugins import PlaintextPlugin
from djangocms_picture.cms_plugins import PicturePlugin

from richie.apps.core.factories import FilerImageFactory, UserFactory
from richie.apps.core.helpers import create_i18n_page
from richie.apps.persons.cms_plugins import PersonPlugin
from richie.apps.persons.factories import PersonFactory
from richie.apps.persons.models import PersonPluginModel


class PersonPluginTestCase(TestCase):
    """
    Test that PersonPlugin correctly displays a Person's page placeholders content
    """

    def test_cms_plugins_person_form_page_choices(self):
        """
        The form to create a person plugin should only list person pages in the select box.
        """

        class PersonPluginModelForm(forms.ModelForm):
            """A form for testing the choices in the select box"""

            class Meta:
                model = PersonPluginModel
                exclude = ()

        person = PersonFactory()
        other_page_title = "other page"
        create_page(other_page_title, "richie/fullwidth.html", settings.LANGUAGE_CODE)
        plugin_form = PersonPluginModelForm()
        self.assertIn(person.get_full_name(), plugin_form.as_table())
        self.assertNotIn(other_page_title, plugin_form.as_table())

    def test_cms_plugins_person_render(self):
        """
        Test that a PersonPlugin correctly renders person's page specific information
        """
        # Create a filer fake image
        staff = UserFactory(is_staff=True, is_superuser=True)
        image = FilerImageFactory(owner=staff)

        # Create a Person
        person = PersonFactory()
        person_page = person.extended_object

        # Add portrait to related placeholder
        portrait_placeholder = person_page.placeholders.get(slot="portrait")
        add_plugin(
            portrait_placeholder,
            PicturePlugin,
            "en",
            **{"picture": image, "attributes": {"alt": "portrait description"}}
        )
        add_plugin(
            portrait_placeholder,
            PicturePlugin,
            "fr",
            **{"picture": image, "attributes": {"alt": "description du portrait"}}
        )
        # A resume to related placeholder
        resume_placeholder = person_page.placeholders.get(slot="resume")
        add_plugin(
            resume_placeholder, PlaintextPlugin, "en", **{"body": "A short resume"}
        )
        add_plugin(
            resume_placeholder, PlaintextPlugin, "fr", **{"body": "Un résumé court"}
        )

        # Create a page to add the plugin to
        page = create_i18n_page({"en": "A page", "fr": "Une page"})
        placeholder = page.placeholders.get(slot="maincontent")
        add_plugin(placeholder, PersonPlugin, "en", **{"person": person})
        add_plugin(placeholder, PersonPlugin, "fr", **{"person": person})

        page.publish("en")
        page.publish("fr")

        # Check the page content in English
        url = page.get_absolute_url(language="en")
        response = self.client.get(url)
        # Person's name should be present as a link to the cms page
        # And CMS page title should be in title attribute of the link
        self.assertContains(
            response,
            '<a href="{url}" title="{page_title}">'.format(
                url=person_page.get_absolute_url(), page_title=person_page.get_title()
            ),
            status_code=200,
        )
        self.assertContains(response, person.get_full_name(), html=True)
        # Person's portrait and its properties should be present
        # pylint: disable=no-member
        self.assertContains(response, image.file.name)
        # Short resume should be present
        self.assertContains(
            response,
            '<div class="person-plugin__content__text">A short resume</div>',
            html=True,
        )
        # The person's full name should be wrapped in a h2
        self.assertContains(
            response,
            '<h2 class="person-plugin__content__title">{:s}</h2>'.format(
                person.get_full_name()
            ),
            html=True,
        )

        # Same checks in French
        url = page.get_absolute_url(language="fr")
        response = self.client.get(url)
        self.assertContains(
            response,
            '<a href="{url}" title="{page_title}">'.format(
                url=person_page.get_absolute_url(), page_title=person_page.get_title()
            ),
            status_code=200,
        )
        # pylint: disable=no-member
        self.assertContains(response, image.file.name)
        self.assertContains(
            response,
            '<div class="person-plugin__content__text">Un résumé court</div>',
            html=True,
        )

[(37, 'richie.apps.persons.factories.PersonFactory', 'PersonFactory', False, 'from richie.apps.persons.factories import PersonFactory\n'), (39, 'cms.api.create_page', 'create_page', False, 'from cms.api import add_plugin, create_page\n'), (49, 'richie.apps.core.factories.UserFactory', 'UserFactory', False, 'from richie.apps.core.factories import FilerImageFactory, UserFactory\n'), (50, 'richie.apps.core.factories.FilerImageFactory', 'FilerImageFactory', False, 'from richie.apps.core.factories import FilerImageFactory, UserFactory\n'), (53, 'richie.apps.persons.factories.PersonFactory', 'PersonFactory', False, 'from richie.apps.persons.factories import PersonFactory\n'), (58, 'cms.api.add_plugin', 'add_plugin', False, 'from cms.api import add_plugin, create_page\n'), (64, 'cms.api.add_plugin', 'add_plugin', False, 'from cms.api import add_plugin, create_page\n'), (72, 'cms.api.add_plugin', 'add_plugin', False, 'from cms.api import add_plugin, create_page\n'), (75, 'cms.api.add_plugin', 'add_plugin', False, 'from cms.api import add_plugin, create_page\n'), (80, 'richie.apps.core.helpers.create_i18n_page', 'create_i18n_page', False, 'from richie.apps.core.helpers import create_i18n_page\n'), (82, 'cms.api.add_plugin', 'add_plugin', False, 'from cms.api import add_plugin, create_page\n'), (83, 'cms.api.add_plugin', 'add_plugin', False, 'from cms.api import add_plugin, create_page\n')]