From 194f2d9f76ac8cc73274296eec1a95cd0049d791 Mon Sep 17 00:00:00 2001 From: Jon Pentland Date: Sat, 22 Oct 2022 10:48:10 +0100 Subject: [PATCH] Use configured portal timezone in tests --- .../app/discussion/tests/test_conversation.py | 30 ++++++++++++------- plone/app/discussion/tests/test_indexers.py | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/plone/app/discussion/tests/test_conversation.py b/plone/app/discussion/tests/test_conversation.py index 610ac4b..a36f88b 100644 --- a/plone/app/discussion/tests/test_conversation.py +++ b/plone/app/discussion/tests/test_conversation.py @@ -20,6 +20,7 @@ from Products.CMFCore.utils import getToolByName from zope import interface from zope.annotation.interfaces import IAnnotations from zope.component import createObject +from zope.component import getUtility from zope.component import queryUtility import unittest @@ -34,6 +35,11 @@ class ConversationTest(unittest.TestCase): setRoles(self.portal, TEST_USER_ID, ["Manager"]) interface.alsoProvides(self.portal.REQUEST, IDiscussionLayer) + # Set the portal timezone to something non-utc + reg_key = "plone.portal_timezone" + registry = getUtility(IRegistry) + registry[reg_key] = "Europe/Berlin" + self.typetool = self.portal.portal_types self.portal_discussion = getToolByName( self.portal, @@ -90,23 +96,25 @@ class ConversationTest(unittest.TestCase): new_id = conversation.addComment(comment) - # Check that comments have timezones - self.assertTrue(comment.creation_date.tzinfo) - self.assertTrue(comment.modification_date.tzinfo) + # Check that comments have the correct portal timezones + self.assertTrue(comment.creation_date.tzinfo, + tz.gettz("Europe/Berlin")) + self.assertTrue(comment.modification_date.tzinfo, + tz.gettz("Europe/Berlin")) # Remove the timezone from the comment dates - comment.creation_date = comment.creation_date.replace(tzinfo=None) - comment.modification_date = comment.modification_date.replace(tzinfo=None) - - # Check that the date is still correct + comment.creation_date = datetime.utcnow() + comment.modification_date = datetime.utcnow() + + # Check that the timezone naive date is converted to UTC + # See https://github.com/plone/plone.app.discussion/pull/204 self.assertTrue( conversation.last_comment_date - - datetime.now().astimezone(tz.gettz(default_timezone())) + - datetime.now().astimezone(timezone.utc) < timedelta(seconds=1), ) - # Check that comments still have timezones - self.assertTrue(comment.creation_date.tzinfo) - self.assertTrue(comment.modification_date.tzinfo) + self.assertTrue(comment.creation_date.tzinfo, timezone.utc) + self.assertTrue(comment.modification_date.tzinfo, timezone.utc) def test_private_comment(self): conversation = IConversation(self.portal.doc1) diff --git a/plone/app/discussion/tests/test_indexers.py b/plone/app/discussion/tests/test_indexers.py index 4e11c0e..79c81a0 100644 --- a/plone/app/discussion/tests/test_indexers.py +++ b/plone/app/discussion/tests/test_indexers.py @@ -138,7 +138,7 @@ class CommentIndexersTest(unittest.TestCase): # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in - # Get the default timezone from the portal + # Set the portal timezone to something non-utc reg_key = "plone.portal_timezone" registry = getUtility(IRegistry) registry[reg_key] = "Europe/Berlin"