0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Reorganise image/document rich text tests into separate classes for frontend and editor HTML

This commit is contained in:
Andy Chosak 2019-03-27 10:05:57 -04:00 committed by Matt Westcott
parent c5c16f7b35
commit 683a50fa47
2 changed files with 128 additions and 83 deletions

View File

@ -1,43 +1,59 @@
from bs4 import BeautifulSoup
from django.test import TestCase
from wagtail.documents.rich_text import DocumentLinkHandler as DocumentFrontendHandler
from wagtail.documents.rich_text.editor_html import DocumentLinkHandler as DocumentEditorHTMLHandler
from wagtail.documents.rich_text import DocumentLinkHandler as FrontendDocumentLinkHandler
from wagtail.documents.rich_text.editor_html import \
DocumentLinkHandler as EditorHtmlDocumentLinkHandler
class TestDocumentRichTextLinkHandler(TestCase):
class TestEditorHtmlDocumentLinkHandler(TestCase):
fixtures = ['test.json']
def test_get_db_attributes(self):
soup = BeautifulSoup('<a data-id="test-id">foo</a>', 'html5lib')
tag = soup.a
result = DocumentEditorHTMLHandler.get_db_attributes(tag)
self.assertEqual(result,
{'id': 'test-id'})
result = EditorHtmlDocumentLinkHandler.get_db_attributes(tag)
self.assertEqual(
result,
{'id': 'test-id'}
)
def test_expand_db_attributes(self):
result = DocumentFrontendHandler.expand_db_attributes({'id': 1})
self.assertEqual(result,
'<a href="/documents/1/test.pdf">')
def test_expand_db_attributes_for_editor(self):
result = EditorHtmlDocumentLinkHandler.expand_db_attributes({'id': 1})
self.assertEqual(
result,
'<a data-linktype="document" data-id="1" href="/documents/1/test.pdf">'
)
def test_expand_db_attributes_for_editor_preserves_id_of_nonexistent_document(self):
result = EditorHtmlDocumentLinkHandler.expand_db_attributes({'id': 0})
self.assertEqual(
result,
'<a data-linktype="document" data-id="0">'
)
def test_expand_db_attributes_for_editor_with_missing_id(self):
result = EditorHtmlDocumentLinkHandler.expand_db_attributes({})
self.assertEqual(
result,
'<a data-linktype="document">'
)
class TestFrontendDocumentLinkHandler(TestCase):
fixtures = ['test.json']
def test_expand_db_attributes_for_frontend(self):
result = FrontendDocumentLinkHandler.expand_db_attributes({'id': 1})
self.assertEqual(
result,
'<a href="/documents/1/test.pdf">'
)
def test_expand_db_attributes_document_does_not_exist(self):
result = DocumentFrontendHandler.expand_db_attributes({'id': 0})
result = FrontendDocumentLinkHandler.expand_db_attributes({'id': 0})
self.assertEqual(result, '<a>')
def test_expand_db_attributes_with_missing_id(self):
result = DocumentFrontendHandler.expand_db_attributes({})
result = FrontendDocumentLinkHandler.expand_db_attributes({})
self.assertEqual(result, '<a>')
def test_expand_db_attributes_for_editor(self):
result = DocumentEditorHTMLHandler.expand_db_attributes({'id': 1})
self.assertEqual(result,
'<a data-linktype="document" data-id="1" href="/documents/1/test.pdf">')
def test_expand_db_attributes_for_editor_preserves_id_of_nonexistent_document(self):
result = DocumentEditorHTMLHandler.expand_db_attributes({'id': 0})
self.assertEqual(result,
'<a data-linktype="document" data-id="0">')
def test_expand_db_attributes_for_editor_with_missing_id(self):
result = DocumentEditorHTMLHandler.expand_db_attributes({})
self.assertEqual(result, '<a data-linktype="document">')

View File

