diff --git a/taccsite_cms/templates/djangocms_picture/default/picture.html b/taccsite_cms/templates/djangocms_picture/default/picture.html index c082c6222..672e3dced 100644 --- a/taccsite_cms/templates/djangocms_picture/default/picture.html +++ b/taccsite_cms/templates/djangocms_picture/default/picture.html @@ -28,7 +28,10 @@ {# start render figure/figcaption #} {# TACC (support children as caption content): #} {# {% if instance.caption_text %} #} -{% if instance.caption_text or instance.child_plugin_instances %} +{# TACC (fall back to image default caption): #} +{# {% if instance.caption_text or instance.child_plugin_instances %} #} +{% if instance.caption_text or instance.child_plugin_instances or instance.picture.default_caption %} +{# /TACC #} {# TACC (assign attributes to parent): #} {#
#}
@@ -60,7 +63,10 @@ {# TACC (allow link to be conditional): #} {% block picture_attributes %} {# TACC (assign attributes to parent): #} - {% if not instance.caption_text and not picture_link and not instance.child_plugin_instances %} + {# TACC (fall back to image default caption): #} + {# {% if not instance.caption_text and not picture_link and not instance.child_plugin_instances %} #} + {% if not instance.caption_text and not instance.picture.default_caption and not picture_link and not instance.child_plugin_instances %} + {# /TACC #} {{ instance.attributes_str }} {% endif %} {# /TACC #} @@ -71,11 +77,17 @@ {# start render figure/figcaption #} {# {% if instance.caption_text %} #} -{% if instance.caption_text or instance.child_plugin_instances %} +{# TACC (fall back to image default caption): #} +{# {% if instance.caption_text or instance.child_plugin_instances %} #} +{% if instance.caption_text or instance.child_plugin_instances or instance.picture.default_caption %} +{# /TACC #} {# TACC (support children as caption content): #} {#
{{ instance.caption_text }}
#}
- {{ instance.caption_text }} + {# TACC (fall back to image default caption): #} + {# {{ instance.caption_text }} #} + {% if instance.caption_text %}{{ instance.caption_text }}{% elif instance.picture.default_caption and not instance.child_plugin_instances %}{{ instance.picture.default_caption }}{% endif %} + {# /TACC #} {% for plugin in instance.child_plugin_instances %} {% render_plugin plugin %} {% endfor %} diff --git a/taccsite_cms/tests/__init__.py b/taccsite_cms/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/taccsite_cms/tests/djangocms_picture_default_caption.py b/taccsite_cms/tests/djangocms_picture_default_caption.py new file mode 100644 index 000000000..04294b352 --- /dev/null +++ b/taccsite_cms/tests/djangocms_picture_default_caption.py @@ -0,0 +1,94 @@ +"""Tests for the TACC `default_caption` fallback in the Picture template. + +The override at +``taccsite_cms/templates/djangocms_picture/default/picture.html`` falls back to +the filer Image's ``default_caption`` for the ``
`` when the editor +provides no instance-specific caption content (neither ``caption_text`` nor +child plugins), mirroring the existing ``default_alt_text`` fallback for ``alt``. +""" + +from django.template.loader import get_template +from django.test import SimpleTestCase + + +class _FakeImage: + """Stand-in for a django-filer Image (``instance.picture``).""" + + def __init__(self, default_alt_text='', default_caption=''): + self.default_alt_text = default_alt_text + self.default_caption = default_caption + + +class _TruthyEmpty: + """Truthy container that iterates to nothing. + + Lets the "child plugins are present" branches be exercised without invoking + the ``{% render_plugin %}`` machinery (which needs full CMS request context). + The template only checks truthiness of ``child_plugin_instances`` and then + iterates it; this is truthy yet yields no items, so the loop renders nothing. + """ + + def __bool__(self): + return True + + def __len__(self): + return 1 + + def __iter__(self): + return iter(()) + + +class _FakePicture: + """Stand-in for a djangocms_picture Picture plugin instance.""" + + def __init__(self, caption_text='', children=None, default_caption='', + default_alt_text='', attributes=None): + self.caption_text = caption_text + self.child_plugin_instances = children if children is not None else [] + self.picture = _FakeImage(default_alt_text=default_alt_text, + default_caption=default_caption) + self.attributes = attributes or {} + self.attributes_str = '' + self.link_attributes_str = '' + self.link_target = '' + self.img_src = '/media/test.jpg' + self.width = None + self.height = None + + +def _render(instance): + template = get_template('djangocms_picture/default/picture.html') + return template.render({ + 'instance': instance, + 'picture_link': '', + 'img_srcset_data': [], + }) + + +class DefaultCaptionFallbackTests(SimpleTestCase): + def test_default_caption_used_when_no_instance_caption(self): + """No caption_text and no children -> default_caption renders.""" + html = _render(_FakePicture(default_caption='From the library')) + self.assertIn('', html) + self.assertIn('From the library', html) + + def test_caption_text_overrides_default_caption(self): + """caption_text wins; default_caption is not shown.""" + html = _render(_FakePicture(caption_text='Specific caption', + default_caption='Default caption')) + self.assertIn('Specific caption', html) + self.assertNotIn('Default caption', html) + + def test_child_plugins_suppress_default_caption(self): + """Child plugins are instance-specific content; default is suppressed.""" + html = _render(_FakePicture(default_caption='Default caption', + children=_TruthyEmpty())) + self.assertIn(' no
/
(unchanged behavior).""" + html = _render(_FakePicture()) + self.assertNotIn('