From 18527550ad1cf3816e1616048aa2272c0b408a97 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 30 Aug 2022 16:43:32 +0200
Subject: [PATCH] wcs: do not show A anchor if URL and label are empty (#62137)

---
 combo/apps/wcs/templates/combo/wcs/card.html  |  4 +-
 .../wcs/templates/combo/wcs/cards-field.html  |  6 ++-
 combo/apps/wcs/templates/combo/wcs/cards.html |  8 ++-
 tests/wcs/test_card.py                        | 52 +++++++++++++++++++
 4 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/combo/apps/wcs/templates/combo/wcs/card.html b/combo/apps/wcs/templates/combo/wcs/card.html
index 099b2045..2a587352 100644
--- a/combo/apps/wcs/templates/combo/wcs/card.html
+++ b/combo/apps/wcs/templates/combo/wcs/card.html
@@ -37,7 +37,9 @@
           <div class="{{ item.cell_size|default:"" }}">
             {% with item.page|default:item.url_template as url_key %}
             {% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:url_key|force_escape as link_url %}
-            <div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
+            {% if link_label and link_url %}
+                <div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
+            {% endif %}
             {% endwith %}
             {% endwith %}
           </div>
diff --git a/combo/apps/wcs/templates/combo/wcs/cards-field.html b/combo/apps/wcs/templates/combo/wcs/cards-field.html
index a3f2ecc4..be9b9825 100644
--- a/combo/apps/wcs/templates/combo/wcs/cards-field.html
+++ b/combo/apps/wcs/templates/combo/wcs/cards-field.html
@@ -9,7 +9,11 @@
   {% if item.template and item.page|default:item.url_template %}
     {% with item.page|default:item.url_template as url_key %}
     {% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:url_key|force_escape as link_url %}
-    {% if not ul_display %}<td>{% endif %}<a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a>{% if not ul_display %}</td>{% endif %}
+    {% if not ul_display %}<td>{% endif %}
+    {% if link_label and link_url %}
+    <a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a>
+    {% endif %}
+    {% if not ul_display %}</td>{% endif %}
     {% endwith %}
     {% endwith %}
   {% endif %}
diff --git a/combo/apps/wcs/templates/combo/wcs/cards.html b/combo/apps/wcs/templates/combo/wcs/cards.html
index f9511328..a2c684ce 100644
--- a/combo/apps/wcs/templates/combo/wcs/cards.html
+++ b/combo/apps/wcs/templates/combo/wcs/cards.html
@@ -15,8 +15,14 @@
   {% if custom_schema.grid_headers %}
   <thead>
     {% for item in custom_schema.cells %}
-    {% if item.varname == "@custom@" or item.varname == "@link@" %}
+    {% if item.varname == "@custom@" %}
+      {% if item.template %}
       <th>{{ item.header|default:"" }}</th>
+      {% endif %}
+    {% elif item.varname == "@link@" %}
+      {% if item.template and item.page|default:item.url_template %}
+      <th>{{ item.header|default:"" }}</th>
+      {% endif %}
     {% else %}
       {% if item.varname %}
       {% with fields_by_varnames|get:item.varname as field %}
diff --git a/tests/wcs/test_card.py b/tests/wcs/test_card.py
index 9d491ae3..324c0f70 100644
--- a/tests/wcs/test_card.py
+++ b/tests/wcs/test_card.py
@@ -1043,6 +1043,19 @@ def test_card_cell_table_mode_render_custom_schema_link_entry(mock_send, context
     cell.save()
     test('<i>a</i> - Foo Bar', '/foo/bar/42/', 'pk-button')
 
+    # empty label or empty url: no link in output
+    cell.custom_schema['cells'][0]['url_template'] = '{{ None|default:"" }}'
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('ul li a') == []
+    assert PyQuery(result).find('table tr td a') == []
+    cell.custom_schema['cells'][0]['url_template'] = 'foo/bar'
+    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('ul li a') == []
+    assert PyQuery(result).find('table tr td a') == []
+
     # check with page link
     root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
     page1 = Page.objects.create(
@@ -1061,7 +1074,9 @@ def test_card_cell_table_mode_render_custom_schema_link_entry(mock_send, context
         parent=other_root_page,
     )
 
+    cell.custom_schema['cells'][0]['url_template'] = ''
     cell.custom_schema['cells'][0]['page'] = page1.pk
+    cell.custom_schema['cells'][0]['template'] = '{{ card.fields.fielda }} - {{ card.fields.related }}'
     cell.save()
     test('<i>a</i> - Foo Bar', '/root/card/11/', 'pk-button')
 
@@ -1069,6 +1084,19 @@ def test_card_cell_table_mode_render_custom_schema_link_entry(mock_send, context
     cell.save()
     test('<i>a</i> - Foo Bar', '/other-root/card-bis/11/', 'pk-button')
 
+    # empty label or empty url: no link in output
+    cell.custom_schema['cells'][0]['page'] = 0
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('ul li a') == []
+    assert PyQuery(result).find('table tr td a') == []
+    cell.custom_schema['cells'][0]['page'] = page1.pk
+    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('ul li a') == []
+    assert PyQuery(result).find('table tr td a') == []
+
 
 @mock.patch('requests.Session.send', side_effect=mocked_requests_send)
 @pytest.mark.parametrize('with_headers', [True, False])
@@ -2046,6 +2074,17 @@ def test_card_cell_card_mode_render_custom_schema_link_entry(mock_send, context,
         in cell_resp
     )
 
+    # empty label or empty url: no link in output
+    cell.custom_schema['cells'][0]['url_template'] = '{{ None|default:"" }}'
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('.value a') == []
+    cell.custom_schema['cells'][0]['url_template'] = 'foo/bar'
+    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('.value a') == []
+
     # check with page link
     root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
     page1 = Page.objects.create(
@@ -2064,7 +2103,9 @@ def test_card_cell_card_mode_render_custom_schema_link_entry(mock_send, context,
         parent=other_root_page,
     )
 
+    cell.custom_schema['cells'][0]['url_template'] = ''
     cell.custom_schema['cells'][0]['page'] = page1.pk
+    cell.custom_schema['cells'][0]['template'] = 'Foo'
     cell.save()
     result = cell.render(context)
     assert PyQuery(result).find('.value a').attr['href'] == '/root/card/11/'
@@ -2074,6 +2115,17 @@ def test_card_cell_card_mode_render_custom_schema_link_entry(mock_send, context,
     result = cell.render(context)
     assert PyQuery(result).find('.value a').attr['href'] == '/other-root/card-bis/11/'
 
+    # empty label or empty url: no link in output
+    cell.custom_schema['cells'][0]['page'] = 0
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('.value a') == []
+    cell.custom_schema['cells'][0]['page'] = page1.pk
+    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
+    cell.save()
+    result = cell.render(context)
+    assert PyQuery(result).find('.value a') == []
+
 
 @mock.patch('requests.Session.send', side_effect=mocked_requests_send)
 def test_card_cell_card_mode_render_all_cards(mock_send, nocache, app):
-- 
2.35.1