@ -1,87 +1,116 @@
from bs4 import BeautifulSoup
from django.test import TestCase
from wagtail.images.rich_text import ImageEmbedHandler as ImageFrontendHandler
from wagtail.images.rich_text.editor_html import ImageEmbedHandler as ImageEditorHTMLHandler
from wagtail.images.rich_text import ImageEmbedHandler as FrontendImageEmbedHandler
from wagtail.images.rich_text.editor_html import ImageEmbedHandler as EditorHtmlImageEmbedHandler
from wagtail.tests.utils import WagtailTestUtils
from .utils import Image, get_test_image_file
class TestImageEmbedHandlers(TestCase, WagtailTestUtils):
class TestEditorHtmlImageEmbedHandler(TestCase, WagtailTestUtils):
def test_get_db_attributes(self):
soup = BeautifulSoup(
'<b data-id="test-id" data-format="test-format" data-alt="test-alt">foo</b>',
'html5lib'
)
tag = soup.b
result = ImageEditorHTMLHandler.get_db_attributes(tag)
self.assertEqual(result,
{'alt': 'test-alt',
'id': 'test-id',
'format': 'test-format'})
def test_expand_db_attributes_image_does_not_exist(self):
result = ImageFrontendHandler.expand_db_attributes({'id': 0})
self.assertEqual(result, '<img>')
def test_expand_db_attributes_not_for_editor(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageFrontendHandler.expand_db_attributes(
{'id': 1,
'alt': 'test-alt',
'format': 'left'}
result = EditorHtmlImageEmbedHandler.get_db_attributes(tag)
self.assertEqual(
result,
{
'alt': 'test-alt',
'id': 'test-id',
'format': 'test-format',
}
)
self.assertTagInHTML('<img class="richtext-image left" />', result, allow_extra_attrs=True)
def test_expand_db_attributes_escapes_alt_text(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageFrontendHandler.expand_db_attributes(
{'id': 1,
'alt': 'Arthur "two sheds" Jackson',
'format': 'left'},
)
self.assertIn('alt="Arthur &quot;two sheds&quot; Jackson"', result)
def test_expand_db_attributes_with_missing_alt(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageFrontendHandler.expand_db_attributes(
{'id': 1,
'format': 'left'},
)
self.assertTagInHTML('<img class="richtext-image left" alt="" />', result, allow_extra_attrs=True)
def test_expand_db_attributes_for_editor(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageEditorHTMLHandler.expand_db_attributes(
{'id': 1,
'alt': 'test-alt',
'format': 'left'},
)
result = EditorHtmlImageEmbedHandler.expand_db_attributes({
'id': 1,
'alt': 'test-alt',
'format': 'left',
})
self.assertTagInHTML(
'<img data-embedtype="image" data-id="1" data-format="left" '
'data-alt="test-alt" class="richtext-image left" />', result, allow_extra_attrs=True)
(
'<img data-embedtype="image" data-id="1" data-format="left" '
'data-alt="test-alt" class="richtext-image left" />'
),
result,
allow_extra_attrs=True
)
def test_expand_db_attributes_for_editor_nonexistent_image(self):
self.assertEqual(
EditorHtmlImageEmbedHandler.expand_db_attributes({'id': 0}),
'<img>'
)
def test_expand_db_attributes_for_editor_escapes_alt_text(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageEditorHTMLHandler.expand_db_attributes(
{'id': 1,
'alt': 'Arthur "two sheds" Jackson',
'format': 'left'},
)
result = EditorHtmlImageEmbedHandler.expand_db_attributes({
'id': 1,
'alt': 'Arthur "two sheds" Jackson',
'format': 'left',
})
self.assertTagInHTML(
'<img data-embedtype="image" data-id="1" data-format="left" '
'data-alt="Arthur &quot;two sheds&quot; Jackson" class="richtext-image left" />',
(
'<img data-embedtype="image" data-id="1" data-format="left" '
'data-alt="Arthur &quot;two sheds&quot; Jackson" class="richtext-image left" />'
),
result, allow_extra_attrs=True)
self.assertIn('alt="Arthur &quot;two sheds&quot; Jackson"', result)
def test_expand_db_attributes_for_editor_with_missing_alt(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageEditorHTMLHandler.expand_db_attributes(
{'id': 1,
'format': 'left'},
)
result = EditorHtmlImageEmbedHandler.expand_db_attributes({
'id': 1,
'format': 'left',
})
self.assertTagInHTML(
'<img data-embedtype="image" data-id="1" data-format="left" data-alt="" '
'class="richtext-image left" />', result, allow_extra_attrs=True)
(
'<img data-embedtype="image" data-id="1" data-format="left" data-alt="" '
'class="richtext-image left" />'
),
result,
allow_extra_attrs=True
)
class TestFrontendImageEmbedHandler(TestCase, WagtailTestUtils):
def test_expand_db_attributes_for_frontend(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = FrontendImageEmbedHandler.expand_db_attributes({
'id': 1,
'alt': 'test-alt',
'format': 'left',
})
self.assertTagInHTML(
'<img class="richtext-image left" />',
result,
allow_extra_attrs=True
)
def test_expand_db_attributes_for_frontend_with_nonexistent_image(self):
result = FrontendImageEmbedHandler.expand_db_attributes({'id': 0})
self.assertEqual(result, '<img>')
def test_expand_db_attributes_for_frontend_escapes_alt_text(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = FrontendImageEmbedHandler.expand_db_attributes({
'id': 1,
'alt': 'Arthur "two sheds" Jackson',
'format': 'left',
})
self.assertIn('alt="Arthur &quot;two sheds&quot; Jackson"', result)
def test_expand_db_attributes_for_frontend_with_missing_alt(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = FrontendImageEmbedHandler.expand_db_attributes({
'id': 1,
'format': 'left',
})
self.assertTagInHTML('<img class="richtext-image left" alt="" />', result, allow_extra_attrs=True)