Merge pull request #202 from plone/maurits-normalize-gettext-tests

Test-only fix: normalize white space when comparing output of getText
This commit is contained in:
Maurits van Rees 2022-06-10 15:41:00 +02:00 committed by GitHub
commit 3cc1efa8a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 7 deletions

3
news/49.bugfix Normal file
View File

@ -0,0 +1,3 @@
Test-only fix: normalize white space when comparing output of ``comment.getText()``.
Needed to not fail with newer ``plone.outputfilters``.
[maurits]

View File

@ -18,6 +18,22 @@ logger = logging.getLogger("plone.app.discussion.tests")
logger.addHandler(logging.StreamHandler()) logger.addHandler(logging.StreamHandler())
def normalize(value):
# Strip all white spaces of every line, then join on one line.
# But try to avoid getting 'Go to<a href' instead of 'Go to <a href'.
lines = []
for line in value.splitlines():
line = line.strip()
if (
line.startswith("<")
and not line.startswith("</")
and not line.startswith("<br")
):
line = " " + line
lines.append(line)
return "".join(lines).strip()
class CommentTest(unittest.TestCase): class CommentTest(unittest.TestCase):
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
@ -156,15 +172,15 @@ class CommentTest(unittest.TestCase):
comment1 = createObject("plone.Comment") comment1 = createObject("plone.Comment")
comment1.text = "First paragraph\n\nSecond_paragraph" comment1.text = "First paragraph\n\nSecond_paragraph"
self.assertEqual( self.assertEqual(
"".join(comment1.getText().split()), normalize(comment1.getText()),
"<p>Firstparagraph<br><br>Second_paragraph</p>", "<p>First paragraph<br><br>Second_paragraph</p>",
) )
def test_getText_escapes_HTML(self): def test_getText_escapes_HTML(self):
comment1 = createObject("plone.Comment") comment1 = createObject("plone.Comment")
comment1.text = "<b>Got HTML?</b>" comment1.text = "<b>Got HTML?</b>"
self.assertEqual( self.assertEqual(
comment1.getText(), normalize(comment1.getText()),
"<p>&lt;b&gt;Got HTML?&lt;/b&gt;</p>", "<p>&lt;b&gt;Got HTML?&lt;/b&gt;</p>",
) )
@ -172,13 +188,13 @@ class CommentTest(unittest.TestCase):
comment1 = createObject("plone.Comment") comment1 = createObject("plone.Comment")
comment1.text = "Umlaute sind ä, ö und ü." comment1.text = "Umlaute sind ä, ö und ü."
out = b"<p>Umlaute sind \xc3\xa4, \xc3\xb6 und \xc3\xbc.</p>" out = b"<p>Umlaute sind \xc3\xa4, \xc3\xb6 und \xc3\xbc.</p>"
self.assertEqual(comment1.getText(), out.decode("utf8")) self.assertEqual(normalize(comment1.getText()), out.decode("utf8"))
def test_getText_doesnt_link(self): def test_getText_doesnt_link(self):
comment1 = createObject("plone.Comment") comment1 = createObject("plone.Comment")
comment1.text = "Go to http://www.plone.org" comment1.text = "Go to http://www.plone.org"
self.assertEqual( self.assertEqual(
comment1.getText(), normalize(comment1.getText()),
"<p>Go to http://www.plone.org</p>", "<p>Go to http://www.plone.org</p>",
) )
@ -187,7 +203,7 @@ class CommentTest(unittest.TestCase):
comment1.text = "Go to http://www.plone.org" comment1.text = "Go to http://www.plone.org"
comment1.mime_type = "text/x-web-intelligent" comment1.mime_type = "text/x-web-intelligent"
self.assertEqual( self.assertEqual(
comment1.getText(), normalize(comment1.getText()),
'Go to <a href="http://www.plone.org" ' 'Go to <a href="http://www.plone.org" '
+ 'rel="nofollow">http://www.plone.org</a>', + 'rel="nofollow">http://www.plone.org</a>',
) )
@ -197,7 +213,7 @@ class CommentTest(unittest.TestCase):
comment1.text = 'Go to <a href="http://www.plone.org">plone.org</a>' comment1.text = 'Go to <a href="http://www.plone.org">plone.org</a>'
comment1.mime_type = "text/html" comment1.mime_type = "text/html"
self.assertEqual( self.assertEqual(
comment1.getText(), normalize(comment1.getText()),
'Go to <a href="http://www.plone.org">plone.org</a>', 'Go to <a href="http://www.plone.org">plone.org</a>',
) )