diff --git a/lizmap_server/get_legend_graphic.py b/lizmap_server/get_legend_graphic.py
index e337be91..227ef538 100644
--- a/lizmap_server/get_legend_graphic.py
+++ b/lizmap_server/get_legend_graphic.py
@@ -8,6 +8,8 @@
import json
import re
+from typing import Optional
+
from qgis.core import Qgis, QgsDataSourceUri, QgsProject
from qgis.server import QgsServerFilter
@@ -21,6 +23,13 @@ class GetLegendGraphicFilter(QgsServerFilter):
only works for single LAYER and STYLE(S) and json format.
"""
+ FEATURE_COUNT_REGEXP = r"(.*) \[≈?(?:\d+|N/A)\]"
+
+ @classmethod
+ def match_label_feature_count(cls, symbol_label: str) -> Optional[re.Match]:
+ """Regexp for extracting the feature count from the label. """
+ return re.match(cls.FEATURE_COUNT_REGEXP, symbol_label)
+
@exception_handler
def responseComplete(self):
@@ -129,11 +138,13 @@ def responseComplete(self):
symbol = symbols[idx]
symbol_label = symbol['title']
if show_feature_count:
- match_label = re.match(r"(.*) \[≈?(?:\d+|N/A)\]", symbol_label)
+ match_label = self.match_label_feature_count(symbol_label)
if match_label:
symbol_label = match_label.group(1)
else:
- logger.info("GetLegendGraphic JSON: symbol label does not match '(.*) \\[≈?(?:\\d+|N/A)\\]' '{}'".format(symbol['title']))
+ logger.info(
+ "GetLegendGraphic JSON: symbol label does not match '{}' '{}'".format(
+ self.FEATURE_COUNT_REGEXP, symbol['title']))
try:
category = categories[symbol_label]
symbol['ruleKey'] = category['ruleKey']
diff --git a/test/test_get_feature_info.py b/test/test_get_feature_info.py
index 5110643c..86a90b90 100644
--- a/test/test_get_feature_info.py
+++ b/test/test_get_feature_info.py
@@ -1,6 +1,7 @@
import logging
import xml.etree.ElementTree as ET
+from qgis.core import Qgis
from xmldiff import main as xml_diff
LOGGER = logging.getLogger('server')
@@ -52,10 +53,12 @@ def test_no_get_feature_info_default_popup(client):
rv = client.get(qs, PROJECT)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('text/xml') == 0
- expected = f'''
-
-
-'''
+ title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
+ expected = f'''
+
+
+
+ '''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff
@@ -66,21 +69,23 @@ def test_single_get_feature_info_default_popup(client):
rv = client.get(qs, PROJECT)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('text/xml') == 0
- expected = f'''
-
-
-
-
-
-
-
-
-
-
-
-
-
-'''
+ title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
+ expected = f'''
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ '''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff
@@ -92,21 +97,23 @@ def test_single_get_feature_info_default_popup_user(client):
rv = client.get(qs, PROJECT, headers)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('text/xml') == 0
- expected = f'''
-
-
-
-
-
-
-
-
-
-
-
-
-
-'''
+ title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
+ expected = f'''
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ '''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff
@@ -118,21 +125,24 @@ def test_single_get_feature_info_qgis_popup(client):
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('text/xml') == 0
+ title_attribute = f'title="{LAYER_QGIS_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
+
# Note the line
maptip |
- expected = f'''
-
-
-
-
-
-
-
-
-
-
-
-
-'''
+ expected = f'''
+
+
+
+
+
+
+
+
+
+
+
+
+
+ '''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff
@@ -156,17 +166,20 @@ def test_single_get_feature_info_form_popup(client):
xml_lines = ET.tostring(root, encoding='utf8', method='xml').decode("utf-8").split('\n')
xml_string = '\n'.join(xml_lines[1:])
- expected = f'''
-
-
-
-
-
-
-
-
-
-
+ title_attribute = f'title="{LAYER_QGIS_FORM}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
+
+ expected = f'''
+
+
+
+
+
+
+
+
+
+
+
'''
diff = xml_diff.diff_texts(expected, xml_string.strip())
diff --git a/test/test_legend.py b/test/test_legend.py
index 5005f1c5..f87f6149 100644
--- a/test/test_legend.py
+++ b/test/test_legend.py
@@ -4,6 +4,8 @@
from qgis.core import Qgis
+from lizmap_server.get_legend_graphic import GetLegendGraphicFilter
+
LOGGER = logging.getLogger('server')
__copyright__ = 'Copyright 2023, 3Liz'
@@ -137,3 +139,15 @@ def test_simple_rule_based_feature_count(client):
assert symbols[0]['expression'] == expected, symbols[0]['expression']
assert b['title'] == ''
assert b['nodes'][0]['title'] == 'rule_based [4]', b['nodes'][0]['title']
+
+
+def test_regexp_feature_count():
+ """ Test the regexp about the feature count. """
+ result = GetLegendGraphicFilter.match_label_feature_count("A label [22]")
+ assert result.group(1) == "A label", result.group(1)
+
+ result = GetLegendGraphicFilter.match_label_feature_count("A label [≈2]")
+ assert result.group(1) == "A label", result.group(1)
+
+ result = GetLegendGraphicFilter.match_label_feature_count("A label")
+ assert result is None