Files
neuf-www/dnscms/tests/test_rich_text.py
T

52 lines
1.9 KiB
Python

"""Tests for the custom "button" rich text link feature (dnscms/rich_text.py).
- DB HTML -> front-end HTML (expand_db_html)
- DB HTML -> ContentState (editor load)
- ContentState -> DB HTML (editor save)
"""
import json
from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter
from wagtail.rich_text import expand_db_html
DB_HTML = '<a linktype="button" data-href="/billetter/">Kjøp billett</a>'
def test_expand_db_html_rewrites_button_link():
assert expand_db_html(DB_HTML) == '<a class="button" href="/billetter/">Kjøp billett</a>'
def test_expand_db_html_escapes_href():
html = expand_db_html('<a linktype="button" data-href="/x/?a=1&amp;b=2">B</a>')
assert html == '<a class="button" href="/x/?a=1&amp;b=2">B</a>'
def test_expand_db_html_missing_href_falls_back_to_empty():
assert expand_db_html('<a linktype="button">B</a>') == '<a class="button" href="">B</a>'
def test_db_html_to_contentstate():
"""Editor load: the linktype anchor becomes a MUTABLE BUTTON entity."""
converter = ContentstateConverter(features=["button-link"])
contentstate = json.loads(converter.from_database_format(DB_HTML))
entities = list(contentstate["entityMap"].values())
assert len(entities) == 1
entity = entities[0]
assert entity["type"] == "BUTTON"
assert entity["mutability"] == "MUTABLE"
assert entity["data"] == {"url": "/billetter/"}
block = contentstate["blocks"][0]
assert block["text"] == "Kjøp billett"
assert len(block["entityRanges"]) == 1
def test_contentstate_roundtrip_preserves_button_link():
"""Editor load + save must not lose or mangle the stored linktype anchor."""
stored = f'<p data-block-key="abc12">{DB_HTML}</p>'
converter = ContentstateConverter(features=["button-link"])
roundtripped = converter.to_database_format(converter.from_database_format(stored))
assert roundtripped == stored