From 9ee3353d8392aeaf6ffd20d728bda02615ded236 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 5 Nov 2021 14:30:06 +0100
Subject: [PATCH] wcs: empty card cell if card_id result is empty (#58413)

---
 combo/apps/wcs/models.py                     |  7 +++---
 combo/apps/wcs/templates/combo/wcs/card.html |  4 ++-
 tests/test_wcs.py                            | 26 +++++++++++++++++---
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index d468194d..519c2862 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -963,12 +963,9 @@ class WcsCardInfosCell(CardMixin, CellBase):
     def get_card_id(self, context):
         if self.card_id:
             try:
-                card_id = Template('{%% load wcs %%}%s' % self.card_id).render(Context(context))
+                return Template('{%% load wcs %%}%s' % self.card_id).render(Context(context))
             except (VariableDoesNotExist, TemplateSyntaxError):
                 return None
-            else:
-                if card_id:
-                    return card_id
 
         card_slug = self.carddef_reference.split(':')[1]
         card_id = '%s_id' % card_slug
@@ -994,6 +991,8 @@ class WcsCardInfosCell(CardMixin, CellBase):
 
         card_id = self.get_card_id(context)
         if not card_id:
+            if self.card_id:  # template defined, but result is empty or None
+                extra_context['card_not_found'] = True
             return extra_context
         card_slug = self.carddef_reference.split(':')[1]
         api_url = '/api/cards/%s/%s/' % (card_slug, card_id)
diff --git a/combo/apps/wcs/templates/combo/wcs/card.html b/combo/apps/wcs/templates/combo/wcs/card.html
index 73109461..b8079a6b 100644
--- a/combo/apps/wcs/templates/combo/wcs/card.html
+++ b/combo/apps/wcs/templates/combo/wcs/card.html
@@ -1,13 +1,14 @@
 {% load assets i18n %}
 
 {% block cell-content %}
+{% if not card_not_found %}
 
 {% block cell-header %}
 {% if title %}<h2>{{ title }}</h2>{% endif %}
 {% include "combo/asset_picture_fragment.html" %}
 {% endblock %}
 
-{% if card %}
+{% if card.id %}
 <div class="cell--body">
 {% spaceless %}
 {% if cell.custom_schema %}
@@ -54,4 +55,5 @@
 </div>
 {% endif %}
 
+{% endif %}
 {% endblock %}
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index c6cdb6a6..507b61eb 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1913,29 +1913,47 @@ def test_card_cell_render_identifier(mock_send, context, nocache):
 
     # check url called
     mock_send.reset_mock()
-    cell.render(context)
+    result = cell.render(context)
     assert '/api/cards/card_model_1/11/' in mock_send.call_args_list[0][0][0].url
+    assert 'Card Model 1' in result
+    assert '<p>Unknown Card</p>' not in result
 
     # with identifier
     cell.card_id = '42'
     cell.save()
     mock_send.reset_mock()
-    cell.render(context)
+    result = cell.render(context)
     assert '/api/cards/card_model_1/42/' in mock_send.call_args_list[0][0][0].url
+    assert 'Card Model 1' in result
+    assert '<p>Unknown Card</p>' in result
 
     context['cards'] = Cards()
     cell.card_id = '{% cards|objects:"card_model_1"|last|get:"id" %}'  # syntax error
     cell.save()
     mock_send.reset_mock()
-    cell.render(context)
+    result = cell.render(context)
     assert mock_send.call_args_list == []
+    assert result.replace('\n', '') == ''  # empty-cell
 
+    del context['card_not_found']
     cell.card_id = '{{ cards|objects:"card_model_1"|last|get:"id" }}'
     cell.save()
     mock_send.reset_mock()
-    cell.render(context)
+    result = cell.render(context)
     assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
     assert '/api/cards/card_model_1/13/' in mock_send.call_args_list[1][0][0].url
+    assert 'Card Model 1' in result
+    assert '<p>Unknown Card</p>' in result
+
+    # with a card_id template, but result is empty
+    del context['card_model_1_id']
+    cell.card_id = '{{ foo }}'
+    cell.save()
+    assert cell.get_card_id(context) == ''
+    mock_send.reset_mock()
+    result = cell.render(context)
+    assert mock_send.call_args_list == []
+    assert result.replace('\n', '') == ''  # empty-cell
 
 
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
-- 
2.33.0

