"""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 = 'Kjøp billett' def test_expand_db_html_rewrites_button_link(): assert expand_db_html(DB_HTML) == 'Kjøp billett' def test_expand_db_html_escapes_href(): html = expand_db_html('B') assert html == 'B' def test_expand_db_html_missing_href_falls_back_to_empty(): assert expand_db_html('B') == 'B' 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'

{DB_HTML}

' converter = ContentstateConverter(features=["button-link"]) roundtripped = converter.to_database_format(converter.from_database_format(stored)) assert roundtripped == stored