diff --git a/src/richie/apps/core/management/commands/create_demo_site.py b/src/richie/apps/core/management/commands/create_demo_site.py index 23e8c6d2fa..a807f5d12c 100755 --- a/src/richie/apps/core/management/commands/create_demo_site.py +++ b/src/richie/apps/core/management/commands/create_demo_site.py @@ -151,7 +151,8 @@ def create_demo_site(): NB_PERSONS, languages=[l[0] for l in settings.LANGUAGES], parent=pages_created["persons"], - with_content=True, + fill_portrait=True, + fill_resume=True, should_publish=True, ) diff --git a/src/richie/apps/persons/factories.py b/src/richie/apps/persons/factories.py index da6ba3aabb..cdc2c19edd 100644 --- a/src/richie/apps/persons/factories.py +++ b/src/richie/apps/persons/factories.py @@ -2,9 +2,7 @@ Persons factories """ import os -import random -from django.conf import settings from django.core.files import File import factory @@ -12,6 +10,7 @@ from filer.models.imagemodels import Image from ..core.factories import PageExtensionDjangoModelFactory +from ..core.helpers import create_text_plugin from ..core.tests.utils import file_getter from .models import Person, PersonTitle @@ -58,46 +57,37 @@ def title(self): @factory.post_generation # pylint: disable=unused-argument - def with_content(self, create, extracted, **kwargs): + def fill_portrait(self, create, extracted, **kwargs): """ - Add content plugins displayed in the "portrait" and "resume" placeholder - of the person page: - - - Picture plugin featuring a random portrait image, - - Text plugin featuring the person resume with a random long text. + Add a portrait with a random image """ if create and extracted: - language = settings.LANGUAGE_CODE portrait_placeholder = self.extended_object.placeholders.get( slot="portrait" ) - resume_placeholder = self.extended_object.placeholders.get(slot="resume") - - # Add a portrait with a random image portrait_file = file_getter(os.path.dirname(__file__), "portrait")() wrapped_portrait = File(portrait_file, portrait_file.name) portrait = Image.objects.create(file=wrapped_portrait) - add_plugin( - language=language, - placeholder=portrait_placeholder, - plugin_type="PicturePlugin", - picture=portrait, - attributes={"alt": "portrait image"}, - ) - - # Add a text plugin for resume with a long random text - nb_paragraphs = random.randint(2, 4) - paragraphs = [ - factory.Faker("text", max_nb_chars=random.randint(200, 1000)).generate( - {} + for language in self.extended_object.get_languages(): + add_plugin( + language=language, + placeholder=portrait_placeholder, + plugin_type="PicturePlugin", + picture=portrait, + attributes={"alt": "portrait image"}, ) - for i in range(nb_paragraphs) - ] - body = ["
{:s}
".format(p) for p in paragraphs] - add_plugin( - language=language, - placeholder=resume_placeholder, - plugin_type="CKEditorPlugin", - body="".join(body), + + @factory.post_generation + # pylint: disable=unused-argument + def fill_resume(self, create, extracted, **kwargs): + """ + Add a text plugin for resume with a long random text + """ + if create and extracted: + create_text_plugin( + self.extended_object, + "resume", + nb_paragraphs=1, + languages=self.extended_object.get_languages(), ) diff --git a/tests/apps/persons/test_factories_person.py b/tests/apps/persons/test_factories_person.py index 867373f128..aa7d00cc93 100644 --- a/tests/apps/persons/test_factories_person.py +++ b/tests/apps/persons/test_factories_person.py @@ -13,28 +13,43 @@ class PersonFactoryTestCase(TestCase): Unit test suite to validate the behavior of the Person factory """ - def test_person_factory(self): + def test_person_factory_portrait(self): """ - PersonFactoryTestCase should be able to generate plugins with realistic fake - data: portrait and resume. + PersonFactory should be able to generate plugins with a realistic portrait for several + languages. """ - person = PersonFactory(with_content=True) - - # The portrait plugin should point to one of our fixtures images - portrait_placeholder = person.extended_object.placeholders.get(slot="portrait") - portrait_plugin = portrait_placeholder.cmsplugin_set.get( - plugin_type="PicturePlugin" - ) - self.assertIn( - "portrait", - os.path.basename( - portrait_plugin.djangocms_picture_picture.picture.file.name - ), - ) - - # The resume plugin should contain paragraphs - resume_placeholder = person.extended_object.placeholders.get(slot="resume") - resume_plugin = resume_placeholder.cmsplugin_set.get( - plugin_type="CKEditorPlugin" - ) - self.assertIn("", resume_plugin.simple_text_ckeditor_simpletext.body) + person = PersonFactory(fill_portrait=True, languages=["fr", "en"]) + + # Check that the portrait plugins were created as expected + portrait = person.extended_object.placeholders.get(slot="portrait") + self.assertEqual(portrait.cmsplugin_set.count(), 2) + + # The portrait plugins should point to one of our fixtures images + for language in ["fr", "en"]: + portrait_plugin = portrait.cmsplugin_set.get( + plugin_type="PicturePlugin", language=language + ) + self.assertIn( + "portrait", + os.path.basename( + portrait_plugin.djangocms_picture_picture.picture.file.name + ), + ) + + def test_person_factory_resume(self): + """ + PersonFactory should be able to generate plugins with a realistic resume for + several languages. + """ + person = PersonFactory(fill_resume=True, languages=["fr", "en"]) + + # Check that the resume plugins were created as expected + resume = person.extended_object.placeholders.get(slot="resume") + self.assertEqual(resume.cmsplugin_set.count(), 2) + + # The resume plugins should contain paragraphs + for language in ["fr", "en"]: + resume_plugin = resume.cmsplugin_set.get( + plugin_type="CKEditorPlugin", language=language + ) + self.assertIn("
", resume_plugin.simple_text_ckeditor_simpletext.body)